Decompiled source of ChasePenry v1.5.1

ChasePenry.dll

Decompiled 2 months ago
using System;
using System.Collections;
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.Bootstrap;
using BepInEx.Configuration;
using GameNetcodeStuff;
using HarmonyLib;
using LethalConfig;
using LethalConfig.ConfigItems;
using LethalConfig.ConfigItems.Options;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ChasePenry")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ChasePenry")]
[assembly: AssemblyTitle("ChasePenry")]
[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.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 ChasePenry
{
	[BepInPlugin("com.yourname.chasepenry", "ChasePenry", "1.35.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class ChasePenryPlugin : BaseUnityPlugin
	{
		public const string GUID = "com.yourname.chasepenry";

		public const string NAME = "ChasePenry";

		public const string VERSION = "1.35.0";

		private readonly Harmony harmony = new Harmony("com.yourname.chasepenry");

		public static List<AudioClip> MonsterDangerClips = new List<AudioClip>();

		public static List<AudioClip> DeadBodyClips = new List<AudioClip>();

		public ConfigEntry<float>? DangerVolumeConfig;

		public ConfigEntry<float>? BodyVolumeConfig;

		public ConfigEntry<float>? FadeInDurationConfig;

		public ConfigEntry<float>? FadeOutDurationConfig;

		public ConfigEntry<float>? LowVolumeThreatsVolumeConfig;

		public ConfigEntry<float>? GracePeriodConfig;

		public Dictionary<string, ConfigEntry<bool>> BlacklistConfigs = new Dictionary<string, ConfigEntry<bool>>();

		private readonly Dictionary<string, bool> defaultVanillaEnemies = new Dictionary<string, bool>
		{
			{ "Manticoil", true },
			{ "Roaming Locusts", true },
			{ "Spore Lizard", true },
			{ "Tulip Snake", true },
			{ "Docile Locust Bees", true },
			{ "Hygrodere", true },
			{ "Backwater Gunkfish", true },
			{ "Masked", true },
			{ "Bracken", false },
			{ "Bunker Spider", false },
			{ "Hoarding Bug", false },
			{ "Thumper", false },
			{ "Circuit Bees", false },
			{ "Forest Giant", false },
			{ "Earth Leviathan", false },
			{ "Baboon Hawk", false },
			{ "Jester", false },
			{ "Nutcracker", false },
			{ "Coil-Head", false },
			{ "Snare Flea", false },
			{ "Butler", false },
			{ "Clay Surgeon", false },
			{ "RadMech", false },
			{ "Eyeless Dog", false },
			{ "Maneater", false }
		};

		public static ChasePenryPlugin? Instance { get; private set; }

		private void Awake()
		{
			Instance = this;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"ChasePenry v1.35.0 has loaded successfully!");
			DangerVolumeConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Audio Settings", "Danger Audio Volume", 0.8f, "Volume scale for danger audio (0.0 to 1.0).");
			BodyVolumeConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Audio Settings", "Body Audio Volume", 0.8f, "Volume scale for body audio (0.0 to 1.0).");
			FadeInDurationConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Audio Settings", "Fade In Duration", 0.5f, "Seconds it takes for danger audio to fade in (0.0 to 10.0).");
			FadeOutDurationConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Audio Settings", "Fade Out Duration", 3f, "Seconds it takes for danger audio to fade out (0.0 to 10.0).");
			LowVolumeThreatsVolumeConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Audio Settings", "Low Volume Threat Multiplier", 0.4f, "Universal volume multiplier for passive/stealth threat states.");
			GracePeriodConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Audio Settings", "Grace Period Duration", 2f, "Duration in seconds threats keep playing audio after losing line-of-sight.");
			foreach (KeyValuePair<string, bool> defaultVanillaEnemy in defaultVanillaEnemies)
			{
				RegisterEnemyToBlacklist(defaultVanillaEnemy.Key, defaultVanillaEnemy.Value);
			}
			harmony.PatchAll();
			((MonoBehaviour)this).StartCoroutine(LoadAllAudioFiles());
			if (Chainloader.PluginInfos.ContainsKey("ainavt.lc.lethalconfig"))
			{
				SetupLethalConfigUI();
			}
		}

		private void SetupLethalConfigUI()
		{
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Expected O, but got Unknown
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Expected O, but got Unknown
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Expected O, but got Unknown
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Expected O, but got Unknown
			//IL_00c4: Expected O, but got Unknown
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Expected O, but got Unknown
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Expected O, but got Unknown
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Expected O, but got Unknown
			//IL_00f7: Expected O, but got Unknown
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Expected O, but got Unknown
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Expected O, but got Unknown
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Expected O, but got Unknown
			//IL_012a: Expected O, but got Unknown
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Expected O, but got Unknown
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Expected O, but got Unknown
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Expected O, but got Unknown
			//IL_015d: Expected O, but got Unknown
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Expected O, but got Unknown
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Expected O, but got Unknown
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Expected O, but got Unknown
			//IL_0190: Expected O, but got Unknown
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Expected O, but got Unknown
			//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Expected O, but got Unknown
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Expected O, but got Unknown
			//IL_01c3: Expected O, but got Unknown
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c8: Expected O, but got Unknown
			//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ef: Expected O, but got Unknown
			string path = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location) ?? "";
			string dangerPath = Path.Combine(path, "danger_audio");
			string bodyPath = Path.Combine(path, "body_audio");
			LethalConfigManager.AddConfigItem((BaseConfigItem)new GenericButtonConfigItem("Audio Folders", "Open Danger Audio", "Opens folder containing danger audio clips.", "Open Folder", (GenericButtonHandler)delegate
			{
				if (!Directory.Exists(dangerPath))
				{
					Directory.CreateDirectory(dangerPath);
				}
				Process.Start(new ProcessStartInfo
				{
					FileName = dangerPath,
					UseShellExecute = true
				});
			}));
			LethalConfigManager.AddConfigItem((BaseConfigItem)new GenericButtonConfigItem("Audio Folders", "Open Body Audio", "Opens folder containing body audio clips.", "Open Folder", (GenericButtonHandler)delegate
			{
				if (!Directory.Exists(bodyPath))
				{
					Directory.CreateDirectory(bodyPath);
				}
				Process.Start(new ProcessStartInfo
				{
					FileName = bodyPath,
					UseShellExecute = true
				});
			}));
			if (DangerVolumeConfig != null)
			{
				ConfigEntry<float>? dangerVolumeConfig = DangerVolumeConfig;
				FloatSliderOptions val = new FloatSliderOptions();
				((BaseRangeOptions<float>)val).Min = 0f;
				((BaseRangeOptions<float>)val).Max = 1f;
				LethalConfigManager.AddConfigItem((BaseConfigItem)new FloatSliderConfigItem(dangerVolumeConfig, val));
			}
			if (BodyVolumeConfig != null)
			{
				ConfigEntry<float>? bodyVolumeConfig = BodyVolumeConfig;
				FloatSliderOptions val2 = new FloatSliderOptions();
				((BaseRangeOptions<float>)val2).Min = 0f;
				((BaseRangeOptions<float>)val2).Max = 1f;
				LethalConfigManager.AddConfigItem((BaseConfigItem)new FloatSliderConfigItem(bodyVolumeConfig, val2));
			}
			if (FadeInDurationConfig != null)
			{
				ConfigEntry<float>? fadeInDurationConfig = FadeInDurationConfig;
				FloatSliderOptions val3 = new FloatSliderOptions();
				((BaseRangeOptions<float>)val3).Min = 0f;
				((BaseRangeOptions<float>)val3).Max = 10f;
				LethalConfigManager.AddConfigItem((BaseConfigItem)new FloatSliderConfigItem(fadeInDurationConfig, val3));
			}
			if (FadeOutDurationConfig != null)
			{
				ConfigEntry<float>? fadeOutDurationConfig = FadeOutDurationConfig;
				FloatSliderOptions val4 = new FloatSliderOptions();
				((BaseRangeOptions<float>)val4).Min = 0f;
				((BaseRangeOptions<float>)val4).Max = 10f;
				LethalConfigManager.AddConfigItem((BaseConfigItem)new FloatSliderConfigItem(fadeOutDurationConfig, val4));
			}
			if (LowVolumeThreatsVolumeConfig != null)
			{
				ConfigEntry<float>? lowVolumeThreatsVolumeConfig = LowVolumeThreatsVolumeConfig;
				FloatSliderOptions val5 = new FloatSliderOptions();
				((BaseRangeOptions<float>)val5).Min = 0f;
				((BaseRangeOptions<float>)val5).Max = 1f;
				LethalConfigManager.AddConfigItem((BaseConfigItem)new FloatSliderConfigItem(lowVolumeThreatsVolumeConfig, val5));
			}
			if (GracePeriodConfig != null)
			{
				ConfigEntry<float>? gracePeriodConfig = GracePeriodConfig;
				FloatSliderOptions val6 = new FloatSliderOptions();
				((BaseRangeOptions<float>)val6).Min = 0f;
				((BaseRangeOptions<float>)val6).Max = 10f;
				LethalConfigManager.AddConfigItem((BaseConfigItem)new FloatSliderConfigItem(gracePeriodConfig, val6));
			}
			foreach (KeyValuePair<string, ConfigEntry<bool>> blacklistConfig in BlacklistConfigs)
			{
				LethalConfigManager.AddConfigItem((BaseConfigItem)new BoolCheckBoxConfigItem(blacklistConfig.Value));
			}
		}

		public void RegisterEnemyToBlacklist(string enemyName, bool defaultBlacklist = false, bool addLethalConfigUI = false)
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Expected O, but got Unknown
			if (!string.IsNullOrEmpty(enemyName) && !BlacklistConfigs.ContainsKey(enemyName))
			{
				ConfigEntry<bool> val = ((BaseUnityPlugin)this).Config.Bind<bool>("Enemy Blacklist", enemyName, defaultBlacklist, "Blacklist " + enemyName + " from triggering danger audio.");
				BlacklistConfigs[enemyName] = val;
				if (addLethalConfigUI && Chainloader.PluginInfos.ContainsKey("ainavt.lc.lethalconfig"))
				{
					LethalConfigManager.AddConfigItem((BaseConfigItem)new BoolCheckBoxConfigItem(val));
				}
			}
		}

		public bool IsEnemyBlacklisted(string enemyName)
		{
			if (string.IsNullOrEmpty(enemyName))
			{
				return false;
			}
			string normalizedKey = GetNormalizedKey(enemyName);
			if (BlacklistConfigs.TryGetValue(normalizedKey, out ConfigEntry<bool> value))
			{
				return value.Value;
			}
			if (BlacklistConfigs.TryGetValue(enemyName, out ConfigEntry<bool> value2))
			{
				return value2.Value;
			}
			return false;
		}

		public static string GetNormalizedKey(string enemyName)
		{
			if (string.IsNullOrEmpty(enemyName))
			{
				return "";
			}
			string text = enemyName.ToLower();
			if (text.Contains("masked") || text.Contains("maskedplayerenemy"))
			{
				return "Masked";
			}
			return enemyName;
		}

		public static string GetScanNameForEnemy(EnemyAI enemy)
		{
			if ((Object)(object)enemy == (Object)null)
			{
				return "Unknown Threat";
			}
			string text = (((Object)(object)enemy.enemyType != (Object)null) ? enemy.enemyType.enemyName : "");
			if (!string.IsNullOrEmpty(text))
			{
				string text2 = text;
				switch (text2.ToLower())
				{
				case "flowerman":
					return "Bracken";
				case "centipede":
					return "Snare Flea";
				case "red locust bees":
					return "Circuit Bees";
				case "puffer":
					return "Spore Lizard";
				case "hygrodere":
				case "blob":
					return "Hygrodere";
				case "crawler":
					return "Thumper";
				case "spring":
					return "Coil-Head";
				case "sand worm":
					return "Earth Leviathan";
				case "mouthdog":
					return "Eyeless Dog";
				case "cavedweller":
					return "Maneater";
				case "masked":
				case "maskedplayerenemy":
					return "Masked";
				case "gunk fish":
				case "backwater gunkfish":
					return "Backwater Gunkfish";
				default:
					return text2;
				}
			}
			return ((Object)((Component)enemy).gameObject).name.Replace("(Clone)", "").Trim();
		}

		private IEnumerator LoadAllAudioFiles()
		{
			string pluginFolder = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			if (string.IsNullOrEmpty(pluginFolder))
			{
				yield break;
			}
			string path = Path.Combine(pluginFolder, "danger_audio");
			if (Directory.Exists(path))
			{
				string[] files = Directory.GetFiles(path, "*.wav");
				foreach (string filePath in files)
				{
					yield return ((MonoBehaviour)this).StartCoroutine(LoadAudioClipFromFile(filePath, MonsterDangerClips));
				}
			}
			else
			{
				Directory.CreateDirectory(path);
			}
			string path2 = Path.Combine(pluginFolder, "body_audio");
			if (Directory.Exists(path2))
			{
				string[] files = Directory.GetFiles(path2, "*.wav");
				foreach (string filePath2 in files)
				{
					yield return ((MonoBehaviour)this).StartCoroutine(LoadAudioClipFromFile(filePath2, DeadBodyClips));
				}
			}
			else
			{
				Directory.CreateDirectory(path2);
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"[ChasePenry] Loaded {MonsterDangerClips.Count} danger clips and {DeadBodyClips.Count} body clips.");
		}

		private IEnumerator LoadAudioClipFromFile(string filePath, List<AudioClip> targetList)
		{
			string absoluteUri = new Uri(filePath).AbsoluteUri;
			UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(absoluteUri, (AudioType)20);
			try
			{
				yield return www.SendWebRequest();
				if ((int)www.result == 1)
				{
					AudioClip content = DownloadHandlerAudioClip.GetContent(www);
					if ((Object)(object)content != (Object)null)
					{
						((Object)content).name = Path.GetFileNameWithoutExtension(filePath);
						targetList.Add(content);
					}
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("[ChasePenry] Failed to load audio from " + filePath + ": " + www.error));
				}
			}
			finally
			{
				((IDisposable)www)?.Dispose();
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class ChasePenryPatch
	{
		private struct ThreatEvaluationResult
		{
			public bool isThreatActive;

			public float volumeMultiplier;

			public bool restartAudio;

			public ThreatEvaluationResult(bool active, float vol, bool restart = false)
			{
				isThreatActive = active;
				volumeMultiplier = vol;
				restartAudio = restart;
			}
		}

		private const float CheckDistance = 14f;

		private static bool isMonsterAlarmPlaying = false;

		private static bool isBodyAlarmPlaying = false;

		private static float monsterLostSightTimer = 0f;

		private static float monsterFadeInTimer = 0f;

		private static bool isFadingIn = false;

		private static bool leviathanBreachedLastFrame = false;

		private static float leviathanBreachTimer = 0f;

		private const float LeviathanSteadyDuration = 4.5f;

		private static float bodyPlayTimer = 0f;

		private const float BodyMaxPlayDuration = 3f;

		private const float BodyFadeOutDuration = 1.5f;

		private static HashSet<int> alertedBodyIDs = new HashSet<int>();

		private static bool wasFleaAttachedLastFrame = false;

		private static AudioSource? GetOrCreateDangerAudioSource(PlayerControllerB player)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			Transform val = ((Component)player).transform.Find("ChasePenry_DangerAudioSource");
			if ((Object)(object)val != (Object)null)
			{
				return ((Component)val).GetComponent<AudioSource>();
			}
			GameObject val2 = new GameObject("ChasePenry_DangerAudioSource");
			val2.transform.SetParent(((Component)player).transform, false);
			AudioSource obj = val2.AddComponent<AudioSource>();
			obj.playOnAwake = false;
			obj.spatialBlend = 0f;
			obj.spatialize = false;
			obj.loop = true;
			return obj;
		}

		private static AudioSource? GetOrCreateBodyAudioSource(PlayerControllerB player)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			Transform val = ((Component)player).transform.Find("ChasePenry_BodyAudioSource");
			if ((Object)(object)val != (Object)null)
			{
				return ((Component)val).GetComponent<AudioSource>();
			}
			GameObject val2 = new GameObject("ChasePenry_BodyAudioSource");
			val2.transform.SetParent(((Component)player).transform, false);
			AudioSource obj = val2.AddComponent<AudioSource>();
			obj.playOnAwake = false;
			obj.spatialBlend = 0f;
			obj.spatialize = false;
			obj.loop = false;
			return obj;
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void Postfix(PlayerControllerB __instance)
		{
			if ((Object)(object)__instance == (Object)null || !((NetworkBehaviour)__instance).IsOwner || __instance.isPlayerDead || (Object)(object)ChasePenryPlugin.Instance == (Object)null)
			{
				return;
			}
			AudioSource orCreateDangerAudioSource = GetOrCreateDangerAudioSource(__instance);
			AudioSource orCreateBodyAudioSource = GetOrCreateBodyAudioSource(__instance);
			if ((Object)(object)orCreateDangerAudioSource == (Object)null || (Object)(object)orCreateBodyAudioSource == (Object)null)
			{
				return;
			}
			float deltaTime = Time.deltaTime;
			float num = ChasePenryPlugin.Instance.DangerVolumeConfig?.Value ?? 0.8f;
			float num2 = ChasePenryPlugin.Instance.BodyVolumeConfig?.Value ?? 0.8f;
			float num3 = Mathf.Max(0.01f, ChasePenryPlugin.Instance.FadeInDurationConfig?.Value ?? 0.5f);
			float num4 = Mathf.Max(0.01f, ChasePenryPlugin.Instance.FadeOutDurationConfig?.Value ?? 3f);
			float num5 = ChasePenryPlugin.Instance.GracePeriodConfig?.Value ?? 2f;
			ThreatEvaluationResult threatEvaluationResult = EvaluateThreats(__instance);
			bool isThreatActive = threatEvaluationResult.isThreatActive;
			float volumeMultiplier = threatEvaluationResult.volumeMultiplier;
			float num6 = num * volumeMultiplier;
			if (isThreatActive)
			{
				monsterLostSightTimer = 0f;
				if (threatEvaluationResult.restartAudio && ChasePenryPlugin.MonsterDangerClips.Count > 0)
				{
					isMonsterAlarmPlaying = true;
					isFadingIn = false;
					AudioClip clip = ChasePenryPlugin.MonsterDangerClips[Random.Range(0, ChasePenryPlugin.MonsterDangerClips.Count)];
					orCreateDangerAudioSource.clip = clip;
					orCreateDangerAudioSource.volume = num6;
					if (!orCreateDangerAudioSource.isPlaying)
					{
						orCreateDangerAudioSource.Play();
					}
				}
				else if (!isMonsterAlarmPlaying && ChasePenryPlugin.MonsterDangerClips.Count > 0)
				{
					isMonsterAlarmPlaying = true;
					isFadingIn = true;
					monsterFadeInTimer = 0f;
					AudioClip clip2 = ChasePenryPlugin.MonsterDangerClips[Random.Range(0, ChasePenryPlugin.MonsterDangerClips.Count)];
					orCreateDangerAudioSource.clip = clip2;
					orCreateDangerAudioSource.volume = 0f;
					orCreateDangerAudioSource.Play();
				}
				else if (isFadingIn && orCreateDangerAudioSource.isPlaying)
				{
					monsterFadeInTimer += deltaTime;
					float num7 = monsterFadeInTimer / num3;
					orCreateDangerAudioSource.volume = Mathf.Lerp(0f, num6, num7);
					if (num7 >= 1f)
					{
						isFadingIn = false;
						orCreateDangerAudioSource.volume = num6;
					}
				}
				else if (orCreateDangerAudioSource.isPlaying)
				{
					orCreateDangerAudioSource.volume = Mathf.MoveTowards(orCreateDangerAudioSource.volume, num6, deltaTime * 5f);
				}
			}
			else if (isMonsterAlarmPlaying)
			{
				isFadingIn = false;
				monsterLostSightTimer += deltaTime;
				if (monsterLostSightTimer <= num5)
				{
					orCreateDangerAudioSource.volume = Mathf.MoveTowards(orCreateDangerAudioSource.volume, num6, deltaTime * 5f);
				}
				else
				{
					if (orCreateDangerAudioSource.isPlaying)
					{
						orCreateDangerAudioSource.volume = Mathf.MoveTowards(orCreateDangerAudioSource.volume, 0f, deltaTime * (num6 / num4));
					}
					if (!orCreateDangerAudioSource.isPlaying || orCreateDangerAudioSource.volume <= 0.005f)
					{
						orCreateDangerAudioSource.Stop();
						orCreateDangerAudioSource.volume = 0f;
						isMonsterAlarmPlaying = false;
						monsterLostSightTimer = 0f;
					}
				}
			}
			else if (orCreateDangerAudioSource.isPlaying)
			{
				orCreateDangerAudioSource.Stop();
				orCreateDangerAudioSource.volume = 0f;
			}
			DeadBodyInfo visibleDeadBody = GetVisibleDeadBody(__instance);
			bool flag = (Object)(object)visibleDeadBody != (Object)null;
			if (flag && (Object)(object)visibleDeadBody != (Object)null && !isBodyAlarmPlaying && ChasePenryPlugin.DeadBodyClips.Count > 0)
			{
				int instanceID = ((Object)visibleDeadBody).GetInstanceID();
				if (!alertedBodyIDs.Contains(instanceID))
				{
					alertedBodyIDs.Add(instanceID);
					isBodyAlarmPlaying = true;
					bodyPlayTimer = 0f;
					AudioClip clip3 = ChasePenryPlugin.DeadBodyClips[Random.Range(0, ChasePenryPlugin.DeadBodyClips.Count)];
					orCreateBodyAudioSource.clip = clip3;
					orCreateBodyAudioSource.volume = num2 * 0.85f;
					orCreateBodyAudioSource.Play();
				}
			}
			if (isBodyAlarmPlaying)
			{
				bodyPlayTimer += deltaTime;
				if (bodyPlayTimer >= 3f || !flag)
				{
					if (!flag && bodyPlayTimer < 3f)
					{
						if (orCreateBodyAudioSource.isPlaying)
						{
							orCreateBodyAudioSource.volume = Mathf.MoveTowards(orCreateBodyAudioSource.volume, 0f, deltaTime * 2f);
						}
					}
					else
					{
						float num8 = (bodyPlayTimer - 3f) / 1.5f;
						if (orCreateBodyAudioSource.isPlaying)
						{
							orCreateBodyAudioSource.volume = Mathf.Lerp(num2 * 0.85f, 0f, num8);
						}
					}
					if (bodyPlayTimer >= 4.5f || (!flag && orCreateBodyAudioSource.volume <= 0.05f))
					{
						orCreateBodyAudioSource.Stop();
						isBodyAlarmPlaying = false;
						bodyPlayTimer = 0f;
					}
				}
			}
			if (!flag && !isBodyAlarmPlaying)
			{
				CleanupDistantBodies();
			}
		}

		private static bool HasLineOfSight(PlayerControllerB localPlayer, Vector3 targetPosition, Camera cam)
		{
			//IL_0011: 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)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)cam == (Object)null)
			{
				return false;
			}
			Vector3 position = ((Component)cam).transform.position;
			Vector3 val = cam.WorldToViewportPoint(targetPosition);
			if (!(val.z > 0f) || !(val.x >= 0f) || !(val.x <= 1f) || !(val.y >= 0f) || !(val.y <= 1f))
			{
				return false;
			}
			int num = (((Object)(object)StartOfRound.Instance != (Object)null) ? StartOfRound.Instance.collidersAndRoomMask : (-1));
			RaycastHit val2 = default(RaycastHit);
			if (Physics.Linecast(position, targetPosition, ref val2, num, (QueryTriggerInteraction)1) && !((RaycastHit)(ref val2)).transform.IsChildOf(((Component)localPlayer).transform))
			{
				return false;
			}
			return true;
		}

		private static ThreatEvaluationResult EvaluateThreats(PlayerControllerB localPlayer)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01df: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0285: Unknown result type (might be due to invalid IL or missing references)
			//IL_028b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			//IL_0297: Unknown result type (might be due to invalid IL or missing references)
			//IL_029e: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_052a: Unknown result type (might be due to invalid IL or missing references)
			//IL_052f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0531: Unknown result type (might be due to invalid IL or missing references)
			//IL_0532: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0401: Unknown result type (might be due to invalid IL or missing references)
			//IL_0406: Unknown result type (might be due to invalid IL or missing references)
			//IL_0409: Unknown result type (might be due to invalid IL or missing references)
			//IL_0766: Unknown result type (might be due to invalid IL or missing references)
			//IL_0684: Unknown result type (might be due to invalid IL or missing references)
			//IL_0686: Unknown result type (might be due to invalid IL or missing references)
			//IL_0690: Unknown result type (might be due to invalid IL or missing references)
			//IL_0695: Unknown result type (might be due to invalid IL or missing references)
			//IL_069a: Unknown result type (might be due to invalid IL or missing references)
			//IL_06be: Unknown result type (might be due to invalid IL or missing references)
			//IL_06c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_08a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_08a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_08ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_08b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_08b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_08bb: Unknown result type (might be due to invalid IL or missing references)
			EnemyAI[] array = Object.FindObjectsOfType<EnemyAI>();
			if (array == null || array.Length == 0)
			{
				wasFleaAttachedLastFrame = false;
				leviathanBreachedLastFrame = false;
				return new ThreatEvaluationResult(active: false, 1f);
			}
			Camera val = (((Object)(object)localPlayer.gameplayCamera != (Object)null) ? localPlayer.gameplayCamera : Camera.main);
			Vector3 position = ((Component)localPlayer).transform.position;
			float vol = ChasePenryPlugin.Instance?.LowVolumeThreatsVolumeConfig?.Value ?? 0.4f;
			EnemyAI[] array2 = array;
			foreach (EnemyAI val2 in array2)
			{
				if ((Object)(object)val2 == (Object)null)
				{
					continue;
				}
				string scanNameForEnemy = ChasePenryPlugin.GetScanNameForEnemy(val2);
				string text = (((Object)(object)val2.enemyType != (Object)null) ? val2.enemyType.enemyName : "");
				if ((Object)(object)ChasePenryPlugin.Instance != (Object)null)
				{
					if (!ChasePenryPlugin.Instance.BlacklistConfigs.ContainsKey(scanNameForEnemy))
					{
						ChasePenryPlugin.Instance.RegisterEnemyToBlacklist(scanNameForEnemy);
					}
					if (!string.IsNullOrEmpty(text) && !ChasePenryPlugin.Instance.BlacklistConfigs.ContainsKey(text))
					{
						ChasePenryPlugin.Instance.RegisterEnemyToBlacklist(text);
					}
				}
			}
			bool flag = false;
			bool flag2 = false;
			array2 = array;
			foreach (EnemyAI val3 in array2)
			{
				if ((Object)(object)val3 == (Object)null || val3.isEnemyDead)
				{
					continue;
				}
				string scanNameForEnemy2 = ChasePenryPlugin.GetScanNameForEnemy(val3);
				string enemyName = (((Object)(object)val3.enemyType != (Object)null) ? val3.enemyType.enemyName : "");
				if ((Object)(object)ChasePenryPlugin.Instance != (Object)null && (ChasePenryPlugin.Instance.IsEnemyBlacklisted(scanNameForEnemy2) || ChasePenryPlugin.Instance.IsEnemyBlacklisted(enemyName)))
				{
					continue;
				}
				Vector3 position2 = ((Component)val3).transform.position;
				float num = Vector3.Distance(position, position2);
				bool flag3 = num <= 14f;
				string text2 = scanNameForEnemy2.ToLower();
				if (text2.Contains("snare flea") || text2.Contains("centipede"))
				{
					CentipedeAI val4 = (CentipedeAI)(object)((val3 is CentipedeAI) ? val3 : null);
					if ((Object)(object)val4 != (Object)null && (Object)(object)val4.clingingToPlayer == (Object)(object)localPlayer)
					{
						flag = true;
						bool restart = !wasFleaAttachedLastFrame;
						wasFleaAttachedLastFrame = true;
						return new ThreatEvaluationResult(active: true, vol, restart);
					}
				}
				else if (text2.Contains("earth leviathan") || text2.Contains("sand worm"))
				{
					float num2 = Vector2.Distance(new Vector2(position.x, position.z), new Vector2(position2.x, position2.z));
					if (val3.currentBehaviourStateIndex == 1 && position2.y >= position.y - 2f && num2 <= 45f)
					{
						flag2 = true;
						if (!leviathanBreachedLastFrame)
						{
							leviathanBreachTimer = 0f;
						}
					}
				}
				else if (text2.Contains("eyeless dog") || text2.Contains("mouthdog"))
				{
					if (num <= 21f)
					{
						float num3 = Mathf.Clamp01(1f - num / 21f);
						return new ThreatEvaluationResult(active: true, Mathf.Max(0.15f, num3 * 0.9f));
					}
				}
				else if (text2.Contains("coil-head"))
				{
					SpringManAI val5 = (SpringManAI)(object)((val3 is SpringManAI) ? val3 : null);
					bool num4 = (Object)(object)val5 != (Object)null && ((EnemyAI)val5).currentBehaviourStateIndex == 1;
					bool flag4 = num <= 3f;
					if (num4 || flag4)
					{
						return new ThreatEvaluationResult(active: true, 1f);
					}
				}
				else
				{
					if (!text2.Contains("nutcracker"))
					{
						continue;
					}
					NutcrackerEnemyAI val6 = (NutcrackerEnemyAI)(object)((val3 is NutcrackerEnemyAI) ? val3 : null);
					if ((!((Object)(object)val6 != (Object)null) || (((EnemyAI)val6).currentBehaviourStateIndex != 2 && !((Object)(object)((EnemyAI)val6).targetPlayer == (Object)(object)localPlayer))) && flag3 && (Object)(object)val != (Object)null)
					{
						Vector3 targetPosition = position2 + Vector3.up * 1.5f;
						if (HasLineOfSight(localPlayer, targetPosition, val))
						{
							return new ThreatEvaluationResult(active: true, vol);
						}
					}
				}
			}
			if (!flag)
			{
				wasFleaAttachedLastFrame = false;
			}
			if (flag2)
			{
				leviathanBreachedLastFrame = true;
				leviathanBreachTimer += Time.deltaTime;
				if (leviathanBreachTimer <= 4.5f)
				{
					return new ThreatEvaluationResult(active: true, 1f);
				}
			}
			else if (leviathanBreachedLastFrame)
			{
				leviathanBreachTimer += Time.deltaTime;
				if (leviathanBreachTimer <= 6.5f)
				{
					return new ThreatEvaluationResult(active: true, vol);
				}
				leviathanBreachedLastFrame = false;
			}
			array2 = array;
			RaycastHit val10 = default(RaycastHit);
			foreach (EnemyAI val7 in array2)
			{
				if ((Object)(object)val7 == (Object)null || val7.isEnemyDead)
				{
					continue;
				}
				string scanNameForEnemy3 = ChasePenryPlugin.GetScanNameForEnemy(val7);
				string enemyName2 = (((Object)(object)val7.enemyType != (Object)null) ? val7.enemyType.enemyName : "");
				if ((Object)(object)ChasePenryPlugin.Instance != (Object)null && (ChasePenryPlugin.Instance.IsEnemyBlacklisted(scanNameForEnemy3) || ChasePenryPlugin.Instance.IsEnemyBlacklisted(enemyName2)))
				{
					continue;
				}
				Vector3 position3 = ((Component)val7).transform.position;
				float num5 = Vector3.Distance(position, position3);
				bool flag5 = num5 <= 14f;
				string text3 = scanNameForEnemy3.ToLower();
				if (text3.Contains("snare flea") || text3.Contains("centipede") || text3.Contains("earth leviathan") || text3.Contains("sand worm"))
				{
					continue;
				}
				if (text3.Contains("hoarding bug") || text3.Contains("hoardingbug"))
				{
					if ((val7.currentBehaviourStateIndex == 1 || (Object)(object)val7.targetPlayer == (Object)(object)localPlayer) && (flag5 || ((Object)(object)val7.targetPlayer == (Object)(object)localPlayer && num5 <= 21f)))
					{
						return new ThreatEvaluationResult(active: true, 1f);
					}
					continue;
				}
				if (text3.Contains("nutcracker"))
				{
					NutcrackerEnemyAI val8 = (NutcrackerEnemyAI)(object)((val7 is NutcrackerEnemyAI) ? val7 : null);
					bool flag6 = (Object)(object)val8 != (Object)null && (Object)(object)((EnemyAI)val8).targetPlayer == (Object)(object)localPlayer;
					bool flag7 = (Object)(object)val8 != (Object)null && ((EnemyAI)val8).currentBehaviourStateIndex == 2;
					if (!(flag6 || flag7) || !(num5 <= 21f))
					{
						continue;
					}
					if (flag6)
					{
						return new ThreatEvaluationResult(active: true, 1f);
					}
					if ((Object)(object)val != (Object)null)
					{
						Vector3 val9 = position3 + Vector3.up * 1.5f;
						int num6 = (((Object)(object)StartOfRound.Instance != (Object)null) ? StartOfRound.Instance.collidersAndRoomMask : (-1));
						if (!Physics.Linecast(((Component)val).transform.position, val9, ref val10, num6, (QueryTriggerInteraction)1) || ((RaycastHit)(ref val10)).transform.IsChildOf(((Component)localPlayer).transform))
						{
							return new ThreatEvaluationResult(active: true, 1f);
						}
					}
					continue;
				}
				if (text3.Contains("jester"))
				{
					JesterAI val11 = (JesterAI)(object)((val7 is JesterAI) ? val7 : null);
					if ((Object)(object)val11 != (Object)null && ((EnemyAI)val11).currentBehaviourStateIndex == 2)
					{
						return new ThreatEvaluationResult(active: true, 1f);
					}
				}
				if (text3.Contains("maneater") || text3.Contains("cavedweller"))
				{
					CaveDwellerAI val12 = (CaveDwellerAI)(object)((val7 is CaveDwellerAI) ? val7 : null);
					if ((Object)(object)val12 != (Object)null && (((Component)val12).transform.localScale.y >= 1.3f || (Object)(object)((EnemyAI)val12).targetPlayer == (Object)(object)localPlayer) && (flag5 || (Object)(object)val7.targetPlayer == (Object)(object)localPlayer || val7.movingTowardsTargetPlayer))
					{
						return new ThreatEvaluationResult(active: true, 1f);
					}
					continue;
				}
				if (text3.Contains("butler"))
				{
					if (val7.currentBehaviourStateIndex == 1 && (flag5 || (Object)(object)val7.targetPlayer == (Object)(object)localPlayer))
					{
						return new ThreatEvaluationResult(active: true, 1f);
					}
					continue;
				}
				if (text3.Contains("jester"))
				{
					JesterAI val13 = (JesterAI)(object)((val7 is JesterAI) ? val7 : null);
					if ((Object)(object)val13 != (Object)null && ((EnemyAI)val13).currentBehaviourStateIndex == 0 && flag5)
					{
						Traverse obj = Traverse.Create((object)val13).Field("marineTimer");
						float vol2 = Mathf.Clamp01(((obj != null) ? obj.GetValue<float>() : 0f) / 40f);
						return new ThreatEvaluationResult(active: true, vol2);
					}
					continue;
				}
				bool flag8 = (Object)(object)val7.targetPlayer == (Object)(object)localPlayer;
				bool flag9 = val7.currentBehaviourStateIndex == 1 || val7.movingTowardsTargetPlayer || flag8;
				if ((!flag5 && !flag8) || !flag9)
				{
					continue;
				}
				if (!flag8)
				{
					if (!((Object)(object)val != (Object)null))
					{
						continue;
					}
					Vector3 targetPosition2 = position3 + Vector3.up * 1f;
					if (!HasLineOfSight(localPlayer, targetPosition2, val))
					{
						continue;
					}
				}
				else if (num5 > 21f)
				{
					continue;
				}
				return new ThreatEvaluationResult(active: true, 1f);
			}
			return new ThreatEvaluationResult(active: false, 1f);
		}

		private static DeadBodyInfo? GetVisibleDeadBody(PlayerControllerB localPlayer)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: 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)
			//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_0095: Unknown result type (might be due to invalid IL or missing references)
			DeadBodyInfo[] array = Object.FindObjectsOfType<DeadBodyInfo>();
			if (array == null || array.Length == 0)
			{
				return null;
			}
			Camera val = (((Object)(object)localPlayer.gameplayCamera != (Object)null) ? localPlayer.gameplayCamera : Camera.main);
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			Vector3 position = ((Component)localPlayer).transform.position;
			DeadBodyInfo[] array2 = array;
			foreach (DeadBodyInfo val2 in array2)
			{
				if (!((Object)(object)val2 == (Object)null) && !(Vector3.Distance(position, ((Component)val2).transform.position) > 14f))
				{
					Vector3 targetPosition = ((Component)val2).transform.position + Vector3.up * 0.5f;
					if (HasLineOfSight(localPlayer, targetPosition, val))
					{
						return val2;
					}
				}
			}
			return null;
		}

		private static void CleanupDistantBodies()
		{
			DeadBodyInfo[] array = Object.FindObjectsOfType<DeadBodyInfo>();
			if (array == null || array.Length == 0)
			{
				alertedBodyIDs.Clear();
			}
		}
	}
}