Decompiled source of EZInventory MONO v1.0.2

Mods/EZInventory_mono.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using EZInventory;
using EZInventory.Utils;
using HarmonyLib;
using MelonLoader;
using MelonLoader.Preferences;
using ScheduleOne;
using ScheduleOne.ItemFramework;
using ScheduleOne.UI;
using ScheduleOne.UI.Items;
using UnityEngine;
using UnityEngine.Events;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(EZInventoryMod), "EZInventory", "1.0.2", "Kaen01", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("EZInventory")]
[assembly: AssemblyConfiguration("Mono")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EZInventory")]
[assembly: AssemblyTitle("EZInventory")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace EZInventory
{
	public class EZInventoryMod : MelonMod
	{
		public static MelonPreferences_Category Config;

		public static MelonPreferences_Entry<string> GrabAllKey;

		public static MelonPreferences_Entry<bool> GrabAllAutoClose;

		public static MelonPreferences_Entry<string> DepositAllKey;

		public static MelonPreferences_Entry<bool> DepositAllAutoClose;

		public override void OnInitializeMelon()
		{
			((MelonBase)this).OnInitializeMelon();
			MelonLogger.Msg("EZInventory loaded!");
			Config = MelonPreferences.CreateCategory("EZInventory", "EZInventory Settings");
			GrabAllKey = Config.CreateEntry<string>("GrabAllKey", "G", (string)null, "Hotkey for Grab All (case-insensitive).", false, false, (ValueValidator)null, (string)null);
			MelonLogger.Msg("EZInventory: GrabAllKey set to '" + GrabAllKey.Value + "'");
			DepositAllKey = Config.CreateEntry<string>("DepositAllKey", "H", (string)null, "Hotkey for Deposit All (case-insensitive).", false, false, (ValueValidator)null, (string)null);
			MelonLogger.Msg("EZInventory: DepositAllKey set to '" + DepositAllKey.Value + "'");
			MelonPreferences.Save();
		}
	}
}
namespace EZInventory.Utils
{
	internal static class EZInventoryUtils
	{
		public static void MoveSlotContents(ItemUIManager mgr, ItemSlot source, List<ItemSlot> dest)
		{
			if (source.ItemInstance == null)
			{
				return;
			}
			int num = source.Quantity;
			int num2 = 0;
			foreach (ItemSlot item in dest)
			{
				if (num <= 0)
				{
					break;
				}
				if (item.ItemInstance != null && item.ItemInstance.CanStackWith(source.ItemInstance, false))
				{
					int num3 = Math.Min(item.GetCapacityForItem(source.ItemInstance, false), num);
					if (num3 > 0)
					{
						item.AddItem(source.ItemInstance.GetCopy(num3), false);
						num -= num3;
						num2 += num3;
					}
				}
			}
			foreach (ItemSlot item2 in dest)
			{
				if (num <= 0)
				{
					break;
				}
				int num4 = Math.Min(item2.GetCapacityForItem(source.ItemInstance, false), num);
				if (num4 > 0)
				{
					item2.AddItem(source.ItemInstance.GetCopy(num4), false);
					num -= num4;
					num2 += num4;
				}
			}
			if (num2 > 0)
			{
				if (num <= 0)
				{
					source.ClearStoredInstance(false);
				}
				else
				{
					source.ChangeQuantity(-num2, false);
				}
				UnityEvent onItemMoved = mgr.onItemMoved;
				if (onItemMoved != null)
				{
					onItemMoved.Invoke();
				}
			}
		}
	}
}
namespace EZInventory.HarmonyPatches
{
	[HarmonyPatch(typeof(ItemUIManager), "SlotClicked")]
	internal class EZInventory_MoveByType_Patch
	{
		private static readonly FieldInfo primaryField = AccessTools.Field(typeof(ItemUIManager), "PrimarySlots");

		private static readonly FieldInfo secondaryField = AccessTools.Field(typeof(ItemUIManager), "SecondarySlots");

		private static bool Prefix(ItemUIManager __instance, ItemSlotUI ui)
		{
			bool button = GameInput.GetButton((ButtonCode)23);
			bool flag = Input.GetKey((KeyCode)306) || Input.GetKey((KeyCode)305);
			if (!(button && flag))
			{
				return true;
			}
			if (!__instance.QuickMoveEnabled)
			{
				return true;
			}
			ItemSlot assignedSlot = ui.assignedSlot;
			ItemInstance itemInstance = assignedSlot.ItemInstance;
			if (itemInstance == null)
			{
				return true;
			}
			if (itemInstance.Definition.Name == "Cash")
			{
				return true;
			}
			List<ItemSlot> list = primaryField.GetValue(__instance) as List<ItemSlot>;
			List<ItemSlot> list2 = secondaryField.GetValue(__instance) as List<ItemSlot>;
			bool flag2 = list.Contains(assignedSlot);
			List<ItemSlot> list3 = (flag2 ? list : list2);
			List<ItemSlot> dest = (flag2 ? list2 : list);
			List<ItemSlot> list4 = new List<ItemSlot>();
			foreach (ItemSlot item in list3)
			{
				if (item.ItemInstance != null && (Object)(object)item.ItemInstance.Definition == (Object)(object)itemInstance.Definition)
				{
					list4.Add(item);
				}
			}
			foreach (ItemSlot item2 in list4)
			{
				EZInventoryUtils.MoveSlotContents(__instance, item2, dest);
			}
			UnityEvent onItemMoved = __instance.onItemMoved;
			if (onItemMoved != null)
			{
				onItemMoved.Invoke();
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(ItemUIManager), "Update")]
	internal class EZInventory_ShiftDragMove_Patch
	{
		private static HashSet<ItemSlotUI> processed = new HashSet<ItemSlotUI>();

		private static readonly FieldInfo primaryField = AccessTools.Field(typeof(ItemUIManager), "PrimarySlots");

		private static readonly FieldInfo secondaryField = AccessTools.Field(typeof(ItemUIManager), "SecondarySlots");

		private static void Postfix(ItemUIManager __instance)
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			if (!__instance.QuickMoveEnabled)
			{
				return;
			}
			if (EZInventoryMod.GrabAllKey != null)
			{
				string text = EZInventoryMod.GrabAllKey.Value.ToUpperInvariant();
				if (Enum.TryParse<KeyCode>(text, ignoreCase: true, out KeyCode result))
				{
					if (Input.GetKeyDown(result))
					{
						TakeEverything(__instance);
					}
				}
				else
				{
					MelonLogger.Warning("EZInventory: Invalid GrabAllKey '" + text + "'");
				}
			}
			if (EZInventoryMod.DepositAllKey != null)
			{
				string text2 = EZInventoryMod.DepositAllKey.Value.ToUpperInvariant();
				if (Enum.TryParse<KeyCode>(text2, ignoreCase: true, out KeyCode result2))
				{
					if (Input.GetKeyDown(result2))
					{
						DepositAll(__instance);
					}
				}
				else
				{
					MelonLogger.Warning("EZInventory: Invalid DepositAllKey '" + text2 + "'");
				}
			}
			bool button = GameInput.GetButton((ButtonCode)23);
			bool button2 = GameInput.GetButton((ButtonCode)0);
			if (button && button2)
			{
				ItemSlotUI hoveredSlot = __instance.HoveredSlot;
				if ((Object)(object)hoveredSlot != (Object)null && !processed.Contains(hoveredSlot))
				{
					processed.Add(hoveredSlot);
					QuickMoveSlot(__instance, hoveredSlot);
				}
			}
			else
			{
				processed.Clear();
			}
		}

		private static void QuickMoveSlot(ItemUIManager mgr, ItemSlotUI ui)
		{
			if (!mgr.CanDragFromSlot(ui))
			{
				return;
			}
			ItemSlot assignedSlot = ui.assignedSlot;
			if (assignedSlot.ItemInstance != null && assignedSlot.ItemInstance.Definition.Name == "Cash")
			{
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(typeof(ItemUIManager), "GetQuickMoveSlots", (Type[])null, (Type[])null);
			if (!(methodInfo.Invoke(mgr, new object[1] { assignedSlot }) is List<ItemSlot> list) || list.Count == 0)
			{
				return;
			}
			int num = 0;
			foreach (ItemSlot item in list)
			{
				if (num >= assignedSlot.Quantity)
				{
					break;
				}
				if (item.ItemInstance != null && item.ItemInstance.CanStackWith(assignedSlot.ItemInstance, false))
				{
					int num2 = Mathf.Min(item.GetCapacityForItem(assignedSlot.ItemInstance, false), assignedSlot.Quantity - num);
					if (num2 > 0)
					{
						item.AddItem(assignedSlot.ItemInstance.GetCopy(num2), false);
						num += num2;
					}
				}
			}
			foreach (ItemSlot item2 in list)
			{
				if (num >= assignedSlot.Quantity)
				{
					break;
				}
				int num3 = Mathf.Min(item2.GetCapacityForItem(assignedSlot.ItemInstance, false), assignedSlot.Quantity - num);
				if (num3 > 0)
				{
					item2.AddItem(assignedSlot.ItemInstance.GetCopy(num3), false);
					num += num3;
				}
			}
			if (num > 0)
			{
				assignedSlot.ChangeQuantity(-num, false);
				UnityEvent onItemMoved = mgr.onItemMoved;
				if (onItemMoved != null)
				{
					onItemMoved.Invoke();
				}
			}
		}

		private static void TakeEverything(ItemUIManager mgr)
		{
			if (!mgr.QuickMoveEnabled)
			{
				return;
			}
			List<ItemSlot> list = primaryField.GetValue(mgr) as List<ItemSlot>;
			List<ItemSlot> list2 = secondaryField.GetValue(mgr) as List<ItemSlot>;
			List<ItemSlot> list3 = list;
			List<ItemSlot> list4 = list2;
			if (list3 == null || list4 == null)
			{
				return;
			}
			foreach (ItemSlot item in list4)
			{
				if (item.ItemInstance != null && !(item.ItemInstance.Definition.Name == "Cash"))
				{
					EZInventoryUtils.MoveSlotContents(mgr, item, list3);
				}
			}
			UnityEvent onItemMoved = mgr.onItemMoved;
			if (onItemMoved != null)
			{
				onItemMoved.Invoke();
			}
		}

		private static void DepositAll(ItemUIManager mgr)
		{
			if (!mgr.QuickMoveEnabled)
			{
				return;
			}
			List<ItemSlot> list = primaryField.GetValue(mgr) as List<ItemSlot>;
			List<ItemSlot> list2 = secondaryField.GetValue(mgr) as List<ItemSlot>;
			List<ItemSlot> list3 = list;
			List<ItemSlot> list4 = list2;
			if (list3 == null || list4 == null)
			{
				return;
			}
			foreach (ItemSlot item in list3)
			{
				if (item.ItemInstance != null && !(item.ItemInstance.Definition.Name == "Cash"))
				{
					EZInventoryUtils.MoveSlotContents(mgr, item, list4);
				}
			}
			UnityEvent onItemMoved = mgr.onItemMoved;
			if (onItemMoved != null)
			{
				onItemMoved.Invoke();
			}
		}
	}
}