Decompiled source of BackpackDrawerFix v1.0.1

Cjayride.BackpackDrawerFix.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyCompany("Cjayride.BackpackDrawerFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("Cjayride.BackpackDrawerFix")]
[assembly: AssemblyTitle("Cjayride.BackpackDrawerFix")]
[assembly: AssemblyVersion("1.0.1.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 Cjayride.BackpackDrawerFix
{
	[BepInPlugin("cjayride.BackpackDrawerFix", "Backpack Drawer Fix", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "cjayride.BackpackDrawerFix";

		public const string PluginName = "Backpack Drawer Fix";

		public const string PluginVersion = "1.0.1";

		public static ConfigEntry<bool> DepositFromBackpack;

		private void Awake()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			DepositFromBackpack = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DepositFromBackpack", false, "When enabled, Shift+Interact (store all) also deposits matching items from the equipped Adventure Backpack. Off by default.");
			new Harmony("cjayride.BackpackDrawerFix").PatchAll();
			if (GiveToPlayerScope.TargetMethod() == null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Grillspett Item Drawers is not loaded; no withdraw patch applied.");
			}
			else if (BackpackInventory.Available)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Drawer withdraw leftover now counts the equipped Adventure Backpack, so matching stacks are not duplicated.");
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Grillspett Item Drawers is loaded, but Adventure Backpacks is not; withdraw uses the original leftover count.");
			}
			if (DepositFromBackpackPatch.TargetMethod() != null && BackpackInventory.Available)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Shift+Interact can also store matching backpack items when DepositFromBackpack is enabled.");
			}
		}
	}
	internal static class BackpackInventory
	{
		private static readonly MethodInfo GetEquippedBackpack = AccessTools.Method("AdventureBackpacks.Extensions.PlayerExtensions:GetEquippedBackpack", new Type[1] { typeof(Player) }, (Type[])null);

		internal static bool Available => GetEquippedBackpack != null;

		internal static Inventory GetEquipped(Player player)
		{
			if ((Object)(object)player == (Object)null || GetEquippedBackpack == null)
			{
				return null;
			}
			object obj = GetEquippedBackpack.Invoke(null, new object[1] { player });
			if (obj == null)
			{
				return null;
			}
			MethodInfo methodInfo = AccessTools.Method(obj.GetType(), "GetInventory", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				return null;
			}
			object? obj2 = methodInfo.Invoke(obj, null);
			return (Inventory)((obj2 is Inventory) ? obj2 : null);
		}
	}
	internal static class ItemNames
	{
		private static readonly MethodInfo PrefabNameOf = AccessTools.Method("ItemDrawers.Game.ItemFacts:PrefabNameOf", (Type[])null, (Type[])null);

		private static readonly MethodInfo LocalizedName = AccessTools.Method("ItemDrawers.Game.ItemFacts:LocalizedName", (Type[])null, (Type[])null);

		private static readonly MethodInfo IsReserved = AccessTools.Method("ItemDrawers.Game.ExtraSlotsCompat:IsInReservedSlot", (Type[])null, (Type[])null);

		internal static string PrefabOf(ItemData item)
		{
			if (PrefabNameOf != null)
			{
				return PrefabNameOf.Invoke(null, new object[1] { item }) as string;
			}
			if (item == null || (Object)(object)item.m_dropPrefab == (Object)null)
			{
				return null;
			}
			return ((Object)item.m_dropPrefab).name;
		}

		internal static string DisplayName(string itemName)
		{
			if (LocalizedName != null)
			{
				string text = LocalizedName.Invoke(null, new object[1] { itemName }) as string;
				if (!string.IsNullOrEmpty(text))
				{
					return text;
				}
			}
			return itemName;
		}

		internal static bool ReservedSlot(ItemData item)
		{
			if (IsReserved == null || item == null)
			{
				return false;
			}
			return (bool)IsReserved.Invoke(null, new object[1] { item });
		}

		internal static int CountMatching(Inventory inventory, string itemName, bool skipReserved)
		{
			int num = 0;
			if (inventory == null || string.IsNullOrEmpty(itemName))
			{
				return 0;
			}
			foreach (ItemData allItem in inventory.GetAllItems())
			{
				if (allItem != null && (!skipReserved || !ReservedSlot(allItem)) && PrefabOf(allItem) == itemName)
				{
					num += allItem.m_stack;
				}
			}
			return num;
		}

		internal static int RemoveMatching(Inventory inventory, string itemName, int amount)
		{
			if (inventory == null || amount <= 0 || string.IsNullOrEmpty(itemName))
			{
				return 0;
			}
			int num = 0;
			foreach (ItemData item in new List<ItemData>(inventory.GetAllItems()))
			{
				if (num >= amount)
				{
					break;
				}
				if (item != null && !(PrefabOf(item) != itemName))
				{
					int num2 = Math.Min(item.m_stack, amount - num);
					if (inventory.RemoveItem(item, num2))
					{
						num += num2;
					}
				}
			}
			return num;
		}
	}
	[HarmonyPatch]
	internal static class GiveToPlayerScope
	{
		internal static int Depth;

		internal static MethodBase TargetMethod()
		{
			return AccessTools.Method("ItemDrawers.Game.ItemFacts:GiveToPlayer", (Type[])null, (Type[])null);
		}

		private static bool Prepare()
		{
			return TargetMethod() != null;
		}

		private static void Prefix()
		{
			Depth++;
		}

		private static void Finalizer()
		{
			if (Depth > 0)
			{
				Depth--;
			}
		}
	}
	[HarmonyPatch]
	internal static class CountWithdrawIntoBackpack
	{
		private static MethodBase TargetMethod()
		{
			return AccessTools.Method("ItemDrawers.Game.ItemFacts:CountByPrefab", (Type[])null, (Type[])null);
		}

		private static bool Prepare()
		{
			return TargetMethod() != null;
		}

		private static void Postfix(Inventory inventory, string itemName, ref int __result)
		{
			if (GiveToPlayerScope.Depth <= 0 || inventory == null || string.IsNullOrEmpty(itemName))
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null) && inventory == ((Humanoid)localPlayer).GetInventory())
			{
				Inventory equipped = BackpackInventory.GetEquipped(localPlayer);
				if (equipped != null && equipped != inventory)
				{
					__result += ItemNames.CountMatching(equipped, itemName, skipReserved: false);
				}
			}
		}
	}
	[HarmonyPatch]
	internal static class DepositFromBackpackPatch
	{
		private static readonly MethodInfo TryDepositExternally = AccessTools.Method("ItemDrawers.Game.DrawerComponent:TryDepositExternally", (Type[])null, (Type[])null);

		internal static MethodBase TargetMethod()
		{
			return AccessTools.Method("ItemDrawers.Game.DrawerComponent:DepositAllOfItemInternal", (Type[])null, (Type[])null);
		}

		private static bool Prepare()
		{
			if (TargetMethod() != null)
			{
				return TryDepositExternally != null;
			}
			return false;
		}

		private static bool Enabled()
		{
			if (Plugin.DepositFromBackpack != null && Plugin.DepositFromBackpack.Value)
			{
				return BackpackInventory.Available;
			}
			return false;
		}

		private static bool Prefix(Player player, string itemName, ref bool __result)
		{
			if (!Enabled() || (Object)(object)player == (Object)null || string.IsNullOrEmpty(itemName))
			{
				return true;
			}
			if (ItemNames.CountMatching(((Humanoid)player).GetInventory(), itemName, skipReserved: true) > 0)
			{
				return true;
			}
			if (ItemNames.CountMatching(BackpackInventory.GetEquipped(player), itemName, skipReserved: false) <= 0)
			{
				return true;
			}
			__result = true;
			return false;
		}

		private static void Postfix(object __instance, Player player, string itemName, bool announce)
		{
			if (!Enabled() || __instance == null || (Object)(object)player == (Object)null || string.IsNullOrEmpty(itemName))
			{
				return;
			}
			Inventory equipped = BackpackInventory.GetEquipped(player);
			int num = ItemNames.CountMatching(equipped, itemName, skipReserved: false);
			if (num <= 0)
			{
				return;
			}
			object[] array = new object[3] { itemName, num, 0 };
			bool num2 = (bool)TryDepositExternally.Invoke(__instance, array);
			int num3 = (int)array[2];
			if (num2 && num3 > 0)
			{
				ItemNames.RemoveMatching(equipped, itemName, num3);
				if (announce)
				{
					((Character)player).Message((MessageType)1, $"Stored {num3} {ItemNames.DisplayName(itemName)} from backpack", 0, (Sprite)null, false);
				}
			}
		}
	}
}