-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwiimetadata.py
executable file
·398 lines (344 loc) · 11.8 KB
/
wiimetadata.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
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
#!/usr/bin/env python
# Author: Bryan Cain (Plombo)
# Date: December 27, 2010
# Description: Reads Wii title metadata from an extracted NAND dump.
# Thanks to Leathl for writing Wii.cs in ShowMiiWads, which was an important
# reference in writing this program.
import os, os.path, struct, shutil
from cStringIO import StringIO
import romc, gensave, n64save
from u8archive import U8Archive
from ccfarchive import CCFArchive
from nes_rom_extract import extract_nes_rom
from snesrestore import restore_brr_samples
# rom: file-like object
# path: string (filesystem path)
def writerom(rom, path):
f = open(path, 'wb')
f.write(rom.read())
f.close()
rom.seek(0)
class RomExtractor(object):
# file extensions for ROMs
extensions = {
'Nintendo 64': '.z64',
'Genesis': '.gen',
'Master System': '.sms',
'NES': '.nes',
'SNES': '.smc',
'TurboGrafx16': '.pce'
}
def __init__(self, id, name, channeltype, nand):
self.id = id
self.name = name
self.channeltype = channeltype
self.nand = nand
def extract(self):
content = os.path.join(self.nand.path, 'title', '00010001', self.id, 'content')
rom_extracted = False
manual_extracted = False
for app in os.listdir(content):
if not app.endswith('.app'): continue
app = os.path.join(content, app)
if self.extractrom(app): rom_extracted = True
if self.extractmanual(app): manual_extracted = True
if rom_extracted and manual_extracted: return
if rom_extracted: print 'Unable to extract manual.'
elif manual_extracted: print 'Unable to extract ROM.'
else: print 'Unable to extract ROM and manual.'
# Actually extract the ROM
# Currently works for almost all NES, SNES, N64, TG16, Master System, and Genesis ROMs.
def extractrom(self, u8path):
funcs = {
'Nintendo 64': self.extractrom_n64,
'Genesis': self.extractrom_sega,
'Master System': self.extractrom_sega,
'NES': self.extractrom_nes,
'SNES': self.extractrom_snes,
'TurboGrafx16': self.extractrom_tg16
}
if self.channeltype == 'NES':
arc = u8path
else:
try:
arc = U8Archive(u8path)
if not arc: return False
except AssertionError:
return False
if self.channeltype in funcs.keys():
return funcs[self.channeltype](arc, self.name + self.extensions[self.channeltype])
else:
return False
# FIXME: use string instead of StringIO
def extractrom_nes(self, u8path, filename):
if not os.path.exists(u8path): return False
f = open(u8path, 'rb')
rom = extract_nes_rom(f)
f.close()
if not rom: return False
# make sure save flag is set if the game has save data
if self.extractsave():
if not (ord(rom.getvalue()[6]) & 2):
rom = list(rom.getvalue())
rom[6] = chr(ord(rom[6]) | 2)
rom = StringIO(''.join(rom))
print 'Set the save flag to true'
print 'Got ROM: %s' % filename
writerom(rom, filename)
return True
def extractrom_n64(self, arc, filename):
if arc.hasfile('rom'):
rom = arc.getfile('rom')
print 'Got ROM: %s' % filename
writerom(rom, filename)
elif arc.hasfile('romc'):
rom = arc.getfile('romc')
print 'Decompressing ROM: %s (this could take a minute or two)' % filename
try:
romdata = romc.decompress(rom)
outfile = open(filename, 'wb')
outfile.write(romdata)
outfile.close()
print 'Got ROM: %s' % filename
except IndexError: # unknown compression - something besides LZSS and romchu?
print 'Decompression failed: unknown compression type'
outfile.close()
os.remove(filename)
return False
else: return False
# extract save file
savepath = self.extractsave()
if savepath: print 'Extracted save file(s)'
else: print 'Failed to extract save file(s)'
return True
def extractrom_sega(self, arc, filename):
if arc.hasfile('data.ccf'):
ccf = CCFArchive(arc.getfile('data.ccf'))
if ccf.hasfile('config'):
for line in ccf.getfile('config'):
if line.startswith('romfile='): romname = line[len('romfile='):].strip('/\\\"\0\r\n')
else:
print 'config not found'
return False
if romname:
print 'Found ROM: %s' % romname
rom = ccf.find(romname)
writerom(rom, filename)
print 'Got ROM: %s' % filename
if self.extractsave(): print 'Extracted save to %s.srm' % self.name
else: print 'No save file found'
return True
else:
print 'ROM filename not specified in config'
return False
def extractrom_tg16(self, arc, filename):
config = arc.getfile('config.ini')
if not config:
print 'config.ini not found'
return False
path = None
for line in config:
if line.startswith('ROM='):
path = line[len('ROM='):].strip('/\\\"\0\r\n')
if not path:
print 'ROM filename not specified in config.ini'
return False
print 'Found ROM: %s' % path
rom = arc.getfile(path)
if rom:
writerom(rom, filename)
print 'Got ROM: %s' % filename
return True
else: return False
def extractrom_snes(self, arc, filename):
extracted = False
# try to find the original ROM first
for f in arc.files:
path = f.path.split('.')
if len(path) == 2 and path[0].startswith('SN') and path[1].isdigit():
print 'Found original ROM: %s' % f.path
rom = arc.getfile(f.path)
writerom(rom, filename)
print 'Got ROM: %s' % filename
extracted = True
# if original ROM not present, try to create a playable ROM by recreating and injecting the original sounds
if not extracted:
for f in arc.files:
path = f.path.split('.')
if len(path) == 2 and path[1] == 'rom':
print "Recreating original ROM from %s" % f.path
vcrom = arc.getfile(f.path)
if not vcrom: print "Error in reading ROM file %s" % f.path; return False
# find raw PCM data
pcm = None
for f2 in arc.files:
path2 = f2.path.split('.')
if len(path2) == 2 and path2[1] == 'pcm':
pcm = arc.getfile(f2.path)
if not pcm: print 'Error: PCM audio data not found'; return False
'''# encode raw PCM in SNES BRR format
print 'Encoding audio as BRR'
brr = StringIO()
enc = BRREncoder(pcm, brr)
enc.encode()
pcm.close()'''
# inject BRR audio into the ROM
print 'Encoding and restoring BRR audio data to ROM'
romdata = restore_brr_samples(vcrom, pcm)
vcrom.close()
pcm.close()
# write the recreated ROM to disk
f = open(filename, 'wb')
f.write(romdata)
f.close()
print 'Got ROM: %s' % filename
extracted = True
# extract save data (but don't overwrite existing save data)
if extracted:
srm = filename[0:filename.rfind('.smc')] + '.srm'
if os.path.lexists(srm): print 'Not overwriting existing save data'
elif self.extractsave(): print 'Extracted save data to %s' % srm
else: print 'Could not extract save data'
return extracted
# copy save file, doing any necessary conversions to common emulator formats
def extractsave(self):
datadir = os.path.join(self.nand.path, 'title', '00010001', self.id, 'data')
datafiles = os.listdir(datadir)
for filename in datafiles:
path = os.path.join(datadir, filename)
if filename == 'savedata.bin':
if self.channeltype == 'SNES':
# VC SNES saves are standard SRM files
outpath = self.name + '.srm'
shutil.copy2(path, outpath)
return True
elif self.channeltype == 'NES':
# VC NES saves use the same format as FCEUX, except with an
# additional 64-byte header
outpath = self.name + '.sav'
infile = open(path, 'rb')
outfile = open(outpath, 'wb')
infile.seek(64)
outfile.write(infile.read())
outfile.close()
infile.close()
return True
elif self.channeltype == 'Genesis':
# VC Genesis saves use a slightly different format from
# the one used by Gens/GS and other emulators
outpath = self.name + '.srm'
gensave.convert(path, outpath)
return True
elif filename.startswith('EEP_') or filename.startswith('RAM_'):
assert self.channeltype == 'Nintendo 64'
n64save.convert(path, self.name)
return True
return False
def extractmanual(self, u8path):
try:
arc = U8Archive(u8path)
if not arc: return False
except AssertionError:
return False
man = None
try:
if arc.findfile('emanual.arc'):
man = U8Archive(arc.getfile(arc.findfile('emanual.arc')))
elif arc.findfile('html.arc'):
man = U8Archive(arc.getfile(arc.findfile('html.arc')))
elif arc.findfile('man.arc'):
man = U8Archive(arc.getfile(arc.findfile('man.arc')))
elif arc.findfile('data.ccf'):
ccf = CCFArchive(arc.getfile(arc.findfile('data.ccf')))
man = U8Archive(ccf.getfile('man.arc'))
elif arc.findfile('htmlc.arc'):
manc = arc.getfile(arc.findfile('htmlc.arc'))
print 'Decompressing manual: htmlc.arc'
man = U8Archive(StringIO(romc.decompress(manc)))
except AssertionError: pass
if man:
man.extract(os.path.join('manuals', self.name))
print 'Extracted manual to ' + os.path.join('manuals', self.name)
return True
return False
class NandDump(object):
# path: path on filesystem to the extracted NAND dump
def __init__(self, path):
self.path = path + '/'
def scantickets(self):
tickets = os.listdir(os.path.join(self.path, 'ticket', '00010001'))
for ticket in tickets:
id = ticket.rstrip('.tik')
content = os.path.join('title', '00010001', id, 'content')
title = os.path.join(content, 'title.tmd')
if(os.path.exists(os.path.join(self.path, title))):
appname = self.getappname(title)
if not appname: continue
#print title, content + appname
name = self.gettitle(os.path.join(content, appname))
channeltype = self.channeltype(ticket)
if name and channeltype:
print '%s: %s (ID: %s)' % (channeltype, name, id)
ext = RomExtractor(id, name, channeltype, self)
ext.extract()
print
# Returns a string denoting the channel type. Returns None if it's not a VC game.
def channeltype(self, ticket):
f = open(os.path.join(self.path, 'ticket', '00010001', ticket), 'rb')
f.seek(0x1dc)
thistype = struct.unpack('>I', f.read(4))[0]
if thistype != 0x10001: return None
f.seek(0x221)
if struct.unpack('>B', f.read(1))[0] != 1: return None
f.seek(0x1e0)
ident = f.read(2)
# TODO: support the commented game types
if ident[0] == 'F': return 'NES'
elif ident[0] == 'J': return 'SNES'
elif ident[0] == 'L': return 'Master System'
elif ident[0] == 'M': return 'Genesis'
elif ident[0] == 'N': return 'Nintendo 64'
elif ident[0] == 'P': return 'TurboGrafx16'
#elif ident == 'EA': return 'Neo Geo'
#elif ident[0] == 'E': return 'Arcade'
#elif ident[0] == 'Q': return 'TurboGrafx CD'
#elif ident[0] == 'C': return 'Commodore 64'
else: return None
# Returns the path to the 00.app file containing the game's title
# Precondition: the file denoted by "title" exists on the filesystem
def getappname(self, title):
f = open(os.path.join(self.path, title), 'rb')
f.seek(0x1de)
count = struct.unpack('>H', f.read(2))[0]
f.seek(0x1e4)
appname = None
for i in range(count):
info = struct.unpack('>IHHQ', f.read(16))
f.read(20)
if info[1] == 0:
appname = '%08x.app' % info[0]
return appname
# Gets title (in English) from a 00.app file
def gettitle(self, path):
path = os.path.join(self.path, path)
if not os.path.exists(path): return None
f = open(path, 'rb')
data = f.read()
f.close()
index = data.find('IMET')
if index < 0: return None
engindex = index + 29 + 84
title = data[engindex:engindex+84]
# Format the title properly
title = title.strip('\0')
while title.find('\0\0\0') >= 0: title = title.replace('\0\0\0', '\0\0')
title = title.replace('\0\0', ' - ')
title = title.replace('\0', '')
title = title.replace(':', ' - ')
while title.find(' ') >= 0: title = title.replace(' ', ' ')
return title
if __name__ == '__main__':
import sys
nand = NandDump(sys.argv[1])
nand.scantickets()
if len(sys.argv) >= 3: print nand.gettitle(sys.argv[2])