Decompiled source of InstantBuildingRepair v0.4.0

BuildingRepair.dll

Decompiled a year ago
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
public class AedenthornUtils
{
	public static bool IgnoreKeyPresses(bool extra = false)
	{
		if (!extra)
		{
			int result;
			if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
			{
				Chat instance = Chat.instance;
				result = ((instance != null && instance.HasFocus()) ? 1 : 0);
			}
			else
			{
				result = 1;
			}
			return (byte)result != 0;
		}
		int result2;
		if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
		{
			Chat instance2 = Chat.instance;
			if ((instance2 == null || !instance2.HasFocus()) && !StoreGui.IsVisible() && !InventoryGui.IsVisible() && !Menu.IsVisible())
			{
				TextViewer instance3 = TextViewer.instance;
				result2 = ((instance3 != null && instance3.IsVisible()) ? 1 : 0);
				goto IL_00d2;
			}
		}
		result2 = 1;
		goto IL_00d2;
		IL_00d2:
		return (byte)result2 != 0;
	}

	public static bool CheckKeyDown(string value)
	{
		try
		{
			return Input.GetKeyDown(value.ToLower());
		}
		catch
		{
			return false;
		}
	}

	public static bool CheckKeyHeld(string value, bool req = true)
	{
		try
		{
			return Input.GetKey(value.ToLower());
		}
		catch
		{
			return !req;
		}
	}
}
namespace BuildingRepair;

[BepInPlugin("aedenthorn.BuildingRepair", "Building Repair", "0.4.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Terminal), "InputText")]
	private static class InputText_Patch
	{
		private static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			string text = ((TMP_InputField)__instance.m_input).text;
			if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
			{
				__instance.AddString(text);
				((BaseUnityPlugin)context).Config.Reload();
				((BaseUnityPlugin)context).Config.Save();
				__instance.AddString(((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded");
				return false;
			}
			if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " repair"))
			{
				__instance.AddString(text);
				int num = repairPieces(repairRadius.Value);
				__instance.AddString($"{((BaseUnityPlugin)context).Info.Metadata.Name} repaired {num} pieces");
				return false;
			}
			if (text.ToLower().StartsWith(typeof(BepInExPlugin).Namespace.ToLower() + " repair "))
			{
				__instance.AddString(text);
				if (int.TryParse(text.Split(new char[1] { ' ' })[2], out var result))
				{
					int num2 = repairPieces(result);
					__instance.AddString($"{((BaseUnityPlugin)context).Info.Metadata.Name} repaired {num2} pieces");
				}
				else
				{
					__instance.AddString(((BaseUnityPlugin)context).Info.Metadata.Name + " syntax error");
				}
				return false;
			}
			return true;
		}
	}

	private static readonly bool isDebug = true;

	private static BepInExPlugin context;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<bool> allowRepairOther;

	public static ConfigEntry<bool> requireCraftingStation;

	public static ConfigEntry<float> repairRadius;

	public static ConfigEntry<string> hotKey;

	public static ConfigEntry<string> repairMessage;

	public static int destroyMask = LayerMask.GetMask(new string[7] { "Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "vehicle" });

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	private void Awake()
	{
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 1277, "Nexus mod ID for updates");
		repairRadius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RepairRadius", 20f, "Radius of repair");
		allowRepairOther = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AllowRepairOther", false, "Aloow repairing other player's pieces");
		requireCraftingStation = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "RequireCraftingStation", true, "Require a nearby crafting station to repair corresponding pieces (this is a vanilla requirement)");
		hotKey = ((BaseUnityPlugin)this).Config.Bind<string>("General", "HotKey", "'", "Hotkey to initiate repair");
		repairMessage = ((BaseUnityPlugin)this).Config.Bind<string>("General", "RepairMessage", "Repaired {0} pieces.", "Repair message text.");
		if (modEnabled.Value)
		{
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}
	}

	private void Update()
	{
		if (!AedenthornUtils.IgnoreKeyPresses(extra: true) && AedenthornUtils.CheckKeyDown(hotKey.Value))
		{
			int num = repairPieces(repairRadius.Value);
			Dbgl($"Repaired {num} pieces.");
		}
	}

	private static int repairPieces(float radius)
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		Player localPlayer = Player.m_localPlayer;
		if (!Object.op_Implicit((Object)(object)localPlayer))
		{
			return 0;
		}
		int num = 0;
		Collider[] array = Physics.OverlapSphere(((Component)localPlayer).transform.position, radius, destroyMask);
		for (int i = 0; i < array.Length; i++)
		{
			Piece componentInParent = ((Component)array[i]).GetComponentInParent<Piece>();
			if (!Object.op_Implicit((Object)(object)componentInParent) || (!allowRepairOther.Value && !componentInParent.IsCreator()) || (requireCraftingStation.Value && !Traverse.Create((object)localPlayer).Method("CheckCanRemovePiece", new object[1] { componentInParent }).GetValue<bool>()))
			{
				continue;
			}
			ZNetView component = ((Component)componentInParent).GetComponent<ZNetView>();
			if (!((Object)(object)component == (Object)null))
			{
				WearNTear component2 = ((Component)componentInParent).GetComponent<WearNTear>();
				if (Object.op_Implicit((Object)(object)component2) && component2.Repair())
				{
					num++;
				}
			}
		}
		((Character)Player.m_localPlayer).Message((MessageType)2, string.Format(repairMessage.Value, num), 0, (Sprite)null);
		return num;
	}
}