root/ndstool.py

Revision 35, 3.7 kB (checked in by jmoiron, 1 year ago)
  • various improvements and fixes including making identification and CRC checking more sane

There's no longer some insane group of identification algorithms. If the CRC is incorrect,
we crap out. Eventually, we will peek in at the header for the XXXX UID.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env python
2
3 import sys, os
4 from romutil import log
5 from romutil import uromkan
6 from romutil import fsutil
7 from romutil import ndsheader
8 from romutil import arclib
9 from optparse import OptionParser, OptionGroup
10
11 ROMTYPES = ['rar', 'zip', '7z', 'nds']
12 ARCHIVETYPES = ['rar', 'zip', '7z']
13
14 class _main:
15     def __init__(self):
16         import sys
17         self.usage =  "usage: %prog [options] file.nds ..."
18         self.version = "%prog 0.1"
19         parser = OptionParser(usage=self.usage, version=self.version)
20         parser.add_option('-i', '--info', action='store_true', dest='info', help='Header information')
21         parser.add_option('-f', '--fix-crc', action='store_true', dest='fixcrc', help='Fix the header CRC.')
22         parser.add_option('-s', '--secure', type='choice', choices=['e', 'd'],
23                           action='store', dest='secure', help='[e|d] Encrypt/Decrypt secure area.')
24
25         fsoptg = OptionGroup(parser, "FAT Filesystem options:",
26                                   "Options dealing with the FAT filesystem"
27                                   "within NDS roms.")
28         fsoptg.add_option('-l', '--list', action='store_true', dest='list', help='List contained files')
29         fsoptg.add_option('-c', '--create', action='store_true', dest='create', help='Create files')
30         fsoptg.add_option('-x', '--extract', action='store_true', dest='extract', help='Extract files')
31
32         generl = OptionGroup(parser, "General options:",
33                                   "Options that are usable with multiple other"
34                                   "options.")
35
36         generl.add_option('-v', '--verbose', action='count', dest='verbose', help='Increase verbosity of output')
37         generl.add_option('-d', '--debug', action='store_true', dest='debug', help='Enable debug output')
38         generl.add_option('-t', '--translate', action='store_true', dest='translate', help='Translate Japanese Characters to Romaji where possible')
39
40         parser.add_option_group(fsoptg)
41         parser.add_option_group(generl)
42
43         (options, args) = parser.parse_args()
44         self.options, self.args = options, args
45
46         if not len(args):
47             parser.print_help()
48             sys.exit(1)
49
50         log.options['debug'] = options.debug
51
52         files = fsutil.paths_to_list(args)
53         self.resolve_conflicting_options()
54
55         if options.info:
56             self.doInfo(fsutil.filter_by_types(args, ROMTYPES))
57
58     def resolve_conflicting_options(self):
59         options = self.options
60         if options.create and options.extract:
61             self.error("-c and -x conflict")
62         if options.list and (options.create or options.extract):
63             self.error("-l and -c/-x conflict")
64         if options.info and (options.fixcrc or options.secure):
65             self.error("-i and -f/-s conflict")
66
67     def doInfo(self, files):
68         for f in files:
69             ext = fsutil.extension_of(f)
70             if ext in ARCHIVETYPES:
71                 ndsrom = arclib.extract(f, '*.nds')[0]
72             else:
73                 ndsrom = open(f, 'rb')
74             header = ndsheader.NdsHeader(ndsrom)
75             info = header.ndstool_info()
76             self.safep(info)
77
78     def doFixCrc(self, files):
79         # fix the internal CRC's first, then fix the overall header crc
80         # Internal CRC's:  secure_area_crc, logo_crc
81         pass
82
83     def error(self, text='(appologies, no error text for this error)'):
84         print >>sys.stderr, "Error: " + text + '.'
85         sys.exit(0)
86
87     def safep(self, text):
88         if self.options.translate:
89             text = uromkan.dekana(text)
90         if sys.platform.startswith('win'):
91             text = uromkan.unistr(text)
92             text = text.encode('mbcs')
93         print text
94
95 if __name__ == "__main__":
96     _main()
Note: See TracBrowser for help on using the browser.