Decompiled source of BepInEx MonoMod Debug Patcher v1.1.0

BepInEx/patchers/BepInExMonoModDebug/BepInExMonoModDebug.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
using MonoMod.Cil;
using MonoMod.RuntimeDetour;
using MonoMod.Utils;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyCompany("BepInExMonoModDebug")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+7ded0892a9f2466ef60109f8a8a1173ed6af5dd6")]
[assembly: AssemblyProduct("BepInExMonoModDebug")]
[assembly: AssemblyTitle("BepInExMonoModDebug")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
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;
		}
	}
	[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 BepInExMonoModDebug
{
	public static class BepInExMonoModDebugPatcher
	{
		internal static Configuration Configuration { get; private set; } = null;


		internal static ManualLogSource Logger { get; } = Logger.CreateLogSource("BepInExMonoModDebugPatcher");


		internal static string DumpsDirectory { get; } = Path.Combine(Paths.BepInExRootPath, "dumps");


		internal static Harmony Harmony { get; } = new Harmony("BepInExMonoModDebugPatcher");


		public static IEnumerable<string> TargetDLLs { get; } = Array.Empty<string>();


		private static void Finish()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			Configuration = new Configuration(new ConfigFile(Path.Combine(Paths.ConfigPath, "BepInExMonoModDebugPatcher.cfg"), false));
			if (!Directory.Exists(DumpsDirectory))
			{
				Directory.CreateDirectory(DumpsDirectory);
			}
			else
			{
				DeleteOldDumpFiles();
			}
			Harmony.PatchAll(typeof(BepInExMonoModDebugPatcher).Assembly);
			ILHook.OnDetour = (Func<ILHook, MethodBase, Manipulator, bool>)Delegate.Combine(ILHook.OnDetour, new Func<ILHook, MethodBase, Manipulator, bool>(OnDetour));
			Environment.SetEnvironmentVariable("MONOMOD_DMD_TYPE", typeof(DebugDMDGenerator).FullName);
		}

		private static bool OnDetour(ILHook hook, MethodBase @base, Manipulator manipulator)
		{
			DebugDMDGenerator.ShouldDump = true;
			return true;
		}

		private static void DeleteOldDumpFiles()
		{
			foreach (string item in Directory.EnumerateFiles(DumpsDirectory))
			{
				try
				{
					File.Delete(item);
				}
				catch (Exception arg)
				{
					Logger.LogWarning((object)$"Failed to delete a file {item}\n{arg}");
				}
			}
		}

		public static void Patch(AssemblyDefinition _)
		{
		}
	}
	internal static class CecilEmitter
	{
		public static void Dump(MethodDefinition md, string dumpPath, MethodBase? original = null)
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Expected O, but got Unknown
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Expected O, but got Unknown
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Expected O, but got Unknown
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Expected O, but got Unknown
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0203: Unknown result type (might be due to invalid IL or missing references)
			//IL_0208: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03cc: Expected O, but got Unknown
			//IL_039a: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a1: Expected O, but got Unknown
			MethodDefinition md2 = md;
			string text = SanitizeTypeName(Extensions.GetID((MethodReference)(object)md2, (string)null, (string)null, false, true));
			string text2 = (original?.Name ?? ((MemberReference)md2).Name).Replace('.', '_');
			ModuleDefinition module = ModuleDefinition.CreateModule(text, new ModuleParameters
			{
				Kind = (ModuleKind)0,
				ReflectionImporterProvider = MMReflectionImporter.ProviderNoDefault
			});
			try
			{
				TypeDefinition val = new TypeDefinition("", text2, (TypeAttributes)385)
				{
					BaseType = module.TypeSystem.Object
				};
				module.Types.Add(val);
				MethodDefinition clone = null;
				Relinker val2 = (Relinker)delegate(IMetadataTokenProvider mtp, IGenericParameterProvider ctx)
				{
					if (mtp == md2)
					{
						return (IMetadataTokenProvider)(object)clone;
					}
					MethodReference val10 = (MethodReference)(object)((mtp is MethodReference) ? mtp : null);
					return (IMetadataTokenProvider)((val10 != null && ((MemberReference)val10).FullName == ((MemberReference)md2).FullName && ((MemberReference)((MemberReference)val10).DeclaringType).FullName == ((MemberReference)md2.DeclaringType).FullName && ((MemberReference)val10).DeclaringType.Scope.Name == ((TypeReference)md2.DeclaringType).Scope.Name) ? ((object)clone) : ((object)Extensions.ImportReference(module, mtp)));
				};
				clone = new MethodDefinition(original?.Name ?? ("_" + ((MemberReference)md2).Name.Replace(".", "_")), md2.Attributes, module.TypeSystem.Void)
				{
					MethodReturnType = ((MethodReference)md2).MethodReturnType,
					Attributes = (MethodAttributes)150,
					ImplAttributes = (MethodImplAttributes)0,
					DeclaringType = val,
					HasThis = false,
					ReturnType = Extensions.Relink(((MethodReference)md2).ReturnType, val2, (IGenericParameterProvider)(object)clone)
				};
				val.Methods.Add(clone);
				Enumerator<ParameterDefinition> enumerator = ((MethodReference)md2).Parameters.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						ParameterDefinition current = enumerator.Current;
						((MethodReference)clone).Parameters.Add(Extensions.Relink(Extensions.Clone(current), val2, (IGenericParameterProvider)(object)clone));
					}
				}
				finally
				{
					((IDisposable)enumerator).Dispose();
				}
				MethodBody val4 = (clone.Body = Extensions.Clone(md2.Body, clone));
				MethodBody val5 = val4;
				Enumerator<VariableDefinition> enumerator2 = clone.Body.Variables.GetEnumerator();
				try
				{
					while (enumerator2.MoveNext())
					{
						VariableDefinition current2 = enumerator2.Current;
						((VariableReference)current2).VariableType = Extensions.Relink(((VariableReference)current2).VariableType, val2, (IGenericParameterProvider)(object)clone);
					}
				}
				finally
				{
					((IDisposable)enumerator2).Dispose();
				}
				foreach (ExceptionHandler item in ((IEnumerable<ExceptionHandler>)clone.Body.ExceptionHandlers).Where((ExceptionHandler handler) => handler.CatchType != null))
				{
					item.CatchType = Extensions.Relink(item.CatchType, val2, (IGenericParameterProvider)(object)clone);
				}
				Enumerator<Instruction> enumerator4 = val5.Instructions.GetEnumerator();
				try
				{
					while (enumerator4.MoveNext())
					{
						Instruction current4 = enumerator4.Current;
						object operand = current4.Operand;
						ParameterDefinition val6 = (ParameterDefinition)((operand is ParameterDefinition) ? operand : null);
						object obj;
						if (val6 == null)
						{
							ILLabel val7 = (ILLabel)((operand is ILLabel) ? operand : null);
							if (val7 == null)
							{
								IMetadataTokenProvider val8 = (IMetadataTokenProvider)((operand is IMetadataTokenProvider) ? operand : null);
								obj = ((val8 == null) ? operand : Extensions.Relink(val8, val2, (IGenericParameterProvider)(object)clone));
							}
							else
							{
								obj = val7.Target;
							}
						}
						else
						{
							obj = ((MethodReference)clone).Parameters[((ParameterReference)val6).Index];
						}
						operand = obj;
						current4.Operand = operand;
					}
				}
				finally
				{
					((IDisposable)enumerator4).Dispose();
				}
				if (((MethodReference)md2).HasThis)
				{
					TypeReference val9 = (TypeReference)(object)md2.DeclaringType;
					if (val9.IsValueType)
					{
						val9 = (TypeReference)new ByReferenceType(val9);
					}
					((MethodReference)clone).Parameters.Insert(0, new ParameterDefinition("<>_this", (ParameterAttributes)0, Extensions.Relink(val9, val2, (IGenericParameterProvider)(object)clone)));
				}
				if (!Directory.Exists(dumpPath))
				{
					Directory.CreateDirectory(dumpPath);
				}
				using FileStream fileStream = new FileStream(Path.Combine(dumpPath, ((ModuleReference)module).Name + ".dll"), FileMode.Create, FileAccess.Write);
				module.Write((Stream)fileStream);
			}
			finally
			{
				if (module != null)
				{
					((IDisposable)module).Dispose();
				}
			}
		}

		private static string SanitizeTypeName(string typeName)
		{
			return new StringBuilder(typeName).Replace(":", "_").Replace(" ", "_").Replace("<", "{")
				.Replace(">", "}")
				.ToString();
		}
	}
	internal class Configuration
	{
		public ConfigEntry<bool> ShouldSaveDumpFile { get; }

		public Configuration(ConfigFile configFile)
		{
			ShouldSaveDumpFile = configFile.Bind<bool>("Dumps", "Save", false, "Should save patched assemblies in 'BepInEx/dumps' directory\nThis setting is for developers!");
			configFile.Save();
		}
	}
	internal sealed class DebugDMDGenerator : DMDGenerator<DebugDMDGenerator>
	{
		internal static bool ShouldDump { get; set; }

		protected override MethodInfo _Generate(DynamicMethodDefinition dmd, object context)
		{
			if (BepInExMonoModDebugPatcher.Configuration.ShouldSaveDumpFile.Value)
			{
				MethodInfo result = DMDGenerator<DMDEmitMethodBuilderGenerator>.Generate(dmd, context);
				if (ShouldDump)
				{
					ShouldDump = false;
					DumpMethodToFile(dmd);
				}
				return result;
			}
			ShouldDump = false;
			return DMDGenerator<DMDEmitMethodBuilderGenerator>.Generate(dmd, context);
		}

		private static void DumpMethodToFile(DynamicMethodDefinition dmd)
		{
			if (!(dmd.OriginalMethod == null))
			{
				CecilEmitter.Dump(dmd.Definition, BepInExMonoModDebugPatcher.DumpsDirectory, dmd.OriginalMethod);
			}
		}
	}
}
namespace BepInExMonoModDebug.Patches
{
	[HarmonyPatch(typeof(StackTrace))]
	internal static class Patch_StackTrace
	{
		[HarmonyPatch("AddFrames")]
		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> AddFrameWithIL(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Expected O, but got Unknown
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Expected O, but got Unknown
			CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
			MethodInfo method = AccessTools.Method(typeof(StackFrame), "GetFileLineNumber", (Type[])null, (Type[])null);
			val.SearchForward((Func<CodeInstruction, bool>)((CodeInstruction c) => CodeInstructionExtensions.Calls(c, method))).ThrowIfInvalid("Failed to find call GetFileLineNumber").InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1]
			{
				new CodeInstruction(OpCodes.Dup, (object)null)
			})
				.Advance(1)
				.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[2]
				{
					new CodeInstruction(OpCodes.Nop, (object)null),
					new CodeInstruction(OpCodes.Nop, (object)null)
				})
				.SetInstruction(new CodeInstruction(OpCodes.Pop, (object)null))
				.Advance(1)
				.Insert((CodeInstruction[])(object)new CodeInstruction[1]
				{
					new CodeInstruction(OpCodes.Call, (object)new Func<StackFrame, string>(GetFileLineOrILOffset).Method)
				});
			return val.InstructionEnumeration();
		}

		internal static string GetFileLineOrILOffset(StackFrame frame)
		{
			int fileLineNumber = frame.GetFileLineNumber();
			if (fileLineNumber > 0)
			{
				return fileLineNumber.ToString();
			}
			return "IL_" + frame.GetILOffset().ToString("X4");
		}
	}
}