Generate system message matcher
This commit is contained in:
parent
a19df04b80
commit
41f4742ac1
|
@ -36,8 +36,11 @@ class SpecJson(TypedDict):
|
|||
game_messages: list[NetMessageJson]
|
||||
system_messages: list[NetMessageJson]
|
||||
|
||||
def gen_match_game7(game_messages: list[NetMessageJson]):
|
||||
match_game7: str = """# generated by scripts/generate_messages.py
|
||||
def gen_match_file7(
|
||||
msg_type: Literal['system', 'game'],
|
||||
messages: list[NetMessageJson]
|
||||
):
|
||||
match_code: str = """# generated by scripts/generate_messages.py
|
||||
from typing import Optional
|
||||
|
||||
import twnet_parser.msg7
|
||||
|
@ -46,46 +49,48 @@ from twnet_parser.net_message import NetMessage
|
|||
"""
|
||||
|
||||
msg: NetMessageJson
|
||||
for msg in game_messages:
|
||||
for msg in messages:
|
||||
name_snake = name_to_snake(msg['name'])
|
||||
match_game7 += "import twnet_parser.messages7.game" \
|
||||
match_code += f"import twnet_parser.messages7.{msg_type}" \
|
||||
f".{name_snake}" \
|
||||
" as \\\n" \
|
||||
f" game7_{name_snake}\n"
|
||||
f" {msg_type}7_{name_snake}\n"
|
||||
|
||||
match_game7 += """
|
||||
def match_game7(msg_id: int, data: bytes) -> NetMessage:
|
||||
match_code += f"""
|
||||
def match_{msg_type}7(msg_id: int, data: bytes) -> NetMessage:
|
||||
msg: Optional[NetMessage] = None
|
||||
"""
|
||||
|
||||
if_ = 'if'
|
||||
for msg in game_messages:
|
||||
for msg in messages:
|
||||
name_snake = name_to_snake(msg['name'])
|
||||
name_camel = name_to_camel(msg['name'])
|
||||
match_game7 += \
|
||||
match_code += \
|
||||
f"""
|
||||
{if_} msg_id == twnet_parser.msg7.{name_snake.upper()}:
|
||||
msg = game7_{name_snake}.Msg{name_camel}()"""
|
||||
msg = {msg_type}7_{name_snake}.Msg{name_camel}()"""
|
||||
if_ = 'elif'
|
||||
|
||||
match_game7 += '\n\n if msg is None:\n'
|
||||
match_game7 += ' '
|
||||
match_game7 += 'raise ValueError('
|
||||
match_game7 += 'f"Error: unknown message sys.id={msg_id} data={data[0]}")\n'
|
||||
match_game7 += '\n'
|
||||
match_game7 += ' msg.unpack(data)\n'
|
||||
match_game7 += ' return msg\n'
|
||||
match_code += '\n\n if msg is None:\n'
|
||||
match_code += ' '
|
||||
match_code += 'raise ValueError('
|
||||
match_code += 'f"Error: unknown ' \
|
||||
+ msg_type + \
|
||||
' message id={msg_id} data={data[0]}")\n'
|
||||
match_code += '\n'
|
||||
match_code += ' msg.unpack(data)\n'
|
||||
match_code += ' return msg\n'
|
||||
|
||||
dirname = os.path.dirname(__file__)
|
||||
file_path= os.path.join(
|
||||
dirname,
|
||||
'../twnet_parser/msg_matcher/game7.py')
|
||||
f'../twnet_parser/msg_matcher/{msg_type}7.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(match_game7)
|
||||
out_file.write(match_code)
|
||||
|
||||
def fix_name_conflict(name: str) -> str:
|
||||
# https://peps.python.org/pep-0008/#descriptive-naming-styles
|
||||
|
@ -326,7 +331,8 @@ def generate(spec: str) -> None:
|
|||
# for msg in [spec_data['game_messages'][1]]:
|
||||
game_messages: list[NetMessageJson] = spec_data['game_messages']
|
||||
system_messages: list[NetMessageJson] = spec_data['system_messages']
|
||||
gen_match_game7(game_messages)
|
||||
gen_match_file7('game', game_messages)
|
||||
gen_match_file7('system', system_messages)
|
||||
for msg in game_messages:
|
||||
generate_msg(msg, 'game')
|
||||
for msg in system_messages:
|
||||
|
|
|
@ -3,18 +3,33 @@ NULL = 0
|
|||
INFO = 1
|
||||
MAP_CHANGE = 2 # sent when client should switch map
|
||||
MAP_DATA = 3 # map transfer, contains a chunk of the map file
|
||||
SERVERINFO = 4
|
||||
SERVER_INFO = 4
|
||||
CON_READY = 5 # connection is ready, client should send start info
|
||||
SNAP = 6 # normal snapshot, multiple parts
|
||||
SNAPEMPTY = 7 # empty snapshot
|
||||
SNAPSINGLE = 8 # ?
|
||||
SNAPSMALL = 9
|
||||
INPUTTIMING = 10 # reports how off the input was
|
||||
SNAP_EMPTY = 7 # empty snapshot
|
||||
SNAP_SINGLE = 8 # ?
|
||||
SNAP_SMALL = 9
|
||||
INPUT_TIMING = 10 # reports how off the input was
|
||||
RCON_AUTH_ON = 11 # rcon authentication enabled
|
||||
RCON_AUTH_OFF = 12 # rcon authentication disabled
|
||||
RCON_LINE = 13 # line that should be printed to the remote console
|
||||
RCON_CMD_ADD = 14
|
||||
RCON_CMD_REM = 15
|
||||
AUTH_CHALLENGE = 16
|
||||
AUTH_RESULT = 17
|
||||
READY = 18
|
||||
ENTER_GAME = 19
|
||||
INPUT = 20
|
||||
RCON_CMD = 21
|
||||
RCON_AUTH = 22
|
||||
REQUEST_MAP_DATA = 23
|
||||
AUTH_START = 24
|
||||
AUTH_RESPONSE = 25
|
||||
PING = 26
|
||||
PING_REPLY = 27
|
||||
ERROR = 28
|
||||
MAPLIST_ENTRY_ADD = 29
|
||||
MAPLIST_ENTRY_REM = 30
|
||||
|
||||
# game
|
||||
INVALID = 0
|
||||
|
|
Loading…
Reference in a new issue