Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathReClassFile.cs
More file actions
390 lines (336 loc) · 9.23 KB
/
Copy pathReClassFile.cs
File metadata and controls
390 lines (336 loc) · 9.23 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Xml.Linq;
using ReClassNET.Logger;
using ReClassNET.Nodes;
using ReClassNET.UI;
using ReClassNET.Util;
namespace ReClassNET.DataExchange
{
class ReClassFile : IReClassImport
{
public const string FormatName = "ReClass File";
public const string FileExtension = ".reclass";
private readonly ReClassNetProject project;
public ReClassFile(ReClassNetProject project)
{
Contract.Requires(project != null);
this.project = project;
}
public void Load(string filePath, ILogger logger)
{
var document = XDocument.Load(filePath);
Type[] typeMap = null;
var versionComment = document.Root.FirstNode as XComment;
if (versionComment != null)
{
switch (versionComment.Value.Substring(0, 12).ToLower())
{
case "reclass 2011":
case "reclass 2013":
typeMap = TypeMap2013;
break;
case "reclass 2015":
case "reclass 2016":
typeMap = TypeMap2016;
break;
}
}
if (typeMap == null)
{
logger.Log(LogLevel.Warning, $"Unknown file version: {versionComment?.Value}");
logger.Log(LogLevel.Warning, "Defaulting to ReClass 2016.");
typeMap = TypeMap2016;
}
var classes = new List<Tuple<XElement, ClassNode>>();
foreach (var element in document.Root
.Elements("Class")
.DistinctBy(e => e.Attribute("Name")?.Value))
{
var node = new ClassNode(false)
{
Name = element.Attribute("Name")?.Value ?? string.Empty,
AddressFormula = TransformAddressString(element.Attribute("strOffset")?.Value ?? string.Empty)
};
project.AddClass(node);
classes.Add(Tuple.Create(element, node));
}
var classMap = classes.ToDictionary(t => t.Item2.Name, t => t.Item2);
foreach (var t in classes)
{
ReadNodeElements(
t.Item1.Elements("Node"),
t.Item2,
classMap,
typeMap,
logger
).ForEach(t.Item2.AddNode);
}
}
/// <summary>Parse ReClass address string and transform it into a ReClass.NET formula.</summary>
/// <param name="address">The address string.</param>
/// <returns>A string.</returns>
private string TransformAddressString(string address)
{
Contract.Requires(address != null);
var parts = address.Split('+').Select(s => s.Trim().ToLower().Replace("\"", string.Empty)).Where(s => s != string.Empty).ToArray();
for (int i = 0; i < parts.Length; ++i)
{
var part = parts[i];
bool isModule = part.Contains(".exe") || part.Contains(".dll");
bool isPointer = false;
if (part.StartsWith("*"))
{
isPointer = true;
part = part.Substring(1);
}
if (isModule)
{
part = $"<{part}>";
}
if (isPointer)
{
part = $"[{part}]";
}
parts[i] = part;
}
return string.Join(" + ", parts);
}
private IEnumerable<BaseNode> ReadNodeElements(IEnumerable<XElement> elements, ClassNode parent, IReadOnlyDictionary<string, ClassNode> classes, Type[] typeMap, ILogger logger)
{
Contract.Requires(elements != null);
Contract.Requires(parent != null);
Contract.Requires(classes != null);
Contract.Requires(typeMap != null);
Contract.Requires(logger != null);
foreach (var element in elements)
{
Type nodeType = null;
int typeVal;
if (int.TryParse(element.Attribute("Type")?.Value, out typeVal))
{
if (typeVal >= 0 && typeVal < typeMap.Length)
{
nodeType = typeMap[typeVal];
}
}
if (nodeType == null)
{
logger.Log(LogLevel.Error, $"Skipping node with unknown type: {element.Attribute("Type")?.Value}");
logger.Log(LogLevel.Warning, element.ToString());
continue;
}
var node = Activator.CreateInstance(nodeType) as BaseNode;
if (node == null)
{
logger.Log(LogLevel.Error, $"Could not create node of type: {nodeType}");
continue;
}
node.Name = element.Attribute("Name")?.Value ?? string.Empty;
node.Comment = element.Attribute("Comments")?.Value ?? string.Empty;
// Convert the Custom node into normal hex nodes.
if (node is CustomNode)
{
int size;
int.TryParse(element.Attribute("Size")?.Value, out size);
while (size != 0)
{
BaseNode paddingNode;
#if WIN64
if (size >= 8)
{
paddingNode = new Hex64Node();
}
else
#endif
if (size >= 4)
{
paddingNode = new Hex32Node();
}
else if (size >= 2)
{
paddingNode = new Hex16Node();
}
else
{
paddingNode = new Hex8Node();
}
paddingNode.Comment = node.Comment;
size -= paddingNode.MemorySize;
yield return paddingNode;
}
continue;
}
var referenceNode = node as BaseReferenceNode;
if (referenceNode != null)
{
string reference;
if (referenceNode is ClassInstanceArrayNode)
{
reference = element.Element("Array")?.Attribute("Name")?.Value;
}
else
{
reference = element.Attribute("Pointer")?.Value ?? element.Attribute("Instance")?.Value;
}
if (reference == null || !classes.ContainsKey(reference))
{
logger.Log(LogLevel.Error, $"Skipping node with unknown reference: {reference}");
logger.Log(LogLevel.Warning, element.ToString());
continue;
}
var innerClassNode = classes[reference];
if (referenceNode.PerformCycleCheck && !ClassUtil.IsCycleFree(parent, innerClassNode, project.Classes))
{
logger.Log(LogLevel.Error, $"Skipping node with cycle reference: {parent.Name}->{node.Name}");
continue;
}
referenceNode.ChangeInnerNode(innerClassNode);
}
var vtableNode = node as VTableNode;
if (vtableNode != null)
{
element
.Elements("Function")
.Select(e => new VMethodNode
{
Name = e.Attribute("Name")?.Value ?? string.Empty,
Comment = e.Attribute("Comments")?.Value ?? string.Empty
})
.ForEach(vtableNode.AddNode);
}
var classInstanceArrayNode = node as ClassInstanceArrayNode;
if (classInstanceArrayNode != null)
{
int count;
TryGetAttributeValue(element, "Total", out count, logger);
classInstanceArrayNode.Count = count;
}
var classPtrArrayNode = node as ClassPtrArrayNode;
if (classPtrArrayNode != null)
{
int count;
TryGetAttributeValue(element, "Size", out count, logger);
classInstanceArrayNode.Count = count / IntPtr.Size;
}
var textNode = node as BaseTextNode;
if (textNode != null)
{
int length;
TryGetAttributeValue(element, "Size", out length, logger);
textNode.Length = textNode is UTF16TextNode ? length / 2 : length;
}
var bitFieldNode = node as BitFieldNode;
if (bitFieldNode != null)
{
int bits;
TryGetAttributeValue(element, "Size", out bits, logger);
bitFieldNode.Bits = bits * 8;
}
yield return node;
}
}
private static void TryGetAttributeValue(XElement element, string attribute, out int val, ILogger logger)
{
if (!int.TryParse(element.Attribute(attribute)?.Value, out val))
{
val = 0;
logger.Log(LogLevel.Error, $"Node is missing a valid '{attribute}' attribute, defaulting to 0.");
logger.Log(LogLevel.Warning, element.ToString());
}
}
/// <summary>Dummy node to represent the ReClass Custom node.</summary>
private class CustomNode : BaseNode
{
public override int MemorySize
{
get { throw new NotImplementedException(); }
}
public override int CalculateHeight(ViewInfo view)
{
throw new NotImplementedException();
}
public override int Draw(ViewInfo view, int x, int y)
{
throw new NotImplementedException();
}
}
#region ReClass 2011 / ReClass 2013
private static readonly Type[] TypeMap2013 = new Type[]
{
null,
typeof(ClassInstanceNode),
null,
null,
typeof(Hex32Node),
typeof(Hex16Node),
typeof(Hex8Node),
typeof(ClassPtrNode),
typeof(Int32Node),
typeof(Int16Node),
typeof(Int8Node),
typeof(FloatNode),
typeof(UInt32Node),
typeof(UInt16Node),
typeof(UInt8Node),
typeof(UTF8TextNode),
typeof(FunctionPtrNode),
typeof(CustomNode),
typeof(Vector2Node),
typeof(Vector3Node),
typeof(Vector4Node),
typeof(Matrix4x4Node),
typeof(VTableNode),
typeof(ClassInstanceArrayNode),
null,
null,
null,
typeof(Int64Node),
typeof(DoubleNode),
typeof(UTF16TextNode),
typeof(ClassPtrArrayNode)
};
#endregion
#region ReClass 2015 / ReClass 2016
private static readonly Type[] TypeMap2016 = new Type[]
{
null,
typeof(ClassInstanceNode),
null,
null,
typeof(Hex32Node),
typeof(Hex64Node),
typeof(Hex16Node),
typeof(Hex8Node),
typeof(ClassPtrNode),
typeof(Int64Node),
typeof(Int32Node),
typeof(Int16Node),
typeof(Int8Node),
typeof(FloatNode),
typeof(DoubleNode),
typeof(UInt32Node),
typeof(UInt16Node),
typeof(UInt8Node),
typeof(UTF8TextNode),
typeof(UTF16TextNode),
typeof(FunctionPtrNode),
typeof(CustomNode),
typeof(Vector2Node),
typeof(Vector3Node),
typeof(Vector4Node),
typeof(Matrix4x4Node),
typeof(VTableNode),
typeof(ClassInstanceArrayNode),
null,
typeof(UTF8TextPtrNode),
typeof(UTF16TextPtrNode),
typeof(BitFieldNode),
typeof(UInt64Node),
typeof(FunctionNode)
};
#endregion
}
}
You can’t perform that action at this time.
