diff --git a/dGame/dComponents/Character/GhostComponent.cpp b/dGame/dComponents/Character/GhostComponent.cpp index c202ff87f..0a2d93ec1 100644 --- a/dGame/dComponents/Character/GhostComponent.cpp +++ b/dGame/dComponents/Character/GhostComponent.cpp @@ -6,6 +6,7 @@ #include "User.h" #include "Amf3.h" +#include "tinyxml2.h" #include "GameMessages.h" #include "PlayerMessages.h" @@ -69,6 +70,33 @@ void GhostComponent::GhostEntity(LWOOBJID id) { m_ObservedEntities.erase(id); } +void GhostComponent::LoadFromSaveData(const tinyxml2::XMLDocument& doc) { + const auto* const obj = doc.FirstChildElement("obj"); + const auto* const ghost = obj ? obj->FirstChildElement("ghst") : nullptr; + // A save without the element, or with an i that is not a boolean, is a visible character + if (ghost) ghost->QueryBoolAttribute("i", &m_IsGMInvisible); +} + +void GhostComponent::WriteSaveData(tinyxml2::XMLDocument& doc) { + auto* const obj = doc.FirstChildElement("obj"); + if (!obj) return; + + auto* ghost = obj->FirstChildElement("ghst"); + if (!ghost) { + if (!m_IsGMInvisible) return; + ghost = obj->InsertNewChildElement("ghst"); + } + + // Only a value that says something else is rewritten + bool saved = false; + const bool readable = ghost->QueryBoolAttribute("i", &saved) == tinyxml2::XML_SUCCESS; + if (m_IsGMInvisible) { + if (!readable || !saved) ghost->SetAttribute("i", 1); + } else if (readable && saved) { + ghost->SetAttribute("i", 0); + } +} + bool GhostComponent::OnToggleGMInvis(GameMessages::ToggleGMInvisEvent& event) { auto& gmInvisMsg = event.msg; gmInvisMsg.target = event.target; // the wire message goes out with the target the event was delivered to diff --git a/dGame/dComponents/Character/GhostComponent.h b/dGame/dComponents/Character/GhostComponent.h index d61893e19..c90627416 100644 --- a/dGame/dComponents/Character/GhostComponent.h +++ b/dGame/dComponents/Character/GhostComponent.h @@ -47,6 +47,15 @@ public: // The client dropped every object it had (it reloaded the zone in place): nothing counts as sent to it any more void ForgetObservedEntities() { m_ObservedEntities.clear(); m_LimboConstructions.clear(); } + // LWOGhostComponent::LoadFromSaveData: , whether the GM was invisible when the save was + // written. The client reads the same element and hides its own character again once the scene has loaded. + void LoadFromSaveData(const tinyxml2::XMLDocument& doc) override; + + // Writes while invisible. A visible character only has an element it already had set back to 0. + void WriteSaveData(tinyxml2::XMLDocument& doc) override; + + bool IsGMInvisible() const { return m_IsGMInvisible; } + bool OnToggleGMInvis(GameMessages::ToggleGMInvisEvent& event); bool OnGetGMInvis(GameMessages::GetGMInvis& msg); diff --git a/tests/dGameTests/dComponentsTests/CMakeLists.txt b/tests/dGameTests/dComponentsTests/CMakeLists.txt index a4a15cadd..d5c8fe3fa 100644 --- a/tests/dGameTests/dComponentsTests/CMakeLists.txt +++ b/tests/dGameTests/dComponentsTests/CMakeLists.txt @@ -6,6 +6,7 @@ set(DCOMPONENTS_TESTS "DestroyableLevelConfigTests.cpp" "ProximityMonitorComponentTests.cpp" "RailActivatorComponentTests.cpp" + "GhostComponentSaveTests.cpp" "MountControlComponentTests.cpp" "ConstructionOnlyComponentTests.cpp" "PropertyEntranceComponentTests.cpp" diff --git a/tests/dGameTests/dComponentsTests/GhostComponentSaveTests.cpp b/tests/dGameTests/dComponentsTests/GhostComponentSaveTests.cpp new file mode 100644 index 000000000..d3cdc84e2 --- /dev/null +++ b/tests/dGameTests/dComponentsTests/GhostComponentSaveTests.cpp @@ -0,0 +1,92 @@ +// GM invisibility in a character's save: LWOGhostComponent::LoadFromSaveData reads under , +// and the client hides its own character again after loading when it is set. +#include "GameDependencies.h" + +#include "Entity.h" +#include "GhostComponent.h" + +#include "tinyxml2.h" + +#include +#include + +class GhostComponentSaveTests : public GameDependenciesTest { +protected: + void SetUp() override { SetUpDependencies(); } + void TearDown() override { TearDownDependencies(); } + + static std::string Print(const tinyxml2::XMLDocument& doc) { + tinyxml2::XMLPrinter printer(nullptr, true); + doc.Print(&printer); + return printer.CStr(); + } + + static void SetInvisible(GhostComponent& ghost, const tinyxml2::XMLDocument& from) { + ghost.LoadFromSaveData(from); + } +}; + +// A save from before the element was written loads as visible and stays byte for byte what it was +TEST_F(GhostComponentSaveTests, OldSaveLoadsVisibleAndIsNotChanged) { + tinyxml2::XMLDocument doc; + ASSERT_EQ(doc.Parse(R"()"), tinyxml2::XML_SUCCESS); + Entity player(80, info); + auto* const ghost = player.AddComponent(-1); + ghost->LoadFromSaveData(doc); + EXPECT_FALSE(ghost->IsGMInvisible()); + ghost->WriteSaveData(doc); + EXPECT_EQ(Print(doc), R"()"); +} + +// An invisible GM's save gains the element, and it reads back as invisible on the next world +TEST_F(GhostComponentSaveTests, InvisibilitySurvivesASaveAndALoad) { + tinyxml2::XMLDocument invisible; + ASSERT_EQ(invisible.Parse(R"()"), tinyxml2::XML_SUCCESS); + + tinyxml2::XMLDocument doc; + ASSERT_EQ(doc.Parse(R"()"), tinyxml2::XML_SUCCESS); + Entity player(81, info); + auto* const ghost = player.AddComponent(-1); + SetInvisible(*ghost, invisible); + ASSERT_TRUE(ghost->IsGMInvisible()); + ghost->WriteSaveData(doc); + EXPECT_EQ(Print(doc), R"()"); + // Saving again changes nothing + ghost->WriteSaveData(doc); + EXPECT_EQ(Print(doc), R"()"); + + Entity nextWorld(82, info); + auto* const loaded = nextWorld.AddComponent(-1); + loaded->LoadFromSaveData(doc); + EXPECT_TRUE(loaded->IsGMInvisible()); +} + +// Becoming visible again sets the element back to 0, in place +TEST_F(GhostComponentSaveTests, AVisibleCharacterWritesZeroOverASetElement) { + tinyxml2::XMLDocument doc; + ASSERT_EQ(doc.Parse(R"()"), tinyxml2::XML_SUCCESS); + Entity player(83, info); + auto* const ghost = player.AddComponent(-1); + ASSERT_FALSE(ghost->IsGMInvisible()); + ghost->WriteSaveData(doc); + EXPECT_EQ(Print(doc), R"()"); + + Entity nextWorld(84, info); + auto* const loaded = nextWorld.AddComponent(-1); + loaded->LoadFromSaveData(doc); + EXPECT_FALSE(loaded->IsGMInvisible()); +} + +// An element without i, or with an i that is not a boolean, is a visible character and is left as it is +TEST_F(GhostComponentSaveTests, AnUnreadableValueIsVisibleAndKept) { + for (const auto* const xml : { R"()", R"()" }) { + tinyxml2::XMLDocument doc; + ASSERT_EQ(doc.Parse(xml), tinyxml2::XML_SUCCESS); + Entity player(85, info); + auto* const ghost = player.AddComponent(-1); + ghost->LoadFromSaveData(doc); + EXPECT_FALSE(ghost->IsGMInvisible()); + ghost->WriteSaveData(doc); + EXPECT_EQ(Print(doc), xml); + } +}