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); };