Decompiled source of ContentsWithin v2.1.4

ContentsWithin.dll

Decompiled 7 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ContentsWithin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ContentsWithin")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a12ea987-2ff3-4af8-8745-24bae44b63bd")]
[assembly: AssemblyFileVersion("2.1.4")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.1.4.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace ContentsWithin
{
	[BepInPlugin("com.maxsch.valheim.contentswithin", "ContentsWithin", "2.1.4")]
	public class ContentsWithin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Player))]
		public class PlayerPatch
		{
			[HarmonyPatch("UpdateHover")]
			[HarmonyPostfix]
			public static void UpdateHoverPostfix(Player __instance)
			{
				if (isModEnabled.Value && !((Object)(object)lastHoverObject == (Object)(object)__instance.m_hovering))
				{
					lastHoverObject = __instance.m_hovering;
					lastHoverContainer = (Object.op_Implicit((Object)(object)lastHoverObject) ? lastHoverObject.GetComponentInParent<Container>() : null);
				}
			}
		}

		[HarmonyPatch(typeof(InventoryGrid))]
		public static class InventoryGridPatch
		{
			[HarmonyPatch("Awake")]
			[HarmonyPostfix]
			public static void AwakePostfix(InventoryGrid __instance)
			{
				initializedGrids.Add(__instance);
			}
		}

		[HarmonyPatch(typeof(InventoryGui))]
		public class InventoryGuiPatch
		{
			[HarmonyPatch("Awake")]
			[HarmonyPostfix]
			[HarmonyPriority(200)]
			public static void AwakePostfix(ref InventoryGui __instance)
			{
				RectTransform obj = __instance.m_player.Ref<RectTransform>();
				inventoryPanel = ((obj != null) ? ((Component)obj).gameObject : null);
				Transform obj2 = __instance.m_infoPanel.Ref<Transform>();
				infoPanel = ((obj2 != null) ? ((Component)obj2).gameObject : null);
				Transform obj3 = __instance.m_inventoryRoot.Find("Crafting").Ref<Transform>();
				craftingPanel = ((obj3 != null) ? ((Component)obj3).gameObject : null);
				Button obj4 = __instance.m_takeAllButton.Ref<Button>();
				takeAllButton = ((obj4 != null) ? ((Component)obj4).gameObject : null);
				Button obj5 = __instance.m_stackAllButton.Ref<Button>();
				stackAllButton = ((obj5 != null) ? ((Component)obj5).gameObject : null);
				if (Chainloader.PluginInfos.ContainsKey("randyknapp.mods.auga"))
				{
					Transform obj6 = __instance.m_inventoryRoot.Find("RightPanel").Ref<Transform>();
					craftingPanel = ((obj6 != null) ? ((Component)obj6).gameObject : null);
				}
			}

			[HarmonyPatch("Show")]
			[HarmonyPostfix]
			public static void ShowPostfix()
			{
				isRealGuiVisible = true;
			}

			[HarmonyPatch("Hide")]
			[HarmonyPostfix]
			public static void HidePostfix()
			{
				isRealGuiVisible = false;
			}

			[HarmonyPatch("Update")]
			[HarmonyPrefix]
			public static void UpdatePrefix(InventoryGui __instance)
			{
				if (!ShowRealGUI())
				{
					__instance.m_animator.SetBool("visible", false);
				}
			}

			[HarmonyPatch("Update")]
			[HarmonyPostfix]
			public static void UpdatePostfix(InventoryGui __instance)
			{
				//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d4: Expected O, but got Unknown
				GameObject obj = inventoryPanel.Ref<GameObject>();
				if (obj != null)
				{
					obj.SetActive(ShowRealGUI());
				}
				GameObject obj2 = craftingPanel.Ref<GameObject>();
				if (obj2 != null)
				{
					obj2.SetActive(ShowRealGUI());
				}
				GameObject obj3 = infoPanel.Ref<GameObject>();
				if (obj3 != null)
				{
					obj3.SetActive(ShowRealGUI());
				}
				GameObject obj4 = takeAllButton.Ref<GameObject>();
				if (obj4 != null)
				{
					obj4.SetActive(ShowRealGUI());
				}
				GameObject obj5 = stackAllButton.Ref<GameObject>();
				if (obj5 != null)
				{
					obj5.SetActive(ShowRealGUI());
				}
				if (Object.op_Implicit((Object)(object)takeAllButton) && Chainloader.PluginInfos.ContainsKey("goldenrevolver.quick_stack_store"))
				{
					bool flag = false;
					foreach (Transform item in takeAllButton.transform.parent)
					{
						Transform val = item;
						switch (((Object)val).name)
						{
						case "quickStackToContainerButton":
							flag = true;
							((Component)val).gameObject.SetActive(ShowRealGUI());
							break;
						case "storeAllButton":
						case "sortContainerButton":
						case "restockFromContainerButton":
							((Component)val).gameObject.SetActive(ShowRealGUI());
							break;
						}
					}
					if (flag)
					{
						GameObject obj6 = stackAllButton.Ref<GameObject>();
						if (obj6 != null)
						{
							obj6.SetActive(false);
						}
					}
				}
				if (!ShowRealGUI())
				{
					if (HasContainerAccess(lastHoverContainer))
					{
						ShowPreviewContainer(lastHoverContainer.GetInventory());
						delayTime = openDelayTime.Value;
					}
					else if (delayTime > 0f)
					{
						ShowPreviewContainer(emptyInventory);
						delayTime -= Time.deltaTime;
					}
					else
					{
						InventoryGui.instance.m_animator.SetBool("visible", false);
						delayTime = 0f;
					}
				}
			}

			private static bool HasContainerAccess(Container container)
			{
				//IL_0019: Unknown result type (might be due to invalid IL or missing references)
				if (!Object.op_Implicit((Object)(object)container))
				{
					return false;
				}
				bool flag = PrivateArea.CheckAccess(((Component)container).transform.position, 0f, false, false);
				bool flag2 = container.CheckAccess(Game.instance.m_playerProfile.m_playerID);
				return flag && flag2;
			}

			[HarmonyPatch("SetupDragItem")]
			[HarmonyPrefix]
			public static bool SetupDragItemPrefix()
			{
				return ShowRealGUI();
			}

			private static void ShowPreviewContainer(Inventory container)
			{
				InventoryGui.instance.m_animator.SetBool("visible", true);
				InventoryGui.instance.m_hiddenFrames = 10;
				((Component)InventoryGui.instance.m_container).gameObject.SetActive(true);
				if (initializedGrids.Contains(InventoryGui.instance.m_containerGrid))
				{
					InventoryGui.instance.m_containerGrid.UpdateInventory(container, (Player)null, (ItemData)null);
					InventoryGui.instance.m_containerGrid.ResetView();
					InventoryGui.instance.m_containerName.text = Localization.instance.Localize(container.GetName());
					int num = Mathf.CeilToInt(container.GetTotalWeight());
					InventoryGui.instance.m_containerWeight.text = num.ToString();
				}
			}
		}

		public const string PluginGUID = "com.maxsch.valheim.contentswithin";

		public const string PluginName = "ContentsWithin";

		public const string PluginVersion = "2.1.4";

		private static ConfigEntry<bool> isModEnabled;

		private static ConfigEntry<bool> startHidden;

		private static ConfigEntry<KeyboardShortcut> toggleShowContentsShortcut;

		private static ConfigEntry<float> openDelayTime;

		private static bool isRealGuiVisible;

		private static bool showContent = true;

		private static float delayTime;

		private static Inventory emptyInventory = new Inventory("", (Sprite)null, 0, 0);

		private static HashSet<InventoryGrid> initializedGrids = new HashSet<InventoryGrid>();

		private static Container lastHoverContainer;

		private static GameObject lastHoverObject;

		private static GameObject inventoryPanel;

		private static GameObject infoPanel;

		private static GameObject craftingPanel;

		private static GameObject takeAllButton;

		private static GameObject stackAllButton;

		private Harmony harmony;

		public void Awake()
		{
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Expected O, but got Unknown
			isModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("_Global", "isModEnabled", true, "Globally enable or disable this mod. When disabled, no container contents will be shown and hotkeys will not work.");
			startHidden = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "startHidden", false, "Hide the container contents until the hotkey is pressed.");
			openDelayTime = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "openDelayTime", 0.3f, "Time before the UI is closed when not hovering over a chest. This reduces the amount of animations when switching between chests.");
			toggleShowContentsShortcut = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "toggleShowContentsShortcut", new KeyboardShortcut((KeyCode)112, (KeyCode[])(object)new KeyCode[1] { (KeyCode)303 }), "Shortcut to toggle on/off the 'show container contents' feature.");
			showContent = !startHidden.Value;
			harmony = new Harmony("com.maxsch.valheim.contentswithin");
			harmony.PatchAll();
		}

		private void Update()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			if (!isModEnabled.Value)
			{
				return;
			}
			KeyboardShortcut value = toggleShowContentsShortcut.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				showContent = !showContent;
				if (Object.op_Implicit((Object)(object)MessageHud.instance))
				{
					MessageHud.instance.ShowMessage((MessageType)2, $"ShowContainerContents: {showContent}", 0, (Sprite)null);
				}
				if (!showContent && !isRealGuiVisible && Object.op_Implicit((Object)(object)InventoryGui.instance))
				{
					InventoryGui.instance.Hide();
				}
			}
		}

		private static bool ShowRealGUI()
		{
			return !isModEnabled.Value || !showContent || isRealGuiVisible;
		}
	}
	public static class ObjectExtensions
	{
		public static T Ref<T>(this T o) where T : Object
		{
			return Object.op_Implicit((Object)(object)o) ? o : default(T);
		}
	}
}