Decompiled source of CasperProtections v1.0.1

plugins/CasperProtections/CasperProtections.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using CasperProtections.Patches;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("CasperProtections")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CasperProtections")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("77b95131-6918-4f34-8603-45953e40205b")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CasperProtections
{
	[BepInPlugin("com.casper.CasperProtections", "CasperProtections", "1.0.1")]
	public class CasperProtections : BaseUnityPlugin
	{
		private const string MyGUID = "com.casper.CasperProtections";

		private const string PluginName = "CasperProtections";

		private const string VersionString = "1.0.1";

		private static readonly Harmony Harmony = new Harmony("com.casper.CasperProtections");

		public static ManualLogSource Log = new ManualLogSource("CasperProtections");

		public static bool _enableProtections;

		public static bool _enableSafesprint;

		public static bool _enableErase;

		public static bool _enableToggles;

		private void Awake()
		{
			_enableProtections = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Disable Protections", true, "Disable Protection Zones").Value;
			_enableSafesprint = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Disable Safesprint", true, "Allow Running In Buildings").Value;
			_enableErase = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Disable Prebuild Protections", true, "Erase Prebuilt Objects").Value;
			_enableToggles = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Disable Toggles", true, "Only Run When Holding Run").Value;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: CasperProtections, VersionString: 1.0.1 is loading...");
			Harmony.PatchAll();
			ApplyPatches();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: CasperProtections, VersionString: 1.0.1 is loaded.");
			Log = ((BaseUnityPlugin)this).Logger;
		}

		private void ApplyPatches()
		{
			Harmony.CreateAndPatchAll(typeof(ProtPatches), (string)null);
			Harmony.CreateAndPatchAll(typeof(safesprintPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(eraserPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(togglePatch), (string)null);
		}
	}
}
namespace CasperProtections.Patches
{
	internal class eraserPatch
	{
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		public class PatchCanErase
		{
			private static readonly MachineTypeEnum[] ExcludedTypes;

			private static bool Prefix(CommonMachineInfo __instance, ref bool __result)
			{
				//IL_0010: Unknown result type (might be due to invalid IL or missing references)
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				if (CasperProtections._enableErase)
				{
					if (ExcludedTypes.Contains(__instance.typeIndex))
					{
						return true;
					}
					__result = true;
					return false;
				}
				return true;
			}

			static PatchCanErase()
			{
				MachineTypeEnum[] array = new MachineTypeEnum[4];
				RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
				ExcludedTypes = (MachineTypeEnum[])(object)array;
			}
		}
	}
	internal class ProtPatches
	{
		[HarmonyPatch(typeof(GridManager), "RegisterProtectedZone")]
		public class SkipRegisterProtectedZonePatch
		{
			private static bool Prefix(ref ProtectionZoneData protectionZone, ref byte strata)
			{
				if (CasperProtections._enableProtections)
				{
					return false;
				}
				return true;
			}
		}

		[HarmonyPatch(typeof(VoxelManager), "CanModifyVoxelAt")]
		public class PatchCanModifyVoxelAt
		{
			private static bool Prefix(ref bool __result)
			{
				if (CasperProtections._enableProtections)
				{
					__result = true;
					return false;
				}
				return true;
			}
		}
	}
	internal class safesprintPatch
	{
		[HarmonyPatch(typeof(SafeSpeedTriggerZoneData), "SetSprintState")]
		public class SkipSetSprintStatePatch
		{
			private static bool Prefix(ref bool isOn)
			{
				if (CasperProtections._enableSafesprint)
				{
					isOn = false;
					return true;
				}
				return true;
			}
		}
	}
	internal class togglePatch
	{
		[HarmonyPatch(typeof(PlayerFirstPersonController), "LateUpdate")]
		public class Patch_PlayerFirstPersonController_LateUpdate
		{
			private static readonly FieldInfo runToggledOnField = typeof(PlayerFirstPersonController).GetField("runToggledOn", BindingFlags.Instance | BindingFlags.NonPublic);

			private static bool Prefix(PlayerFirstPersonController __instance)
			{
				//IL_003d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0042: Unknown result type (might be due to invalid IL or missing references)
				if (CasperProtections._enableToggles && runToggledOnField != null)
				{
					bool sprintHeld = InputHandler.instance.SprintHeld;
					runToggledOnField.SetValue(__instance, sprintHeld);
					Vector2 moveAxes = InputHandler.instance.MoveAxes;
					if (((Vector2)(ref moveAxes)).magnitude < 0.5f)
					{
						runToggledOnField.SetValue(__instance, false);
					}
				}
				return true;
			}
		}
	}
}