Decompiled source of Joive BG SaveInventory v1.0.0

BG-SaveInventory-Joive.dll

Decompiled 2 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;

[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("Keeps player inventory after death in Burglin Gnomes.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BG SaveInventory")]
[assembly: AssemblyTitle("BG-SaveInventory-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 BGSaveInventory
{
	[BepInPlugin("joive.bgsaveinventory", "BG SaveInventory", "1.0.0")]
	public sealed class SaveInventoryPlugin : BaseUnityPlugin
	{
		private Harmony harmony;

		private static ConfigEntry<bool> preserveDrops;

		private static ConfigEntry<bool> preserveClears;

		private void Awake()
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			preserveDrops = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "KeepInventoryOnDeath", true, "Prevent player inventory from dropping after death.");
			preserveClears = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "KeepInventoryOnRoundEndRespawn", true, "Prevent player inventory from being cleared when a dead player is respawned after round end.");
			harmony = new Harmony("joive.bgsaveinventory");
			PatchInventoryMethod("DropInventoryContents", "InventoryDropPrefix");
			PatchInventoryMethod("ClearInventory", "InventoryClearPrefix");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BG SaveInventory 1.0.0 loaded.");
		}

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

		private void PatchInventoryMethod(string gameMethodName, string prefixName)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			try
			{
				MethodInfo methodInfo = AccessTools.Method(typeof(InventoryBase), gameMethodName, (Type[])null, (Type[])null);
				HarmonyMethod val = new HarmonyMethod(typeof(SaveInventoryPlugin).GetMethod(prefixName));
				harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Patched InventoryBase." + gameMethodName));
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Failed to patch InventoryBase." + gameMethodName + ": " + ex.Message));
			}
		}

		public static bool InventoryDropPrefix(InventoryBase __instance)
		{
			if (preserveDrops.Value)
			{
				return !IsPlayerInventory(__instance);
			}
			return true;
		}

		public static bool InventoryClearPrefix(InventoryBase __instance)
		{
			if (preserveClears.Value)
			{
				return !IsPlayerInventory(__instance);
			}
			return true;
		}

		private static bool IsPlayerInventory(InventoryBase inventory)
		{
			return inventory is PlayerInventory;
		}
	}
}