From 813d8f5f9668094671b1a41447f17caca3afed93 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 16 Jun 2024 10:32:15 +0800 Subject: [PATCH] feat: implement dict on ChunkHeader --- twnet_parser/chunk_header.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/twnet_parser/chunk_header.py b/twnet_parser/chunk_header.py index 54f13da..858207f 100644 --- a/twnet_parser/chunk_header.py +++ b/twnet_parser/chunk_header.py @@ -6,9 +6,18 @@ class ChunkFlags(PrettyPrint): def __init__(self): self.resend = False self.vital = False + def __repr__(self): return ": " + str(self.__dict__) + def __iter__(self): + flags = [] + if self.resend: + flags.append('resend') + if self.vital: + flags.append('vital') + return iter(flags) + # same fields for 0.6 and 0.7 # different bit layout tho class ChunkHeader(PrettyPrint): @@ -32,6 +41,12 @@ class ChunkHeader(PrettyPrint): # if unset self.seq: int = -1 + def __iter__(self): + yield 'version', self.version + yield 'flags', list(self.flags) + yield 'size', self.size + yield 'seq', self.seq + def pack(self) -> bytes: flags: int = 0 if self.flags.resend: @@ -60,3 +75,7 @@ class ChunkHeader(PrettyPrint): data[1] |= (self.seq >> 2) & 0xc0 data += bytes([self.seq & 0xff]) return bytes(data) + + def __repr__(self): + return ": " + str(dict(self)) +