some project sorting

This commit is contained in:
əlemi 2021-10-10 01:38:17 +02:00 committed by alemidev
parent c08f3b5153
commit 5cde07da56
6 changed files with 71 additions and 8 deletions

1
aiocraft/__main__.py Normal file
View file

@ -0,0 +1 @@
print(":)")

View file

@ -0,0 +1,2 @@
class Dispatcher:

View file

@ -6,14 +6,17 @@ from typing import List, Dict
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 .minecraft.packet import Packet\nfrom .minecraft.types import *\n""" IMPORTS = """from typing import Tuple
from .minecraft.packet import Packet
from .minecraft.types import *\n"""
IMPORT_ALL = """from .* import * # TODO!\n""" IMPORT_ALL = """from .* import * # TODO!\n"""
OBJECT = """ OBJECT = """
class {name}(Packet): class {name}(Packet):
ID : int = 0x{id:X} id : int = 0x{id:X}
SLOTS : list = ( _slots : Tuple[Tuple[str, Type]] = (
{slots} {slots}
) )
{fields}
""" """
def snake_to_camel(name:str) -> str: def snake_to_camel(name:str) -> str:
@ -30,6 +33,20 @@ def parse_slot(slot: dict) -> str:
return f"(\"anon\", {slot['type']})" return f"(\"anon\", {slot['type']})"
else: else:
return f"(\"anon\", [Special])" return f"(\"anon\", [Special])"
return slot
def parse_field(slot: dict) -> str:
if "name" in slot:
if "type" in slot and isinstance(slot["type"], str):
return f"{slot['name']} : {slot['type'].pytype()}"
else:
return f"{slot['name']} : Any"
elif "anon" in slot:
if "type" in slot and isinstance(slot["type"], str):
return f"anon : {slot['type'].pytype()}"
else:
return f"anon : Any"
class PacketClassWriter: class PacketClassWriter:
@ -43,7 +60,14 @@ class PacketClassWriter:
self.slots = slots self.slots = slots
def compile(self) -> str: def compile(self) -> str:
return PREFACE + IMPORTS + OBJECT.format(id=pid, name=self.title, slots=parse_slot(self.slots)) return PREFACE + \
IMPORTS + \
OBJECT.format(
id=pid,
name=self.title,
slots=parse_slot(self.slots),
fields=parse_field(self.slots),
)
def _make_module(path:str): def _make_module(path:str):
os.mkdir(path) os.mkdir(path)
@ -54,8 +78,8 @@ def _make_module(path:str):
if __name__ == "__main__": if __name__ == "__main__":
# TODO load relatively! # TODO load relatively!
# with open("???/minecraft-data/data/pc/1.12.2/protocol.json") as f: with open("/home/alemi/projects/minecraft-data/data/pc/1.12.2/protocol.json") as f:
# data = json.load(f) data = json.load(f)
_make_module("protocol") _make_module("protocol")
for state in ("handshaking", "status", "login", "play"): for state in ("handshaking", "status", "login", "play"):

View file

@ -103,8 +103,6 @@ class EntityMetadata(Type):
class Slot(Type): class Slot(Type):
# TODO # TODO
pass pass
if buf & 0b10000000 == 0:
break
class NBTTag(Type): class NBTTag(Type):
# TODO # TODO

View file

@ -0,0 +1,21 @@
from typing import Tuple
from .mctypes import Type
class Packet:
id : int
_slots : Tuple[Tuple[str, Type]]
def serialize(self) -> bytes:
return b''.join(
slot[1].serialize(getattr(self, slot[0]))
for slot in self._slots
)
@classmethod
def deserialize(cls, data:bytes):
pkt = cls()
for slot in cls._slots:
setattr(pkt, slot[0], slot[1].deserialize(data))
return pkt

17
aiocraft/session.py Normal file
View file

@ -0,0 +1,17 @@
class Session:
host:str
port:int
def __init__(
self,
host: str = "localhost",
port: int = 25565,
):
self.host = host
self.port = port
async def run(self):