From 6e02fafbc6c11f62d9c213915640e945b2e6ba03 Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Fri, 27 Nov 2026 18:24:09 -0600 Subject: [PATCH 1/2] feat(scripts): custom server script always wins over the row's As the client picks its script component's script: a non-empty custom_script_server in the level config always replaces the ScriptComponent row's script_name, not only a missing one or scripts\empty.lua. An empty or missing custom script keeps the row's. An object whose custom script has no C++ port now runs no script even when its row script was ported; that is logged once per LOT at debug level ("custom server script ... (not ported) replaces its row script"). No level object or spawner in the shipped zones has both a row script and a different custom server script, so nothing there changes. CppScripts::IsPorted: a name in the loader map or the excluded list. Co-Authored-By: Claude Opus 5.5 --- dGame/dComponents/ScriptComponent.cpp | 26 +++++++++++++++++++++----- dGame/dComponents/ScriptComponent.h | 7 +++++++ dScripts/CppScripts.cpp | 4 ++++ dScripts/CppScripts.h | 3 +++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/dGame/dComponents/ScriptComponent.cpp b/dGame/dComponents/ScriptComponent.cpp index 8dd3ec123..b6d9c4a01 100644 --- a/dGame/dComponents/ScriptComponent.cpp +++ b/dGame/dComponents/ScriptComponent.cpp @@ -9,8 +9,10 @@ #include "CDScriptComponentTable.h" #include "GameMessages.h" #include "Amf3.h" +#include "Logger.h" #include +#include #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 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); diff --git a/dGame/dComponents/ScriptComponent.h b/dGame/dComponents/ScriptComponent.h index e65d51f35..e024d55e1 100644 --- a/dGame/dComponents/ScriptComponent.h +++ b/dGame/dComponents/ScriptComponent.h @@ -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; /** diff --git a/dScripts/CppScripts.cpp b/dScripts/CppScripts.cpp index 850ef204d..b4161b7a4 100644 --- a/dScripts/CppScripts.cpp +++ b/dScripts/CppScripts.cpp @@ -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); +} diff --git a/dScripts/CppScripts.h b/dScripts/CppScripts.h index 3ff6c603b..1421d2d0d 100644 --- a/dScripts/CppScripts.h +++ b/dScripts/CppScripts.h @@ -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); }; From 35fc5b16b2ad569ad86005087253ba90fda51d8e Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Fri, 27 Nov 2026 18:24:09 -0600 Subject: [PATCH 2/2] test(scripts): which server script an object runs Custom script over a ported row script, over the empty script and with no row script; empty custom script keeps the row's; an unported custom script still wins (no script); once-per-LOT reporting; IsPorted. Co-Authored-By: Claude Opus 5.5 --- .../dComponentsTests/CMakeLists.txt | 1 + .../dComponentsTests/ScriptComponentTests.cpp | 101 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tests/dGameTests/dComponentsTests/ScriptComponentTests.cpp diff --git a/tests/dGameTests/dComponentsTests/CMakeLists.txt b/tests/dGameTests/dComponentsTests/CMakeLists.txt index d5c8fe3fa..8a657b8ed 100644 --- a/tests/dGameTests/dComponentsTests/CMakeLists.txt +++ b/tests/dGameTests/dComponentsTests/CMakeLists.txt @@ -1,6 +1,7 @@ set(DCOMPONENTS_TESTS "DeletionRestrictionTests.cpp" "MovingPlatformComponentTests.cpp" + "ScriptComponentTests.cpp" "InventorySaveTests.cpp" "EquipMessagesTests.cpp" "DestroyableLevelConfigTests.cpp" diff --git a/tests/dGameTests/dComponentsTests/ScriptComponentTests.cpp b/tests/dGameTests/dComponentsTests/ScriptComponentTests.cpp new file mode 100644 index 000000000..aa268307f --- /dev/null +++ b/tests/dGameTests/dComponentsTests/ScriptComponentTests.cpp @@ -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 + +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; + + void SetUp() override { + SetUpDependencies(); + auto& scripts = CDClientManager::GetEntriesMutable(); + scripts[90001] = { 90001, cannonScript, "" }; + scripts[90002] = { 90002, emptyScript, "" }; + scripts[90003] = { 90003, "", "" }; + entity = std::make_unique(1, info); + } + + void TearDown() override { + entity.reset(); + auto& scripts = CDClientManager::GetEntriesMutable(); + 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(u"custom_script_server", ""); + EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), cannonScript)); +} + +TEST_F(ScriptComponentTests, CustomScriptWinsOverAPortedRowScript) { + entity->SetVar(u"custom_script_server", targetScript); + EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), targetScript)); +} + +TEST_F(ScriptComponentTests, CustomScriptWinsOverTheEmptyScript) { + entity->SetVar(u"custom_script_server", targetScript); + EXPECT_EQ(Load(90002), CppScripts::GetScript(entity.get(), targetScript)); +} + +TEST_F(ScriptComponentTests, CustomScriptWithoutARowScript) { + entity->SetVar(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(u"custom_script_server", std::string("scripts\\no\\such\\script.lua")); + EXPECT_EQ(Load(90001), CppScripts::GetInvalidScript()); +}