#define TRACE 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.Bootstrap; using HarmonyLib; using HarmonyLib.Internal.Patching; using Microsoft.CodeAnalysis; using Mono.Cecil; using Mono.Cecil.Cil; using MonoMod.Utils; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: IgnoresAccessChecksTo("0Harmony")] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: AssemblyCompany("LethalLevelLoader.Patcher")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0+91ce948550bf16502208ab52512cca78b892665c")] [assembly: AssemblyProduct("LethalLevelLoader.Patcher")] [assembly: AssemblyTitle("LethalLevelLoader.Patcher")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/IAmBatby/LethalLevelLoader/")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("1.0.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.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace LethalLevelLoader.Patcher { public class LethalLevelLoaderPatcher { public static IEnumerable TargetDLLs { get; } = Array.Empty(); internal static Harmony Harmony { get; } = new Harmony("imabatby.lethallevelloader.patcher"); public static event Action onChainloaderFinish; private static void Initialize() { Trace.TraceInformation("[LethalLevelLoader.Patcher] Initializing..."); } private static void Finish() { Trace.TraceInformation("[LethalLevelLoader.Patcher] Patching..."); Harmony.PatchAll(typeof(ChainloaderEventPatch)); Harmony.PatchAll(typeof(CursedHarmonyPatch)); Trace.TraceInformation("[LethalLevelLoader.Patcher] Done!"); } private static void ChainloaderFinish() { try { LethalLevelLoaderPatcher.onChainloaderFinish?.Invoke(); LethalLevelLoaderPatcher.onChainloaderFinish = null; } catch (Exception ex) { Trace.TraceError("[LethalLevelLoader.Patcher] Error during Chainloader event invokation: " + ex); } } public static void Patch(AssemblyDefinition _) { } } [HarmonyPatch] internal static class ChainloaderEventPatch { [HarmonyPatch(typeof(Chainloader), "Initialize")] [HarmonyPostfix] [HarmonyPriority(0)] private static void ChainloaderInitialize_Postfix() { //IL_0036: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Expected O, but got Unknown MethodInfo method = typeof(Chainloader).GetMethod("Start", BindingFlags.Static | BindingFlags.Public); MethodInfo method2 = typeof(LethalLevelLoaderPatcher).GetMethod("ChainloaderFinish", BindingFlags.Static | BindingFlags.NonPublic); LethalLevelLoaderPatcher.Harmony.Patch((MethodBase)method, (HarmonyMethod)null, new HarmonyMethod(method2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); } } internal static class CursedHarmonyPatch { private static void ReadInstruction_Prefix(Instruction ins) { //IL_002c: Unknown result type (might be due to invalid IL or missing references) //IL_0036: Expected O, but got Unknown if (ins.Operand is DynamicMethod dynamicMethod) { ModuleDefinition val = ModuleDefinition.ReadModule(typeof(object).Module.FullyQualifiedName); ins.Operand = (object)new DynamicMethodReference(val, (MethodInfo)dynamicMethod); Trace.TraceInformation("[LethalLevelLoader.Patcher] DynamicMethod '" + dynamicMethod.Name + "' replaced!"); } } [HarmonyPatch(typeof(Chainloader), "Initialize")] [HarmonyPostfix] [HarmonyPriority(800)] private static void ChainloaderInitialize_Postfix() { //IL_00ef: Unknown result type (might be due to invalid IL or missing references) //IL_00f6: Expected O, but got Unknown string environmentVariable = Environment.GetEnvironmentVariable("MONOMOD_DMD_TYPE"); if (!string.IsNullOrEmpty(environmentVariable) && !string.Equals(environmentVariable, "dynamicmethod", StringComparison.Ordinal)) { return; } Type type = null; MethodInfo methodInfo = null; Assembly assembly = Assembly.GetAssembly(typeof(ILManipulator)); Type[] types = assembly.GetTypes(); for (int num = types.Length - 1; num >= 0; num--) { type = types[num]; MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic); methodInfo = Array.Find(methods, (MethodInfo method) => method.Name.Contains("ReadInstruction", StringComparison.Ordinal)); if (methodInfo != null) { Trace.TraceInformation("[LethalLevelLoader.Patcher] Found Method '" + type.FullName + "/" + methodInfo.Name + "'!"); break; } } if (!(methodInfo == null)) { HarmonyMethod val = new HarmonyMethod(typeof(CursedHarmonyPatch), "ReadInstruction_Prefix", (Type[])null); LethalLevelLoaderPatcher.Harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); } } } } namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] internal sealed class IgnoresAccessChecksToAttribute : Attribute { public IgnoresAccessChecksToAttribute(string assemblyName) { } } }