Decompiled source of BenchFree v1.0.0

plugins/BenchFree/BenchFree.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace BenchFree;

[BepInPlugin("felih.BenchFree", "Bench Free", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "felih.BenchFree";

	public const string NAME = "Bench Free";

	public const string VERSION = "1.0.0";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<KeyboardShortcut> ToggleKey;

	internal static ConfigEntry<bool> RequireKnownStation;

	internal static ConfigEntry<bool> RemoveAndRepair;

	internal static ConfigEntry<bool> DetailedLog;

	private void Awake()
	{
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Build without the crafting station (workbench, forge, stonecutter...) nearby.");
		ToggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("1 - General", "ToggleKey", new KeyboardShortcut((KeyCode)0, (KeyCode[])(object)new KeyCode[0]), "Optional key that turns the mod on and off in game. None = no key.");
		RequireKnownStation = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Rules", "RequireKnownStation", true, "Only for stations you already know (you have built or stood next to one). Off = every piece in the hammer menu can be built anywhere.");
		RemoveAndRepair = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Rules", "RemoveAndRepair", true, "Also remove and repair pieces without their station nearby.");
		DetailedLog = ((BaseUnityPlugin)this).Config.Bind<bool>("9 - Debug", "DetailedLog", false, "Writes details to the BepInEx log.");
		new Harmony("felih.BenchFree").PatchAll(typeof(Plugin).Assembly);
		Log.LogInfo((object)"Bench Free 1.0.0 loaded");
	}

	private void Update()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		KeyboardShortcut value = ToggleKey.Value;
		if ((int)((KeyboardShortcut)(ref value)).MainKey == 0)
		{
			return;
		}
		KeyboardShortcut value2 = ToggleKey.Value;
		if (((KeyboardShortcut)(ref value2)).IsDown())
		{
			Enabled.Value = !Enabled.Value;
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer != (Object)null)
			{
				((Character)localPlayer).Message((MessageType)1, "Bench Free: " + (Enabled.Value ? "on" : "off"), 0, (Sprite)null, false);
			}
		}
	}

	internal static void Debug(string msg)
	{
		if (DetailedLog.Value)
		{
			Log.LogInfo((object)msg);
		}
	}
}
[HarmonyPatch]
internal static class StationCheck
{
	private static Piece _piece;

	private static bool _removing;

	private static readonly HashSet<string> Logged = new HashSet<string>();

	private static readonly FieldInfo KnownStationsField = AccessTools.Field(typeof(Player), "m_knownStations");

	private static bool KnowsStation(Player p, string name)
	{
		return ((KnownStationsField != null) ? (KnownStationsField.GetValue(p) as Dictionary<string, int>) : null)?.ContainsKey(name) ?? false;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Player), "HaveRequirements", new Type[]
	{
		typeof(Piece),
		typeof(RequirementMode)
	})]
	private static void BuildCheck(Player __instance, Piece piece)
	{
		_piece = (((Object)(object)__instance == (Object)(object)Player.m_localPlayer) ? piece : null);
		_removing = false;
	}

	[HarmonyFinalizer]
	[HarmonyPatch(typeof(Player), "HaveRequirements", new Type[]
	{
		typeof(Piece),
		typeof(RequirementMode)
	})]
	private static void BuildCheckDone()
	{
		_piece = null;
	}

	[HarmonyPatch(typeof(Player), "CheckCanRemovePiece")]
	[HarmonyPrefix]
	private static void RemoveCheck(Player __instance, Piece piece)
	{
		_piece = (((Object)(object)__instance == (Object)(object)Player.m_localPlayer) ? piece : null);
		_removing = true;
	}

	[HarmonyPatch(typeof(Player), "CheckCanRemovePiece")]
	[HarmonyFinalizer]
	private static void RemoveCheckDone()
	{
		_piece = null;
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(CraftingStation), "HaveBuildStationInRange")]
	private static void InRange(string name, ref CraftingStation __result)
	{
		if ((Object)(object)__result != (Object)null || (Object)(object)_piece == (Object)null || !Plugin.Enabled.Value)
		{
			return;
		}
		CraftingStation craftingStation = _piece.m_craftingStation;
		if ((Object)(object)craftingStation == (Object)null || craftingStation.m_name != name || (_removing && !Plugin.RemoveAndRepair.Value))
		{
			return;
		}
		Player localPlayer = Player.m_localPlayer;
		if (!Plugin.RequireKnownStation.Value || (!((Object)(object)localPlayer == (Object)null) && KnowsStation(localPlayer, name)))
		{
			__result = craftingStation;
			if (Plugin.DetailedLog.Value && Logged.Add((_removing ? "remove " : "build ") + _piece.m_name))
			{
				Plugin.Log.LogInfo((object)("[" + (_removing ? "remove/repair" : "build") + "] " + _piece.m_name + " allowed without " + name + " nearby"));
			}
		}
	}
}