refactor: mission, flag and collectible game messages as structs

Converts OfferMission, NotifyMission, NotifyMissionTask, ResetMissions,
NotifyClientFlagChange and NotifyLevelRewards (sent with SendToClient:
the old functions never broadcast) and the received RespondToMission,
MissionDialogueOK, RequestLinkedMission, SetFlag and HasBeenCollected
to NetGameMsgs in MissionMessages.{h,cpp}. Callers are switched,
OfferMission's send-it-twice behaviour moves to MissionOfferComponent,
the received messages are registered in GameMessageHandler's map and
the old functions are deleted. Handlers are copied verbatim. The
byte-equality helper can now compare SendToClient messages.

No wire change and no change in recipients. Verified byte for byte
against a frozen verbatim copy of the old functions over an input grid
(including OfferMission's two packets), received messages compared with
the old handlers' read sequences and every truncated payload rejected,
hand computed golden bytes and round trips. Layouts confirmed against
the 1.10.64 client in Ghidra.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-26 12:44:51 -05:00
parent 9f142c9cc7
commit 55fd5aa8c3
16 changed files with 904 additions and 275 deletions

View File

@@ -2,6 +2,7 @@ SET(DGAMEMESSAGES_TESTS
"ActivityMessagesTests.cpp"
"GameMessageTests.cpp"
"GameMsgSplitTests.cpp"
"MissionMessagesTests.cpp"
"RacingMessagesTests.cpp"
"UnSmashTests.cpp"
"LegacyGameMessageTests.cpp")

View File

@@ -32,14 +32,18 @@ namespace GameMessageTestUtils {
return FromBitStream(bitStream);
}
// Sends the same message through the frozen legacy function and through the struct, to one client and as a
// broadcast, and requires identical bytes and the same effective destination.
// How the struct is sent in a comparison: Send (UNASSIGNED broadcasts; legacy functions that did
// "if (UNASSIGNED) SEND_PACKET_BROADCAST; SEND_PACKET;") or SendToClient (legacy functions with only SEND_PACKET).
enum class SendMode { Send, SendToClient };
// Sends the same message through the frozen legacy function and through the struct, to one client and to
// UNASSIGNED_SYSTEM_ADDRESS, and requires identical bytes and the same effective destination.
// legacySend receives the address to send to.
inline void ExpectSameAsLegacy(const std::function<void(const SystemAddress&)>& legacySend, const GameMessages::NetGameMsg& msg) {
inline void ExpectSameAsLegacy(const std::function<void(const SystemAddress&)>& legacySend, const GameMessages::NetGameMsg& msg, SendMode mode = SendMode::Send) {
for (const auto& address : g_Addresses) {
SCOPED_TRACE(address == UNASSIGNED_SYSTEM_ADDRESS ? "broadcast" : "single client");
SCOPED_TRACE(address == UNASSIGNED_SYSTEM_ADDRESS ? "unassigned address" : "single client");
const auto legacyPackets = Capture([&] { legacySend(address); });
const auto newPackets = Capture([&] { msg.Send(address); });
const auto newPackets = Capture([&] { mode == SendMode::Send ? msg.Send(address) : msg.SendToClient(address); });
ASSERT_FALSE(legacyPackets.empty());
ASSERT_EQ(newPackets.size(), 1);

View File

@@ -0,0 +1,185 @@
#ifndef MISSIONMESSAGESLEGACY_H
#define MISSIONMESSAGESLEGACY_H
// FROZEN ORACLE - DO NOT EDIT.
// Verbatim copies of the hand written GameMessages functions that MissionMessages.h replaced
// (dGame/dGameMessages/GameMessages.cpp, branched from origin/main 129199e4). Only the namespace changed.
// The Read* functions are the read sequences of the replaced GameMessages::Handle* functions.
#include "BitStreamUtils.h"
#include "dCommonVars.h"
#include "dServer.h"
#include "Entity.h"
#include "eMissionState.h"
#include "Game.h"
#include "MessageType/Client.h"
#include "MessageType/Game.h"
#include "ServiceType.h"
#include <vector>
namespace LegacyGameMessages {
inline void SendResetMissions(Entity* entity, const SystemAddress& sysAddr, const int32_t missionid) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(entity->GetObjectID());
bitStream.Write(MessageType::Game::RESET_MISSIONS);
bitStream.Write(missionid != -1);
if (missionid != -1) bitStream.Write(missionid);
SEND_PACKET;
}
inline void SendNotifyClientFlagChange(const LWOOBJID& objectID, uint32_t iFlagID, bool bFlag, const SystemAddress& sysAddr) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectID);
bitStream.Write(MessageType::Game::NOTIFY_CLIENT_FLAG_CHANGE);
bitStream.Write(bFlag);
bitStream.Write(iFlagID);
SEND_PACKET;
}
inline void SendOfferMission(const LWOOBJID& entity, const SystemAddress& sysAddr, int32_t missionID, const LWOOBJID& offererID) {
//You might be wondering.
//"Why are we sending it twice, once to a non-player object?
//Well, the first one (sent to the offerer) makes the client zoom into the object.
//The second, actually makes the UI pop up so you can be offered the mission.
//Why is it like this? Because LU isn't just a clown, it's the entire circus.
CBITSTREAM;
CMSGHEADER;
bitStream.Write(offererID);
bitStream.Write(MessageType::Game::OFFER_MISSION);
bitStream.Write(missionID);
bitStream.Write(offererID);
SEND_PACKET;
{
CBITSTREAM;
CMSGHEADER;
bitStream.Write(entity);
bitStream.Write(MessageType::Game::OFFER_MISSION);
bitStream.Write(missionID);
bitStream.Write(offererID);
SEND_PACKET;
}
}
inline void SendNotifyMission(Entity* entity, const SystemAddress& sysAddr, int missionID, int missionState, bool sendingRewards) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(entity->GetObjectID());
bitStream.Write(MessageType::Game::NOTIFY_MISSION);
bitStream.Write(missionID);
bitStream.Write(missionState);
bitStream.Write(sendingRewards);
SEND_PACKET;
}
inline void SendNotifyMissionTask(Entity* entity, const SystemAddress& sysAddr, int missionID, int taskMask, std::vector<float> updates) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(entity->GetObjectID());
bitStream.Write(MessageType::Game::NOTIFY_MISSION_TASK);
bitStream.Write(missionID);
bitStream.Write(taskMask);
bitStream.Write<unsigned char>(updates.size());
for (uint32_t i = 0; i < updates.size(); ++i) {
bitStream.Write(updates[i]);
}
SEND_PACKET;
}
inline void NotifyLevelRewards(LWOOBJID objectID, const SystemAddress& sysAddr, int level, bool sending_rewards) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectID);
bitStream.Write(MessageType::Game::NOTIFY_LEVEL_REWARDS);
bitStream.Write(level);
bitStream.Write(sending_rewards);
SEND_PACKET;
}
// GameMessages::HandleSetFlag
struct LegacySetFlag { bool bFlag{}; int32_t iFlagID{}; };
inline LegacySetFlag ReadSetFlag(RakNet::BitStream& inStream) {
bool bFlag{};
int32_t iFlagID{};
inStream.Read(bFlag);
inStream.Read(iFlagID);
return { bFlag, iFlagID };
}
// GameMessages::HandleRespondToMission
struct LegacyRespondToMission { int missionID{}; LWOOBJID playerID{}; LWOOBJID receiverID{}; LOT reward = LOT_NULL; };
inline LegacyRespondToMission ReadRespondToMission(RakNet::BitStream& inStream) {
int missionID{};
LWOOBJID playerID{};
LWOOBJID receiverID{};
bool isDefaultReward{};
LOT reward = LOT_NULL;
inStream.Read(missionID);
inStream.Read(playerID);
inStream.Read(receiverID);
inStream.Read(isDefaultReward);
if (isDefaultReward) inStream.Read(reward);
return { missionID, playerID, receiverID, reward };
}
// GameMessages::HandleMissionDialogOK
struct LegacyMissionDialogOK { bool bIsComplete{}; eMissionState iMissionState{}; int missionID{}; LWOOBJID responder{}; };
inline LegacyMissionDialogOK ReadMissionDialogOK(RakNet::BitStream& inStream) {
bool bIsComplete{};
eMissionState iMissionState{};
int missionID{};
LWOOBJID responder{};
inStream.Read(bIsComplete);
inStream.Read(iMissionState);
inStream.Read(missionID);
inStream.Read(responder);
return { bIsComplete, iMissionState, missionID, responder };
}
// GameMessages::HandleRequestLinkedMission
struct LegacyRequestLinkedMission { LWOOBJID playerId{}; int missionId{}; bool bMissionOffered{}; };
inline LegacyRequestLinkedMission ReadRequestLinkedMission(RakNet::BitStream& inStream) {
LWOOBJID playerId{};
int missionId{};
bool bMissionOffered{};
inStream.Read(playerId);
inStream.Read(missionId);
inStream.Read(bMissionOffered);
return { playerId, missionId, bMissionOffered };
}
// GameMessages::HandleHasBeenCollected
inline LWOOBJID ReadHasBeenCollected(RakNet::BitStream& inStream) {
LWOOBJID playerID;
inStream.Read(playerID);
return playerID;
}
}
#endif // MISSIONMESSAGESLEGACY_H

View File

@@ -0,0 +1,268 @@
#include "MissionMessages.h"
#include "GameDependencies.h"
#include "GameMessageTestUtils.h"
#include "Legacy/MissionMessagesLegacy.h"
#include "eMissionState.h"
#include <functional>
#include <limits>
#include <vector>
#include <gtest/gtest.h>
using namespace GameMessageTestUtils;
namespace {
const std::vector<int32_t> g_Ints = { 0, 1, -1, 1727, std::numeric_limits<int32_t>::max(), std::numeric_limits<int32_t>::min() };
PacketBytes Payload(const GameMessages::NetGameMsg& msg) {
RakNet::BitStream bitStream;
msg.Serialize(bitStream);
return FromBitStream(bitStream);
}
// Serializes msg, then reads it with the legacy read sequence; both must consume exactly the same bits.
template<typename Result>
Result ReadWithLegacy(const GameMessages::NetGameMsg& msg, const std::function<Result(RakNet::BitStream&)>& read) {
RakNet::BitStream wire;
msg.Serialize(wire);
RakNet::BitStream legacyStream(wire.GetData(), wire.GetNumberOfBytesUsed(), false);
auto result = read(legacyStream);
EXPECT_EQ(legacyStream.GetReadOffset(), wire.GetNumberOfBitsUsed());
return result;
}
}
class MissionMessagesTests : public GameDependenciesTest {
protected:
void SetUp() override { SetUpDependencies(); }
void TearDown() override { TearDownDependencies(); }
};
TEST_F(MissionMessagesTests, OfferMissionMatchesLegacy) {
// The legacy function sent the message twice, first targeting the offerer and then the player.
for (const auto player : g_Targets) {
for (const auto offerer : g_Targets) {
for (const auto missionID : g_Ints) {
for (const auto& address : g_Addresses) {
const auto legacy = Capture([&] { LegacyGameMessages::SendOfferMission(player, address, missionID, offerer); });
const auto ours = Capture([&] {
GameMessages::OfferMission msg;
msg.missionID = missionID;
msg.offerer = offerer;
msg.target = offerer;
msg.SendToClient(address);
msg.target = player;
msg.SendToClient(address);
});
ASSERT_EQ(legacy.size(), 2);
ASSERT_EQ(ours.size(), 2);
for (size_t i = 0; i < 2; i++) {
EXPECT_PACKET_EQ(FromCapture(legacy[i]), FromCapture(ours[i]));
EXPECT_EQ(legacy[i].broadcast, ours[i].broadcast);
EXPECT_EQ(legacy[i].sysAddr, ours[i].sysAddr);
}
}
GameMessages::OfferMission msg;
msg.missionID = missionID;
msg.offerer = offerer;
const auto copy = RoundTrip(msg);
EXPECT_EQ(copy.missionID, missionID);
EXPECT_EQ(copy.offerer, offerer);
}
}
}
}
TEST_F(MissionMessagesTests, NotifyMissionMatchesLegacy) {
for (const auto target : g_Targets) {
Entity entity(target, info);
for (const auto missionID : g_Ints) {
for (const int missionState : { 0, 1, 2, 4, 8, 12, -1 }) {
for (const bool sendingRewards : { false, true }) {
GameMessages::NotifyMission msg;
msg.target = target;
msg.missionID = missionID;
msg.missionState = missionState;
msg.sendingRewards = sendingRewards;
ExpectSameAsLegacy([&](const SystemAddress& a) { LegacyGameMessages::SendNotifyMission(&entity, a, missionID, missionState, sendingRewards); }, msg, SendMode::SendToClient);
const auto copy = RoundTrip(msg);
EXPECT_EQ(copy.missionID, missionID);
EXPECT_EQ(copy.missionState, missionState);
EXPECT_EQ(copy.sendingRewards, sendingRewards);
}
}
}
}
}
TEST_F(MissionMessagesTests, NotifyMissionTaskMatchesLegacy) {
std::vector<float> many(255);
for (size_t i = 0; i < many.size(); i++) many[i] = static_cast<float>(i) * 0.5f;
const std::vector<std::vector<float>> updateSets = { {}, { 1.0f }, { 0.0f, -1.0f, 3.5f }, many };
for (const auto target : g_Targets) {
Entity entity(target, info);
for (const auto missionID : g_Ints) {
for (const int taskMask : { 0, 2, 4, 1 << 30 }) {
for (const auto& updates : updateSets) {
GameMessages::NotifyMissionTask msg;
msg.target = target;
msg.missionID = missionID;
msg.taskMask = taskMask;
msg.updates = updates;
ExpectSameAsLegacy([&](const SystemAddress& a) { LegacyGameMessages::SendNotifyMissionTask(&entity, a, missionID, taskMask, updates); }, msg, SendMode::SendToClient);
const auto copy = RoundTrip(msg);
EXPECT_EQ(copy.missionID, missionID);
EXPECT_EQ(copy.taskMask, taskMask);
EXPECT_EQ(copy.updates, updates);
}
}
}
}
}
TEST_F(MissionMessagesTests, ResetMissionsMatchesLegacy) {
for (const auto target : g_Targets) {
Entity entity(target, info);
for (const auto missionID : g_Ints) {
GameMessages::ResetMissions msg;
msg.target = target;
msg.missionID = missionID;
ExpectSameAsLegacy([&](const SystemAddress& a) { LegacyGameMessages::SendResetMissions(&entity, a, missionID); }, msg, SendMode::SendToClient);
EXPECT_EQ(RoundTrip(msg).missionID, missionID);
}
}
}
TEST_F(MissionMessagesTests, NotifyClientFlagChangeMatchesLegacy) {
for (const auto target : g_Targets) {
for (const uint32_t flagID : { 0u, 42u, 1110u, std::numeric_limits<uint32_t>::max() }) {
for (const bool bFlag : { false, true }) {
GameMessages::NotifyClientFlagChange msg;
msg.target = target;
msg.iFlagID = flagID;
msg.bFlag = bFlag;
ExpectSameAsLegacy([&](const SystemAddress& a) { LegacyGameMessages::SendNotifyClientFlagChange(target, flagID, bFlag, a); }, msg, SendMode::SendToClient);
const auto copy = RoundTrip(msg);
EXPECT_EQ(copy.iFlagID, flagID);
EXPECT_EQ(copy.bFlag, bFlag);
}
}
}
}
TEST_F(MissionMessagesTests, NotifyLevelRewardsMatchesLegacy) {
for (const auto target : g_Targets) {
for (const auto level : g_Ints) {
for (const bool sendingRewards : { false, true }) {
GameMessages::NotifyLevelRewards msg;
msg.target = target;
msg.level = level;
msg.sendingRewards = sendingRewards;
ExpectSameAsLegacy([&](const SystemAddress& a) { LegacyGameMessages::NotifyLevelRewards(target, a, level, sendingRewards); }, msg, SendMode::SendToClient);
const auto copy = RoundTrip(msg);
EXPECT_EQ(copy.level, level);
EXPECT_EQ(copy.sendingRewards, sendingRewards);
}
}
}
}
TEST_F(MissionMessagesTests, InboundReadsLikeLegacy) {
for (const auto id : g_Targets) {
for (const auto value : g_Ints) {
for (const bool flag : { false, true }) {
GameMessages::SetFlag setFlag;
setFlag.bFlag = flag;
setFlag.iFlagID = value;
const auto legacySetFlag = ReadWithLegacy<LegacyGameMessages::LegacySetFlag>(setFlag, LegacyGameMessages::ReadSetFlag);
const auto setFlagCopy = RoundTrip(setFlag);
EXPECT_EQ(setFlagCopy.bFlag, legacySetFlag.bFlag);
EXPECT_EQ(setFlagCopy.iFlagID, legacySetFlag.iFlagID);
ExpectTruncatedFails(setFlag);
for (const LOT reward : { LOT_NULL, LOT{ 1727 }, LOT{ 0 } }) {
GameMessages::RespondToMission respond;
respond.missionID = value;
respond.playerID = id;
respond.receiver = 0x0102030405060708LL;
respond.rewardItem = reward;
const auto legacyRespond = ReadWithLegacy<LegacyGameMessages::LegacyRespondToMission>(respond, LegacyGameMessages::ReadRespondToMission);
const auto respondCopy = RoundTrip(respond);
EXPECT_EQ(respondCopy.missionID, legacyRespond.missionID);
EXPECT_EQ(respondCopy.playerID, legacyRespond.playerID);
EXPECT_EQ(respondCopy.receiver, legacyRespond.receiverID);
EXPECT_EQ(respondCopy.rewardItem, legacyRespond.reward);
ExpectTruncatedFails(respond);
}
for (const auto state : { eMissionState::AVAILABLE, eMissionState::READY_TO_COMPLETE, eMissionState::COMPLETE_AVAILABLE, static_cast<eMissionState>(-1) }) {
GameMessages::MissionDialogueOK ok;
ok.bIsComplete = flag;
ok.iMissionState = state;
ok.missionID = value;
ok.responder = id;
const auto legacyOk = ReadWithLegacy<LegacyGameMessages::LegacyMissionDialogOK>(ok, LegacyGameMessages::ReadMissionDialogOK);
const auto okCopy = RoundTrip(ok);
EXPECT_EQ(okCopy.bIsComplete, legacyOk.bIsComplete);
EXPECT_EQ(okCopy.iMissionState, legacyOk.iMissionState);
EXPECT_EQ(okCopy.missionID, legacyOk.missionID);
EXPECT_EQ(okCopy.responder, legacyOk.responder);
ExpectTruncatedFails(ok);
}
GameMessages::RequestLinkedMission linked;
linked.playerID = id;
linked.missionID = value;
linked.bMissionOffered = flag;
const auto legacyLinked = ReadWithLegacy<LegacyGameMessages::LegacyRequestLinkedMission>(linked, LegacyGameMessages::ReadRequestLinkedMission);
const auto linkedCopy = RoundTrip(linked);
EXPECT_EQ(linkedCopy.playerID, legacyLinked.playerId);
EXPECT_EQ(linkedCopy.missionID, legacyLinked.missionId);
EXPECT_EQ(linkedCopy.bMissionOffered, legacyLinked.bMissionOffered);
ExpectTruncatedFails(linked);
}
}
GameMessages::HasBeenCollected collected;
collected.playerID = id;
EXPECT_EQ(ReadWithLegacy<LWOOBJID>(collected, LegacyGameMessages::ReadHasBeenCollected), id);
EXPECT_EQ(RoundTrip(collected).playerID, id);
ExpectTruncatedFails(collected);
}
}
// Independent of the legacy code: hand computed payloads (RakNet writes MSB first).
TEST_F(MissionMessagesTests, GoldenBytes) {
GameMessages::ResetMissions reset;
EXPECT_PACKET_EQ(FromHex("00", 1), Payload(reset));
reset.missionID = 5;
EXPECT_PACKET_EQ(FromHex("82 80 00 00 00", 33), Payload(reset));
GameMessages::RespondToMission respond;
respond.missionID = 7;
respond.playerID = 0x11;
respond.receiver = 0x22;
EXPECT_PACKET_EQ(FromHex("07 00 00 00 11 00 00 00 00 00 00 00 22 00 00 00 00 00 00 00 00", 161), Payload(respond));
respond.rewardItem = 1727;
EXPECT_PACKET_EQ(FromHex("07 00 00 00 11 00 00 00 00 00 00 00 22 00 00 00 00 00 00 00 df 83 00 00 00", 193), Payload(respond));
GameMessages::MissionDialogueOK ok;
ok.bIsComplete = true;
ok.iMissionState = static_cast<eMissionState>(2);
ok.missionID = 7;
ok.responder = 0x33;
EXPECT_PACKET_EQ(FromHex("81 00 00 00 03 80 00 00 19 80 00 00 00 00 00 00 00", 129), Payload(ok));
GameMessages::NotifyClientFlagChange flag;
flag.bFlag = true;
flag.iFlagID = 42;
EXPECT_PACKET_EQ(FromHex("95 00 00 00 00", 33), Payload(flag));
GameMessages::NotifyMissionTask task;
task.missionID = 1;
task.taskMask = 4;
task.updates = { 1.5f, 2.0f };
EXPECT_PACKET_EQ(FromHex("01 00 00 00 04 00 00 00 02 00 00 c0 3f 00 00 00 40"), Payload(task));
}