Decompiled source of FriendlyFireToggle v1.0.2

FriendlyFireToggle.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Landfall.TABS;
using TGCore;
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.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace FriendlyFireToggle;

[BepInPlugin("org.tabs.friendlyfiretoggle", "Friendly Fire Toggle", "1.0.2")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public sealed class FriendlyFireToggleMod : TGMod
{
	internal static ConfigEntry<bool> ProtectionEnabled;

	private Harmony _harmony;

	internal static bool IsProtectionEnabled => ProtectionEnabled != null && ProtectionEnabled.Value;

	public override void AddSettings()
	{
		((BaseUnityPlugin)this).Config.SaveOnConfigSet = true;
		ProtectionEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Gameplay", "FriendlyFireProtectionEnabled", true, "When enabled, units cannot damage units on their own team. This also filters teammate-owned explosions.");
		SettingsInstance val = TGAddons.CreateSetting((SettingsType)0, "Friendly fire protection", "Blocks same-team health damage while preserving allied support effects, including Hidden Units Bishop healing.", "GAMEPLAY", 0f, ProtectionEnabled.Value ? 0f : 1f, new string[2] { "Disable friendly fire", "Enable friendly fire" }, 0f, 1f);
		val.OnValueChanged += delegate(int value)
		{
			ProtectionEnabled.Value = value == 0;
			((BaseUnityPlugin)this).Config.Save();
		};
	}

	public override void LateLaunch()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		_harmony = new Harmony("org.tabs.friendlyfiretoggle");
		_harmony.PatchAll(typeof(FriendlyFireToggleMod).Assembly);
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Friendly Fire Toggle loaded. Protection enabled: " + IsProtectionEnabled));
	}

	private void OnDestroy()
	{
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
		}
	}
}
[HarmonyPatch(typeof(HealthHandler), "TakeDamage", new Type[]
{
	typeof(float),
	typeof(Vector3),
	typeof(Unit),
	typeof(DamageType)
})]
internal static class FriendlyFireHealthPatch
{
	[HarmonyPrefix]
	private static bool Prefix(float damage, Unit ___unit, Unit damager)
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		if (!FriendlyFireToggleMod.IsProtectionEnabled)
		{
			return true;
		}
		if (damage <= 0f)
		{
			return true;
		}
		if ((Object)(object)___unit == (Object)null || (Object)(object)damager == (Object)null)
		{
			return true;
		}
		return ___unit.Team != damager.Team;
	}
}