Decompiled source of Backrooms Load Fix v1.0.1

BepInEx/plugins/BackroomsLoadFix/BackroomsLoadFix.dll

Decompiled 3 hours 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 System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("BackroomsLoadFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("BackroomsLoadFix")]
[assembly: AssemblyTitle("BackroomsLoadFix")]
[assembly: AssemblyVersion("1.0.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 BackroomsLoadFix
{
	[BepInPlugin("repo.backroomsloadfix", "Backrooms Load Fix", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "repo.backroomsloadfix";

		public const string Name = "Backrooms Load Fix";

		public const string Version = "1.0.1";

		private const string BackroomsLevelName = "Level - Backrooms";

		private const string BackroomsDllName = "Backrooms.dll";

		private static ManualLogSource log;

		private static FieldInfo fieldInStartRoom;

		private static ConfigEntry<bool> applyOnNewerBackrooms;

		private static bool? armed;

		private void Awake()
		{
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Expected O, but got Unknown
			log = ((BaseUnityPlugin)this).Logger;
			applyOnNewerBackrooms = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ApplyOnNewerBackrooms", false, "The fix is armed only for Backrooms " + BackroomsVersion.LastBroken?.ToString() + " and older. On a newer Backrooms it assumes the author fixed the level and stays off. Set this to true if a newer Backrooms still drops you to the main menu while loading.");
			fieldInStartRoom = AccessTools.Field(typeof(LevelPoint), "inStartRoom");
			MethodInfo methodInfo = AccessTools.Method(typeof(EnemyDirector), "FirstSpawnPointAdd", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				log.LogWarning((object)"EnemyDirector.FirstSpawnPointAdd not found in this game version; the fix is inactive.");
			}
			else
			{
				new Harmony("repo.backroomsloadfix").Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin), "FirstSpawnPointAddPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}

		private void Start()
		{
			IsArmed();
		}

		private static bool IsArmed()
		{
			if (armed.HasValue)
			{
				return armed.Value;
			}
			string source;
			Version version = FindBackroomsVersion(out source);
			if (version == null)
			{
				armed = true;
				log.LogWarning((object)("Could not read the Backrooms version (" + source + "); the fix is armed."));
			}
			else if (BackroomsVersion.IsKnownBroken(version))
			{
				armed = true;
				log.LogInfo((object)("Backrooms " + version?.ToString() + " found (" + source + "); the fix is armed."));
			}
			else if (applyOnNewerBackrooms.Value)
			{
				armed = true;
				log.LogWarning((object)("Backrooms " + version?.ToString() + " is newer than " + BackroomsVersion.LastBroken?.ToString() + ", but ApplyOnNewerBackrooms = true; the fix is armed."));
			}
			else
			{
				armed = false;
				log.LogWarning((object)("Backrooms " + version?.ToString() + " is newer than " + BackroomsVersion.LastBroken?.ToString() + "; assuming the author fixed it, so this fix stays off. You can probably remove this mod. If Backrooms still fails to load, set ApplyOnNewerBackrooms = true in BepInEx/config/repo.backroomsloadfix.cfg."));
			}
			return armed.Value;
		}

		private static Version FindBackroomsVersion(out string source)
		{
			PluginInfo val = ((IEnumerable<PluginInfo>)Chainloader.PluginInfos.Values).FirstOrDefault((Func<PluginInfo, bool>)((PluginInfo p) => p.Location != null && string.Equals(Path.GetFileName(p.Location), "Backrooms.dll", StringComparison.OrdinalIgnoreCase)));
			if (val == null)
			{
				source = "Backrooms is not loaded";
				return null;
			}
			string directoryName = Path.GetDirectoryName(val.Location);
			string[] array = new string[2]
			{
				directoryName,
				Path.GetDirectoryName(directoryName)
			};
			foreach (string text in array)
			{
				if (string.IsNullOrEmpty(text))
				{
					continue;
				}
				string text2 = Path.Combine(text, "manifest.json");
				if (File.Exists(text2))
				{
					Version version = BackroomsVersion.FromManifest(File.ReadAllText(text2));
					if (version != null)
					{
						source = text2;
						return version;
					}
				}
			}
			source = "no Backrooms manifest.json next to " + val.Location;
			return null;
		}

		private static bool InStartRoom(LevelPoint point)
		{
			if (fieldInStartRoom != null)
			{
				return (bool)fieldInStartRoom.GetValue(point);
			}
			return false;
		}

		private static void FirstSpawnPointAddPrefix()
		{
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: 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)
			LevelGenerator instance = LevelGenerator.Instance;
			if ((Object)(object)instance == (Object)null || (Object)(object)instance.LevelPathTruck != (Object)null || instance.LevelPathPoints == null)
			{
				return;
			}
			string text = (((Object)(object)RunManager.instance != (Object)null && (Object)(object)RunManager.instance.levelCurrent != (Object)null) ? ((Object)RunManager.instance.levelCurrent).name : null);
			if (text != "Level - Backrooms" || !IsArmed())
			{
				return;
			}
			Vector3? val = (((Object)(object)TruckSafetySpawnPoint.instance != (Object)null) ? new Vector3?(((Component)TruckSafetySpawnPoint.instance).transform.position) : ((Vector3?)null));
			LevelPoint val2 = null;
			float num = float.MaxValue;
			int num2 = 0;
			foreach (LevelPoint levelPathPoint in instance.LevelPathPoints)
			{
				if (!((Object)(object)levelPathPoint == (Object)null))
				{
					bool flag = InStartRoom(levelPathPoint);
					if (flag)
					{
						num2++;
					}
					float num3 = (val.HasValue ? Vector3.Distance(((Component)levelPathPoint).transform.position, val.Value) : (flag ? 0f : 1f));
					if (num3 < num)
					{
						num = num3;
						val2 = levelPathPoint;
					}
				}
			}
			if (!((Object)(object)val2 == (Object)null))
			{
				instance.LevelPathTruck = val2;
				ManualLogSource obj = log;
				string[] obj2 = new string[13]
				{
					"Level '",
					text,
					"' has no truck level point (",
					instance.LevelPathPoints.Count.ToString(),
					" points, ",
					num2.ToString(),
					" in the start room, truck safety spawn ",
					val.HasValue ? "found" : "missing",
					"). Using '",
					((Object)val2).name,
					"' at ",
					null,
					null
				};
				Vector3 position = ((Component)val2).transform.position;
				obj2[11] = ((Vector3)(ref position)).ToString("F1");
				obj2[12] = " instead.";
				obj.LogWarning((object)string.Concat(obj2));
			}
		}
	}
	internal static class BackroomsVersion
	{
		internal const string PackageName = "Backrooms";

		internal static readonly Version LastBroken = new Version(1, 1, 9);

		private static readonly Regex NameField = new Regex("\"name\"\\s*:\\s*\"([^\"]*)\"");

		private static readonly Regex VersionField = new Regex("\"version_number\"\\s*:\\s*\"([0-9]+\\.[0-9]+\\.[0-9]+)\"");

		internal static Version FromManifest(string json)
		{
			if (json == null)
			{
				return null;
			}
			Match match = NameField.Match(json);
			Match match2 = VersionField.Match(json);
			if (!match.Success || match.Groups[1].Value != "Backrooms" || !match2.Success)
			{
				return null;
			}
			return new Version(match2.Groups[1].Value);
		}

		internal static bool IsKnownBroken(Version installed)
		{
			return installed <= LastBroken;
		}
	}
}