Decompiled source of KALI Deposit Filters v0.1.0

BepInEx/plugins/KALI_Deposit_Filters/FinallyDepositFilters.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ServersideQoL.AutoStore;
using ServersideQoL.Processors;
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("FinallyDepositFilters")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("FinallyDepositFilters")]
[assembly: AssemblyTitle("FinallyDepositFilters")]
[assembly: AssemblyVersion("0.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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;
		}
	}
}
[BepInPlugin("local.finally.depositfilters", "Finally Deposit Filters", "0.1.0")]
[BepInDependency("ArgusMagnus.ServersideQoL.AutoStore", "2.5.1")]
public sealed class FinallyDepositFilters : BaseUnityPlugin
{
	private static ObjectDB? foodDatabase;

	private static ZNetScene? foodScene;

	private static readonly HashSet<string> preparedFoods = new HashSet<string>(StringComparer.Ordinal);

	public static bool Ready { get; private set; }

	private void Awake()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Expected O, but got Unknown
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Expected O, but got Unknown
		MethodInfo methodInfo = AccessTools.Method(typeof(ContainerProcessor), "OnPlayerEmoteDetected", (Type[])null, (Type[])null);
		Harmony val = new Harmony("local.finally.depositfilters");
		val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(FinallyDepositFilters), "Guard", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		try
		{
			val.Patch((MethodBase)methodInfo, (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(FinallyDepositFilters), "FilterTemplates", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
			Ready = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Quick deposit excludes weapons, tools, ammo, utilities, keys, quest items, potions, meads, cooked food and prepared meals. Equipped items retain vanilla protection. Hotbar slot filtering is not supported server-only.");
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Quick deposit disabled because exclusion patch failed: " + ex));
		}
	}

	private static bool Guard()
	{
		return Ready;
	}

	public static bool Excluded(ItemData item)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: 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)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Invalid comparison between Unknown and I4
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Invalid comparison between Unknown and I4
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Expected I4, but got Unknown
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c4: Invalid comparison between Unknown and I4
		SharedData shared = item.m_shared;
		ItemType itemType = shared.m_itemType;
		if (itemType - 3 > 2 && (int)itemType != 9)
		{
			switch (itemType - 14)
			{
			case 0:
			case 1:
			case 4:
			case 5:
			case 8:
			case 9:
			case 10:
				break;
			default:
			{
				if (shared.m_questItem)
				{
					return true;
				}
				if (IsPreparedFood(item))
				{
					return true;
				}
				GameObject dropPrefab = item.m_dropPrefab;
				string text = ((dropPrefab != null) ? ((Object)dropPrefab).name : null) ?? "";
				if (text.IndexOf("Key", StringComparison.OrdinalIgnoreCase) >= 0 || text.Equals("Wishbone", StringComparison.OrdinalIgnoreCase) || text.StartsWith("Mead", StringComparison.OrdinalIgnoreCase) || text.StartsWith("Potion", StringComparison.OrdinalIgnoreCase))
				{
					return true;
				}
				if ((int)shared.m_itemType == 2 && (Object)(object)shared.m_consumeStatusEffect != (Object)null && shared.m_food == 0f && shared.m_foodStamina == 0f)
				{
					return shared.m_foodEitr == 0f;
				}
				return false;
			}
			}
		}
		return true;
	}

	private static bool IsFood(ItemData item)
	{
		if (!(item.m_shared.m_food > 0f) && !(item.m_shared.m_foodStamina > 0f))
		{
			return item.m_shared.m_foodEitr > 0f;
		}
		return true;
	}

	public static bool IsPreparedFood(ItemData item)
	{
		if (!IsFood(item))
		{
			return false;
		}
		ObjectDB instance = ObjectDB.instance;
		ZNetScene instance2 = ZNetScene.instance;
		if ((Object)(object)instance == (Object)null || (Object)(object)instance2 == (Object)null)
		{
			return true;
		}
		if ((Object)(object)foodDatabase != (Object)(object)instance || (Object)(object)foodScene != (Object)(object)instance2)
		{
			preparedFoods.Clear();
			HashSet<string> hashSet = new HashSet<string>(StringComparer.Ordinal);
			foreach (Recipe recipe in instance.m_recipes)
			{
				if ((Object)(object)recipe != (Object)null)
				{
					Add(recipe.m_item);
				}
			}
			foreach (GameObject prefab in instance2.m_prefabs)
			{
				CookingStation[] componentsInChildren = prefab.GetComponentsInChildren<CookingStation>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					foreach (ItemConversion item2 in componentsInChildren[i].m_conversion)
					{
						Add(item2.m_to);
						if ((Object)(object)item2.m_from != (Object)null)
						{
							hashSet.Add(item2.m_from.m_itemData.m_shared.m_name);
						}
					}
				}
				Smelter[] componentsInChildren2 = prefab.GetComponentsInChildren<Smelter>(true);
				foreach (Smelter val in componentsInChildren2)
				{
					if (((Object)prefab).name.IndexOf("oven", StringComparison.OrdinalIgnoreCase) < 0)
					{
						continue;
					}
					foreach (ItemConversion item3 in val.m_conversion)
					{
						Add(item3.m_to);
						if ((Object)(object)item3.m_from != (Object)null)
						{
							hashSet.Add(item3.m_from.m_itemData.m_shared.m_name);
						}
					}
				}
			}
			preparedFoods.ExceptWith(hashSet);
			foodDatabase = instance;
			foodScene = instance2;
		}
		return preparedFoods.Contains(item.m_shared.m_name);
		static void Add(ItemDrop output)
		{
			if ((Object)(object)output != (Object)null && IsFood(output.m_itemData))
			{
				preparedFoods.Add(output.m_itemData.m_shared.m_name);
			}
		}
	}

	private static List<ItemData> EligibleTemplates(IInventory inventory)
	{
		return inventory.Items.Where((ItemData i) => !Excluded(i)).ToList();
	}

	private static IEnumerable<CodeInstruction> FilterTemplates(IEnumerable<CodeInstruction> instructions)
	{
		List<CodeInstruction> list = instructions.ToList();
		MethodInfo getter = AccessTools.PropertyGetter(typeof(IInventory), "Items");
		List<CodeInstruction> list2 = list.Where((CodeInstruction i) => CodeInstructionExtensions.Calls(i, getter)).ToList();
		if (list2.Count != 2)
		{
			throw new InvalidOperationException("Expected source iteration and helper seeding getters in AutoStore 2.5.1; found " + list2.Count);
		}
		list2[0].opcode = OpCodes.Call;
		list2[0].operand = AccessTools.Method(typeof(FinallyDepositFilters), "EligibleTemplates", (Type[])null, (Type[])null);
		return list;
	}
}