Decompiled source of Moon Scrap Limits Patch v1.0.0

BepInEx/plugins/MoonScrapLimitsPatch/MoonScrapLimitsPatch.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
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 MoonScrapLimitsPatch.NetcodePatcher;
using MoonScrapLimitsPatch.Patches;
using MoonScrapLimitsPatch.Util;

[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: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[module: NetcodePatchedAssembly]
internal class <Module>
{
	static <Module>()
	{
	}
}
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 MoonScrapLimitsPatch
{
	[BepInPlugin("com.github.WhiteSpike.MoonScrapLimitsPatch", "Moon Scrap Limits Patch", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static readonly Harmony harmony = new Harmony("com.github.WhiteSpike.MoonScrapLimitsPatch");

		internal static readonly ManualLogSource mls = Logger.CreateLogSource("Moon Scrap Limits Patch");

		private void Awake()
		{
			harmony.PatchAll(typeof(RoundManagerPatcher));
			mls.LogInfo((object)"Moon Scrap Limits Patch 1.0.0 has been loaded successfully.");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "MoonScrapLimitsPatch";

		public const string PLUGIN_NAME = "MoonScrapLimitsPatch";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace MoonScrapLimitsPatch.Util
{
	internal static class Tools
	{
		public static void FindCodeInstruction(ref int index, ref List<CodeInstruction> codes, object findValue, MethodInfo addCode, bool skip = false, bool requireInstance = false, bool notInstruction = false, bool andInstruction = false, bool orInstruction = false, string errorMessage = "Not found")
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Expected O, but got Unknown
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Expected O, but got Unknown
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Expected O, but got Unknown
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Expected O, but got Unknown
			bool flag = false;
			while (index < codes.Count)
			{
				if (CheckCodeInstruction(codes[index], findValue))
				{
					flag = true;
					if (!skip)
					{
						if (andInstruction)
						{
							codes.Insert(index + 1, new CodeInstruction(OpCodes.And, (object)null));
						}
						if (!andInstruction && orInstruction)
						{
							codes.Insert(index + 1, new CodeInstruction(OpCodes.Or, (object)null));
						}
						if (notInstruction)
						{
							codes.Insert(index + 1, new CodeInstruction(OpCodes.Not, (object)null));
						}
						codes.Insert(index + 1, new CodeInstruction(OpCodes.Call, (object)addCode));
						if (requireInstance)
						{
							codes.Insert(index + 1, new CodeInstruction(OpCodes.Ldarg_0, (object)null));
						}
					}
					break;
				}
				index++;
			}
			if (!flag)
			{
				Plugin.mls.LogError((object)errorMessage);
			}
			index++;
		}

		public static void FindLocalField(ref int index, ref List<CodeInstruction> codes, int localIndex, object addCode = null, bool skip = false, bool store = false, bool requireInstance = false, string errorMessage = "Not found")
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			bool flag = false;
			while (index < codes.Count)
			{
				if (CheckCodeInstruction(codes[index], localIndex, store))
				{
					flag = true;
					if (!skip)
					{
						codes.Insert(index + 1, new CodeInstruction(OpCodes.Call, addCode));
						if (requireInstance)
						{
							codes.Insert(index + 1, new CodeInstruction(OpCodes.Ldarg_0, (object)null));
						}
					}
					break;
				}
				index++;
			}
			if (!flag)
			{
				Plugin.mls.LogError((object)errorMessage);
			}
			index++;
		}

		public static void FindString(ref int index, ref List<CodeInstruction> codes, string findValue, MethodInfo addCode = null, bool skip = false, bool notInstruction = false, bool andInstruction = false, bool orInstruction = false, bool requireInstance = false, string errorMessage = "Not found")
		{
			FindCodeInstruction(ref index, ref codes, findValue, addCode, skip, requireInstance, notInstruction, andInstruction, orInstruction, errorMessage);
		}

		public static void FindField(ref int index, ref List<CodeInstruction> codes, FieldInfo findField, MethodInfo addCode = null, bool skip = false, bool notInstruction = false, bool andInstruction = false, bool orInstruction = false, bool requireInstance = false, string errorMessage = "Not found")
		{
			FindCodeInstruction(ref index, ref codes, findField, addCode, skip, requireInstance, notInstruction, andInstruction, orInstruction, errorMessage);
		}

		public static void FindMethod(ref int index, ref List<CodeInstruction> codes, MethodInfo findMethod, MethodInfo addCode = null, bool skip = false, bool notInstruction = false, bool andInstruction = false, bool orInstruction = false, bool requireInstance = false, string errorMessage = "Not found")
		{
			FindCodeInstruction(ref index, ref codes, findMethod, addCode, skip, requireInstance, notInstruction, andInstruction, orInstruction, errorMessage);
		}

		public static void FindFloat(ref int index, ref List<CodeInstruction> codes, float findValue, MethodInfo addCode = null, bool skip = false, bool notInstruction = false, bool andInstruction = false, bool orInstruction = false, bool requireInstance = false, string errorMessage = "Not found")
		{
			FindCodeInstruction(ref index, ref codes, findValue, addCode, skip, requireInstance, notInstruction, andInstruction, orInstruction, errorMessage);
		}

		public static void FindInteger(ref int index, ref List<CodeInstruction> codes, sbyte findValue, MethodInfo addCode = null, bool skip = false, bool notInstruction = false, bool andInstruction = false, bool orInstruction = false, bool requireInstance = false, string errorMessage = "Not found")
		{
			FindCodeInstruction(ref index, ref codes, findValue, addCode, skip, requireInstance, notInstruction, andInstruction, orInstruction, errorMessage);
		}

		public static void FindSub(ref int index, ref List<CodeInstruction> codes, MethodInfo addCode = null, bool skip = false, bool notInstruction = false, bool andInstruction = false, bool orInstruction = false, bool requireInstance = false, string errorMessage = "Not found")
		{
			object findValue = OpCodes.Sub;
			bool notInstruction2 = notInstruction;
			bool andInstruction2 = andInstruction;
			bool orInstruction2 = orInstruction;
			FindCodeInstruction(ref index, ref codes, findValue, addCode, skip, requireInstance, notInstruction2, andInstruction2, orInstruction2, errorMessage);
		}

		public static void FindDiv(ref int index, ref List<CodeInstruction> codes, MethodInfo addCode = null, bool skip = false, bool notInstruction = false, bool andInstruction = false, bool orInstruction = false, bool requireInstance = false, string errorMessage = "Not found")
		{
			object findValue = OpCodes.Div;
			bool notInstruction2 = notInstruction;
			bool andInstruction2 = andInstruction;
			bool orInstruction2 = orInstruction;
			FindCodeInstruction(ref index, ref codes, findValue, addCode, skip, requireInstance, notInstruction2, andInstruction2, orInstruction2, errorMessage);
		}

		public static void FindAdd(ref int index, ref List<CodeInstruction> codes, MethodInfo addCode = null, bool skip = false, bool notInstruction = false, bool andInstruction = false, bool orInstruction = false, bool requireInstance = false, string errorMessage = "Not found")
		{
			object findValue = OpCodes.Add;
			bool notInstruction2 = notInstruction;
			bool andInstruction2 = andInstruction;
			bool orInstruction2 = orInstruction;
			FindCodeInstruction(ref index, ref codes, findValue, addCode, skip, requireInstance, notInstruction2, andInstruction2, orInstruction2, errorMessage);
		}

		public static void FindMul(ref int index, ref List<CodeInstruction> codes, MethodInfo addCode = null, bool skip = false, bool notInstruction = false, bool andInstruction = false, bool orInstruction = false, bool requireInstance = false, string errorMessage = "Not found")
		{
			object findValue = OpCodes.Mul;
			bool notInstruction2 = notInstruction;
			bool andInstruction2 = andInstruction;
			bool orInstruction2 = orInstruction;
			FindCodeInstruction(ref index, ref codes, findValue, addCode, skip, requireInstance, notInstruction2, andInstruction2, orInstruction2, errorMessage);
		}

		private static bool CheckCodeInstruction(CodeInstruction code, int localIndex, bool store = false)
		{
			if (!store)
			{
				return localIndex switch
				{
					0 => code.opcode == OpCodes.Ldloc_0, 
					1 => code.opcode == OpCodes.Ldloc_1, 
					2 => code.opcode == OpCodes.Ldloc_2, 
					3 => code.opcode == OpCodes.Ldloc_3, 
					_ => (code.opcode == OpCodes.Ldloc || code.opcode == OpCodes.Ldloc_S) && code.operand is LocalBuilder localBuilder && localBuilder.LocalIndex == localIndex, 
				};
			}
			return localIndex switch
			{
				0 => code.opcode == OpCodes.Stloc_0, 
				1 => code.opcode == OpCodes.Stloc_1, 
				2 => code.opcode == OpCodes.Stloc_2, 
				3 => code.opcode == OpCodes.Stloc_3, 
				_ => (code.opcode == OpCodes.Stloc || code.opcode == OpCodes.Stloc_S) && code.operand is LocalBuilder localBuilder2 && localBuilder2.LocalIndex == localIndex, 
			};
		}

		private static bool CheckCodeInstruction(CodeInstruction code, object findValue)
		{
			if (findValue is sbyte)
			{
				return CheckIntegerCodeInstruction(code, findValue);
			}
			if (findValue is float)
			{
				return code.opcode == OpCodes.Ldc_R4 && code.operand.Equals(findValue);
			}
			if (findValue is string)
			{
				return code.opcode == OpCodes.Ldstr && code.operand.Equals(findValue);
			}
			if (findValue is MethodInfo)
			{
				return (code.opcode == OpCodes.Call || code.opcode == OpCodes.Callvirt) && code.operand == findValue;
			}
			if (findValue is FieldInfo)
			{
				return (code.opcode == OpCodes.Ldfld || code.opcode == OpCodes.Stfld) && code.operand == findValue;
			}
			if (findValue is OpCode)
			{
				return code.opcode == (OpCode)findValue;
			}
			return false;
		}

		private static bool CheckIntegerCodeInstruction(CodeInstruction code, object findValue)
		{
			return (sbyte)findValue switch
			{
				0 => code.opcode == OpCodes.Ldc_I4_0, 
				1 => code.opcode == OpCodes.Ldc_I4_1, 
				2 => code.opcode == OpCodes.Ldc_I4_2, 
				3 => code.opcode == OpCodes.Ldc_I4_3, 
				4 => code.opcode == OpCodes.Ldc_I4_4, 
				5 => code.opcode == OpCodes.Ldc_I4_5, 
				6 => code.opcode == OpCodes.Ldc_I4_6, 
				7 => code.opcode == OpCodes.Ldc_I4_7, 
				8 => code.opcode == OpCodes.Ldc_I4_8, 
				_ => code.opcode == OpCodes.Ldc_I4_S && code.operand.Equals(findValue), 
			};
		}
	}
}
namespace MoonScrapLimitsPatch.Patches
{
	[HarmonyPatch(typeof(RoundManager))]
	internal static class RoundManagerPatcher
	{
		[HarmonyPatch("SpawnScrapInLevel")]
		[HarmonyTranspiler]
		internal static IEnumerable<CodeInstruction> RoundManagerSpawnScrapInLevel_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Expected O, but got Unknown
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Expected O, but got Unknown
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Expected O, but got Unknown
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Expected O, but got Unknown
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Expected O, but got Unknown
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Expected O, but got Unknown
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Expected O, but got Unknown
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Expected O, but got Unknown
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Expected O, but got Unknown
			//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Expected O, but got Unknown
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Expected O, but got Unknown
			//IL_021c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0226: Expected O, but got Unknown
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0239: Expected O, but got Unknown
			//IL_0247: Unknown result type (might be due to invalid IL or missing references)
			//IL_0251: Expected O, but got Unknown
			//IL_025a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0264: Expected O, but got Unknown
			List<CodeInstruction> codes = new List<CodeInstruction>(instructions);
			int i = 0;
			Tools.FindLocalField(ref i, ref codes, 4, null, skip: true, store: true, requireInstance: false, "Couldn't find the local field used to store minTotalScrapValue");
			codes.RemoveAt(i - 2);
			codes.Insert(i - 2, new CodeInstruction(OpCodes.Callvirt, (object)AccessTools.Method(typeof(Random), "Next", new Type[2]
			{
				typeof(int),
				typeof(int)
			}, (Type[])null)));
			codes.Insert(i - 2, new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(SelectableLevel), "maxTotalScrapValue")));
			codes.Insert(i - 2, new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(RoundManager), "currentLevel")));
			codes.Insert(i - 2, new CodeInstruction(OpCodes.Ldarg_0, (object)null));
			codes.Insert(i - 2, new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(SelectableLevel), "minTotalScrapValue")));
			codes.Insert(i - 2, new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(RoundManager), "currentLevel")));
			codes.Insert(i - 2, new CodeInstruction(OpCodes.Ldarg_0, (object)null));
			codes.Insert(i - 2, new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(RoundManager), "AnomalyRandom")));
			codes.Insert(i - 2, new CodeInstruction(OpCodes.Ldarg_0, (object)null));
			Tools.FindLocalField(ref i, ref codes, 4, null, skip: true, store: true, requireInstance: false, "Couldn't find the local field used to store minTotalScrapValue");
			Tools.FindLocalField(ref i, ref codes, 4, null, skip: true, store: true, requireInstance: false, "Couldn't find the local field used to store minTotalScrapValue");
			codes.RemoveAt(i - 2);
			codes.Insert(i - 2, new CodeInstruction(OpCodes.Sub, (object)null));
			for (; i < codes.Count && !(codes[i].opcode == OpCodes.Blt); i++)
			{
			}
			codes[i].opcode = OpCodes.Brtrue;
			codes.Insert(i, new CodeInstruction(OpCodes.And, (object)null));
			codes.Insert(i, new CodeInstruction(OpCodes.Cgt, (object)null));
			codes.Insert(i, new CodeInstruction(OpCodes.Ldc_I4_0, (object)null));
			codes.Insert(i, new CodeInstruction(OpCodes.Ldloc_S, (object)4));
			codes.Insert(i, new CodeInstruction(OpCodes.Clt, (object)null));
			return codes;
		}
	}
}
namespace MoonScrapLimitsPatch.Misc
{
	internal static class Metadata
	{
		public const string GUID = "com.github.WhiteSpike.MoonScrapLimitsPatch";

		public const string NAME = "Moon Scrap Limits Patch";

		public const string VERSION = "1.0.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}
namespace MoonScrapLimitsPatch.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}