Decompiled source of BodyRemover v1.0.1

BepInEx/plugins/deadbegone.dll

Decompiled 2 months 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 CleanEnemies.ConfigInfo;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("deadbegone")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CleanEnemies")]
[assembly: AssemblyTitle("deadbegone")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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 CleanEnemies
{
	[BepInPlugin("deadbegone", "CleanEnemies", "1.0.0")]
	public class CleanEnemies : BaseUnityPlugin
	{
		public static bool InGameAndHost = false;

		public static Dictionary<NetworkObject, float> Despawnlist = new Dictionary<NetworkObject, float>();

		public static CleanEnemies Instance { get; private set; } = null;


		internal static ManualLogSource Logger { get; private set; } = null;


		internal static Harmony? Harmony { get; set; }

		public static ConfigFile BepInExConfig()
		{
			return ((BaseUnityPlugin)Instance).Config;
		}

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			Config.Instance.Setup();
			Patch();
			Logger.LogInfo((object)"deadbegone v1.0.0 has loaded!");
		}

		internal static void Patch()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("deadbegone");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "deadbegone";

		public const string PLUGIN_NAME = "CleanEnemies";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace CleanEnemies.Patches
{
	[HarmonyPatch(typeof(EnemyAI))]
	public class EnemyDespawn
	{
		[HarmonyPatch("KillEnemy")]
		[HarmonyPostfix]
		private static void CheckToDespawn(EnemyAI __instance)
		{
			if (!CleanEnemies.InGameAndHost || !Config.Instance.Enabled.Value)
			{
				return;
			}
			bool flag = __instance.enemyType.enemyName == "Butler" || __instance.enemyType.enemyName == "Nutcracker";
			if (__instance.thisNetworkObject.IsSpawned & (flag == Config.Instance.ForceRemoveAll.Value || !flag))
			{
				if (Config.Instance.EnableLogging.Value && flag)
				{
					CleanEnemies.Logger.LogInfo((object)"Enemy is Special Case");
				}
				float realtimeSinceStartup = Time.realtimeSinceStartup;
				CleanEnemies.Despawnlist.Add(__instance.thisNetworkObject, realtimeSinceStartup);
				if (Config.Instance.EnableLogging.Value)
				{
					CleanEnemies.Logger.LogInfo((object)"Added {__instance.thisNetworkObject.name} to despawn list");
				}
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	public class PlayerConnection
	{
		[HarmonyPatch("ConnectClientToPlayerObject")]
		[HarmonyPostfix]
		public static void findplayer(PlayerControllerB __instance)
		{
			if (GameNetworkManager.Instance.isHostingGame)
			{
				CleanEnemies.InGameAndHost = true;
				if (Config.Instance.EnableLogging.Value)
				{
					CleanEnemies.Logger.LogInfo((object)"Host connected");
				}
			}
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	public class PlayerDisconnection
	{
		[HarmonyPatch("OnLocalDisconnect")]
		[HarmonyPostfix]
		public static void removeplayer()
		{
			CleanEnemies.InGameAndHost = false;
			CleanEnemies.Despawnlist.Clear();
			if (Config.Instance.EnableLogging.Value)
			{
				CleanEnemies.Logger.LogInfo((object)"Despawnlist has been cleared and mod is waiting to be hosting a game");
			}
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	public class UpdateListLoop
	{
		public static int CurrentFrame;

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		public static void CheckIfDespawn()
		{
			CurrentFrame++;
			if (CurrentFrame > Config.Instance.UpdateAfterMultipleFrames.Value)
			{
				CurrentFrame = 1;
			}
			if (!CleanEnemies.InGameAndHost || !Config.Instance.Enabled.Value || CurrentFrame < Config.Instance.UpdateAfterMultipleFrames.Value)
			{
				return;
			}
			foreach (KeyValuePair<NetworkObject, float> item in CleanEnemies.Despawnlist)
			{
				if (!CleanEnemies.InGameAndHost || !Config.Instance.Enabled.Value)
				{
					break;
				}
				if (Time.realtimeSinceStartup - item.Value > Config.Instance.TimeToClear.Value)
				{
					item.Key.Despawn(true);
					if (Config.Instance.EnableLogging.Value)
					{
						CleanEnemies.Logger.LogInfo((object)"{p.Key.name} has been despawned after {(Time.realtimeSinceStartup - p.Value)} seconds");
					}
					CleanEnemies.Despawnlist.Remove(item.Key);
				}
			}
		}
	}
}
namespace CleanEnemies.ConfigInfo
{
	public sealed class Config
	{
		private static Config instance;

		public ConfigEntry<bool> Enabled { get; set; }

		public ConfigEntry<bool> EnableLogging { get; set; }

		public ConfigEntry<bool> ForceRemoveAll { get; set; }

		public ConfigEntry<float> TimeToClear { get; set; }

		public ConfigEntry<int> UpdateAfterMultipleFrames { get; set; }

		public static Config Instance
		{
			get
			{
				if (instance == null)
				{
					instance = new Config();
				}
				return instance;
			}
		}

		public void Setup()
		{
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Expected O, but got Unknown
			Enabled = CleanEnemies.BepInExConfig().Bind<bool>("General", "Enabled", true, "Enables the mod");
			EnableLogging = CleanEnemies.BepInExConfig().Bind<bool>("General", "Enable Logs", true, "Enables Logging");
			ForceRemoveAll = CleanEnemies.BepInExConfig().Bind<bool>("General", "Remove Special Cases", false, "Makes nutcrackers and butlers affected by the mod, Will cause items / events after death to not spawn if time to remove is too short or be removed once enemy is destroyed");
			TimeToClear = CleanEnemies.BepInExConfig().Bind<float>("General", "Time Until Removed", 3f, new ConfigDescription("Time it takes in seconds for an enemy to be removed after it has died", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 120f), Array.Empty<object>()));
			UpdateAfterMultipleFrames = CleanEnemies.BepInExConfig().Bind<int>("General", "Update After Multiple Frames", 1, new ConfigDescription("How many frames after each loop update should it check to despawn anything", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 120), Array.Empty<object>()));
		}
	}
}