-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreadiffdata.py
More file actions
executable file
·187 lines (141 loc) · 5.15 KB
/
Copy pathreadiffdata.py
File metadata and controls
executable file
·187 lines (141 loc) · 5.15 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
#!/usr/bin/env python3
import argparse
import os
from struct import unpack, pack
from pathlib import Path
import json
CHUNK_W_ENTRIES = [ b'AUDO', \
b'TXTR', \
b'EXTN', \
b'SOND', \
b'SPRT', \
b'BGND', \
b'PATH', \
b'SCPT', \
b'SHDR', \
b'FONT', \
b'TMLN', \
b'OBJT', \
b'ROOM', \
b'TPAG', \
b'CODE', \
b'VARI', \
b'FUNC', \
b'STRG'
]
AUDO_DIR=Path("./audo")
def pretty_size(size):
units = ['B ','KB','MB','GB']
n = size
while n > 1024:
n = n / 1024
units = units[1:]
return f"{int(n):#4} {units[0]}"
def read_chunk(fin,args):
global CHUNK_W_ENTRIES
chunk_offset = fin.tell()
chunk_token = fin.read(4)
chunk_size = unpack('<I',fin.read(4))[0]
print(f"{chunk_token.decode('ascii')} size: {pretty_size(chunk_size)} ({chunk_size:#10x}) offset: {chunk_offset:#10x}")
if args.extract and chunk_token.decode('ascii') in args.extract:
read_chunk_entries(fin,chunk_token,chunk_size,args, True)
elif chunk_token in CHUNK_W_ENTRIES and args.moreinfo > 0:
read_chunk_entries(fin,chunk_token,chunk_size,args, False)
elif chunk_token != b'FORM':
# Go to the end of the chunk
# We don't want to do this for FORM as we will
# reach the end of the file without parsing
# the embedded chunks
fin.seek(chunk_size,1)
def read_chunk_entries(fin,token,size,args, extract):
chunk_nbentries = unpack('<I',fin.read(4))[0]
print(f"`-entries: {chunk_nbentries:#7} ({chunk_nbentries:#10x})")
if args.moreinfo < 2 and not extract:
fin.seek(size-4,1)
elif token == b'AUDO':
read_audo_entries(fin,size,chunk_nbentries,args, extract)
elif token == b'SOND':
read_sond_entries(fin,size,chunk_nbentries,args, extract)
else:
fin.seek(size-4,1)
return
def read_str(fin,offset):
if offset == 0x0:
return ""
cur_offset=fin.tell()
fin.seek(offset-4)
text=fin.read(unpack('<I',fin.read(4))[0]).decode('utf-8')
fin.seek(cur_offset)
return text
def read_sond_entries(fin,size,nbentries,args, extract):
entry_table_offset = fin.tell()
offset_table = []
for i in range(nbentries):
offset_table.append(unpack('<I',fin.read(4))[0])
sounds = {}
for i,offset in enumerate(offset_table):
fin.seek(offset)
name = read_str(fin,unpack('<I',fin.read(4))[0])
flags = unpack('<I',fin.read(4))[0]
type = read_str(fin,unpack('<I',fin.read(4))[0])
file = read_str(fin,unpack('<I',fin.read(4))[0])
[ effect, volume, pitch, audiogroup, audiofile ] = \
unpack('<IffII', fin.read(20))
key=f"{i:#04}"
sounds[key] = {
"name" : name,
"flags" : f"{flags:#4x}",
"type" : type,
"file" : file,
"effect" : effect,
"volume" : volume,
"pitch" : pitch,
"audiogroup" : audiogroup,
"audiofile" : audiofile
}
print(f" {key} -> {sounds[key]}")
if extract:
with open("sond.json", 'w', encoding='utf-8') as jsonfile:
jsonfile.write(json.dumps(sounds, indent=4))
fin.seek(entry_table_offset + size - 4)
def read_audo_entries(fin,size,nbentries,args , extract):
global AUDO_DIR
entry_table_offset = fin.tell()
for n in range(nbentries):
fin.seek(entry_table_offset + 4 * n)
entry_offset = unpack('<I',fin.read(4))[0]
fin.seek(entry_offset)
entry_size = unpack('<I',fin.read(4))[0]
entry_head = fin.read(4)
print(f" {n:#6} -> {entry_head.decode('ascii')} size: {pretty_size(entry_size)} ({entry_size:#10x}) offset: {entry_offset:#10x}")
if extract:
with open(AUDO_DIR / f"{n}.{get_data_extension(entry_head)}", 'wb+') as audiofile:
fin.seek(-4,1)
audiofile.write(fin.read(entry_size))
# go at the end of the audio chunk
fin.seek(entry_table_offset + size - 4)
def get_data_extension(head):
extension = "raw"
if head == b'RIFF':
# WAV
extension = "wav"
elif head == b'OggS':
# OGG
extension = "ogg"
return extension
def main():
parser = argparse.ArgumentParser(description='Process IFF data file')
parser.add_argument('-e','--extract', nargs='?', action='append', help='Extract chunk (eg. AUDO, SOND)')
parser.add_argument('-m','--moreinfo', action='count', default=0, help='Get more info on chunks')
parser.add_argument('filepath', help='Input file path')
args = parser.parse_args()
with open(args.filepath,'rb') as inputfile:
inputfile.seek(0, os.SEEK_END)
filesize = inputfile.tell()
inputfile.seek(0)
print(f"Processing: {args.filepath}")
print(f"File size: {pretty_size(filesize)}")
while ( inputfile.tell() < filesize):
read_chunk(inputfile, args)
if __name__ == '__main__':
main()