Decompiled source of SwimSpeed v1.3.1

SwimSpeed.dll

Decompiled 3 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("SwimSpeed")]
[assembly: AssemblyDescription("Adjust your Valheim swim speed in real-time with arrow keys.")]
[assembly: AssemblyCompany("YouDied")]
[assembly: AssemblyProduct("SwimSpeed")]
[assembly: AssemblyCopyright("Copyright © YouDied 2024")]
[assembly: ComVisible(false)]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace SwimSpeed;

[BepInPlugin("youdied.swimspeed", "SwimSpeed", "1.3.1")]
[BepInProcess("valheim.exe")]
public class SwimSpeedPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Character), "UpdateSwimming")]
	private static class Patch_UpdateSwimming
	{
		[HarmonyPrefix]
		private static void Prefix(Character __instance, out float __state)
		{
			Player val = (Player)(object)((__instance is Player) ? __instance : null);
			if (val == null || (Object)(object)val != (Object)(object)Player.m_localPlayer)
			{
				__state = -1f;
				return;
			}
			s_inSwimUpdate = true;
			__state = __instance.m_swimSpeed;
			__instance.m_swimSpeed *= GetEffectiveMultiplier();
		}

		[HarmonyPostfix]
		private static void Postfix(Character __instance, float __state)
		{
			s_inSwimUpdate = false;
			if (!(__state < 0f))
			{
				__instance.m_swimSpeed = __state;
			}
		}
	}

	[HarmonyPatch(typeof(Character), "Jump")]
	private static class Patch_Jump
	{
		[HarmonyPrefix]
		private static void Prefix(Character __instance, out float __state)
		{
			float effectiveMultiplier = GetEffectiveMultiplier();
			if (s_inSwimUpdate && effectiveMultiplier > 1f)
			{
				Player val = (Player)(object)((__instance is Player) ? __instance : null);
				if (val != null && (Object)(object)val == (Object)(object)Player.m_localPlayer)
				{
					__state = __instance.m_swimSpeed;
					__instance.m_swimSpeed /= effectiveMultiplier;
					return;
				}
			}
			__state = -1f;
		}

		[HarmonyPostfix]
		private static void Postfix(Character __instance, float __state)
		{
			if (!(__state < 0f))
			{
				__instance.m_swimSpeed = __state;
			}
		}
	}

	public const string PluginGUID = "youdied.swimspeed";

	public const string PluginName = "SwimSpeed";

	public const string PluginVersion = "1.3.1";

	internal static ManualLogSource Log;

	private static ConfigEntry<int> _cfgMin;

	private static ConfigEntry<int> _cfgMax;

	private static ConfigEntry<int> _cfgStep;

	private static ConfigEntry<int> _cfgCurrent;

	private static ConfigEntry<bool> _cfgLudicrousEnabled;

	private static ConfigEntry<int> _cfgLudicrousMultiplier;

	private static ConfigEntry<KeyCode> _cfgKeyIncrease;

	private static ConfigEntry<KeyCode> _cfgKeyDecrease;

	private static int s_multiplier = 1;

	private static bool s_inSwimUpdate = false;

	private Harmony _harmony;

	private static int GetMin()
	{
		return _cfgMin.Value;
	}

	private static int GetMax()
	{
		return _cfgMax.Value;
	}

	private static int GetStep()
	{
		return Mathf.Max(1, Mathf.RoundToInt((float)_cfgStep.Value / 100f * (float)GetMax()));
	}

	private static float GetEffectiveMultiplier()
	{
		if (_cfgLudicrousEnabled.Value)
		{
			return (float)s_multiplier * (float)_cfgLudicrousMultiplier.Value;
		}
		return s_multiplier;
	}

	private void Awake()
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Expected O, but got Unknown
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Expected O, but got Unknown
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Expected O, but got Unknown
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Expected O, but got Unknown
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_0122: Expected O, but got Unknown
		//IL_0193: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		_cfgMin = ((BaseUnityPlugin)this).Config.Bind<int>("Speed Range", "Minimum Multiplier", 1, new ConfigDescription("Minimum swim speed multiplier (1 = default speed).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 10), Array.Empty<object>()));
		_cfgMax = ((BaseUnityPlugin)this).Config.Bind<int>("Speed Range", "Maximum Multiplier", 5, new ConfigDescription("Maximum swim speed multiplier (5 = 5× default speed). Up to 10.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 10), Array.Empty<object>()));
		_cfgStep = ((BaseUnityPlugin)this).Config.Bind<int>("Speed Range", "Step Percent", 20, new ConfigDescription("How much each key press changes speed, as a % of the maximum multiplier.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 100), Array.Empty<object>()));
		_cfgCurrent = ((BaseUnityPlugin)this).Config.Bind<int>("Current Speed", "Current Multiplier", 1, new ConfigDescription("Current swim speed multiplier. Saved between sessions.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 10), Array.Empty<object>()));
		_cfgLudicrousEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Ludicrous Speed", "Enable Ludicrous Speed", false, "When enabled, all speed values are further multiplied by the Ludicrous Multiplier.");
		_cfgLudicrousMultiplier = ((BaseUnityPlugin)this).Config.Bind<int>("Ludicrous Speed", "Ludicrous Multiplier", 5, new ConfigDescription("Extra multiplier applied on top of the current speed when Ludicrous Speed is on.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(2, 20), Array.Empty<object>()));
		_cfgKeyIncrease = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keybinds", "Increase Speed Key", (KeyCode)275, "Key to increase swim speed by one step.");
		_cfgKeyDecrease = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Keybinds", "Decrease Speed Key", (KeyCode)276, "Key to decrease swim speed by one step.");
		s_multiplier = Mathf.Clamp(_cfgCurrent.Value, GetMin(), GetMax());
		_harmony = new Harmony("youdied.swimspeed");
		_harmony.PatchAll();
		Log.LogInfo((object)("SwimSpeed v1.3.1 loaded. " + $"Swim multiplier: {s_multiplier}x (range {GetMin()}–{GetMax()})."));
	}

	private void Update()
	{
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)Player.m_localPlayer == (Object)null) && (!((Object)(object)Chat.instance != (Object)null) || !Chat.instance.HasFocus()) && !InventoryGui.IsVisible() && !Menu.IsVisible())
		{
			bool flag = false;
			if (Input.GetKeyDown(_cfgKeyIncrease.Value))
			{
				int num = s_multiplier + GetStep();
				s_multiplier = Mathf.Min(num, GetMax());
				flag = true;
			}
			else if (Input.GetKeyDown(_cfgKeyDecrease.Value))
			{
				int num2 = s_multiplier - GetStep();
				s_multiplier = Mathf.Max(num2, GetMin());
				flag = true;
			}
			if (flag)
			{
				_cfgCurrent.Value = s_multiplier;
				string arg = ((s_multiplier <= GetMin()) ? " (default)" : ((s_multiplier >= GetMax()) ? " (max)" : ""));
				string arg2 = (_cfgLudicrousEnabled.Value ? $" [LUDICROUS ×{_cfgLudicrousMultiplier.Value}]" : "");
				((Character)Player.m_localPlayer).Message((MessageType)1, $"Swim Speed: {s_multiplier}x{arg}{arg2}", 0, (Sprite)null, false);
				Log.LogInfo((object)$"Swim speed set to {s_multiplier}x (effective: {GetEffectiveMultiplier()}x).");
			}
		}
	}

	private void OnDestroy()
	{
		_harmony.UnpatchSelf();
	}
}