Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for custom assembly names #1

Merged
merged 6 commits into from
Feb 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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