fix(flags): a client SetFlag for a flag only the server sets is ignored

PlayerFlags.OnlySetByServer marks 22 flags. The client's flag component refuses
to set them itself (LWOFlagComponent::SendMessage logs "Attempted to
set a flag from the client side that can only be set from the server"), and
none of the 205 client SetFlag messages in the live captures named one; the
server set them. SetFlag::Handle set whatever flag the message named.

The message is now dropped for those flags, with a debug log line. Flags
already saved on a character are not touched.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-10 20:43:54 -06:00
parent d08b35815e
commit a05cedbe01
3 changed files with 85 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
#include "MissionMessages.h"
#include "BitStreamUtils.h"
#include "CDPlayerFlagsTable.h"
#include "Character.h"
#include "CppScripts.h"
#include "dConfig.h"
@@ -248,6 +249,15 @@ namespace GameMessages {
}
void SetFlag::Handle(Entity& entity, const SystemAddress& sysAddr) {
// PlayerFlags.OnlySetByServer: the client's flag component refuses to set these itself and logs "Attempted to
// set a flag from the client side that can only be set from the server" (LWOFlagComponent::SendMessage
// ), and no live client SetFlag named one (205 looked at). The saved flags are left as they are.
const auto flagEntry = CDPlayerFlagsTable::GetEntry(iFlagID);
if (flagEntry && flagEntry->onlySetByServer) {
LOG_DEBUG("Ignored SetFlag %i = %d from %llu: the flag can only be set by the server", iFlagID, bFlag, entity.GetObjectID());
return;
}
auto character = entity.GetCharacter();
if (character) character->SetPlayerFlag(iFlagID, bFlag);

View File

@@ -7,6 +7,7 @@ SET(DGAMEMESSAGES_TESTS
"EffectsMessagesTests.cpp"
"InventoryMessagesTests.cpp"
"MissionMessagesTests.cpp"
"SetFlagServerOnlyTests.cpp"
"PetMessagesTests.cpp"
"PropertyMessagesTests.cpp"
"RacingMessagesTests.cpp"

View File

@@ -0,0 +1,74 @@
// A client SetFlag for a flag marked PlayerFlags.OnlySetByServer is ignored: the client's own flag component refuses
// to set these (LWOFlagComponent::SendMessage), and none of the 205 live client SetFlag messages named one.
#include "GameDependencies.h"
#include "CDClientDatabase.h"
#include "Character.h"
#include "Entity.h"
#include "MissionMessages.h"
#include "User.h"
#include <gtest/gtest.h>
#include <memory>
namespace {
constexpr int32_t SERVER_ONLY_FLAG = 1110;
constexpr int32_t CLIENT_FLAG = 1111;
constexpr int32_t UNLISTED_FLAG = 1112;
}
class SetFlagServerOnlyTests : public GameDependenciesTest {
protected:
void SetUp() override {
SetUpDependencies();
CDClientDatabase::Connect(":memory:");
CDClientDatabase::ExecuteDML("CREATE TABLE PlayerFlags (id INTEGER, SessionOnly INTEGER, OnlySetByServer INTEGER, SessionZoneOnly INTEGER);");
CDClientDatabase::ExecuteDML("INSERT INTO PlayerFlags VALUES (1110, 0, 1, 0), (1111, 0, 0, 0);");
user = std::make_unique<User>(UNASSIGNED_SYSTEM_ADDRESS, "tester", "key");
character = std::make_unique<Character>(1, user.get());
entity = std::make_unique<Entity>(1, info);
entity->SetCharacter(character.get());
character->SetEntity(entity.get());
}
void TearDown() override {
entity->SetCharacter(nullptr);
entity.reset();
character.reset();
user.reset();
TearDownDependencies();
}
void ClientSetsFlag(const int32_t flag, const bool value) {
GameMessages::SetFlag setFlag;
setFlag.iFlagID = flag;
setFlag.bFlag = value;
setFlag.Handle(*entity, UNASSIGNED_SYSTEM_ADDRESS);
}
std::unique_ptr<User> user;
std::unique_ptr<Character> character;
std::unique_ptr<Entity> entity;
};
TEST_F(SetFlagServerOnlyTests, ClientCannotSetAServerOnlyFlag) {
ClientSetsFlag(SERVER_ONLY_FLAG, true);
EXPECT_FALSE(character->GetPlayerFlag(SERVER_ONLY_FLAG));
}
// A flag the server set stays set: the client cannot clear it either, and nothing saved is dropped
TEST_F(SetFlagServerOnlyTests, ClientCannotClearAServerOnlyFlag) {
character->SetPlayerFlag(SERVER_ONLY_FLAG, true);
ClientSetsFlag(SERVER_ONLY_FLAG, false);
EXPECT_TRUE(character->GetPlayerFlag(SERVER_ONLY_FLAG));
}
// Every other flag is set and cleared as before, with or without a PlayerFlags row
TEST_F(SetFlagServerOnlyTests, OtherFlagsAreStillSetByTheClient) {
for (const auto flag : { CLIENT_FLAG, UNLISTED_FLAG }) {
ClientSetsFlag(flag, true);
EXPECT_TRUE(character->GetPlayerFlag(flag));
ClientSetsFlag(flag, false);
EXPECT_FALSE(character->GetPlayerFlag(flag));
}
}