Decompiled source of NoMonsters v1.0.0

plugins/NoMonsters.dll

Decompiled 7 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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 Il2CppInterop.Runtime.InteropTypes;
using Microsoft.CodeAnalysis;
using Riverboat.Monsters;
using Riverboat.Monsters.Network;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("NoMonsters")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("NoMonsters")]
[assembly: AssemblyTitle("NoMonsters")]
[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 NoEnemies
{
	[BepInPlugin("nomonsters.voodoofishin", "NoMonsters", "1.0.0")]
	public class Plugin : BasePlugin
	{
		public const string Guid = "nomonsters.voodoofishin";

		public const string PluginName = "NoMonsters";

		public const string Version = "1.0.0";

		internal static ManualLogSource Logger;

		internal static ConfigEntry<bool> BlockSpawns;

		internal static ConfigEntry<bool> RemoveSceneMonsters;

		internal static ConfigEntry<float> CleanupInterval;

		internal static ConfigEntry<bool> Verbose;

		internal static ConfigEntry<float> BlockedReportInterval;

		public override void Load()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Expected O, but got Unknown
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Expected O, but got Unknown
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BasePlugin)this).Log;
			ConfigFile val = new ConfigFile(Path.Combine(Paths.ConfigPath, "nomonsters.voodoofishin.cfg"), true);
			BlockSpawns = val.Bind<bool>("General", "Block monster spawns", true, "Refuse every monster spawn at both the director and the manager, so no monster object is ever created. Covers Charybdis, Nessie, King Kroc, Bat, Slenderman, Shroud, Mosquito, Sneaky Peep, Pitcher Plant, Venus Flytrap and Mamma Spider. Spawns are decided by the lobby host, so this only takes effect in a lobby you are hosting.");
			RemoveSceneMonsters = val.Bind<bool>("General", "Remove monsters placed in the map", true, "Flytraps, pitcher plants and other monsters authored into the map already exist before any spawn happens. These are unregistered and deactivated once each. Host-side.");
			CleanupInterval = val.Bind<float>("General", "Cleanup interval", 10f, new ConfigDescription("Seconds between checks for map-placed monsters. Each check is a single pass over the registered monsters, and each monster is only acted on once.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 120f), Array.Empty<object>()));
			Verbose = val.Bind<bool>("General", "Log removals", false, "Write a log line whenever a map-placed monster is removed.");
			BlockedReportInterval = val.Bind<float>("General", "Blocked spawn report interval", 5f, new ConfigDescription("Seconds between 'blocked N spawn requests' log lines while spawns are being refused. Set to 0 to turn the report off.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 120f), Array.Empty<object>()));
			try
			{
				new Harmony("nomonsters.voodoofishin").PatchAll(typeof(Plugin).Assembly);
				Logger.LogInfo((object)"Monster spawns blocked (MonsterDirector.RequestSpawn, MonsterManager.SpawnMonster).");
				if (RemoveSceneMonsters.Value)
				{
					Logger.LogInfo((object)"Map-placed monster cleanup installed (MonsterManager.Update).");
				}
				Logger.LogInfo((object)"No Enemies is active.");
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Could not install patches: " + ex));
			}
		}
	}
	internal static class Patches
	{
		[HarmonyPatch]
		private static class BlockSpawn
		{
			private static IEnumerable<MethodBase> TargetMethods()
			{
				MethodInfo[] methods = typeof(MonsterManager).GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				foreach (MethodInfo methodInfo in methods)
				{
					if (methodInfo.Name == "SpawnMonster")
					{
						yield return methodInfo;
					}
				}
			}

			[HarmonyPrefix]
			private static bool Prefix(ref int __result)
			{
				if (!Plugin.BlockSpawns.Value)
				{
					return true;
				}
				__result = -1;
				NoteBlockedRequest();
				return false;
			}
		}

		private static readonly HashSet<long> Handled = new HashSet<long>();

		private static double _nextCleanup;

		private static int _blockedTotal;

		private static int _blockedSinceReport;

		private static double _nextBlockedReport;

		private static void NoteBlockedRequest()
		{
			_blockedTotal++;
			_blockedSinceReport++;
			float value = Plugin.BlockedReportInterval.Value;
			if (!(value <= 0f))
			{
				double unscaledTimeAsDouble = Time.unscaledTimeAsDouble;
				if (!(unscaledTimeAsDouble < _nextBlockedReport))
				{
					_nextBlockedReport = unscaledTimeAsDouble + (double)value;
					Plugin.Logger.LogInfo((object)("Blocked " + _blockedSinceReport + " monster spawn request(s) in the last " + value.ToString("0.#") + "s (" + _blockedTotal + " total)."));
					_blockedSinceReport = 0;
				}
			}
		}

		[HarmonyPatch(typeof(MonsterDirector), "RequestSpawn")]
		[HarmonyPrefix]
		private static bool BlockRequest(ref bool __result)
		{
			if (!Plugin.BlockSpawns.Value)
			{
				return true;
			}
			__result = false;
			NoteBlockedRequest();
			return false;
		}

		[HarmonyPatch(typeof(MonsterManager), "Update")]
		[HarmonyPostfix]
		private static void Cleanup(MonsterManager __instance)
		{
			if (!Plugin.RemoveSceneMonsters.Value || (Object)(object)__instance == (Object)null)
			{
				return;
			}
			double unscaledTimeAsDouble = Time.unscaledTimeAsDouble;
			if (unscaledTimeAsDouble < _nextCleanup)
			{
				return;
			}
			_nextCleanup = unscaledTimeAsDouble + (double)Plugin.CleanupInterval.Value;
			try
			{
				NetworkManager singleton = NetworkManager.Singleton;
				if (!((Object)(object)singleton != (Object)null) || singleton.IsServer)
				{
					int num = Sweep(__instance);
					if (num > 0 && Plugin.Verbose.Value)
					{
						Plugin.Logger.LogInfo((object)("Removed " + num + " map-placed monster(s)."));
					}
				}
			}
			catch (Exception ex)
			{
				Plugin.Logger.LogWarning((object)("Cleanup failed: " + ex.Message));
			}
		}

		private static int Sweep(MonsterManager manager)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			int num = 0;
			int num2 = 0;
			int num4 = default(int);
			MonsterBehaviorBase val2 = default(MonsterBehaviorBase);
			for (int i = 1; i <= 32; i++)
			{
				MonsterType val = (MonsterType)i;
				int num3 = int.MinValue;
				for (int j = 0; j < 256; j++)
				{
					if (!manager.TryGetFirstOfType(val, ref num4))
					{
						break;
					}
					if (num4 == num3)
					{
						break;
					}
					if (!manager.TryGetBehavior(num4, ref val2))
					{
						break;
					}
					num3 = num4;
					if ((Object)(object)val2 == (Object)null)
					{
						break;
					}
					try
					{
						long num5 = ((Il2CppObjectBase)val2).Pointer.ToInt64();
						if (num5 == 0L)
						{
							num5 = ((Object)val2).GetInstanceID();
						}
						if (!Handled.Add(num5))
						{
							((Component)val2).gameObject.SetActive(false);
							break;
						}
						((Component)val2).gameObject.SetActive(false);
						manager.DespawnMonster(num4);
						num++;
					}
					catch (Exception ex)
					{
						num2++;
						if (num2 == 1)
						{
							Plugin.Logger.LogDebug((object)("Could not remove monster " + num4 + ": " + ex.Message));
						}
						break;
					}
				}
			}
			return num;
		}
	}
}