From 60bab106c4acd44c47d4d24ef02573cac11ae680 Mon Sep 17 00:00:00 2001 From: xiaoxiao921 Date: Sat, 27 Feb 2021 15:01:16 +0100 Subject: [PATCH 1/6] Update package refs --- BepInEx.MonoMod.HookGenPatcher.csproj | 53 +++++++++++---------------- packages.config | 10 ++--- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/BepInEx.MonoMod.HookGenPatcher.csproj b/BepInEx.MonoMod.HookGenPatcher.csproj index 3ef9d6b..a04b1df 100644 --- a/BepInEx.MonoMod.HookGenPatcher.csproj +++ b/BepInEx.MonoMod.HookGenPatcher.csproj @@ -10,6 +10,7 @@ BepInEx.MonoMod.HookGenPatcher BepInEx.MonoMod.HookGenPatcher v4.7.2 + preview 512 true @@ -35,54 +36,42 @@ - ..\libs\BepInEx.dll - False + libs\BepInEx.dll - ..\libs\BepInEx.Preloader.dll - False + libs\BepInEx.Preloader.dll - - packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.dll - False + + packages\Mono.Cecil.0.11.3\lib\net40\Mono.Cecil.dll - - packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Mdb.dll - False + + packages\Mono.Cecil.0.11.3\lib\net40\Mono.Cecil.Mdb.dll - - packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Pdb.dll - False + + packages\Mono.Cecil.0.11.3\lib\net40\Mono.Cecil.Pdb.dll - - packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Rocks.dll - False + + packages\Mono.Cecil.0.11.3\lib\net40\Mono.Cecil.Rocks.dll - - packages\MonoMod.20.5.21.5\lib\net40\MonoMod.exe - True + + packages\MonoMod.21.1.11.1\lib\net40\MonoMod.exe - - packages\MonoMod.RuntimeDetour.20.5.21.5\lib\net40\MonoMod.RuntimeDetour.dll - False + + packages\MonoMod.RuntimeDetour.21.1.11.1\lib\net40\MonoMod.RuntimeDetour.dll - - packages\MonoMod.RuntimeDetour.HookGen.20.5.21.5\lib\net40\MonoMod.RuntimeDetour.HookGen.exe - True + + packages\MonoMod.RuntimeDetour.HookGen.21.1.11.1\lib\net40\MonoMod.RuntimeDetour.HookGen.exe - - packages\MonoMod.Utils.20.5.21.5\lib\net40\MonoMod.Utils.dll - False + + packages\MonoMod.Utils.21.1.11.1\lib\net40\MonoMod.Utils.dll - ..\libs\UnityEngine.dll - False + libs\UnityEngine.dll - ..\libs\UnityEngine.CoreModule.dll - False + libs\UnityEngine.CoreModule.dll diff --git a/packages.config b/packages.config index 0468f46..0ecf2cc 100644 --- a/packages.config +++ b/packages.config @@ -1,8 +1,8 @@  - - - - - + + + + + \ No newline at end of file From 2b3aa64531ab67c5c2a1406bf6b6d9cb47aef2a8 Mon Sep 17 00:00:00 2001 From: xiaoxiao921 Date: Sat, 27 Feb 2021 15:01:28 +0100 Subject: [PATCH 2/6] Allow for custom assembly names --- HookGenPatcher.cs | 107 +++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 45 deletions(-) diff --git a/HookGenPatcher.cs b/HookGenPatcher.cs index ee96a48..d67713c 100644 --- a/HookGenPatcher.cs +++ b/HookGenPatcher.cs @@ -1,4 +1,5 @@ -using Mono.Cecil; +using BepInEx.Configuration; +using Mono.Cecil; using MonoMod; using MonoMod.RuntimeDetour.HookGen; using System; @@ -7,11 +8,20 @@ namespace BepInEx.MonoMod.HookGenPatcher { - public static class HookGenPatcher { internal static Logging.ManualLogSource Logger = Logging.Logger.CreateLogSource("HookGenPatcher"); + private const string CONFIG_FILE_NAME = "HookGenPatcher.cfg"; + + private static readonly ConfigFile Config = + new(Path.Combine(Paths.ConfigPath, CONFIG_FILE_NAME), true); + + private const char EntrySeparator = ','; + + private static readonly ConfigEntry AssemblyNamesToHookGenPatch = Config.Bind("General", "MMHOOKAssemblyNames", + "Assembly-CSharp.dll", $"Assembly names to make mmhooks for, separate entries with : ${EntrySeparator} "); + public static IEnumerable TargetDLLs { get; } = new string[] { }; /** @@ -19,68 +29,75 @@ public static class HookGenPatcher */ public static void Initialize() { - string pathIn = Path.Combine(Paths.ManagedPath, "Assembly-CSharp.dll"); - string pathOut = Path.Combine(Paths.PluginPath, "MMHOOK_Assembly-CSharp.dll"); + var assemblyNames = AssemblyNamesToHookGenPatch.Value.Split(EntrySeparator); - foreach(string mmhookFile in Directory.EnumerateFiles(Paths.PluginPath, "MMHOOK_Assembly-CSharp.dll",SearchOption.AllDirectories)) + foreach (var customAssemblyName in assemblyNames) { - if (Path.GetFileName(mmhookFile).Equals("MMHOOK_Assembly-CSharp.dll")) + var mmhookFileName = "MMHOOK_" + customAssemblyName; + + string pathIn = Path.Combine(Paths.ManagedPath, customAssemblyName); + string pathOut = Path.Combine(Paths.PluginPath, mmhookFileName); + + foreach (string mmhookFile in Directory.EnumerateFiles(Paths.PluginPath, mmhookFileName, SearchOption.AllDirectories)) { - pathOut = mmhookFile; - Logger.LogInfo("Previous MMHOOK location found. Using that location to save instead."); - break; + if (Path.GetFileName(mmhookFile).Equals(mmhookFileName)) + { + pathOut = mmhookFile; + Logger.LogInfo("Previous MMHOOK location found. Using that location to save instead."); + break; + } } - } - var size = new FileInfo(pathIn).Length; + var size = new FileInfo(pathIn).Length; - if (File.Exists(pathOut)) - { - using(var oldMM = AssemblyDefinition.ReadAssembly(pathOut)) + if (File.Exists(pathOut)) { - bool hash = oldMM.MainModule.GetType("BepHookGen.hash" + size) != null; - if (hash) + using (var oldMM = AssemblyDefinition.ReadAssembly(pathOut)) { - Logger.LogInfo("Already ran for this version, reusing that file."); - return; + bool hash = oldMM.MainModule.GetType("BepHookGen.hash" + size) != null; + if (hash) + { + Logger.LogInfo("Already ran for this version, reusing that file."); + return; + } } } - } - Environment.SetEnvironmentVariable("MONOMOD_HOOKGEN_PRIVATE", "1"); - Environment.SetEnvironmentVariable("MONOMOD_DEPENDENCY_MISSING_THROW", "0"); + Environment.SetEnvironmentVariable("MONOMOD_HOOKGEN_PRIVATE", "1"); + Environment.SetEnvironmentVariable("MONOMOD_DEPENDENCY_MISSING_THROW", "0"); - using (MonoModder mm = new MonoModder() - { - InputPath = pathIn, - OutputPath = pathOut, - ReadingMode = ReadingMode.Deferred - }) - { + using (MonoModder mm = new MonoModder() + { + InputPath = pathIn, + OutputPath = pathOut, + ReadingMode = ReadingMode.Deferred + }) + { - (mm.AssemblyResolver as BaseAssemblyResolver)?.AddSearchDirectory(Paths.BepInExAssemblyDirectory); + (mm.AssemblyResolver as BaseAssemblyResolver)?.AddSearchDirectory(Paths.BepInExAssemblyDirectory); - mm.Read(); + mm.Read(); - mm.MapDependencies(); + mm.MapDependencies(); - if (File.Exists(pathOut)) - { - Logger.LogDebug($"Clearing {pathOut}"); - File.Delete(pathOut); - } + if (File.Exists(pathOut)) + { + Logger.LogDebug($"Clearing {pathOut}"); + File.Delete(pathOut); + } - Logger.LogInfo("Starting HookGenerator"); - HookGenerator gen = new HookGenerator(mm, Path.GetFileName(pathOut)); + Logger.LogInfo("Starting HookGenerator"); + HookGenerator gen = new HookGenerator(mm, Path.GetFileName(pathOut)); - using (ModuleDefinition mOut = gen.OutputModule) - { - gen.Generate(); - mOut.Types.Add(new TypeDefinition("BepHookGen", "hash" + size, TypeAttributes.AutoClass)); - mOut.Write(pathOut); - } + using (ModuleDefinition mOut = gen.OutputModule) + { + gen.Generate(); + mOut.Types.Add(new TypeDefinition("BepHookGen", "hash" + size, Mono.Cecil.TypeAttributes.AutoClass)); + mOut.Write(pathOut); + } - Logger.LogInfo("Done."); + Logger.LogInfo("Done."); + } } } From dfcf0d199fcd2dc6e78123576518848feacc21de Mon Sep 17 00:00:00 2001 From: xiaoxiao921 Date: Sat, 27 Feb 2021 15:52:50 +0100 Subject: [PATCH 3/6] create folder for cleaner organization fix bug where we would skip other assemblies if one was already patched before --- HookGenPatcher.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/HookGenPatcher.cs b/HookGenPatcher.cs index d67713c..0289d8b 100644 --- a/HookGenPatcher.cs +++ b/HookGenPatcher.cs @@ -31,12 +31,15 @@ public static void Initialize() { var assemblyNames = AssemblyNamesToHookGenPatch.Value.Split(EntrySeparator); + var mmhookFolder = Path.Combine(Paths.PluginPath, "MMHOOK"); + Directory.CreateDirectory(mmhookFolder); + foreach (var customAssemblyName in assemblyNames) { var mmhookFileName = "MMHOOK_" + customAssemblyName; string pathIn = Path.Combine(Paths.ManagedPath, customAssemblyName); - string pathOut = Path.Combine(Paths.PluginPath, mmhookFileName); + string pathOut = Path.Combine(mmhookFolder, mmhookFileName); foreach (string mmhookFile in Directory.EnumerateFiles(Paths.PluginPath, mmhookFileName, SearchOption.AllDirectories)) { @@ -58,7 +61,7 @@ public static void Initialize() if (hash) { Logger.LogInfo("Already ran for this version, reusing that file."); - return; + continue; } } } From 2c6b7f96fc9d059ce77600dda575197d5ea3f09c Mon Sep 17 00:00:00 2001 From: xiaoxiao921 Date: Sat, 27 Feb 2021 15:59:12 +0100 Subject: [PATCH 4/6] typo --- HookGenPatcher.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HookGenPatcher.cs b/HookGenPatcher.cs index 0289d8b..861b15a 100644 --- a/HookGenPatcher.cs +++ b/HookGenPatcher.cs @@ -20,7 +20,7 @@ public static class HookGenPatcher private const char EntrySeparator = ','; private static readonly ConfigEntry AssemblyNamesToHookGenPatch = Config.Bind("General", "MMHOOKAssemblyNames", - "Assembly-CSharp.dll", $"Assembly names to make mmhooks for, separate entries with : ${EntrySeparator} "); + "Assembly-CSharp.dll", $"Assembly names to make mmhooks for, separate entries with : {EntrySeparator} "); public static IEnumerable TargetDLLs { get; } = new string[] { }; From a455b1c58972c85edca3cd4a491fdac4495b9417 Mon Sep 17 00:00:00 2001 From: HarbingerOfMe Date: Sat, 27 Feb 2021 16:28:18 +0100 Subject: [PATCH 5/6] Revert project file changes --- BepInEx.MonoMod.HookGenPatcher.csproj | 53 ++++++++++++++++----------- packages.config | 10 ++--- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/BepInEx.MonoMod.HookGenPatcher.csproj b/BepInEx.MonoMod.HookGenPatcher.csproj index a04b1df..3ef9d6b 100644 --- a/BepInEx.MonoMod.HookGenPatcher.csproj +++ b/BepInEx.MonoMod.HookGenPatcher.csproj @@ -10,7 +10,6 @@ BepInEx.MonoMod.HookGenPatcher BepInEx.MonoMod.HookGenPatcher v4.7.2 - preview 512 true @@ -36,42 +35,54 @@ - libs\BepInEx.dll + ..\libs\BepInEx.dll + False - libs\BepInEx.Preloader.dll + ..\libs\BepInEx.Preloader.dll + False - - packages\Mono.Cecil.0.11.3\lib\net40\Mono.Cecil.dll + + packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.dll + False - - packages\Mono.Cecil.0.11.3\lib\net40\Mono.Cecil.Mdb.dll + + packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Mdb.dll + False - - packages\Mono.Cecil.0.11.3\lib\net40\Mono.Cecil.Pdb.dll + + packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Pdb.dll + False - - packages\Mono.Cecil.0.11.3\lib\net40\Mono.Cecil.Rocks.dll + + packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Rocks.dll + False - - packages\MonoMod.21.1.11.1\lib\net40\MonoMod.exe + + packages\MonoMod.20.5.21.5\lib\net40\MonoMod.exe + True - - packages\MonoMod.RuntimeDetour.21.1.11.1\lib\net40\MonoMod.RuntimeDetour.dll + + packages\MonoMod.RuntimeDetour.20.5.21.5\lib\net40\MonoMod.RuntimeDetour.dll + False - - packages\MonoMod.RuntimeDetour.HookGen.21.1.11.1\lib\net40\MonoMod.RuntimeDetour.HookGen.exe + + packages\MonoMod.RuntimeDetour.HookGen.20.5.21.5\lib\net40\MonoMod.RuntimeDetour.HookGen.exe + True - - packages\MonoMod.Utils.21.1.11.1\lib\net40\MonoMod.Utils.dll + + packages\MonoMod.Utils.20.5.21.5\lib\net40\MonoMod.Utils.dll + False - libs\UnityEngine.dll + ..\libs\UnityEngine.dll + False - libs\UnityEngine.CoreModule.dll + ..\libs\UnityEngine.CoreModule.dll + False diff --git a/packages.config b/packages.config index 0ecf2cc..0468f46 100644 --- a/packages.config +++ b/packages.config @@ -1,8 +1,8 @@  - - - - - + + + + + \ No newline at end of file From a328d669cbc130c519951da5f419f490fc4fa737 Mon Sep 17 00:00:00 2001 From: HarbingerOfMe Date: Sat, 27 Feb 2021 16:29:01 +0100 Subject: [PATCH 6/6] Only create directory if needed and code cleanup --- HookGenPatcher.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/HookGenPatcher.cs b/HookGenPatcher.cs index 861b15a..835a31d 100644 --- a/HookGenPatcher.cs +++ b/HookGenPatcher.cs @@ -14,8 +14,7 @@ public static class HookGenPatcher private const string CONFIG_FILE_NAME = "HookGenPatcher.cfg"; - private static readonly ConfigFile Config = - new(Path.Combine(Paths.ConfigPath, CONFIG_FILE_NAME), true); + private static readonly ConfigFile Config = new ConfigFile(Path.Combine(Paths.ConfigPath, CONFIG_FILE_NAME), true); private const char EntrySeparator = ','; @@ -32,7 +31,7 @@ public static void Initialize() var assemblyNames = AssemblyNamesToHookGenPatch.Value.Split(EntrySeparator); var mmhookFolder = Path.Combine(Paths.PluginPath, "MMHOOK"); - Directory.CreateDirectory(mmhookFolder); + foreach (var customAssemblyName in assemblyNames) { @@ -40,6 +39,7 @@ public static void Initialize() string pathIn = Path.Combine(Paths.ManagedPath, customAssemblyName); string pathOut = Path.Combine(mmhookFolder, mmhookFileName); + bool shouldCreateDirectory = true; foreach (string mmhookFile in Directory.EnumerateFiles(Paths.PluginPath, mmhookFileName, SearchOption.AllDirectories)) { @@ -47,10 +47,15 @@ public static void Initialize() { pathOut = mmhookFile; Logger.LogInfo("Previous MMHOOK location found. Using that location to save instead."); + shouldCreateDirectory = false; break; } } + if(shouldCreateDirectory) + { + Directory.CreateDirectory(mmhookFolder); + } var size = new FileInfo(pathIn).Length; if (File.Exists(pathOut)) @@ -95,7 +100,7 @@ public static void Initialize() using (ModuleDefinition mOut = gen.OutputModule) { gen.Generate(); - mOut.Types.Add(new TypeDefinition("BepHookGen", "hash" + size, Mono.Cecil.TypeAttributes.AutoClass)); + mOut.Types.Add(new TypeDefinition("BepHookGen", "hash" + size, TypeAttributes.AutoClass)); mOut.Write(pathOut); }