fix: use definitions from previous protocols

It's not really elegant but it works. If a definition is missing, it's
likely because it's unchanged since last revision
This commit is contained in:
əlemi 2023-02-16 18:42:36 +01:00
parent 6448e7bad4
commit d6e5e7a6f6
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 10 additions and 2 deletions

View file

@ -233,7 +233,10 @@ class Dispatcher:
if not self._proto:
raise InvalidState("Cannot access registries from invalid protocol")
proto_reg = reg[self._proto]
proto = self._proto
while proto not in reg:
proto -= 1
proto_reg = reg[proto]
return proto_reg[packet_id]

View file

@ -22,6 +22,8 @@ class Packet:
def __init__(self, proto:int, **kwargs):
self._proto = proto
self._processed = Event()
while proto not in self._definitions:
proto -= 1
self.definition = self._definitions[proto]
self.id = self._ids[proto]
for name, t in self.definition:
@ -36,7 +38,10 @@ class Packet:
@classmethod
def deserialize(cls, proto:int, buffer:io.BytesIO):
ctx = Context(_proto=proto)
for k, t in cls._definitions[proto]:
fallback_proto = proto
while fallback_proto not in cls._definitions:
fallback_proto -= 1
for k, t in cls._definitions[fallback_proto]:
setattr(ctx, k, t.read(buffer, ctx=ctx))
return cls(proto, **ctx.serialize())
# return cls(proto, **{ name : t.read(buffer) for (name, t) in cls._definitions[proto] })