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 @@ -137,10 +137,7 @@ Trigger.prototype.SetDisableTemplates = function() { for (let i = 1; i < TriggerHelper.GetNumberOfPlayers(); ++i) - { - let cmpPlayer = QueryPlayerIDInterface(i); - cmpPlayer.SetDisabledTemplates(disabledTemplates(cmpPlayer.GetCiv())); - } + QueryPlayerIDInterface(i).SetDisabledTemplates(disabledTemplates(QueryPlayerIDInterface(i, IID_Identity).GetCiv())); }; /** 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: binaries/data/mods/public/simulation/ai/common-api/shared.js =================================================================== --- binaries/data/mods/public/simulation/ai/common-api/shared.js +++ binaries/data/mods/public/simulation/ai/common-api/shared.js @@ -52,7 +52,7 @@ m.SharedScript.prototype.GetTemplate = function(name) { if (this._templates[name] === undefined) - this._templates[name] = Engine.GetTemplate(name) || null; + this._templates[name] = SimEngine.GetTemplate(name) || null; return this._templates[name]; }; Index: source/graphics/MapGenerator.h =================================================================== --- source/graphics/MapGenerator.h +++ source/graphics/MapGenerator.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* 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 @@ -157,16 +157,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. */ std::vector FindTemplates(const std::string& path, bool includeSubdirectories); Index: source/graphics/MapGenerator.cpp =================================================================== --- source/graphics/MapGenerator.cpp +++ source/graphics/MapGenerator.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* 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 @@ -31,6 +31,7 @@ #include "ps/FileIo.h" #include "ps/Profile.h" #include "ps/TaskManager.h" +#include "ps/scripting/JSInterface_Templates.h" #include "ps/scripting/JSInterface_VFS.h" #include "scriptinterface/FunctionWrapper.h" #include "scriptinterface/ScriptContext.h" @@ -169,7 +170,7 @@ m_ScriptInterface->ReplaceNondeterministicRNG(m_MapGenRNG); m_MapGenRNG.seed(seed); - // VFS + JSI_Templates::RegisterScriptFunctions_Maps(*m_ScriptInterface); JSI_VFS::RegisterScriptFunctions_Maps(*m_ScriptInterface); // Globalscripts may use VFS script functions @@ -196,8 +197,6 @@ ScriptRequest rq(m_ScriptInterface); // Template functions - REGISTER_MAPGEN_FUNC(GetTemplate); - REGISTER_MAPGEN_FUNC(TemplateExists); REGISTER_MAPGEN_FUNC(FindTemplates); REGISTER_MAPGEN_FUNC(FindActorTemplates); @@ -246,20 +245,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 = m_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 m_TemplateLoader.TemplateExists(templateName); -} - std::vector CMapGeneratorWorker::FindTemplates(const std::string& path, bool includeSubdirectories) { return m_TemplateLoader.FindTemplates(path, includeSubdirectories, SIMULATION_TEMPLATES); Index: source/gui/GUIManager.h =================================================================== --- source/gui/GUIManager.h +++ source/gui/GUIManager.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* 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 @@ -114,16 +114,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. */ void DisplayLoadProgress(int percent, const wchar_t* pending_task); Index: source/gui/GUIManager.cpp =================================================================== --- source/gui/GUIManager.cpp +++ source/gui/GUIManager.cpp @@ -392,20 +392,6 @@ } } -bool CGUIManager::TemplateExists(const std::string& templateName) const -{ - return m_TemplateLoader.TemplateExists(templateName); -} - -const CParamNode& CGUIManager::GetTemplate(const std::string& templateName) -{ - const CParamNode& templateRoot = m_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 @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* 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 @@ -63,16 +63,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) { @@ -81,8 +71,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 @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* 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 @@ -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_Templates.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_Templates::RegisterScriptFunctions_GUI(rq); JSI_UserReport::RegisterScriptFunctions(rq); JSI_VFS::RegisterScriptFunctions_GUI(rq); JSI_VisualReplay::RegisterScriptFunctions(rq); Index: source/ps/scripting/JSInterface_Templates.h =================================================================== --- /dev/null +++ source/ps/scripting/JSInterface_Templates.h @@ -0,0 +1,30 @@ +/* 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_Templates +{ + void RegisterScriptFunctions_GUI(const ScriptRequest& rq); + void RegisterScriptFunctions_Simulation(const ScriptRequest& rq); + void RegisterScriptFunctions_Maps(const ScriptRequest& rq); +} + +#endif // INCLUDED_JSI_Templates Index: source/ps/scripting/JSInterface_Templates.cpp =================================================================== --- /dev/null +++ source/ps/scripting/JSInterface_Templates.cpp @@ -0,0 +1,76 @@ +/* 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_Templates.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_Templates +{ + +bool TemplateExists(const std::string& templateName) +{ + return TemplateLoader::TemplateExists(templateName); +} + +CParamNode GetTemplate(const std::string& templateName) +{ + if (!g_Game) + { + 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; + } + + CmpPtr cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY); + if (!cmpTemplateManager) + return CParamNode(false); + + if (!cmpTemplateManager->TemplateExists(templateName)) + return CParamNode(false); + + return *cmpTemplateManager->GetTemplate(templateName); +} + +void RegisterScriptFunctions_GUI(const ScriptRequest& rq) +{ + ScriptFunction::Register<&TemplateExists>(rq, "TemplateExists"); + ScriptFunction::Register<&GetTemplate>(rq, "GetTemplate"); +} + +void RegisterScriptFunctions_Simulation(const ScriptRequest& rq) +{ + ScriptFunction::Register<&TemplateExists>(rq, "TemplateExists"); + ScriptFunction::Register<&GetTemplate>(rq, "GetTemplate"); +} + +void RegisterScriptFunctions_Maps(const ScriptRequest& rq) +{ + ScriptFunction::Register<&TemplateExists>(rq, "TemplateExists"); + ScriptFunction::Register<&GetTemplate>(rq, "GetTemplate"); +} +} Index: source/simulation2/components/CCmpAIManager.cpp =================================================================== --- source/simulation2/components/CCmpAIManager.cpp +++ source/simulation2/components/CCmpAIManager.cpp @@ -30,7 +30,6 @@ #include "ps/Filesystem.h" #include "ps/Profile.h" #include "ps/scripting/JSInterface_VFS.h" -#include "ps/TemplateLoader.h" #include "ps/Util.h" #include "scriptinterface/FunctionWrapper.h" #include "scriptinterface/ScriptContext.h" @@ -266,7 +265,6 @@ REGISTER_FUNC_NAME(ComputePathScript, "ComputePath"); REGISTER_FUNC_NAME(DumpImage, "DumpImage"); - REGISTER_FUNC_NAME(GetTemplate, "GetTemplate"); #undef REGISTER_FUNC_NAME @@ -350,13 +348,6 @@ waypoints.emplace_back(wp.x, wp.z); } - CParamNode GetTemplate(const std::string& name) - { - if (!m_TemplateLoader.TemplateExists(name)) - return CParamNode(false); - return m_TemplateLoader.GetTemplateFileData(name).GetChild("Entity"); - } - /** * Debug function for AI scripts to dump 2D array data (e.g. terrain tile weights). */ @@ -859,8 +850,6 @@ LongPathfinder m_LongPathfinder; bool m_CommandsComputed; - - CTemplateLoader m_TemplateLoader; }; Index: source/simulation2/components/CCmpTemplateManager.cpp =================================================================== --- source/simulation2/components/CCmpTemplateManager.cpp +++ source/simulation2/components/CCmpTemplateManager.cpp @@ -152,7 +152,10 @@ { const CParamNode& fileData = m_templateLoader.GetTemplateFileData(templateName); if (!fileData.IsOk()) + { + LOGERROR("Invalid file data for template '%s'.", templateName.c_str()); return NULL; + } if (!m_DisableValidation) { 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_Templates.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_Templates::RegisterScriptFunctions_Simulation(m_ScriptInterface); JSI_VFS::RegisterScriptFunctions_Simulation(m_ScriptInterface); ScriptRequest rq(m_ScriptInterface); constexpr ScriptFunction::ObjectGetter Getter = &ScriptInterface::ObjectFromCBData;