Decompiled source of Infinite Chest v1.0.3

InfiniteChest.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("InfiniteChest")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3+08e6dc21d070282d58a73b53b86ddaf5d1d4c9d2")]
[assembly: AssemblyProduct("InfiniteChest")]
[assembly: AssemblyTitle("InfiniteChest")]
[assembly: AssemblyVersion("1.0.3.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;
		}
	}
}
internal static class ModInfo
{
	public const string Guid = "BrugherheimFolks.InfiniteChest";

	public const string Name = "Infinite Chest";

	public const string Version = "1.0.3";
}
namespace InfiniteChest
{
	[BepInPlugin("BrugherheimFolks.InfiniteChest", "Infinite Chest", "1.0.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public enum FilterMode
		{
			All,
			Whitelist,
			Blacklist
		}

		[HarmonyPatch(typeof(Container), "OnContainerChanged")]
		private static class Refill
		{
			private static void Postfix(Container __instance, ZNetView ___m_nview, bool ___m_loading)
			{
				if (refilling || ___m_loading || __instance.m_name != "Infinite Chest" || !__instance.IsOwner())
				{
					return;
				}
				Inventory inventory = __instance.GetInventory();
				ZDO zDO = ___m_nview.GetZDO();
				string text = zDO.GetString("InfiniteChest_item", "");
				if (text == "")
				{
					ItemData val = inventory.GetAllItems().Find((ItemData i) => Object.op_Implicit((Object)(object)i.m_dropPrefab));
					if (val == null)
					{
						return;
					}
					text = ((Object)val.m_dropPrefab).name;
					zDO.Set("InfiniteChest_item", text);
				}
				GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(text);
				if (!Object.op_Implicit((Object)(object)itemPrefab))
				{
					return;
				}
				int maxStackSize = itemPrefab.GetComponent<ItemDrop>().m_itemData.m_shared.m_maxStackSize;
				refilling = true;
				try
				{
					while (inventory.CanAddItem(itemPrefab, 1))
					{
						inventory.AddItem(itemPrefab, maxStackSize);
					}
				}
				finally
				{
					refilling = false;
				}
			}
		}

		[HarmonyPatch]
		private static class OnlyChosenItem
		{
			private static IEnumerable<MethodBase> TargetMethods()
			{
				return from m in AccessTools.GetDeclaredMethods(typeof(Inventory))
					where m.Name == "AddItem" && m.GetParameters()[0].ParameterType == typeof(ItemData)
					select m;
			}

			private static bool Prefix(Inventory __instance, ItemData __0, object[] __args, ref bool __result)
			{
				if (refilling || loading || InventoryName.Invoke(__instance) != "Infinite Chest")
				{
					return true;
				}
				if (!Allowed(__instance, __0))
				{
					return __result = false;
				}
				if (__instance.GetAllItems().Count == 0)
				{
					return true;
				}
				if (__args.Length >= 4)
				{
					__0.m_stack -= Math.Min((int)__args[1], __0.m_stack);
				}
				__result = true;
				return false;
			}
		}

		[HarmonyPatch]
		private static class LoadUntouched
		{
			private static IEnumerable<MethodBase> TargetMethods()
			{
				return from m in AccessTools.GetDeclaredMethods(typeof(Inventory))
					where m.Name == "Load" || m.Name == "LoadOld"
					select m;
			}

			private static void Prefix()
			{
				loading = true;
			}

			private static void Finalizer()
			{
				loading = false;
			}
		}

		[HarmonyPatch(typeof(InventoryGrid), "DropItem")]
		private static class NoLossySwap
		{
			private static bool Prefix(InventoryGrid __instance, Inventory fromInventory, ItemData item, Vector2i pos, ref bool __result)
			{
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				//IL_000e: Unknown result type (might be due to invalid IL or missing references)
				Inventory inventory = __instance.GetInventory();
				ItemData itemAt = inventory.GetItemAt(pos.x, pos.y);
				return __result = Allowed(inventory, item) && (itemAt == null || itemAt == item || Allowed(fromInventory, itemAt));
			}
		}

		[HarmonyPatch(typeof(Container), "OnDestroyed")]
		private static class NoDrop
		{
			private static bool Prefix(Container __instance)
			{
				return __instance.m_name != "Infinite Chest";
			}
		}

		public const string ChestName = "Infinite Chest";

		private const string ItemKey = "InfiniteChest_item";

		private static bool refilling;

		private static bool loading;

		private static ConfigEntry<FilterMode> filterMode;

		private static ConfigEntry<string> filterItems;

		private static readonly FieldRef<Inventory, string> InventoryName = AccessTools.FieldRefAccess<Inventory, string>("m_name");

		private void Awake()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Expected O, but got Unknown
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			ConfigurationManagerAttributes val = new ConfigurationManagerAttributes
			{
				IsAdminOnly = true
			};
			filterMode = ((BaseUnityPlugin)this).Config.Bind<FilterMode>("Filter", "Mode", FilterMode.All, new ConfigDescription("Which items an Infinite Chest accepts: All, Whitelist (only Items) or Blacklist (all but Items).", (AcceptableValueBase)null, new object[1] { val }));
			filterItems = ((BaseUnityPlugin)this).Config.Bind<string>("Filter", "Items", "", new ConfigDescription("Comma separated item prefab names, as used by the spawn console command (e.g. Wood,Stone,Resin).", (AcceptableValueBase)null, new object[1] { val }));
			PrefabManager.OnVanillaPrefabsAvailable += AddPiece;
			new Harmony("BrugherheimFolks.InfiniteChest").PatchAll();
		}

		private static void AddPiece()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			PrefabManager.OnVanillaPrefabsAvailable -= AddPiece;
			CustomPiece val = new CustomPiece("piece_chest_infinite", "piece_chest_wood", "Hammer");
			val.Piece.m_name = "Infinite Chest";
			val.Piece.m_description = "Put one item in: it is filled with that item forever.";
			val.PiecePrefab.GetComponent<Container>().m_name = "Infinite Chest";
			PieceManager.Instance.AddPiece(val);
		}

		private static bool Allowed(Inventory inv, ItemData item)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Invalid comparison between Unknown and I4
			if (InventoryName.Invoke(inv) != "Infinite Chest")
			{
				return true;
			}
			List<ItemData> allItems = inv.GetAllItems();
			if ((!item.IsEquipable() || (int)item.m_shared.m_itemType == 9) && PassesFilter(item))
			{
				if (allItems.Count != 0)
				{
					return allItems[0].m_shared.m_name == item.m_shared.m_name;
				}
				return true;
			}
			return false;
		}

		private static bool PassesFilter(ItemData item)
		{
			if (filterMode.Value == FilterMode.All)
			{
				return true;
			}
			return filterItems.Value.Split(',').Any(delegate(string n)
			{
				string text = n.Trim();
				GameObject dropPrefab = item.m_dropPrefab;
				return text.Equals((dropPrefab != null) ? ((Object)dropPrefab).name : null, StringComparison.OrdinalIgnoreCase);
			}) == (filterMode.Value == FilterMode.Whitelist);
		}
	}
}