-
Notifications
You must be signed in to change notification settings - Fork 11
/
HookGenPatcher.cs
155 lines (129 loc) · 6.25 KB
/
HookGenPatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using BepInEx.Configuration;
using Mono.Cecil;
using MonoMod;
using MonoMod.RuntimeDetour.HookGen;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
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} ");
private static readonly ConfigEntry<bool> preciseHash = Config.Bind<bool>("General", "Preciser filehashing", false, "Hash file using contents instead of size. Minor perfomance impact.");
private static bool skipHashing => !preciseHash.Value;
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()
{
var assemblyNames = AssemblyNamesToHookGenPatch.Value.Split(EntrySeparator);
var mmhookFolder = Path.Combine(Paths.PluginPath, "MMHOOK");
foreach (var customAssemblyName in assemblyNames)
{
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.GetFiles(Paths.PluginPath, mmhookFileName, SearchOption.AllDirectories))
{
if (Path.GetFileName(mmhookFile).Equals(mmhookFileName))
{
pathOut = mmhookFile;
Logger.LogInfo("Previous MMHOOK location found. Using that location to save instead.");
shouldCreateDirectory = false;
break;
}
}
if (shouldCreateDirectory)
{
Directory.CreateDirectory(mmhookFolder);
}
var fileInfo = new FileInfo(pathIn);
var size = fileInfo.Length;
long hash = 0;
if (File.Exists(pathOut))
{
try
{
using (var oldMM = AssemblyDefinition.ReadAssembly(pathOut))
{
bool mmSizeHash = oldMM.MainModule.GetType("BepHookGen.size" + size) != null;
if (mmSizeHash)
{
if (skipHashing)
{
Logger.LogInfo("Already ran for this version, reusing that file.");
continue;
}
hash = fileInfo.makeHash();
bool mmContentHash = oldMM.MainModule.GetType("BepHookGen.content" + hash) != null;
if (mmContentHash)
{
Logger.LogInfo("Already ran for this version, reusing that file.");
continue;
}
}
}
}
catch (BadImageFormatException)
{
Logger.LogWarning($"Failed to read {Path.GetFileName(pathOut)}, probably corrupted, remaking one.");
}
}
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
})
{
(mm.AssemblyResolver as BaseAssemblyResolver)?.AddSearchDirectory(Paths.BepInExAssemblyDirectory);
mm.Read();
mm.MapDependencies();
if (File.Exists(pathOut))
{
Logger.LogDebug($"Clearing {pathOut}");
File.Delete(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", "size" + size, TypeAttributes.Class | TypeAttributes.Public, mOut.TypeSystem.Object));
if (!skipHashing)
{
mOut.Types.Add(new TypeDefinition("BepHookGen", "content" + (hash == 0 ? fileInfo.makeHash() : hash), TypeAttributes.Class | TypeAttributes.Public, mOut.TypeSystem.Object));
}
mOut.Write(pathOut);
}
Logger.LogInfo("Done.");
}
}
}
public static void Patch(AssemblyDefinition _)
{
}
private static long makeHash(this FileInfo fileInfo)
{
var fileStream = fileInfo.OpenRead();
byte[] hashbuffer = null;
using (MD5 md5 = new MD5CryptoServiceProvider())
{
hashbuffer = md5.ComputeHash(fileStream);
}
long hash = BitConverter.ToInt64(hashbuffer, 0);
return hash != 0 ? hash : 1;
}
}
}