-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDecompiler.cs
352 lines (318 loc) · 12.3 KB
/
Decompiler.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
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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
namespace spb2xml
{
/// <summary>
/// optionnal models defini. Accepts an optional
/// models definition file (mapping guid to model friendly names,
/// useful for missions or LW conf files).
///
/// Note that xml propdefs definition files are read by an independant
/// static class, SymbolBank
///
/// Once the decompiler has been created, passing the .SPB file t
/// o the constructor, it can be decompiled to XML with any of the
/// Decompile method.
/// </summary>
class Decompiler
{
private SymbolBank bank;
private BinaryReader reader;
private DefinitionElement[] tags;
private int ntags;
private XmlDocument doc;
private ModelBank models;
private static UTF8Encoding encoder = new UTF8Encoding();
private const string DEC_FORMAT = "0.000";
public Decompiler(string spbFileUrl)
{
bank = SymbolBank.Instance;
FileStream fs = new FileStream(spbFileUrl, FileMode.Open);
reader = new BinaryReader(fs);
}
public void SetModels(ModelBank mBank)
{
models = mBank;
}
public void Decompile(string fileUrl)
{
Decompile(new FileStream(fileUrl, FileMode.Create));
}
/// <summary>
/// Throws SPBException on any error specific to the SPB file format
/// </summary>
/// <param name="outStream"></param>
public void Decompile(Stream outStream)
{
// read headers
ReadHeaders();
ReadTagData();
doc = new XmlDocument();
XmlDeclaration xmlDeclNode = (XmlDeclaration) doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
doc.AppendChild(xmlDeclNode);
ParseElement(null, doc);
// write
XmlTextWriter writer = new XmlTextWriter(outStream, Encoding.Default);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
}
/// <summary>
/// Read basic headers (U16 signature and 10 U32 headers (unknown meaning))
/// </summary>
private void ReadHeaders()
{
// first byte
ushort fType = reader.ReadUInt16();
if (fType != 0xEBAC)
{
throw new SPBException("Invalid file ID");
}
ntags = 0;
// read headers (unknown)
for (int i = 0; i < 12; i++)
{
int v = reader.ReadInt32();
if (i == 6) ntags = v;
}
// allocate tags
tags = new DefinitionElement[ntags];
}
private void ReadTagData()
{
for (int i = 0; i < (ntags - 1); i++)
{
byte[] guidData = reader.ReadBytes(16);
Guid g = new Guid(guidData);
DefinitionElement de = bank.LookupElement(g);
if (de == null)
{
throw new SPBException("Unbound property GUID : " + g.ToString());
}
tags[i] = de;
reader.ReadInt32(); // unknwon flag
}
}
private void ParseElement(SymbolDef current, XmlNode node)
{
int tagNumber = reader.ReadInt32() - 1;
if (tagNumber == -1) return; // shit happens
if (tagNumber < 0 || tagNumber > ntags)
{
throw new SPBException("Invalid tag index " + tagNumber);
}
DefinitionElement tagType = tags[tagNumber];
if (tagType is SetDef)
{
SetDef set = (SetDef)tagType;
ParseSet(current, set, node);
}
else if (tagType is PropertyDef)
{
PropertyDef pd = (PropertyDef)tagType;
ParseProperty(current, pd, node);
}
else
{
throw new SPBException("Unexpected tag type : " + tagType.GetType().Name);
}
}
private void ParseSet(SymbolDef current, SetDef set, XmlNode node)
{
int setSize = reader.ReadInt32();
long endPosition = reader.BaseStream.Position + setSize;
XmlNode setNode;
// compare SetDefs
if (current == null || set.Parent != current)
{
// switch symbols
setNode = doc.CreateElement("", set.Parent.Name + "." + set.Name, "");
current = set.Parent;
} else {
setNode = doc.CreateElement("", set.Name, "");
}
node.AppendChild(setNode);
while (reader.BaseStream.Position < endPosition)
{
ParseElement(current, setNode);
}
}
private void ParseProperty(SymbolDef current, PropertyDef prop, XmlNode node)
{
TypeDef type = prop.Type;
switch (type.Name) {
case "TEXT":
case "MLTEXT":
{
int stringLen = reader.ReadInt32();
string s;
if (stringLen <= 0)
{
s = "";
}
else
{
s = TextDecode.Decode(reader.ReadBytes(stringLen));
}
AddProp(current, prop, s, node);
}
break;
case "ULONG":
{
uint uv = reader.ReadUInt32();
AddProp(current, prop, Convert.ToString(uv), node);
}
break;
case "LONG":
{
int v = reader.ReadInt32();
AddProp(current, prop, Convert.ToString(v), node);
}
break;
case "LONG2":
{
int v1 = reader.ReadInt32();
int v2 = reader.ReadInt32();
AddProp(current, prop, v1 + "," + v2, node);
}
break;
case "LONG4":
{
int v1 = reader.ReadInt32();
int v2 = reader.ReadInt32();
int v3 = reader.ReadInt32();
int v4 = reader.ReadInt32();
AddProp(current, prop, v1 + "," + v2 + "," + v3 + "," + v4, node);
}
break;
case "BOOL":
{
int v = reader.ReadInt32();
AddProp(current, prop, (v == 1) ? "true" : "false", node);
}
break;
case "FLOAT":
{
float f = reader.ReadSingle();
AddProp(current, prop, f.ToString(DEC_FORMAT), node);
}
break;
case "FLOAT2":
{
float f1 = reader.ReadSingle();
float f2 = reader.ReadSingle();
AddProp(current, prop, f1.ToString(DEC_FORMAT) + "," + f2.ToString(DEC_FORMAT), node);
}
break;
case "FLOAT4":
{
float f1 = reader.ReadSingle();
float f2 = reader.ReadSingle();
float f3 = reader.ReadSingle();
float f4 = reader.ReadSingle();
AddProp(current, prop, f1.ToString(DEC_FORMAT) + "," +
f2.ToString(DEC_FORMAT) + "," +
f3.ToString(DEC_FORMAT) + "," +
f4.ToString(DEC_FORMAT), node);
}
break;
case "DOUBLE":
{
double v = reader.ReadDouble();
AddProp(current, prop, v.ToString(DEC_FORMAT), node);
}
break;
case "BYTE4":
{
byte b1 = reader.ReadByte();
byte b2 = reader.ReadByte();
byte b3 = reader.ReadByte();
byte b4 = reader.ReadByte();
AddProp(current, prop, b1 + "," + b2 + "," + b3 + "," + b4, node);
}
break;
case "GUID":
{
byte[] gd = reader.ReadBytes(16);
Guid g = new Guid(gd);
if (models != null)
{
string modelName = models.Lookup(g);
if (modelName != null)
{
XmlComment com = doc.CreateComment("Model: " + modelName);
node.AppendChild(com);
}
}
AddProp(current, prop, "{" + new Guid(gd).ToString().ToUpper() + "}", node);
}
break;
case "PBH":
case "PBH32":
{
double p = reader.ReadUInt32() / ((double) 65536 * 65536) * 360.0;
double b = reader.ReadUInt32() / ((double)65536 * 65536) * 360.0;
double h = reader.ReadUInt32() / ((double)65536 * 65536) * 360.0;
// pad
reader.ReadInt32();
AddProp(current, prop, p.ToString(DEC_FORMAT) + "," + b.ToString(DEC_FORMAT) + "," + h.ToString(DEC_FORMAT), node);
}
break;
case "ENUM":
{
EnumDef enumDef = prop.Enum;
if (enumDef == null)
{
throw new SPBException("Enum type without values");
}
int idx = reader.ReadInt32();
string val = enumDef[idx];
AddProp(current, prop, val, node);
}
break;
case "LLA":
{
long lat = reader.ReadInt64();
long lon = reader.ReadInt64();
uint alt1 = reader.ReadUInt32();
int alt2 = reader.ReadInt32();
LLA l = new LLA(lat, lon, alt1, alt2);
AddProp(current, prop, l.ToString("D2"), node);
}
break;
case "FILETIME":
{
// TODO: filetime
}
break;
default:
throw new SPBException("Don't know how to format type " + type.Type + " at " + reader.BaseStream.Position);
}
}
private void AddProp(SymbolDef current, PropertyDef pd, String text, XmlNode node)
{
if (pd.IsAttribute())
{
XmlAttribute aNode = doc.CreateAttribute(pd.Name);
aNode.Value = text;
node.Attributes.Append(aNode);
}
else
{
// fix namespace issue (if property was issued from a different context)
String propName = pd.Name;
if (pd.SymbolContext != null &&
pd.SymbolContext != current)
propName = pd.SymbolContext.Name + "." + propName;
//
// create property as enclosed element
XmlNode pNode = doc.CreateElement("", propName.TrimEnd(), "");
XmlNode tNode = doc.CreateTextNode(text);
pNode.AppendChild(tNode);
node.AppendChild(pNode);
}
}
}
}