First actual parse

This commit is contained in:
ChillerDragon 2023-03-16 10:26:36 +01:00
parent a3c3343c42
commit d516b591b0
3 changed files with 22 additions and 11 deletions

View file

@ -425,6 +425,7 @@ disable=raw-checker-failed,
trailing-newlines,
missing-class-docstring,
missing-module-docstring,
missing-function-docstring,
fixme,
use-symbolic-message-instead

View file

@ -1,12 +1,11 @@
A Python teeworlds client & server library, designed according to sans I/O (http://sans-io.readthedocs.io/) principles
## goal
## sample usage
```python
msg = twnet_parser.parse(b'\x10\x10\x00\x04\x9a\xcb9\xc9')
print(msg)
# <class '__main__.CtrlMessage'>: {'name': 'disconnect', 'token': b'\x9a\xcb9\xc9', 'fields': {}}
pck = parse(b'\x10\x10\x00\x04\x9a\xcb9\xc9')
for msg in pck.messages:
print(msg.name) # => close
```
## setup

23
main.py
View file

@ -2,13 +2,17 @@
from typing import Union
class CtrlMessage:
class BaseMessage:
def __init__(self, name):
self.name = name
class CtrlMessage(BaseMessage):
pass
class GameMessage:
class GameMessage(BaseMessage):
pass
class SysMessage:
class SysMessage(BaseMessage):
pass
class PacketFlags7():
@ -36,10 +40,17 @@ class TwPacket:
def __init__(self) -> None:
self.header: Header = Header()
self.messages: list[Union[CtrlMessage, GameMessage, SysMessage]] = []
self.messages.append(GameMessage())
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)
pck = TwPacket()
print(pck)
def parse(data: bytes):
if data[2] == 0x00: # control
if data[3] == 0x04: # close
pck = TwPacket()
msg_dc = CtrlMessage('close')
pck.messages.append(msg_dc)
return pck
return TwPacket()