Skip to content

Commit ebcf1ff

Browse files
author
Reynald Pader
committed
Merge branch 'use-stableid-json'
2 parents e98cd55 + 9471626 commit ebcf1ff

File tree

10 files changed

+3263
-472
lines changed

10 files changed

+3263
-472
lines changed

examples/zerg_rush.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async def on_step(self, iteration):
3434
for queen in self.units(QUEEN).idle:
3535
abilities = await self.get_available_abilities(queen)
3636
if AbilityId.INJECTLARVA in abilities:
37-
await self.do(queen(INJECTLARVA, hatchery))
37+
await self.do(queen(EFFECT_INJECTLARVA, hatchery))
3838

3939
if self.vespene >= 100:
4040
sp = self.units(SPAWNINGPOOL).ready

generate_id_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def clike_enum_parse(code):
1616

1717
# parse enum blocks
1818
enums = {}
19-
for enum in re.findall(r"enum(?: class)? ([a-zA-Z_][a-zA-Z0-9_]*) \{\s?(.+?)\s?}", code):
19+
for enum in re.findall(r"enum(?: class)? ([a-zA-Z_][a-zA-Z0-9_]*) {\s?(.+?)\s?}", code):
2020
name, body = enum
2121
body = {key: int(value) for key, value in re.findall(r"([a-zA-Z_][a-zA-Z0-9_]*) = (\d+),?\s?", body)}
2222
enums[name] = body
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import json
2+
from pathlib import Path
3+
import platform
4+
5+
HEADER = f"# DO NOT EDIT!\n# This file was automatically generated by \"{__file__}\"\n"
6+
7+
PF = platform.system()
8+
9+
HOME_DIR = str(Path.home())
10+
DATA_JSON = {
11+
"Darwin": HOME_DIR + "/Library/Application Support/Blizzard/StarCraft II/stableid.json",
12+
"Windows": HOME_DIR + "/Documents/StarCraft II/stableid.json"
13+
}
14+
15+
ENUM_TRANSLATE = {
16+
"Units": "UnitTypeId",
17+
"Abilities": "AbilityId",
18+
"Upgrades": "UpgradeId",
19+
"Buffs": "BuffId",
20+
"Effects": "EffectId"
21+
}
22+
23+
FILE_TRANSLATE = {
24+
"Units": "unit_typeid",
25+
"Abilities": "ability_id",
26+
"Upgrades": "upgrade_id",
27+
"Buffs": "buff_id",
28+
"Effects": "effect_id"
29+
}
30+
31+
32+
def clike_enum_parse(data):
33+
# for d in data: # Units, Abilities, Upgrades, Buffs, Effects
34+
35+
units = parse_simple('Units', data)
36+
upgrades = parse_simple('Upgrades', data)
37+
effects = parse_simple('Effects', data)
38+
buffs = parse_simple('Buffs', data)
39+
40+
abilities = {}
41+
for v in data['Abilities']:
42+
key = v['buttonname']
43+
44+
if not key:
45+
continue
46+
47+
key = key.upper().replace(" ", "_")
48+
49+
if 'name' in v:
50+
key = "{}_{}".format(v['name'].upper().replace(" ", "_"), key)
51+
52+
if 'friendlyname' in v:
53+
key = v['friendlyname'].upper().replace(" ", "_")
54+
55+
if key[0].isdigit():
56+
key = "_" + key
57+
58+
if key in abilities and v['index'] == 0:
59+
print(key)
60+
raise ValueError
61+
abilities[key] = v['id']
62+
63+
abilities['SMART'] = 1
64+
65+
enums = {}
66+
enums["Units"] = units
67+
enums["Abilities"] = abilities
68+
enums["Upgrades"] = upgrades
69+
enums["Buffs"] = buffs
70+
enums["Effects"] = effects
71+
72+
return enums
73+
74+
75+
def parse_simple(d, data):
76+
units = {}
77+
for v in data[d]:
78+
key = v['name']
79+
80+
if not key:
81+
continue
82+
83+
if key[0].isdigit():
84+
key = "_" + key
85+
86+
key = key.upper().replace(" ", "_")
87+
units[key] = v['id']
88+
return units
89+
90+
91+
def generate_python_code(enums):
92+
assert {"Units", "Abilities", "Upgrades", "Buffs", "Effects"} <= enums.keys()
93+
94+
sc2dir = Path("sc2/")
95+
idsdir = (sc2dir / "ids")
96+
idsdir.mkdir(exist_ok=True)
97+
98+
with (idsdir / "__init__.py").open("w") as f:
99+
f.write("\n".join([
100+
HEADER,
101+
f"__all__ = {[n.lower() for n in FILE_TRANSLATE.values()] !r}\n"
102+
]))
103+
104+
for name, body in enums.items():
105+
class_name = ENUM_TRANSLATE[name]
106+
107+
code = [
108+
HEADER,
109+
"import enum",
110+
"",
111+
f"class {class_name}(enum.Enum):"
112+
]
113+
114+
for key, value in sorted(body.items(), key=lambda p: p[1]):
115+
code.append(f" {key} = {value}")
116+
117+
code += [
118+
"",
119+
f"for item in {class_name}:",
120+
f" assert not item.name in globals()",
121+
f" globals()[item.name] = item",
122+
""
123+
]
124+
125+
with (idsdir / FILE_TRANSLATE[name]).with_suffix(".py").open("w") as f:
126+
f.write("\n".join(code))
127+
128+
129+
if __name__ == '__main__':
130+
with open(DATA_JSON[PF], encoding='utf-8') as data_file:
131+
data = json.loads(data_file.read())
132+
generate_python_code(clike_enum_parse(data))

sc2/constants.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
from .ids.unit_typeid import *
21
from .ids.ability_id import *
2+
from .ids.buff_id import *
3+
from .ids.effect_id import *
4+
from .ids.unit_typeid import *
5+
from .ids.upgrade_id import *

sc2/ids/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# DO NOT EDIT!
2-
# This file was automatically generated by "generate_id_constants.py"
2+
# This file was automatically generated by "generate_id_constants_from_stableid.py"
33

4-
__all__ = ['unit_typeid', 'ability_id', 'upgrade_id', 'buff_id']
4+
__all__ = ['unit_typeid', 'ability_id', 'upgrade_id', 'buff_id', 'effect_id']

0 commit comments

Comments
 (0)