mirror of
https://git.neolegacy.dev/neoStudiosLCE/neoLegacy.git
synced 2026-12-03 06:29:05 +01:00
91 lines
2.8 KiB
Python
91 lines
2.8 KiB
Python
"""Packet builders for the LCE server stress-test client."""
|
|
|
|
import struct
|
|
|
|
from protocol import DataInputStream
|
|
|
|
PRE_LOGIN = 2
|
|
LOGIN = 1
|
|
KEEP_ALIVE = 0
|
|
CUSTOM_PAYLOAD = 250
|
|
DISCONNECT = 255
|
|
DISCONNECT_QUITTING = 1
|
|
|
|
NETWORK_VERSION = 571
|
|
LOGIN_PROTOCOL_VERSION = 79
|
|
PRELOGIN_SAVE_NAME_LENGTH = 14
|
|
|
|
DISCONNECT_REASONS: dict[int, str] = {}
|
|
|
|
|
|
def _write_utf(value: str) -> bytes:
|
|
return struct.pack(">h", len(value)) + value.encode("utf-16-be")
|
|
|
|
|
|
def build_prelogin(name: str, netcode_version: int = NETWORK_VERSION) -> bytes:
|
|
return b"".join((
|
|
struct.pack(">h", netcode_version),
|
|
_write_utf(name),
|
|
b"\x00", # friends-only flags
|
|
struct.pack(">i", 0), # UGC players version
|
|
b"\x00", # player count
|
|
bytes(PRELOGIN_SAVE_NAME_LENGTH),
|
|
struct.pack(">i", 0), # server settings
|
|
b"\x00", # host index
|
|
struct.pack(">i", 0), # texture pack id
|
|
))
|
|
|
|
|
|
def build_login(name: str, xuid: int, ugc_players_version: int = 0) -> bytes:
|
|
return b"".join((
|
|
struct.pack(">i", LOGIN_PROTOCOL_VERSION),
|
|
_write_utf(name),
|
|
_write_utf(""),
|
|
struct.pack(">q", 0), # seed
|
|
struct.pack(">i", 0), # game type
|
|
b"\x00\x00\x00", # dimension, map height, max players
|
|
struct.pack(">q", xuid), # offline XUID
|
|
struct.pack(">q", xuid), # online XUID
|
|
b"\x00", # friends-only UGC
|
|
struct.pack(">i", ugc_players_version),
|
|
b"\x01", # difficulty
|
|
struct.pack(">i", 0), # multiplayer instance id
|
|
b"\x00", # player index
|
|
struct.pack(">ii", 0, 0), # skin and cape ids
|
|
b"\x00\x00", # guest and new sea level
|
|
struct.pack(">i", 0), # UI game privileges
|
|
struct.pack(">hb", 320, 8), # large-world XZ size and Nether scale
|
|
))
|
|
|
|
|
|
def build_keep_alive(counter: int) -> bytes:
|
|
return struct.pack(">i", counter)
|
|
|
|
|
|
def build_disconnect(reason: int = DISCONNECT_QUITTING) -> bytes:
|
|
return struct.pack(">i", reason)
|
|
|
|
|
|
def build_custom_payload(channel: str, payload: bytes = b"") -> bytes:
|
|
return _write_utf(channel) + struct.pack(">h", len(payload)) + payload
|
|
|
|
|
|
def parse_prelogin_response(data: bytes) -> dict[str, int]:
|
|
stream = DataInputStream(data)
|
|
stream.read_short()
|
|
stream.read_utf()
|
|
stream.read_byte()
|
|
ugc_players_version = stream.read_int()
|
|
player_count = stream.read_byte()
|
|
stream.read_raw(player_count * 8)
|
|
stream.read_raw(PRELOGIN_SAVE_NAME_LENGTH)
|
|
stream.read_int()
|
|
stream.read_byte()
|
|
stream.read_int()
|
|
return {"player_count": player_count, "ugc_players_version": ugc_players_version}
|
|
|
|
|
|
def parse_login_response_stream(stream: DataInputStream) -> dict[str, int | str | None]:
|
|
entity_id = stream.read_int()
|
|
username = stream.read_utf()
|
|
return {"entity_id": entity_id, "username": username} |