Skip to content

Commit

Permalink
Merge pull request #1 from xiaoxiao921/master
Browse files Browse the repository at this point in the history
Allow for custom assembly names
  • Loading branch information
harbingerofme committed Feb 27, 2021
2 parents bc3d3b3 + a328d66 commit 3b3861b
Showing 1 changed file with 70 additions and 45 deletions.
115 changes: 70 additions & 45 deletions HookGenPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mono.Cecil;
using BepInEx.Configuration;
using Mono.Cecil;
using MonoMod;
using MonoMod.RuntimeDetour.HookGen;
using System;
Expand All @@ -7,80 +8,104 @@

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 ConfigFile(Path.Combine(Paths.ConfigPath, CONFIG_FILE_NAME), true);

private const char EntrySeparator = ',';

private static readonly ConfigEntry<string> AssemblyNamesToHookGenPatch = Config.Bind("General", "MMHOOKAssemblyNames",
"Assembly-CSharp.dll", $"Assembly names to make mmhooks for, separate entries with : {EntrySeparator} ");

public static IEnumerable<string> TargetDLLs { get; } = new string[] { };

/**
* Code largely based on https://github.com/MonoMod/MonoMod/blob/master/MonoMod.RuntimeDetour.HookGen/Program.cs
*/
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);

var mmhookFolder = Path.Combine(Paths.PluginPath, "MMHOOK");

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(mmhookFolder, mmhookFileName);
bool shouldCreateDirectory = true;

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.");
shouldCreateDirectory = false;
break;
}
}
}

var size = new FileInfo(pathIn).Length;
if(shouldCreateDirectory)
{
Directory.CreateDirectory(mmhookFolder);
}
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.");
continue;
}
}
}
}

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, TypeAttributes.AutoClass));
mOut.Write(pathOut);
}

Logger.LogInfo("Done.");
Logger.LogInfo("Done.");
}
}
}

Expand Down

0 comments on commit 3b3861b

Please sign in to comment.