Decompiled source of CadaverBloomSpawner v1.0.0

CadaverBloomSpawner.dll

Decompiled an hour ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("CadaverBloomSpawner")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CadaverBloomSpawner")]
[assembly: AssemblyTitle("CadaverBloomSpawner")]
[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 CadaverBloomSpawner
{
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "Sosisyen.CadaverBloomSpawner";

		public const string PLUGIN_NAME = "CadaverBloomSpawner";

		public const string PLUGIN_VERSION = "1.0.0";
	}
	[BepInPlugin("Sosisyen.CadaverBloomSpawner", "CadaverBloomSpawner", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ConfigEntry<float> SpawnChance;

		internal static ConfigEntry<int> SpawnCount;

		internal static ConfigEntry<bool> IgnorePowerLevel;

		internal static ConfigEntry<bool> TriggerPerGrowth;

		internal static ConfigEntry<int> BiteDamage;

		internal static ConfigEntry<bool> CanBloomsSpreadGrowths;

		internal static ConfigEntry<float> GrowthSpreadSpeedMultiplier;

		private readonly Harmony _harmony = new Harmony("Sosisyen.CadaverBloomSpawner");

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			SpawnChance = ((BaseUnityPlugin)this).Config.Bind<float>("Spawning", "SpawnChance", 100f, "Chance (0-100%) to spawn a Cadaver Bloom inside the facility when Cadaver Growths appear.");
			SpawnCount = ((BaseUnityPlugin)this).Config.Bind<int>("Spawning", "SpawnCount", 1, "Number of Cadaver Blooms to spawn when triggered.");
			IgnorePowerLevel = ((BaseUnityPlugin)this).Config.Bind<bool>("Spawning", "IgnorePowerLevel", true, "Ignore the facility's max inside power level when force-spawning Cadaver Bloom.");
			TriggerPerGrowth = ((BaseUnityPlugin)this).Config.Bind<bool>("Spawning", "TriggerPerGrowth", false, "If true, rolls the spawn chance for every individual Cadaver Growth spawned. If false, rolls once per day.");
			BiteDamage = ((BaseUnityPlugin)this).Config.Bind<int>("Balance", "BiteDamage", 20, "Damage dealt by Cadaver Bloom's bite attack (vanilla is 30). Set to -1 to keep vanilla damage.");
			CanBloomsSpreadGrowths = ((BaseUnityPlugin)this).Config.Bind<bool>("GrowthBehavior", "CanBloomsSpreadGrowths", true, "If false, Cadaver Blooms will not plant new Cadaver Growths while roaming.");
			GrowthSpreadSpeedMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("GrowthBehavior", "GrowthSpreadSpeedMultiplier", 1f, "Multiplier for how fast Cadaver Growths spread (e.g., 2.0 = twice as fast, 0.5 = twice as slow, 1.0 = vanilla).");
			_harmony.PatchAll(typeof(RoundManagerResetPatch));
			_harmony.PatchAll(typeof(RoundManagerSpawnPatch));
			_harmony.PatchAll(typeof(EnemyAIPatch));
			_harmony.PatchAll(typeof(CadaverBloomDamagePatch));
			Log.LogInfo((object)"CadaverBloomSpawner v1.0.0 by Sosisyen loaded!");
		}
	}
	[HarmonyPatch(typeof(RoundManager), "LoadNewLevel")]
	internal class RoundManagerResetPatch
	{
		private static void Prefix()
		{
			EnemyAIPatch.BloomSpawnedThisRound = false;
		}
	}
	[HarmonyPatch(typeof(RoundManager), "SpawnEnemyGameObject")]
	internal class RoundManagerSpawnPatch
	{
		private static bool Prefix(EnemyType enemyType, ref NetworkObjectReference __result)
		{
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			if (Plugin.CanBloomsSpreadGrowths.Value)
			{
				return true;
			}
			if ((Object)(object)enemyType == (Object)null)
			{
				return true;
			}
			string obj = enemyType.enemyName ?? "";
			string text = ((Object)enemyType).name ?? "";
			if (obj.IndexOf("CadaverGrowth", StringComparison.OrdinalIgnoreCase) < 0 && text.IndexOf("CadaverGrowth", StringComparison.OrdinalIgnoreCase) < 0)
			{
				return true;
			}
			StackTrace stackTrace = new StackTrace();
			for (int i = 1; i < Mathf.Min(stackTrace.FrameCount, 12); i++)
			{
				MethodBase methodBase = stackTrace.GetFrame(i)?.GetMethod();
				if (methodBase?.DeclaringType != null && methodBase.DeclaringType.Name == "CadaverBloomAI")
				{
					__result = default(NetworkObjectReference);
					return false;
				}
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(EnemyAI), "Start")]
	internal class EnemyAIPatch
	{
		internal static bool BloomSpawnedThisRound;

		private static void Postfix(EnemyAI __instance)
		{
			if ((Object)(object)RoundManager.Instance == (Object)null || !((NetworkBehaviour)RoundManager.Instance).IsServer || (Object)(object)__instance.enemyType == (Object)null)
			{
				return;
			}
			string obj = __instance.enemyType.enemyName ?? "";
			string text = ((Object)__instance.enemyType).name ?? "";
			string name = ((object)__instance).GetType().Name;
			if (obj.IndexOf("CadaverGrowth", StringComparison.OrdinalIgnoreCase) < 0 && text.IndexOf("CadaverGrowth", StringComparison.OrdinalIgnoreCase) < 0 && name.IndexOf("CadaverGrowth", StringComparison.OrdinalIgnoreCase) < 0)
			{
				return;
			}
			ApplyGrowthSpeedMultiplier(__instance);
			if (Plugin.TriggerPerGrowth.Value || !BloomSpawnedThisRound)
			{
				BloomSpawnedThisRound = true;
				if (!(Random.Range(0f, 100f) > Plugin.SpawnChance.Value))
				{
					SpawnCadaverBloomInFacility(RoundManager.Instance);
				}
			}
		}

		private static void ApplyGrowthSpeedMultiplier(EnemyAI growthAI)
		{
			float value = Plugin.GrowthSpreadSpeedMultiplier.Value;
			if (Mathf.Approximately(value, 1f) || value <= 0.01f)
			{
				return;
			}
			growthAI.AIIntervalTime /= value;
			FieldInfo[] fields = ((object)growthAI).GetType().GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (FieldInfo fieldInfo in fields)
			{
				if (!(fieldInfo.FieldType == typeof(float)))
				{
					continue;
				}
				string text = fieldInfo.Name.ToLowerInvariant();
				if (text.Contains("interval") || text.Contains("time") || text.Contains("delay") || text.Contains("cooldown"))
				{
					float num = (float)fieldInfo.GetValue(growthAI);
					if (num > 0.1f)
					{
						fieldInfo.SetValue(growthAI, num / value);
					}
				}
				else if (text.Contains("speed") || text.Contains("rate"))
				{
					float num2 = (float)fieldInfo.GetValue(growthAI);
					if (num2 > 0f)
					{
						fieldInfo.SetValue(growthAI, num2 * value);
					}
				}
			}
		}

		private static void SpawnCadaverBloomInFacility(RoundManager roundManager)
		{
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			EnemyType val = ((IEnumerable<EnemyType>)Resources.FindObjectsOfTypeAll<EnemyType>()).FirstOrDefault((Func<EnemyType, bool>)((EnemyType e) => (e.enemyName != null && e.enemyName.IndexOf("CadaverBloom", StringComparison.OrdinalIgnoreCase) >= 0) || (((Object)e).name != null && ((Object)e).name.IndexOf("CadaverBloom", StringComparison.OrdinalIgnoreCase) >= 0)));
			if ((Object)(object)val == (Object)null || roundManager.allEnemyVents == null || roundManager.allEnemyVents.Length == 0)
			{
				return;
			}
			int num = 0;
			NetworkObject val3 = default(NetworkObject);
			for (int num2 = 0; num2 < Plugin.SpawnCount.Value; num2++)
			{
				if (!Plugin.IgnorePowerLevel.Value && roundManager.currentEnemyPower + val.PowerLevel > roundManager.currentMaxInsidePower)
				{
					break;
				}
				EnemyVent obj = roundManager.allEnemyVents[Random.Range(0, roundManager.allEnemyVents.Length)];
				Vector3 position = obj.floorNode.position;
				Vector3 eulerAngles = obj.floorNode.eulerAngles;
				NetworkObjectReference val2 = roundManager.SpawnEnemyGameObject(position, eulerAngles.y, -1, val);
				if (((NetworkObjectReference)(ref val2)).TryGet(ref val3, (NetworkManager)null))
				{
					EnemyAI component = ((Component)val3).GetComponent<EnemyAI>();
					if ((Object)(object)component != (Object)null)
					{
						((MonoBehaviour)roundManager).StartCoroutine(ActivateCadaverBloomRoutine(component, position, eulerAngles));
					}
				}
				if (!Plugin.IgnorePowerLevel.Value)
				{
					roundManager.currentEnemyPower += val.PowerLevel;
				}
				num++;
			}
			Plugin.Log.LogInfo((object)$"Spawned {num} Cadaver Bloom(s) inside the facility.");
		}

		private static IEnumerator ActivateCadaverBloomRoutine(EnemyAI bloomAI, Vector3 spawnPos, Vector3 spawnRot)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			yield return null;
			if (!((Object)(object)bloomAI == (Object)null))
			{
				Type type = ((object)bloomAI).GetType();
				MethodInfo method = type.GetMethod("SetSuit", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (method != null)
				{
					method.Invoke(bloomAI, new object[1] { 0 });
				}
				PlayerControllerB val = GameNetworkManager.Instance?.localPlayerController ?? StartOfRound.Instance?.allPlayerScripts?.FirstOrDefault((Func<PlayerControllerB, bool>)((PlayerControllerB p) => (Object)(object)p != (Object)null));
				MethodInfo method2 = type.GetMethod("BurstForth", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (method2 != null && (Object)(object)val != (Object)null)
				{
					method2.Invoke(bloomAI, new object[4] { val, false, spawnPos, spawnRot });
					Plugin.Log.LogInfo((object)"CadaverBloom successfully emerged via BurstForth!");
				}
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "DamagePlayer")]
	internal class CadaverBloomDamagePatch
	{
		private static void Prefix(ref int damageNumber)
		{
			if (Plugin.BiteDamage.Value >= 0 && IsCalledByCadaverBloom())
			{
				damageNumber = Plugin.BiteDamage.Value;
			}
		}

		private static bool IsCalledByCadaverBloom()
		{
			StackTrace stackTrace = new StackTrace();
			for (int i = 1; i < Mathf.Min(stackTrace.FrameCount, 8); i++)
			{
				MethodBase methodBase = stackTrace.GetFrame(i)?.GetMethod();
				if (methodBase?.DeclaringType != null && methodBase.DeclaringType.Name == "CadaverBloomAI")
				{
					return true;
				}
			}
			return false;
		}
	}
}