Decompiled source of Safe Item Stacks v1.0.0

BepInEx/plugins/OctoberJemini-Safe_Item_Stacks/SafeItemStacks.dll

Decompiled 3 days ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace OctoberJemini.SafeItemStacks;

[BepInPlugin("OctoberJemini.SafeItemStacks", "Safe Item Stacks", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public sealed class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	private static class DBAwake
	{
		[HarmonyPriority(0)]
		[HarmonyPostfix]
		[HarmonyAfter(new string[] { "net.mtnewton.itemstacks" })]
		private static void Postfix(ObjectDB __instance)
		{
			ApplyDB(__instance);
		}
	}

	[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
	private static class DBCopy
	{
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		[HarmonyAfter(new string[] { "net.mtnewton.itemstacks" })]
		private static void Postfix(ObjectDB __instance)
		{
			ApplyDB(__instance);
		}
	}

	[HarmonyPatch(typeof(ItemDrop), "Awake")]
	private static class DropAwake
	{
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		private static void Postfix(ItemDrop __instance)
		{
			Apply(__instance);
		}
	}

	public const string Guid = "OctoberJemini.SafeItemStacks";

	private static ConfigEntry<int> size;

	private static ConfigEntry<int> sealSize;

	private static ConfigEntry<int> meadSize;

	private static ConfigEntry<int> utilitySize;

	private Harmony harmony;

	private void Awake()
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Expected O, but got Unknown
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Expected O, but got Unknown
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Expected O, but got Unknown
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Expected O, but got Unknown
		//IL_0149: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Expected O, but got Unknown
		size = ((BaseUnityPlugin)this).Config.Bind<int>("Stacking", "StackSize", 50, new ConfigDescription("Maximum dragon eggs per stack. Applies only to dragon eggs.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(2, 1000), new object[0]));
		sealSize = ((BaseUnityPlugin)this).Config.Bind<int>("Stacking", "SealBreakerStackSize", 50, new ConfigDescription("Stack limit for completed seal breakers only. Does not reduce a higher existing limit. Restart after changing.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(2, 1000), new object[0]));
		meadSize = ((BaseUnityPlugin)this).Config.Bind<int>("Stacking", "MeadBaseStackSize", 50, new ConfigDescription("Stack limit for mead bases. Fermenters still consume one base per batch. Does not reduce a higher existing limit. Restart after changing.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(2, 1000), new object[0]));
		utilitySize = ((BaseUnityPlugin)this).Config.Bind<int>("Stacking", "UtilityStackSize", 50, new ConfigDescription("Stack limit for swamp keys and saddles only. Equipment and all Dyrnwyn items are excluded. Does not reduce higher existing limits. Restart after changing.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(2, 1000), new object[0]));
		harmony = new Harmony("OctoberJemini.SafeItemStacks");
		harmony.PatchAll(typeof(Plugin).Assembly);
		Type type = AccessTools.TypeByName("ItemStacks.ItemStacksPlugin");
		if (type != null)
		{
			MethodInfo methodInfo = AccessTools.Method(type, "ModifyItemStackSizeAndWeight", (Type[])null, (Type[])null);
			if (methodInfo != null)
			{
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "AfterItemStacks", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Stack limits: dragon eggs " + size.Value + ", seal breakers " + sealSize.Value + ", mead bases " + meadSize.Value + ", selected utility items " + utilitySize.Value + ". Weight, teleport rules and fermenter consumption unchanged."));
	}

	private static void Apply(ItemDrop item)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Invalid comparison between Unknown and I4
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Invalid comparison between Unknown and I4
		if (Object.op_Implicit((Object)(object)item) && item.m_itemData != null && item.m_itemData.m_shared != null)
		{
			SharedData shared = item.m_itemData.m_shared;
			if (!shared.m_useDurability && shared.m_maxQuality <= 1 && ((int)shared.m_itemType == 1 || (int)shared.m_itemType == 16))
			{
				GameObject dropPrefab = item.m_itemData.m_dropPrefab;
				string prefab = (Object.op_Implicit((Object)(object)dropPrefab) ? ((Object)dropPrefab).name : ((Object)((Component)item).gameObject).name);
				shared.m_maxStackSize = StackPolicy.Limit(prefab, shared.m_name, shared.m_maxStackSize, size.Value, sealSize.Value, meadSize.Value, utilitySize.Value);
			}
		}
	}

	private static void ApplyDB(ObjectDB db)
	{
		if (!Object.op_Implicit((Object)(object)db) || db.m_items == null)
		{
			return;
		}
		foreach (GameObject item in db.m_items)
		{
			if (Object.op_Implicit((Object)(object)item))
			{
				Apply(item.GetComponent<ItemDrop>());
			}
		}
	}

	private static void AfterItemStacks(ObjectDB __0)
	{
		ApplyDB(__0);
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}
internal static class StackPolicy
{
	internal static int Limit(string prefab, string token, int current, int eggs, int seals, int mead, int utility = 50)
	{
		if (token == "$item_dragonegg")
		{
			return eggs;
		}
		if (prefab != null && prefab.EndsWith("(Clone)", StringComparison.Ordinal))
		{
			prefab = prefab.Substring(0, prefab.Length - 7);
		}
		switch (prefab)
		{
		case "DvergrKey":
			return Math.Max(current, seals);
		default:
			if (!prefab.StartsWith("MeadBase", StringComparison.Ordinal))
			{
				break;
			}
			goto case "BarleyWineBase";
		case "BarleyWineBase":
			return Math.Max(current, mead);
		case null:
			break;
		}
		switch (prefab)
		{
		case "CryptKey":
		case "SaddleLox":
		case "SaddleAsksvin":
		case "SaddleMoose":
			return Math.Max(current, utility);
		default:
			return current;
		}
	}
}