Decompiled source of Moon Day Speed Multiplier Patcher v1.0.1

BepInEx/plugins/MoonDaySpeedMultiplierPatcher/MoonDaySpeedMultiplierPatcher.dll

Decompiled a month 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 MoonDaySpeedMultiplierPatcher.NetcodePatcher;
using MoonDaySpeedMultiplierPatcher.Patches;
using MoonDaySpeedMultiplierPatcher.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 MoonDaySpeedMultiplierPatcher
{
	[BepInPlugin("com.github.WhiteSpike.MoonDaySpeedMultiplierPatcher", "Moon Day Speed Multiplier Patcher", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static readonly Harmony harmony = new Harmony("com.github.WhiteSpike.MoonDaySpeedMultiplierPatcher");

		internal static ManualLogSource mls;

		private void Awake()
		{
			mls = ((BaseUnityPlugin)this).Logger;
			harmony.PatchAll(typeof(TimeOfDayPatcher));
			mls.LogInfo((object)"Moon Day Speed Multiplier Patcher 1.0.0 has been loaded successfully.");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "MoonDaySpeedMultiplierPatcher";

		public const string PLUGIN_NAME = "MoonDaySpeedMultiplierPatcher";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace MoonDaySpeedMultiplierPatcher.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 MoonDaySpeedMultiplierPatcher.Patches
{
	[HarmonyPatch(typeof(TimeOfDay))]
	internal static class TimeOfDayPatcher
	{
		[HarmonyPatch("MoveGlobalTime")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> MoveGlobalTimeTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Expected O, but got Unknown
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Expected O, but got Unknown
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Expected O, but got Unknown
			FieldInfo field = typeof(TimeOfDay).GetField("currentLevel");
			FieldInfo field2 = typeof(SelectableLevel).GetField("DaySpeedMultiplier");
			FieldInfo field3 = typeof(TimeOfDay).GetField("globalTimeSpeedMultiplier");
			List<CodeInstruction> codes = new List<CodeInstruction>(instructions);
			int index = 0;
			Tools.FindField(ref index, ref codes, field3, null, skip: true, notInstruction: false, andInstruction: false, orInstruction: false, requireInstance: false, "Couldn't find the global time speed multiplier field");
			codes.Insert(index, new CodeInstruction(OpCodes.Mul, (object)null));
			codes.Insert(index, new CodeInstruction(OpCodes.Ldfld, (object)field2));
			codes.Insert(index, new CodeInstruction(OpCodes.Ldfld, (object)field));
			codes.Insert(index, new CodeInstruction(OpCodes.Ldarg_0, (object)null));
			return codes;
		}

		[HarmonyPatch("Update")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> UpdateTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			FieldInfo field = typeof(SelectableLevel).GetField("DaySpeedMultiplier");
			int index = 0;
			List<CodeInstruction> codes = new List<CodeInstruction>(instructions);
			Tools.FindField(ref index, ref codes, field, null, skip: true, notInstruction: false, andInstruction: false, orInstruction: false, requireInstance: false, "Couldn't find the day speed multiplier field");
			codes.RemoveRange(index - 3, 3);
			codes.Insert(index - 3, new CodeInstruction(OpCodes.Ldc_R4, (object)1f));
			return codes;
		}

		[HarmonyPatch("CalculatePlanetTime")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> CalculatePlanetTimeTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			FieldInfo field = typeof(SelectableLevel).GetField("DaySpeedMultiplier");
			int index = 0;
			List<CodeInstruction> codes = new List<CodeInstruction>(instructions);
			Tools.FindField(ref index, ref codes, field, null, skip: true, notInstruction: false, andInstruction: false, orInstruction: false, requireInstance: false, "Couldn't find the day speed multiplier field");
			codes.RemoveRange(index - 2, 2);
			codes.Insert(index - 2, new CodeInstruction(OpCodes.Ldc_R4, (object)1f));
			return codes;
		}
	}
}
namespace MoonDaySpeedMultiplierPatcher.Misc
{
	internal static class Metadata
	{
		public const string GUID = "com.github.WhiteSpike.MoonDaySpeedMultiplierPatcher";

		public const string NAME = "Moon Day Speed Multiplier Patcher";

		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 MoonDaySpeedMultiplierPatcher.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}