-
Notifications
You must be signed in to change notification settings - Fork 4
/
musicdata_tool.py
301 lines (227 loc) · 10.1 KB
/
musicdata_tool.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
import argparse
import ctypes
import json
import sys
import struct
def read_string(infile, length, encoding='shift-jis'):
return infile.read(length).decode(encoding).strip('\0')
def write_string(outfile, input, length, fill='\0', encoding='shift-jis'):
string_data = input[:length].encode(encoding)
outfile.write(string_data)
if len(input) < length:
outfile.write("".join([fill] * (length - len(string_data))).encode(encoding))
# Cannonballers reader/writer
def reader_19(infile, song_count):
song_entries = []
for i in range(song_count):
title = read_string(infile, 0x40)
title_ascii = read_string(infile, 0x40)
genre = read_string(infile, 0x40)
artist = read_string(infile, 0x40)
texture_title, texture_artist, texture_genre, texture_load, texture_list = struct.unpack("<IIIII", infile.read(20))
font_idx, game_version = struct.unpack("<IH", infile.read(6))
other_folder, bemani_folder, splittable_diff = struct.unpack("<HHH", infile.read(6))
difficulties = [x for x in infile.read(8)]
unk_sect1 = infile.read(0xa0)
song_id, volume = struct.unpack("<II", infile.read(8))
file_identifiers = [x for x in infile.read(8)]
bga_delay = ctypes.c_short(struct.unpack("<H", infile.read(2))[0]).value
unk_sect2 = infile.read(2)
bga_filename = read_string(infile, 0x20)
afp_flag = struct.unpack("<I", infile.read(4))[0]
afp_data = []
for x in range(10):
afp_data.append(infile.read(0x20).hex())
song_entries.append({
'song_id': song_id,
'title': title,
'title_ascii': title_ascii,
'genre': genre,
'artist': artist,
'texture_title': texture_title,
'texture_artist': texture_artist,
'texture_genre': texture_genre,
'texture_load': texture_load,
'texture_list': texture_list,
'font_idx': font_idx,
'game_version': game_version,
'other_folder': other_folder,
'bemani_folder': bemani_folder,
'splittable_diff': splittable_diff,
'difficulties': difficulties,
'volume': volume,
'file_identifiers': file_identifiers,
'bga_filename': bga_filename,
'bga_delay': bga_delay,
'afp_flag': afp_flag,
'afp_data': afp_data,
'unk_sect1': unk_sect1.hex(),
'unk_sect2': unk_sect2.hex(),
})
return song_entries
def writer_19(outfile, data):
DATA_VERSION = 25
MAX_ENTRIES = 26000
CUR_STYLE_ENTRIES = MAX_ENTRIES - 1000
# Write header
outfile.write(b"IIDX")
outfile.write(struct.pack("<IHHI", DATA_VERSION, len(data), MAX_ENTRIES, 0))
# Write song index table
exist_ids = {}
for song_data in data:
exist_ids[song_data['song_id']] = True
cur_song = 0
for i in range(MAX_ENTRIES):
if i in exist_ids:
outfile.write(struct.pack("<H", cur_song))
cur_song += 1
elif i >= CUR_STYLE_ENTRIES:
outfile.write(struct.pack("<H", 0x0000))
else:
outfile.write(struct.pack("<H", 0xffff))
# Write song entries
for song_data in data:
write_string(outfile, song_data['title'], 0x40)
write_string(outfile, song_data['title_ascii'], 0x40)
write_string(outfile, song_data['genre'], 0x40)
write_string(outfile, song_data['artist'], 0x40)
outfile.write(struct.pack("<IIIII", song_data['texture_title'], song_data['texture_artist'], song_data['texture_genre'], song_data['texture_load'], song_data['texture_list']))
outfile.write(struct.pack("<IH", song_data['font_idx'], song_data['game_version']))
outfile.write(struct.pack("<HHH", song_data['other_folder'], song_data['bemani_folder'], song_data['splittable_diff']))
for difficulty in song_data['difficulties']:
outfile.write(struct.pack("<B", difficulty))
outfile.write(bytes.fromhex(song_data['unk_sect1']))
outfile.write(struct.pack("<II", song_data['song_id'], song_data['volume']))
for ident in song_data['file_identifiers']:
outfile.write(struct.pack("<B", ident))
outfile.write(struct.pack("<h", song_data['bga_delay']))
outfile.write(bytes.fromhex(song_data['unk_sect2']))
write_string(outfile, song_data['bga_filename'], 0x20)
outfile.write(struct.pack("<I", song_data['afp_flag']))
for afp_data in song_data['afp_data']:
outfile.write(bytes.fromhex(afp_data))
read_handlers = {
0x19: reader_19,
}
write_handlers = {
0x19: writer_19,
}
def extract_file(input, output, in_memory=False):
with open(input, "rb") as infile:
if infile.read(4) != b"IIDX":
print("Invalid", input)
exit(-1)
data_ver, available_entries, total_entries, unk4 = struct.unpack("<IHIH", infile.read(12))
song_ids = {}
for i in range(total_entries):
song_id = struct.unpack("<H", infile.read(2))[0]
if song_id != 0xffff and (len(song_ids) == 0 or song_id != 0):
song_ids[i] = song_id
if data_ver in read_handlers:
output_data = read_handlers[data_ver](infile, available_entries)
output_data = {
'data_ver': data_ver,
'data': output_data,
}
if in_memory:
return output_data
json.dump(output_data, open(output, "w", encoding="utf8"), indent=4, ensure_ascii=False)
else:
print("Couldn't find a handler for this data version")
exit(-1)
return []
def create_file(input, output, data_version):
data = json.load(open(input, "r", encoding="utf8"))
data_ver = data.get('data_ver', data_version)
if not data_ver:
print("Couldn't find data version")
exit(-1)
if data_ver in write_handlers:
write_handlers[data_ver](open(output, "wb"), data['data'])
else:
print("Couldn't find a handler for this data version")
exit(-1)
def convert_file(input, output, data_version):
with open(input, "rb") as infile:
if infile.read(4) != b"IIDX":
print("Invalid", input)
exit(-1)
data_ver, available_entries, total_entries, unk4 = struct.unpack("<IHIH", infile.read(12))
song_ids = {}
for i in range(total_entries):
song_id = struct.unpack("<H", infile.read(2))[0]
if song_id != 0xffff and (len(song_ids) == 0 or song_id != 0):
song_ids[i] = song_id
if data_ver in read_handlers:
output_data = read_handlers[data_ver](infile, available_entries)
write_handlers[data_version](open(output, "wb"), output_data)
else:
print("Couldn't find a handler for this input data version")
exit(-1)
def merge_files(input, basefile, output):
with open(input, "rb") as infile:
if infile.read(4) != b"IIDX":
print("Invalid", input)
exit(-1)
data_ver, available_entries, total_entries, unk4 = struct.unpack("<IHIH", infile.read(12))
song_ids = {}
for i in range(total_entries):
song_id = struct.unpack("<H", infile.read(2))[0]
if song_id != 0xffff and (len(song_ids) == 0 or song_id != 0):
song_ids[i] = song_id
if data_ver in read_handlers:
old_data = read_handlers[data_ver](infile, available_entries)
else:
print("Couldn't find a handler for this input data version")
exit(-1)
with open(basefile, "rb") as infile:
if infile.read(4) != b"IIDX":
print("Invalid", basefile)
exit(-1)
data_ver, available_entries, total_entries, unk4 = struct.unpack("<IHIH", infile.read(12))
song_ids = {}
for i in range(total_entries):
song_id = struct.unpack("<H", infile.read(2))[0]
if song_id != 0xffff and (len(song_ids) == 0 or song_id != 0):
song_ids[i] = song_id
if data_ver in read_handlers:
new_data = read_handlers[data_ver](infile, available_entries)
else:
print("Couldn't find a handler for this input data version")
exit(-1)
# Create list of
exist_ids_new = {}
for song_data in new_data:
exist_ids_new[song_data['song_id']] = True
for song_data in old_data:
if song_data['song_id'] not in exist_ids_new:
new_data.append(song_data)
write_handlers[data_ver](open(output, "wb"), new_data)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--input', help='Input file', required=True)
parser.add_argument('--output', help='Output file', required=True)
parser.add_argument('--extract', help='Extraction mode', default=False, action='store_true')
parser.add_argument('--create', help='Creation mode', default=False, action='store_true')
parser.add_argument('--convert', help='Conversion mode', default=False, action='store_true')
parser.add_argument('--merge', help='Merge mode', default=False, action='store_true')
parser.add_argument('--data-version', help='Force a data version (usedful for converts)', default=None, type=int)
args = parser.parse_args()
if args.create == False and args.extract == False and args.convert == False and args.merge == False:
print("You must specify either --extract or --create or --convert or --merge")
exit(-1)
if args.convert == True:
if args.data_version == None:
print("You must specify a target --data-version with --convert")
exit(-1)
elif args.data_version not in write_handlers:
print("Don't know how to handle specified data version")
exit(-1)
if args.extract:
extract_file(args.input, args.output)
elif args.create:
create_file(args.input, args.output, args.data_version)
elif args.convert:
convert_file(args.input, args.output, args.data_version)
elif args.merge:
merge_files(args.input, args.output, args.output)