-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.py
82 lines (74 loc) · 3.12 KB
/
convert.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
73
74
75
76
77
78
79
80
81
82
"""
Convert .dds icons to .png under the `icons/` folder using magick.
"""
from typing import *
import json
import os
import subprocess
from filepaths import Paths
ORIGINAL_DIR = 'icons/original'
BLIZZARD_DIR = 'icons/blizzard'
def main(paths: Paths, fast: bool = True) -> None:
verbose = False
extras = {
'_terran': ['ui_glues_help_armyicon_terran.dds'],
'_protoss': ['ui_glues_help_armyicon_protoss.dds'],
'_zerg': ['ui_glues_help_armyicon_zerg.dds'],
}
with open(paths.workspace, 'r') as fp:
config: dict[str, str] = json.load(fp)
with open(paths.icon_paths, 'r') as fp:
location_info: dict[str, dict] = json.load(fp)
dds_dir = config['dds_files']
mod_dir = config['mod_files']
failures = 0
successes = 0
skipped = 0
no_information = 0
parsed_locations: dict[str, list[str]] = location_info['locations']
parsed_locations.update(extras)
info = {}
items = sorted(parsed_locations)
if not os.path.exists(ORIGINAL_DIR):
os.makedirs(ORIGINAL_DIR)
if not os.path.exists(BLIZZARD_DIR):
os.makedirs(BLIZZARD_DIR)
converted: set[str] = set()
for item_number, item in enumerate(items):
if item_number % 50 == 0:
print(f'{item_number} / {len(items)}')
locations = parsed_locations[item]
if not locations:
no_information += 1
continue
filenames = [os.path.basename(x) for x in locations]
stems = [os.path.splitext(x)[0] for x in filenames]
for location, filename, stem in zip(locations, filenames, stems):
if location.startswith('ap'):
target_dir = ORIGINAL_DIR
source_path = os.path.join(mod_dir, 'Mods/ArchipelagoPlayer.SC2Mod/Base.SC2Assets', location)
else:
target_dir = BLIZZARD_DIR
source_path = os.path.join(dds_dir, filename)
if not os.path.exists(source_path):
print(f'Failure: {source_path} does not exist')
failures += 1
continue
target_path = f'{target_dir}/{stem}.png'
if target_path in converted or (fast and os.path.isfile(target_path)):
skipped += 1
if verbose: print(f'Skipping {target_path} as it is already converted')
else:
retval = subprocess.call(f'magick convert "{source_path}" -define png:exclude-chunk=date,time {target_path}', shell=True)
if retval:
failures += 1
print(f'magick returned non-zero value {retval} trying to convert {source_path}')
continue
successes += 1
converted.add(target_path)
info.setdefault(item, []).append(target_path)
with open(paths.icon_manifest, 'w') as fp:
json.dump(info, fp, indent=1)
print(f'Converted: {successes} | Skipped (duplicate): {skipped} | Failed: {failures} | No path: {no_information} | Items: {len(items)}')
if __name__ == '__main__':
main(Paths())