Merge pull request #438 from s4Ys369/revert-434-patch-1
[sm64pc.git] / tools / cleancrcmap.py
bloba5f343c2355216ae79dbe5a0e5558c9d31483538
1 #!/usr/bin/env python3
3 import sys
4 import os
5 import glob
7 if len(sys.argv) < 4:
8 print("usage: cleancrcmap <in_map> <out_map> <searchdir>")
9 sys.exit(1)
11 # load and check the old map
12 searchpath = sys.argv[3]
13 inmap = list()
14 with open(sys.argv[1], 'r') as f:
15 for line in f:
16 line = line.strip()
17 if line == '' or line[0] == '#':
18 continue
19 tok = line.split(',')
20 crcstr = tok[0].strip()
21 if crcstr.startswith('0x'):
22 crc = int(crcstr[2:], 16)
23 else:
24 crc = int(crcstr)
25 tok[1] = tok[1].strip()
26 [fname, fext] = os.path.splitext(tok[1])
27 [fname, ffmt] = os.path.splitext(fname)
28 fname = fname + ffmt[:-1] + '*'
29 matches = glob.glob(os.path.join(searchpath, fname))
30 if len(matches) == 0:
31 print("warning: texture '{0}' does not match anything in '{1}'".format(fname, searchpath))
32 else:
33 for s in matches:
34 tup = (crc, os.path.relpath(s, searchpath))
35 if not (tup in inmap):
36 inmap.append(tup)
38 # save cleaned up version to the new one
39 with open(sys.argv[2], 'w') as f:
40 for (crc, fpath) in inmap:
41 f.write("0x{0:08x}, {1}\n".format(crc, fpath))