diff --git a/source/graphics/MapGenerator.cpp b/source/graphics/MapGenerator.cpp index ffeec7e2ea..780ca790ec 100644 --- a/source/graphics/MapGenerator.cpp +++ b/source/graphics/MapGenerator.cpp @@ -231,9 +231,6 @@ std::vector CMapGeneratorWorker::GetCivData(ScriptInterface::CxPriv CParamNode CMapGeneratorWorker::GetTemplate(ScriptInterface::CxPrivate* pCxPrivate, std::string templateName) { - // TODO: Find a way to validate templates outside of the simulation. - // This should be implemented in TemplateLoader though. - CMapGeneratorWorker* self = static_cast(pCxPrivate->pCBData); const CParamNode& templateRoot = self->m_TemplateLoader.GetTemplateFileData(templateName).GetChild("Entity"); if (!templateRoot.IsOk()) diff --git a/source/gui/GUIManager.cpp b/source/gui/GUIManager.cpp index 6c171574a9..d0999c7798 100644 --- a/source/gui/GUIManager.cpp +++ b/source/gui/GUIManager.cpp @@ -403,6 +403,15 @@ void CGUIManager::UpdateResolution() it->gui->UpdateResolution(); } +const CParamNode& CGUIManager::GetTemplate(const std::string& templateName) +{ + const CParamNode& templateRoot = m_TemplateLoader.GetTemplateFileData(templateName).GetChild("Entity"); + if (!templateRoot.IsOk()) + LOGERROR(L"Invalid template found for '%hs'", templateName.c_str()); + + return templateRoot; +} + // This returns a shared_ptr to make sure the CGUI doesn't get deallocated // while we're in the middle of calling a function on it (e.g. if a GUI script // calls SwitchPage) diff --git a/source/gui/GUIManager.h b/source/gui/GUIManager.h index 75e9ac1760..fa0a2946e4 100644 --- a/source/gui/GUIManager.h +++ b/source/gui/GUIManager.h @@ -24,6 +24,7 @@ #include "lib/file/vfs/vfs_path.h" #include "ps/CLogger.h" #include "ps/CStr.h" +#include "ps/TemplateLoader.h" #include "scriptinterface/ScriptVal.h" #include "scriptinterface/ScriptInterface.h" @@ -139,6 +140,11 @@ public: void RestoreSavedGameData(std::string jsonData); + /** + * Retrieve the requested template, used for displaying faction specificities. + */ + const CParamNode& GetTemplate(const std::string& templateName); + private: struct SGUIPage { @@ -162,6 +168,8 @@ private: typedef std::vector PageStackType; PageStackType m_PageStack; + + CTemplateLoader m_TemplateLoader; }; extern CGUIManager* g_GUI; diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index ac934f52c4..77839888d2 100644 --- a/source/gui/scripting/ScriptFunctions.cpp +++ b/source/gui/scripting/ScriptFunctions.cpp @@ -842,6 +842,11 @@ CScriptVal ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring fil return out.get(); } +CParamNode GetTemplate(ScriptInterface::CxPrivate* pCxPrivate, std::string templateName) +{ + return g_GUI->GetTemplate(templateName); +} + //----------------------------------------------------------------------------- // Timer //----------------------------------------------------------------------------- @@ -1002,6 +1007,7 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction("GetFPS"); scriptInterface.RegisterFunction("GetBuildTimestamp"); scriptInterface.RegisterFunction("ReadJSONFile"); + scriptInterface.RegisterFunction("GetTemplate"); // User report functions scriptInterface.RegisterFunction("IsUserReportEnabled"); diff --git a/source/ps/TemplateLoader.h b/source/ps/TemplateLoader.h index 30888b60ba..7f471aaa6b 100644 --- a/source/ps/TemplateLoader.h +++ b/source/ps/TemplateLoader.h @@ -33,10 +33,14 @@ enum ETemplatesType * - the initialisation and deserialization of entity components in the * simulation (CmpTemplateManager). * - access to actor templates, obstruction data, etc. in RMS/RMGEN + * - access to various templates in the GUI, to display faction specificities * * Template names are intentionally restricted to ASCII strings for storage/serialization * efficiency (we have a lot of strings so this is significant); * they correspond to filenames so they shouldn't contain non-ASCII anyway. + * + * + * TODO: Find a way to validate templates outside of the simulation. */ class CTemplateLoader { @@ -48,7 +52,7 @@ public: /** * Provides the file data for requested template. */ - const CParamNode &GetTemplateFileData(const std::string& templateName); + const CParamNode& GetTemplateFileData(const std::string& templateName); /** * Returns a list of strings that could be validly passed as @c templateName to LoadTemplateFile.