using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInExMonoModDebug.Patches;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using MonoMod.Cil;
using MonoMod.RuntimeDetour;
using MonoMod.Utils;
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("BepInExMonoModDebug")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+82d030892dd4475f0b186cfd22cb15e8dbacc7aa")]
[assembly: AssemblyProduct("BepInExMonoModDebug")]
[assembly: AssemblyTitle("BepInExMonoModDebug")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.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();
}
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);
Harmony.PatchAll(typeof(Patch_Chainloader));
}
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 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 (!ShouldDump || dmd.OriginalMethod == null)
{
ToggleDump(toggle: false);
return DMDGenerator<DMDCecilGenerator>.Generate(dmd, context);
}
ShouldDump = false;
ToggleDump(toggle: true);
MethodInfo result;
try
{
result = DMDGenerator<DMDCecilGenerator>.Generate(dmd, context);
}
catch (Exception ex)
{
BepInExMonoModDebugPatcher.Logger.LogWarning((object)("Failed to generate debug file\n" + ex));
ToggleDump(toggle: false);
return DMDGenerator<DMDEmitDynamicMethodGenerator>.Generate(dmd, context);
}
DumpMethodToFile(dmd);
return result;
}
private static void DumpMethodToFile(DynamicMethodDefinition dmd)
{
if (!BepInExMonoModDebugPatcher.Configuration.ShouldSaveDumpFile.Value)
{
return;
}
try
{
string path = GetFullMethodName(dmd.OriginalMethod) + ".dll";
string path2 = dmd.GetDumpName("Cecil") + ".dll";
string text = Path.Combine(BepInExMonoModDebugPatcher.DumpsDirectory, path);
if (text != null)
{
File.Delete(text);
}
string sourceFileName = Path.Combine(BepInExMonoModDebugPatcher.DumpsDirectory, path2);
File.Move(sourceFileName, text);
}
catch (Exception ex)
{
BepInExMonoModDebugPatcher.Logger.LogError((object)("Failed to create dump file: " + ex));
}
}
private static string GetFullMethodName(MethodBase @base)
{
StringBuilder stringBuilder = new StringBuilder(256);
Type declaringType = @base.DeclaringType;
if (declaringType != null)
{
string @namespace = declaringType.Namespace;
if (!string.IsNullOrEmpty(@namespace))
{
stringBuilder.Append(@namespace).Append('.');
}
stringBuilder.Append(declaringType.Name);
}
if (stringBuilder.Length > 0)
{
stringBuilder.Append('_');
}
stringBuilder.Append(@base.Name);
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
foreach (char oldChar in invalidFileNameChars)
{
stringBuilder.Replace(oldChar, '_');
}
return stringBuilder.ToString();
}
private void ToggleDump(bool toggle)
{
if (BepInExMonoModDebugPatcher.Configuration.ShouldSaveDumpFile.Value)
{
Environment.SetEnvironmentVariable("MONOMOD_DMD_DUMP", toggle ? BepInExMonoModDebugPatcher.DumpsDirectory : null);
}
}
}
}
namespace BepInExMonoModDebug.Patches
{
internal static class Patch_Chainloader
{
[HarmonyPatch(typeof(Chainloader), "Initialize")]
[HarmonyPostfix]
public static void ChainloaderInitialized()
{
try
{
BepInExMonoModDebugPatcher.Harmony.PatchAll(typeof(Patch_Chainloader).Assembly);
}
catch (Exception ex)
{
BepInExMonoModDebugPatcher.Logger.LogWarning((object)("Failed to patch, probably can be ignored\n" + ex));
}
}
}
[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>(Patch_StackTraceUtilities.GetFileLineOrILOffset).Method)
});
return val.InstructionEnumeration();
}
}
[HarmonyPatch]
internal class Patch_StackTraceUtilities
{
[HarmonyPatch(typeof(StackTraceUtility), "ExtractFormattedStackTrace")]
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> ShowILOffset(IEnumerable<CodeInstruction> instructions)
{
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
//IL_000e: Expected O, but got Unknown
CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
MethodInfo fileLineNumberMethod = AccessTools.Method(typeof(StackFrame), "GetFileLineNumber", (Type[])null, (Type[])null);
val.SearchForward((Func<CodeInstruction, bool>)((CodeInstruction c) => CodeInstructionExtensions.Calls(c, fileLineNumberMethod))).ThrowIfInvalid("Failed to find call GetFileLineNumber").Set(OpCodes.Call, (object)new Func<StackFrame, string>(GetFileLineOrILOffset).Method);
val.Advance(1);
val.RemoveInstructions(3);
return val.InstructionEnumeration();
}
internal static string GetFileLineOrILOffset(StackFrame frame)
{
int fileLineNumber = frame.GetFileLineNumber();
if (fileLineNumber > 0)
{
return fileLineNumber.ToString();
}
return "IL_" + frame.GetILOffset().ToString("x5");
}
}
}