Decompiled source of LootFilter v1.0.2

wonkotron.LootFilter.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace lootfilter
{
	[BepInPlugin("wonkotron.LootFilter", "LootFilter", "1.0.2")]
	public class LootFilter : BaseUnityPlugin
	{
		internal const string ModName = "LootFilter";

		internal const string ModVersion = "1.0.2";

		internal const string Author = "wonkotron";

		private const string ModGUID = "wonkotron.LootFilter";

		private Harmony _harmony = null;

		public static ConfigEntry<bool> filterEnabled;

		private static ConfigEntry<string> blacklistString;

		public static ConfigEntry<bool> debugLogging;

		private static LootFilter _instance;

		public static string[] blacklist { get; private set; }

		public static ManualLogSource Log { get; private set; }

		[UsedImplicitly]
		private void Awake()
		{
			_instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "wonkotron.LootFilter");
			filterEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Filter", "Filter Enabled", true, "Enable Loot Filter");
			blacklistString = ((BaseUnityPlugin)this).Config.Bind<string>("Filter", "Item Blacklist", "", "Comma separated list (NO SPACES) of PrefabID strings (e.g. Resin,LeatherScraps)");
			debugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Debug Logging", false, "Enable debug logging for this plugin");
			blacklist = blacklistString.Value.Split(new char[1] { ',' });
			((BaseUnityPlugin)this).Config.SettingChanged += Config_SettingChanged;
		}

		private void Config_SettingChanged(object sender, EventArgs e)
		{
			((BaseUnityPlugin)this).Config.SettingChanged -= Config_SettingChanged;
			blacklist = blacklistString.Value.Split(new char[1] { ',' });
			((BaseUnityPlugin)this).Config.SettingChanged += Config_SettingChanged;
		}

		[UsedImplicitly]
		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.SettingChanged -= Config_SettingChanged;
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(Inventory), "CanAddItem", new Type[]
	{
		typeof(ItemData),
		typeof(int)
	})]
	public static class Inventory_CanAddItem_Patch
	{
		public static void Postfix(ItemData item, int stack, ref bool __result)
		{
			try
			{
				if (!__result || !LootFilter.filterEnabled.Value)
				{
					return;
				}
				object obj;
				if (item == null)
				{
					obj = null;
				}
				else
				{
					GameObject dropPrefab = item.m_dropPrefab;
					obj = ((dropPrefab != null) ? ((Object)dropPrefab).name : null);
				}
				string value = (string)obj;
				if (string.IsNullOrEmpty(value))
				{
					if (LootFilter.debugLogging.Value)
					{
						LootFilter.Log.LogDebug((object)"null or empty item?.m_dropPrefab?.name (https://github.com/wonkovalheim/lootfilter/issues/2)");
						LootFilter.Log.LogDebug((object)$"item null?  {item == null}");
						LootFilter.Log.LogDebug((object)$"item.m_dropPrefab null? {(Object)(object)item?.m_dropPrefab == (Object)null}");
						ManualLogSource log = LootFilter.Log;
						object obj2;
						if (item == null)
						{
							obj2 = null;
						}
						else
						{
							GameObject dropPrefab2 = item.m_dropPrefab;
							obj2 = ((dropPrefab2 != null) ? ((Object)dropPrefab2).name : null);
						}
						log.LogDebug((object)$"item.m_dropPrefab.name null? {obj2 == null}");
					}
				}
				else if (LootFilter.blacklist.Contains(value))
				{
					__result = false;
				}
			}
			catch (Exception ex)
			{
				LootFilter.Log.LogError((object)"Exception caught when executing PostFix");
				LootFilter.Log.LogError((object)ex.ToString());
			}
		}
	}
}