-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathScript.cs
More file actions
152 lines (143 loc) · 6.17 KB
/
Copy pathScript.cs
File metadata and controls
152 lines (143 loc) · 6.17 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using dnlib.DotNet.Emit;
using Newtonsoft.Json.Linq;
namespace dnpatch.script
{
public class Script
{
private string _scriptFile; // Filepath
private JObject _script; // Script
private Patcher _patcher; // Patcher
private readonly Dictionary<string, Target> _targets = new Dictionary<string, Target>(); // Target
private string _optional; // Optional arguments
public Script(string path)
{
_scriptFile = path;
_script = JObject.Parse(File.ReadAllText(path));
BuildTarget();
_patcher = new Patcher(_script.GetValue("target").ToString());
}
public void Patch()
{
foreach (var target in _targets)
{
if (target.Key == "empty")
_patcher.WriteEmptyBody(target.Value);
else if (target.Key == "return")
_patcher.WriteReturnBody(target.Value, Convert.ToBoolean(_optional));
else if (target.Key == "replace")
_patcher.ReplaceInstruction(target.Value);
else if(target.Key == "remove")
_patcher.RemoveInstruction(target.Value);
}
if (_script["save"] != null)
{
if (_script.GetValue("save").ToString() == _script.GetValue("target").ToString())
{
Save(true);
}
else
{
Save(_script.GetValue("save").ToString());
}
}
}
public void Save(bool backup)
{
_patcher.Save(backup);
}
public void Save(string name)
{
_patcher.Save(name);
}
public void LoadScript(string path)
{
_scriptFile = path;
_script = JObject.Parse(path);
BuildTarget();
_patcher = new Patcher(_script.GetValue("target").ToString());
}
private void BuildTarget()
{
JArray targets = (JArray) _script.GetValue("targets");
foreach (var t in targets)
{
Target target = new Target
{
Namespace = t["namespace"].ToString(),
Class = t["class"].ToString(),
Method = t["method"].ToString()
};
if (t["index"] != null)
target.Index = Convert.ToInt32(t["index"]);
if (t["indices"] != null)
target.Indices = t["indices"].Values<int>().ToArray();
if (t["optional"] != null)
_optional = t["optional"].ToString();
if (t["instructions"] != null)
{
JArray instructions = (JArray) t["instructions"];
if (instructions.Count == 1)
{
if (instructions[0]["opcode"] != null && instructions[0]["operand"] != null)
{
var operand = instructions[0].Last.Last;
if (operand.Type == JTokenType.Integer)
{
target.Instruction =
Instruction.Create(
(OpCode)
GetInstructionField(instructions[0].First.First.ToString()).GetValue(this),
operand.Value<int>());
}
else if (operand.Type == JTokenType.String)
{
target.Instruction =
Instruction.Create(
(OpCode)
GetInstructionField(instructions[0].First.First.ToString()).GetValue(this),
operand.Value<string>());
}
}
else if(instructions[0]["opcode"] != null)
{
target.Instruction =
Instruction.Create(
(OpCode) GetInstructionField(instructions[0].First.First.ToString()).GetValue(this));
}
}
else
{
target.Instructions = new Instruction[instructions.Count];
for (int i = 0; i < instructions.Count; i++)
{
var instruction = instructions[i];
if (instruction["opcode"] != null && instruction["operand"] != null)
target.Instructions[i] =
Instruction.Create((OpCode)GetInstructionField(instruction.First.First.ToString()).GetValue(this),
instruction.Last.Last.Value<dynamic>());
else
target.Instructions[i] =
Instruction.Create(
(OpCode)GetInstructionField(instruction.First.First.ToString()).GetValue(this));
}
}
}
_targets.Add(t["action"].ToString(), target);
}
}
private FieldInfo GetInstructionField(string name)
{
var type = typeof(OpCodes);
var field = type.GetField(name, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Static);
return field;
}
}
}