Decompiled source of Niquelesturrets v1.2.0

Niquelesturrets.dll

Decompiled 2 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[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("Niquelesturrets")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Niquelesturrets")]
[assembly: AssemblyTitle("Niquelesturrets")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 Niquelesturrets
{
	[BepInPlugin("com.noir.Niquelesturrets", "Niquelesturrets", "1.2.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.noir.Niquelesturrets";

		public const string PluginName = "Niquelesturrets";

		public const string PluginVersion = "1.2.0";

		internal static Plugin Instance;

		private ConfigEntry<bool> removeTurrets;

		private ConfigEntry<bool> removeLandmines;

		private ConfigEntry<float> scanDelay;

		private ConfigEntry<int> scanPasses;

		private ConfigEntry<float> scanInterval;

		private ConfigEntry<bool> logRemoved;

		private Harmony harmony;

		private Coroutine cleanupCoroutine;

		private readonly HashSet<int> removed = new HashSet<int>();

		private void Awake()
		{
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			Instance = this;
			removeTurrets = ((BaseUnityPlugin)this).Config.Bind<bool>("Hazards", "Remove turrets", true, "Supprime toutes les tourelles présentes dans la facility.");
			removeLandmines = ((BaseUnityPlugin)this).Config.Bind<bool>("Hazards", "Remove landmines", true, "Supprime toutes les mines présentes dans la facility.");
			scanDelay = ((BaseUnityPlugin)this).Config.Bind<float>("Hazards", "Scan delay", 2f, "Temps d'attente après la génération du niveau.");
			scanPasses = ((BaseUnityPlugin)this).Config.Bind<int>("Hazards", "Scan passes", 5, "Nombre de passages de nettoyage.");
			scanInterval = ((BaseUnityPlugin)this).Config.Bind<float>("Hazards", "Scan interval", 0.5f, "Temps entre chaque passage.");
			logRemoved = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Log removed objects", false, "Affiche dans le log chaque objet supprimé.");
			harmony = new Harmony("com.noir.Niquelesturrets");
			harmony.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Niquelesturrets 1.2.0 loaded.");
		}

		internal void ScheduleCleanup()
		{
			if (cleanupCoroutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(cleanupCoroutine);
			}
			removed.Clear();
			cleanupCoroutine = ((MonoBehaviour)this).StartCoroutine(CleanupRoutine());
		}

		private IEnumerator CleanupRoutine()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Starting global hazard cleanup.");
			yield return (object)new WaitForSeconds(Mathf.Max(0.1f, scanDelay.Value));
			int passes = Mathf.Clamp(scanPasses.Value, 1, 10);
			for (int pass = 0; pass < passes; pass++)
			{
				int num = RemoveHazards();
				if (num > 0)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"Cleanup pass {pass + 1}/{passes}: removed {num} hazards.");
				}
				if (pass < passes - 1)
				{
					yield return (object)new WaitForSeconds(Mathf.Max(0.1f, scanInterval.Value));
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Niquelesturrets cleanup finished.");
			cleanupCoroutine = null;
		}

		private int RemoveHazards()
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			int num = 0;
			MonoBehaviour[] array = Object.FindObjectsOfType<MonoBehaviour>(true);
			foreach (MonoBehaviour val in array)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				GameObject gameObject = ((Component)val).gameObject;
				if ((Object)(object)gameObject == (Object)null)
				{
					continue;
				}
				Scene scene = gameObject.scene;
				if (!((Scene)(ref scene)).IsValid())
				{
					continue;
				}
				scene = gameObject.scene;
				if (!((Scene)(ref scene)).isLoaded)
				{
					continue;
				}
				string name = ((object)val).GetType().Name;
				bool flag = removeTurrets.Value && IsTurret(name, ((Object)gameObject).name);
				bool flag2 = removeLandmines.Value && IsLandmine(name, ((Object)gameObject).name);
				if (!flag && !flag2)
				{
					continue;
				}
				int instanceID = ((Object)gameObject).GetInstanceID();
				if (!removed.Contains(instanceID))
				{
					removed.Add(instanceID);
					if (logRemoved.Value)
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Removing " + (flag ? "turret" : "landmine") + ": " + GetPath(gameObject)));
					}
					try
					{
						Object.Destroy((Object)(object)gameObject);
						num++;
					}
					catch (Exception ex)
					{
						((BaseUnityPlugin)this).Logger.LogWarning((object)("Failed to remove " + ((Object)gameObject).name + ": " + ex.Message));
					}
				}
			}
			return num;
		}

		private static bool IsTurret(string typeName, string objectName)
		{
			if (typeName.IndexOf("Turret", StringComparison.OrdinalIgnoreCase) < 0 && objectName.IndexOf("Turret", StringComparison.OrdinalIgnoreCase) < 0)
			{
				return objectName.IndexOf("Sentry", StringComparison.OrdinalIgnoreCase) >= 0;
			}
			return true;
		}

		private static bool IsLandmine(string typeName, string objectName)
		{
			if (typeName.IndexOf("Landmine", StringComparison.OrdinalIgnoreCase) < 0 && objectName.IndexOf("Landmine", StringComparison.OrdinalIgnoreCase) < 0)
			{
				return objectName.IndexOf("Land Mine", StringComparison.OrdinalIgnoreCase) >= 0;
			}
			return true;
		}

		private static string GetPath(GameObject go)
		{
			List<string> list = new List<string>();
			Transform val = go.transform;
			while ((Object)(object)val != (Object)null)
			{
				list.Add(((Object)val).name);
				val = val.parent;
			}
			list.Reverse();
			return string.Join("/", list);
		}

		private void OnDestroy()
		{
			if (cleanupCoroutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(cleanupCoroutine);
				cleanupCoroutine = null;
			}
			if (harmony != null)
			{
				harmony.UnpatchSelf();
				harmony = null;
			}
			Instance = null;
		}
	}
	[HarmonyPatch(typeof(RoundManager), "FinishGeneratingLevel")]
	internal static class FinishGeneratingLevelPatch
	{
		private static void Postfix()
		{
			Plugin.Instance?.ScheduleCleanup();
		}
	}
	[HarmonyPatch(typeof(RoundManager), "SpawnEnemiesOutside")]
	internal static class SpawnEnemiesOutsidePatch
	{
		private static void Postfix()
		{
			Plugin.Instance?.ScheduleCleanup();
		}
	}
}