mirror of
https://git.neolegacy.dev/neoStudiosLCE/neoLegacy.git
synced 2026-12-03 06:29:05 +01:00
112 lines
3.2 KiB
Python
112 lines
3.2 KiB
Python
"""Minimal stream and framing helpers for the LCE stress-test client."""
|
|
|
|
import struct
|
|
|
|
|
|
def frame_packet(packet_id: int, payload: bytes) -> bytes:
|
|
return bytes((packet_id,)) + payload
|
|
|
|
|
|
class DataInputStream:
|
|
def __init__(self, data: bytes):
|
|
self._data = memoryview(data)
|
|
self._offset = 0
|
|
|
|
def read_raw(self, length: int) -> bytes:
|
|
end = self._offset + length
|
|
if length < 0 or end > len(self._data):
|
|
raise EOFError("Not enough data in packet")
|
|
result = self._data[self._offset:end].tobytes()
|
|
self._offset = end
|
|
return result
|
|
|
|
def read_byte(self) -> int:
|
|
return struct.unpack(">b", self.read_raw(1))[0]
|
|
|
|
def read_bool(self) -> bool:
|
|
return self.read_raw(1) != b"\x00"
|
|
|
|
def read_short(self) -> int:
|
|
return struct.unpack(">h", self.read_raw(2))[0]
|
|
|
|
def read_int(self) -> int:
|
|
return struct.unpack(">i", self.read_raw(4))[0]
|
|
|
|
def read_long(self) -> int:
|
|
return struct.unpack(">q", self.read_raw(8))[0]
|
|
|
|
def read_float(self) -> float:
|
|
return struct.unpack(">f", self.read_raw(4))[0]
|
|
|
|
def read_double(self) -> float:
|
|
return struct.unpack(">d", self.read_raw(8))[0]
|
|
|
|
def read_utf(self) -> str:
|
|
length = self.read_short()
|
|
if length < 0:
|
|
raise ValueError("Negative string length")
|
|
return self.read_raw(length * 2).decode("utf-16-be")
|
|
|
|
@property
|
|
def offset(self) -> int:
|
|
return self._offset
|
|
|
|
|
|
def read_packet(buffer: bytearray) -> tuple[int, bytes] | None:
|
|
if not buffer:
|
|
return None
|
|
|
|
packet_id = buffer[0]
|
|
stream = DataInputStream(bytes(buffer[1:]))
|
|
try:
|
|
if packet_id == 2:
|
|
stream.read_short()
|
|
stream.read_utf()
|
|
stream.read_byte()
|
|
stream.read_int()
|
|
player_count = stream.read_byte()
|
|
stream.read_raw(player_count * 8)
|
|
stream.read_raw(14)
|
|
stream.read_int()
|
|
stream.read_byte()
|
|
stream.read_int()
|
|
elif packet_id == 1:
|
|
stream.read_int()
|
|
stream.read_utf()
|
|
stream.read_utf()
|
|
stream.read_long()
|
|
stream.read_int()
|
|
stream.read_raw(3 + 16 + 1 + 4 + 1 + 4 + 1 + 8 + 2 + 4)
|
|
elif packet_id == 250:
|
|
stream.read_utf()
|
|
payload_length = stream.read_short()
|
|
if payload_length > 0:
|
|
stream.read_raw(payload_length)
|
|
elif packet_id in (0, 255):
|
|
stream.read_int()
|
|
else:
|
|
raise ValueError(f"Unsupported packet ID {packet_id} during login")
|
|
except EOFError:
|
|
return None
|
|
|
|
packet_length = 1 + stream.offset
|
|
payload = bytes(buffer[1:packet_length])
|
|
del buffer[:packet_length]
|
|
return packet_id, payload
|
|
|
|
|
|
class DataOutputStream:
|
|
def __init__(self):
|
|
self._data = bytearray()
|
|
|
|
def write_bool(self, value: bool) -> None:
|
|
self._data.extend(b"\x01" if value else b"\x00")
|
|
|
|
def write_float(self, value: float) -> None:
|
|
self._data.extend(struct.pack(">f", value))
|
|
|
|
def write_double(self, value: float) -> None:
|
|
self._data.extend(struct.pack(">d", value))
|
|
|
|
def getvalue(self) -> bytes:
|
|
return bytes(self._data) |