diff --git a/dGame/dGameMessages/MissionMessages.cpp b/dGame/dGameMessages/MissionMessages.cpp index c3be670e2..be448f52d 100644 --- a/dGame/dGameMessages/MissionMessages.cpp +++ b/dGame/dGameMessages/MissionMessages.cpp @@ -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); diff --git a/tests/dGameTests/dGameMessagesTests/CMakeLists.txt b/tests/dGameTests/dGameMessagesTests/CMakeLists.txt index f71cdb16b..6dab3280a 100644 --- a/tests/dGameTests/dGameMessagesTests/CMakeLists.txt +++ b/tests/dGameTests/dGameMessagesTests/CMakeLists.txt @@ -7,6 +7,7 @@ SET(DGAMEMESSAGES_TESTS "EffectsMessagesTests.cpp" "InventoryMessagesTests.cpp" "MissionMessagesTests.cpp" + "SetFlagServerOnlyTests.cpp" "PetMessagesTests.cpp" "PropertyMessagesTests.cpp" "RacingMessagesTests.cpp" diff --git a/tests/dGameTests/dGameMessagesTests/SetFlagServerOnlyTests.cpp b/tests/dGameTests/dGameMessagesTests/SetFlagServerOnlyTests.cpp new file mode 100644 index 000000000..210ca5b96 --- /dev/null +++ b/tests/dGameTests/dGameMessagesTests/SetFlagServerOnlyTests.cpp @@ -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 +#include + +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(UNASSIGNED_SYSTEM_ADDRESS, "tester", "key"); + character = std::make_unique(1, user.get()); + entity = std::make_unique(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; + std::unique_ptr character; + std::unique_ptr 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)); + } +}