forked from Dentosal/python-sc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_id_constants_from_stableid.py
142 lines (106 loc) · 3.43 KB
/
generate_id_constants_from_stableid.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import json
from pathlib import Path
import platform
HEADER = f"# DO NOT EDIT!\n# This file was automatically generated by \"{__file__}\"\n"
PF = platform.system()
HOME_DIR = str(Path.home())
DATA_JSON = {
"Darwin": HOME_DIR + "/Library/Application Support/Blizzard/StarCraft II/stableid.json",
"Windows": HOME_DIR + "/Documents/StarCraft II/stableid.json"
}
ENUM_TRANSLATE = {
"Units": "UnitTypeId",
"Abilities": "AbilityId",
"Upgrades": "UpgradeId",
"Buffs": "BuffId",
"Effects": "EffectId"
}
FILE_TRANSLATE = {
"Units": "unit_typeid",
"Abilities": "ability_id",
"Upgrades": "upgrade_id",
"Buffs": "buff_id",
"Effects": "effect_id"
}
def make_key(key):
if key[0].isdigit():
key = "_" + key
return key.upper().replace(" ", "_")
def parse_data(data):
# for d in data: # Units, Abilities, Upgrades, Buffs, Effects
units = parse_simple("Units", data)
upgrades = parse_simple("Upgrades", data)
effects = parse_simple("Effects", data)
buffs = parse_simple("Buffs", data)
abilities = {}
for v in data["Abilities"]:
key = v["buttonname"]
remapid = v.get("remapid")
if (not key) and (remapid is None):
assert v["buttonname"] == ""
continue
if not key:
if v["friendlyname"] != "":
key = v["friendlyname"]
else:
exit(f"Not mapped: {v !r}")
key = key.upper().replace(" ", "_")
if "name" in v:
key = "{}_{}".format(v["name"].upper().replace(" ", "_"), key)
if "friendlyname" in v:
key = v["friendlyname"].upper().replace(" ", "_")
if key[0].isdigit():
key = "_" + key
if key in abilities and v["index"] == 0:
print(key)
raise ValueError
abilities[key] = v["id"]
abilities["SMART"] = 1
enums = {}
enums["Units"] = units
enums["Abilities"] = abilities
enums["Upgrades"] = upgrades
enums["Buffs"] = buffs
enums["Effects"] = effects
return enums
def parse_simple(d, data):
units = {}
for v in data[d]:
key = v["name"]
if not key:
continue
units[make_key(key)] = v["id"]
return units
def generate_python_code(enums):
assert {"Units", "Abilities", "Upgrades", "Buffs", "Effects"} <= enums.keys()
sc2dir = Path("sc2/")
idsdir = (sc2dir / "ids")
idsdir.mkdir(exist_ok=True)
with (idsdir / "__init__.py").open("w") as f:
f.write("\n".join([
HEADER,
f"__all__ = {[n.lower() for n in FILE_TRANSLATE.values()] !r}\n"
]))
for name, body in enums.items():
class_name = ENUM_TRANSLATE[name]
code = [
HEADER,
"import enum",
"",
f"class {class_name}(enum.Enum):"
]
for key, value in sorted(body.items(), key=lambda p: p[1]):
code.append(f" {key} = {value}")
code += [
"",
f"for item in {class_name}:",
f" assert not item.name in globals()",
f" globals()[item.name] = item",
""
]
with (idsdir / FILE_TRANSLATE[name]).with_suffix(".py").open("w") as f:
f.write("\n".join(code))
if __name__ == "__main__":
with open(DATA_JSON[PF], encoding="utf-8") as data_file:
data = json.loads(data_file.read())
generate_python_code(parse_data(data))