Decompiled source of Echohold Station Fix v1.0.0

EchoholdCompatPatch.dll

Decompiled 2 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using REPOLib;
using REPOLib.Modules;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Audio;
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("Empress")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.5.0")]
[assembly: AssemblyInformationalVersion("1.0.5")]
[assembly: AssemblyProduct("EchoholdCompatPatch")]
[assembly: AssemblyTitle("EchoholdCompatPatch")]
[assembly: AssemblyVersion("1.0.5.0")]
[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 EchoholdCompatPatch
{
	[BepInPlugin("empress.repo.echoholdcompatpatch", "Empress Echohold Compat Patch", "1.0.5")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class EchoholdCompatPatchPlugin : BaseUnityPlugin
	{
		private sealed class EchoholdPrefabBuckets
		{
			public List<GameObject> StartRooms { get; set; } = new List<GameObject>();


			public List<GameObject> Normal1 { get; set; } = new List<GameObject>();


			public List<GameObject> Passage1 { get; set; } = new List<GameObject>();


			public List<GameObject> DeadEnd1 { get; set; } = new List<GameObject>();


			public List<GameObject> Extraction1 { get; set; } = new List<GameObject>();


			public List<GameObject> Normal2 { get; set; } = new List<GameObject>();


			public List<GameObject> Passage2 { get; set; } = new List<GameObject>();


			public List<GameObject> DeadEnd2 { get; set; } = new List<GameObject>();


			public List<GameObject> Extraction2 { get; set; } = new List<GameObject>();


			public List<GameObject> Normal3 { get; set; } = new List<GameObject>();


			public List<GameObject> Passage3 { get; set; } = new List<GameObject>();


			public List<GameObject> DeadEnd3 { get; set; } = new List<GameObject>();


			public List<GameObject> Extraction3 { get; set; } = new List<GameObject>();


			public int TotalModuleCount => Normal1.Count + Passage1.Count + DeadEnd1.Count + Extraction1.Count + Normal2.Count + Passage2.Count + DeadEnd2.Count + Extraction2.Count + Normal3.Count + Passage3.Count + DeadEnd3.Count + Extraction3.Count;
		}

		internal const string PluginGuid = "empress.repo.echoholdcompatpatch";

		internal const string PluginName = "Empress Echohold Compat Patch";

		internal const string PluginVersion = "1.0.5";

		private const string EchoholdBundleFileName = "Echohold.repobundle";

		private const string EchoholdLevelName = "Level - Echohold";

		private const string EchoholdLevelResourcePath = "Echohold";

		private const float EchoholdLevelPointCheckRadius = 0.5f;

		private const float EchoholdLevelPointRepairRadius = 2f;

		private const float EchoholdLevelPointConnectDistance = 15f;

		private const float EchoholdLevelPointConnectForwardDot = -0.8f;

		private const float EchoholdLevelPointConnectApproachDot = 0.8f;

		private const float EchoholdLevelPointValidationDelay = 0.5f;

		private static readonly string[] EchoholdStartRoomNames = new string[1] { "Start Room - Echohold - Lab" };

		private static readonly string[] EchoholdNormal1Names = new string[5] { "Module - Echohold - N - 1 - Corridor 2", "Module - Echohold - N - 1 - Main Corridor", "Module - Echohold - N - 1 - Server Room", "Module - Echohold - N - 1 - Zero Gravity Lab", "Module - Echohold - N - 1 - Corridor 3" };

		private static readonly string[] EchoholdPassage1Names = new string[4] { "Module - Echohold - P - 1 - Cafeteria", "Module - Echohold - P - 1 - Control Room", "Module - Echohold - P - 1 - Engine Room", "Module - Echohold - P - 1 - Science Lab" };

		private static readonly string[] EchoholdDeadEnd1Names = new string[5] { "Module - Echohold - DE - 1 - Crew Quarters", "Module - Echohold - DE - 1 - Crew Quarters 2", "Module - Echohold - DE - 1 - Lab Testing", "Module - Echohold - DE - 1 - Medical Room", "Module - Echohold - DE - 1 - Storage Room" };

		private static readonly string[] EchoholdExtraction1Names = new string[1] { "Module - Echohold - E - 1 - Lab" };

		private readonly Harmony _echoholdHarmony = new Harmony("empress.repo.echoholdcompatpatch");

		private AssetBundle? _echoholdBundle;

		private bool _echoholdRepairApplied;

		private bool _echoholdRepairQueued;

		private bool _echoholdLoggedMissingBundle;

		private bool _echoholdLoggedMissingPrefabs;

		private bool _echoholdLoggedDoorMapFallback;

		internal static EchoholdCompatPatchPlugin? Instance { get; private set; }

		private void Awake()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			GameObject gameObject = ((Component)this).gameObject;
			((Object)gameObject).hideFlags = (HideFlags)(((Object)gameObject).hideFlags | 0x23);
			BundleLoader.OnAllBundlesLoaded += EchoholdHandleBundlesLoaded;
			_echoholdHarmony.PatchAll();
			EchoholdPatchUnityWarningFilter();
			if ((Object)(object)RunManager.instance != (Object)null)
			{
				((MonoBehaviour)this).StartCoroutine(EchoholdRepairWhenReady());
			}
		}

		private void OnDestroy()
		{
			BundleLoader.OnAllBundlesLoaded -= EchoholdHandleBundlesLoaded;
			_echoholdHarmony.UnpatchSelf();
			if (Instance == this)
			{
				Instance = null;
			}
		}

		private void EchoholdHandleBundlesLoaded()
		{
			if (!_echoholdRepairQueued && !_echoholdRepairApplied)
			{
				_echoholdRepairQueued = true;
				((MonoBehaviour)this).StartCoroutine(EchoholdRepairWhenReady());
			}
		}

		private void EchoholdPatchUnityWarningFilter()
		{
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Expected O, but got Unknown
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Expected O, but got Unknown
			Type type = AccessTools.TypeByName("UnityEngine.DebugLogHandler");
			if (!(type == null))
			{
				MethodInfo methodInfo = AccessTools.Method(type, "LogFormat", new Type[4]
				{
					typeof(LogType),
					typeof(Object),
					typeof(string),
					typeof(object[])
				}, (Type[])null);
				if (methodInfo != null)
				{
					_echoholdHarmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(EchoholdCompatPatchPlugin), "EchoholdSuppressMissingScriptWarningPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				MethodInfo methodInfo2 = AccessTools.Method(type, "LogFormat", new Type[5]
				{
					typeof(LogType),
					typeof(LogOption),
					typeof(Object),
					typeof(string),
					typeof(object[])
				}, (Type[])null);
				if (methodInfo2 != null)
				{
					_echoholdHarmony.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(EchoholdCompatPatchPlugin), "EchoholdSuppressMissingScriptWarningPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
		}

		private IEnumerator EchoholdRepairWhenReady()
		{
			yield return null;
			for (int attempt = 0; attempt < 120; attempt++)
			{
				if (_echoholdRepairApplied)
				{
					break;
				}
				if (EchoholdTryRepairLevel())
				{
					break;
				}
				yield return null;
			}
			_echoholdRepairQueued = false;
		}

		internal bool EchoholdTryRepairCurrentLevel()
		{
			List<Level> list = new List<Level>();
			if ((Object)(object)LevelGenerator.Instance != (Object)null && (Object)(object)LevelGenerator.Instance.Level != (Object)null)
			{
				list.Add(LevelGenerator.Instance.Level);
			}
			if ((Object)(object)RunManager.instance != (Object)null && (Object)(object)RunManager.instance.levelCurrent != (Object)null)
			{
				list.Add(RunManager.instance.levelCurrent);
			}
			return EchoholdTryRepairLevel(list);
		}

		private bool EchoholdTryRepairLevel(IEnumerable<Level>? candidates = null)
		{
			if (_echoholdRepairApplied)
			{
				return true;
			}
			Level val = EchoholdFindLevel(candidates) ?? EchoholdFindLevel(Levels.AllLevels);
			if ((Object)(object)val == (Object)null)
			{
				return false;
			}
			AssetBundle val2 = EchoholdGetBundle();
			if ((Object)(object)val2 == (Object)null)
			{
				if (!_echoholdLoggedMissingBundle)
				{
					_echoholdLoggedMissingBundle = true;
					((BaseUnityPlugin)this).Logger.LogWarning((object)"Empress Echohold Compat Patch could not locate the Echohold bundle.");
				}
				return false;
			}
			_echoholdLoggedMissingBundle = false;
			EchoholdPrefabBuckets echoholdPrefabBuckets = EchoholdCollectPrefabs(val2);
			if (echoholdPrefabBuckets.StartRooms.Count == 0 || echoholdPrefabBuckets.TotalModuleCount == 0)
			{
				if (!_echoholdLoggedMissingPrefabs)
				{
					_echoholdLoggedMissingPrefabs = true;
					((BaseUnityPlugin)this).Logger.LogError((object)"Empress Echohold Compat Patch could not rebuild the Echohold prefab lists from the original bundle.");
				}
				return false;
			}
			_echoholdLoggedMissingPrefabs = false;
			EchoholdApplyHideFlags((Object)(object)val2);
			EchoholdApplyHideFlags((Object)(object)val);
			EchoholdEnsureLevelFallbackData(val);
			EchoholdRegisterLevelObjects(val);
			val.StartRooms = EchoholdRegisterPrefabs(val, "StartRoom", echoholdPrefabBuckets.StartRooms);
			val.ModulesNormal1 = EchoholdRegisterPrefabs(val, "Normal", echoholdPrefabBuckets.Normal1);
			val.ModulesPassage1 = EchoholdRegisterPrefabs(val, "Passage", echoholdPrefabBuckets.Passage1);
			val.ModulesDeadEnd1 = EchoholdRegisterPrefabs(val, "DeadEnd", echoholdPrefabBuckets.DeadEnd1);
			val.ModulesExtraction1 = EchoholdRegisterPrefabs(val, "Extraction", echoholdPrefabBuckets.Extraction1);
			val.ModulesNormal2 = EchoholdRegisterPrefabs(val, "Normal", echoholdPrefabBuckets.Normal2);
			val.ModulesPassage2 = EchoholdRegisterPrefabs(val, "Passage", echoholdPrefabBuckets.Passage2);
			val.ModulesDeadEnd2 = EchoholdRegisterPrefabs(val, "DeadEnd", echoholdPrefabBuckets.DeadEnd2);
			val.ModulesExtraction2 = EchoholdRegisterPrefabs(val, "Extraction", echoholdPrefabBuckets.Extraction2);
			val.ModulesNormal3 = EchoholdRegisterPrefabs(val, "Normal", echoholdPrefabBuckets.Normal3);
			val.ModulesPassage3 = EchoholdRegisterPrefabs(val, "Passage", echoholdPrefabBuckets.Passage3);
			val.ModulesDeadEnd3 = EchoholdRegisterPrefabs(val, "DeadEnd", echoholdPrefabBuckets.DeadEnd3);
			val.ModulesExtraction3 = EchoholdRegisterPrefabs(val, "Extraction", echoholdPrefabBuckets.Extraction3);
			_echoholdRepairApplied = val.StartRooms.Count > 0 && EchoholdCountModules(val) > 0;
			if (_echoholdRepairApplied)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Empress Echohold Compat Patch repaired {((Object)val).name} with {val.StartRooms.Count} start rooms and {EchoholdCountModules(val)} modules.");
			}
			return _echoholdRepairApplied;
		}

		private static Level? EchoholdFindLevel(IEnumerable<Level>? candidates)
		{
			if (candidates == null)
			{
				return null;
			}
			foreach (Level candidate in candidates)
			{
				if (EchoholdIsEchoholdLevel(candidate))
				{
					return candidate;
				}
			}
			return null;
		}

		private AssetBundle? EchoholdGetBundle()
		{
			if ((Object)(object)_echoholdBundle != (Object)null)
			{
				return _echoholdBundle;
			}
			foreach (AssetBundle allLoadedAssetBundle in AssetBundle.GetAllLoadedAssetBundles())
			{
				if (EchoholdBundleMatches(allLoadedAssetBundle))
				{
					_echoholdBundle = allLoadedAssetBundle;
					return _echoholdBundle;
				}
			}
			string text = Directory.GetFiles(Paths.PluginPath, "Echohold.repobundle", SearchOption.AllDirectories).FirstOrDefault();
			if (string.IsNullOrWhiteSpace(text))
			{
				return null;
			}
			_echoholdBundle = AssetBundle.LoadFromFile(text);
			if ((Object)(object)_echoholdBundle != (Object)null)
			{
				return _echoholdBundle;
			}
			foreach (AssetBundle allLoadedAssetBundle2 in AssetBundle.GetAllLoadedAssetBundles())
			{
				if (EchoholdBundleMatches(allLoadedAssetBundle2))
				{
					_echoholdBundle = allLoadedAssetBundle2;
					return _echoholdBundle;
				}
			}
			return _echoholdBundle;
		}

		private static bool EchoholdBundleMatches(AssetBundle? bundle)
		{
			if ((Object)(object)bundle == (Object)null)
			{
				return false;
			}
			try
			{
				if (!string.IsNullOrWhiteSpace(((Object)bundle).name) && ((Object)bundle).name.IndexOf("echohold", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return true;
				}
			}
			catch
			{
			}
			try
			{
				if (bundle.GetAllAssetNames().Any((string assetName) => assetName.IndexOf("echohold", StringComparison.OrdinalIgnoreCase) >= 0 || assetName.EndsWith("level - echohold.asset", StringComparison.OrdinalIgnoreCase)))
				{
					return true;
				}
			}
			catch
			{
			}
			try
			{
				Level[] array = bundle.LoadAllAssets<Level>();
				for (int i = 0; i < array.Length; i++)
				{
					if (EchoholdIsEchoholdLevel(array[i]))
					{
						return true;
					}
				}
			}
			catch
			{
			}
			try
			{
				GameObject[] array2 = bundle.LoadAllAssets<GameObject>();
				foreach (GameObject val in array2)
				{
					if (!((Object)(object)val == (Object)null) && (((Object)val).name.StartsWith("Start Room - Echohold -", StringComparison.OrdinalIgnoreCase) || ((Object)val).name.StartsWith("Module - Echohold -", StringComparison.OrdinalIgnoreCase)))
					{
						return true;
					}
				}
			}
			catch
			{
			}
			return false;
		}

		private static EchoholdPrefabBuckets EchoholdCollectPrefabs(AssetBundle bundle)
		{
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			Dictionary<string, GameObject> dictionary = new Dictionary<string, GameObject>(StringComparer.OrdinalIgnoreCase);
			StartRoom[] array = bundle.LoadAllAssets<StartRoom>();
			foreach (StartRoom val in array)
			{
				EchoholdAddAssetByName(dictionary, ((Object)(object)val != (Object)null) ? ((Component)val).gameObject : null);
			}
			Module[] array2 = bundle.LoadAllAssets<Module>();
			foreach (Module val2 in array2)
			{
				EchoholdAddAssetByName(dictionary, ((Object)(object)val2 != (Object)null) ? ((Component)val2).gameObject : null);
			}
			GameObject[] array3 = bundle.LoadAllAssets<GameObject>();
			foreach (GameObject prefab in array3)
			{
				EchoholdAddAssetByName(dictionary, prefab);
			}
			if (!dictionary.Keys.Any((string name) => name.StartsWith("Start Room - Echohold -", StringComparison.OrdinalIgnoreCase)) || !dictionary.Keys.Any((string name) => name.StartsWith("Module - Echohold -", StringComparison.OrdinalIgnoreCase)))
			{
				array = Resources.FindObjectsOfTypeAll<StartRoom>();
				Scene scene;
				foreach (StartRoom val3 in array)
				{
					if (!((Object)(object)val3 == (Object)null))
					{
						scene = ((Component)val3).gameObject.scene;
						if (!((Scene)(ref scene)).IsValid())
						{
							EchoholdAddAssetByName(dictionary, ((Component)val3).gameObject);
						}
					}
				}
				array2 = Resources.FindObjectsOfTypeAll<Module>();
				foreach (Module val4 in array2)
				{
					if (!((Object)(object)val4 == (Object)null))
					{
						scene = ((Component)val4).gameObject.scene;
						if (!((Scene)(ref scene)).IsValid())
						{
							EchoholdAddAssetByName(dictionary, ((Component)val4).gameObject);
						}
					}
				}
				array3 = Resources.FindObjectsOfTypeAll<GameObject>();
				foreach (GameObject val5 in array3)
				{
					if (!((Object)(object)val5 == (Object)null))
					{
						scene = val5.scene;
						if (!((Scene)(ref scene)).IsValid())
						{
							EchoholdAddAssetByName(dictionary, val5);
						}
					}
				}
			}
			return new EchoholdPrefabBuckets
			{
				StartRooms = EchoholdResolvePrefabs(dictionary, EchoholdStartRoomNames),
				Normal1 = EchoholdResolvePrefabs(dictionary, EchoholdNormal1Names),
				Passage1 = EchoholdResolvePrefabs(dictionary, EchoholdPassage1Names),
				DeadEnd1 = EchoholdResolvePrefabs(dictionary, EchoholdDeadEnd1Names),
				Extraction1 = EchoholdResolvePrefabs(dictionary, EchoholdExtraction1Names),
				Normal2 = new List<GameObject>(),
				Passage2 = new List<GameObject>(),
				DeadEnd2 = new List<GameObject>(),
				Extraction2 = new List<GameObject>(),
				Normal3 = new List<GameObject>(),
				Passage3 = new List<GameObject>(),
				DeadEnd3 = new List<GameObject>(),
				Extraction3 = new List<GameObject>()
			};
		}

		private static void EchoholdAddAssetByName(IDictionary<string, GameObject> prefabsByName, GameObject? prefab)
		{
			if (!((Object)(object)prefab == (Object)null) && !string.IsNullOrWhiteSpace(((Object)prefab).name) && !prefabsByName.ContainsKey(((Object)prefab).name))
			{
				prefabsByName[((Object)prefab).name] = prefab;
			}
		}

		private static List<GameObject> EchoholdResolvePrefabs(IReadOnlyDictionary<string, GameObject> prefabsByName, IEnumerable<string> names)
		{
			List<GameObject> list = new List<GameObject>();
			foreach (string name in names)
			{
				if (prefabsByName.TryGetValue(name, out GameObject value))
				{
					list.Add(value);
				}
			}
			return list;
		}

		private static void EchoholdRegisterLevelObjects(Level level)
		{
			if ((Object)(object)level.ConnectObject != (Object)null)
			{
				EchoholdRegisterNetworkPrefab("Level/" + (level.ResourcePath ?? "Echohold") + "/Other/" + ((Object)level.ConnectObject).name, level.ConnectObject);
			}
			if ((Object)(object)level.BlockObject != (Object)null)
			{
				EchoholdRegisterNetworkPrefab("Level/" + (level.ResourcePath ?? "Echohold") + "/Other/" + ((Object)level.BlockObject).name, level.BlockObject);
			}
		}

		private static List<PrefabRef> EchoholdRegisterPrefabs(Level level, string moduleTypeName, IEnumerable<GameObject> prefabs)
		{
			List<PrefabRef> list = new List<PrefabRef>();
			foreach (GameObject item in prefabs.OrderBy<GameObject, string>((GameObject gameObject) => ((Object)gameObject).name, StringComparer.OrdinalIgnoreCase))
			{
				PrefabRef val = EchoholdRegisterNetworkPrefab("Level/" + ((Object)level).name + "/" + moduleTypeName + "/" + ((Object)item).name, item);
				if (val != null)
				{
					list.Add(val);
				}
			}
			return list;
		}

		private static PrefabRef? EchoholdRegisterNetworkPrefab(string prefabId, GameObject prefab)
		{
			PrefabRef val = EchoholdGetRegisteredPrefabRef(prefabId, prefab);
			if (val != null)
			{
				return val;
			}
			EchoholdPreparePrefab(prefab);
			Utilities.FixAudioMixerGroups(prefab);
			return NetworkPrefabs.RegisterNetworkPrefab(prefabId, prefab);
		}

		private static PrefabRef? EchoholdGetRegisteredPrefabRef(string prefabId, GameObject prefab)
		{
			if (!NetworkPrefabs.PrefabRefs.TryGetValue(prefabId, out var value) || value == null)
			{
				return null;
			}
			if ((Object)(object)value.Prefab == (Object)null || (Object)(object)value.Prefab == (Object)(object)prefab)
			{
				return value;
			}
			return null;
		}

		private static void EchoholdPreparePrefab(GameObject prefab)
		{
			EchoholdApplyHideFlags((Object)(object)prefab);
			EchoholdAssignFallbackAudioMixerGroups(prefab);
			EchoholdAssignFallbackPhysAttributes(prefab);
		}

		private static void EchoholdAssignFallbackAudioMixerGroups(GameObject prefab)
		{
			AudioMixerGroup val = EchoholdGetFallbackAudioMixerGroup();
			if ((Object)(object)prefab == (Object)null || (Object)(object)val == (Object)null)
			{
				return;
			}
			AudioSource[] componentsInChildren = prefab.GetComponentsInChildren<AudioSource>(true);
			foreach (AudioSource val2 in componentsInChildren)
			{
				if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val2.outputAudioMixerGroup != (Object)null))
				{
					val2.outputAudioMixerGroup = val;
				}
			}
		}

		private static AudioMixerGroup? EchoholdGetFallbackAudioMixerGroup()
		{
			if ((Object)(object)AudioManager.instance == (Object)null)
			{
				return null;
			}
			return AudioManager.instance.SoundMasterGroup ?? AudioManager.instance.PersistentSoundGroup ?? AudioManager.instance.MusicMasterGroup ?? AudioManager.instance.MicrophoneSoundGroup ?? AudioManager.instance.MicrophoneSpectateGroup ?? AudioManager.instance.TTSSoundGroup ?? AudioManager.instance.TTSSpectateGroup;
		}

		private static void EchoholdAssignFallbackPhysAttributes(GameObject prefab)
		{
			if ((Object)(object)prefab == (Object)null)
			{
				return;
			}
			NotValuableObject[] componentsInChildren = prefab.GetComponentsInChildren<NotValuableObject>(true);
			foreach (NotValuableObject val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)null) && !((Object)(object)val.physAttributePreset != (Object)null))
				{
					Rigidbody component = ((Component)val).GetComponent<Rigidbody>();
					float mass = (((Object)(object)component != (Object)null && component.mass > 0f) ? component.mass : 1f);
					val.physAttributePreset = EchoholdCreateFallbackPhysAttribute(mass);
				}
			}
		}

		private static PhysAttribute EchoholdCreateFallbackPhysAttribute(float mass)
		{
			PhysAttribute obj = ScriptableObject.CreateInstance<PhysAttribute>();
			obj.mass = Mathf.Max(0.01f, mass);
			EchoholdApplyHideFlags((Object)(object)obj);
			return obj;
		}

		private static int EchoholdCountModules(Level level)
		{
			return (level.ModulesNormal1?.Count ?? 0) + (level.ModulesPassage1?.Count ?? 0) + (level.ModulesDeadEnd1?.Count ?? 0) + (level.ModulesExtraction1?.Count ?? 0) + (level.ModulesNormal2?.Count ?? 0) + (level.ModulesPassage2?.Count ?? 0) + (level.ModulesDeadEnd2?.Count ?? 0) + (level.ModulesExtraction2?.Count ?? 0) + (level.ModulesNormal3?.Count ?? 0) + (level.ModulesPassage3?.Count ?? 0) + (level.ModulesDeadEnd3?.Count ?? 0) + (level.ModulesExtraction3?.Count ?? 0);
		}

		internal static bool EchoholdIsCurrentLevel()
		{
			if (!EchoholdIsEchoholdLevel(((Object)(object)LevelGenerator.Instance != (Object)null) ? LevelGenerator.Instance.Level : null))
			{
				return EchoholdIsEchoholdLevel(((Object)(object)RunManager.instance != (Object)null) ? RunManager.instance.levelCurrent : null);
			}
			return true;
		}

		internal static bool EchoholdShouldSuppressMissingScriptWarning(LogType logType, string? format, object[]? args)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			if ((int)logType != 2 || !EchoholdIsCurrentLevel())
			{
				return false;
			}
			if (EchoholdIsMissingScriptWarning(format))
			{
				return true;
			}
			if (args == null || args.Length == 0 || string.IsNullOrWhiteSpace(format))
			{
				return false;
			}
			try
			{
				return EchoholdIsMissingScriptWarning(string.Format(format, args));
			}
			catch
			{
				return false;
			}
		}

		internal static bool EchoholdShouldSuppressMissingScriptWarning(LogEventArgs? eventArgs)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Invalid comparison between Unknown and I4
			if (eventArgs == null || (int)eventArgs.Level != 4 || !EchoholdIsCurrentLevel())
			{
				return false;
			}
			if (!EchoholdIsUnityLogSource(eventArgs.Source))
			{
				return false;
			}
			if (eventArgs.Data is string message)
			{
				return EchoholdIsMissingScriptWarning(message);
			}
			return EchoholdIsMissingScriptWarning(eventArgs.Data?.ToString());
		}

		private static bool EchoholdIsUnityLogSource(ILogSource? source)
		{
			if (source != null)
			{
				return string.Equals(source.SourceName, "Unity Log", StringComparison.OrdinalIgnoreCase);
			}
			return false;
		}

		private static bool EchoholdIsMissingScriptWarning(string? message)
		{
			if (string.IsNullOrWhiteSpace(message))
			{
				return false;
			}
			if (message.IndexOf("The referenced script", StringComparison.Ordinal) >= 0)
			{
				return message.IndexOf("missing", StringComparison.OrdinalIgnoreCase) >= 0;
			}
			return false;
		}

		private static bool EchoholdSuppressMissingScriptWarningPrefix(LogType logType, string format, object[] args)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			return !EchoholdShouldSuppressMissingScriptWarning(logType, format, args);
		}

		private static bool EchoholdIsEchoholdLevel(Level? level)
		{
			if ((Object)(object)level == (Object)null)
			{
				return false;
			}
			if (!string.Equals(((Object)level).name, "Level - Echohold", StringComparison.OrdinalIgnoreCase))
			{
				return string.Equals(level.ResourcePath, "Echohold", StringComparison.OrdinalIgnoreCase);
			}
			return true;
		}

		internal static void EchoholdEnsureLevelFallbackData(Level? level)
		{
			if (EchoholdIsEchoholdLevel(level))
			{
				Level val = level;
				if (val.ValuablePresets == null)
				{
					val.ValuablePresets = new List<LevelValuables>();
				}
				val = level;
				if (val.AmbiencePresets == null)
				{
					val.AmbiencePresets = new List<LevelAmbience>();
				}
				if (level.ValuablePresets.Count == 0 && (Object)(object)ValuablePresets.GenericValuablePreset != (Object)null)
				{
					level.ValuablePresets.Add(ValuablePresets.GenericValuablePreset);
				}
				if (level.AmbiencePresets.Count == 0 && EchoholdTryGetFallbackAmbience(out LevelAmbience ambience))
				{
					level.AmbiencePresets.Add(ambience);
				}
			}
		}

		private static bool EchoholdTryGetFallbackAmbience(out LevelAmbience ambience)
		{
			ambience = null;
			if ((Object)(object)AudioManager.instance != (Object)null && AudioManager.instance.levelAmbiences != null)
			{
				foreach (LevelAmbience levelAmbience in AudioManager.instance.levelAmbiences)
				{
					if (!((Object)(object)levelAmbience == (Object)null))
					{
						ambience = levelAmbience;
						return true;
					}
				}
			}
			if ((Object)(object)RunManager.instance != (Object)null && RunManager.instance.levels != null)
			{
				foreach (Level level in RunManager.instance.levels)
				{
					if (level?.AmbiencePresets == null)
					{
						continue;
					}
					foreach (LevelAmbience ambiencePreset in level.AmbiencePresets)
					{
						if (!((Object)(object)ambiencePreset == (Object)null))
						{
							ambience = ambiencePreset;
							return true;
						}
					}
				}
			}
			return false;
		}

		internal static void EchoholdEnsureExtractionPointCompatibility(ExtractionPoint extractionPoint)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: 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)
			//IL_00da: Expected O, but got Unknown
			if ((Object)(object)extractionPoint == (Object)null || !EchoholdIsCurrentLevel())
			{
				return;
			}
			if ((Object)(object)extractionPoint.extractionArea == (Object)null)
			{
				GameObject val = GameObject.CreatePrimitive((PrimitiveType)2);
				((Object)val).name = "Echohold Patch Extraction Area";
				val.transform.SetParent(((Component)extractionPoint).transform, false);
				val.transform.localPosition = Vector3.zero;
				val.transform.localRotation = Quaternion.identity;
				val.transform.localScale = Vector3.one * 0.1f;
				MeshRenderer component = val.GetComponent<MeshRenderer>();
				if ((Object)(object)component != (Object)null)
				{
					((Renderer)component).enabled = false;
				}
				Collider component2 = val.GetComponent<Collider>();
				if ((Object)(object)component2 != (Object)null)
				{
					Object.Destroy((Object)(object)component2);
				}
				val.SetActive(false);
				EchoholdApplyHideFlags((Object)(object)val);
				extractionPoint.extractionArea = val;
			}
			if ((Object)(object)extractionPoint.grossUp == (Object)null)
			{
				GameObject val2 = new GameObject("Echohold Patch GrossUp");
				val2.transform.SetParent(((Component)extractionPoint).transform, false);
				val2.SetActive(false);
				EchoholdApplyHideFlags((Object)(object)val2);
				extractionPoint.grossUp = val2;
			}
		}

		internal void EchoholdRepairLevelPoints(LevelGenerator levelGenerator)
		{
			if ((Object)(object)levelGenerator == (Object)null || !EchoholdIsEchoholdLevel(levelGenerator.Level) || levelGenerator.LevelPathPoints == null)
			{
				return;
			}
			int num = 0;
			int num2 = 0;
			foreach (LevelPoint levelPathPoint in levelGenerator.LevelPathPoints)
			{
				if (EchoholdRepairLevelPoint(levelPathPoint))
				{
					num++;
				}
				if (EchoholdRepairLevelPointRoom(levelPathPoint))
				{
					num2++;
				}
			}
			if (num > 0 || num2 > 0)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Empress Echohold Compat Patch repaired {num} Echohold level points onto the navmesh and restored {num2} room links.");
			}
		}

		private static bool EchoholdRepairLevelPoint(LevelPoint? levelPoint)
		{
			//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_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)levelPoint == (Object)null)
			{
				return false;
			}
			Vector3 position = ((Component)levelPoint).transform.position;
			NavMeshHit val = default(NavMeshHit);
			if (NavMesh.SamplePosition(position, ref val, 0.5f, -1))
			{
				return false;
			}
			NavMeshHit val2 = default(NavMeshHit);
			if (!NavMesh.SamplePosition(position, ref val2, 2f, -1))
			{
				return false;
			}
			((Component)levelPoint).transform.position = ((NavMeshHit)(ref val2)).position;
			EchoholdRepairLevelPointRoom(levelPoint);
			return true;
		}

		private static bool EchoholdRepairLevelPointRoom(LevelPoint? levelPoint)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)levelPoint == (Object)null || (Object)(object)levelPoint.Room != (Object)null)
			{
				return false;
			}
			RoomVolume room = default(RoomVolume);
			Vector3 val = default(Vector3);
			if (!SemiFunc.GetRoomVolumeAtPosition(((Component)levelPoint).transform.position, ref room, ref val))
			{
				return false;
			}
			levelPoint.Room = room;
			return true;
		}

		internal IEnumerator EchoholdSilentLevelPointCheck(LevelPoint levelPoint)
		{
			if (!((Object)(object)levelPoint == (Object)null))
			{
				while ((Object)(object)LevelGenerator.Instance == (Object)null || !LevelGenerator.Instance.Generated)
				{
					yield return (object)new WaitForSeconds(0.1f);
				}
				yield return (object)new WaitForSeconds(0.5f);
				if (EchoholdIsCurrentLevel())
				{
					EchoholdRepairLevelPoint(levelPoint);
					EchoholdRepairLevelPointRoom(levelPoint);
					EchoholdRepairLevelPointConnections(levelPoint, LevelGenerator.Instance?.LevelPathPoints);
				}
			}
		}

		private static void EchoholdRepairLevelPointConnections(LevelPoint? levelPoint, IList<LevelPoint>? allLevelPoints)
		{
			if ((Object)(object)levelPoint == (Object)null)
			{
				return;
			}
			if (levelPoint.ConnectedPoints == null)
			{
				levelPoint.ConnectedPoints = new List<LevelPoint>();
			}
			levelPoint.ConnectedPoints.RemoveAll((LevelPoint connectedPoint) => (Object)(object)connectedPoint == (Object)null);
			LevelPoint[] array = levelPoint.ConnectedPoints.ToArray();
			for (int i = 0; i < array.Length; i++)
			{
				EchoholdAddLevelPointConnection(array[i], levelPoint);
			}
			if (!levelPoint.ModuleConnect || allLevelPoints == null || levelPoint.ConnectedPoints.Count > 0)
			{
				return;
			}
			LevelPoint val = null;
			float currentBestDistance = float.MaxValue;
			foreach (LevelPoint allLevelPoint in allLevelPoints)
			{
				if (EchoholdCanConnectLevelPoints(levelPoint, allLevelPoint, currentBestDistance, out var distance))
				{
					val = allLevelPoint;
					currentBestDistance = distance;
				}
			}
			EchoholdAddLevelPointConnection(levelPoint, val);
			EchoholdAddLevelPointConnection(val, levelPoint);
		}

		private static bool EchoholdCanConnectLevelPoints(LevelPoint source, LevelPoint? candidate, float currentBestDistance, out float distance)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: 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)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			distance = 0f;
			if ((Object)(object)source == (Object)null || (Object)(object)candidate == (Object)null || (Object)(object)source == (Object)(object)candidate || !candidate.ModuleConnect)
			{
				return false;
			}
			Vector3 val = ((Component)source).transform.position - ((Component)candidate).transform.position;
			if (((Vector3)(ref val)).sqrMagnitude <= Mathf.Epsilon)
			{
				return false;
			}
			distance = ((Vector3)(ref val)).magnitude;
			if (distance >= 15f || distance >= currentBestDistance)
			{
				return false;
			}
			if (Vector3.Dot(((Component)candidate).transform.forward, ((Component)source).transform.forward) > -0.8f)
			{
				return false;
			}
			return Vector3.Dot(((Component)candidate).transform.forward, ((Vector3)(ref val)).normalized) > 0.8f;
		}

		private static void EchoholdAddLevelPointConnection(LevelPoint? source, LevelPoint? target)
		{
			if (!((Object)(object)source == (Object)null) && !((Object)(object)target == (Object)null))
			{
				if (source.ConnectedPoints == null)
				{
					source.ConnectedPoints = new List<LevelPoint>();
				}
				if (!source.ConnectedPoints.Contains(target))
				{
					source.ConnectedPoints.Add(target);
				}
			}
		}

		internal GameObject EchoholdCreateFallbackDoorMapObject(Map map, DirtFinderMapDoor door)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			MapLayer layerParent = map.GetLayerParent(((Component)door).transform.position.y);
			GameObject val = EchoholdInstantiateFallbackDoorMapObject(map, ((Component)layerParent).transform);
			((Object)val).name = ((Object)((Component)door).gameObject).name;
			door.Target = val.transform;
			DirtFinderMapDoorTarget val2 = val.GetComponent<DirtFinderMapDoorTarget>() ?? val.AddComponent<DirtFinderMapDoorTarget>();
			val2.Target = ((Component)door).transform;
			val2.Layer = layerParent;
			DirtFinderMapDoorTarget val3 = val2;
			if (val3.HingeTransform == null)
			{
				val3.HingeTransform = val.transform;
			}
			map.DoorUpdate(val2.HingeTransform, ((Component)door).transform, layerParent);
			if (!_echoholdLoggedDoorMapFallback)
			{
				_echoholdLoggedDoorMapFallback = true;
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Empress Echohold Compat Patch injected fallback minimap door markers for missing Echohold door prefabs.");
			}
			return val;
		}

		private static GameObject EchoholdInstantiateFallbackDoorMapObject(Map map, Transform parent)
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			GameObject val;
			if ((Object)(object)map.Door1x1Object != (Object)null)
			{
				val = Object.Instantiate<GameObject>(map.Door1x1Object, parent);
				Collider[] componentsInChildren = val.GetComponentsInChildren<Collider>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					Object.Destroy((Object)(object)componentsInChildren[i]);
				}
			}
			else
			{
				val = GameObject.CreatePrimitive((PrimitiveType)3);
				val.transform.SetParent(parent, false);
				val.transform.localScale = new Vector3(0.12f, 0.12f, 0.3f);
				Collider component = val.GetComponent<Collider>();
				if ((Object)(object)component != (Object)null)
				{
					Object.Destroy((Object)(object)component);
				}
			}
			val.transform.localPosition = Vector3.zero;
			val.transform.localRotation = Quaternion.identity;
			DirtFinderMapDoorTarget val2 = val.GetComponent<DirtFinderMapDoorTarget>() ?? val.AddComponent<DirtFinderMapDoorTarget>();
			if (val2.HingeTransform == null)
			{
				val2.HingeTransform = val.transform;
			}
			EchoholdApplyHideFlags((Object)(object)val);
			return val;
		}

		private static void EchoholdApplyHideFlags(Object obj)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: 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_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			if (obj == (Object)null)
			{
				return;
			}
			obj.hideFlags = (HideFlags)(obj.hideFlags | 0x23);
			GameObject val = (GameObject)(object)((obj is GameObject) ? obj : null);
			if (val == null)
			{
				return;
			}
			Transform[] componentsInChildren = val.GetComponentsInChildren<Transform>(true);
			foreach (Transform obj2 in componentsInChildren)
			{
				((Object)obj2).hideFlags = (HideFlags)(((Object)obj2).hideFlags | 0x23);
				GameObject gameObject = ((Component)obj2).gameObject;
				((Object)gameObject).hideFlags = (HideFlags)(((Object)gameObject).hideFlags | 0x23);
				Component[] components = ((Component)obj2).GetComponents<Component>();
				foreach (Component val2 in components)
				{
					if ((Object)(object)val2 != (Object)null)
					{
						((Object)val2).hideFlags = (HideFlags)(((Object)val2).hideFlags | 0x23);
					}
				}
			}
		}
	}
	[HarmonyPatch(typeof(LevelGenerator), "StartRoomGeneration")]
	internal static class EchoholdCompatPatchStartRoomGenerationPatch
	{
		[HarmonyPrefix]
		private static void EchoholdPrefix()
		{
			EchoholdCompatPatchPlugin.Instance?.EchoholdTryRepairCurrentLevel();
		}
	}
	[HarmonyPatch(typeof(ExtractionPoint), "Start")]
	internal static class EchoholdCompatPatchExtractionPointStartPatch
	{
		[HarmonyPostfix]
		private static void EchoholdPostfix(ExtractionPoint __instance)
		{
			EchoholdCompatPatchPlugin.EchoholdEnsureExtractionPointCompatibility(__instance);
		}
	}
	[HarmonyPatch(typeof(Levels), "RegisterLevelWithGame")]
	internal static class EchoholdCompatPatchRegisterLevelPatch
	{
		[HarmonyPrefix]
		private static void EchoholdPrefix(Level level)
		{
			EchoholdCompatPatchPlugin.EchoholdEnsureLevelFallbackData(level);
		}
	}
	[HarmonyPatch(typeof(Map), "AddDoor")]
	internal static class EchoholdCompatPatchMapAddDoorPatch
	{
		[HarmonyPrefix]
		private static bool EchoholdPrefix(Map __instance, DirtFinderMapDoor door, GameObject doorPrefab, ref GameObject __result)
		{
			if (!EchoholdCompatPatchPlugin.EchoholdIsCurrentLevel() || (Object)(object)door == (Object)null || (Object)(object)doorPrefab != (Object)null)
			{
				return true;
			}
			EchoholdCompatPatchPlugin instance = EchoholdCompatPatchPlugin.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return true;
			}
			__result = instance.EchoholdCreateFallbackDoorMapObject(__instance, door);
			return false;
		}
	}
	[HarmonyPatch(typeof(LevelGenerator), "NavMeshSetupRPC")]
	internal static class EchoholdCompatPatchNavMeshSetupPatch
	{
		[HarmonyPostfix]
		private static void EchoholdPostfix(LevelGenerator __instance)
		{
			EchoholdCompatPatchPlugin.Instance?.EchoholdRepairLevelPoints(__instance);
		}
	}
	[HarmonyPatch(typeof(LevelPoint), "NavMeshCheck")]
	internal static class EchoholdCompatPatchLevelPointNavMeshCheckPatch
	{
		[HarmonyPrefix]
		private static bool EchoholdPrefix(LevelPoint __instance, ref IEnumerator __result)
		{
			if (!EchoholdCompatPatchPlugin.EchoholdIsCurrentLevel())
			{
				return true;
			}
			EchoholdCompatPatchPlugin instance = EchoholdCompatPatchPlugin.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return true;
			}
			__result = instance.EchoholdSilentLevelPointCheck(__instance);
			return false;
		}
	}
	[HarmonyPatch(typeof(UnityLogListener), "LogEvent")]
	internal static class EchoholdCompatPatchUnityLogListenerPatch
	{
		[HarmonyPrefix]
		private static bool EchoholdPrefix(LogEventArgs eventArgs)
		{
			return !EchoholdCompatPatchPlugin.EchoholdShouldSuppressMissingScriptWarning(eventArgs);
		}
	}
	[HarmonyPatch(typeof(UnityLogSource), "UnityLogMessageHandler")]
	internal static class EchoholdCompatPatchUnityLogSourcePatch
	{
		[HarmonyPrefix]
		private static bool EchoholdPrefix(LogEventArgs eventArgs)
		{
			return !EchoholdCompatPatchPlugin.EchoholdShouldSuppressMissingScriptWarning(eventArgs);
		}
	}
	[HarmonyPatch(typeof(Logger), "InternalLogEvent")]
	internal static class EchoholdCompatPatchInternalLogEventPatch
	{
		[HarmonyPrefix]
		private static bool EchoholdPrefix(LogEventArgs eventArgs)
		{
			return !EchoholdCompatPatchPlugin.EchoholdShouldSuppressMissingScriptWarning(eventArgs);
		}
	}
}