forked from tModLoader/tModLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHookGenTask.cs
96 lines (81 loc) · 2.77 KB
/
HookGenTask.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
using Mono.Cecil;
using MonoMod;
using MonoMod.RuntimeDetour.HookGen;
using System.IO;
using System.Windows;
using XnaToFna;
using static Terraria.ModLoader.Setup.Program;
namespace Terraria.ModLoader.Setup
{
internal class HookGenTask : SetupOperation
{
public HookGenTask(ITaskInterface taskInterface) : base(taskInterface)
{
}
public override void Run()
{
string targetExePath = @"src\tModLoader\bin\WindowsDebug\net45\Terraria.exe";
if (!File.Exists(targetExePath)) {
var result = MessageBox.Show($"\"{targetExePath}\" does not exist. Use Vanilla exe instead?", "tML exe not found", MessageBoxButton.YesNo);
if (result != MessageBoxResult.Yes) {
taskInterface.SetStatus("Cancelled");
return;
}
if (!File.Exists(TerrariaPath))
throw new FileNotFoundException(TerrariaPath);
targetExePath = TerrariaPath;
}
var outputPath = Path.Combine(referencesDir, "TerrariaHooks.XNA.dll");
if (File.Exists(outputPath))
File.Delete(outputPath);
taskInterface.SetStatus($"Hooking: Terraria.exe -> TerrariaHooks.XNA.dll");
HookGen(targetExePath, outputPath);
taskInterface.SetStatus($"XnaToFna: TerrariaHooks.XNA.dll -> TerrariaHooks.FNA.dll");
var monoPath = Path.Combine(referencesDir, "TerrariaHooks.FNA.dll");
if (File.Exists(monoPath))
File.Delete(monoPath);
File.Copy(outputPath, monoPath);
XnaToFna(monoPath);
File.Delete(Path.ChangeExtension(monoPath, "pdb"));
}
public static void HookGen(string inputPath, string outputPath)
{
using var mm = new MonoModder {
InputPath = inputPath,
OutputPath = outputPath,
ReadingMode = ReadingMode.Deferred,
MissingDependencyThrow = false,
};
mm.Read();
mm.MapDependencies();
mm.DependencyCache["MonoMod.RuntimeDetour"] = ModuleDefinition.ReadModule(Path.Combine(referencesDir, "MonoMod.RuntimeDetour.dll"));
mm.DependencyCache["MonoMod.Utils"] = ModuleDefinition.ReadModule(Path.Combine(referencesDir, "MonoMod.Utils.dll"));
var gen = new HookGenerator(mm, "TerrariaHooks") {
HookPrivate = true,
};
gen.Generate();
RemoveModLoaderTypes(gen.OutputModule);
gen.OutputModule.Write(outputPath);
}
private static void RemoveModLoaderTypes(ModuleDefinition module)
{
for (int i = module.Types.Count - 1; i >= 0; i--)
if (module.Types[i].FullName.Contains("Terraria.ModLoader"))
module.Types.RemoveAt(i);
}
public static void XnaToFna(string inputPath)
{
using var xnaToFnaUtil = new XnaToFnaUtil {
HookCompat = false,
HookHacks = false,
HookEntryPoint = false,
HookBinaryFormatter = false,
HookReflection = false,
AddAssemblyReference = false
};
xnaToFnaUtil.ScanPath(Path.Combine(referencesDir, "FNA.dll"));
xnaToFnaUtil.ScanPath(inputPath);
xnaToFnaUtil.RelinkAll();
}
}
}