-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGBXChallengeFetcher.py
314 lines (268 loc) · 10.5 KB
/
GBXChallengeFetcher.py
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
import struct
import os
import xml.parsers.expat
import weakref
"""
\file GBXChallengeFetcher.py
\brief Contains a class for reading gbx files
"""
class GBXChallengeFetcher:
"""
\brief A class capable of reading GBX files
"""
def __init__(self):
"""
\brief Create a new Fetcher
"""
self.fileName = ''
self.parseXML = False
self.tnimage = False
self.uid = 0
self.version = 0
self.name = ''
self.author = ''
self.azone = ''
self.type = ''
self.envir = ''
self.mood = ''
self.pub = ''
self.authortm = 0
self.goldtm = 0
self.silvertm = 0
self.bronzetime = 0
self.cost = 0
self.multi = False
self.unknown = None
self.unknown2 = None
self.unknown3 = None
self.unknown4 = None
self.unknown5 = None
self.ascore = 0
self.editor = 0
self.password = 0
self.xml = False
self.parsedXML = {}
self.xmlver = None
self.exever = 0
self.exebld = 0
self.lightmap = None
self.nblaps = 0
self.songfile = ''
self.songurl = ''
self.modname = ''
self.modfile = ''
self.modurl = ''
self.thumbnail = None
self.comment = ''
self.xmlTreeRoute = [self.parsedXML]
@staticmethod
def FetchChallenge(fileName, parseXML, tnimage = False):
"""
\brief Fetches the data about the GBX Challenge
\param fileName The (full) path to the GBX file
\param parseXML Should the xml be parsed
\param tnimage Should a thumbnail be extracted from the file
\return Save the data in the fetcher
"""
fetcher = GBXChallengeFetcher()
fetcher.fileName = fileName
fetcher.parseXML = parseXML
fetcher.tnimage = tnimage
fetcher.getData()
return fetcher
def ReadGBXString(self, fileHandle):
"""
\brief Reads one String (length, text) from the file
\param fileHandle The file to read from
\return text
"""
strlen = struct.unpack('<L', fileHandle.read(4))[0]
if strlen < 0 or strlen > 0x10000:
return 'read error'
return fileHandle.read(strlen)
def startTag(self, name, attrs):
"""
\brief Handle the begin of an xml tag
\param name The name of the tag
\param attrs The attributes of the tag
"""
node = self.xmlTreeRoute[-1]
if not node.has_key(name):
node[name] = []
newNode = {}
newNode['ATTR'] = attrs
newNode['CHIL'] = {}
node[name].append(newNode);
self.xmlTreeRoute.append(node[name][-1]['CHIL'])
def endTag(self, name):
"""
\brief Handle the end of an xml tag
\param Name the name of the tag
"""
self.xmlTreeRoute.pop()
def getData(self):
"""
\brief Read the data from the file
"""
fileHandle = open(self.fileName, 'rb')
fileHandle.seek(0, os.SEEK_SET)
magicGBXHeader = fileHandle.read(5)
if magicGBXHeader != 'GBX\x06\x00':
fileHandle.close()
return False
fileHandle.seek(4, os.SEEK_CUR)
data = '{:08X}'.format(struct.unpack('>L', fileHandle.read(4))[0])
if data != '003000234' and data != '00300403':
fileHandle.close()
return False
fileHandle.seek(4, os.SEEK_CUR)
self.version = struct.unpack('<L', fileHandle.read(4))[0]
if self.version < 2 or self.version > 6:
fileHandle.close()
return False
lengths = range(self.version + 1)
for i in range(1, self.version + 1, 1):
fileHandle.seek(4, os.SEEK_CUR)
lengths[i] = struct.unpack('<L', fileHandle.read(4))[0]
if self.version >= 5:
lengths[4] &= 0x7FFFFFFF
lengths[5] &= 0x7FFFFFFF
#start of Times/info block
count = ord(fileHandle.read(1))
#skip unknown integer
fileHandle.seek(4, os.SEEK_CUR)
self.bronzetm = struct.unpack('<L', fileHandle.read(4))[0]
self.silvertm = struct.unpack('<L', fileHandle.read(4))[0]
self.goldtm = struct.unpack('<L', fileHandle.read(4))[0]
self.authortm = struct.unpack('<L', fileHandle.read(4))[0]
if self.version >= 3:
self.cost = struct.unpack('<L', fileHandle.read(4))[0]
if count >= 6:
self.multi = (struct.unpack('<L', fileHandle.read(4))[0] != 0)
mapType = struct.unpack('<L', fileHandle.read(4))[0]
typeDict = {0 : 'Race',
1 : 'Platform',
2 : 'Puzzle',
3 : 'Crazy',
4 : 'Shortcut',
5 : 'Stuns',
6 : 'Script'}
self.type = typeDict.get(mapType, 'unknown!')
if count >= 9:
self.unknown = struct.unpack('<L', fileHandle.read(4))[0]
if count >= 10:
self.ascore = struct.unpack('<L', fileHandle.read(4))[0]
if count >= 11:
self.editor = (struct.unpack('<L', fileHandle.read(4))[0] != 0)
if count >= 12:
self.unknown2 = struct.unpack('<L', fileHandle.read(4))[0]
if count >= 13:
self.unknown4 = struct.unpack('<L', fileHandle.read(4))[0]
self.unknown5 = struct.unpack('<L', fileHandle.read(4))[0]
fileHandle.seek(4, os.SEEK_CUR)
fileHandle.seek(5, os.SEEK_CUR)
self.uid = self.ReadGBXString(fileHandle)
data = struct.unpack('<L', fileHandle.read(4))[0]
if data != 12:
self.envir = self.ReadGBXString(fileHandle)
else:
self.envir = 'XML'
fileHandle.seek(4, os.SEEK_CUR)
self.author = self.ReadGBXString(fileHandle)
self.name = self.ReadGBXString(fileHandle)
fileHandle.seek(1, os.SEEK_CUR)
if self.version >= 3:
fileHandle.seek(4, os.SEEK_CUR)
self.password = self.ReadGBXString(fileHandle)
if self.password == 'read error':
self.password = ''
if self.version >= 4 and count >= 8:
fileHandle.seek(4, os.SEEK_CUR)
self.mood = self.ReadGBXString(fileHandle)
fileHandle.seek(4, os.SEEK_CUR)
if ord(fileHandle.read(4)[0]) != 3:
self.pub = self.ReadGBXString(fileHandle)
else:
self.pub = ''
length = 0
for i in xrange(1, self.version + 1, 1):
length += 8
if i <= 3:
length += lengths[i]
fileHandle.seek(0x15 + length, os.SEEK_SET)
if self.version >= 4:
self.xml = self.ReadGBXString(fileHandle)
self.xml = self.xml.replace('><', '>\n<')
if self.version >= 5:
fileHandle.seek(4, os.SEEK_CUR)
thumblen = struct.unpack('<L', fileHandle.read(4))[0]
fileHandle.seek(15, os.SEEK_CUR)
if thumblen > 0 and thumblen < 0x10000:
data = fileHandle.read(thumblen)
if self.tnimage:
self.thumbnail = data
fileHandle.seek(16, os.SEEK_CUR)
fileHandle.seek(10, os.SEEK_CUR)
self.comment = self.ReadGBXString(fileHandle)
if self.comment == 'read error':
self.comment = ''
fileHandle.seek(11, os.SEEK_CUR)
fileHandle.close()
if self.parseXML and isinstance(self.xml, str):
parser = xml.parsers.expat.ParserCreate()
parser.StartElementHandler = self.startTag
parser.EndElementHandler = self.endTag
parser.CharacterDataHandler = lambda data: None
try:
parser.Parse(self.xml.encode('UTF-8'))
except Exception as e:
self.parsedXML = e
return False
try:
self.azone = self.parsedXML['header'][0]['CHIL']['ident'][0]['ATTR']['authorzone']
except KeyError:
self.azone = ''
try:
self.envir = self.parsedXML['header'][0]['CHIL']['desc'][0]['ATTR']['envir']
except KeyError:
self.envir = ''
try:
self.mood = self.parsedXML['header'][0]['CHIL']['desc'][0]['ATTR']['mood']
except KeyError:
self.mood = ''
try:
self.modname = self.parsedXML['header'][0]['CHIL']['desc'][0]['ATTR']['mod']
except KeyError:
self.modname = ''
"""
try:
self.xmlver = self.parsedXML['HEADER']['ATTR']['VERSION']
except KeyError:
self.xmlver = ''
"""
try:
self.exever = self.parsedXML['header'][0]['ATTR']['exever']
except KeyError:
self.exever = ''
try:
self.exebld = self.parsedXML['header'][0]['ATTR']['exebuild']
except KeyError:
self.exebld = ''
try:
self.respawns = self.parsedXML['header'][0]['CHIL']['times'][0]['ATTR']['respawns']
except KeyError:
self.respawns = ''
try:
self.stuntscore = self.parsedXML['header'][0]['CHIL']['times'][0]['ATTR']['stuntscore']
except KeyError:
self.stuntscore = ''
try:
self.validable = self.parsedXML['header'][0]['CHIL']['times'][0]['ATTR']['validable']
except:
self.validable = ''
try:
self.cpscur = self.parsedXML['header'][0]['CHIL']['checkpoints'][0]['ATTR']['cur']
self.cpslap = self.parsedXML['header'][0]['CHIL']['checkpoints'][0]['ATTR']['onelap']
except KeyError:
self.cpscur = ''
self.cpslap = ''