Decompiled source of BuildHelper v0.2.1

BepInEx/plugins/BuildHelper/BuildHelper.dll

Decompiled 4 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("BuildHelper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BuildHelper")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: AssemblyTitle("fc6b973d-39c7-4be6-adc1-e6f1e5867c67")]
[assembly: AssemblyTitle("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BuildHelper;

public static class PluginInfo
{
	public const string Name = "BuildHelper";

	public const string Guid = "Xenofell.BuildHelper";

	public const string Version = "0.2";
}
[BepInPlugin("Xenofell.BuildHelper", "BuildHelper", "0.2.1")]
public class Plugin : BaseUnityPlugin
{
	public static ConfigEntry<float> WorkbenchRadius;

	public static ConfigEntry<float> WorkbenchAttachmentRadius;

	public static ConfigEntry<float> BuildDistance;

	public static ConfigEntry<float> AreaRepairRadius;

	public static ConfigEntry<bool> DropExcludedResources;

	public void Awake()
	{
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		WorkbenchRadius = ((BaseUnityPlugin)this).Config.Bind<float>("BuildHelper", "WorkbenchRadius", 150f, "Changes the workbench/stonecutter/etc distance. (Valheim default is 20.0)");
		WorkbenchAttachmentRadius = ((BaseUnityPlugin)this).Config.Bind<float>("BuildHelper", "WorkbenchAttachmentRadius", 10f, "Changes the distance that station extensions can apply to a station. (Valheim default is 5.0)");
		BuildDistance = ((BaseUnityPlugin)this).Config.Bind<float>("BuildHelper", "BuildDistance", 10f, "Changes the distance you can build using the hammer. (Valheim default is 5.0)");
		AreaRepairRadius = ((BaseUnityPlugin)this).Config.Bind<float>("BuildHelper", "AreaRepairRadius", 7.5f, "The radius of pieces that will be repaired around the original piece.");
		DropExcludedResources = ((BaseUnityPlugin)this).Config.Bind<bool>("BuildHelper", "DropExcludedResources", true, "Pieces will always drop full resources, even when a resource is flagged to never drop.");
		new Harmony("Xenofell.BuildHelper").PatchAll();
	}
}
[HarmonyPatch]
public static class Patches
{
	[HarmonyPatch(typeof(Piece), "DropResources")]
	public static class Piece_DropResources
	{
		private static MethodInfo method_Piece_IsPlacedByPlayer = AccessTools.Method(typeof(Piece), "IsPlacedByPlayer", (Type[])null, (Type[])null);

		private static FieldInfo field_Requirement_m_recover = AccessTools.Field(typeof(Requirement), "m_recover");

		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			List<CodeInstruction> list = instructions.ToList();
			for (int i = 0; i < list.Count; i++)
			{
				if (CodeInstructionExtensions.Calls(list[i], method_Piece_IsPlacedByPlayer))
				{
					list[i] = new CodeInstruction(OpCodes.Ldc_I4_1, (object)null);
					list.RemoveAt(i - 1);
				}
			}
			if (Plugin.DropExcludedResources.Value)
			{
				for (int j = 0; j < list.Count; j++)
				{
					if (CodeInstructionExtensions.LoadsField(list[j], field_Requirement_m_recover, false))
					{
						list.RemoveRange(j - 1, 3);
					}
				}
			}
			return list.AsEnumerable();
		}
	}

	[HarmonyPatch]
	public static class AreaRepair
	{
		[HarmonyPatch(typeof(Player), "UpdatePlacement")]
		public static class Player_UpdatePlacement
		{
			private static MethodInfo method_Player_Repair = AccessTools.Method(typeof(Player), "Repair", (Type[])null, (Type[])null);

			private static FieldRef<Player, Piece> field_Player_m_hoveringPiece = AccessTools.FieldRefAccess<Player, Piece>("m_hoveringPiece");

			private static MethodInfo method_RepairNearby = AccessTools.Method(typeof(Player_UpdatePlacement), "RepairNearby", (Type[])null, (Type[])null);

			public static int RepairCount;

			[HarmonyTranspiler]
			public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
			{
				List<CodeInstruction> list = instructions.ToList();
				for (int i = 0; i < list.Count; i++)
				{
					if (CodeInstructionExtensions.Calls(list[i], method_Player_Repair))
					{
						list[i].operand = method_RepairNearby;
					}
				}
				return list.AsEnumerable();
			}

			public static void RepairNearby(Player Call, ItemData toolItem, Piece _1)
			{
			//Invalid MethodBodyBlock: Invalid SEH header: 0x02
			}
		}

		[HarmonyPatch(typeof(Player), "Repair")]
		public static class Player_Repair
		{
			private static MethodInfo method_Character_Message = AccessTools.Method(typeof(Character), "Message", (Type[])null, (Type[])null);

			private static MethodInfo method_MessageNoop = AccessTools.Method(typeof(Player_Repair), "MessageNoop", (Type[])null, (Type[])null);

			[HarmonyTranspiler]
			public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
			{
				//IL_0052: Unknown result type (might be due to invalid IL or missing references)
				//IL_005c: Expected O, but got Unknown
				List<CodeInstruction> list = instructions.ToList();
				int num = 0;
				for (int i = 0; i < list.Count; i++)
				{
					if (CodeInstructionExtensions.Calls(list[i], method_Character_Message))
					{
						CodeInstruction obj = list[i];
						obj.operand = method_MessageNoop;
						obj.opcode = OpCodes.Call;
						list.Insert(i++, new CodeInstruction((num == 0) ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0, (object)null));
						num++;
					}
				}
				return list.AsEnumerable();
			}

			public static void MessageNoop(Character Call, MessageType _1, string _2, int _3, Sprite _4, bool opcode, int P_6)
			{
				Player_UpdatePlacement.RepairCount += P_6;
			}
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(CraftingStation), "Start")]
	public static void CraftingStation_Start(ref float ___m_rangeBuild, ref GameObject ___m_areaMarker)
	{
		___m_rangeBuild = Plugin.WorkbenchRadius.Value;
		if ((Object)(object)___m_areaMarker != (Object)null)
		{
			CircleProjector component = ___m_areaMarker.GetComponent<CircleProjector>();
			if ((Object)(object)component != (Object)null)
			{
				component.m_radius = ___m_rangeBuild;
			}
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Humanoid), "EquipItem")]
	public static void Humanoid_EquipItem(ItemData item, bool triggerEquipEffects)
	{
		if (item.m_shared.m_name.Contains("item_hammer") || item.m_shared.m_name.Contains("item_hoe") || item.m_shared.m_name.Contains("item_cultivator"))
		{
			item.m_shared.m_attack.m_attackStamina = 0f;
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(StationExtension), "Awake")]
	public static void StationExtension_Awake(ref float ___m_maxStationDistance)
	{
		___m_maxStationDistance = Plugin.WorkbenchAttachmentRadius.Value;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Player), "Awake")]
	public static void Player_Awake(ref float ___m_maxPlaceDistance)
	{
		___m_maxPlaceDistance = Plugin.BuildDistance.Value;
	}
}