Decompiled source of SafeLox v1.0.0

SafeLox.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using SafeLox.Configuration;
using SafeLox.Core;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SafeLox")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+1eae7c24c32ec34b8159b7f8d62573a6837568a5")]
[assembly: AssemblyProduct("SafeLox")]
[assembly: AssemblyTitle("SafeLox")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/UncleBugDev/SafeLox")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SafeLox
{
	[BepInPlugin("com.unclebug.safelox", "SafeLox", "1.0.0")]
	public class SafeLoxPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.unclebug.safelox";

		public const string PluginName = "SafeLox";

		public const string PluginVersion = "1.0.0";

		private Harmony? _harmony;

		public static SafeLoxPlugin Instance { get; private set; }

		public static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			ModConfig.Initialize(((BaseUnityPlugin)this).Config);
			try
			{
				_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.unclebug.safelox");
				Log.LogInfo((object)"SafeLox v1.0.0 loaded. Lox structure damage is now blocked!");
			}
			catch (Exception arg)
			{
				Log.LogError((object)string.Format("Failed to patch {0}: {1}", "SafeLox", arg));
			}
		}

		private void OnDestroy()
		{
			Harmony? harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Log.LogInfo((object)"SafeLox unloaded.");
		}
	}
}
namespace SafeLox.Patches
{
	[HarmonyPatch(typeof(WearNTear))]
	public static class WearNTearDamagePatches
	{
		[HarmonyPatch("Damage")]
		[HarmonyPrefix]
		public static bool Damage_Prefix(HitData hit)
		{
			if (!ModConfig.BlockWearNTear.Value || !LoxHelper.ShouldBlockStructureDamage(hit))
			{
				return true;
			}
			if (ModConfig.ZeroOutDamageInsteadOfSkip.Value)
			{
				LoxHelper.ZeroDamage(hit);
				return true;
			}
			return false;
		}

		[HarmonyPatch("RPC_Damage")]
		[HarmonyPrefix]
		public static bool RPC_Damage_Prefix(HitData hit)
		{
			if (!ModConfig.BlockWearNTear.Value || !LoxHelper.ShouldBlockStructureDamage(hit))
			{
				return true;
			}
			if (ModConfig.ZeroOutDamageInsteadOfSkip.Value)
			{
				LoxHelper.ZeroDamage(hit);
				return true;
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(Destructible))]
	public static class DestructibleDamagePatches
	{
		[HarmonyPatch("Damage")]
		[HarmonyPrefix]
		public static bool Damage_Prefix(HitData hit)
		{
			if (!ModConfig.BlockDestructibles.Value || !LoxHelper.ShouldBlockStructureDamage(hit))
			{
				return true;
			}
			if (ModConfig.ZeroOutDamageInsteadOfSkip.Value)
			{
				LoxHelper.ZeroDamage(hit);
				return true;
			}
			return false;
		}

		[HarmonyPatch("RPC_Damage")]
		[HarmonyPrefix]
		public static bool RPC_Damage_Prefix(HitData hit)
		{
			if (!ModConfig.BlockDestructibles.Value || !LoxHelper.ShouldBlockStructureDamage(hit))
			{
				return true;
			}
			if (ModConfig.ZeroOutDamageInsteadOfSkip.Value)
			{
				LoxHelper.ZeroDamage(hit);
				return true;
			}
			return false;
		}
	}
}
namespace SafeLox.Core
{
	public static class LoxHelper
	{
		private const string LoxLocalizedName = "$enemy_lox";

		private const string LoxPrefabName = "Lox";

		private const string LoxCalfPrefabName = "Lox_Calf";

		public static bool ShouldBlockStructureDamage(HitData? hit)
		{
			if (!ModConfig.ModEnabled.Value || hit == null || !hit.HaveAttacker())
			{
				return false;
			}
			Character attacker = hit.GetAttacker();
			if ((Object)(object)attacker == (Object)null)
			{
				return false;
			}
			if (!IsLox(attacker))
			{
				return false;
			}
			if (ModConfig.OnlyTamedLox.Value && !attacker.IsTamed())
			{
				return false;
			}
			return true;
		}

		public static bool IsLox(Character attacker)
		{
			if (attacker.m_name == "$enemy_lox")
			{
				return true;
			}
			string prefabName = Utils.GetPrefabName(((Component)attacker).gameObject);
			if (!(prefabName == "Lox"))
			{
				return prefabName == "Lox_Calf";
			}
			return true;
		}

		public static void ZeroDamage(HitData hit)
		{
			((DamageTypes)(ref hit.m_damage)).Modify(0f);
			hit.m_pushForce = 0f;
			hit.m_staggerMultiplier = 0f;
		}
	}
}
namespace SafeLox.Configuration
{
	public static class ModConfig
	{
		public static ConfigEntry<bool> ModEnabled { get; private set; }

		public static ConfigEntry<bool> OnlyTamedLox { get; private set; }

		public static ConfigEntry<bool> BlockWearNTear { get; private set; }

		public static ConfigEntry<bool> BlockDestructibles { get; private set; }

		public static ConfigEntry<bool> ZeroOutDamageInsteadOfSkip { get; private set; }

		public static void Initialize(ConfigFile config)
		{
			ModEnabled = config.Bind<bool>("General", "Enabled", true, "Enable or disable SafeLox completely.");
			OnlyTamedLox = config.Bind<bool>("Behavior", "OnlyTamedLox", false, "If true, only tamed Loxes are prevented from damaging structures. If false, ALL Loxes (wild and tamed) deal 0 structure damage.");
			BlockWearNTear = config.Bind<bool>("Protection", "BlockPlayerStructures", true, "Prevent Loxes from damaging any player-built structures, walls, roofs, chests, crafting stations, and wards (WearNTear).");
			BlockDestructibles = config.Bind<bool>("Protection", "BlockDestructibles", true, "Prevent Loxes from damaging world destructible props/objects (Destructible).");
			ZeroOutDamageInsteadOfSkip = config.Bind<bool>("Visuals", "ShowZeroDamageText", false, "If false (recommended), completely cancels structure hits with no sparks/sound/0-text spam. If true, lets the hit pass with 0 damage.");
		}
	}
}