forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReClass2007File.cs
More file actions
207 lines (169 loc) · 5.13 KB
/
Copy pathReClass2007File.cs
File metadata and controls
207 lines (169 loc) · 5.13 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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics.Contracts;
using System.Linq;
using ReClassNET.Logger;
using ReClassNET.Nodes;
using ReClassNET.Util;
namespace ReClassNET.DataExchange
{
class ReClass2007File : IReClassImport
{
public const string FormatName = "ReClass 2007 File";
public const string FileExtension = ".rdc";
private static readonly Type[] TypeMap = new Type[]
{
null,
typeof(ClassInstanceNode),
typeof(ClassNode),
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)
};
private ReClassNetProject project;
public ReClass2007File(ReClassNetProject project)
{
Contract.Requires(project != null);
this.project = project;
}
public void Load(string filePath, ILogger logger)
{
using (var connection = new SQLiteConnection($@"Data Source={filePath}"))
{
connection.Open();
var classes = new Dictionary<int, ClassNode>();
var vtables = new Dictionary<int, VTableNode>();
foreach (var row in Query(connection, "SELECT tbl_name FROM sqlite_master WHERE tbl_name LIKE 'class%'"))
{
var id = Convert.ToInt32(row["tbl_name"].ToString().Substring(5));
var classRow = Query(connection, $"SELECT variable, comment FROM class{id} WHERE type = 2 LIMIT 1").FirstOrDefault();
if (classRow == null)
{
continue;
}
// Skip the vtable classes.
if (classRow["variable"].ToString() == "VTABLE")
{
var vtableNode = new VTableNode();
Query(connection, $"SELECT variable, comment FROM class{id} WHERE type = 16")
.Select(e => new VMethodNode
{
Name = Convert.ToString(e["variable"]) ?? string.Empty,
Comment = Convert.ToString(e["comment"]) ?? string.Empty
})
.ForEach(vtableNode.AddNode);
foreach (var method in vtableNode.Nodes)
{
if (method.Name == "void function()")
{
method.Name = string.Empty;
}
}
vtables.Add(id, vtableNode);
continue;
}
var node = new ClassNode(false)
{
Name = classRow["variable"].ToString(),
Comment = classRow["comment"].ToString()
};
project.AddClass(node);
classes.Add(id, node);
}
foreach (var kv in classes)
{
ReadNodeRows(
Query(connection, $"SELECT variable, comment, type, length, ref FROM class{kv.Key} WHERE type != 2"),
kv.Value,
classes,
vtables,
logger
).ForEach(kv.Value.AddNode);
}
}
}
private IEnumerable<BaseNode> ReadNodeRows(IEnumerable<DataRow> rows, ClassNode parent, IReadOnlyDictionary<int, ClassNode> classes, IReadOnlyDictionary<int, VTableNode> vtables, ILogger logger)
{
Contract.Requires(rows != null);
Contract.Requires(parent != null);
Contract.Requires(logger != null);
foreach (var row in rows)
{
Type nodeType = null;
int typeVal = Convert.ToInt32(row["type"]);
if (typeVal >= 0 && typeVal < TypeMap.Length)
{
nodeType = TypeMap[typeVal];
}
if (nodeType == null)
{
logger.Log(LogLevel.Error, $"Skipping node with unknown type: {row["type"]}");
logger.Log(LogLevel.Warning, string.Join(",", row.ItemArray));
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 = Convert.ToString(row["variable"]) ?? string.Empty;
node.Comment = Convert.ToString(row["comment"]) ?? string.Empty;
var referenceNode = node as BaseReferenceNode;
if (referenceNode != null)
{
var reference = Convert.ToInt32(row["ref"]);
if (!classes.ContainsKey(reference))
{
VTableNode vtableNode;
if (!vtables.TryGetValue(reference, out vtableNode))
{
logger.Log(LogLevel.Error, $"Skipping node with unknown reference: {row["ref"]}");
logger.Log(LogLevel.Warning, string.Join(",", row.ItemArray));
continue;
}
yield return vtableNode;
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 textNode = node as BaseTextNode;
if (textNode != null)
{
textNode.Length = Math.Max(IntPtr.Size, Convert.ToInt32(row["length"]));
}
yield return node;
}
}
private IEnumerable<DataRow> Query(SQLiteConnection connection, string query)
{
Contract.Requires(connection != null);
Contract.Requires(query != null);
using (var adapter = new SQLiteDataAdapter(query, connection))
{
var ds = new DataSet();
adapter.Fill(ds);
return ds.Tables[0].AsEnumerable();
}
}
}
}