Decompiled source of FastTools v1.2.2

FastTools.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("FastTools")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("FastTools")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f56f1a49-d0ab-40c0-a18f-7787796b5b83")]
[assembly: AssemblyFileVersion("1.2.2.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.2.2.0")]
namespace FastTools;

[BepInPlugin("dev.crystal.fasttools", "Fast Tools", "1.2.2.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class FastToolsPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player))]
	private static class Player_Patches
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void Awake_Postfix(Player __instance)
		{
			__instance.m_placeDelay = PlaceDelay.Value;
			__instance.m_removeDelay = RemoveDelay.Value;
			sPlayers.Add(__instance);
		}

		[HarmonyPatch("OnDestroy")]
		[HarmonyPrefix]
		private static void OnDestroy_Prefix(Player __instance)
		{
			sPlayers.Remove(__instance);
		}
	}

	[HarmonyPatch(typeof(Player))]
	private static class Player_Placement_Patches
	{
		private enum TranspilerState
		{
			Searching,
			Calculating,
			Searching2,
			Checking,
			Checking2,
			Checking3,
			Replacing
		}

		[HarmonyPatch("UpdatePlacement")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> UpdatePlacement_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
		{
			LocalBuilder stamina = generator.DeclareLocal(typeof(float));
			stamina.SetLocalSymInfo("stamina");
			yield return new CodeInstruction(OpCodes.Ldc_R4, (object)0f);
			yield return new CodeInstruction(OpCodes.Stloc, (object)stamina.LocalIndex);
			FieldInfo sharedField = typeof(ItemData).GetField("m_shared");
			FieldInfo attackField = typeof(SharedData).GetField("m_attack");
			FieldInfo attackStaminaField = typeof(Attack).GetField("m_attackStamina");
			TranspilerState state = TranspilerState.Searching;
			CodeInstruction instruction2 = null;
			CodeInstruction instruction3 = null;
			CodeInstruction instruction4 = null;
			foreach (CodeInstruction instruction in instructions)
			{
				switch (state)
				{
				case TranspilerState.Searching:
					if (instruction.opcode == OpCodes.Call && ((MethodInfo)instruction.operand).Name.Equals("GetRightItem"))
					{
						state = TranspilerState.Calculating;
					}
					yield return instruction;
					break;
				case TranspilerState.Calculating:
					yield return instruction;
					yield return new CodeInstruction(OpCodes.Ldloc_0, (object)null);
					yield return new CodeInstruction(OpCodes.Ldfld, (object)sharedField);
					yield return new CodeInstruction(OpCodes.Ldfld, (object)attackField);
					yield return new CodeInstruction(OpCodes.Ldfld, (object)attackStaminaField);
					yield return new CodeInstruction(OpCodes.Ldc_R4, (object)StaminaUseMultiplier.Value);
					yield return new CodeInstruction(OpCodes.Mul, (object)null);
					yield return new CodeInstruction(OpCodes.Stloc, (object)stamina.LocalIndex);
					state = TranspilerState.Searching2;
					break;
				case TranspilerState.Searching2:
					if (instruction.opcode == OpCodes.Ldloc_0)
					{
						instruction2 = instruction;
						state = TranspilerState.Checking;
					}
					else
					{
						yield return instruction;
					}
					break;
				case TranspilerState.Checking:
					if (instruction.opcode == OpCodes.Ldfld && ((FieldInfo)instruction.operand).Name == sharedField.Name)
					{
						instruction3 = instruction;
						state = TranspilerState.Checking2;
					}
					else
					{
						yield return instruction2;
						yield return instruction;
						state = TranspilerState.Searching2;
					}
					break;
				case TranspilerState.Checking2:
					if (instruction.opcode == OpCodes.Ldfld && ((FieldInfo)instruction.operand).Name == attackField.Name)
					{
						instruction4 = instruction;
						state = TranspilerState.Checking3;
						break;
					}
					yield return instruction2;
					yield return instruction3;
					yield return instruction;
					state = TranspilerState.Searching2;
					break;
				case TranspilerState.Checking3:
					if (instruction.opcode == OpCodes.Ldfld && ((FieldInfo)instruction.operand).Name == attackStaminaField.Name)
					{
						state = TranspilerState.Replacing;
						break;
					}
					yield return instruction2;
					yield return instruction3;
					yield return instruction4;
					yield return instruction;
					state = TranspilerState.Searching2;
					break;
				case TranspilerState.Replacing:
					yield return new CodeInstruction(OpCodes.Ldloc, (object)stamina.LocalIndex);
					yield return instruction;
					state = TranspilerState.Searching2;
					break;
				}
			}
		}
	}

	public const string ModId = "dev.crystal.fasttools";

	public static ConfigEntry<float> PlaceDelay;

	public static ConfigEntry<float> RemoveDelay;

	public static ConfigEntry<float> StaminaUseMultiplier;

	private static Harmony sPlayerHarmony;

	private static Harmony sPlayerPlacementHarmony;

	private static readonly List<Player> sPlayers;

	static FastToolsPlugin()
	{
		sPlayers = new List<Player>();
	}

	private void Awake()
	{
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Expected O, but got Unknown
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Expected O, but got Unknown
		PlaceDelay = ((BaseUnityPlugin)this).Config.Bind<float>("Tools", "PlaceDelay", 0.25f, "The delay time for placing items, in seconds. Allowed range 0-10. Game default is 0.4.");
		PlaceDelay.SettingChanged += Delay_SettingChanged;
		RemoveDelay = ((BaseUnityPlugin)this).Config.Bind<float>("Tools", "RemoveDelay", 0.15f, "The delay time for removing items, in seconds. Allowed range 0-10. Game default is 0.25.");
		RemoveDelay.SettingChanged += Delay_SettingChanged;
		StaminaUseMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Tools", "StaminaUseMultiplier", 1f, "Multiplier to apply to the stamina cost of using a placement tool (hammer, hoe, cultivator). Game default is 1.0.");
		StaminaUseMultiplier.SettingChanged += StaminaUseMultiplier_SettingChanged;
		ClampConfig();
		sPlayerHarmony = new Harmony("dev.crystal.fasttools_Player");
		sPlayerPlacementHarmony = new Harmony("dev.crystal.fasttools_Player_Placement");
		sPlayerHarmony.PatchAll(typeof(Player_Patches));
		sPlayerPlacementHarmony.PatchAll(typeof(Player_Placement_Patches));
	}

	private void OnDestroy()
	{
		sPlayerHarmony.UnpatchSelf();
		sPlayerPlacementHarmony.UnpatchSelf();
		sPlayers.Clear();
	}

	private static void ClampConfig()
	{
		if (PlaceDelay.Value < 0f)
		{
			PlaceDelay.Value = 0f;
		}
		if (PlaceDelay.Value > 10f)
		{
			PlaceDelay.Value = 10f;
		}
		if (RemoveDelay.Value < 0f)
		{
			RemoveDelay.Value = 0f;
		}
		if (RemoveDelay.Value > 10f)
		{
			RemoveDelay.Value = 10f;
		}
		if (StaminaUseMultiplier.Value < 0f)
		{
			StaminaUseMultiplier.Value = 0f;
		}
		if (StaminaUseMultiplier.Value > 10f)
		{
			StaminaUseMultiplier.Value = 10f;
		}
	}

	private void Delay_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		foreach (Player sPlayer in sPlayers)
		{
			sPlayer.m_placeDelay = PlaceDelay.Value;
			sPlayer.m_removeDelay = RemoveDelay.Value;
		}
	}

	private void StaminaUseMultiplier_SettingChanged(object sender, EventArgs e)
	{
		sPlayerPlacementHarmony.UnpatchSelf();
		sPlayerPlacementHarmony.PatchAll(typeof(Player_Placement_Patches));
	}
}