bta-proxy/bta_proxy/debug.py

53 lines
1.5 KiB
Python
Raw Normal View History

2023-08-24 15:29:57 +03:00
from collections.abc import Iterable
from bta_proxy.packets.base import Packet
2023-08-24 17:40:23 +03:00
from bta_proxy.packets import *
2023-08-24 15:29:57 +03:00
from .datainputstream import DataInputStream
from typing import Generator, TextIO, TypeVar
T = TypeVar('T')
def chunks(gen: Iterable[T], size: int) -> Generator[list[T], None, None]:
bucket: list[T] = []
for item in gen:
bucket.append(item)
if len(bucket) >= size:
yield bucket
bucket.clear()
if bucket:
yield bucket
def debug_client(buffer: bytes, tmpfile: TextIO):
stream = DataInputStream(buffer)
while not stream.empty():
try:
packet = Packet.parse_packet(stream)
2023-08-24 17:40:23 +03:00
match packet.packet_id:
2023-08-24 15:29:57 +03:00
case _:
2023-08-24 17:40:23 +03:00
print('[C]', packet)
2023-08-24 15:29:57 +03:00
except ValueError:
# print(f'[C:rest] {stream.end()}')
buf = stream.end()
2023-08-24 17:40:23 +03:00
print(f"[C] {buf[0]=} {len(buf)=}, {buf=}", file=tmpfile)
2023-08-24 15:29:57 +03:00
def debug_server(buffer: bytes, tmpfile: TextIO):
stream = DataInputStream(buffer)
while not stream.empty():
try:
packet = Packet.parse_packet(stream)
2023-08-24 17:40:23 +03:00
match packet.packet_id:
case Packet50PreChunk.packet_id:
continue
case Packet38EntityStatus.packet_id:
continue
case _:
print('[S]', packet)
2023-08-24 15:29:57 +03:00
except ValueError:
# print(f'[S:rest] {stream.end()}')
buf = stream.end()
2023-08-24 17:40:23 +03:00
print(f"[S] {buf[0]=} {len(buf)=}, {buf=}", file=tmpfile)
2023-08-24 15:29:57 +03:00