Decompiled source of RapidExplore v1.0.1

plugins/RapidExplore.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("local.valheim.rapidexplore", "Rapid Explore", "1.0.1")]
public sealed class RapidExplore : BaseUnityPlugin
{
	private const string Id = "local.valheim.rapidexplore";

	private static Player explorer;

	private ConfigEntry<Key> toggle;

	private Harmony patches;

	private void Awake()
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		toggle = ((BaseUnityPlugin)this).Config.Bind<Key>("Controls", "Toggle", (Key)70, "Toggle exploration mode.");
		patches = new Harmony("local.valheim.rapidexplore");
		try
		{
			string[] array = new string[2] { "UpdateWalking", "UpdateSwimming" };
			foreach (string text in array)
			{
				patches.Patch((MethodBase)AccessTools.Method(typeof(Character), text, (Type[])null, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, Hook("ScaleMovement"), (HarmonyMethod)null, (HarmonyMethod)null);
			}
			string[] array2 = new string[3] { "Damage", "RPC_Damage", "ApplyDamage" };
			foreach (string text2 in array2)
			{
				patches.Patch((MethodBase)AccessTools.Method(typeof(Character), text2, (Type[])null, (Type[])null), Hook("AllowDamage"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			patches.Patch((MethodBase)AccessTools.Method(typeof(Player), "RPC_UseStamina", (Type[])null, (Type[])null), Hook("AllowStaminaUse"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			patches.Patch((MethodBase)AccessTools.Method(typeof(Player), "HaveStamina", (Type[])null, (Type[])null), Hook("CheckStamina"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		catch
		{
			patches.UnpatchSelf();
			((Behaviour)this).enabled = false;
			throw;
		}
	}

	private static HarmonyMethod Hook(string name)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Expected O, but got Unknown
		return new HarmonyMethod(typeof(RapidExplore), name, (Type[])null);
	}

	private void Update()
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer != (Object)(object)explorer)
		{
			explorer = null;
		}
		if (!Object.op_Implicit((Object)(object)localPlayer) || ((Character)localPlayer).IsDead())
		{
			explorer = null;
			return;
		}
		Keyboard current = Keyboard.current;
		if (current != null && (int)toggle.Value != 0 && ((ButtonControl)current[toggle.Value]).wasPressedThisFrame)
		{
			explorer = (Object.op_Implicit((Object)(object)explorer) ? null : localPlayer);
			((Character)localPlayer).Message((MessageType)2, Object.op_Implicit((Object)(object)explorer) ? "跑图模式已开启 ×10" : "跑图模式已关闭", 0, (Sprite)null, false);
		}
		if (Object.op_Implicit((Object)(object)explorer) && localPlayer.GetStamina() < ((Character)localPlayer).GetMaxStamina())
		{
			((Character)localPlayer).AddStamina(((Character)localPlayer).GetMaxStamina());
		}
	}

	private static bool Active(Character character)
	{
		if (Object.op_Implicit((Object)(object)explorer) && (Object)(object)explorer == (Object)(object)character)
		{
			return (Object)(object)explorer == (Object)(object)Player.m_localPlayer;
		}
		return false;
	}

	private static bool AllowDamage(Character __instance)
	{
		return !Active(__instance);
	}

	private static bool AllowStaminaUse(Player __instance)
	{
		return !Active((Character)(object)__instance);
	}

	private static bool CheckStamina(Player __instance, ref bool __result)
	{
		if (!Active((Character)(object)__instance))
		{
			return true;
		}
		__result = true;
		return false;
	}

	private static float TravelSpeed(float speed, Character character)
	{
		if (!Active(character))
		{
			return speed;
		}
		return speed * 10f;
	}

	private static IEnumerable<CodeInstruction> ScaleMovement(IEnumerable<CodeInstruction> instructions)
	{
		MethodInfo scale = AccessTools.Method(typeof(RapidExplore), "TravelSpeed", (Type[])null, (Type[])null);
		foreach (CodeInstruction instruction in instructions)
		{
			yield return instruction;
			FieldInfo field = instruction.operand as FieldInfo;
			if (!(instruction.opcode != OpCodes.Ldfld) && !(field == null) && !(field.DeclaringType != typeof(Character)))
			{
				switch (field.Name)
				{
				case "m_speed":
				case "m_walkSpeed":
				case "m_runSpeed":
				case "m_crouchSpeed":
				case "m_swimSpeed":
					yield return new CodeInstruction(OpCodes.Ldarg_0, (object)null);
					yield return new CodeInstruction(OpCodes.Call, (object)scale);
					break;
				}
			}
		}
	}

	private void OnDestroy()
	{
		explorer = null;
		if (patches != null)
		{
			patches.UnpatchSelf();
		}
	}
}