Files
DarkflameServer/tests/dGameTests/dComponentsTests/GhostComponentSaveTests.cpp
Aaron Kimbrell d08b35815e feat(gm): GM invisibility is kept in the character save
LWOGhostComponent::LoadFromSaveData reads <ghst i> under <obj> and
the client hides its own character again once the scene has loaded when it is
set. Nothing wrote or read the element, so a GM was visible again after every
world change.

GhostComponent now writes <ghst i="1"/> while invisible and reads it back. A
save without the element loads as visible and is not given one; an element
that is set goes back to 0 when the GM is visible again.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-10 21:21:33 -06:00

93 lines
3.5 KiB
C++

// GM invisibility in a character's save: LWOGhostComponent::LoadFromSaveData reads <ghst i> under <obj>,
// 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 <gtest/gtest.h>
#include <string>
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"(<obj v="1"><mf hc="1"/><char cc="5"/><dest hm="4"/></obj>)"), tinyxml2::XML_SUCCESS);
Entity player(80, info);
auto* const ghost = player.AddComponent<GhostComponent>(-1);
ghost->LoadFromSaveData(doc);
EXPECT_FALSE(ghost->IsGMInvisible());
ghost->WriteSaveData(doc);
EXPECT_EQ(Print(doc), R"(<obj v="1"><mf hc="1"/><char cc="5"/><dest hm="4"/></obj>)");
}
// 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"(<obj><ghst i="1"/></obj>)"), tinyxml2::XML_SUCCESS);
tinyxml2::XMLDocument doc;
ASSERT_EQ(doc.Parse(R"(<obj v="1"><char cc="5"/></obj>)"), tinyxml2::XML_SUCCESS);
Entity player(81, info);
auto* const ghost = player.AddComponent<GhostComponent>(-1);
SetInvisible(*ghost, invisible);
ASSERT_TRUE(ghost->IsGMInvisible());
ghost->WriteSaveData(doc);
EXPECT_EQ(Print(doc), R"(<obj v="1"><char cc="5"/><ghst i="1"/></obj>)");
// Saving again changes nothing
ghost->WriteSaveData(doc);
EXPECT_EQ(Print(doc), R"(<obj v="1"><char cc="5"/><ghst i="1"/></obj>)");
Entity nextWorld(82, info);
auto* const loaded = nextWorld.AddComponent<GhostComponent>(-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"(<obj v="1"><ghst i="1"/><dest hm="4"/></obj>)"), tinyxml2::XML_SUCCESS);
Entity player(83, info);
auto* const ghost = player.AddComponent<GhostComponent>(-1);
ASSERT_FALSE(ghost->IsGMInvisible());
ghost->WriteSaveData(doc);
EXPECT_EQ(Print(doc), R"(<obj v="1"><ghst i="0"/><dest hm="4"/></obj>)");
Entity nextWorld(84, info);
auto* const loaded = nextWorld.AddComponent<GhostComponent>(-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"(<obj v="1"><ghst/></obj>)", R"(<obj v="1"><ghst i="x"/></obj>)" }) {
tinyxml2::XMLDocument doc;
ASSERT_EQ(doc.Parse(xml), tinyxml2::XML_SUCCESS);
Entity player(85, info);
auto* const ghost = player.AddComponent<GhostComponent>(-1);
ghost->LoadFromSaveData(doc);
EXPECT_FALSE(ghost->IsGMInvisible());
ghost->WriteSaveData(doc);
EXPECT_EQ(Print(doc), xml);
}
}