First actual parse
This commit is contained in:
parent
a3c3343c42
commit
d516b591b0
|
@ -425,6 +425,7 @@ disable=raw-checker-failed,
|
|||
trailing-newlines,
|
||||
missing-class-docstring,
|
||||
missing-module-docstring,
|
||||
missing-function-docstring,
|
||||
fixme,
|
||||
use-symbolic-message-instead
|
||||
|
||||
|
|
|
@ -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
23
main.py
|
@ -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()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue