Decompiled source of RespawnNoiseControl v4.0.0

RespawnNoiseControl.dll

Decompiled 2 weeks 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 HarmonyLib;
using Microsoft.CodeAnalysis;
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: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("REPOJP")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("zabuMod")]
[assembly: AssemblyTitle("zabuMod")]
[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 REPOJP.RespawnNoiseControl
{
	[BepInPlugin("REPOJP.RespawnNoiseControl", "RespawnNoiseControl", "1.1.0")]
	public sealed class RespawnNoiseControlPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "REPOJP.RespawnNoiseControl";

		public const string PluginName = "RespawnNoiseControl";

		public const string PluginVersion = "1.1.0";

		internal static ManualLogSource Log;

		internal static RespawnNoiseControlPlugin Instance;

		private Harmony _harmony;

		internal static ConfigEntry<bool> EnableMod;

		internal static ConfigEntry<float> RespawnNoiseThreshold;

		internal static ConfigEntry<float> RespawnDecreaseSeconds;

		private void Awake()
		{
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: 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
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Expected O, but got Unknown
			try
			{
				((Component)this).transform.parent = null;
				((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
				Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
				Instance = this;
				Log = ((BaseUnityPlugin)this).Logger;
				EnableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableMod", true, "Enable or disable this mod.このMODの有効無効");
				RespawnNoiseThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RespawnNoiseThreshold", 10f, new ConfigDescription("Noise radius threshold required to reduce enemy respawn timer.敵のリスポーン時間短縮が発生する音量半径しきい値", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
				RespawnDecreaseSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RespawnDecreaseSeconds", 5f, new ConfigDescription("How many seconds to reduce the enemy respawn timer when the threshold is met.しきい値到達時に敵のリスポーン時間を何秒短縮するか", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 60f), Array.Empty<object>()));
				RespawnNoiseControlReflection.Initialize();
				_harmony = new Harmony("REPOJP.RespawnNoiseControl");
				_harmony.PatchAll();
				Log.LogInfo((object)"RespawnNoiseControl 1.1.0 loaded");
				Log.LogInfo((object)("EnableMod = " + EnableMod.Value));
				Log.LogInfo((object)("RespawnNoiseThreshold = " + RespawnNoiseThreshold.Value));
				Log.LogInfo((object)("RespawnDecreaseSeconds = " + RespawnDecreaseSeconds.Value));
				Log.LogInfo((object)("ReflectionReady = " + RespawnNoiseControlReflection.IsReady()));
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Failed to initialize RespawnNoiseControl\n" + ex));
			}
		}

		private void OnDestroy()
		{
			try
			{
				if (_harmony != null)
				{
					_harmony.UnpatchSelf();
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Failed to unpatch RespawnNoiseControl\n" + ex));
			}
		}
	}
	internal static class RespawnNoiseControlReflection
	{
		private static FieldInfo enemyDirectorDebugInvestigateField;

		private static FieldInfo enemyParentSpawnedField;

		private static FieldInfo enemyParentEnemyField;

		private static FieldInfo enemyHasStateInvestigateField;

		private static FieldInfo enemyStateInvestigateField;

		private static bool missingLogged;

		internal static void Initialize()
		{
			enemyDirectorDebugInvestigateField = AccessTools.Field(typeof(EnemyDirector), "debugInvestigate");
			enemyParentSpawnedField = AccessTools.Field(typeof(EnemyParent), "Spawned");
			enemyParentEnemyField = AccessTools.Field(typeof(EnemyParent), "Enemy");
			enemyHasStateInvestigateField = AccessTools.Field(typeof(Enemy), "HasStateInvestigate");
			enemyStateInvestigateField = AccessTools.Field(typeof(Enemy), "StateInvestigate");
		}

		internal static bool IsReady()
		{
			return enemyParentSpawnedField != null && enemyParentEnemyField != null && enemyHasStateInvestigateField != null && enemyStateInvestigateField != null;
		}

		internal static void LogMissingOnce()
		{
			if (!missingLogged)
			{
				missingLogged = true;
				RespawnNoiseControlPlugin.Log.LogWarning((object)"Required reflection fields were not found. Falling back to vanilla EnemyDirector.SetInvestigate.必須Reflectionフィールドが見つからないためバニラ処理へフォールバック");
			}
		}

		internal static bool GetDebugInvestigate(EnemyDirector enemyDirector)
		{
			if (enemyDirectorDebugInvestigateField == null || (Object)(object)enemyDirector == (Object)null)
			{
				return false;
			}
			if (!(enemyDirectorDebugInvestigateField.GetValue(enemyDirector) is bool result))
			{
				return false;
			}
			return result;
		}

		internal static bool GetSpawned(EnemyParent enemyParent)
		{
			if (!(enemyParentSpawnedField.GetValue(enemyParent) is bool result))
			{
				return false;
			}
			return result;
		}

		internal static Enemy GetEnemy(EnemyParent enemyParent)
		{
			object? value = enemyParentEnemyField.GetValue(enemyParent);
			return (Enemy)((value is Enemy) ? value : null);
		}

		internal static bool GetHasStateInvestigate(Enemy enemy)
		{
			if (!(enemyHasStateInvestigateField.GetValue(enemy) is bool result))
			{
				return false;
			}
			return result;
		}

		internal static EnemyStateInvestigate GetStateInvestigate(Enemy enemy)
		{
			object? value = enemyStateInvestigateField.GetValue(enemy);
			return (EnemyStateInvestigate)((value is EnemyStateInvestigate) ? value : null);
		}
	}
	[HarmonyPatch(typeof(EnemyDirector), "SetInvestigate")]
	internal static class EnemyDirectorSetInvestigatePatch
	{
		private static bool Prefix(EnemyDirector __instance, Vector3 position, float radius, bool pathfindOnly)
		{
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (!RespawnNoiseControlPlugin.EnableMod.Value)
				{
					return true;
				}
				if ((Object)(object)__instance == (Object)null)
				{
					return true;
				}
				if (!SemiFunc.IsMasterClientOrSingleplayer())
				{
					return false;
				}
				if (!RespawnNoiseControlReflection.IsReady())
				{
					RespawnNoiseControlReflection.LogMissingOnce();
					return true;
				}
				TryCreateDebugInvestigate(__instance, position, radius);
				List<EnemyParent> enemiesSpawned = __instance.enemiesSpawned;
				if (enemiesSpawned == null)
				{
					return false;
				}
				for (int i = 0; i < enemiesSpawned.Count; i++)
				{
					EnemyParent val = enemiesSpawned[i];
					if (!((Object)(object)val == (Object)null))
					{
						if (!RespawnNoiseControlReflection.GetSpawned(val))
						{
							TryDecreaseRespawnTimer(val, radius);
						}
						else
						{
							TrySetEnemyInvestigate(val, position, radius, pathfindOnly);
						}
					}
				}
				return false;
			}
			catch (Exception ex)
			{
				if (RespawnNoiseControlPlugin.Log != null)
				{
					RespawnNoiseControlPlugin.Log.LogError((object)("Failed in EnemyDirector.SetInvestigate patch\n" + ex));
				}
				return true;
			}
		}

		private static void TryCreateDebugInvestigate(EnemyDirector enemyDirector, Vector3 position, float radius)
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (RespawnNoiseControlReflection.GetDebugInvestigate(enemyDirector) && !((Object)(object)AssetManager.instance == (Object)null) && !((Object)(object)AssetManager.instance.debugEnemyInvestigate == (Object)null))
				{
					GameObject val = Object.Instantiate<GameObject>(AssetManager.instance.debugEnemyInvestigate, position, Quaternion.identity);
					DebugEnemyInvestigate component = val.GetComponent<DebugEnemyInvestigate>();
					if ((Object)(object)component != (Object)null)
					{
						component.radius = radius;
					}
				}
			}
			catch (Exception ex)
			{
				RespawnNoiseControlPlugin.Log.LogWarning((object)("Failed to create debug investigate object\n" + ex));
			}
		}

		private static void TryDecreaseRespawnTimer(EnemyParent enemyParent, float radius)
		{
			try
			{
				if (!(radius < RespawnNoiseControlPlugin.RespawnNoiseThreshold.Value))
				{
					float num = Mathf.Max(0f, RespawnNoiseControlPlugin.RespawnDecreaseSeconds.Value);
					if (!(num <= 0f))
					{
						enemyParent.DisableDecrease(num);
					}
				}
			}
			catch (Exception ex)
			{
				RespawnNoiseControlPlugin.Log.LogWarning((object)("Failed to decrease enemy respawn timer\n" + ex));
			}
		}

		private static void TrySetEnemyInvestigate(EnemyParent enemyParent, Vector3 position, float radius, bool pathfindOnly)
		{
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Enemy enemy = RespawnNoiseControlReflection.GetEnemy(enemyParent);
				if ((Object)(object)enemy == (Object)null || !RespawnNoiseControlReflection.GetHasStateInvestigate(enemy))
				{
					return;
				}
				EnemyStateInvestigate stateInvestigate = RespawnNoiseControlReflection.GetStateInvestigate(enemy);
				if (!((Object)(object)stateInvestigate == (Object)null))
				{
					float num = stateInvestigate.rangeMultiplier;
					if (Mathf.Approximately(num, 0f))
					{
						num = 1f;
					}
					float num2 = Vector3.Distance(position, ((Component)enemy).transform.position) / num;
					if (num2 < radius)
					{
						stateInvestigate.Set(position, pathfindOnly);
					}
				}
			}
			catch (Exception ex)
			{
				RespawnNoiseControlPlugin.Log.LogWarning((object)("Failed to set enemy investigate state\n" + ex));
			}
		}
	}
}