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

@@ -150,3 +150,15 @@ void ChatPackets::SendRoutedMsg(const LUBitStream& msg, const LWOOBJID targetID,
msg.WritePacket(bitStream);
Game::server->Send(bitStream, sysAddr, sysAddr == UNASSIGNED_SYSTEM_ADDRESS);
}
void ChatPackets::RoutedFromClient::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(senderID);
for (const auto byte : data) bitStream.Write(byte);
}
bool ChatPackets::RoutedFromClient::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(senderID));
data.resize(BITS_TO_BYTES(bitStream.GetNumberOfUnreadBits()));
if (!data.empty()) VALIDATE_READ(bitStream.ReadBits(data.data(), BYTES_TO_BITS(data.size()), true));
return true;
}

View File

@@ -9,6 +9,7 @@
struct SystemAddress;
#include <string>
#include <vector>
#include "dCommonVars.h"
#include "MessageType/Chat.h"
#include "BitStreamUtils.h"
@@ -57,6 +58,17 @@ namespace ChatPackets {
// No Deserialize needed on our end
};
// World -> chat. A chat packet the client sent through the world server (WorldPackets::RoutePacket): its id,
// the object ID of the player who sent it (the chat server has no other way to know), then the routed bytes.
struct RoutedFromClient : public LUBitStream {
LWOOBJID senderID{};
std::vector<uint8_t> data{};
RoutedFromClient(uint8_t routedMessageID = 0) : LUBitStream(ServiceType::CHAT, routedMessageID) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
void SendChatMessage(const SystemAddress& sysAddr, char chatChannel, const std::string& senderName, LWOOBJID playerObjectID, bool senderMythran, const std::u16string& message);
void SendSystemMessage(const SystemAddress& sysAddr, const std::string& message, bool broadcast = false);
void SendSystemMessage(const SystemAddress& sysAddr, const std::u16string& message, bool broadcast = false);

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;
}
}

View File

@@ -8,19 +8,23 @@
#include <cstdint>
#include <ctime>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "BitStreamUtils.h"
#include "MessageType/Client.h"
#include "Stamps.h"
#include "dCommonVars.h"
#include "NiPoint3.h"
enum class eCharacterCreationResponse : uint8_t;
enum class eGameMasterLevel : uint8_t;
enum class eRenameResponse : uint8_t;
enum class eLoginResponse : uint8_t;
class PositionUpdate;
struct Packet;
enum class Language : uint32_t {
en_US,
pl_US,
@@ -28,19 +32,6 @@ enum class Language : uint32_t {
en_GB,
};
struct ChatMessage {
uint8_t chatChannel = 0;
uint16_t unknown = 0;
std::u16string message;
};
struct ChatModerationRequest {
uint8_t chatLevel = 0;
uint8_t requestID = 0;
std::string receiver;
std::string message;
};
namespace ClientPackets {
// Server -> client. The auth server's answer to a login (the client's clientLoginResponsePacket).
struct LoginResponse : public LUBitStream {
@@ -73,10 +64,173 @@ namespace ClientPackets {
bool Deserialize(RakNet::BitStream& bitStream) override;
};
ChatMessage HandleChatMessage(Packet* packet);
PositionUpdate HandleClientPositionUpdate(Packet* packet);
ChatModerationRequest HandleChatModerationRequest(Packet* packet);
int32_t SendTop5HelpIssues(Packet* packet);
// Server -> client. Tells the client which zone to load (ClientLoadStaticZonePacket in the client).
struct LoadStaticZone : public LUBitStream {
uint16_t mapID{};
uint16_t instanceID{};
uint32_t cloneID{}; // DLU has always sent 0 here
uint32_t mapChecksum{};
uint8_t editorEnabled{};
uint8_t editorLevel{};
NiPoint3 playerPosition{};
uint32_t instanceType{}; // Change this to eventually use 4 on activity worlds
LoadStaticZone() : LUBitStream(ServiceType::CLIENT, MessageType::Client::LOAD_STATIC_ZONE) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client. The account's characters for the character select screen.
struct CharacterListResponse : public LUBitStream {
struct Character {
LWOOBJID objectID{};
uint32_t unknown1{};
LUWString name{ 33 };
LUWString unapprovedName{ 33 };
bool nameRejected{}; // Written as one byte
bool isFreeToPlay{}; // Written as one byte
LUString unknown2{ 10 };
uint32_t shirtColor{};
uint32_t shirtStyle{};
uint32_t pantsColor{};
uint32_t hairStyle{};
uint32_t hairColor{};
uint32_t leftHand{};
uint32_t rightHand{};
uint32_t eyebrows{};
uint32_t eyes{};
uint32_t mouth{};
uint32_t unknown3{};
uint16_t zoneID{};
uint16_t zoneInstance{};
uint32_t zoneClone{};
uint64_t lastLogin{};
std::vector<LOT> equippedItems{}; // After a u16 count
};
// Written as a u8 count first
uint8_t selectedCharacterIndex{}; // TODO: Pick the most recent played index. DLU always sends 0
std::vector<Character> characters{};
CharacterListResponse() : LUBitStream(ServiceType::CLIENT, MessageType::Client::CHARACTER_LIST_RESPONSE) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client.
struct CharacterCreateResponse : public LUBitStream {
eCharacterCreationResponse response{};
CharacterCreateResponse() : LUBitStream(ServiceType::CLIENT, MessageType::Client::CHARACTER_CREATE_RESPONSE) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client.
struct CharacterRenameResponse : public LUBitStream {
eRenameResponse response{};
CharacterRenameResponse() : LUBitStream(ServiceType::CLIENT, MessageType::Client::CHARACTER_RENAME_RESPONSE) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client.
struct DeleteCharacterResponse : public LUBitStream {
bool success{}; // Written as one byte
DeleteCharacterResponse() : LUBitStream(ServiceType::CLIENT, MessageType::Client::DELETE_CHARACTER_RESPONSE) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client. Sends the client to another world server.
struct TransferToWorld : public LUBitStream {
LUString serverIP{ 33 };
uint16_t serverPort{};
bool mythranShift{}; // Written as one byte
TransferToWorld() : LUBitStream(ServiceType::CLIENT, MessageType::Client::TRANSFER_TO_WORLD) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client.
struct ServerStates : public LUBitStream {
uint8_t serverState = 1; // If the server is receiving this request, it probably is ready anyway.
ServerStates() : LUBitStream(ServiceType::CLIENT, MessageType::Client::SERVER_STATES) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client. The player's own character: a compressed LDF list.
// On the wire: u32 compressed size + 9, u8 1 (compressed), u32 uncompressed size, u32 compressed size, the zlib data.
struct CreateCharacter : public LUBitStream {
LWOOBJID objectID{};
LOT templateID = 1;
std::string xmlData{};
std::u16string name{};
eGameMasterLevel gmLevel{};
int32_t chatMode{}; // DLU sends the GM level
int64_t reputation{};
int32_t propertyCloneID{};
CreateCharacter() : LUBitStream(ServiceType::CLIENT, MessageType::Client::CREATE_CHARACTER) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client. The answer to WorldPackets::StringCheck.
struct ChatModerationString : public LUBitStream {
bool requestAccepted{}; // Written as one byte
uint16_t sourceID = 0x16; // Unknown
uint8_t requestID{};
uint8_t chatMode{};
LUWString receiver{ 42 };
// (start, length) of each part that was not accepted, in 64 two byte slots; unused slots are 0
std::set<std::pair<uint8_t, uint8_t>> rejectedSegments{};
ChatModerationString() : LUBitStream(ServiceType::CLIENT, MessageType::Client::CHAT_MODERATION_STRING) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client.
struct MakeGMResponse : public LUBitStream {
bool success{}; // Written as one byte
eGameMasterLevel highestLevel{}; // Written as u16
eGameMasterLevel previousLevel{}; // Written as u16
eGameMasterLevel newLevel{}; // Written as u16
MakeGMResponse() : LUBitStream(ServiceType::CLIENT, MessageType::Client::MAKE_GM_RESPONSE) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client. DLU does not send it.
struct HTTPMonitorInfoResponse : public LUBitStream {
uint16_t port = 80;
// Each written as one byte
bool openWeb = false;
bool supportsSum = false;
bool supportsDetail = false;
bool supportsWho = false;
bool supportsObjects = false;
HTTPMonitorInfoResponse() : LUBitStream(ServiceType::CLIENT, MessageType::Client::HTTP_MONITOR_INFO_RESPONSE) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Server -> client. Text for the client's debug output. DLU does not send it.
struct DebugOutput : public LUBitStream {
std::string data{}; // After a u32 length
DebugOutput() : LUBitStream(ServiceType::CLIENT, MessageType::Client::DEBUG_OUTPUT) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
};
#endif // CLIENTPACKETS_H

View File

@@ -1,180 +1,351 @@
#include "WorldPackets.h"
#include "dCommonVars.h"
#include "BitStream.h"
#include "GeneralUtils.h"
#include "Logger.h"
#include "Game.h"
#include "LDFFormat.h"
#include "dServer.h"
#include "ZCompression.h"
#include "ServiceType.h"
#include "BitStreamUtils.h"
#include <iostream>
#include <ranges>
#include <algorithm>
void HTTPMonitorInfo::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);
}
void WorldPackets::SendLoadStaticZone(const SystemAddress& sysAddr, float x, float y, float z, uint32_t checksum, LWOZONEID zone) {
RakNet::BitStream bitStream;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::LOAD_STATIC_ZONE);
bitStream.Write<uint16_t>(zone.GetMapID());
bitStream.Write<uint16_t>(zone.GetInstanceID());
//bitStream.Write<uint32_t>(zone.GetCloneID());
bitStream.Write(0);
bitStream.Write(checksum);
bitStream.Write<uint16_t>(0); // ??
bitStream.Write(x);
bitStream.Write(y);
bitStream.Write(z);
bitStream.Write<uint32_t>(0); // Change this to eventually use 4 on activity worlds
SEND_PACKET;
}
void WorldPackets::SendCharacterCreationResponse(const SystemAddress& sysAddr, eCharacterCreationResponse response) {
RakNet::BitStream bitStream;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::CHARACTER_CREATE_RESPONSE);
bitStream.Write(response);
SEND_PACKET;
}
void WorldPackets::SendCharacterRenameResponse(const SystemAddress& sysAddr, eRenameResponse response) {
RakNet::BitStream bitStream;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::CHARACTER_RENAME_RESPONSE);
bitStream.Write(response);
SEND_PACKET;
}
void WorldPackets::SendCharacterDeleteResponse(const SystemAddress& sysAddr, bool response) {
RakNet::BitStream bitStream;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::DELETE_CHARACTER_RESPONSE);
bitStream.Write<uint8_t>(response);
SEND_PACKET;
}
void WorldPackets::SendTransferToWorld(const SystemAddress& sysAddr, const std::string& serverIP, uint32_t serverPort, bool mythranShift) {
RakNet::BitStream bitStream;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::TRANSFER_TO_WORLD);
bitStream.Write(LUString(serverIP));
bitStream.Write<uint16_t>(serverPort);
bitStream.Write<uint8_t>(mythranShift);
SEND_PACKET;
}
void WorldPackets::SendServerState(const SystemAddress& sysAddr) {
RakNet::BitStream bitStream;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::SERVER_STATES);
bitStream.Write<uint8_t>(1); //If the server is receiving this request, it probably is ready anyway.
SEND_PACKET;
}
void WorldPackets::SendCreateCharacter(const SystemAddress& sysAddr, int64_t reputation, LWOOBJID player, const std::string& xmlData, const std::u16string& username, eGameMasterLevel gm, const LWOCLONEID cloneID) {
using namespace std;
RakNet::BitStream bitStream;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::CREATE_CHARACTER);
RakNet::BitStream data;
LwoNameValue ldfData;
ldfData.Insert<LWOOBJID>(u"objid", player);
ldfData.Insert<LOT>(u"template", 1);
ldfData.Insert<string>(u"xmlData", xmlData);
ldfData.Insert<u16string>(u"name", username);
ldfData.Insert<int32_t>(u"gmlevel", static_cast<int32_t>(gm));
ldfData.Insert<int32_t>(u"chatmode", static_cast<int32_t>(gm));
ldfData.Insert<int64_t>(u"reputation", reputation);
ldfData.Insert<int32_t>(u"propertycloneid", cloneID);
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)
SEND_PACKET;
LOG("Sent CreateCharacter for ID: %llu", player);
}
void WorldPackets::SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::set<std::pair<uint8_t, uint8_t>> unacceptedItems) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::CHAT_MODERATION_STRING);
bitStream.Write<uint8_t>(unacceptedItems.empty()); // Is sentence ok?
bitStream.Write<uint16_t>(0x16); // Source ID, unknown
bitStream.Write<uint8_t>(requestID); // request ID
bitStream.Write<char>(0); // chat mode
bitStream.Write(LUWString(receiver, 42)); // receiver name
for (auto it : unacceptedItems) {
bitStream.Write<uint8_t>(it.first); // start index
bitStream.Write<uint8_t>(it.second); // length
namespace {
// Copies every unread bit of from into to.
bool ReadRest(RakNet::BitStream& from, RakNet::BitStream& to) {
to.Reset();
return from.Read(to, from.GetNumberOfUnreadBits());
}
for (int i = unacceptedItems.size(); 64 > i; i++) {
bitStream.Write<uint16_t>(0);
void WriteAll(RakNet::BitStream& to, const RakNet::BitStream& from) {
auto& nonConst = const_cast<RakNet::BitStream&>(from); // GetData is not const in RakNet
to.WriteBits(nonConst.GetData(), nonConst.GetNumberOfBitsUsed(), false);
}
}
namespace WorldPackets {
void Validation::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(username);
bitStream.Write(sessionKey);
bitStream.Write(fdbChecksum);
}
SEND_PACKET;
}
void WorldPackets::SendGMLevelChange(const SystemAddress& sysAddr, bool success, eGameMasterLevel highestLevel, eGameMasterLevel prevLevel, eGameMasterLevel newLevel) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::MAKE_GM_RESPONSE);
bitStream.Write<uint8_t>(success);
bitStream.Write(static_cast<uint16_t>(highestLevel));
bitStream.Write(static_cast<uint16_t>(prevLevel));
bitStream.Write(static_cast<uint16_t>(newLevel));
SEND_PACKET;
}
void WorldPackets::SendHTTPMonitorInfo(const SystemAddress& sysAddr, const HTTPMonitorInfo& info) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::HTTP_MONITOR_INFO_RESPONSE);
info.Serialize(bitStream);
SEND_PACKET;
}
void WorldPackets::SendDebugOuput(const SystemAddress& sysAddr, const std::string& data) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, ServiceType::CLIENT, MessageType::Client::DEBUG_OUTPUT);
bitStream.Write<uint32_t>(data.size());
bitStream.Write(data);
SEND_PACKET;
bool Validation::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(username));
// sometimes client puts a null terminator at the end of the checksum and sometimes doesn't, weird
VALIDATE_READ(bitStream.Read(sessionKey));
VALIDATE_READ(bitStream.Read(fdbChecksum));
return true;
}
void CharacterCreateRequest::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(name);
bitStream.Write(firstNameIndex);
bitStream.Write(middleNameIndex);
bitStream.Write(lastNameIndex);
for (const auto byte : unknown) bitStream.Write(byte);
bitStream.Write(shirtColor);
bitStream.Write(shirtStyle);
bitStream.Write(pantsColor);
bitStream.Write(hairStyle);
bitStream.Write(hairColor);
bitStream.Write(leftHand);
bitStream.Write(rightHand);
bitStream.Write(eyebrows);
bitStream.Write(eyes);
bitStream.Write(mouth);
}
bool CharacterCreateRequest::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(name));
VALIDATE_READ(bitStream.Read(firstNameIndex));
VALIDATE_READ(bitStream.Read(middleNameIndex));
VALIDATE_READ(bitStream.Read(lastNameIndex));
for (auto& byte : unknown) VALIDATE_READ(bitStream.Read(byte));
VALIDATE_READ(bitStream.Read(shirtColor));
VALIDATE_READ(bitStream.Read(shirtStyle));
VALIDATE_READ(bitStream.Read(pantsColor));
VALIDATE_READ(bitStream.Read(hairStyle));
VALIDATE_READ(bitStream.Read(hairColor));
VALIDATE_READ(bitStream.Read(leftHand));
VALIDATE_READ(bitStream.Read(rightHand));
VALIDATE_READ(bitStream.Read(eyebrows));
VALIDATE_READ(bitStream.Read(eyes));
VALIDATE_READ(bitStream.Read(mouth));
return true;
}
void CharacterLoginRequest::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(playerID);
}
bool CharacterLoginRequest::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(playerID));
return true;
}
void GameMessage::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(objectID);
bitStream.Write(messageID);
WriteAll(bitStream, data);
}
bool GameMessage::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(objectID));
VALIDATE_READ(bitStream.Read(messageID));
VALIDATE_READ(ReadRest(bitStream, data));
return true;
}
void CharacterDeleteRequest::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(objectID);
}
bool CharacterDeleteRequest::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(objectID));
return true;
}
void CharacterRenameRequest::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(objectID);
bitStream.Write(name);
}
bool CharacterRenameRequest::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(objectID));
VALIDATE_READ(bitStream.Read(name));
return true;
}
void LevelLoadComplete::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(mapID);
bitStream.Write(instanceID);
bitStream.Write(cloneID);
}
bool LevelLoadComplete::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(mapID));
VALIDATE_READ(bitStream.Read(instanceID));
VALIDATE_READ(bitStream.Read(cloneID));
return true;
}
void PositionUpdate::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(update.position.x);
bitStream.Write(update.position.y);
bitStream.Write(update.position.z);
bitStream.Write(update.rotation.x);
bitStream.Write(update.rotation.y);
bitStream.Write(update.rotation.z);
bitStream.Write(update.rotation.w);
bitStream.Write(update.onGround);
bitStream.Write(update.onRail);
bitStream.Write(hasVelocity);
if (hasVelocity) {
bitStream.Write(update.velocity.x);
bitStream.Write(update.velocity.y);
bitStream.Write(update.velocity.z);
}
bitStream.Write(hasAngularVelocity);
if (hasAngularVelocity) {
bitStream.Write(update.angularVelocity.x);
bitStream.Write(update.angularVelocity.y);
bitStream.Write(update.angularVelocity.z);
}
bitStream.Write(hasLocalSpaceInfo);
if (hasLocalSpaceInfo) {
bitStream.Write(update.localSpaceInfo.objectId);
bitStream.Write(update.localSpaceInfo.position.x);
bitStream.Write(update.localSpaceInfo.position.y);
bitStream.Write(update.localSpaceInfo.position.z);
bitStream.Write(hasLinearVelocity);
if (hasLinearVelocity) {
bitStream.Write(update.localSpaceInfo.linearVelocity.x);
bitStream.Write(update.localSpaceInfo.linearVelocity.y);
bitStream.Write(update.localSpaceInfo.linearVelocity.z);
}
}
bitStream.Write(hasRemoteInputInfo);
if (hasRemoteInputInfo) {
bitStream.Write(update.remoteInputInfo.m_RemoteInputX);
bitStream.Write(update.remoteInputInfo.m_RemoteInputY);
bitStream.Write(update.remoteInputInfo.m_IsPowersliding);
bitStream.Write(update.remoteInputInfo.m_IsModified);
}
}
bool PositionUpdate::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(update.position.x));
VALIDATE_READ(bitStream.Read(update.position.y));
VALIDATE_READ(bitStream.Read(update.position.z));
VALIDATE_READ(bitStream.Read(update.rotation.x));
VALIDATE_READ(bitStream.Read(update.rotation.y));
VALIDATE_READ(bitStream.Read(update.rotation.z));
VALIDATE_READ(bitStream.Read(update.rotation.w));
VALIDATE_READ(bitStream.Read(update.onGround));
VALIDATE_READ(bitStream.Read(update.onRail));
VALIDATE_READ(bitStream.Read(hasVelocity));
if (hasVelocity) {
VALIDATE_READ(bitStream.Read(update.velocity.x));
VALIDATE_READ(bitStream.Read(update.velocity.y));
VALIDATE_READ(bitStream.Read(update.velocity.z));
}
VALIDATE_READ(bitStream.Read(hasAngularVelocity));
if (hasAngularVelocity) {
VALIDATE_READ(bitStream.Read(update.angularVelocity.x));
VALIDATE_READ(bitStream.Read(update.angularVelocity.y));
VALIDATE_READ(bitStream.Read(update.angularVelocity.z));
}
// The last two sections may be missing entirely (DLU has always accepted that)
hasLocalSpaceInfo = false;
hasLinearVelocity = false;
if (bitStream.Read(hasLocalSpaceInfo) && hasLocalSpaceInfo) {
VALIDATE_READ(bitStream.Read(update.localSpaceInfo.objectId));
VALIDATE_READ(bitStream.Read(update.localSpaceInfo.position.x));
VALIDATE_READ(bitStream.Read(update.localSpaceInfo.position.y));
VALIDATE_READ(bitStream.Read(update.localSpaceInfo.position.z));
if (bitStream.Read(hasLinearVelocity) && hasLinearVelocity) {
VALIDATE_READ(bitStream.Read(update.localSpaceInfo.linearVelocity.x));
VALIDATE_READ(bitStream.Read(update.localSpaceInfo.linearVelocity.y));
VALIDATE_READ(bitStream.Read(update.localSpaceInfo.linearVelocity.z));
}
}
hasRemoteInputInfo = false;
if (bitStream.Read(hasRemoteInputInfo) && hasRemoteInputInfo) {
VALIDATE_READ(bitStream.Read(update.remoteInputInfo.m_RemoteInputX));
VALIDATE_READ(bitStream.Read(update.remoteInputInfo.m_RemoteInputY));
VALIDATE_READ(bitStream.Read(update.remoteInputInfo.m_IsPowersliding));
VALIDATE_READ(bitStream.Read(update.remoteInputInfo.m_IsModified));
}
return true;
}
void MailPacket::Serialize(RakNet::BitStream& bitStream) const {
WriteAll(bitStream, data);
}
bool MailPacket::Deserialize(RakNet::BitStream& bitStream) {
return ReadRest(bitStream, data);
}
void RoutePacket::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(size);
bitStream.Write(routedService);
bitStream.Write(routedMessageID);
bitStream.Write(padding);
for (const auto byte : routedData) bitStream.Write(byte);
}
bool RoutePacket::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(size));
VALIDATE_READ(bitStream.Read(routedService));
VALIDATE_READ(bitStream.Read(routedMessageID));
VALIDATE_READ(bitStream.Read(padding));
routedData.resize(BITS_TO_BYTES(bitStream.GetNumberOfUnreadBits()));
if (!routedData.empty()) VALIDATE_READ(bitStream.ReadBits(routedData.data(), BYTES_TO_BITS(routedData.size()), true));
return true;
}
ChatPackets::RoutedFromClient RoutePacket::ToChat(const LWOOBJID senderID) const {
constexpr size_t ROUTED_DATA_SKIP = 4;
ChatPackets::RoutedFromClient routed(static_cast<uint8_t>(routedMessageID & 0xFF));
routed.senderID = senderID;
for (size_t i = ROUTED_DATA_SKIP; i - ROUTED_DATA_SKIP < size && i < routedData.size(); ++i) {
routed.data.push_back(routedData[i]);
}
return routed;
}
void StringCheck::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(chatLevel);
bitStream.Write(requestID);
auto fixedReceiver = receiver;
fixedReceiver.resize(42);
bitStream.Write(fixedReceiver);
bitStream.Write<uint16_t>(message.size());
bitStream.Write(message);
}
bool StringCheck::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(chatLevel));
VALIDATE_READ(bitStream.Read(requestID));
receiver.resize(42);
VALIDATE_READ(bitStream.ReadBits(reinterpret_cast<unsigned char*>(receiver.data()), BYTES_TO_BITS(receiver.size() * sizeof(char16_t)), true));
VALIDATE_READ(BitStreamUtils::ReadLengthPrefixed<uint16_t>(bitStream, message));
return true;
}
std::string StringCheck::GetNarrowReceiver() const {
std::string narrow;
for (const auto character : receiver) narrow.push_back(static_cast<uint8_t>(character));
if (!narrow.empty()) {
if (std::string(narrow.c_str(), 4) == "[GM]") { // Shift the string forward if we are speaking to a GM as the client appends "[GM]" if they are
narrow = std::string(narrow.c_str() + 4, narrow.size() - 4);
}
}
return narrow;
}
std::string StringCheck::GetNarrowMessage() const {
std::string narrow;
for (const auto character : message) narrow.push_back(static_cast<uint8_t>(character));
return narrow;
}
void GeneralChatMessage::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(chatChannel);
bitStream.Write(unknown);
bitStream.Write<int32_t>(message.size() + 1);
bitStream.Write(message);
bitStream.Write<char16_t>(u'\0');
}
bool GeneralChatMessage::Deserialize(RakNet::BitStream& bitStream) {
int32_t messageLength{};
VALIDATE_READ(bitStream.Read(chatChannel));
VALIDATE_READ(bitStream.Read(unknown));
VALIDATE_READ(bitStream.Read(messageLength));
if (messageLength < 0 || static_cast<uint32_t>(messageLength) > MAX_MESSAGE_LENGTH) return false;
message.clear();
for (int32_t i = 0; i < (messageLength - 1); ++i) {
char16_t character;
VALIDATE_READ(bitStream.Read(character));
message.push_back(character);
}
// The null terminator the count includes; DLU never needed it
if (messageLength > 0) bitStream.IgnoreBits(std::min<BitSize_t>(bitStream.GetNumberOfUnreadBits(), BYTES_TO_BITS(sizeof(char16_t))));
return true;
}
void HandleFunness::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(cheatInfo);
bitStream.Write(cheatType);
}
bool HandleFunness::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(cheatInfo));
VALIDATE_READ(bitStream.Read(cheatType));
return true;
}
void UIHelpTop5::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(language);
}
bool UIHelpTop5::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(language));
return true;
}
}

View File

@@ -1,41 +1,214 @@
#ifndef WORLDPACKETS_H
#define WORLDPACKETS_H
#include "BitStreamUtils.h"
#include "ChatPackets.h"
#include "dCommonVars.h"
#include <vector>
#include "eFunnessTypes.h"
#include "MessageType/Game.h"
#include "MessageType/World.h"
#include "PositionUpdate.h"
#include "RakNetTypes.h"
#include <array>
#include <cstdint>
#include <string>
#include <vector>
class User;
struct SystemAddress;
enum class eGameMasterLevel : uint8_t;
enum class eCharacterCreationResponse : uint8_t;
enum class eRenameResponse : uint8_t;
namespace RakNet {
class BitStream;
};
struct HTTPMonitorInfo {
uint16_t port = 80;
bool openWeb = false;
bool supportsSum = false;
bool supportsDetail = false;
bool supportsWho = false;
bool supportsObjects = false;
void Serialize(RakNet::BitStream &bitstream) const;
};
// Packets of ServiceType::WORLD: what a client sends to a world server. The server's answers are ClientPackets.
// The structs only read and write bytes; the world server attaches the handlers (see WorldServer.cpp).
namespace WorldPackets {
void SendLoadStaticZone(const SystemAddress& sysAddr, float x, float y, float z, uint32_t checksum, LWOZONEID zone);
void SendCharacterCreationResponse(const SystemAddress& sysAddr, eCharacterCreationResponse response);
void SendCharacterRenameResponse(const SystemAddress& sysAddr, eRenameResponse response);
void SendCharacterDeleteResponse(const SystemAddress& sysAddr, bool response);
void SendTransferToWorld(const SystemAddress& sysAddr, const std::string& serverIP, uint32_t serverPort, bool mythranShift);
void SendServerState(const SystemAddress& sysAddr);
void SendCreateCharacter(const SystemAddress& sysAddr, int64_t reputation, LWOOBJID player, const std::string& xmlData, const std::u16string& username, eGameMasterLevel gm, const LWOCLONEID cloneID);
void SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::set<std::pair<uint8_t, uint8_t>> unacceptedItems);
void SendGMLevelChange(const SystemAddress& sysAddr, bool success, eGameMasterLevel highestLevel, eGameMasterLevel prevLevel, eGameMasterLevel newLevel);
void SendHTTPMonitorInfo(const SystemAddress& sysAddr, const HTTPMonitorInfo& info);
void SendDebugOuput(const SystemAddress& sysAddr, const std::string& data);
// Base of every world packet: the dispatcher sets sysAddr before Deserialize and Handle.
struct WorldLUBitStream : public LUBitStream {
SystemAddress sysAddr = UNASSIGNED_SYSTEM_ADDRESS;
WorldLUBitStream() = default;
template<typename T>
WorldLUBitStream(T internalPacketID) : LUBitStream(ServiceType::WORLD, internalPacketID) {}
};
// Client -> server. Who the client is and the session key auth gave it.
struct Validation : public WorldLUBitStream {
LUWString username{ 33 };
LUWString sessionKey{ 33 };
LUString fdbChecksum{ 32 };
// The client may send one more byte after the checksum (a null terminator); it is ignored.
Validation() : WorldLUBitStream(MessageType::World::VALIDATION) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server. No payload.
struct CharacterListRequest : public WorldLUBitStream {
CharacterListRequest() : WorldLUBitStream(MessageType::World::CHARACTER_LIST_REQUEST) {}
};
// Client -> server. A new character from the character creator.
struct CharacterCreateRequest : public WorldLUBitStream {
LUWString name{ 33 };
uint32_t firstNameIndex{};
uint32_t middleNameIndex{};
uint32_t lastNameIndex{};
std::array<uint8_t, 9> unknown{}; // Not used by DLU
uint32_t shirtColor{};
uint32_t shirtStyle{};
uint32_t pantsColor{};
uint32_t hairStyle{};
uint32_t hairColor{};
uint32_t leftHand{};
uint32_t rightHand{};
uint32_t eyebrows{};
uint32_t eyes{};
uint32_t mouth{};
// The client sends one more byte; it is ignored.
CharacterCreateRequest() : WorldLUBitStream(MessageType::World::CHARACTER_CREATE_REQUEST) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server. The character the player picked to play.
struct CharacterLoginRequest : public WorldLUBitStream {
LWOOBJID playerID{};
CharacterLoginRequest() : WorldLUBitStream(MessageType::World::LOGIN_REQUEST) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server. A game message; data holds its parameters for GameMessageHandler.
struct GameMessage : public WorldLUBitStream {
LWOOBJID objectID{};
MessageType::Game messageID{};
RakNet::BitStream data;
GameMessage() : WorldLUBitStream(MessageType::World::GAME_MSG) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server.
struct CharacterDeleteRequest : public WorldLUBitStream {
LWOOBJID objectID{};
CharacterDeleteRequest() : WorldLUBitStream(MessageType::World::CHARACTER_DELETE_REQUEST) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server.
struct CharacterRenameRequest : public WorldLUBitStream {
LWOOBJID objectID{};
LUWString name{ 33 };
CharacterRenameRequest() : WorldLUBitStream(MessageType::World::CHARACTER_RENAME_REQUEST) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server. The client finished loading the zone from LoadStaticZone.
struct LevelLoadComplete : public WorldLUBitStream {
// The zone the client loaded; not used by DLU
uint16_t mapID{};
uint16_t instanceID{};
uint32_t cloneID{};
LevelLoadComplete() : WorldLUBitStream(MessageType::World::LEVEL_LOAD_COMPLETE) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server. Where the player's character is.
struct PositionUpdate : public WorldLUBitStream {
::PositionUpdate update{};
// Which optional sections are present
bool hasVelocity{};
bool hasAngularVelocity{};
bool hasLocalSpaceInfo{};
bool hasLinearVelocity{}; // Inside the local space info
bool hasRemoteInputInfo{};
PositionUpdate() : WorldLUBitStream(MessageType::World::POSITION_UPDATE) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server. A mail request; data holds it for Mail::HandleMail (which has its own sub-header).
struct MailPacket : public WorldLUBitStream {
RakNet::BitStream data;
MailPacket() : WorldLUBitStream(MessageType::World::MAIL) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server. A packet for the chat server, wrapped: its size, then the chat packet's own header
// (service, id, pad) and body.
struct RoutePacket : public WorldLUBitStream {
uint32_t size{};
ServiceType routedService{};
uint32_t routedMessageID{};
uint8_t padding{};
std::vector<uint8_t> routedData{}; // Everything after the routed header
RoutePacket() : WorldLUBitStream(MessageType::World::ROUTE_PACKET) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
// What DLU forwards to the chat server: the first byte of the routed id, the sender, then at most size bytes
// of the routed data from 4 bytes in (the chat server skips 4 bytes after the sender ID it reads).
ChatPackets::RoutedFromClient ToChat(LWOOBJID senderID) const;
};
// Client -> server. Asks whether what the player is typing may be sent.
struct StringCheck : public WorldLUBitStream {
uint8_t chatLevel{};
uint8_t requestID{};
std::u16string receiver = std::u16string(42, u'\0'); // Always 42 characters on the wire
std::u16string message{}; // After a u16 character count
StringCheck() : WorldLUBitStream(MessageType::World::STRING_CHECK) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
// The receiver as DLU has always used it: each character cut to one byte, all 42 of them (the text is
// followed by nulls and whatever else the client left in its buffer), without a leading "[GM]".
std::string GetNarrowReceiver() const;
// The message with each character cut to one byte.
std::string GetNarrowMessage() const;
};
// Client -> server. A message in the zone's chat.
struct GeneralChatMessage : public WorldLUBitStream {
uint8_t chatChannel{};
uint16_t unknown{};
// On the wire: an i32 count that includes a null terminator, the characters, then the terminator
std::u16string message{};
GeneralChatMessage() : WorldLUBitStream(MessageType::World::GENERAL_CHAT_MESSAGE) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server. The client's anti cheat noticed something.
struct HandleFunness : public WorldLUBitStream {
float cheatInfo{};
eFunnessTypes cheatType{};
HandleFunness() : WorldLUBitStream(MessageType::World::HANDLE_FUNNESS) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
// Client -> server. The help screen wants its top 5 issues.
struct UIHelpTop5 : public WorldLUBitStream {
int32_t language{}; // 0: en_US, 1: pl_US, 2: de_DE, 3: en_GB
UIHelpTop5() : WorldLUBitStream(MessageType::World::UI_HELP_TOP_5) {}
void Serialize(RakNet::BitStream& bitStream) const override;
bool Deserialize(RakNet::BitStream& bitStream) override;
};
}
#endif // WORLDPACKETS_H