Decompiled source of SentryNoFF v1.0.0

SentryNoFF.dll

Decompiled 16 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Player;
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 SentryNoFF;

[BepInPlugin("Mogiven_SentryNoFF", "Sentry No Friendly Fire", "1.0.0")]
public class Plugin : BasePlugin
{
	internal static ManualLogSource Logger;

	internal static ConfigEntry<bool> OnlyImmuneOwnSentry;

	public override void Load()
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Expected O, but got Unknown
		Logger = ((BasePlugin)this).Log;
		OnlyImmuneOwnSentry = ((BasePlugin)this).Config.Bind<bool>("Settings", "OnlyImmuneOwnSentry", false, "设为 true = 只免疫自己放的炮台;设为 false = 免疫全场所有人的炮台");
		Harmony val = new Harmony("Mogiven_SentryNoFF");
		Type[] array = new Type[4]
		{
			typeof(Patch_Sentry_FireBullet),
			typeof(Patch_Sentry_Shotgun),
			typeof(Patch_LocalPlayer_BulletDamage),
			typeof(Patch_Synced_BulletDamage)
		};
		Type[] array2 = array;
		foreach (Type type in array2)
		{
			try
			{
				val.PatchAll(type);
				((BasePlugin)this).Log.LogInfo((object)$"[SentryNoFF] 补丁成功: {type.Name}");
			}
			catch (Exception arg)
			{
				((BasePlugin)this).Log.LogError((object)$"[SentryNoFF] 补丁失败: {type.Name}\n{arg}");
			}
		}
	}
}
internal static class SentryFFState
{
	public static bool SentryFiring;

	public static PlayerAgent CurrentSentryOwner;

	public static bool ShouldBlockDamage(PlayerAgent hitPlayer)
	{
		if (!SentryFiring)
		{
			return false;
		}
		if (Plugin.OnlyImmuneOwnSentry.Value && (Object)(object)CurrentSentryOwner != (Object)null && (Object)(object)hitPlayer != (Object)null)
		{
			return CurrentSentryOwner.PlayerSlotIndex == hitPlayer.PlayerSlotIndex;
		}
		return true;
	}
}
[HarmonyPatch(typeof(SentryGunInstance_Firing_Bullets), "FireBullet")]
internal static class Patch_Sentry_FireBullet
{
	private static void Prefix(SentryGunInstance_Firing_Bullets __instance)
	{
		SentryFFState.SentryFiring = true;
		SentryFFState.CurrentSentryOwner = ((__instance.m_core != null) ? __instance.m_core.Owner : null);
	}

	private static void Postfix()
	{
		SentryFFState.SentryFiring = false;
		SentryFFState.CurrentSentryOwner = null;
	}
}
[HarmonyPatch(typeof(SentryGunInstance_Firing_Bullets), "UpdateFireShotgunSemi")]
internal static class Patch_Sentry_Shotgun
{
	private static void Prefix(SentryGunInstance_Firing_Bullets __instance)
	{
		SentryFFState.SentryFiring = true;
		SentryFFState.CurrentSentryOwner = ((__instance.m_core != null) ? __instance.m_core.Owner : null);
	}

	private static void Postfix()
	{
		SentryFFState.SentryFiring = false;
		SentryFFState.CurrentSentryOwner = null;
	}
}
[HarmonyPatch(typeof(Dam_PlayerDamageLocal), "BulletDamage")]
internal static class Patch_LocalPlayer_BulletDamage
{
	private static bool Prefix(Dam_PlayerDamageLocal __instance)
	{
		if (SentryFFState.ShouldBlockDamage(((Dam_PlayerDamageBase)__instance).Owner))
		{
			Plugin.Logger.LogInfo((object)"[SentryNoFF] 成功免疫了一次对【自己】的炮台伤害!");
			return false;
		}
		return true;
	}
}
[HarmonyPatch(typeof(Dam_SyncedDamageBase), "BulletDamage")]
internal static class Patch_Synced_BulletDamage
{
	private static bool Prefix(Dam_SyncedDamageBase __instance)
	{
		if (!SentryFFState.SentryFiring)
		{
			return true;
		}
		Dam_PlayerDamageBase component = ((Component)__instance).GetComponent<Dam_PlayerDamageBase>();
		if ((Object)(object)component != (Object)null && SentryFFState.ShouldBlockDamage(component.Owner))
		{
			Plugin.Logger.LogInfo((object)"[SentryNoFF] 成功免疫了一次对【队友/Bot】的炮台伤害!");
			return false;
		}
		return true;
	}
}