Decompiled source of Joive BG StorageQoL v1.0.0

BG-StorageQoL-Joive.dll

Decompiled 3 weeks ago
using System;
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 Unity.Netcode;
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("Joive")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Storage quality-of-life changes for Burglin Gnomes.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BG Storage QoL")]
[assembly: AssemblyTitle("BG-StorageQoL-Joive")]
[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 BGStorageQoLJoive
{
	[BepInPlugin("joive.bg.storageqol", "BG Storage QoL", "1.0.0")]
	public sealed class StorageQoLPlugin : BaseUnityPlugin
	{
		private const string Version = "1.0.0";

		private const int RemovedLimitStackSize = 60000;

		private static StorageQoLPlugin instance;

		private static ConfigEntry<bool> enableMod;

		private static ConfigEntry<int> storageStackLimit;

		private static ConfigEntry<bool> removeStorageLimit;

		private static ConfigEntry<bool> allowPotionStorage;

		private static ConfigEntry<bool> depositPotionsWithResources;

		private static ConfigEntry<bool> debugLogs;

		private Harmony harmony;

		internal static bool Enabled
		{
			get
			{
				if (enableMod != null)
				{
					return enableMod.Value;
				}
				return false;
			}
		}

		internal static bool PotionStorageEnabled
		{
			get
			{
				if (Enabled && allowPotionStorage != null)
				{
					return allowPotionStorage.Value;
				}
				return false;
			}
		}

		internal static bool PotionDepositEnabled
		{
			get
			{
				if (PotionStorageEnabled && depositPotionsWithResources != null)
				{
					return depositPotionsWithResources.Value;
				}
				return false;
			}
		}

		internal static int EffectiveStackLimit
		{
			get
			{
				if (removeStorageLimit != null && removeStorageLimit.Value)
				{
					return 60000;
				}
				return Math.Max(1, Math.Min(60000, (storageStackLimit != null) ? storageStackLimit.Value : 9999));
			}
		}

		private void Awake()
		{
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Expected O, but got Unknown
			instance = this;
			enableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableMod", true, "Enable storage quality-of-life changes.");
			storageStackLimit = ((BaseUnityPlugin)this).Config.Bind<int>("Storage", "StorageStackLimit", 9999, "Stack limit used by the home stockpile.");
			removeStorageLimit = ((BaseUnityPlugin)this).Config.Bind<bool>("Storage", "RemoveStorageLimit", false, "Use a very high stockpile stack limit.");
			allowPotionStorage = ((BaseUnityPlugin)this).Config.Bind<bool>("Potions", "AllowPotionStorage", true, "Allow crafted potions to be stored in the home stockpile.");
			depositPotionsWithResources = ((BaseUnityPlugin)this).Config.Bind<bool>("Potions", "DepositPotionsWithResources", true, "The Deposit Resources button also moves potions into the stockpile.");
			debugLogs = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "DebugLogs", false, "Write storage changes to BepInEx LogOutput.log.");
			harmony = new Harmony("joive.bg.storageqol");
			harmony.PatchAll(typeof(StorageQoLPlugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BG Storage QoL 1.0.0 loaded.");
		}

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

		internal static bool IsStockpileInventory(InventoryBase inventory)
		{
			if ((Object)(object)inventory == (Object)null)
			{
				return false;
			}
			try
			{
				GnomiumDeposit val = (((Object)(object)GameProgressionManager.Instance != (Object)null) ? GameProgressionManager.Instance.Deposit : null);
				return (Object)(object)val != (Object)null && (Object)(object)val.ResourceInventory == (Object)(object)inventory;
			}
			catch
			{
				return false;
			}
		}

		internal static bool IsPotion(ItemData item)
		{
			return item is PotionData;
		}

		internal static void ApplyStockpileLimit(InventoryBase inventory)
		{
			if (!Enabled || (Object)(object)inventory == (Object)null)
			{
				return;
			}
			try
			{
				inventory.SetMaxOverriddenStackSize(EffectiveStackLimit);
				Log("Stockpile stack limit set to " + EffectiveStackLimit + ".");
			}
			catch (Exception ex)
			{
				Log("Could not set stockpile stack limit: " + ex.Message);
			}
		}

		internal static int MovePotionsToStockpile(PlayerNetworking player, InventoryBase stockpile)
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			if (!PotionDepositEnabled || (Object)(object)player == (Object)null || (Object)(object)stockpile == (Object)null || (Object)(object)player.Inventory == (Object)null)
			{
				return 0;
			}
			int num = 0;
			InventoryBase inventory = (InventoryBase)(object)player.Inventory;
			for (int i = 0; i < inventory.SlotCount; i++)
			{
				InventoryItem itemInSlot = inventory.GetItemInSlot(i);
				if (((InventoryItem)(ref itemInSlot)).IsValidItem && IsPotion(inventory.GetItemData((int)((InventoryItem)(ref itemInSlot)).ItemIndex)))
				{
					int num2 = stockpile.TryAddItem(((InventoryItem)(ref itemInSlot)).InstanceData);
					if (num2 > 0)
					{
						inventory.RemoveFromSlot(i, num2);
						num += num2;
					}
				}
			}
			if (num > 0)
			{
				Log("Moved " + num + " potion item(s) to stockpile.");
			}
			return num;
		}

		internal static bool InventoryHasPotion(PlayerInventory inventory)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			if (!PotionDepositEnabled || (Object)(object)inventory == (Object)null)
			{
				return false;
			}
			for (int i = 0; i < ((InventoryBase)inventory).SlotCount; i++)
			{
				InventoryItem itemInSlot = ((InventoryBase)inventory).GetItemInSlot(i);
				if (((InventoryItem)(ref itemInSlot)).IsValidItem && IsPotion(((InventoryBase)inventory).GetItemData((int)((InventoryItem)(ref itemInSlot)).ItemIndex)))
				{
					return true;
				}
			}
			return false;
		}

		internal static void Log(string message)
		{
			if (debugLogs != null && debugLogs.Value)
			{
				StorageQoLPlugin storageQoLPlugin = instance;
				if (storageQoLPlugin != null)
				{
					((BaseUnityPlugin)storageQoLPlugin).Logger.LogInfo((object)message);
				}
			}
		}
	}
	[HarmonyPatch(typeof(HomeManager), "SpawnHome")]
	internal static class HomeManagerSpawnHomePatch
	{
		private static void Postfix(HomeManager __instance)
		{
			if (!((Object)(object)__instance == (Object)null) && ((NetworkBehaviour)__instance).IsServer)
			{
				GnomiumDeposit val = (((Object)(object)GameProgressionManager.Instance != (Object)null) ? GameProgressionManager.Instance.Deposit : null);
				StorageQoLPlugin.ApplyStockpileLimit(((Object)(object)val != (Object)null) ? val.ResourceInventory : null);
			}
		}
	}
	[HarmonyPatch(typeof(InventoryBase), "GetMaxStackSize")]
	internal static class InventoryBaseGetMaxStackSizePatch
	{
		private static void Postfix(InventoryBase __instance, ItemData item, ref int __result)
		{
			if (StorageQoLPlugin.Enabled && StorageQoLPlugin.IsStockpileInventory(__instance) && !((Object)(object)item == (Object)null))
			{
				__result = Math.Max(__result, StorageQoLPlugin.EffectiveStackLimit);
			}
		}
	}
	[HarmonyPatch(typeof(InventoryBase), "CanContainItem")]
	internal static class InventoryBaseCanContainItemPatch
	{
		private static void Postfix(InventoryBase __instance, ItemData itemData, ref ItemContainmentResult __result)
		{
			if (StorageQoLPlugin.PotionStorageEnabled && StorageQoLPlugin.IsStockpileInventory(__instance) && StorageQoLPlugin.IsPotion(itemData))
			{
				__result = (ItemContainmentResult)0;
			}
		}
	}
	[HarmonyPatch(typeof(InventoryBase), "GetHasDepositableResources")]
	internal static class InventoryBaseGetHasDepositableResourcesPatch
	{
		private static void Postfix(InventoryBase __instance, ref bool __result)
		{
			if (!__result)
			{
				PlayerInventory val = (PlayerInventory)(object)((__instance is PlayerInventory) ? __instance : null);
				if (val != null)
				{
					__result = StorageQoLPlugin.InventoryHasPotion(val);
				}
			}
		}
	}
	[HarmonyPatch(typeof(GnomiumDeposit), "DepositResourcesToStockpileRpc")]
	internal static class GnomiumDepositDepositResourcesPatch
	{
		private static void Postfix(GnomiumDeposit __instance, RpcParams rpcParams = default(RpcParams))
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)__instance == (Object)null) && ((NetworkBehaviour)__instance).IsServer)
			{
				StorageQoLPlugin.MovePotionsToStockpile(ServerManager.GetPlayer(rpcParams.Receive.SenderClientId), __instance.ResourceInventory);
			}
		}
	}
}