Decompiled source of KeepLoaded v1.0.0

KeepLoaded.dll

Decompiled 7 hours 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;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("KeepLoaded")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("KeepLoaded")]
[assembly: AssemblyTitle("KeepLoaded")]
[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 KeepLoaded
{
	[BepInPlugin("pieter.valheim.keeploaded", "Keep Loaded", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "pieter.valheim.keeploaded";

		public const string Name = "Keep Loaded";

		public const string Version = "1.0.0";

		private const string LoadedKey = "pieter_keeploaded";

		private static ConfigEntry<bool> _crossbows;

		private static ConfigEntry<bool> _grapplingHook;

		private static readonly FieldRef<Player, ItemData> WeaponLoaded = AccessTools.FieldRefAccess<Player, ItemData>("m_weaponLoaded");

		private static readonly Action<Player, ItemData> SetWeaponLoaded = AccessTools.MethodDelegate<Action<Player, ItemData>>(AccessTools.Method(typeof(Player), "SetWeaponLoaded", (Type[])null, (Type[])null), (object)null, true);

		private void Awake()
		{
			_crossbows = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Crossbows stay loaded", true, "A loaded crossbow stays loaded when you switch to another weapon and back.");
			_grapplingHook = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Grappling hook stays loaded", true, "A loaded grappling hook stays loaded when you switch to another item and back.");
			Harmony.CreateAndPatchAll(typeof(Plugin), "pieter.valheim.keeploaded");
		}

		private static bool Applies(ItemData item)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Invalid comparison between Unknown and I4
			if (item?.m_shared == null || !item.m_shared.m_attack.m_requiresReload)
			{
				return false;
			}
			if ((int)item.m_shared.m_skillType == 14)
			{
				return _crossbows.Value;
			}
			if ((((Object)(object)item.m_dropPrefab != (Object)null) ? ((Object)item.m_dropPrefab).name : "").Contains("Grappling") || item.m_shared.m_name.ToLowerInvariant().Contains("grappl"))
			{
				return _grapplingHook.Value;
			}
			return false;
		}

		[HarmonyPatch(typeof(Humanoid), "UnequipItem")]
		[HarmonyPrefix]
		private static void Unequip(Humanoid __instance, ItemData item)
		{
			Player val = (Player)(object)((__instance is Player) ? __instance : null);
			if (val != null && !((Object)(object)val != (Object)(object)Player.m_localPlayer) && item != null && WeaponLoaded.Invoke(val) == item && Applies(item) && ((Humanoid)val).IsItemEquiped(item))
			{
				item.m_customData["pieter_keeploaded"] = "1";
			}
		}

		[HarmonyPatch(typeof(Player), "UpdateWeaponLoading")]
		[HarmonyPrefix]
		private static void UpdateWeaponLoading(Player __instance, ItemData weapon)
		{
			if (weapon != null && WeaponLoaded.Invoke(__instance) != weapon && weapon.m_customData.ContainsKey("pieter_keeploaded"))
			{
				weapon.m_customData.Remove("pieter_keeploaded");
				if (Applies(weapon))
				{
					SetWeaponLoaded(__instance, weapon);
				}
			}
		}
	}
}