// 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); } }