Decompiled source of EnemiesStayDead v1.0.0

EnemiesStayDead.dll

Decompiled 8 hours ago
using System;
using System.Collections.Generic;
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 EnemiesStayDead.Patches;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace EnemiesStayDead
{
	internal static class DeathRegistry
	{
		private static readonly Dictionary<int, int> Deaths = new Dictionary<int, int>();

		private static readonly Dictionary<int, int> Spawns = new Dictionary<int, int>();

		internal static void RecordDeath(EnemyParent parent)
		{
			if (!((Object)(object)parent == (Object)null))
			{
				int instanceID = ((Object)parent).GetInstanceID();
				Deaths.TryGetValue(instanceID, out var value);
				Deaths[instanceID] = value + 1;
				Plugin.Trace($"[EnemiesStayDead] '{parent.enemyName}' died ({value + 1}/{Plugin.Lives.Value} lives used).");
			}
		}

		internal static void RecordSpawn(EnemyParent parent)
		{
			if (!((Object)(object)parent == (Object)null))
			{
				int instanceID = ((Object)parent).GetInstanceID();
				Spawns.TryGetValue(instanceID, out var value);
				Spawns[instanceID] = value + 1;
			}
		}

		internal static bool ShouldStayGone(EnemyParent parent)
		{
			if ((Object)(object)parent == (Object)null)
			{
				return false;
			}
			int instanceID = ((Object)parent).GetInstanceID();
			if (Deaths.TryGetValue(instanceID, out var value) && value >= Plugin.Lives.Value)
			{
				return true;
			}
			if (Plugin.BlockAllRespawns.Value && Spawns.TryGetValue(instanceID, out var value2) && value2 >= 1)
			{
				return true;
			}
			return false;
		}

		internal static void Clear()
		{
			if (Deaths.Count > 0 || Spawns.Count > 0)
			{
				Plugin.Trace($"[EnemiesStayDead] Level changed - cleared {Deaths.Count} death record(s).");
			}
			Deaths.Clear();
			Spawns.Clear();
		}
	}
	[BepInPlugin("benjamin.enemiesstaydead", "EnemiesStayDead", "1.0.0")]
	[BepInProcess("REPO.exe")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "benjamin.enemiesstaydead";

		public const string Name = "EnemiesStayDead";

		public const string Version = "1.0.0";

		internal static ManualLogSource Log;

		private Harmony _harmony;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<int> Lives;

		internal static ConfigEntry<bool> BlockAllRespawns;

		internal static ConfigEntry<bool> ForceDespawnOnDeath;

		internal static ConfigEntry<bool> Verbose;

		private void Awake()
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch. Turn off to restore vanilla respawn behaviour without uninstalling.");
			Lives = ((BaseUnityPlugin)this).Config.Bind<int>("Death", "Lives", 1, new ConfigDescription("How many times an enemy can die before it stops coming back. 1 = dead on the first kill. Higher values give monsters extra lives.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 20), Array.Empty<object>()));
			BlockAllRespawns = ((BaseUnityPlugin)this).Config.Bind<bool>("Death", "BlockAllRespawns", false, "Stop enemies returning after ANY despawn, not just death - including the routine despawn that happens when every player wanders far away. Makes levels far emptier. Off by default.");
			ForceDespawnOnDeath = ((BaseUnityPlugin)this).Config.Bind<bool>("Death", "ForceDespawnOnDeath", false, "Also force EnemyParent.Despawn() when an enemy dies. Only useful for enemies that bug out and stay alive after death (the Robe is the usual offender). Leave off if you already run UnkillableEnemyFixer.");
			Verbose = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Verbose", false, "Log every death recorded and every respawn blocked.");
			_harmony = new Harmony("benjamin.enemiesstaydead");
			_harmony.PatchAll(typeof(DeathTracker));
			_harmony.PatchAll(typeof(RespawnBlocker));
			_harmony.PatchAll(typeof(LevelResetPatch));
			Log.LogInfo((object)"EnemiesStayDead v1.0.0 loaded. Dead is dead.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		internal static void Trace(string msg)
		{
			if (Verbose != null && Verbose.Value)
			{
				Log.LogInfo((object)msg);
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "EnemiesStayDead";

		public const string PLUGIN_NAME = "EnemiesStayDead";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace EnemiesStayDead.Patches
{
	[HarmonyPatch(typeof(EnemyHealth))]
	internal static class DeathTracker
	{
		[HarmonyPostfix]
		[HarmonyPatch("Death")]
		private static void AfterDeath(EnemyHealth __instance, Vector3 _deathDirection)
		{
			if (!Plugin.Enabled.Value)
			{
				return;
			}
			Enemy enemy = __instance.enemy;
			if ((Object)(object)enemy == (Object)null)
			{
				return;
			}
			EnemyParent enemyParent = enemy.EnemyParent;
			if (!((Object)(object)enemyParent == (Object)null))
			{
				DeathRegistry.RecordDeath(enemyParent);
				if (Plugin.ForceDespawnOnDeath.Value && SemiFunc.IsMasterClientOrSingleplayer())
				{
					enemyParent.Despawn();
				}
			}
		}
	}
	[HarmonyPatch(typeof(EnemyParent))]
	internal static class RespawnBlocker
	{
		[HarmonyPrefix]
		[HarmonyPatch("Spawn")]
		private static bool BeforeSpawn(EnemyParent __instance)
		{
			if (!Plugin.Enabled.Value)
			{
				return true;
			}
			if (!SemiFunc.IsMasterClientOrSingleplayer())
			{
				return true;
			}
			if (DeathRegistry.ShouldStayGone(__instance))
			{
				__instance.DespawnedTimerSet(9999f, false);
				Plugin.Trace("[EnemiesStayDead] Blocked respawn of '" + __instance.enemyName + "'.");
				return false;
			}
			DeathRegistry.RecordSpawn(__instance);
			return true;
		}
	}
	[HarmonyPatch(typeof(RunManager))]
	internal static class LevelResetPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("ChangeLevel")]
		private static void AfterChangeLevel()
		{
			DeathRegistry.Clear();
		}
	}
}