Decompiled source of OrbitEnemyPurge v1.0.0

OrbitEnemyPurge.dll

Decompiled a week 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 GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using OrbitEnemyPurge.Cleaner;
using Unity.Netcode;
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("v0id")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Purges, clears, and despawns all enemies when in orbit to prevent enemy transfer across moons.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("OrbitEnemyPurge")]
[assembly: AssemblyTitle("OrbitEnemyPurge")]
[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 OrbitEnemyPurge
{
	[BepInPlugin("com.v0id.orbitenemypurge", "OrbitEnemyPurge", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static class PluginInfo
		{
			public const string PLUGIN_GUID = "com.v0id.orbitenemypurge";

			public const string PLUGIN_NAME = "OrbitEnemyPurge";

			public const string PLUGIN_VERSION = "1.0.0";
		}

		private Harmony _harmony;

		public static Plugin Instance { get; private set; }

		internal static ManualLogSource Log => ((BaseUnityPlugin)Instance).Logger;

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

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

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

		private void Awake()
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Expected O, but got Unknown
			Instance = this;
			ConfigEnablePurgeOnShipLeave = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnablePurgeOnShipLeave", true, "Purge all enemies when the scoreboard / performance report is displayed at the end of the round in orbit.");
			ConfigEnablePreLandingSweep = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnablePreLandingSweep", true, "Perform a final enemy purge immediately before descending to land on a new moon.");
			ConfigLogPurgedEnemies = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "LogPurgedEnemies", true, "Log sweep executions and purged enemy information, both in orbit and when not in orbit.");
			_harmony = new Harmony("com.v0id.orbitenemypurge");
			_harmony.PatchAll();
			Log.LogInfo((object)"OrbitEnemyPurge v1.0.0 initialized successfully.");
		}
	}
}
namespace OrbitEnemyPurge.Patches
{
	[HarmonyPatch(typeof(StartOfRound))]
	public static class ShipPatches
	{
		[HarmonyPatch("SwitchMapMonitorPurpose")]
		[HarmonyPostfix]
		public static void OnSwitchMapMonitorPurpose(bool displayInfo)
		{
			if (Plugin.ConfigEnablePurgeOnShipLeave.Value && displayInfo && !((Object)(object)StartOfRound.Instance == (Object)null) && StartOfRound.Instance.shipIsLeaving)
			{
				EnemyPurgeManager.PurgeAllEnemies("ScoreboardDisplay");
			}
		}

		[HarmonyPatch("SetShipReadyToLand")]
		[HarmonyPostfix]
		public static void OnSetShipReadyToLand()
		{
			if (Plugin.ConfigEnablePurgeOnShipLeave.Value)
			{
				EnemyPurgeManager.PurgeAllEnemies("SetShipReadyToLand");
			}
		}

		[HarmonyPatch("StartGame")]
		[HarmonyPrefix]
		public static void OnStartGame()
		{
			if (Plugin.ConfigEnablePreLandingSweep.Value)
			{
				EnemyPurgeManager.PurgeAllEnemies("PreLandingSweep");
			}
		}
	}
}
namespace OrbitEnemyPurge.Cleaner
{
	public static class EnemyPurgeManager
	{
		public static int PurgeAllEnemies(string triggerReason)
		{
			try
			{
				int num = 0;
				HashSet<GameObject> hashSet = new HashSet<GameObject>();
				EnemyAI[] array = Object.FindObjectsOfType<EnemyAI>(true);
				foreach (EnemyAI val in array)
				{
					if ((Object)(object)val != (Object)null && (Object)(object)((Component)val).gameObject != (Object)null)
					{
						hashSet.Add(((Component)val).gameObject);
					}
				}
				if ((Object)(object)StartOfRound.Instance != (Object)null && (Object)(object)StartOfRound.Instance.elevatorTransform != (Object)null)
				{
					EnemyAICollisionDetect[] componentsInChildren = ((Component)StartOfRound.Instance.elevatorTransform).GetComponentsInChildren<EnemyAICollisionDetect>(true);
					foreach (EnemyAICollisionDetect val2 in componentsInChildren)
					{
						if ((Object)(object)val2 != (Object)null && (Object)(object)val2.mainScript != (Object)null && (Object)(object)((Component)val2.mainScript).gameObject != (Object)null)
						{
							hashSet.Add(((Component)val2.mainScript).gameObject);
						}
					}
					Transform[] componentsInChildren2 = ((Component)StartOfRound.Instance.elevatorTransform).GetComponentsInChildren<Transform>(true);
					int num2 = LayerMask.NameToLayer("Enemies");
					Transform[] array2 = componentsInChildren2;
					foreach (Transform val3 in array2)
					{
						if ((Object)(object)val3 != (Object)null && (Object)(object)((Component)val3).gameObject != (Object)null)
						{
							bool num3 = num2 != -1 && ((Component)val3).gameObject.layer == num2;
							bool flag = ((Component)val3).CompareTag("Enemy");
							if ((num3 || flag) && (Object)(object)((Component)val3).GetComponent<PlayerControllerB>() == (Object)null && (Object)(object)((Component)val3).GetComponent<DeadBodyInfo>() == (Object)null)
							{
								hashSet.Add(((Component)val3).gameObject);
							}
						}
					}
				}
				EnemyAINestSpawnObject[] array3 = Object.FindObjectsOfType<EnemyAINestSpawnObject>(true);
				foreach (EnemyAINestSpawnObject val4 in array3)
				{
					if ((Object)(object)val4 != (Object)null && (Object)(object)((Component)val4).gameObject != (Object)null)
					{
						hashSet.Add(((Component)val4).gameObject);
					}
				}
				string text = (((Object)(object)StartOfRound.Instance != (Object)null && StartOfRound.Instance.inShipPhase) ? "In Orbit" : "Not In Orbit");
				if (hashSet.Count == 0)
				{
					if (Plugin.ConfigLogPurgedEnemies.Value)
					{
						Plugin.Log.LogInfo((object)("Sweep executed (" + triggerReason + " | Status: " + text + "): 0 enemies found."));
					}
					return 0;
				}
				bool flag2 = (Object)(object)NetworkManager.Singleton != (Object)null && NetworkManager.Singleton.IsServer;
				foreach (GameObject item in hashSet)
				{
					if ((Object)(object)item == (Object)null)
					{
						continue;
					}
					try
					{
						if (Plugin.ConfigLogPurgedEnemies.Value)
						{
							Plugin.Log.LogInfo((object)("Purging enemy '" + ((Object)item).name + "' (" + triggerReason + " | Status: " + text + ")."));
						}
						if ((Object)(object)item.transform.parent != (Object)null && (Object)(object)StartOfRound.Instance != (Object)null && (Object)(object)StartOfRound.Instance.elevatorTransform != (Object)null && item.transform.IsChildOf(StartOfRound.Instance.elevatorTransform))
						{
							item.transform.SetParent((Transform)null, true);
						}
						NetworkObject component = item.GetComponent<NetworkObject>();
						if (flag2)
						{
							if ((Object)(object)component != (Object)null && component.IsSpawned)
							{
								component.Despawn(true);
							}
							else
							{
								Object.Destroy((Object)(object)item);
							}
						}
						else
						{
							Object.Destroy((Object)(object)item);
						}
						num++;
					}
					catch (Exception ex)
					{
						Plugin.Log.LogError((object)("Error purging enemy object " + ((Object)item).name + ": " + ex.Message));
					}
				}
				if ((Object)(object)RoundManager.Instance != (Object)null)
				{
					if (RoundManager.Instance.SpawnedEnemies != null)
					{
						RoundManager.Instance.SpawnedEnemies.Clear();
					}
					RoundManager.Instance.numberOfEnemiesInScene = 0;
					RoundManager.Instance.currentEnemyPower = 0f;
					RoundManager.Instance.currentEnemyPowerNoDeaths = 0f;
					RoundManager.Instance.currentOutsideEnemyPower = 0f;
					RoundManager.Instance.currentOutsideEnemyPowerNoDeaths = 0f;
					RoundManager.Instance.currentDaytimeEnemyPower = 0f;
					RoundManager.Instance.currentDaytimeEnemyPowerNoDeaths = 0f;
					RoundManager.Instance.currentWeedEnemyPower = 0f;
				}
				if (Plugin.ConfigLogPurgedEnemies.Value)
				{
					Plugin.Log.LogInfo((object)$"Sweep completed ({triggerReason} | Status: {text}): Purged {num} enemy entities.");
				}
				return num;
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Exception during PurgeAllEnemies ({triggerReason}): {arg}");
				return 0;
			}
		}
	}
}