-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
78 lines (65 loc) · 3.6 KB
/
Program.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
using System.IO;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace NutstoreAutoPatch
{
internal class Program
{
public static void Main(string[] args)
{
const string nutstoreLib = "NutstoreLib.dll";
const string nutstoreLibBackup = "NutstoreLib.origin.dll";
const string nutstoreLibTarget = "NutstoreLib.target.dll";
const string directoryutils = "NutstoreLib.Utils.DirectoryUtils";
const string appdataNutstoreDir = "get_APPDATA_NUTSTORE_DIR";
// backup origin
File.Copy(nutstoreLib, nutstoreLibBackup, true);
// Read the assembly
using (var assemblyDefinition = AssemblyDefinition.ReadAssembly(nutstoreLib))
{
var directoryUtil = assemblyDefinition.MainModule.Types.First(t => t.FullName == directoryutils);
var appdataDirMethod = directoryUtil.Methods.First(m => m.Name == appdataNutstoreDir);
// 清除当前指令
appdataDirMethod.Body.Instructions.Clear();
// 获取IL处理器
var ilProcessor = appdataDirMethod.Body.GetILProcessor();
// 构造调用NutstoreLib.Utils.DirectoryUtils.get_NUTSTORE_INSTALL_DIR的指令
var getInstallDirMethod = appdataDirMethod.Module.ImportReference(
appdataDirMethod.DeclaringType.Module.Types
.First(t => t.Name == "DirectoryUtils" && t.Namespace == "NutstoreLib.Utils")
.Methods.First(m => m.Name == "get_NUTSTORE_INSTALL_DIR")
);
ilProcessor.Append(ilProcessor.Create(OpCodes.Call, getInstallDirMethod));
// 构造调用System.IO.DirectoryInfo构造函数的指令
var directoryInfoConstructor = appdataDirMethod.Module.ImportReference(
typeof(System.IO.DirectoryInfo).GetConstructor(new[] { typeof(string) })
);
ilProcessor.Append(ilProcessor.Create(OpCodes.Newobj, directoryInfoConstructor));
// 构造获取DirectoryInfo.Parent属性的调用指令
var getParentMethod = appdataDirMethod.Module.ImportReference(
typeof(System.IO.DirectoryInfo).GetProperty("Parent").GetGetMethod()
);
ilProcessor.Append(ilProcessor.Create(OpCodes.Call, getParentMethod));
// 构造调用System.IO.FileSystemInfo.FullName属性的指令
var getFullNameMethod = appdataDirMethod.Module.ImportReference(
typeof(System.IO.FileSystemInfo).GetProperty("FullName").GetGetMethod()
);
ilProcessor.Append(ilProcessor.Create(OpCodes.Callvirt, getFullNameMethod));
// 加载字符串"UserData"
ilProcessor.Append(ilProcessor.Create(OpCodes.Ldstr, "UserData"));
// 构造调用Pri.LongPath.Path.Combine的指令
var pathCombineMethod = appdataDirMethod.Module.ImportReference(
typeof(System.IO.Path).GetMethod("Combine", new[] { typeof(string), typeof(string) })
);
ilProcessor.Append(ilProcessor.Create(OpCodes.Call, pathCombineMethod));
// 返回指令
ilProcessor.Append(ilProcessor.Create(OpCodes.Ret));
// Write the modified assembly
assemblyDefinition.Write(nutstoreLibTarget);
}
File.Copy(nutstoreLibTarget,nutstoreLib,true);
File.Delete(nutstoreLibTarget);
}
}
}