Generate enums

This commit is contained in:
ChillerDragon 2023-04-16 08:43:37 +02:00
parent 8b0cce28be
commit 1767d05f4e
2 changed files with 157 additions and 9 deletions

View file

@ -21,19 +21,19 @@ KIND = Literal[ \
'flags', \ 'flags', \
'optional'] 'optional']
class GameEnumValueJson(TypedDict):
value: int
name: list[str]
class GameEnumJson(TypedDict):
name: list[str]
values: list[GameEnumValueJson]
class ConstantJson(TypedDict): class ConstantJson(TypedDict):
name: list[str] name: list[str]
type: str type: str
value: int value: int
class NetEnumValuesJson(TypedDict):
value: str
name: list[str]
class NetEnumJson(TypedDict):
name: list[str]
values: list[NetEnumValuesJson]
class InnerNetMessageMemberTypeJson(TypedDict): class InnerNetMessageMemberTypeJson(TypedDict):
kind: KIND kind: KIND
disallow_cc: bool disallow_cc: bool
@ -68,7 +68,7 @@ class NetMessageJson(TypedDict):
class SpecJson(TypedDict): class SpecJson(TypedDict):
constants: list[ConstantJson] constants: list[ConstantJson]
game_enumerations: list[NetEnumJson] game_enumerations: list[GameEnumJson]
game_messages: list[NetMessageJson] game_messages: list[NetMessageJson]
system_messages: list[NetMessageJson] system_messages: list[NetMessageJson]
@ -556,13 +556,39 @@ def get_default(field_path: str) -> Optional[str]:
print(f" please check {def_file} for errors") print(f" please check {def_file} for errors")
exit(1) exit(1)
def gen_enum_file7(enums: list[GameEnumJson]):
enum_code: str = ''
enum: GameEnumJson
for enum in enums:
base: str = name_to_snake(enum['name']).upper()
val: GameEnumValueJson
for val in enum['values']:
sub: str = name_to_snake(val['name']).upper()
enum_code += f"{base}_{sub}: int = {val['value']}\n"
enum_code += "\n"
# cut off last doubled newline
# because we do not split a section anymore
enum_code = enum_code[:-1]
dirname = os.path.dirname(__file__)
file_path= os.path.join(
dirname,
'../twnet_parser/msg_matcher/enum7.py')
# if os.path.exists(file_path):
# print(f"Warning: file already exists! {file_path}")
# return
with open(file_path, 'w') as out_file:
print(f"Generating {file_path} ...")
out_file.write(enum_code)
def generate(spec: str) -> None: def generate(spec: str) -> None:
print(f"generating classes from {spec} ...") print(f"generating classes from {spec} ...")
with open(spec) as spec_io: with open(spec) as spec_io:
spec_data: SpecJson = json.load(spec_io) spec_data: SpecJson = json.load(spec_io)
# for msg in [spec_data['game_messages'][1]]: # for msg in [spec_data['game_messages'][1]]:
game_enums: list[GameEnumJson] = spec_data['game_enumerations']
game_messages: list[NetMessageJson] = spec_data['game_messages'] game_messages: list[NetMessageJson] = spec_data['game_messages']
system_messages: list[NetMessageJson] = spec_data['system_messages'] system_messages: list[NetMessageJson] = spec_data['system_messages']
gen_enum_file7(game_enums)
gen_match_file7('game', game_messages) gen_match_file7('game', game_messages)
gen_match_file7('system', system_messages) gen_match_file7('system', system_messages)
for msg in game_messages: for msg in game_messages:

View file

@ -0,0 +1,122 @@
PICKUP_HEALTH: int = 0
PICKUP_ARMOR: int = 1
PICKUP_GRENADE: int = 2
PICKUP_SHOTGUN: int = 3
PICKUP_LASER: int = 4
PICKUP_NINJA: int = 5
PICKUP_GUN: int = 6
PICKUP_HAMMER: int = 7
EMOTE_NORMAL: int = 0
EMOTE_PAIN: int = 1
EMOTE_HAPPY: int = 2
EMOTE_SURPRISE: int = 3
EMOTE_ANGRY: int = 4
EMOTE_BLINK: int = 5
EMOTICON_OOP: int = 0
EMOTICON_EXCLAMATION: int = 1
EMOTICON_HEARTS: int = 2
EMOTICON_DROP: int = 3
EMOTICON_DOTDOT: int = 4
EMOTICON_MUSIC: int = 5
EMOTICON_SORRY: int = 6
EMOTICON_GHOST: int = 7
EMOTICON_SUSHI: int = 8
EMOTICON_SPLATTEE: int = 9
EMOTICON_DEVILTEE: int = 10
EMOTICON_ZOMG: int = 11
EMOTICON_ZZZ: int = 12
EMOTICON_WTF: int = 13
EMOTICON_EYES: int = 14
EMOTICON_QUESTION: int = 15
VOTE_UNKNOWN: int = 0
VOTE_START_OP: int = 1
VOTE_START_KICK: int = 2
VOTE_START_SPEC: int = 3
VOTE_END_ABORT: int = 4
VOTE_END_PASS: int = 5
VOTE_END_FAIL: int = 6
CHAT_NONE: int = 0
CHAT_ALL: int = 1
CHAT_TEAM: int = 2
CHAT_WHISPER: int = 3
GAMEMSG_TEAM_SWAP: int = 0
GAMEMSG_SPEC_INVALIDID: int = 1
GAMEMSG_TEAM_SHUFFLE: int = 2
GAMEMSG_TEAM_BALANCE: int = 3
GAMEMSG_CTF_DROP: int = 4
GAMEMSG_CTF_RETURN: int = 5
GAMEMSG_TEAM_ALL: int = 6
GAMEMSG_TEAM_BALANCE_VICTIM: int = 7
GAMEMSG_CTF_GRAB: int = 8
GAMEMSG_CTF_CAPTURE: int = 9
GAMEMSG_GAME_PAUSED: int = 10
WEAPON_HAMMER: int = 0
WEAPON_PISTOL: int = 1
WEAPON_SHOTGUN: int = 2
WEAPON_GRENADE: int = 3
WEAPON_RIFLE: int = 4
WEAPON_NINJA: int = 5
TEAM_SPECTATORS: int = -1
TEAM_RED: int = 0
TEAM_BLUE: int = 1
SOUND_GUN_FIRE: int = 0
SOUND_SHOTGUN_FIRE: int = 1
SOUND_GRENADE_FIRE: int = 2
SOUND_HAMMER_FIRE: int = 3
SOUND_HAMMER_HIT: int = 4
SOUND_NINJA_FIRE: int = 5
SOUND_GRENADE_EXPLODE: int = 6
SOUND_NINJA_HIT: int = 7
SOUND_RIFLE_FIRE: int = 8
SOUND_RIFLE_BOUNCE: int = 9
SOUND_WEAPON_SWITCH: int = 10
SOUND_PLAYER_PAIN_SHORT: int = 11
SOUND_PLAYER_PAIN_LONG: int = 12
SOUND_BODY_LAND: int = 13
SOUND_PLAYER_AIRJUMP: int = 14
SOUND_PLAYER_JUMP: int = 15
SOUND_PLAYER_DIE: int = 16
SOUND_PLAYER_SPAWN: int = 17
SOUND_PLAYER_SKID: int = 18
SOUND_TEE_CRY: int = 19
SOUND_HOOK_LOOP: int = 20
SOUND_HOOK_ATTACH_GROUND: int = 21
SOUND_HOOK_ATTACH_PLAYER: int = 22
SOUND_HOOK_NOATTACH: int = 23
SOUND_PICKUP_HEALTH: int = 24
SOUND_PICKUP_ARMOR: int = 25
SOUND_PICKUP_GRENADE: int = 26
SOUND_PICKUP_SHOTGUN: int = 27
SOUND_PICKUP_NINJA: int = 28
SOUND_WEAPON_SPAWN: int = 29
SOUND_WEAPON_NOAMMO: int = 30
SOUND_HIT: int = 31
SOUND_CHAT_SERVER: int = 32
SOUND_CHAT_CLIENT: int = 33
SOUND_CHAT_HIGHLIGHT: int = 34
SOUND_CTF_DROP: int = 35
SOUND_CTF_RETURN: int = 36
SOUND_CTF_GRAB_PL: int = 37
SOUND_CTF_GRAB_EN: int = 38
SOUND_CTF_CAPTURE: int = 39
SOUND_MENU: int = 40
SPEC_FREEVIEW: int = 0
SPEC_PLAYER: int = 1
SPEC_FLAGRED: int = 2
SPEC_FLAGBLUE: int = 3
SKINPART_BODY: int = 0
SKINPART_MARKING: int = 1
SKINPART_DECORATION: int = 2
SKINPART_HANDS: int = 3
SKINPART_FEET: int = 4
SKINPART_EYES: int = 5