diff --git a/compiler/proto.py b/compiler/proto.py index 16e29b8..90eafea 100644 --- a/compiler/proto.py +++ b/compiler/proto.py @@ -9,6 +9,8 @@ from typing import List, Dict, Union from aiocraft.mc.types import * +# TODO de-spaghetti this file sometime! + DIR_MAP = {"toClient": "clientbound", "toServer": "serverbound"} PREFACE = """\"\"\"[!] This file is autogenerated\"\"\"\n\n""" IMPORTS = """from typing import Tuple, List, Dict @@ -82,14 +84,16 @@ def parse_field(slot: dict) -> str: class PacketClassWriter: title : str ids : str + attrs : List[str] slots : str fields: str state : int - def __init__(self, title:str, ids:str, slots:str, fields:str, state:int): + def __init__(self, title:str, ids:str, attrs:List[str], slots:str, fields:str, state:int): self.title = title self.ids = ids + self.attrs = attrs self.slots = slots self.fields = fields self.state = state @@ -101,6 +105,7 @@ class PacketClassWriter: name=self.title, ids='{\n\t\t' + ',\n\t\t'.join(self.ids) + '\n\t}\n', definitions='{\n\t\t' + '\n\t\t'.join(self.slots) + '\n\t}\n', + slots=', '.join(f"'{x}'" for x in self.attrs), fields='\n\t'.join(self.fields), state=self.state, ) @@ -212,6 +217,7 @@ def compile(): pkt = PACKETS[state][direction][packet] slots = [] fields = set() + attrs = set() ids = [] for v in sorted(PACKETS[state][direction][packet]["definitions"].keys()): defn = pkt["definitions"][v] @@ -224,12 +230,14 @@ def compile(): for slot in defn["slots"]: v_slots.append(parse_slot(slot)) fields.add(parse_field(slot)) + if "name" in slot: + attrs.add(slot["name"]) slots.append(f"{v} : [ {','.join(v_slots)} ],") with open(mc_path / f"proto/{state}/{direction}/{packet}.py", "w") as f: f.write( PacketClassWriter( - pkt["name"], ids, slots, fields, _STATE_MAP[state] + pkt["name"], ids, attrs, slots, fields, _STATE_MAP[state] ).compile() )