Overview
The YSchat module provides utilities for constructing and sending chat messages and other packet data to YSFlight clients. It handles packet formatting with proper size headers and type information.
Functions
send()
def send(buffer: bytes) -> bytes
Adds size information to a packet buffer.
The raw packet data to wrap with size information
The packet buffer prefixed with a 4-byte unsigned integer containing the buffer length
Example
from lib import YSchat
raw_data = b"Hello, world!"
packet = YSchat.send(raw_data)
# Returns: struct.pack("I", 13) + b"Hello, world!"
reply()
def reply(type: int, buffer: bytes) -> bytes
Generates complete packets ready to send to clients, including size header and packet type.
The packet type identifier (e.g., FSNETCMD_TEXTMESSAGE)
A complete packet with size header, type identifier, and payload
Example
from lib import YSchat
from lib.PacketManager.packets import FSNETCMD_TEXTMESSAGE
message_data = b"Server announcement"
packet = YSchat.reply(FSNETCMD_TEXTMESSAGE, message_data)
message()
def message(msg: str) -> bytes
Generates a complete text message packet ready to send to YSFlight clients.
The text message to send to clients
A properly encoded FSNETCMD_TEXTMESSAGE packet ready for transmission
Example
from lib import YSchat
# Send welcome message
welcome_msg = YSchat.message("Welcome to the server!")
message_to_client.append(welcome_msg)
# Send health hack warning
cheating_msg = YSchat.message(f"Health hack detected by {player.username}")
message_to_server.append(cheating_msg)
# Send to client writer directly
client_writer.write(YSchat.message("You have been kicked!"))
await client_writer.drain()
Usage in Proxy
The YSchat.message() function is commonly used throughout the proxy to send notifications:
# Version compatibility notification (proxy.py:173-174)
if player.version != YSF_VERSION and VIA_VERSION:
message_to_client.append(YSchat.message(f"Porting you to YSFlight {YSF_VERSION}"))
message_to_client.append(YSchat.message(f"Please report any bugs"))
# Welcome message (proxy.py:265-266)
welcome_msg = YSchat.message(WELCOME_MESSAGE.format(username=player.username))
message_to_server.append(welcome_msg)
# Anti-cheat detection (proxy.py:194)
if player.aircraft.prev_life < player.aircraft.life:
cheating_msg = YSchat.message(f"{HEALTH_HACK_MESSAGE} by {player.username}")
message_to_server.append(cheating_msg)
# Disconnect notification (proxy.py:341)
for p in CONNECTED_PLAYERS:
p.streamWriterObject.write(YSchat.message(f"{player.username} has left!"))
await p.streamWriterObject.drain()
Notes
- All functions use
struct.pack() for binary encoding
- The
send() function adds a 4-byte unsigned integer ("I" format) containing the payload length
- The
message() function uses FSNETCMD_TEXTMESSAGE.encode() with the True parameter
- Messages can be queued in
message_to_client or message_to_server lists for async delivery