From 1767d05f4ef3c8e1f1b94716d2fe897ae882ced1 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 16 Apr 2023 08:43:37 +0200 Subject: [PATCH] Generate enums --- scripts/generate_messages.py | 44 ++++++++--- twnet_parser/msg_matcher/enum7.py | 122 ++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 twnet_parser/msg_matcher/enum7.py diff --git a/scripts/generate_messages.py b/scripts/generate_messages.py index fe1adbb..e0eb41b 100755 --- a/scripts/generate_messages.py +++ b/scripts/generate_messages.py @@ -21,19 +21,19 @@ KIND = Literal[ \ 'flags', \ 'optional'] +class GameEnumValueJson(TypedDict): + value: int + name: list[str] + +class GameEnumJson(TypedDict): + name: list[str] + values: list[GameEnumValueJson] + class ConstantJson(TypedDict): name: list[str] type: str value: int -class NetEnumValuesJson(TypedDict): - value: str - name: list[str] - -class NetEnumJson(TypedDict): - name: list[str] - values: list[NetEnumValuesJson] - class InnerNetMessageMemberTypeJson(TypedDict): kind: KIND disallow_cc: bool @@ -68,7 +68,7 @@ class NetMessageJson(TypedDict): class SpecJson(TypedDict): constants: list[ConstantJson] - game_enumerations: list[NetEnumJson] + game_enumerations: list[GameEnumJson] game_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") 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: print(f"generating classes from {spec} ...") with open(spec) as spec_io: spec_data: SpecJson = json.load(spec_io) # for msg in [spec_data['game_messages'][1]]: + game_enums: list[GameEnumJson] = spec_data['game_enumerations'] game_messages: list[NetMessageJson] = spec_data['game_messages'] system_messages: list[NetMessageJson] = spec_data['system_messages'] + gen_enum_file7(game_enums) gen_match_file7('game', game_messages) gen_match_file7('system', system_messages) for msg in game_messages: diff --git a/twnet_parser/msg_matcher/enum7.py b/twnet_parser/msg_matcher/enum7.py new file mode 100644 index 0000000..3aac055 --- /dev/null +++ b/twnet_parser/msg_matcher/enum7.py @@ -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