mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
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>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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: <ghst i>, 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 <ghst i="1"/> 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);
|
||||
|
||||
@@ -6,6 +6,7 @@ set(DCOMPONENTS_TESTS
|
||||
"DestroyableLevelConfigTests.cpp"
|
||||
"ProximityMonitorComponentTests.cpp"
|
||||
"RailActivatorComponentTests.cpp"
|
||||
"GhostComponentSaveTests.cpp"
|
||||
"MountControlComponentTests.cpp"
|
||||
"ConstructionOnlyComponentTests.cpp"
|
||||
"PropertyEntranceComponentTests.cpp"
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user