-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblorb.py
More file actions
399 lines (341 loc) · 11.7 KB
/
blorb.py
File metadata and controls
399 lines (341 loc) · 11.7 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
391
392
393
394
395
396
397
398
399
# Copyright (C) 2001 - 2019 David Fillmore
#
# This file is part of buffle.
#
# buffle is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# buffle is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import iff
import tempfile
import os
import sys
import babel
class rect:
def __init__(self, data=None):
if data:
width = data[:4]
height = data[4:8]
self.width = int.from_bytes(width, byteorder='big')
self.height = int.from_bytes(height, byteorder='big')
else:
self.width = 0
self.height = 0
def getPalette(self):
return None
def setPalette(self, palette):
pass
def draw(self, window, x, y):
pass
def getWidth(self):
return self.width
def getHeight(self):
return self.height
def scale(self, width, height):
newRect = rect()
newRect.width = width
newRect.height = height
return newRect
currentpalette = [(0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0), (0,0,0)]
class InvalidBlorbFile(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class InvalidIFhdChunk(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class NoExecChunk(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class Blorb:
def __init__(self, filename, game=None):
try:
file = open(filename, 'rb')
self.data = file.read()
file.close()
except:
raise InvalidBlorbFile("Error opening the Blorb file.")
x = self.findChunk(b'RIdx')
x += 8
rescount = int.from_bytes(self.data[x:x+4], byteorder='big')
x += 4
self.resindex = {}
self.resindex[b'Pict'] = {}
self.resindex[b'Snd '] = {}
self.resindex[b'Exec'] = {}
for a in range(rescount):
usage = self.data[x+(a*12):x+(a*12)+4]
resnum = int.from_bytes(self.data[x+(a*12)+4:x+(a*12)+8], byteorder='big')
pos = int.from_bytes(self.data[x+(a*12)+8:x+(a*12)+12], byteorder='big')
try:
self.resindex[usage][resnum] = pos
except:
self.resindex[usage] = {}
self.resindex[usage][resnum] = pos
#if game:
# valid = self.checkgame(game)
# if valid == False:
# raise InvalidIFhdChunk("The Blorb file does not match the game.")
#else:
# if self.getExec(0) == False:
# raise NoExecChunk("The Blorb file does not contain a game file.")
x = self.findChunk(b'RelN')
if x == 0:
self.release = 0
else:
x += 8
self.release = (self.data[x] << 8) + self.data[x+1]
def checkgame(self, game):
x = self.findChunk(b'IFhd')
if x == 0:
return True
x += 8
idRelease = int.from_bytes(self.data[x:x+2], byteorder='big')
x += 2
idSerial = self.data[x:x+6]
x+=6
idChecksum = int.from_bytes(self.data[x:x+2], byteorder='big')
x = 2
gameRelease = int.from_bytes(game[x:x+2], byteorder='big')
x = 0x12
gameSerial = game[x:x+6]
x = 0x1C
gameChecksum = int.from_bytes(game[x:x+2], byteorder='big')
if gameRelease == idRelease and gameSerial == idSerial and gameChecksum == idChecksum:
return True
return False
def chunkSize(self, place):
size = int.from_bytes(self.data[place+4:place+8], byteorder='big')
return size
def chunkType(self, place):
type = self.data[place:place+4]
return type
def getExec(self, execnum):
try:
x = self.resindex[b'Exec'][execnum]
except:
return False
size = self.chunkSize(x)
data = self.data[x+8:x+8+size]
return data
def getExecFormat(self, execnum):
try:
x = self.resindex[b'Exec'][execnum]
except:
return False
type = self.chunkType(x)
return type
def getPict(self, picnum):
try:
x = self.resindex[b'Pict'][picnum]
except:
return False
size = self.chunkSize(x)
data = self.data[x+8:x+8+size]
return data
def getPictFormat(self, picnum):
try:
x = self.resindex[b'Pict'][picnum]
except:
return False
type = self.chunkType(x)
return type
def getSnd(self, sndnum):
try:
x = self.resindex[b'Snd '][sndnum]
except:
return False
type = self.chunkType(x)
size = self.chunkSize(x)
if type == b'FORM':
data = self.data[x:x+8+size]
else:
data = self.data[x+8:x+8+size]
return data
def getSndFormat(self, sndnum):
try:
x = self.resindex[b'Snd '][sndnum]
except:
return False
type = self.chunkType(x)
if type == b'FORM':
return b'AIFF'
else:
return type
def getSndType(self, sndnum):
# AIFF Sounds = effect
# Ogg Sounds = music
# mod sounds = music
# Song Sounds = music (unsupported)
format = self.getSndFormat(sndnum)
if format == b'AIFF':
return 0 # sample
if format in ['OGGV', 'MOD ', 'SONG']:
return 1 # music
return 2 # unknown format, so unknown type
def getWinSizes(self):
x = self.findChunk(b'Reso')
if x == 0:
return None
resosize = self.chunkSize(x)
resochunk = self.data[x+8:x+8+resosize]
x = 0
px = float(int.from_bytes(resochunk[x:x+4], byteorder='big')) # standard window width
x += 4
py = float(int.from_bytes(resochunk[x:x+4], byteorder='big')) # standard window height
x += 4
minx = int.from_bytes(resochunk[x:x+4], byteorder='big') # minimum window width
x += 4
miny = int.from_bytes(resochunk[x:x+4], byteorder='big') # minimum window height
x += 4
maxx = int.from_bytes(resochunk[x:x+4], byteorder='big') # maximum window width
x += 4
maxy = int.from_bytes(resochunk[x:x+4], byteorder='big') # maximum window height
return (px, py, minx, miny, maxx, maxy)
def getScale(self, picnum, winx, winy):
scaleData = getScaleData(picnum)
px, py, minx, miny, maxx, maxy = self.getWinSizes()
ratnum = scaleData['ratnum']
ratden = scaleData['ratden']
minnum = scaleData['minnum']
minden = scaleData['minden']
maxnum = scaleData['maxnum']
maxden = scaleData['maxden']
stdratio = ratnum / ratden
if minnum != 0:
minratio = minnum / minden
else:
minratio = .0
if maxnum != 0:
maxratio = maxnum / maxden
else:
maxratio = .0
if (winx/px) < (winy/py):
ERF = winx/px
else:
ERF = winy/py
if (ERF * stdratio < minratio) and (minratio != 0):
R = minratio
elif (ERF * stdratio > maxratio) and (maxratio != 0):
R = maxratio
else:
R = ERF * stdratio
return R
def getScaleData(self, picnum):
x = self.findChunk(b'Reso')
scaleData = {'ratnum':1,
'ratden':1,
'minnum':1,
'minden':1,
'maxnum':1,
'maxden':1
}
if x == 0:
return scaleData
resosize = self.chunkSize(x)
resochunk = self.data[x+8:x+8+resosize]
x = 24
entrydata = resochunk[x:]
entries = len(entrydata) // 28
found = False
for a in range(entries):
b = a * 28
entry = entrydata[b:b+28]
if int.from_bytes(entry[:4], byteorder='big') == picnum:
found = True
break
if found == False:
return scaleData
scaleData['ratnum'] = int.from_bytes(entry[4:8], byteorder='big')
scaleData['ratden'] = int.from_bytes(entry[8:12], byteorder='big')
scaleData['minnum'] = int.from_bytes(entry[12:16], byteorder='big')
scaleData['minden'] = int.from_bytes(entry[16:20], byteorder='big')
scaleData['maxnum'] = int.from_bytes(entry[20:24], byteorder='big')
scaleData['maxden'] = int.from_bytes(entry[24:28], byteorder='big')
return scaleData
def getPalette(self, picnum, palette):
global currentpalette
if not palette:
return None
palette = palette[:16]
pos = self.findChunk(b'APal')
if pos == 0:
return palette
csize = self.chunkSize(pos)
chunk = self.data[pos+8:pos+8+csize]
numentries = csize // 4
entries = []
for a in range(numentries):
entries.append(int.from_bytes(chunk[4*a:(4*a) + 4], byteorder='big'))
if picnum in entries:
return currentpalette
for a in range(2, len(palette)):
if palette[a] != (0,0,0):
currentpalette[a] = palette[a][:]
return palette
def findChunk(self, chunkname):
id = None
x = 12
while (id != chunkname) and (x < len(self.data)):
id = self.data[x:x+4]
csize = int.from_bytes(self.data[x+4:x+8], byteorder='big')
if csize % 2 == 1:
csize += 1
if id == chunkname:
break
x += csize + 8
if id != chunkname:
return False
#x -= csize + 8
return x
def listChunks(self):
id = None
x = 12
chunks = []
while (x < len(self.data)):
id = self.data[x:x+4]
csize = int.from_bytes(self.data[x+4:x+8], byteorder='big')
c = {}
c['type'] = id
c['location'] = x
c['size'] = csize
chunks.append(c)
if csize % 2 == 1:
csize += 1
x += csize + 8
return chunks
def getMetaData(self):
resoplace = self.findChunk(b'IFmd')
if resoplace == False:
return None
resosize = self.chunkSize(resoplace)
metadata = self.data[resoplace+8:resoplace+8+resosize]
return metadata
def gettitlepic(self):
resoplace = self.findChunk(b'Fspc')
if resoplace == None:
iFiction = self.getmetadata()
picnum = babel.getcoverpicture(iFiction)
if picnum != None:
pic = getpic(picnum, titlepic=True)
else:
pic = None
return pic
#rfile.seek(resoplace + 4)
#resosize = int.from_bytes(rfile.read(4), byteorder='big')
#if resosize != 4:
# return None
#rfile.seek(resoplace+8)
#picnum = int.from_bytes(rfile.read(4), byteorder='big')
#pic = getpic(picnum, titlepic=True)
return None