summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/util/dump_savegame
blob: a29c36c733ef8b19f3378cc78c82395049e1c123 (plain) (blame)
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
#!/usr/local/bin/python
#
# Tool for examining saved games.

import os
import sys
import optparse

try: import crawl.tags
except ImportError:
    print "You need to put crawl-ref/source/python in your PYTHONPATH; I'll try."
    try: sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), "../python"))
    except: pass
    import crawl.tags


def process_zip(opts, fn):
    """Process one or more sub-files from a .zip saved game."""
    if fn.lower().endswith('zip'):
        import zipfile
        from cStringIO import StringIO
        zip = zipfile.ZipFile(fn, 'r')
        for n in zip.namelist():
            # Ignore non-tag files for now
            if ( n.endswith('.kil') or n.endswith('.nts') or n.endswith('.tc')
                 or n.endswith('.tut') or n.endswith('.st')):
                #print "Skipping %s" % n
                continue
                
            print "Reading %s" % n
            try:
                save = crawl.tags.TaggedFile(StringIO(zip.read(n)))
            except Exception, e:
                print "  Failed (not a tag file?)"
                raise

            process_file(opts, save)


def process_file(opts, save):
    """Process a file; a portion of a saved game.
    Pass a TaggedFile instance"""
    # Not much going on here yet
    TAG_YOU = crawl.tags.tags_enum.s2i['TAG_YOU']
    if TAG_YOU in save.tags:
        print "Dumping YOU's quiver:"
        you = save.tags[TAG_YOU]
        you.quiver.dump()
        print


def main(args):
    parser = optparse.OptionParser()
    parser.add_option("-p", dest='player', help='If .zip, process the player (.sav) portion',
                      action='store_true')
    opts, args = parser.parse_args(args)

    for fn in args:
        if fn.lower().endswith('zip'):
            process_zip(opts, fn)
        else:
            process_file(opts, crawl.tags.TaggedFile(fn))


if __name__=='__main__':
    main(sys.argv[1:])