Decompiled source of HotbarDeathKeeper v1.0.1

HotbarDeathKeeper.dll

Decompiled a month 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 BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.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 HotbarDeathKeeper
{
	public static class DeathKeeperPatches
	{
		private sealed class KeptSlot
		{
			public ItemData Item { get; }

			public bool WasEquipped { get; }

			public KeptSlot(ItemData item, bool wasEquipped)
			{
				Item = item;
				WasEquipped = wasEquipped;
			}
		}

		[HarmonyPatch(typeof(Character), "CheckDeath")]
		private static class Character_CheckDeath_Patch
		{
			[HarmonyPriority(800)]
			private static void Prefix(Character __instance)
			{
				//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
				if (!HotbarDeathKeeperPlugin.modEnabled.Value)
				{
					return;
				}
				Player val = (Player)(object)((__instance is Player) ? __instance : null);
				if (val == null || !((Character)val).IsOwner() || ((Character)val).IsDead() || ((Character)val).GetHealth() > 0f || keptItems.Count != 0)
				{
					return;
				}
				Inventory inventory = ((Humanoid)val).GetInventory();
				if (inventory == null)
				{
					return;
				}
				for (int i = 0; i < 8; i++)
				{
					ItemData itemAt = inventory.GetItemAt(i, 0);
					if (itemAt != null)
					{
						keptItems.Add(new KeptSlot(itemAt, ((Humanoid)val).IsItemEquiped(itemAt)));
					}
				}
				if (keptItems.Count == 0)
				{
					HotbarDeathKeeperPlugin.log.LogDebug((object)"CheckDeath.Prefix: hotbar was empty, nothing to keep.");
					return;
				}
				foreach (KeptSlot keptItem in keptItems)
				{
					bool flag = inventory.RemoveItem(keptItem.Item);
					HotbarDeathKeeperPlugin.log.LogDebug((object)($"CheckDeath.Prefix: removed '{keptItem.Item.m_shared.m_name}' at {keptItem.Item.m_gridPos} " + $"(equipped={keptItem.WasEquipped}) -> success={flag}. Held until respawn."));
				}
			}
		}

		[HarmonyPatch(typeof(Player), "OnSpawned")]
		private static class Player_OnSpawned_Patch
		{
			private static void Postfix(Player __instance)
			{
				//IL_004e: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)__instance == (Object)null || !((Character)__instance).IsOwner() || keptItems.Count == 0)
				{
					return;
				}
				Inventory inventory = ((Humanoid)__instance).GetInventory();
				try
				{
					foreach (KeptSlot keptItem in keptItems)
					{
						bool flag = inventory != null && inventory.AddItem(keptItem.Item, keptItem.Item.m_gridPos);
						bool flag2 = false;
						if (flag && HotbarDeathKeeperPlugin.keepEquippedState.Value && keptItem.WasEquipped)
						{
							flag2 = ((Humanoid)__instance).EquipItem(keptItem.Item, true);
						}
						HotbarDeathKeeperPlugin.log.LogDebug((object)($"OnSpawned: restored '{keptItem.Item.m_shared.m_name}' at {keptItem.Item.m_gridPos} " + $"-> added={flag}, equipped={flag2} (wasEquipped={keptItem.WasEquipped})"));
					}
				}
				finally
				{
					keptItems.Clear();
				}
			}
		}

		private const int HotbarSlotCount = 8;

		private const int HotbarRow = 0;

		private static readonly List<KeptSlot> keptItems = new List<KeptSlot>();
	}
	[BepInPlugin("hdk.valheim.hotbardeathkeeper", "Hotbar Death Keeper", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class HotbarDeathKeeperPlugin : BaseUnityPlugin
	{
		private static class DependencyGuids
		{
			public const string ExtraSlots = "shudnal.ExtraSlots";
		}

		private static class ConfigSection
		{
			public const string General = "General";
		}

		public const string PluginGUID = "hdk.valheim.hotbardeathkeeper";

		public const string PluginName = "Hotbar Death Keeper";

		public const string PluginVersion = "1.0.1";

		internal static ConfigEntry<bool> modEnabled;

		internal static ConfigEntry<bool> keepEquippedState;

		internal static ManualLogSource log;

		private Harmony harmony;

		private void Awake()
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Expected O, but got Unknown
			log = ((BaseUnityPlugin)this).Logger;
			modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Keep vanilla hotbar (row 1, slots 1-8) items on death instead of losing them to the tombstone.");
			keepEquippedState = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Keep equipped state", true, "Re-equip the weapon/tool/shield that was in hand when you died.");
			harmony = new Harmony("hdk.valheim.hotbardeathkeeper");
			harmony.PatchAll();
			log.LogInfo((object)string.Format("{0} {1} loaded. Patched methods: {2}", "Hotbar Death Keeper", "1.0.1", harmony.GetPatchedMethods().Count()));
		}

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