Decompiled source of KeepWeaponsAfterSwim v1.0.1

plugins/KeepWeaponsAfterSwim.dll

Decompiled a week ago
using System;
using System.Diagnostics;
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.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("KeepWeaponsAfterSwim")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+e7c8c6e1936757ff04de0f8462655ec37ee47391")]
[assembly: AssemblyProduct("KeepWeaponsAfterSwim")]
[assembly: AssemblyTitle("KeepWeaponsAfterSwim")]
[assembly: AssemblyVersion("1.0.1.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 KeepWeaponsAfterSwim
{
	internal enum SwimMode
	{
		AutoRestore,
		KeepInHand,
		Off
	}
	[BepInPlugin("lazly.valheim.keepweaponsafterswim", "KeepWeaponsAfterSwim", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string ModGuid = "lazly.valheim.keepweaponsafterswim";

		public const string ModName = "KeepWeaponsAfterSwim";

		public const string ModVersion = "1.0.1";

		internal static ManualLogSource Log;

		internal static ConfigEntry<SwimMode> Mode;

		internal static ConfigEntry<float> RestoreDelay;

		private void Awake()
		{
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Mode = ((BaseUnityPlugin)this).Config.Bind<SwimMode>("General", "Mode", SwimMode.AutoRestore, "AutoRestore = weapons are hidden while swimming (vanilla) and taken back out when you leave the water. KeepInHand = weapons are never hidden while swimming. Off = vanilla behaviour.");
			RestoreDelay = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RestoreDelay", 0.5f, "AutoRestore: seconds out of the water before the weapons are taken back out (avoids flicker when bobbing at the shoreline).");
			new Harmony("lazly.valheim.keepweaponsafterswim").PatchAll(typeof(Plugin).Assembly);
			Log.LogInfo((object)"KeepWeaponsAfterSwim 1.0.1 loaded");
		}
	}
	internal static class HumanoidAccess
	{
		private static readonly MethodInfo GetRightItemMethod = AccessTools.Method(typeof(Humanoid), "GetRightItem", (Type[])null, (Type[])null);

		private static readonly MethodInfo GetLeftItemMethod = AccessTools.Method(typeof(Humanoid), "GetLeftItem", (Type[])null, (Type[])null);

		private static readonly MethodInfo ShowHandItemsMethod = AccessTools.Method(typeof(Humanoid), "ShowHandItems", new Type[2]
		{
			typeof(bool),
			typeof(bool)
		}, (Type[])null);

		public static bool HasHandItems(Humanoid h)
		{
			if (GetRightItemMethod?.Invoke(h, null) == null)
			{
				return GetLeftItemMethod?.Invoke(h, null) != null;
			}
			return true;
		}

		public static void ShowHandItems(Humanoid h)
		{
			ShowHandItemsMethod?.Invoke(h, new object[2] { false, true });
		}
	}
	internal static class SwimState
	{
		public static bool HiddenBySwim;

		public static float OutOfWaterTime;

		public static void Reset()
		{
			HiddenBySwim = false;
			OutOfWaterTime = 0f;
		}
	}
	[HarmonyPatch(typeof(Humanoid), "HideHandItems")]
	internal static class HideHandItemsPatch
	{
		private static bool Prefix(Humanoid __instance)
		{
			if (Plugin.Mode.Value == SwimMode.Off)
			{
				return true;
			}
			Player val = (Player)(object)((__instance is Player) ? __instance : null);
			if (val == null || (Object)(object)val != (Object)(object)Player.m_localPlayer || !((Character)val).IsSwimming())
			{
				return true;
			}
			if (Plugin.Mode.Value == SwimMode.KeepInHand)
			{
				return false;
			}
			if (HumanoidAccess.HasHandItems((Humanoid)(object)val))
			{
				if (!SwimState.HiddenBySwim)
				{
					Plugin.Log.LogDebug((object)"Hands hidden by swimming, will restore after leaving the water");
				}
				SwimState.HiddenBySwim = true;
				SwimState.OutOfWaterTime = 0f;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Player), "Update")]
	internal static class PlayerUpdatePatch
	{
		private static void Postfix(Player __instance)
		{
			if (!SwimState.HiddenBySwim || (Object)(object)__instance != (Object)(object)Player.m_localPlayer)
			{
				return;
			}
			if (Plugin.Mode.Value != SwimMode.AutoRestore || ((Character)__instance).IsDead())
			{
				SwimState.Reset();
				return;
			}
			if (((Character)__instance).IsSwimming())
			{
				SwimState.OutOfWaterTime = 0f;
				return;
			}
			SwimState.OutOfWaterTime += Time.deltaTime;
			if (!(SwimState.OutOfWaterTime < Plugin.RestoreDelay.Value))
			{
				SwimState.Reset();
				HumanoidAccess.ShowHandItems((Humanoid)(object)__instance);
			}
		}
	}
}