From ee0fcf2b95b9998c3d3c199bbc106bd73dbac4a4 Mon Sep 17 00:00:00 2001 From: wraitii Date: Mon, 9 May 2022 18:13:34 +0000 Subject: [PATCH] Fix map errors from GetTemplate not being available in the sim Follows ea72437739. Fixes it by providing a convenience function in the component Manager that matches the API in the GUI / AI Refs #6444 Differential Revision: https://code.wildfiregames.com/D4630 This was SVN commit r26867. --- .../simulation2/system/ComponentManager.cpp | 19 +++++++++++++++++++ source/simulation2/system/ComponentManager.h | 3 ++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/source/simulation2/system/ComponentManager.cpp b/source/simulation2/system/ComponentManager.cpp index 6037177994..3145da0c5e 100644 --- a/source/simulation2/system/ComponentManager.cpp +++ b/source/simulation2/system/ComponentManager.cpp @@ -86,6 +86,8 @@ CComponentManager::CComponentManager(CSimContext& context, std::shared_ptr(rq, "QueryInterface"); ScriptFunction::Register<&CComponentManager::DestroyComponentsSoon, Getter>(rq, "DestroyEntity"); ScriptFunction::Register<&CComponentManager::FlushDestroyedComponents, Getter>(rq, "FlushDestroyedEntities"); + ScriptFunction::Register<&CComponentManager::Script_GetTemplate, Getter>(rq, "GetTemplate"); + } // Globalscripts may use VFS script functions @@ -382,6 +384,23 @@ void CComponentManager::Script_RegisterGlobal(const std::string& name, JS::Handl m_ScriptInterface.SetGlobal(name.c_str(), value, m_CurrentlyHotloading); } +const CParamNode& CComponentManager::Script_GetTemplate(const std::string& templateName) +{ + static CParamNode nullNode(false); + + ICmpTemplateManager* cmpTemplateManager = static_cast (QueryInterface(SYSTEM_ENTITY, IID_TemplateManager)); + if (!cmpTemplateManager) + { + LOGERROR("Template manager is not loaded"); + return nullNode; + } + + const CParamNode* tmpl = cmpTemplateManager->GetTemplate(templateName); + if (!tmpl) + return nullNode; + return *tmpl; +} + std::vector CComponentManager::Script_GetEntitiesWithInterface(int iid) { std::vector ret; diff --git a/source/simulation2/system/ComponentManager.h b/source/simulation2/system/ComponentManager.h index d0bfac8bf7..e21ae25c92 100644 --- a/source/simulation2/system/ComponentManager.h +++ b/source/simulation2/system/ComponentManager.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 @@ -286,6 +286,7 @@ private: void Script_BroadcastMessage(int mtid, JS::HandleValue data); int Script_AddEntity(const std::wstring& templateName); int Script_AddLocalEntity(const std::wstring& templateName); + const CParamNode& Script_GetTemplate(const std::string& templateName); CMessage* ConstructMessage(int mtid, JS::HandleValue data); void SendGlobalMessage(entity_id_t ent, const CMessage& msg);