Decompiled source of Steelhammer DSPCompatibility v2.1.17

BepInEx/plugins/Steelhammer_DSPCompatibility/Steelhammer_DSPCompatibility.dll

Decompiled 2 days ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("SteelhammerEssentialsDynamicStoragePilesPatch")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SteelhammerEssentialsDynamicStoragePilesPatch")]
[assembly: AssemblyTitle("SteelhammerEssentialsDynamicStoragePilesPatch")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SteelhammerEssentialsDynamicStoragePilesPatch
{
	[BepInPlugin("dylan.steelhammer.dynamicstoragepilespatch", "SteelhammerEssentials Dynamic Storage Piles Patch", "2.1.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "dylan.steelhammer.dynamicstoragepilespatch";

		public const string PluginName = "SteelhammerEssentials Dynamic Storage Piles Patch";

		public const string PluginVersion = "2.1.2";

		private Harmony harmony;

		private static Type visualStackType;

		private static FieldInfo visualStackInventoryField;

		private static MethodInfo visualStackSetVisualsMethod;

		private static MethodInfo slotsUsedPercentageMethod;

		private void Awake()
		{
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Expected O, but got Unknown
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Expected O, but got Unknown
			if (!Chainloader.PluginInfos.ContainsKey("com.maxsch.valheim.DynamicStoragePiles"))
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Dynamic Storage Piles is not installed. Patch will remain inactive.");
				return;
			}
			visualStackType = AccessTools.TypeByName("DynamicStoragePiles.VisualStack");
			if (visualStackType == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not find DynamicStoragePiles.VisualStack.");
				return;
			}
			visualStackInventoryField = AccessTools.Field(visualStackType, "inventory");
			visualStackSetVisualsMethod = AccessTools.Method(visualStackType, "SetVisualsActive", (Type[])null, (Type[])null);
			slotsUsedPercentageMethod = AccessTools.Method(typeof(Inventory), "SlotsUsedPercentage", (Type[])null, (Type[])null);
			harmony = new Harmony("dylan.steelhammer.dynamicstoragepilespatch");
			if (visualStackSetVisualsMethod != null)
			{
				harmony.Patch((MethodBase)visualStackSetVisualsMethod, new HarmonyMethod(typeof(Plugin), "CorrectTransientEmptyVisual", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"SteelhammerEssentials Dynamic Storage Piles Patch v2.1.1 loaded.");
		}

		private static void CorrectTransientEmptyVisual(object __instance, ref float fillPercentage)
		{
			if (__instance == null || visualStackInventoryField == null || slotsUsedPercentageMethod == null)
			{
				return;
			}
			try
			{
				object? value = visualStackInventoryField.GetValue(__instance);
				Inventory val = (Inventory)((value is Inventory) ? value : null);
				if (val != null)
				{
					float num = Convert.ToSingle(slotsUsedPercentageMethod.Invoke(val, null));
					if (num > 0f || fillPercentage <= 0f)
					{
						fillPercentage = num;
					}
					MonoBehaviour val2 = (MonoBehaviour)((__instance is MonoBehaviour) ? __instance : null);
					if ((Object)(object)val2 != (Object)null)
					{
						val2.StartCoroutine(RefreshVisualsNextFrame(__instance));
					}
				}
			}
			catch
			{
			}
		}

		private static IEnumerator RefreshVisualsNextFrame(object instance)
		{
			yield return null;
			try
			{
				if (instance != null && !(visualStackInventoryField == null) && !(visualStackSetVisualsMethod == null) && !(slotsUsedPercentageMethod == null))
				{
					object? value = visualStackInventoryField.GetValue(instance);
					Inventory val = (Inventory)((value is Inventory) ? value : null);
					if (val != null)
					{
						float num = Convert.ToSingle(slotsUsedPercentageMethod.Invoke(val, null));
						visualStackSetVisualsMethod.Invoke(instance, new object[1] { num });
					}
				}
			}
			catch
			{
			}
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}
	}
}