Decompiled source of RoundManagerObjectReferenceFix v1.0.1

Plugin/RoundManagerObjectReferenceFix.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("RoundManagerObjectReferenceFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+07a6d9d1c3d69f97219a693be029bfd09402e9ad")]
[assembly: AssemblyProduct("RoundManagerObjectReferenceFix")]
[assembly: AssemblyTitle("RoundManagerObjectReferenceFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
[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 RoundManagerObjectReferenceFix
{
	[BepInPlugin("RoundManagerObjectReferenceFix", "RoundManagerObjectReferenceFix", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger = null;

		private static readonly HashSet<string> Logged = new HashSet<string>();

		internal static bool FirstTime(string key)
		{
			return Logged.Add(key);
		}

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			new Harmony("RoundManagerObjectReferenceFix").PatchAll();
			Logger.LogInfo((object)"Plugin [RoundManagerObjectReferenceFix version: 1.0.1] is loaded!");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "RoundManagerObjectReferenceFix";

		public const string PLUGIN_NAME = "RoundManagerObjectReferenceFix";

		public const string PLUGIN_VERSION = "1.0.1";
	}
}
namespace RoundManagerObjectReferenceFix.Patches
{
	[HarmonyPatch(typeof(RoundManager), "SpawnOutsideHazards")]
	public class SpawnableOutsideObjectsPatch
	{
		[HarmonyPrefix]
		private static void Prefix(RoundManager __instance)
		{
			try
			{
				Check(__instance);
			}
			catch (Exception arg)
			{
				Plugin.Logger.LogError((object)$"Outside object check failed, continuing with vanilla behaviour: {arg}");
			}
		}

		private static void Check(RoundManager __instance)
		{
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Expected O, but got Unknown
			//IL_029b: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a5: Expected O, but got Unknown
			Plugin.Logger.LogInfo((object)"Checking: SpawnOutsideHazards");
			if (__instance.currentLevel.spawnableOutsideObjects == null)
			{
				return;
			}
			SelectableLevel currentLevel = __instance.currentLevel;
			for (int i = 0; i < currentLevel.spawnableOutsideObjects.Length; i++)
			{
				SpawnableOutsideObjectWithRarity val = currentLevel.spawnableOutsideObjects[i];
				if (val == null)
				{
					if (Plugin.FirstTime($"outside-null-entry:{((Object)currentLevel).name}:{i}"))
					{
						Plugin.Logger.LogWarning((object)$"Null outside object entry at index {i} in spawn list of '{((Object)currentLevel).name}'");
					}
					continue;
				}
				SpawnableOutsideObject spawnAbleObject = val.spawnableObject;
				if ((Object)(object)spawnAbleObject == (Object)null)
				{
					if (Plugin.FirstTime($"outside-null-object:{((Object)currentLevel).name}:{i}"))
					{
						Plugin.Logger.LogWarning((object)$"Outside object at index {i} in '{((Object)currentLevel).name}' has null spawnableObject, removing from spawn list");
					}
					val.randomAmount = new AnimationCurve();
					continue;
				}
				if ((Object)(object)spawnAbleObject.prefabToSpawn != (Object)null)
				{
					if (Plugin.FirstTime("outside-ok:" + ((Object)spawnAbleObject).name))
					{
						Plugin.Logger.LogInfo((object)("Outside object '" + ((Object)spawnAbleObject).name + "' | Prefab: " + ((Object)spawnAbleObject.prefabToSpawn).name + " OK"));
					}
					continue;
				}
				SpawnableOutsideObject val2 = StartOfRound.Instance.levels.Where((SelectableLevel l) => l?.spawnableOutsideObjects != null).SelectMany((SelectableLevel l) => l.spawnableOutsideObjects).FirstOrDefault((Func<SpawnableOutsideObjectWithRarity, bool>)((SpawnableOutsideObjectWithRarity o) => (Object)(object)o?.spawnableObject?.prefabToSpawn != (Object)null && ((Object)o.spawnableObject).name == ((Object)spawnAbleObject).name))?.spawnableObject;
				if ((Object)(object)val2 != (Object)null)
				{
					Plugin.Logger.LogWarning((object)("Outside object '" + ((Object)spawnAbleObject).name + "' has null prefab, resolving from vanilla level"));
					spawnAbleObject.prefabToSpawn = val2.prefabToSpawn;
					continue;
				}
				if (Plugin.FirstTime("outside-removed:" + ((Object)spawnAbleObject).name))
				{
					Plugin.Logger.LogWarning((object)("Outside object '" + ((Object)spawnAbleObject).name + "' has null prefab, removing from spawn list"));
				}
				val.randomAmount = new AnimationCurve();
			}
		}
	}
	[HarmonyPatch(typeof(RoundManager), "SpawnMapObjects")]
	public class SpawnMapObjectsPatch
	{
		[HarmonyPrefix]
		private static void Prefix(RoundManager __instance)
		{
			try
			{
				Check(__instance);
			}
			catch (Exception arg)
			{
				Plugin.Logger.LogError((object)$"Indoor hazard check failed, continuing with vanilla behaviour: {arg}");
			}
		}

		private static void Check(RoundManager __instance)
		{
			//IL_0255: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Expected O, but got Unknown
			Plugin.Logger.LogInfo((object)"Checking: SpawnMapObjects");
			if (__instance.currentLevel.indoorMapHazards == null)
			{
				return;
			}
			IndoorMapHazard[] indoorMapHazards = __instance.currentLevel.indoorMapHazards;
			foreach (IndoorMapHazard val in indoorMapHazards)
			{
				if (val == null)
				{
					Plugin.Logger.LogWarning((object)"Null indoor hazard entry in spawn list");
					continue;
				}
				IndoorMapHazardType hazardType = val.hazardType;
				if ((Object)(object)hazardType == (Object)null)
				{
					Plugin.Logger.LogWarning((object)"Null hazard type entry in spawn list");
					continue;
				}
				if ((Object)(object)hazardType.prefabToSpawn != (Object)null)
				{
					if (Plugin.FirstTime("indoor-ok:" + ((Object)hazardType).name))
					{
						Plugin.Logger.LogInfo((object)("Indoor Hazard: '" + ((Object)hazardType).name + "' | Prefab: " + ((Object)hazardType.prefabToSpawn).name + " OK"));
					}
					continue;
				}
				IndoorMapHazardType val2 = null;
				SelectableLevel[] levels = StartOfRound.Instance.levels;
				foreach (SelectableLevel val3 in levels)
				{
					if (val3?.indoorMapHazards != null)
					{
						val2 = ((IEnumerable<IndoorMapHazard>)val3.indoorMapHazards).FirstOrDefault((Func<IndoorMapHazard, bool>)((IndoorMapHazard h) => (Object)(object)h?.hazardType?.prefabToSpawn != (Object)null && ((Object)h.hazardType).name == ((Object)hazardType).name))?.hazardType;
						if ((Object)(object)val2 != (Object)null)
						{
							break;
						}
					}
				}
				if ((Object)(object)val2 != (Object)null)
				{
					Plugin.Logger.LogWarning((object)("Indoor hazard '" + ((Object)hazardType).name + "' has null prefab, resolving from vanilla level"));
					hazardType.prefabToSpawn = val2.prefabToSpawn;
					continue;
				}
				if (Plugin.FirstTime("indoor-removed:" + ((Object)hazardType).name))
				{
					Plugin.Logger.LogWarning((object)("Indoor hazard '" + ((Object)hazardType).name + "' has null prefab, removing from spawn list"));
				}
				val.numberToSpawn = new AnimationCurve();
			}
		}
	}
	[HarmonyPatch(typeof(RoundManager), "SpawnScrapInLevel")]
	public class SpawnScrapPatch
	{
		private const int ClosestCandidates = 3;

		[HarmonyPrefix]
		private static void Prefix(RoundManager __instance)
		{
			try
			{
				Check(__instance);
			}
			catch (Exception arg)
			{
				Plugin.Logger.LogError((object)$"Scrap check failed, continuing with vanilla behaviour: {arg}");
			}
		}

		private static Item PickReplacement(Item broken, List<Item> validItems)
		{
			if (broken.minValue <= 0 && broken.maxValue <= 0)
			{
				return validItems[Random.Range(0, validItems.Count)];
			}
			float brokenValue = (float)(broken.minValue + broken.maxValue) / 2f;
			List<Item> list = validItems.OrderBy((Item i) => Math.Abs((float)(i.minValue + i.maxValue) / 2f - brokenValue)).Take(3).ToList();
			return list[Random.Range(0, list.Count)];
		}

		private static void Check(RoundManager __instance)
		{
			Plugin.Logger.LogInfo((object)"Checking: SpawnScrapInLevel");
			if (__instance.currentLevel.spawnableScrap == null)
			{
				return;
			}
			List<Item> list = (from s in __instance.currentLevel.spawnableScrap
				where (Object)(object)s?.spawnableItem?.spawnPrefab != (Object)null
				select s.spawnableItem).ToList();
			if (list.Count == 0)
			{
				Plugin.Logger.LogWarning((object)"No valid items found in moon's spawn list, cannot replace null prefabs!");
				return;
			}
			bool flag = true;
			foreach (SpawnableItemWithRarity item in __instance.currentLevel.spawnableScrap)
			{
				if (item == null)
				{
					Plugin.Logger.LogWarning((object)"Null scrap entry in spawn list");
					flag = false;
					continue;
				}
				Item spawnableItem = item.spawnableItem;
				if ((Object)(object)spawnableItem == (Object)null)
				{
					Plugin.Logger.LogWarning((object)"Scrap entry has null spawnableItem, skipping");
					flag = false;
				}
				else if (!((Object)(object)spawnableItem.spawnPrefab != (Object)null))
				{
					flag = false;
					Item val = PickReplacement(spawnableItem, list);
					Plugin.Logger.LogWarning((object)("Scrap item '" + ((Object)spawnableItem).name + "' has null prefab, replacing with '" + ((Object)val).name + "'"));
					item.spawnableItem = val;
				}
			}
			if (flag)
			{
				Plugin.Logger.LogInfo((object)"All scrap items OK");
			}
		}
	}
}