Merge exp/script-precedence: custom server script wins as the client's

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-27 18:35:29 -06:00
6 changed files with 137 additions and 5 deletions

View File

@@ -9,8 +9,10 @@
#include "CDScriptComponentTable.h"
#include "GameMessages.h"
#include "Amf3.h"
#include "Logger.h"
#include <ranges>
#include <set>
#include "ObjectLoadState.h"
#include "dZoneManager.h"
#include "CDZoneTableTable.h"
@@ -40,8 +42,17 @@ void ScriptComponent::LoadDataFromTemplate() {
m_ClientScriptName = row.client_script_name;
}
// custom_script_server replaces a missing (or empty) server script; a client script (the row's or custom_script_client, or
// any object's without a server script) makes it a client script component
const std::string& ScriptComponent::ChooseServerScript(const std::string& rowScript, const std::string& customScript) {
return customScript.empty() ? rowScript : customScript;
}
bool ScriptComponent::FirstUnportedReplacement(const LOT lot) {
static std::set<LOT> loggedLots;
return loggedLots.insert(lot).second;
}
// A non-empty custom_script_server replaces the row's server script; a client script (the row's or
// custom_script_client, or any object's without a server script) makes it a client script component
void ScriptComponent::LoadConfigData() {
if (!m_FromObject) return;
const bool isCharacter = m_Parent->GetCharacter() != nullptr;
@@ -50,9 +61,14 @@ void ScriptComponent::LoadConfigData() {
const auto customScriptServer = m_Parent->GetVarAsString(u"custom_script_server");
if (!m_Parent->GetVarAsString(u"custom_script_client").empty()) client = true;
// The client's script component takes a custom script over its row's; here it replaces the row's only where that is
// the empty script (the GF shooting gallery targets: the cannon gives them SG_TARGET.lua)
if (!customScriptServer.empty() && (m_ScriptName.empty() || m_ScriptName == "scripts\\empty.lua")) m_ScriptName = customScriptServer;
const auto& serverScript = ChooseServerScript(m_ScriptName, customScriptServer);
if (serverScript != m_ScriptName) {
if (!m_ScriptName.empty() && !CppScripts::IsPorted(serverScript) && FirstUnportedReplacement(m_Parent->GetLOT())) {
LOG_DEBUG("LOT %i: custom server script '%s' (not ported) replaces its row script '%s' (%s)", m_Parent->GetLOT(),
serverScript.c_str(), m_ScriptName.c_str(), CppScripts::IsPorted(m_ScriptName) ? "ported" : "not ported");
}
m_ScriptName = serverScript;
}
m_Client = client && m_ScriptName.empty();
SetScript(m_ScriptName);

View File

@@ -38,6 +38,13 @@ public:
void LoadDataFromTemplate() override;
void LoadConfigData() override;
// The server script an object runs, as the client picks its script: a non-empty custom script (the level config's
// custom_script_server) always wins over the ScriptComponent row's; an empty or missing one leaves the row's
static const std::string& ChooseServerScript(const std::string& rowScript, const std::string& customScript);
// True the first time a custom script with no C++ port replaces a row script on this LOT (logged once per LOT)
static bool FirstUnportedReplacement(const LOT lot);
void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override;
/**

View File

@@ -847,3 +847,7 @@ CppScripts::Script* const CppScripts::GetScript(Entity* parent, const std::strin
CppScripts::Script* const CppScripts::GetInvalidScript() {
return &InvalidToReturn;
}
bool CppScripts::IsPorted(const std::string& scriptName) {
return scriptLoader.contains(scriptName) || g_ExcludedScripts.contains(scriptName);
}

View File

@@ -415,4 +415,7 @@ namespace CppScripts {
// more cluttery to use. Also this allows us to control where this invalid script is defined and initialized
// since we dont want anyone externally modifying it.
Script* const GetInvalidScript();
// Whether a script name has a C++ port, or is one left out on purpose (the empty script and the like)
bool IsPorted(const std::string& scriptName);
};

View File

@@ -1,6 +1,7 @@
set(DCOMPONENTS_TESTS
"DeletionRestrictionTests.cpp"
"MovingPlatformComponentTests.cpp"
"ScriptComponentTests.cpp"
"InventorySaveTests.cpp"
"EquipMessagesTests.cpp"
"DestroyableLevelConfigTests.cpp"

View File

@@ -0,0 +1,101 @@
// Which server script an object runs: as the client picks its script, a non-empty custom script in the level config
// always wins over the ScriptComponent row's; an empty or missing one leaves the row's.
#include "GameDependencies.h"
#include "GameMessages.h"
#include "CDClientManager.h"
#include "CDScriptComponentTable.h"
#include "Entity.h"
#include "ScriptComponent.h"
#include <gtest/gtest.h>
namespace {
const std::string cannonScript = "scripts\\ai\\MINIGAME\\SG_GF\\SERVER\\SG_CANNON.lua";
const std::string targetScript = "scripts/ai/ACT/SG_TARGET.lua";
const std::string emptyScript = "scripts\\empty.lua";
}
class ScriptComponentTests : public GameDependenciesTest {
protected:
std::unique_ptr<Entity> entity;
void SetUp() override {
SetUpDependencies();
auto& scripts = CDClientManager::GetEntriesMutable<CDScriptComponentTable>();
scripts[90001] = { 90001, cannonScript, "" };
scripts[90002] = { 90002, emptyScript, "" };
scripts[90003] = { 90003, "", "" };
entity = std::make_unique<Entity>(1, info);
}
void TearDown() override {
entity.reset();
auto& scripts = CDClientManager::GetEntriesMutable<CDScriptComponentTable>();
scripts.erase(90001);
scripts.erase(90002);
scripts.erase(90003);
TearDownDependencies();
}
CppScripts::Script* Load(const int32_t componentID) {
ScriptComponent component(entity.get(), componentID);
component.LoadDataFromTemplate();
component.LoadConfigData();
return component.GetScript();
}
};
TEST(ScriptComponentChoice, CustomScriptWinsOverTheRowScript) {
EXPECT_EQ(ScriptComponent::ChooseServerScript(cannonScript, targetScript), targetScript);
EXPECT_EQ(ScriptComponent::ChooseServerScript(emptyScript, targetScript), targetScript);
EXPECT_EQ(ScriptComponent::ChooseServerScript("", targetScript), targetScript);
}
TEST(ScriptComponentChoice, EmptyCustomScriptKeepsTheRowScript) {
EXPECT_EQ(ScriptComponent::ChooseServerScript(cannonScript, ""), cannonScript);
EXPECT_EQ(ScriptComponent::ChooseServerScript("", ""), "");
}
TEST(ScriptComponentChoice, UnportedReplacementIsReportedOncePerLot) {
EXPECT_TRUE(ScriptComponent::FirstUnportedReplacement(999901));
EXPECT_FALSE(ScriptComponent::FirstUnportedReplacement(999901));
EXPECT_TRUE(ScriptComponent::FirstUnportedReplacement(999902));
}
TEST(ScriptComponentChoice, PortedScripts) {
EXPECT_TRUE(CppScripts::IsPorted(cannonScript));
EXPECT_TRUE(CppScripts::IsPorted(targetScript));
EXPECT_TRUE(CppScripts::IsPorted(emptyScript)); // left out on purpose
EXPECT_FALSE(CppScripts::IsPorted("scripts\\no\\such\\script.lua"));
}
TEST_F(ScriptComponentTests, RowScriptWithoutCustomScript) {
EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), cannonScript));
}
TEST_F(ScriptComponentTests, EmptyCustomScriptKeepsTheRowScript) {
entity->SetVar<std::string>(u"custom_script_server", "");
EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), cannonScript));
}
TEST_F(ScriptComponentTests, CustomScriptWinsOverAPortedRowScript) {
entity->SetVar<std::string>(u"custom_script_server", targetScript);
EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), targetScript));
}
TEST_F(ScriptComponentTests, CustomScriptWinsOverTheEmptyScript) {
entity->SetVar<std::string>(u"custom_script_server", targetScript);
EXPECT_EQ(Load(90002), CppScripts::GetScript(entity.get(), targetScript));
}
TEST_F(ScriptComponentTests, CustomScriptWithoutARowScript) {
entity->SetVar<std::string>(u"custom_script_server", targetScript);
EXPECT_EQ(Load(90003), CppScripts::GetScript(entity.get(), targetScript));
}
// Not ported: the object runs no script (the row's is lost until the custom one is ported)
TEST_F(ScriptComponentTests, UnportedCustomScriptStillWins) {
entity->SetVar<std::string>(u"custom_script_server", std::string("scripts\\no\\such\\script.lua"));
EXPECT_EQ(Load(90001), CppScripts::GetInvalidScript());
}