Decompiled source of MysteriousEggRoom v1.0.0

plugins/MeticulouslyRare-EggRoom/meticulouslyrare.eggroom.dll

Decompiled 13 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 System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PluginConfig.API;
using PluginConfig.API.Fields;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.Events;
using UnityEngine.ResourceManagement.AsyncOperations;
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: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("meticulouslyrare.eggroom")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("meticulouslyrare.eggroom")]
[assembly: AssemblyTitle("meticulouslyrare.eggroom")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
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;
		}
	}
}
public class _EggRoomDoorAdd : MonoBehaviour
{
	public Door parentDoor;

	public _EggRoomMain eggMain;

	public void Awake()
	{
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Expected O, but got Unknown
		if ((Object)(object)parentDoor == (Object)null)
		{
			parentDoor = ((Component)this).gameObject.GetComponentInParent<Door>();
		}
		if ((Object)(object)eggMain == (Object)null)
		{
			eggMain = _EggRoomMain.Instance;
		}
		if ((Object)(object)parentDoor != (Object)null && (Object)(object)eggMain != (Object)null)
		{
			parentDoor.onFullyOpened.AddListener(new UnityAction(TeleportToEggRoom));
		}
	}

	public void TeleportToEggRoom()
	{
		//IL_001d: 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_0048: Expected O, but got Unknown
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		if (!eggMain.goneIntoEggRoom)
		{
			Debug.Log((object)$"TeleportToEggRoom called. Player at {((Component)MonoSingleton<NewMovement>.Instance).transform.position}");
			eggMain.goneIntoEggRoom = true;
			GameObject val = new GameObject("exitPos");
			Vector3 position = ((Component)MonoSingleton<NewMovement>.Instance).transform.position;
			val.transform.position = position;
			eggMain.roomDoorAdd = this;
			eggMain.exitPos = val;
			eggMain.lastCalmMusic = MonoSingleton<MusicManager>.Instance.cleanTheme.clip;
			eggMain.lastCombatMusic = MonoSingleton<MusicManager>.Instance.battleTheme.clip;
			eggMain.lastBossMusic = MonoSingleton<MusicManager>.Instance.bossTheme.clip;
			eggMain.GenericTeleport(eggMain.enterPos);
			eggMain.OnEnterEggRoom();
		}
	}
}
public class _EggRoomMain : MonoBehaviour
{
	public ObjectActivator exitTrigger;

	public GameObject enterPos;

	[HideInInspector]
	public GameObject exitPos;

	public bool goneIntoEggRoom;

	[HideInInspector]
	public _EggRoomDoorAdd roomDoorAdd;

	public AudioClip lastCalmMusic;

	public AudioClip lastCombatMusic;

	public AudioClip lastBossMusic;

	public AudioClip eggRoomMusic;

	private bool previousFog;

	public static _EggRoomMain Instance { get; private set; }

	private void Awake()
	{
		if ((Object)(object)Instance != (Object)null && (Object)(object)Instance != (Object)(object)this)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
		}
		else
		{
			Instance = this;
		}
	}

	public void OnEnterEggRoom()
	{
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Expected O, but got Unknown
		if ((Object)(object)eggRoomMusic != (Object)null)
		{
			MonoSingleton<MusicManager>.Instance.StopMusic();
			MonoSingleton<MusicManager>.Instance.cleanTheme.clip = eggRoomMusic;
			MonoSingleton<MusicManager>.Instance.battleTheme.clip = eggRoomMusic;
			MonoSingleton<MusicManager>.Instance.bossTheme.clip = eggRoomMusic;
			MonoSingleton<MusicManager>.Instance.cleanTheme.pitch = 1f;
			MonoSingleton<MusicManager>.Instance.battleTheme.pitch = 1f;
			MonoSingleton<MusicManager>.Instance.bossTheme.pitch = 1f;
			MonoSingleton<MusicManager>.Instance.StartMusic();
		}
		else
		{
			Debug.LogWarning((object)"Egg room music is not loaded.");
		}
		previousFog = RenderSettings.fog;
		RenderSettings.fog = false;
		MonoSingleton<StatsManager>.Instance.currentCheckPoint.onRestart.AddListener(new UnityAction(OnCheckpointReload));
	}

	public void OnExitEggRoom()
	{
		Object.Destroy((Object)(object)((Component)roomDoorAdd).gameObject);
		MonoSingleton<MusicManager>.Instance.StopMusic();
		MonoSingleton<MusicManager>.Instance.cleanTheme.clip = lastCalmMusic;
		MonoSingleton<MusicManager>.Instance.battleTheme.clip = lastCombatMusic;
		MonoSingleton<MusicManager>.Instance.bossTheme.clip = lastBossMusic;
		MonoSingleton<MusicManager>.Instance.cleanTheme.pitch = 1f;
		MonoSingleton<MusicManager>.Instance.battleTheme.pitch = 1f;
		MonoSingleton<MusicManager>.Instance.bossTheme.pitch = 1f;
		MonoSingleton<MusicManager>.Instance.StartMusic();
		RenderSettings.fog = previousFog;
		GenericTeleport(exitPos);
	}

	public void GenericTeleport(GameObject targetPos)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		NewMovement instance = MonoSingleton<NewMovement>.Instance;
		if (Object.op_Implicit((Object)(object)instance.ridingRocket))
		{
			instance.ridingRocket.PlayerRideEnd();
		}
		instance.StopMovement();
		instance.gc.heavyFall = false;
		((Component)instance).transform.position = targetPos.transform.position;
		((Component)instance).transform.rotation = targetPos.transform.rotation;
		instance.dodgeDirection = Vector3.forward;
	}

	private void OnDestroy()
	{
		if ((Object)(object)Instance == (Object)(object)this)
		{
			Instance = null;
		}
	}

	public void OnCheckpointReload()
	{
		Object.Destroy((Object)(object)((Component)roomDoorAdd).gameObject);
		MonoSingleton<MusicManager>.Instance.StopMusic();
		MonoSingleton<MusicManager>.Instance.cleanTheme.clip = lastCalmMusic;
		MonoSingleton<MusicManager>.Instance.battleTheme.clip = lastCombatMusic;
		MonoSingleton<MusicManager>.Instance.bossTheme.clip = lastBossMusic;
		MonoSingleton<MusicManager>.Instance.StartMusic();
		RenderSettings.fog = previousFog;
	}
}
namespace EggRoom
{
	[BepInPlugin("meticulouslyrare.EggRoom", "Mysterious Man's Room", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin? Instance;

		public static string eggContent = Path.Combine(ModPath(), "EggContent");

		public bool isContentLoaded;

		public List<Door> currentLevelDoors = new List<Door>();

		public List<Door> currentDoorCandidates = new List<Door>();

		public string eggRoomHash = "5d293bcd90f9e5041b0846d3a3970b97";

		public string eggMusicHash = "67c1d91c51199fe44bc8d12dbba8479e";

		public PluginConfigurator config;

		public bool modEnabled = true;

		public float behaviorSpawnChance = 5f;

		private static readonly HashSet<string> forbiddenDoors = new HashSet<string> { "DoorLeft", "DoorRight", "Halls Entrance" };

		public static string ModPath()
		{
			return Assembly.GetExecutingAssembly().Location.Substring(0, Assembly.GetExecutingAssembly().Location.LastIndexOf(Path.DirectorySeparatorChar));
		}

		private bool ShouldSpawnEggRoom()
		{
			if (modEnabled)
			{
				return Random.Range(0f, 100f) < behaviorSpawnChance;
			}
			return false;
		}

		private void Awake()
		{
			//IL_0049: 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_0084: Expected O, but got Unknown
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Expected O, but got Unknown
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: 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
			Instance = this;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Egg Room loaded!");
			if (!isContentLoaded)
			{
				((MonoBehaviour)this).StartCoroutine(LoadCatalogAndBundle(forceReload: true));
				isContentLoaded = true;
			}
			SceneManager.sceneLoaded += OnSceneLoaded;
			new Harmony("meticulouslyrare.EggRoom").PatchAll();
			config = PluginConfigurator.Create("Mysterious Man's Room", "meticulouslyrare.EggRoom");
			BoolField val = new BoolField(config.rootPanel, "Enable Mod", "meticulouslyrare.eggroom.enabled", true);
			FloatField val2 = new FloatField(config.rootPanel, "Behavior Spawn Chance", "meticulouslyrare.eggroom.spawnchance", 5f, 0f, 100f);
			modEnabled = val.value;
			behaviorSpawnChance = val2.value;
			val.onValueChange += (BoolValueChangeEventDelegate)delegate(BoolValueChangeEvent e)
			{
				modEnabled = e.value;
			};
			val2.onValueChange += (FloatValueChangeEventDelegate)delegate(FloatValueChangeEvent e)
			{
				behaviorSpawnChance = e.value;
			};
		}

		private T[] FindAllInstances<T>() where T : Object
		{
			return Resources.FindObjectsOfTypeAll<T>();
		}

		private void SaveCatalogKeysToFile()
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (object item in Addressables.ResourceLocators.SelectMany((IResourceLocator locator) => locator.Keys))
			{
				stringBuilder.AppendLine(item.ToString());
			}
			string text = Path.Combine(ModPath(), "catalogInfo.txt");
			try
			{
				File.WriteAllText(text, stringBuilder.ToString());
				Debug.Log((object)("Catalog keys saved to: " + text));
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("Failed to save catalog keys: " + ex.Message));
			}
		}

		public void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			((MonoBehaviour)this).StartCoroutine(SetupDoors());
		}

		private IEnumerator SetupDoors()
		{
			yield return null;
			currentLevelDoors.Clear();
			currentDoorCandidates.Clear();
			Door[] collection = Object.FindObjectsByType<Door>((FindObjectsInactive)1, (FindObjectsSortMode)0);
			currentLevelDoors.AddRange(collection);
			foreach (Door currentLevelDoor in currentLevelDoors)
			{
				Debug.LogError((object)("Door found: " + ((Object)currentLevelDoor).name));
				if (forbiddenDoors.Contains(((Object)currentLevelDoor).name))
				{
					Debug.LogError((object)("Skipping forbidden door: " + ((Object)currentLevelDoor).name));
					continue;
				}
				GameObject[] activatedRooms = currentLevelDoor.activatedRooms;
				if (((activatedRooms != null && activatedRooms.Length != 0) ? 1 : 0) <= (false ? 1 : 0))
				{
					GameObject[] deactivatedRooms = currentLevelDoor.deactivatedRooms;
					if (((deactivatedRooms != null && deactivatedRooms.Length != 0) ? 1 : 0) <= (false ? 1 : 0))
					{
						continue;
					}
				}
				Debug.LogError((object)("Candidate: " + ((Object)currentLevelDoor).name));
				currentDoorCandidates.Add(currentLevelDoor);
			}
			Debug.LogError((object)$"Candidates: {currentDoorCandidates.Count}");
			if (currentDoorCandidates.Count == 0)
			{
				Debug.LogError((object)"No valid door candidates.");
				yield break;
			}
			bool flag = ShouldSpawnEggRoom();
			Debug.LogError((object)$"ShouldSpawn = {flag}");
			if (flag)
			{
				Debug.LogError((object)"Starting instantiate...");
				AsyncOperationHandle<GameObject> handle = Addressables.InstantiateAsync((object)eggRoomHash, (Transform)null, false, true);
				yield return handle;
				Debug.LogError((object)$"Instantiate status: {handle.Status}");
				if ((int)handle.Status != 1)
				{
					Debug.LogError((object)"Instantiate failed.");
					Debug.LogException(handle.OperationException);
					yield break;
				}
				_ = handle.Result;
				Debug.LogError((object)"Instantiate succeeded.");
				int index = Random.Range(0, currentDoorCandidates.Count);
				Door val = currentDoorCandidates[index];
				Debug.LogError((object)("Door chosen: " + ((Object)val).name));
				GameObject val2 = new GameObject("_EggRoomDoorAdd");
				val2.transform.SetParent(((Component)val).transform, false);
				val2.AddComponent<_EggRoomDoorAdd>().parentDoor = val;
				Debug.LogError((object)"Finished SetupDoors.");
			}
		}

		private IEnumerator LoadCatalogAndBundle(bool forceReload)
		{
			string text = Path.Combine(eggContent, "catalog.json");
			string text2 = Path.Combine(eggContent, "patched_catalog.json");
			if (!File.Exists(text))
			{
				Debug.LogError((object)("Catalog file not found at: " + text));
				yield break;
			}
			try
			{
				JObject val = JObject.Parse(File.ReadAllText(text));
				string text3 = "{AngryLevelLoader.Plugin.tempFolderPath}\\\\";
				string newValue = "{EggRoom.Plugin.eggContent}\\";
				JToken val2 = ((JToken)val).SelectToken("m_InternalIds");
				if (val2 != null && (int)val2.Type == 2)
				{
					JArray val3 = (JArray)val2;
					for (int i = 0; i < ((JContainer)val3).Count; i++)
					{
						string text4 = ((object)val3[i]).ToString();
						if (text4.Contains(text3))
						{
							val3[i] = JToken.op_Implicit(text4.Replace(text3, newValue));
						}
					}
				}
				File.WriteAllText(text2, ((JToken)val).ToString((Formatting)1, Array.Empty<JsonConverter>()));
				Debug.Log((object)"Patched catalog created successfully.");
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("Error while patching catalog: " + ex.Message));
				yield break;
			}
			AsyncOperationHandle<IResourceLocator> addressableHandle = Addressables.LoadContentCatalogAsync(text2, false, (string)null);
			yield return (object)new WaitUntil((Func<bool>)(() => addressableHandle.IsDone || (int)addressableHandle.Status == 2));
			if ((int)addressableHandle.Status == 1)
			{
				Debug.Log((object)"Patched catalog loaded successfully.");
				SaveCatalogKeysToFile();
			}
			else
			{
				Debug.LogError((object)$"Failed to load patched catalog: {addressableHandle.OperationException}");
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		internal IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}