Throw error when simulation script can't be loaded

When a script in "simulation/helpers/" contained an error. Files in
"simulation/components" aren't loaded. The return value of
`LoadDefaultScripts` indicated an error but was ignored. The simulation
still tried to start.

Now instead of returning a ignoreable error code the error is thrown. In
the common path the error is implicitly rethrown to the JS-function
which tried to start the game.

fixes: #8133
(cherry picked from commit 9a526bcae1)
Signed-off-by: phosit <phosit@autistici.org>
This commit is contained in:
phosit
2025-11-05 19:38:19 +01:00
parent 97eca26d58
commit 7ec2d3f0e0
5 changed files with 44 additions and 29 deletions
+14 -18
View File
@@ -114,8 +114,8 @@ public:
componentManager.AddSystemComponents(skipScriptedComponents, skipAI);
}
static bool LoadDefaultScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts);
static bool LoadScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts, const VfsPath& path);
static void LoadDefaultScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts);
static void LoadScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts, const VfsPath& path);
static bool LoadTriggerScripts(CComponentManager& componentManager, JS::HandleValue mapSettings, std::set<VfsPath>* loadedScripts);
Status ReloadChangedFile(const VfsPath& path);
@@ -193,31 +193,27 @@ public:
}
};
bool CSimulation2Impl::LoadDefaultScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts)
void CSimulation2Impl::LoadDefaultScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts)
{
return (
LoadScripts(componentManager, loadedScripts, L"simulation/components/interfaces/") &&
LoadScripts(componentManager, loadedScripts, L"simulation/helpers/") &&
LoadScripts(componentManager, loadedScripts, L"simulation/components/")
);
LoadScripts(componentManager, loadedScripts, L"simulation/components/interfaces/");
LoadScripts(componentManager, loadedScripts, L"simulation/helpers/");
LoadScripts(componentManager, loadedScripts, L"simulation/components/");
}
bool CSimulation2Impl::LoadScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts, const VfsPath& path)
void CSimulation2Impl::LoadScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts, const VfsPath& path)
{
VfsPaths pathnames;
if (vfs::GetPathnames(g_VFS, path, L"*.js", pathnames) < 0)
return false;
throw CSimulation2::LoadScriptError{"Error enumerating " + path.string8()};
bool ok = true;
for (const VfsPath& scriptPath : pathnames)
{
if (loadedScripts)
loadedScripts->insert(scriptPath);
LOGMESSAGE("Loading simulation script '%s'", scriptPath.string8());
if (!componentManager.LoadScript(scriptPath))
ok = false;
throw CSimulation2::LoadScriptError{"Error loading " + scriptPath.string8()};
}
return ok;
}
bool CSimulation2Impl::LoadTriggerScripts(CComponentManager& componentManager, JS::HandleValue mapSettings, std::set<VfsPath>* loadedScripts)
@@ -425,7 +421,7 @@ void CSimulation2Impl::Update(int turnLength, const std::vector<SimulationComman
m_SecondaryComponentManager->LoadComponentTypes();
m_SecondaryLoadedScripts = std::make_unique<std::set<VfsPath>>();
ENSURE(LoadDefaultScripts(*m_SecondaryComponentManager, m_SecondaryLoadedScripts.get()));
LoadDefaultScripts(*m_SecondaryComponentManager, m_SecondaryLoadedScripts.get());
ResetComponentState(*m_SecondaryComponentManager, false, false);
ScriptRequest rq(scriptInterface);
@@ -782,14 +778,14 @@ float CSimulation2::GetLastFrameOffset() const
return m->m_LastFrameOffset;
}
bool CSimulation2::LoadScripts(const VfsPath& path)
void CSimulation2::LoadScripts(const VfsPath& path)
{
return m->LoadScripts(m->m_ComponentManager, &m->m_LoadedScripts, path);
m->LoadScripts(m->m_ComponentManager, &m->m_LoadedScripts, path);
}
bool CSimulation2::LoadDefaultScripts()
void CSimulation2::LoadDefaultScripts()
{
return m->LoadDefaultScripts(m->m_ComponentManager, &m->m_LoadedScripts);
m->LoadDefaultScripts(m->m_ComponentManager, &m->m_LoadedScripts);
}
void CSimulation2::SetStartupScript(const std::string& code)