Decompiled source of InfiniteRoom v1.0.0

plugins/LevelOverrideMod.dll

Decompiled a day ago
using System;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LevelOverrideMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LevelOverrideMod")]
[assembly: AssemblyTitle("LevelOverrideMod")]
[assembly: AssemblyVersion("1.0.0.0")]
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;
		}
	}
}
namespace LevelOverrideMod
{
	[BepInPlugin("com.atomarium-collin.leveloverridemod", "Level Override Mod", "1.0.0")]
	public class LevelOverridePlugin : BaseUnityPlugin
	{
		private class LevelOverrideData
		{
			public string selectedLevel { get; set; }
		}

		private static M_Level cachedLevel;

		private static string selectedLevelName;

		private static string configPath;

		private static bool selectionAttempted;

		private static bool configGenerated;

		private void Awake()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Expected O, but got Unknown
			//IL_00aa: Expected O, but got Unknown
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Expected O, but got Unknown
			configPath = Path.Combine(Paths.ConfigPath, "leveloverride.json");
			Harmony val = new Harmony("com.atomarium-collin.leveloverridemod");
			MethodInfo methodInfo = typeof(M_Region).GetMethods(BindingFlags.Instance | BindingFlags.Public).FirstOrDefault((MethodInfo m) => m.Name == "GetLevels" && m.ReturnType == typeof(List<M_Level>));
			if (methodInfo != null)
			{
				MethodInfo method = typeof(LevelOverridePlugin).GetMethod("GetLevelsPrefix", BindingFlags.Static | BindingFlags.Public);
				MethodInfo method2 = typeof(LevelOverridePlugin).GetMethod("GetLevelsPostfix", BindingFlags.Static | BindingFlags.Public);
				val.Patch((MethodBase)methodInfo, new HarmonyMethod(method), new HarmonyMethod(method2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				Debug.LogWarning((object)"[LevelOverrideMod] Patched GetLevels with prefix and postfix");
			}
			MethodInfo method3 = typeof(M_Region).GetMethod("GetSubregionQueue", BindingFlags.Instance | BindingFlags.NonPublic);
			if (method3 != null)
			{
				MethodInfo method4 = typeof(LevelOverridePlugin).GetMethod("GetSubregionQueuePrefix", BindingFlags.Static | BindingFlags.Public);
				val.Patch((MethodBase)method3, new HarmonyMethod(method4), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				Debug.LogWarning((object)"[LevelOverrideMod] Patched GetSubregionQueue");
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Level Override Mod Loaded!");
		}

		public static void GetLevelsPostfix(M_Region __instance, ref List<M_Level> __result)
		{
			if (__result == null)
			{
				__result = new List<M_Level>();
			}
			if (!configGenerated && __result.Count > 0)
			{
				TryGenerateConfig(__result);
			}
			if ((Object)(object)cachedLevel != (Object)null)
			{
				__result.Clear();
				__result.Add(cachedLevel);
				return;
			}
			List<M_Level> list = new List<M_Level>();
			foreach (SubregionGroup item in __instance.subregionGroups ?? new List<SubregionGroup>())
			{
				foreach (M_Subregion item2 in item.subregions ?? new List<M_Subregion>())
				{
					foreach (M_Level item3 in item2.levels ?? new List<M_Level>())
					{
						if (item3.spawnSettings.GetEffectiveSpawnChance() > 0f)
						{
							list.Add(item3);
						}
					}
				}
			}
			if (__instance.startLevels != null)
			{
				list.AddRange(__instance.startLevels);
			}
			if (!configGenerated && list.Count > 0)
			{
				TryGenerateConfig(list);
			}
			if (selectionAttempted)
			{
				Debug.LogWarning((object)"[LevelOverrideMod] Selection already attempted. Using cached fallback.");
			}
			else
			{
				selectionAttempted = true;
				if (!string.IsNullOrEmpty(selectedLevelName))
				{
					cachedLevel = ((IEnumerable<M_Level>)list).FirstOrDefault((Func<M_Level, bool>)((M_Level l) => ((Object)l).name == selectedLevelName));
					if ((Object)(object)cachedLevel != (Object)null)
					{
						Debug.LogWarning((object)("[LevelOverrideMod] Selected level from config: " + ((Object)cachedLevel).name));
					}
					else
					{
						Debug.LogWarning((object)"[LevelOverrideMod] Config level not found, falling back to random.");
					}
				}
				if ((Object)(object)cachedLevel == (Object)null && list.Count > 0)
				{
					cachedLevel = list[Random.Range(0, list.Count)];
					Debug.LogWarning((object)("[LevelOverrideMod] Randomly selected level: " + ((Object)cachedLevel).name));
				}
				if ((Object)(object)cachedLevel == (Object)null && __result.Count > 0)
				{
					cachedLevel = __result[0];
					Debug.LogWarning((object)("[LevelOverrideMod] Fallback to original list: " + ((Object)cachedLevel).name));
				}
			}
			if ((Object)(object)cachedLevel != (Object)null)
			{
				__result.Clear();
				__result.Add(cachedLevel);
				Debug.LogWarning((object)("[LevelOverrideMod] Applied level override: " + ((Object)cachedLevel).name));
			}
		}

		private static void TryGenerateConfig(List<M_Level> availableLevels)
		{
			try
			{
				if (!configGenerated && !File.Exists(configPath) && availableLevels.Count > 0)
				{
					Directory.CreateDirectory(Paths.ConfigPath);
					M_Level val = availableLevels[Random.Range(0, availableLevels.Count)];
					LevelOverrideData levelOverrideData = new LevelOverrideData
					{
						selectedLevel = ((Object)val).name
					};
					string contents = JsonConvert.SerializeObject((object)levelOverrideData, (Formatting)1);
					File.WriteAllText(configPath, contents);
					Debug.LogWarning((object)("[LevelOverrideMod] Generated new config with random level: " + ((Object)val).name));
					selectedLevelName = ((Object)val).name;
				}
				else if (File.Exists(configPath))
				{
					string text = File.ReadAllText(configPath);
					LevelOverrideData levelOverrideData2 = JsonConvert.DeserializeObject<LevelOverrideData>(text);
					selectedLevelName = (string.IsNullOrWhiteSpace(levelOverrideData2.selectedLevel) ? null : levelOverrideData2.selectedLevel);
					Debug.LogWarning((object)("[LevelOverrideMod] Config loaded: " + selectedLevelName));
				}
				configGenerated = true;
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("[LevelOverrideMod] Error with config: " + ex.Message));
			}
		}

		public static bool GetLevelsPrefix(M_Region __instance, ref List<M_Level> __result)
		{
			if ((Object)(object)cachedLevel != (Object)null)
			{
				__result = new List<M_Level> { cachedLevel };
				return false;
			}
			return true;
		}

		public static bool GetSubregionQueuePrefix(M_Region __instance, ref Queue<M_Subregion> __result)
		{
			if ((Object)(object)cachedLevel != (Object)null)
			{
				__result = new Queue<M_Subregion>();
				if ((Object)(object)cachedLevel.subRegion != (Object)null)
				{
					__result.Enqueue(cachedLevel.subRegion);
				}
				return false;
			}
			return true;
		}
	}
}