Decompiled source of SeeHostInventoryStash v1.0.3

HostInventoryStash.dll

Decompiled 9 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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using HostInventoryStash.Services;
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("OutwardModTemplate")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OutwardModTemplate")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c5450fe0-edcf-483f-b9ea-4b1ef9d36da7")]
[assembly: AssemblyFileVersion("1.0.3")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.0")]
[module: UnverifiableCode]
namespace HostInventoryStash
{
	[BepInPlugin("theGrungler.HostInventoryStash", "HostInventoryStash", "1.0.3")]
	public class HostInventoryStash : BaseUnityPlugin
	{
		public const string GUID = "theGrungler.HostInventoryStash";

		public const string NAME = "HostInventoryStash";

		public const string VERSION = "1.0.3";

		public const string DISPLAY_NAME = "Host Inventory Stash";

		internal static ManualLogSource Log;

		private static Harmony _harmony;

		internal static InventoryStashService inventoryStashService;

		public static ConfigEntry<bool> ShowStash;

		public static ConfigEntry<bool> ShowStashOutsideOfTown;

		internal void Awake()
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogMessage((object)"Starting HostInventoryStash 1.0.3!");
			InitializeConfig();
			_harmony = new Harmony("theGrungler.HostInventoryStash");
			_harmony.PatchAll();
			inventoryStashService = new InventoryStashService();
		}

		internal void OnDestroy()
		{
			inventoryStashService.deleteStash();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Log.LogMessage((object)"Destoryed");
			((BaseUnityPlugin)this).Config.Clear();
		}

		private void InitializeConfig()
		{
			ShowStash = ((BaseUnityPlugin)this).Config.Bind<bool>("Host Inventory Stash", "Show Host inventory stash", true, "Allows host's stash to be shown while in inventory");
			ShowStashOutsideOfTown = ((BaseUnityPlugin)this).Config.Bind<bool>("Host Inventory Stash", "Show outside of town", false, "Allows Host's stash to be accessable while out of town");
		}

		internal void Update()
		{
		}
	}
}
namespace HostInventoryStash.Services
{
	internal class InventoryStashService
	{
		public static readonly HashSet<AreaEnum> stashAreas = new HashSet<AreaEnum>
		{
			(AreaEnum)100,
			(AreaEnum)200,
			(AreaEnum)500,
			(AreaEnum)300,
			(AreaEnum)400,
			(AreaEnum)601
		};

		private Character _character;

		private const string _inventoryDisplayPath = "Canvas/GameplayPanels/Menus/CharacterMenus/MainPanel/Content/MiddlePanel/Inventory/Content/SectionContent";

		private ContainerDisplay storedStashDisplay;

		private int timesCalled = 0;

		private CharacterUI _characterUI => _character.CharacterUI;

		public bool GetAreaContainsStash()
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			AreaEnum area;
			return TryGetCurrentAreaEnum(out area) && stashAreas.Contains(area);
		}

		public int Start()
		{
			timesCalled++;
			return timesCalled;
		}

		public void createStashMenu(CharacterUI characterUI, MenuScreens menu, Item item)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Invalid comparison between Unknown and I4
			if ((int)menu == 2)
			{
				ToggleStashes(characterUI);
			}
		}

		private void ToggleStashes(CharacterUI _instance)
		{
			InventoryContentDisplay component = ((Component)((Component)_instance).transform.Find("Canvas/GameplayPanels/Menus/CharacterMenus/MainPanel/Content/MiddlePanel/Inventory/Content/SectionContent")).GetComponent<InventoryContentDisplay>();
			AddStashDisplay(_instance, component);
		}

		private void AddStashDisplay(CharacterUI _instance, InventoryContentDisplay inventoryContentDisplay)
		{
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Expected O, but got Unknown
			string gameObjectPath = UnityEngineExtensions.GetGameObjectPath(((Component)inventoryContentDisplay).transform);
			if (gameObjectPath.EndsWith("Canvas/GameplayPanels/Menus/CharacterMenus/MainPanel/Content/MiddlePanel/Inventory/Content/SectionContent"))
			{
				if ((!GetAreaContainsStash() && !HostInventoryStash.ShowStashOutsideOfTown.Value) || !HostInventoryStash.ShowStash.Value)
				{
					deleteStash();
				}
				else if ((Object)(object)storedStashDisplay == (Object)null)
				{
					HostInventoryStash.Log.LogInfo((object)"creating new stash display");
					RectTransform val = inventoryContentDisplay.m_overrideContentHolder;
					if ((Object)(object)val == (Object)null)
					{
						ScrollRect inventoriesScrollRect = inventoryContentDisplay.m_inventoriesScrollRect;
						val = ((inventoriesScrollRect != null) ? inventoriesScrollRect.content : null);
					}
					RectTransform containerDisplayPrefab = inventoryContentDisplay.ContainerDisplayPrefab;
					ContainerDisplay component = ((Component)Object.Instantiate<RectTransform>(containerDisplayPrefab)).GetComponent<ContainerDisplay>();
					((Component)component).transform.SetParent((Transform)(object)val);
					UnityEngineExtensions.ResetLocal(((Component)component).transform, true);
					((Object)component).name = "HostStashDisplay";
					component.SetName("Host Display");
					Transform val2 = ((Component)component).transform.Find("Header");
					if ((Object)(object)val2 != (Object)null)
					{
						RectTransform val3 = (RectTransform)((Component)val2).transform.Find("lblWeight");
						if ((Object)(object)val3 != (Object)null)
						{
							Object.Destroy((Object)(object)((Component)val3).gameObject);
						}
						Transform val4 = ((Component)val2).transform.Find("iconWeight");
						if ((Object)(object)val4 != (Object)null)
						{
							Object.Destroy((Object)(object)((Component)val4).gameObject);
						}
					}
					storedStashDisplay = component;
					ShowStashPanel(_instance);
				}
				else
				{
					HostInventoryStash.Log.LogInfo((object)"Stash Display already exists");
					ShowStashPanel(_instance);
				}
			}
			else
			{
				HostInventoryStash.Log.LogInfo((object)"not an Inventory?");
			}
		}

		private void ShowStashPanel(CharacterUI instance)
		{
			HostInventoryStash.Log.LogInfo((object)$"Stash in Area: {GetAreaContainsStash()}");
			Character worldHostCharacter = CharacterManager.Instance.GetWorldHostCharacter();
			Character firstLocalCharacter = CharacterManager.Instance.GetFirstLocalCharacter();
			ItemContainer stash = firstLocalCharacter.Stash;
			ItemContainer stash2 = worldHostCharacter.Stash;
			StashPanel stashPanel = firstLocalCharacter.CharacterUI.StashPanel;
			stashPanel.SetStash(stash2);
			stash2.ShowContent(firstLocalCharacter);
			storedStashDisplay.SetReferencedContainer(stash2);
		}

		public void deleteStash()
		{
			if (Object.op_Implicit((Object)(object)storedStashDisplay))
			{
				((Component)storedStashDisplay).gameObject.SetActive(false);
				Object.Destroy((Object)(object)((Component)storedStashDisplay).gameObject);
			}
		}

		public static bool TryGetCurrentAreaEnum(out AreaEnum area)
		{
			area = (AreaEnum)0;
			AreaManager instance = AreaManager.Instance;
			string text = ((instance == null) ? null : instance.CurrentArea?.SceneName);
			if (string.IsNullOrEmpty(text))
			{
				return false;
			}
			area = (AreaEnum)AreaManager.Instance.GetAreaIndexFromSceneName(text);
			return true;
		}

		public void ConfigureStashDisplay(InventoryContentDisplay inventoryContentDisplay)
		{
			if (!((Object)(object)storedStashDisplay == (Object)null))
			{
				ItemFilter filter = inventoryContentDisplay.m_filter;
				ItemFilter exceptionFilter = inventoryContentDisplay.m_exceptionFilter;
				((ItemListDisplay)storedStashDisplay).SetFilter(filter);
				((ItemListDisplay)storedStashDisplay).SetExceptionFilter(exceptionFilter);
				storedStashDisplay.Refresh();
				ManualLogSource log = HostInventoryStash.Log;
				string[] obj = new string[5] { "Configure filters for stash display ", null, null, null, null };
				ContainerDisplay obj2 = storedStashDisplay;
				obj[1] = ((obj2 != null) ? ((Object)obj2).name : null);
				obj[2] = " - '";
				obj[3] = UnityEngineExtensions.GetGameObjectPath(((Component)storedStashDisplay).transform);
				obj[4] = "'.";
				log.LogInfo((object)string.Concat(obj));
			}
		}
	}
}
namespace HostInventoryStash.Patches
{
	[HarmonyPatch(typeof(InventoryContentDisplay))]
	internal class InventoryContentDisplayPatches
	{
		[HarmonyPatch("RefreshContainerDisplays")]
		[HarmonyPatch(new Type[] { typeof(bool) })]
		[HarmonyPostfix]
		private static void RefreshContainerDisplaysPostfix(InventoryContentDisplay __instance, bool _clearAssignedDisplay)
		{
			try
			{
				HostInventoryStash.Log.LogInfo((object)"Refreshing Container Display");
				HostInventoryStash.inventoryStashService.ConfigureStashDisplay(__instance);
			}
			catch (Exception)
			{
				HostInventoryStash.Log.LogInfo((object)"Failed To Refresh Container Display");
			}
		}
	}
	[HarmonyPatch(typeof(CharacterUI))]
	internal static class CharacterUiPatches
	{
		public delegate void ShowMenuDelegate(CharacterUI characterUI, MenuScreens menu, Item item);

		public static event ShowMenuDelegate BeforeShowMenu;

		[HarmonyPatch("ShowMenu")]
		[HarmonyPatch(new Type[]
		{
			typeof(MenuScreens),
			typeof(Item)
		})]
		[HarmonyPrefix]
		private static void ShowMenuPrefix(CharacterUI __instance, MenuScreens _menu, Item _item)
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Invalid comparison between Unknown and I4
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (!((Object)(object)__instance.TargetCharacter == (Object)null) && !((Object)(object)__instance.TargetCharacter.OwnerPlayerSys == (Object)null) && __instance.TargetCharacter.IsLocalPlayer && (int)_menu == 2)
				{
					HostInventoryStash.inventoryStashService.createStashMenu(__instance, _menu, _item);
				}
			}
			catch (Exception)
			{
				HostInventoryStash.Log.LogInfo((object)"Failed to Run Open Inventory Is Menu Focused");
			}
		}
	}
}