Decompiled source of QuickSlotDeathGuard v0.1.0

QuickSlotDeathGuard.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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("QuickSlotDeathGuard")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("QuickSlotDeathGuard")]
[assembly: AssemblyTitle("QuickSlotDeathGuard")]
[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 QuickSlotDeathGuard
{
	[HarmonyPatch(typeof(Player), "CreateTombStone")]
	internal static class CreateTombStonePatches
	{
		private struct StagedItem
		{
			public ItemData Item;

			public Vector2i Pos;
		}

		private static readonly List<StagedItem> Staged = new List<StagedItem>();

		[HarmonyPriority(800)]
		[HarmonyPrefix]
		private static void Prefix()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			Staged.Clear();
			Inventory quickInventory = GearSlotsInterop.GetQuickInventory();
			if (quickInventory == null)
			{
				return;
			}
			foreach (ItemData item in new List<ItemData>(quickInventory.GetAllItems()))
			{
				Staged.Add(new StagedItem
				{
					Item = item,
					Pos = item.m_gridPos
				});
				quickInventory.RemoveItem(item);
			}
		}

		[HarmonyPriority(0)]
		[HarmonyPostfix]
		private static void Postfix()
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			if (Staged.Count == 0)
			{
				return;
			}
			Inventory quickInventory = GearSlotsInterop.GetQuickInventory();
			if (quickInventory == null)
			{
				Staged.Clear();
				return;
			}
			foreach (StagedItem item in Staged)
			{
				if (!quickInventory.AddItem(item.Item, item.Pos))
				{
					quickInventory.AddItem(item.Item);
				}
			}
			Staged.Clear();
		}
	}
	internal static class GearSlotsInterop
	{
		private static readonly ManualLogSource Log = Logger.CreateLogSource("QuickSlotDeathGuard.Interop");

		private static PropertyInfo _quickInventoryProp;

		private static bool _resolved;

		public static bool IsAvailable
		{
			get
			{
				Resolve();
				return _quickInventoryProp != null;
			}
		}

		private static void Resolve()
		{
			if (_resolved)
			{
				return;
			}
			_resolved = true;
			Type type = AccessTools.TypeByName("GearSlots.Runtime.SlotStateManager");
			if (!(type == null))
			{
				_quickInventoryProp = type.GetProperty("QuickInventory", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				if (_quickInventoryProp == null)
				{
					Log.LogWarning((object)"Found GearSlots.Runtime.SlotStateManager but no static 'QuickInventory' property - GearSlots likely updated its internals; QuickSlotDeathGuard needs an update too.");
				}
			}
		}

		public static Inventory GetQuickInventory()
		{
			Resolve();
			if (_quickInventoryProp == null)
			{
				return null;
			}
			try
			{
				object? value = _quickInventoryProp.GetValue(null);
				return (Inventory)((value is Inventory) ? value : null);
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("Failed reading GearSlots QuickInventory: " + ex));
				return null;
			}
		}
	}
	[BepInPlugin("chini.quickslotdeathguard", "QuickSlotDeathGuard", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "chini.quickslotdeathguard";

		public const string PluginName = "QuickSlotDeathGuard";

		public const string PluginVersion = "0.1.0";

		public const string GearSlotsGuid = "com.jg224.gearslots";

		private Harmony _harmony;

		private void Awake()
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Expected O, but got Unknown
			if (!GearSlotsInterop.IsAvailable)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"GearSlots not detected (or its internals changed) - QuickSlotDeathGuard has nothing to do, staying idle.");
				return;
			}
			_harmony = new Harmony("chini.quickslotdeathguard");
			_harmony.PatchAll(typeof(CreateTombStonePatches));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"QuickSlotDeathGuard: quick-slot death protection active.");
		}

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