-
Notifications
You must be signed in to change notification settings - Fork 0
/
wadindex.py
executable file
·72 lines (60 loc) · 1.86 KB
/
wadindex.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
#!/usr/bin/env python3
"""
Creates a json file listing all found wads and information about them.
"""
import wad
import os
import argparse
import json
import zipfile
parser = argparse.ArgumentParser()
parser.add_argument('--wadsdir', type=str, help='Path that will be recursively scanned for WADS.')
parser.add_argument('--outf', type=str, help='Path to write JSON index to.')
args = parser.parse_args()
def get_wads():
wadfiles = []
for root, dirs, files in os.walk(args.wadsdir):
for file in files:
if file.lower().endswith('.wad'):
wadfiles.append(os.path.join(root, file))
wadfiles.sort()
return wadfiles
def get_saves():
rv = []
for root, dirs, files in os.walk(args.wadsdir):
for file in files:
if file.lower().endswith('.zds') and file.lower().startswith('save'):
rv.append(os.path.join(root, file))
rv.sort()
return rv
def main():
wads_index = {}
for pwad in get_wads():
wads_index[pwad] = {}
maps = list(wad.enum_map_names(pwad))
if len(maps) > 0:
wads_index[pwad]['maps'] = maps
saves_index = {}
for zdsp in get_saves():
with zipfile.ZipFile(zdsp, mode='r') as zf:
print(f'reading {zdsp}')
def load_json(zp):
jsonstring = zf.read(zp).decode('utf-8')
return json.loads(jsonstring)
g = load_json('globals.json')
played_levels = []
if 'statistics' in g and 'levels' in g['statistics']:
played_levels += [lev['levelname'].upper() for lev in g['statistics']['levels']]
info = load_json('info.json')
if 'Current Map' in info:
played_levels += [info['Current Map'].upper()]
saves_index[zdsp] = {}
saves_index[zdsp]['played_levels'] = played_levels
index = {
'wads': wads_index,
'saves': saves_index,
}
with open(args.outf, 'w') as f:
json.dump(index, f, indent=2)
if __name__ == '__main__':
main()