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 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-27 18:24:09 -06:00
parent 1d2490a226
commit 6e02fafbc6
4 changed files with 35 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);
};