using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; using BepInEx.Logging; using Mono.Cecil; using MonoMod; using MonoMod.RuntimeDetour.HookGen; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)] [assembly: AssemblyTitle("Bepinex.Monomod.HookGenPatcher")] [assembly: AssemblyDescription("Runtime HookGen for BepInEx")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("HarbingerOfMe")] [assembly: AssemblyProduct("Bepinex.Monomod.HookGenPatcher")] [assembly: AssemblyCopyright("HarbingerOfMe-2022")] [assembly: AssemblyTrademark("MIT")] [assembly: ComVisible(false)] [assembly: Guid("12032e45-9577-4195-8f4f-a729911b2f08")] [assembly: AssemblyFileVersion("1.2.1.0")] [assembly: NeutralResourcesLanguage("en")] [assembly: AssemblyVersion("1.2.1.0")] namespace BepInEx.MonoMod.HookGenPatcher; public static class HookGenPatcher { public const string Version = "3"; internal static ManualLogSource Logger = Logger.CreateLogSource("HookGenPatcher"); private static string AssemblyNamesToHookGenPatch = "JoelG.ENA4.dll,LMirman.RewiredGlyphs.dll,LMirman.Utilities.dll,LMirman.VespaIO.dll,LMirman.VespaIO.LLAPI.dll"; private const char EntrySeparator = ','; public static IEnumerable TargetDLLs { get; } = new string[0]; public static void Initialize() { //IL_01df: Unknown result type (might be due to invalid IL or missing references) //IL_01e4: Unknown result type (might be due to invalid IL or missing references) //IL_01ec: Unknown result type (might be due to invalid IL or missing references) //IL_01f4: Unknown result type (might be due to invalid IL or missing references) //IL_01f6: Unknown result type (might be due to invalid IL or missing references) //IL_01fd: Expected O, but got Unknown //IL_0272: Unknown result type (might be due to invalid IL or missing references) //IL_0279: Expected O, but got Unknown //IL_02a9: Unknown result type (might be due to invalid IL or missing references) //IL_02b3: Expected O, but got Unknown //IL_02e4: Unknown result type (might be due to invalid IL or missing references) //IL_02ee: Expected O, but got Unknown Logger.LogInfo((object)"HookGenPatcher v3"); string[] array = AssemblyNamesToHookGenPatch.Split(new char[1] { ',' }); string text = Path.Combine(Paths.PluginPath, "MMHOOK"); string[] array2 = array; foreach (string text2 in array2) { string text3 = "MMHOOK_" + text2; string text4 = Path.Combine(Paths.ManagedPath, text2); string text5 = Path.Combine(text, text3); bool flag = true; string[] files = Directory.GetFiles(Paths.PluginPath, text3, SearchOption.AllDirectories); foreach (string text6 in files) { if (Path.GetFileName(text6).Equals(text3)) { text5 = text6; Logger.LogInfo((object)"Previous MMHOOK location found. Using that location to save instead."); flag = false; break; } } if (flag) { Directory.CreateDirectory(text); } FileInfo fileInfo = new FileInfo(text4); string text7 = null; if (File.Exists(text5)) { try { AssemblyDefinition val = AssemblyDefinition.ReadAssembly(text5); try { if (val.MainModule.GetType("BepHookGen.version3") != null) { text7 = fileInfo.MakeHash(); if (val.MainModule.GetType("BepHookGen.hash" + text7) != null) { Logger.LogInfo((object)"Already ran for this version, reusing that file."); continue; } } } finally { ((IDisposable)val)?.Dispose(); } } catch (Exception arg) { Logger.LogWarning((object)$"Failed to read {Path.GetFileName(text5)}, probably corrupted, remaking one. {arg}"); } } Environment.SetEnvironmentVariable("MONOMOD_HOOKGEN_PRIVATE", "1"); Environment.SetEnvironmentVariable("MONOMOD_HOOKGEN_NO_VISIBLE_CHECK", "1"); Environment.SetEnvironmentVariable("MONOMOD_HOOKGEN_NO_VISIBLE_CHECK_BACKCOMPAT_SUFFIX", "1"); Environment.SetEnvironmentVariable("MONOMOD_DEPENDENCY_MISSING_THROW", "0"); MonoModder val2 = new MonoModder { InputPath = text4, OutputPath = text5, ReadingMode = (ReadingMode)2 }; try { IAssemblyResolver assemblyResolver = val2.AssemblyResolver; IAssemblyResolver obj = ((assemblyResolver is BaseAssemblyResolver) ? assemblyResolver : null); if (obj != null) { ((BaseAssemblyResolver)obj).AddSearchDirectory(Paths.BepInExAssemblyDirectory); } val2.Read(); val2.MapDependencies(); if (File.Exists(text5)) { Logger.LogDebug((object)("Clearing " + text5)); File.Delete(text5); } Logger.LogInfo((object)"Starting HookGenerator"); HookGenerator val3 = new HookGenerator(val2, Path.GetFileName(text5)); ModuleDefinition outputModule = val3.OutputModule; try { val3.Generate(); outputModule.Types.Add(new TypeDefinition("BepHookGen", "version3", (TypeAttributes)1, outputModule.TypeSystem.Object)); outputModule.Types.Add(new TypeDefinition("BepHookGen", "hash" + (text7 ?? fileInfo.MakeHash()), (TypeAttributes)1, outputModule.TypeSystem.Object)); outputModule.Write(text5); } finally { ((IDisposable)outputModule)?.Dispose(); } Logger.LogInfo((object)"Done."); } finally { ((IDisposable)val2)?.Dispose(); } } } public static void Patch(AssemblyDefinition _) { } public static string MakeHash(this FileInfo fileInfo) { using MD5 mD = new MD5CryptoServiceProvider(); using FileStream inputStream = fileInfo.OpenRead(); byte[] array = mD.ComputeHash(inputStream); StringBuilder stringBuilder = new StringBuilder(); byte[] array2 = array; foreach (byte b in array2) { stringBuilder.Append(b.ToString("X2")); } return stringBuilder.ToString(); } }