This commit is contained in:
əlemi 2021-11-18 03:06:49 +01:00
parent 6d83836fcb
commit 80f3b769a2

View file

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