diff --git a/binaries/data/mods/public/gui/structree/load.js b/binaries/data/mods/public/gui/structree/load.js index 10400213e7..3c511c99dc 100644 --- a/binaries/data/mods/public/gui/structree/load.js +++ b/binaries/data/mods/public/gui/structree/load.js @@ -37,9 +37,9 @@ function getGatherRates(templateName) function loadUnit(templateName) { - var template = loadTemplate(templateName); - if (!template) + if (!Engine.TemplateExists(templateName)) return null; + var template = loadTemplate(templateName); var unit = GetTemplateDataHelper(template); unit.phase = false; diff --git a/source/gui/GUIManager.cpp b/source/gui/GUIManager.cpp index f4a7a31ca6..de3260f940 100644 --- a/source/gui/GUIManager.cpp +++ b/source/gui/GUIManager.cpp @@ -402,10 +402,13 @@ void CGUIManager::UpdateResolution() it->gui->UpdateResolution(); } +bool CGUIManager::TemplateExists(const std::string& templateName) +{ + return m_TemplateLoader.TemplateExists(templateName); +} + const CParamNode& CGUIManager::GetTemplate(const std::string& templateName) { - if (!m_TemplateLoader.TemplateExists(templateName)) - return NULL; const CParamNode& templateRoot = m_TemplateLoader.GetTemplateFileData(templateName).GetChild("Entity"); if (!templateRoot.IsOk()) LOGERROR("Invalid template found for '%s'", templateName.c_str()); diff --git a/source/gui/GUIManager.h b/source/gui/GUIManager.h index fa0a2946e4..5e2964b210 100644 --- a/source/gui/GUIManager.h +++ b/source/gui/GUIManager.h @@ -140,6 +140,11 @@ public: void RestoreSavedGameData(std::string jsonData); + /** + * Check if a template with this name exists + */ + bool TemplateExists(const std::string& templateName); + /** * Retrieve the requested template, used for displaying faction specificities. */ diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index e837bfa2c8..f2a6003008 100644 --- a/source/gui/scripting/ScriptFunctions.cpp +++ b/source/gui/scripting/ScriptFunctions.cpp @@ -825,6 +825,11 @@ void WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring filePath g_VFS->CreateFile(path, buf.Data(), buf.Size()); } +bool TemplateExists(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string templateName) +{ + return g_GUI->TemplateExists(templateName); +} + CParamNode GetTemplate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string templateName) { return g_GUI->GetTemplate(templateName); @@ -991,6 +996,7 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction("GetBuildTimestamp"); scriptInterface.RegisterFunction("ReadJSONFile"); scriptInterface.RegisterFunction("WriteJSONFile"); + scriptInterface.RegisterFunction("TemplateExists"); scriptInterface.RegisterFunction("GetTemplate"); // User report functions