-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.py
76 lines (58 loc) · 1.84 KB
/
extractor.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
import os
import re
import sys
import io
from PIL import Image
output_raw = False
output_textures = False
output_palettes = False
output_backgrounds = True
output_models = False
output_sounds = False
output_briefings = False
output_surfaces = False
output_font = True
output_maps = False
hog_files = []
f = os.open("./input/DESCENT.HOG", os.O_RDONLY)
info = os.fstat(f)
sig = os.read(f, 3).decode("latin1")
# print("File Info :", info)
if sig != "DHF":
print("HOG file not DHF")
exit(1)
file_offset = 3
while file_offset < info.st_size:
# get filename and strip 0x00 and 0x16
file_name = os.read(f, 13).decode('latin1')
file_offset += 13
# strip everything but a-z, dot and digits
file_name = re.sub('[^a-z\.\d]', '', file_name)
# get extension without dot
file_type = os.path.splitext(file_name)[1][-3:]
# f is at 16 now (3+13), read 4 bytes for a 32bit integer filesize
file_size = os.read(f, 4);
file_size = int.from_bytes(file_size, byteorder=sys.byteorder)
file_offset += 4
# file_offset += 4 The extracter node.js script pushes the offset further after every read.
# in our python script, the read advances the filepointer, so the fileoffset is purely for the loop.
file_data = os.read(f, file_size)
file_offset += file_size
print(file_name)
file = {
"file_name": file_name,
"type": file_type,
"file_size": file_size,
"data": file_data
}
hog_files.append(file)
if output_raw:
of = open('./output/' + file_name, 'wb')
of.write(file_data)
of.close()
# Save PCX
if output_backgrounds:
for row in filter(lambda hfile: hfile['type'] == "pcx", hog_files):
image = Image.open(io.BytesIO(row['data']))
image.save('./converted/backgrounds/' + row['file_name'][:-3] + 'png', format="png")
print("Done")