Index: binaries/data/mods/public/globalscripts/Templates.js =================================================================== --- binaries/data/mods/public/globalscripts/Templates.js +++ binaries/data/mods/public/globalscripts/Templates.js @@ -24,9 +24,12 @@ continue; const template = Engine.GetTemplate("special/players/" + data.Code); - data.Name = template.Identity.GenericName; - data.Emblem = "session/portraits/" + template.Identity.Icon; - data.History = template.Identity.History; + if (template.Identity) + { + data.Name = template.Identity.GenericName; + data.Emblem = "session/portraits/" + template.Identity.Icon; + data.History = template.Identity.History; + } civData[data.Code] = data; } Index: binaries/data/mods/public/maps/random/survivalofthefittest_triggers.js =================================================================== --- binaries/data/mods/public/maps/random/survivalofthefittest_triggers.js +++ binaries/data/mods/public/maps/random/survivalofthefittest_triggers.js @@ -139,7 +139,7 @@ for (let i = 1; i < TriggerHelper.GetNumberOfPlayers(); ++i) { let cmpPlayer = QueryPlayerIDInterface(i); - cmpPlayer.SetDisabledTemplates(disabledTemplates(cmpPlayer.GetCiv())); + cmpPlayer.SetDisabledTemplates(disabledTemplates(QueryPlayerIDInterface(i, IID_Identity).GetCiv())); } }; Index: binaries/data/mods/public/maps/random/tests/test_Constraint.js =================================================================== --- binaries/data/mods/public/maps/random/tests/test_Constraint.js +++ binaries/data/mods/public/maps/random/tests/test_Constraint.js @@ -1,13 +1,3 @@ -Engine.GetTemplate = (path) => { - return { - "Identity": { - "GenericName": null, - "Icon": null, - "History": null - } - }; -}; - Engine.LoadLibrary("rmgen"); var g_MapSettings = { "Size": 512 }; Index: binaries/data/mods/public/maps/random/tests/test_DiskPlacer.js =================================================================== --- binaries/data/mods/public/maps/random/tests/test_DiskPlacer.js +++ binaries/data/mods/public/maps/random/tests/test_DiskPlacer.js @@ -1,13 +1,3 @@ -Engine.GetTemplate = (path) => { - return { - "Identity": { - "GenericName": null, - "Icon": null, - "History": null - } - }; -}; - Engine.LoadLibrary("rmgen"); { Index: binaries/data/mods/public/maps/random/tests/test_LayeredPainter.js =================================================================== --- binaries/data/mods/public/maps/random/tests/test_LayeredPainter.js +++ binaries/data/mods/public/maps/random/tests/test_LayeredPainter.js @@ -1,13 +1,3 @@ -Engine.GetTemplate = (path) => { - return { - "Identity": { - "GenericName": null, - "Icon": null, - "History": null - } - }; -}; - Engine.LoadLibrary("rmgen"); var g_MapSettings = { "Size": 512 }; Index: binaries/data/mods/public/maps/random/tests/test_RectPlacer.js =================================================================== --- binaries/data/mods/public/maps/random/tests/test_RectPlacer.js +++ binaries/data/mods/public/maps/random/tests/test_RectPlacer.js @@ -1,13 +1,3 @@ -Engine.GetTemplate = (path) => { - return { - "Identity": { - "GenericName": null, - "Icon": null, - "History": null - } - }; -}; - Engine.LoadLibrary("rmgen"); var g_MapSettings = { "Size": 512 }; Index: binaries/data/mods/public/maps/random/tests/test_SmoothingPainter.js =================================================================== --- binaries/data/mods/public/maps/random/tests/test_SmoothingPainter.js +++ binaries/data/mods/public/maps/random/tests/test_SmoothingPainter.js @@ -1,13 +1,3 @@ -Engine.GetTemplate = (path) => { - return { - "Identity": { - "GenericName": null, - "Icon": null, - "History": null - } - }; -}; - Engine.LoadLibrary("rmgen"); var g_MapSettings = { "Size": 512 }; Index: binaries/data/mods/public/maps/random/tests/test_TileClass.js =================================================================== --- binaries/data/mods/public/maps/random/tests/test_TileClass.js +++ binaries/data/mods/public/maps/random/tests/test_TileClass.js @@ -1,13 +1,3 @@ -Engine.GetTemplate = (path) => { - return { - "Identity": { - "GenericName": null, - "Icon": null, - "History": null - } - }; -}; - Engine.LoadLibrary("rmgen"); var g_MapSettings = { "Size": 512 }; Index: binaries/data/mods/public/maps/scripts/Regicide.js =================================================================== --- binaries/data/mods/public/maps/scripts/Regicide.js +++ binaries/data/mods/public/maps/scripts/Regicide.js @@ -5,7 +5,7 @@ let playersCivs = []; for (let playerID = 1; playerID < TriggerHelper.GetNumberOfPlayers(); ++playerID) - playersCivs[playerID] = QueryPlayerIDInterface(playerID).GetCiv(); + playersCivs[playerID] = QueryPlayerIDInterface(playerID, IID_Identity).GetCiv(); // Get all hero templates of these civs let heroTemplates = {}; Index: source/graphics/MapGenerator.h =================================================================== --- source/graphics/MapGenerator.h +++ source/graphics/MapGenerator.h @@ -21,7 +21,6 @@ #include "ps/FileIo.h" #include "ps/Future.h" #include "scriptinterface/StructuredClone.h" -#include "simulation2/system/ParamNode.h" #include #include @@ -156,16 +155,6 @@ */ double GetMicroseconds(); - /** - * Return the template data of the given template name. - */ - CParamNode GetTemplate(const std::string& templateName); - - /** - * Check whether the given template exists. - */ - bool TemplateExists(const std::string& templateName); - /** * Returns all template names of simulation entity templates. */ Index: source/graphics/MapGenerator.cpp =================================================================== --- source/graphics/MapGenerator.cpp +++ source/graphics/MapGenerator.cpp @@ -32,6 +32,7 @@ #include "ps/Profile.h" #include "ps/TaskManager.h" #include "ps/TemplateLoader.h" +#include "ps/scripting/JSInterface_TemplateLoader.h" #include "ps/scripting/JSInterface_VFS.h" #include "scriptinterface/FunctionWrapper.h" #include "scriptinterface/ScriptContext.h" @@ -170,7 +171,7 @@ m_ScriptInterface->ReplaceNondeterministicRNG(m_MapGenRNG); m_MapGenRNG.seed(seed); - // VFS + JSI_TemplateLoader::RegisterScriptFunctions(*m_ScriptInterface); JSI_VFS::RegisterScriptFunctions_Maps(*m_ScriptInterface); // Globalscripts may use VFS script functions @@ -197,8 +198,6 @@ ScriptRequest rq(m_ScriptInterface); // Template functions - REGISTER_MAPGEN_FUNC(GetTemplate); - REGISTER_MAPGEN_FUNC(TemplateExists); REGISTER_MAPGEN_FUNC(FindTemplates); REGISTER_MAPGEN_FUNC(FindActorTemplates); @@ -247,20 +246,6 @@ LOGWARNING("The random map script tried to reduce the loading progress from %d to %d", m_Progress, progress); } -CParamNode CMapGeneratorWorker::GetTemplate(const std::string& templateName) -{ - const CParamNode& templateRoot = TemplateLoader::GetTemplateFileData(templateName).GetChild("Entity"); - if (!templateRoot.IsOk()) - LOGERROR("Invalid template found for '%s'", templateName.c_str()); - - return templateRoot; -} - -bool CMapGeneratorWorker::TemplateExists(const std::string& templateName) -{ - return TemplateLoader::TemplateExists(templateName); -} - std::vector CMapGeneratorWorker::FindTemplates(const std::string& path, bool includeSubdirectories) { return TemplateLoader::FindTemplates(path, includeSubdirectories, TemplateLoader::SIMULATION_TEMPLATES); Index: source/gui/GUIManager.h =================================================================== --- source/gui/GUIManager.h +++ source/gui/GUIManager.h @@ -29,7 +29,6 @@ class CCanvas2D; class CGUI; -class CParamNode; /** * External interface to the GUI system. @@ -114,16 +113,6 @@ */ void UpdateResolution(); - /** - * Check if a template with this name exists - */ - bool TemplateExists(const std::string& templateName) const; - - /** - * Retrieve the requested template, used for displaying faction specificities. - */ - const CParamNode& GetTemplate(const std::string& templateName); - /** * Display progress / description in loading screen. */ Index: source/gui/GUIManager.cpp =================================================================== --- source/gui/GUIManager.cpp +++ source/gui/GUIManager.cpp @@ -25,7 +25,6 @@ #include "ps/Filesystem.h" #include "ps/GameSetup/Config.h" #include "ps/Profile.h" -#include "ps/TemplateLoader.h" #include "ps/VideoMode.h" #include "ps/XML/Xeromyces.h" #include "scriptinterface/FunctionWrapper.h" @@ -409,20 +408,6 @@ } } -bool CGUIManager::TemplateExists(const std::string& templateName) const -{ - return TemplateLoader::TemplateExists(templateName); -} - -const CParamNode& CGUIManager::GetTemplate(const std::string& templateName) -{ - const CParamNode& templateRoot = TemplateLoader::GetTemplateFileData(templateName).GetChild("Entity"); - if (!templateRoot.IsOk()) - LOGERROR("Invalid template found for '%s'", templateName.c_str()); - - return templateRoot; -} - void CGUIManager::DisplayLoadProgress(int percent, const wchar_t* pending_task) { const ScriptInterface& scriptInterface = *(GetActiveGUI()->GetScriptInterface()); Index: source/gui/Scripting/JSInterface_GUIManager.cpp =================================================================== --- source/gui/Scripting/JSInterface_GUIManager.cpp +++ source/gui/Scripting/JSInterface_GUIManager.cpp @@ -64,16 +64,6 @@ g_VideoMode.ResetCursor(); } -bool TemplateExists(const std::string& templateName) -{ - return g_GUI->TemplateExists(templateName); -} - -CParamNode GetTemplate(const std::string& templateName) -{ - return g_GUI->GetTemplate(templateName); -} - void RegisterScriptFunctions(const ScriptRequest& rq) { @@ -82,8 +72,6 @@ ScriptFunction::Register<&PopGuiPage>(rq, "PopGuiPage"); ScriptFunction::Register<&SetCursor>(rq, "SetCursor"); ScriptFunction::Register<&ResetCursor>(rq, "ResetCursor"); - ScriptFunction::Register<&TemplateExists>(rq, "TemplateExists"); - ScriptFunction::Register<&GetTemplate>(rq, "GetTemplate"); ScriptFunction::Register<&CGUI::FindObjectByName, &ScriptInterface::ObjectFromCBData>(rq, "GetGUIObjectByName"); ScriptFunction::Register<&CGUI::SetGlobalHotkey, &ScriptInterface::ObjectFromCBData>(rq, "SetGlobalHotkey"); Index: source/gui/Scripting/ScriptFunctions.cpp =================================================================== --- source/gui/Scripting/ScriptFunctions.cpp +++ source/gui/Scripting/ScriptFunctions.cpp @@ -34,6 +34,7 @@ #include "ps/scripting/JSInterface_Mod.h" #include "ps/scripting/JSInterface_ModIo.h" #include "ps/scripting/JSInterface_SavedGame.h" +#include "ps/scripting/JSInterface_TemplateLoader.h" #include "ps/scripting/JSInterface_UserReport.h" #include "ps/scripting/JSInterface_VFS.h" #include "ps/scripting/JSInterface_VisualReplay.h" @@ -70,6 +71,7 @@ JSI_SavedGame::RegisterScriptFunctions(rq); JSI_Simulation::RegisterScriptFunctions(rq); JSI_Sound::RegisterScriptFunctions(rq); + JSI_TemplateLoader::RegisterScriptFunctions(rq); JSI_UserReport::RegisterScriptFunctions(rq); JSI_VFS::RegisterScriptFunctions_GUI(rq); JSI_VisualReplay::RegisterScriptFunctions(rq); Index: source/ps/scripting/JSInterface_TemplateLoader.h =================================================================== --- /dev/null +++ source/ps/scripting/JSInterface_TemplateLoader.h @@ -0,0 +1,28 @@ +/* Copyright (C) 2022 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#ifndef INCLUDED_JSI_Templates +#define INCLUDED_JSI_Templates + +class ScriptRequest; + +namespace JSI_TemplateLoader +{ + void RegisterScriptFunctions(const ScriptRequest& rq); +} + +#endif // INCLUDED_JSI_Templates Index: source/ps/scripting/JSInterface_TemplateLoader.cpp =================================================================== --- /dev/null +++ source/ps/scripting/JSInterface_TemplateLoader.cpp @@ -0,0 +1,65 @@ +/* Copyright (C) 2022 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#include "precompiled.h" + +#include "JSInterface_TemplateLoader.h" + +#include "ps/Game.h" +#include "ps/TemplateLoader.h" +#include "scriptinterface/FunctionWrapper.h" +#include "simulation2/components/ICmpTemplateManager.h" +#include "simulation2/Simulation2.h" +#include "simulation2/system/ParamNode.h" + +namespace JSI_TemplateLoader +{ + + bool TemplateExists(const std::string &templateName) + { + return TemplateLoader::TemplateExists(templateName); + } + + CParamNode GetTemplateFromFile(const std::string &templateName) + { + if (!TemplateLoader::TemplateExists(templateName)) + return CParamNode(false); + const CParamNode &templateRoot = TemplateLoader::GetTemplateFileData(templateName).GetChild("Entity"); + if (!templateRoot.IsOk()) + LOGERROR("Invalid template found for '%s'", templateName.c_str()); + return templateRoot; + } + + const CParamNode GetTemplateFromManager(const std::string &templateName) + { + if (!g_Game) + return CParamNode(false); + + CmpPtr cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY); + if (!cmpTemplateManager || !cmpTemplateManager->TemplateExists(templateName)) + return CParamNode(false); + + const CParamNode &entityTemplate = *cmpTemplateManager->GetTemplate(templateName); + return entityTemplate; + } + + void RegisterScriptFunctions(const ScriptRequest &rq) + { + ScriptFunction::Register<&TemplateExists>(rq, "TemplateExists"); + ScriptFunction::Register<&GetTemplateFromFile>(rq, "GetTemplate"); + } +} Index: source/simulation2/components/CCmpAIManager.cpp =================================================================== --- source/simulation2/components/CCmpAIManager.cpp +++ source/simulation2/components/CCmpAIManager.cpp @@ -29,6 +29,7 @@ #include "ps/CLogger.h" #include "ps/Filesystem.h" #include "ps/Profile.h" +#include "ps/scripting/JSInterface_TemplateLoader.h" #include "ps/scripting/JSInterface_VFS.h" #include "ps/TemplateLoader.h" #include "ps/Util.h" @@ -240,7 +241,6 @@ m_PassabilityMapVal.init(rq.cx); m_TerritoryMapVal.init(rq.cx); - m_ScriptInterface->ReplaceNondeterministicRNG(m_RNG); m_ScriptInterface->SetCallbackData(static_cast (this)); @@ -266,10 +266,10 @@ REGISTER_FUNC_NAME(ComputePathScript, "ComputePath"); REGISTER_FUNC_NAME(DumpImage, "DumpImage"); - REGISTER_FUNC_NAME(GetTemplate, "GetTemplate"); #undef REGISTER_FUNC_NAME + JSI_TemplateLoader::RegisterScriptFunctions(rq); JSI_VFS::RegisterScriptFunctions_Simulation(rq); // Globalscripts may use VFS script functions @@ -350,13 +350,6 @@ waypoints.emplace_back(wp.x, wp.z); } - CParamNode GetTemplate(const std::string& name) - { - if (!TemplateLoader::TemplateExists(name)) - return CParamNode(false); - return TemplateLoader::GetTemplateFileData(name).GetChild("Entity"); - } - /** * Debug function for AI scripts to dump 2D array data (e.g. terrain tile weights). */ Index: source/simulation2/system/ComponentManager.cpp =================================================================== --- source/simulation2/system/ComponentManager.cpp +++ source/simulation2/system/ComponentManager.cpp @@ -23,6 +23,7 @@ #include "ps/CLogger.h" #include "ps/Filesystem.h" #include "ps/Profile.h" +#include "ps/scripting/JSInterface_TemplateLoader.h" #include "ps/scripting/JSInterface_VFS.h" #include "scriptinterface/FunctionWrapper.h" #include "simulation2/components/ICmpTemplateManager.h" @@ -68,6 +69,7 @@ // these functions, so we skip registering them here in those cases if (!skipScriptFunctions) { + JSI_TemplateLoader::RegisterScriptFunctions(m_ScriptInterface); JSI_VFS::RegisterScriptFunctions_Simulation(m_ScriptInterface); ScriptRequest rq(m_ScriptInterface); constexpr ScriptFunction::ObjectGetter Getter = &ScriptInterface::ObjectFromCBData;