Index: source/simulation2/components/CCmpTemplateManager.h =================================================================== --- /dev/null +++ source/simulation2/components/CCmpTemplateManager.h @@ -0,0 +1,93 @@ +/* Copyright (C) 2023 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_CCMPTEMPLATEMANAGER +#define INCLUDED_CCMPTEMPLATEMANAGER + +#include "simulation2/system/Component.h" +#include "ICmpTemplateManager.h" + +#include "ps/TemplateLoader.h" +#include "ps/XML/RelaxNG.h" +#include "simulation2/system/Entity.h" + +#include +#include +#include + +class CCmpTemplateManager final : public ICmpTemplateManager +{ +public: + static void ClassInit(CComponentManager& componentManager); + + DEFAULT_COMPONENT_ALLOCATOR(TemplateManager) + + static std::string GetSchema(); + + void Init(const CParamNode& UNUSED(paramNode)) override; + + void Deinit() override; + + void Serialize(ISerializer& serialize) override; + + void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) override; + + void HandleMessage(const CMessage& msg, bool UNUSED(global)) override; + + void DisableValidation() override; + + const CParamNode* LoadTemplate(entity_id_t ent, const std::string& templateName) override; + + const CParamNode* GetTemplate(const std::string& templateName) override; + + const CParamNode* GetTemplateWithoutValidation(const std::string& templateName) override; + + bool TemplateExists(const std::string& templateName) const override; + + const CParamNode* LoadLatestTemplate(entity_id_t ent) override; + + std::string GetCurrentTemplateName(entity_id_t ent) const override; + + std::vector FindAllTemplates(bool includeActors) const override; + + std::vector> GetCivData() override; + + std::vector FindUsedTemplates() const override; + + std::vector GetEntitiesUsingTemplate(const std::string& templateName) const override; + +private: + // Template loader + CTemplateLoader m_templateLoader; + + // Entity template XML validator + RelaxNGValidator m_Validator; + + // Disable validation, for test cases + bool m_DisableValidation; + + // Map from template name to schema validation status. + // (Some files, e.g. inherited parent templates, may not be valid themselves but we still need to load + // them and use them; we only reject invalid templates that were requested directly by GetTemplate/etc) + std::map m_TemplateSchemaValidity; + + // Remember the template used by each entity, so we can return them + // again for deserialization. + std::map m_LatestTemplates; +}; + +#endif // INCLUDED_CCMPTEMPLATEMANAGER Index: source/simulation2/components/CCmpTemplateManager.cpp =================================================================== --- source/simulation2/components/CCmpTemplateManager.cpp +++ source/simulation2/components/CCmpTemplateManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -17,129 +17,81 @@ #include "precompiled.h" -#include "simulation2/system/Component.h" -#include "ICmpTemplateManager.h" +#include "CCmpTemplateManager.h" #include "simulation2/MessageTypes.h" #include "simulation2/serialization/SerializedTypes.h" #include "lib/utf8.h" #include "ps/CLogger.h" -#include "ps/TemplateLoader.h" -#include "ps/XML/RelaxNG.h" -class CCmpTemplateManager final : public ICmpTemplateManager -{ -public: - static void ClassInit(CComponentManager& componentManager) - { - componentManager.SubscribeGloballyToMessageType(MT_Destroy); - } +REGISTER_COMPONENT_TYPE(TemplateManager) - DEFAULT_COMPONENT_ALLOCATOR(TemplateManager) +void CCmpTemplateManager::ClassInit(CComponentManager& componentManager) +{ + componentManager.SubscribeGloballyToMessageType(MT_Destroy); +} - static std::string GetSchema() - { - return ""; - } +std::string CCmpTemplateManager::GetSchema() +{ + return ""; +} - void Init(const CParamNode& UNUSED(paramNode)) override - { - m_DisableValidation = false; +void CCmpTemplateManager::Init(const CParamNode& UNUSED(paramNode)) +{ + m_DisableValidation = false; - m_Validator.LoadGrammar(GetSimContext().GetComponentManager().GenerateSchema()); - // TODO: handle errors loading the grammar here? - // TODO: support hotloading changes to the grammar - } + m_Validator.LoadGrammar(GetSimContext().GetComponentManager().GenerateSchema()); + // TODO: handle errors loading the grammar here? + // TODO: support hotloading changes to the grammar +} - void Deinit() override - { - } +void CCmpTemplateManager::Deinit() +{ +} - void Serialize(ISerializer& serialize) override - { - std::map> templateMap; +void CCmpTemplateManager::Serialize(ISerializer& serialize) +{ + std::map> templateMap; - for (const std::pair& templateEnt : m_LatestTemplates) - if (!ENTITY_IS_LOCAL(templateEnt.first)) - templateMap[templateEnt.second].push_back(templateEnt.first); + for (const std::pair& templateEnt : m_LatestTemplates) + if (!ENTITY_IS_LOCAL(templateEnt.first)) + templateMap[templateEnt.second].push_back(templateEnt.first); - Serializer(serialize, "templates", templateMap); - } + Serializer(serialize, "templates", templateMap); +} - void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) override - { - Init(paramNode); +void CCmpTemplateManager::Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) +{ + Init(paramNode); - std::map> templateMap; - Serializer(deserialize, "templates", templateMap); - for (const std::pair>& mapEl : templateMap) - for (entity_id_t id : mapEl.second) - m_LatestTemplates[id] = mapEl.first; - } + std::map> templateMap; + Serializer(deserialize, "templates", templateMap); + for (const std::pair>& mapEl : templateMap) + for (entity_id_t id : mapEl.second) + m_LatestTemplates[id] = mapEl.first; +} - void HandleMessage(const CMessage& msg, bool UNUSED(global)) override +void CCmpTemplateManager::HandleMessage(const CMessage& msg, bool UNUSED(global)) +{ + switch (msg.GetType()) { - switch (msg.GetType()) - { - case MT_Destroy: - { - const CMessageDestroy& msgData = static_cast (msg); + case MT_Destroy: + { + const CMessageDestroy& msgData = static_cast (msg); - // Clean up m_LatestTemplates so it doesn't record any data for destroyed entities - m_LatestTemplates.erase(msgData.entity); + // Clean up m_LatestTemplates so it doesn't record any data for destroyed entities + m_LatestTemplates.erase(msgData.entity); - break; - } - } + break; } - - void DisableValidation() override - { - m_DisableValidation = true; } +} - const CParamNode* LoadTemplate(entity_id_t ent, const std::string& templateName) override; - - const CParamNode* GetTemplate(const std::string& templateName) override; - - const CParamNode* GetTemplateWithoutValidation(const std::string& templateName) override; - - bool TemplateExists(const std::string& templateName) const override; - - const CParamNode* LoadLatestTemplate(entity_id_t ent) override; - - std::string GetCurrentTemplateName(entity_id_t ent) const override; - - std::vector FindAllTemplates(bool includeActors) const override; - - std::vector> GetCivData() override; - - std::vector FindUsedTemplates() const override; - - std::vector GetEntitiesUsingTemplate(const std::string& templateName) const override; - -private: - // Template loader - CTemplateLoader m_templateLoader; - - // Entity template XML validator - RelaxNGValidator m_Validator; - - // Disable validation, for test cases - bool m_DisableValidation; - - // Map from template name to schema validation status. - // (Some files, e.g. inherited parent templates, may not be valid themselves but we still need to load - // them and use them; we only reject invalid templates that were requested directly by GetTemplate/etc) - std::map m_TemplateSchemaValidity; - - // Remember the template used by each entity, so we can return them - // again for deserialization. - std::map m_LatestTemplates; -}; - -REGISTER_COMPONENT_TYPE(TemplateManager) +void CCmpTemplateManager::DisableValidation() +{ + m_DisableValidation = true; +} const CParamNode* CCmpTemplateManager::LoadTemplate(entity_id_t ent, const std::string& templateName) {