refactor: world packets as structs

WorldPackets now holds what a client sends a world server, one struct per
packet with Serialize/Deserialize: Validation, CharacterListRequest,
CharacterCreateRequest, CharacterLoginRequest, GameMessage,
CharacterDeleteRequest, CharacterRenameRequest, LevelLoadComplete,
PositionUpdate, MailPacket, RoutePacket, StringCheck, GeneralChatMessage,
HandleFunness, UIHelpTop5. WorldServer's switch became a dispatch map of
handlers (each overrides Handle in WorldServer.cpp, logic unchanged:
dashboard hooks, LoadPlayer, chat logging, migration checks, message
inspector capture all still run); a packet that does not deserialize is
logged and dropped. UserManager's create/delete/rename take the structs.

The answers are ClientPackets (the CLIENT service): LoadStaticZone,
CharacterListResponse, CharacterCreateResponse, CharacterRenameResponse,
DeleteCharacterResponse, TransferToWorld, ServerStates, CreateCharacter,
ChatModerationString, MakeGMResponse, HTTPMonitorInfoResponse and
DebugOutput, built at the call sites (world server, UserManager, slash
commands, dashboard actions, components, migration). The world -> chat
forward of a routed packet is ChatPackets::RoutedFromClient. All
WorldPackets::Send* functions, HTTPMonitorInfo and the ClientPackets parse
functions are gone. The architecture doc now states the file rule: a
packet lives in the file of the ServiceType in its header.

No wire change. Verified against frozen verbatim copies of the old code
(tests/dGameTests/dNetTests/Legacy/WorldPacketsLegacy.h): every response
is sent through the old function and the struct over grids of inputs
(all enum values, strings of every length class, IDs, >64 moderation
segments, big XML) and must match byte for byte; every request is read
by the old code and the struct and must give the same values; plus
golden bytes, round trips, truncation checks and a field width mutation
that made the tests fail. Only differences: malformed requests are
dropped instead of handled with partial values, and LevelLoadComplete now
reads the zone ID the client sends after it (lu_packets and captures show
the 1.10.64 client always sends it; DLU ignored it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-26 23:10:57 -05:00
parent efa08ac1d0
commit b489ee59f8
21 changed files with 3040 additions and 907 deletions

View File

@@ -5,7 +5,15 @@
#include "ClientPackets.h"
#include "dCommonVars.h"
#include "PositionUpdate.h"
#include "eCharacterCreationResponse.h"
#include "eGameMasterLevel.h"
#include "eRenameResponse.h"
#include "LDFFormat.h"
#include "ZCompression.h"
#include <cassert>
#include <memory>
#include <ranges>
#include "eLoginResponse.h"
namespace ClientPackets {
@@ -61,122 +69,374 @@ namespace ClientPackets {
VALIDATE_READ(stamps.Deserialize(bitStream));
return true;
}
}
ChatMessage ClientPackets::HandleChatMessage(Packet* packet) {
CINSTREAM_SKIP_HEADER;
ChatMessage message;
int32_t messageLength{};
inStream.Read(message.chatChannel);
inStream.Read(message.unknown);
inStream.Read(messageLength);
if (messageLength > MAX_MESSAGE_LENGTH || messageLength < 0) return message;
for (int32_t i = 0; i < (messageLength - 1); ++i) {
char16_t character;
inStream.Read(character);
message.message.push_back(character);
void LoadStaticZone::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(mapID);
bitStream.Write(instanceID);
bitStream.Write(cloneID);
bitStream.Write(mapChecksum);
bitStream.Write(editorEnabled);
bitStream.Write(editorLevel);
bitStream.Write(playerPosition.x);
bitStream.Write(playerPosition.y);
bitStream.Write(playerPosition.z);
bitStream.Write(instanceType);
}
return message;
}
PositionUpdate ClientPackets::HandleClientPositionUpdate(Packet* packet) {
PositionUpdate update;
CINSTREAM_SKIP_HEADER;
inStream.Read(update.position.x);
inStream.Read(update.position.y);
inStream.Read(update.position.z);
inStream.Read(update.rotation.x);
inStream.Read(update.rotation.y);
inStream.Read(update.rotation.z);
inStream.Read(update.rotation.w);
inStream.Read(update.onGround);
inStream.Read(update.onRail);
bool velocityFlag = false;
inStream.Read(velocityFlag);
if (velocityFlag) {
inStream.Read(update.velocity.x);
inStream.Read(update.velocity.y);
inStream.Read(update.velocity.z);
bool LoadStaticZone::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(mapID));
VALIDATE_READ(bitStream.Read(instanceID));
VALIDATE_READ(bitStream.Read(cloneID));
VALIDATE_READ(bitStream.Read(mapChecksum));
VALIDATE_READ(bitStream.Read(editorEnabled));
VALIDATE_READ(bitStream.Read(editorLevel));
VALIDATE_READ(bitStream.Read(playerPosition.x));
VALIDATE_READ(bitStream.Read(playerPosition.y));
VALIDATE_READ(bitStream.Read(playerPosition.z));
VALIDATE_READ(bitStream.Read(instanceType));
return true;
}
bool angVelocityFlag = false;
inStream.Read(angVelocityFlag);
if (angVelocityFlag) {
inStream.Read(update.angularVelocity.x);
inStream.Read(update.angularVelocity.y);
inStream.Read(update.angularVelocity.z);
}
void CharacterListResponse::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write<uint8_t>(characters.size());
bitStream.Write(selectedCharacterIndex);
// TODO figure out how to use these. Ignoring for now, but reading in if they exist.
bool hasLocalSpaceInfo{};
if (inStream.Read(hasLocalSpaceInfo) && hasLocalSpaceInfo) {
inStream.Read(update.localSpaceInfo.objectId);
inStream.Read(update.localSpaceInfo.position.x);
inStream.Read(update.localSpaceInfo.position.y);
inStream.Read(update.localSpaceInfo.position.z);
bool hasLinearVelocity = false;
if (inStream.Read(hasLinearVelocity) && hasLinearVelocity) {
inStream.Read(update.localSpaceInfo.linearVelocity.x);
inStream.Read(update.localSpaceInfo.linearVelocity.y);
inStream.Read(update.localSpaceInfo.linearVelocity.z);
for (const auto& character : characters) {
bitStream.Write(character.objectID);
bitStream.Write(character.unknown1);
bitStream.Write(character.name);
bitStream.Write(character.unapprovedName);
bitStream.Write<uint8_t>(character.nameRejected);
bitStream.Write<uint8_t>(character.isFreeToPlay);
bitStream.Write(character.unknown2);
bitStream.Write(character.shirtColor);
bitStream.Write(character.shirtStyle);
bitStream.Write(character.pantsColor);
bitStream.Write(character.hairStyle);
bitStream.Write(character.hairColor);
bitStream.Write(character.leftHand);
bitStream.Write(character.rightHand);
bitStream.Write(character.eyebrows);
bitStream.Write(character.eyes);
bitStream.Write(character.mouth);
bitStream.Write(character.unknown3);
bitStream.Write(character.zoneID);
bitStream.Write(character.zoneInstance);
bitStream.Write(character.zoneClone);
bitStream.Write(character.lastLogin);
bitStream.Write<uint16_t>(character.equippedItems.size());
for (const auto item : character.equippedItems) bitStream.Write(item);
}
}
bool hasRemoteInputInfo{};
if (inStream.Read(hasRemoteInputInfo) && hasRemoteInputInfo) {
inStream.Read(update.remoteInputInfo.m_RemoteInputX);
inStream.Read(update.remoteInputInfo.m_RemoteInputY);
inStream.Read(update.remoteInputInfo.m_IsPowersliding);
inStream.Read(update.remoteInputInfo.m_IsModified);
bool CharacterListResponse::Deserialize(RakNet::BitStream& bitStream) {
uint8_t count{};
VALIDATE_READ(bitStream.Read(count));
VALIDATE_READ(bitStream.Read(selectedCharacterIndex));
characters.resize(count);
for (auto& character : characters) {
VALIDATE_READ(bitStream.Read(character.objectID));
VALIDATE_READ(bitStream.Read(character.unknown1));
VALIDATE_READ(bitStream.Read(character.name));
VALIDATE_READ(bitStream.Read(character.unapprovedName));
uint8_t flag{};
VALIDATE_READ(bitStream.Read(flag));
character.nameRejected = flag != 0;
VALIDATE_READ(bitStream.Read(flag));
character.isFreeToPlay = flag != 0;
VALIDATE_READ(bitStream.Read(character.unknown2));
VALIDATE_READ(bitStream.Read(character.shirtColor));
VALIDATE_READ(bitStream.Read(character.shirtStyle));
VALIDATE_READ(bitStream.Read(character.pantsColor));
VALIDATE_READ(bitStream.Read(character.hairStyle));
VALIDATE_READ(bitStream.Read(character.hairColor));
VALIDATE_READ(bitStream.Read(character.leftHand));
VALIDATE_READ(bitStream.Read(character.rightHand));
VALIDATE_READ(bitStream.Read(character.eyebrows));
VALIDATE_READ(bitStream.Read(character.eyes));
VALIDATE_READ(bitStream.Read(character.mouth));
VALIDATE_READ(bitStream.Read(character.unknown3));
VALIDATE_READ(bitStream.Read(character.zoneID));
VALIDATE_READ(bitStream.Read(character.zoneInstance));
VALIDATE_READ(bitStream.Read(character.zoneClone));
VALIDATE_READ(bitStream.Read(character.lastLogin));
uint16_t itemCount{};
VALIDATE_READ(bitStream.Read(itemCount));
if (itemCount > BITS_TO_BYTES(bitStream.GetNumberOfUnreadBits()) / sizeof(LOT)) return false;
character.equippedItems.resize(itemCount);
for (auto& item : character.equippedItems) VALIDATE_READ(bitStream.Read(item));
}
return true;
}
return update;
}
ChatModerationRequest ClientPackets::HandleChatModerationRequest(Packet* packet) {
CINSTREAM_SKIP_HEADER;
ChatModerationRequest request;
inStream.Read(request.chatLevel);
inStream.Read(request.requestID);
for (uint32_t i = 0; i < 42; ++i) {
uint16_t character;
inStream.Read(character);
request.receiver.push_back(static_cast<uint8_t>(character));
void CharacterCreateResponse::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(response);
}
if (!request.receiver.empty()) {
if (std::string(request.receiver.c_str(), 4) == "[GM]") { // Shift the string forward if we are speaking to a GM as the client appends "[GM]" if they are
request.receiver = std::string(request.receiver.c_str() + 4, request.receiver.size() - 4);
bool CharacterCreateResponse::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(response));
return true;
}
void CharacterRenameResponse::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(response);
}
bool CharacterRenameResponse::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(response));
return true;
}
void DeleteCharacterResponse::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write<uint8_t>(success);
}
bool DeleteCharacterResponse::Deserialize(RakNet::BitStream& bitStream) {
uint8_t flag{};
VALIDATE_READ(bitStream.Read(flag));
success = flag != 0;
return true;
}
void TransferToWorld::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(serverIP);
bitStream.Write(serverPort);
bitStream.Write<uint8_t>(mythranShift);
}
bool TransferToWorld::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(serverIP));
VALIDATE_READ(bitStream.Read(serverPort));
uint8_t flag{};
VALIDATE_READ(bitStream.Read(flag));
mythranShift = flag != 0;
return true;
}
void ServerStates::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(serverState);
}
bool ServerStates::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(serverState));
return true;
}
void CreateCharacter::Serialize(RakNet::BitStream& bitStream) const {
using namespace std;
RakNet::BitStream data;
LwoNameValue ldfData;
ldfData.Insert<LWOOBJID>(u"objid", objectID);
ldfData.Insert<LOT>(u"template", templateID);
ldfData.Insert<string>(u"xmlData", xmlData);
ldfData.Insert<u16string>(u"name", name);
ldfData.Insert<int32_t>(u"gmlevel", static_cast<int32_t>(gmLevel));
ldfData.Insert<int32_t>(u"chatmode", chatMode);
ldfData.Insert<int64_t>(u"reputation", reputation);
ldfData.Insert<int32_t>(u"propertycloneid", propertyCloneID);
data.Write<uint32_t>(ldfData.values.size());
for (const auto& toSerialize : ldfData | std::views::values) toSerialize->WriteToPacket(data);
//Compress the data before sending:
const uint32_t reservedSize = ZCompression::GetMaxCompressedLength(data.GetNumberOfBytesUsed());
auto compressedData = std::make_unique<uint8_t[]>(reservedSize);
size_t size = ZCompression::Compress(data.GetData(), data.GetNumberOfBytesUsed(), compressedData.get(), reservedSize);
assert(size <= reservedSize);
bitStream.Write<uint32_t>(size + 9); //size of data + header bytes (8)
bitStream.Write<uint8_t>(1); //compressed boolean, true
bitStream.Write<uint32_t>(data.GetNumberOfBytesUsed());
bitStream.Write<uint32_t>(size);
/**
* In practice, this warning serves no purpose for us. We allocate the max memory needed on the heap
* and then compress the data. In the off chance that the compression actually increases the size,
* an assertion is done to prevent bad data from being saved or sent.
*/
#pragma warning(disable:6385) // C6385 Reading invalid data from 'compressedData'.
bitStream.WriteAlignedBytes(compressedData.get(), size);
#pragma warning(default:6385)
}
namespace {
bool ReadLdfKey(RakNet::BitStream& bitStream, std::u16string& key) {
uint8_t keyBytes{};
VALIDATE_READ(bitStream.Read(keyBytes));
key.resize(keyBytes / sizeof(char16_t));
for (auto& character : key) VALIDATE_READ(bitStream.Read(character));
return true;
}
}
uint16_t messageLength;
inStream.Read(messageLength);
if (messageLength > MAX_MESSAGE_LENGTH) return request;
for (uint32_t i = 0; i < messageLength; ++i) {
uint16_t character;
inStream.Read(character);
request.message.push_back(static_cast<uint8_t>(character));
bool CreateCharacter::Deserialize(RakNet::BitStream& bitStream) {
uint32_t sizePlusHeader{};
uint8_t compressed{};
uint32_t uncompressedSize{};
uint32_t compressedSize{};
VALIDATE_READ(bitStream.Read(sizePlusHeader));
VALIDATE_READ(bitStream.Read(compressed));
VALIDATE_READ(bitStream.Read(uncompressedSize));
VALIDATE_READ(bitStream.Read(compressedSize));
if (compressed != 1 || sizePlusHeader != compressedSize + 9) return false;
if (compressedSize > BITS_TO_BYTES(bitStream.GetNumberOfUnreadBits()) || uncompressedSize > MAX_MESSAGE_LENGTH) return false;
std::vector<uint8_t> compressedData(compressedSize);
if (compressedSize > 0) VALIDATE_READ(bitStream.ReadAlignedBytes(compressedData.data(), compressedSize));
std::vector<uint8_t> uncompressed(uncompressedSize);
int32_t error{};
const auto decompressedSize = ZCompression::Decompress(compressedData.data(), compressedSize, uncompressed.data(), uncompressedSize, error);
if (decompressedSize != static_cast<int32_t>(uncompressedSize)) return false;
RakNet::BitStream data(uncompressed.data(), uncompressed.size(), false);
uint32_t count{};
VALIDATE_READ(data.Read(count));
for (uint32_t i = 0; i < count; i++) {
std::u16string key;
VALIDATE_READ(ReadLdfKey(data, key));
uint8_t type{};
VALIDATE_READ(data.Read(type));
switch (type) {
case LDF_TYPE_S32: {
int32_t value{};
VALIDATE_READ(data.Read(value));
if (key == u"template") templateID = value;
else if (key == u"gmlevel") gmLevel = static_cast<eGameMasterLevel>(value);
else if (key == u"chatmode") chatMode = value;
else if (key == u"propertycloneid") propertyCloneID = value;
break;
}
case LDF_TYPE_OBJID: {
int64_t value{};
VALIDATE_READ(data.Read(value));
if (key == u"objid") objectID = value;
else if (key == u"reputation") reputation = value;
break;
}
case LDF_TYPE_UTF_16: {
std::u16string value;
VALIDATE_READ(BitStreamUtils::ReadLengthPrefixed<uint32_t>(data, value));
if (key == u"name") name = value;
break;
}
case LDF_TYPE_UTF_8: {
std::string value;
VALIDATE_READ(BitStreamUtils::ReadLengthPrefixed<uint32_t>(data, value));
if (key == u"xmlData") xmlData = value;
break;
}
default:
return false; // CreateCharacter never uses other types
}
}
return true;
}
return request;
}
void ChatModerationString::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write<uint8_t>(requestAccepted); // Is sentence ok?
bitStream.Write(sourceID); // Source ID, unknown
int32_t ClientPackets::SendTop5HelpIssues(Packet* packet) {
CINSTREAM_SKIP_HEADER;
int32_t language = 0;
inStream.Read(language);
return language;
bitStream.Write(requestID); // request ID
bitStream.Write(chatMode); // chat mode
bitStream.Write(receiver); // receiver name
for (const auto& [start, length] : rejectedSegments) {
bitStream.Write(start); // start index
bitStream.Write(length); // length
}
for (auto i = rejectedSegments.size(); i < 64; i++) {
bitStream.Write<uint16_t>(0);
}
}
bool ChatModerationString::Deserialize(RakNet::BitStream& bitStream) {
uint8_t accepted{};
VALIDATE_READ(bitStream.Read(accepted));
requestAccepted = accepted != 0;
VALIDATE_READ(bitStream.Read(sourceID));
VALIDATE_READ(bitStream.Read(requestID));
VALIDATE_READ(bitStream.Read(chatMode));
VALIDATE_READ(bitStream.Read(receiver));
rejectedSegments.clear();
for (int i = 0; i < 64; i++) {
uint8_t start{};
uint8_t length{};
VALIDATE_READ(bitStream.Read(start));
VALIDATE_READ(bitStream.Read(length));
if (start != 0 || length != 0) rejectedSegments.emplace(start, length);
}
return true;
}
void MakeGMResponse::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write<uint8_t>(success);
bitStream.Write(static_cast<uint16_t>(highestLevel));
bitStream.Write(static_cast<uint16_t>(previousLevel));
bitStream.Write(static_cast<uint16_t>(newLevel));
}
bool MakeGMResponse::Deserialize(RakNet::BitStream& bitStream) {
uint8_t flag{};
uint16_t highest{};
uint16_t previous{};
uint16_t next{};
VALIDATE_READ(bitStream.Read(flag));
VALIDATE_READ(bitStream.Read(highest));
VALIDATE_READ(bitStream.Read(previous));
VALIDATE_READ(bitStream.Read(next));
success = flag != 0;
highestLevel = static_cast<eGameMasterLevel>(highest);
previousLevel = static_cast<eGameMasterLevel>(previous);
newLevel = static_cast<eGameMasterLevel>(next);
return true;
}
void HTTPMonitorInfoResponse::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(port);
bitStream.Write<uint8_t>(openWeb);
bitStream.Write<uint8_t>(supportsSum);
bitStream.Write<uint8_t>(supportsDetail);
bitStream.Write<uint8_t>(supportsWho);
bitStream.Write<uint8_t>(supportsObjects);
}
bool HTTPMonitorInfoResponse::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(port));
for (auto* flag : { &openWeb, &supportsSum, &supportsDetail, &supportsWho, &supportsObjects }) {
uint8_t value{};
VALIDATE_READ(bitStream.Read(value));
*flag = value != 0;
}
return true;
}
void DebugOutput::Serialize(RakNet::BitStream& bitStream) const {
BitStreamUtils::WriteLengthPrefixed<uint32_t>(bitStream, data);
}
bool DebugOutput::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(BitStreamUtils::ReadLengthPrefixed<uint32_t>(bitStream, data));
return true;
}
}