Update README.md
[sm64pc.git] / tools / texrename.py
blob600c8bfce480f73d79908e40f452e94de44f23ad
1 #!/usr/bin/env python3
3 import sys
4 import os
5 import shutil
7 if len(sys.argv) < 3:
8 print("usage: texrename <in_dir> <out_dir> [<crcmap_file>]")
9 sys.exit(1)
11 inpath = sys.argv[1]
12 outpath = sys.argv[2]
13 mapfname = "crcmap.txt"
14 if len(sys.argv) > 3:
15 mapfname = sys.argv[3]
17 # catalog the original texture pack
18 texmap = dict()
19 imgexts = frozenset(['.png', '.bmp', '.jpg', '.tga', '.gif'])
20 try:
21 for root, dirs, files in os.walk(inpath):
22 for f in files:
23 ffull = os.path.join(root, f)
24 [fpath, fname] = os.path.split(f)
25 ext = os.path.splitext(fname)[1].lower()
26 if fname[0] == '.' or not (ext in imgexts):
27 continue
28 crc = 0
29 try:
30 if '#' in fname: # rice pack format: "GAME NAME#hash#whatever"
31 crc = int(fname.split('#')[1], 16)
32 else: # just the crc probably
33 crc = int(os.path.splitext(fname)[0], 16)
34 except ValueError:
35 print('unknown filename format: {0}'.format(ffull))
36 continue
37 texmap[crc] = ffull
38 except OSError as e:
39 print('error opening {0}: {1}'.format(inpath, e))
40 sys.exit(2)
42 # load the CRC map
43 crcmap = list()
44 try:
45 with open(mapfname, 'r') as f:
46 for line in f:
47 line = line.strip()
48 if line == '' or line[0] == '#':
49 continue
50 tok = line.split(',')
51 crcstr = tok[0].strip()
52 if crcstr.startswith('0x'):
53 crc = int(crcstr[2:], 16)
54 else:
55 crc = int(crcstr)
56 crcmap.append((crc, os.path.join(outpath, 'gfx', tok[1].strip())))
57 except OSError as e:
58 print('could not open {0}: {1}'.format(mapfname, e))
59 except ValueError as e:
60 print('invalid integer in {0}: {1}'.format(mapfname, e))
61 sys.exit(3)
63 # copy the files to the correct locations
64 for (crc, path) in crcmap:
65 if not (crc in texmap):
66 print('unmatched CRC: {0} ({1})'.format(crc, path))
67 else:
68 [fpath, fname] = os.path.split(path)
69 if not os.path.exists(fpath):
70 os.makedirs(fpath)
71 shutil.copy2(texmap[crc], path)