Files
DarkflameServer/dGame/dGameMessages/GameMessageDecoder.cpp
Aaron Kimbrell 5a9a3e380b refactor: master packets as structs
Every MASTER service packet is now an LUBitStream struct (docs/PacketArchitecture.md,
PR 14) and the master server's switch is a dispatch map (PacketDispatcher), as are the
master handlers of the world, chat and dashboard servers.

- dNet/MasterPackets.h: RequestZoneTransfer, RequestZoneTransferResponse, ServerInfo,
  RequestSessionKey, SetSessionKey, SessionKeyResponse, NewSessionAlert, PlayerAdded /
  PlayerRemoved, CreatePrivateZone, RequestPrivateZone (passwords still cut to 50
  characters when read), WorldReady, WorldReadyInfo (WORLD_READY to the dashboard),
  PrepZone, Shutdown, ShutdownResponse, WorldShutDown (SHUTDOWN_RESPONSE to the
  dashboard), ShutdownUniverse, AffirmTransferRequest/Response, RequestServerList,
  ServerListResponse, DashboardShutdown, ConfigReload, InstanceShutdown. The Send*
  functions are gone; MasterPackets::SendToMaster(msg) and SendTo(sysAddr, msg) send a
  struct.
- The dashboard and instance migration structs (PlayerAction, DataChanged, Dashboard
  messages, MessageCapture, InstanceMigration) are LUBitStreams of the MASTER service now
  and moved to dNet/master/, included by MasterPackets.h. Their payloads are unchanged;
  master forwards them by re-serializing the struct instead of copying raw bytes.
- InstanceManager, ZoneInstanceManager, MigrationCoordinator, dServer (server info, zone
  transfer response), auth (SET_SESSION_KEY), the world (session keys, player added and
  removed, world ready, shutdown response, affirmations, prep zone, shutdown universe) and
  the dashboard (server list, instance shutdown, config reload, announcements, player
  actions, message capture) send and read structs.
- The login stamps are a `stamps` field of RequestZoneTransfer and
  RequestZoneTransferResponse (read leniently as before: a message without them reads as
  empty); master adds its stamps in the REQUEST_ZONE_TRANSFER handler and when it answers,
  as it did.
- InstanceManager::GetInstanceBySysAddr takes a const address.

Verified: tests/dGameTests/dNetTests/Legacy/MasterPacketsLegacy.h is a verbatim copy of
the old writers and readers; MasterPacketsTests requires identical bytes for a grid of
inputs, checks the old readers read what the structs write, round trips and truncation,
hand written golden packets, that zone transfers without stamps still read, that the dashboard/migration structs write what
"header + Serialize" wrote, and that the dispatcher drops truncated packets. No wire
bytes changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:30:50 -05:00

100 lines
4.9 KiB
C++

#include "GameMessageDecoder.h"
#include <functional>
#include <map>
#include <string>
#include <utility>
#include "GameMessages.h"
#include "StartSkill.h"
#include "SyncSkill.h"
#include "EchoStartSkill.h"
#include "EchoSyncSkill.h"
#include "RequestServerProjectileImpact.h"
#include "DoClientProjectileImpact.h"
#include "master/MessageCapture.h"
namespace {
using json = nlohmann::json;
using Decoder = std::function<std::optional<json>(RakNet::BitStream&)>;
// Object IDs as strings: they don't fit in a JavaScript number
std::string Id(LWOOBJID id) { return std::to_string(id); }
json Point(const NiPoint3& point) { return json::array({ point.x, point.y, point.z }); }
json Rotation(const NiQuaternion& rotation) { return json::array({ rotation.x, rotation.y, rotation.z, rotation.w }); }
// A skill's behavior data, which is its own bit stream
json Bytes(const std::string& bytes) { return MessageCapture::ToHex(bytes); }
// Reads T with its Deserialize, then lists its fields with `fields`
template<typename T>
Decoder Make(std::function<void(const T&, json&)> fields) {
return [fields](RakNet::BitStream& stream) -> std::optional<json> {
T message;
if (!message.Deserialize(stream)) return std::nullopt;
json out = json::object();
fields(message, out);
return out;
};
}
// (to server, message ID) -> decoder
const std::map<std::pair<bool, MessageType::Game>, Decoder>& Decoders() {
using namespace GameMessages;
using enum MessageType::Game;
static const std::map<std::pair<bool, MessageType::Game>, Decoder> decoders{
// Sent by the client
{ { true, REQUEST_USE }, Make<RequestUse>([](const RequestUse& m, json& j) {
j = { {"bIsMultiInteractUse", m.bIsMultiInteractUse}, {"multiInteractID", m.multiInteractID},
{"multiInteractType", m.multiInteractType}, {"object", Id(m.object)}, {"secondary", m.secondary} };
}) },
{ { true, REQUEST_SERVER_OBJECT_INFO }, Make<RequestServerObjectInfo>([](const RequestServerObjectInfo& m, json& j) {
j = { {"bVerbose", m.bVerbose}, {"clientId", Id(m.clientId)}, {"targetForReport", Id(m.targetForReport)} };
}) },
{ { true, SHOOTING_GALLERY_FIRE }, Make<ShootingGalleryFire>([](const ShootingGalleryFire& m, json& j) {
j = { {"target", Point(m.target)}, {"rotation", Rotation(m.rotation)} };
}) },
{ { true, PICKUP_ITEM }, Make<PickupItem>([](const PickupItem& m, json& j) {
j = { {"lootID", Id(m.lootID)}, {"lootOwnerID", Id(m.lootOwnerID)} };
}) },
{ { true, START_SKILL }, Make<StartSkill>([](const StartSkill& m, json& j) {
j = { {"bUsedMouse", m.bUsedMouse}, {"consumableItemID", Id(m.consumableItemID)}, {"fCasterLatency", m.fCasterLatency},
{"iCastType", m.iCastType}, {"lastClickedPosit", Point(m.lastClickedPosit)}, {"optionalOriginatorID", Id(m.optionalOriginatorID)},
{"optionalTargetID", Id(m.optionalTargetID)}, {"originatorRot", Rotation(m.originatorRot)}, {"sBitStream", Bytes(m.sBitStream)},
{"skillID", m.skillID}, {"uiSkillHandle", m.uiSkillHandle} };
}) },
{ { true, SYNC_SKILL }, Make<SyncSkill>([](const SyncSkill& m, json& j) {
j = { {"bDone", m.bDone}, {"sBitStream", Bytes(m.sBitStream)}, {"uiBehaviorHandle", m.uiBehaviorHandle}, {"uiSkillHandle", m.uiSkillHandle} };
}) },
{ { true, REQUEST_SERVER_PROJECTILE_IMPACT }, Make<RequestServerProjectileImpact>([](const RequestServerProjectileImpact& m, json& j) {
j = { {"i64LocalID", Id(m.i64LocalID)}, {"i64TargetID", Id(m.i64TargetID)}, {"sBitStream", Bytes(m.sBitStream)} };
}) },
// Sent to the client
{ { false, ECHO_START_SKILL }, Make<EchoStartSkill>([](const EchoStartSkill& m, json& j) {
j = { {"bUsedMouse", m.bUsedMouse}, {"fCasterLatency", m.fCasterLatency}, {"iCastType", m.iCastType},
{"lastClickedPosit", Point(m.lastClickedPosit)}, {"optionalOriginatorID", Id(m.optionalOriginatorID)},
{"optionalTargetID", Id(m.optionalTargetID)}, {"originatorRot", Rotation(m.originatorRot)}, {"sBitStream", Bytes(m.sBitStream)},
{"skillID", m.skillID}, {"uiSkillHandle", m.uiSkillHandle} };
}) },
{ { false, ECHO_SYNC_SKILL }, Make<EchoSyncSkill>([](const EchoSyncSkill& m, json& j) {
j = { {"bDone", m.bDone}, {"sBitStream", Bytes(m.sBitStream)}, {"uiBehaviorHandle", m.uiBehaviorHandle}, {"uiSkillHandle", m.uiSkillHandle} };
}) },
{ { false, DO_CLIENT_PROJECTILE_IMPACT }, Make<DoClientProjectileImpact>([](const DoClientProjectileImpact& m, json& j) {
j = { {"i64OrgID", Id(m.i64OrgID)}, {"i64OwnerID", Id(m.i64OwnerID)}, {"i64TargetID", Id(m.i64TargetID)}, {"sBitStream", Bytes(m.sBitStream)} };
}) },
};
return decoders;
}
}
namespace GameMessageDecoder {
bool CanDecode(MessageType::Game messageId, bool toServer) {
return Decoders().contains({ toServer, messageId });
}
std::optional<nlohmann::json> Decode(MessageType::Game messageId, bool toServer, RakNet::BitStream& payload) {
const auto it = Decoders().find({ toServer, messageId });
if (it == Decoders().end()) return std::nullopt;
return it->second(payload);
}
}