2017-01-15 19:41:53 +01:00
|
|
|
/* Copyright (C) 2017 Wildfire Games.
|
2010-01-09 20:20:14 +01:00
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "simulation2/system/Component.h"
|
|
|
|
#include "ICmpTemplateManager.h"
|
|
|
|
|
2010-03-07 21:14:30 +01:00
|
|
|
#include "simulation2/MessageTypes.h"
|
2016-05-02 11:26:07 +02:00
|
|
|
#include "simulation2/serialization/SerializeTemplates.h"
|
2010-03-07 21:14:30 +01:00
|
|
|
|
2014-06-07 14:12:53 +02:00
|
|
|
#include "ps/TemplateLoader.h"
|
|
|
|
|
2010-03-01 15:55:34 +01:00
|
|
|
#include "lib/utf8.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "ps/CLogger.h"
|
2010-04-14 19:22:32 +02:00
|
|
|
#include "ps/XML/RelaxNG.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
class CCmpTemplateManager : public ICmpTemplateManager
|
|
|
|
{
|
|
|
|
public:
|
2010-03-07 21:14:30 +01:00
|
|
|
static void ClassInit(CComponentManager& componentManager)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-03-07 21:14:30 +01:00
|
|
|
componentManager.SubscribeGloballyToMessageType(MT_Destroy);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(TemplateManager)
|
|
|
|
|
2010-04-23 18:09:03 +02:00
|
|
|
static std::string GetSchema()
|
|
|
|
{
|
|
|
|
return "<a:component type='system'/><empty/>";
|
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Init(const CParamNode& UNUSED(paramNode))
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-07-21 18:04:17 +02:00
|
|
|
m_DisableValidation = false;
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
m_Validator.LoadGrammar(GetSimContext().GetComponentManager().GenerateSchema());
|
2010-04-14 19:22:32 +02:00
|
|
|
// TODO: handle errors loading the grammar here?
|
|
|
|
// TODO: support hotloading changes to the grammar
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Deinit()
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Serialize(ISerializer& serialize)
|
|
|
|
{
|
2016-05-02 11:26:07 +02:00
|
|
|
std::map<CStr, std::vector<entity_id_t>> templateMap;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2016-05-02 21:03:19 +02:00
|
|
|
for (const std::pair<entity_id_t, std::string>& templateEnt : m_LatestTemplates)
|
2016-05-02 11:26:07 +02:00
|
|
|
if (!ENTITY_IS_LOCAL(templateEnt.first))
|
|
|
|
templateMap[templateEnt.second].push_back(templateEnt.first);
|
2010-05-23 01:02:07 +02:00
|
|
|
|
2016-05-02 11:26:07 +02:00
|
|
|
SerializeMap<SerializeString, SerializeVector<SerializeU32_Unbounded>>()(serialize, "templates", templateMap);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2011-01-16 15:08:38 +01:00
|
|
|
Init(paramNode);
|
2010-04-14 19:22:32 +02:00
|
|
|
|
2016-05-02 11:26:07 +02:00
|
|
|
std::map<CStr, std::vector<entity_id_t>> templateMap;
|
|
|
|
SerializeMap<SerializeString, SerializeVector<SerializeU32_Unbounded>>()(deserialize, "templates", templateMap);
|
2016-05-02 21:03:19 +02:00
|
|
|
for (const std::pair<CStr, std::vector<entity_id_t>>& mapEl : templateMap)
|
2016-05-02 11:26:07 +02:00
|
|
|
for (entity_id_t id : mapEl.second)
|
2016-11-23 15:09:58 +01:00
|
|
|
m_LatestTemplates[id] = mapEl.first;
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-03-07 21:14:30 +01:00
|
|
|
switch (msg.GetType())
|
|
|
|
{
|
|
|
|
case MT_Destroy:
|
|
|
|
{
|
|
|
|
const CMessageDestroy& msgData = static_cast<const CMessageDestroy&> (msg);
|
|
|
|
|
|
|
|
// Clean up m_LatestTemplates so it doesn't record any data for destroyed entities
|
|
|
|
m_LatestTemplates.erase(msgData.entity);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-07-21 18:04:17 +02:00
|
|
|
virtual void DisableValidation()
|
|
|
|
{
|
|
|
|
m_DisableValidation = true;
|
|
|
|
}
|
|
|
|
|
2017-01-15 19:41:53 +01:00
|
|
|
virtual const CParamNode* LoadTemplate(entity_id_t ent, const std::string& templateName);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2016-01-23 16:17:56 +01:00
|
|
|
virtual const CParamNode* GetTemplate(const std::string& templateName);
|
2010-01-26 00:43:58 +01:00
|
|
|
|
2016-01-23 16:17:56 +01:00
|
|
|
virtual const CParamNode* GetTemplateWithoutValidation(const std::string& templateName);
|
2011-02-20 21:45:39 +01:00
|
|
|
|
2017-01-20 03:25:19 +01:00
|
|
|
virtual bool TemplateExists(const std::string& templateName) const;
|
2015-01-02 00:10:24 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
virtual const CParamNode* LoadLatestTemplate(entity_id_t ent);
|
|
|
|
|
2017-01-20 03:25:19 +01:00
|
|
|
virtual std::string GetCurrentTemplateName(entity_id_t ent) const;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2017-01-20 03:25:19 +01:00
|
|
|
virtual std::vector<std::string> FindAllTemplates(bool includeActors) const;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2017-11-24 20:13:28 +01:00
|
|
|
virtual std::vector<std::string> FindUsedTemplates() const;
|
|
|
|
|
2017-01-20 03:25:19 +01:00
|
|
|
virtual std::vector<entity_id_t> GetEntitiesUsingTemplate(const std::string& templateName) const;
|
2010-11-07 01:12:55 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
private:
|
2014-06-07 14:12:53 +02:00
|
|
|
// Template loader
|
|
|
|
CTemplateLoader m_templateLoader;
|
|
|
|
|
2010-04-14 19:22:32 +02:00
|
|
|
// Entity template XML validator
|
|
|
|
RelaxNGValidator m_Validator;
|
|
|
|
|
2010-07-21 18:04:17 +02:00
|
|
|
// Disable validation, for test cases
|
|
|
|
bool m_DisableValidation;
|
|
|
|
|
2010-04-14 19:22:32 +02:00
|
|
|
// 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)
|
2010-05-25 20:17:12 +02:00
|
|
|
std::map<std::string, bool> m_TemplateSchemaValidity;
|
2010-04-14 19:22:32 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
// Remember the template used by each entity, so we can return them
|
|
|
|
// again for deserialization.
|
2010-05-25 20:17:12 +02:00
|
|
|
std::map<entity_id_t, std::string> m_LatestTemplates;
|
2010-01-09 20:20:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_COMPONENT_TYPE(TemplateManager)
|
|
|
|
|
2017-01-15 19:41:53 +01:00
|
|
|
const CParamNode* CCmpTemplateManager::LoadTemplate(entity_id_t ent, const std::string& templateName)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
m_LatestTemplates[ent] = templateName;
|
|
|
|
|
2017-01-15 19:41:53 +01:00
|
|
|
return GetTemplate(templateName);
|
2010-01-26 00:43:58 +01:00
|
|
|
}
|
|
|
|
|
2016-01-23 16:17:56 +01:00
|
|
|
const CParamNode* CCmpTemplateManager::GetTemplate(const std::string& templateName)
|
2010-01-26 00:43:58 +01:00
|
|
|
{
|
2014-06-07 14:12:53 +02:00
|
|
|
const CParamNode& fileData = m_templateLoader.GetTemplateFileData(templateName);
|
|
|
|
if (!fileData.IsOk())
|
2010-01-14 21:36:29 +01:00
|
|
|
return NULL;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-07-21 18:04:17 +02:00
|
|
|
if (!m_DisableValidation)
|
|
|
|
{
|
|
|
|
// Compute validity, if it's not computed before
|
|
|
|
if (m_TemplateSchemaValidity.find(templateName) == m_TemplateSchemaValidity.end())
|
2012-03-08 06:13:35 +01:00
|
|
|
{
|
2014-06-07 14:12:53 +02:00
|
|
|
m_TemplateSchemaValidity[templateName] = m_Validator.Validate(wstring_from_utf8(templateName), fileData.ToXML());
|
2012-03-08 06:13:35 +01:00
|
|
|
|
|
|
|
// Show error on the first failure to validate the template
|
|
|
|
if (!m_TemplateSchemaValidity[templateName])
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Failed to validate entity template '%s'", templateName.c_str());
|
2012-03-08 06:13:35 +01:00
|
|
|
}
|
2010-07-21 18:04:17 +02:00
|
|
|
// Refuse to return invalid templates
|
|
|
|
if (!m_TemplateSchemaValidity[templateName])
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-04-14 19:22:32 +02:00
|
|
|
|
2014-06-07 14:12:53 +02:00
|
|
|
const CParamNode& templateRoot = fileData.GetChild("Entity");
|
2010-02-03 00:01:17 +01:00
|
|
|
if (!templateRoot.IsOk())
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-07-21 18:04:17 +02:00
|
|
|
// The validator should never let this happen
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Invalid root element in entity template '%s'", templateName.c_str());
|
2010-01-09 20:20:14 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-03 00:01:17 +01:00
|
|
|
return &templateRoot;
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2016-01-23 16:17:56 +01:00
|
|
|
const CParamNode* CCmpTemplateManager::GetTemplateWithoutValidation(const std::string& templateName)
|
2011-02-20 21:45:39 +01:00
|
|
|
{
|
2014-06-07 14:12:53 +02:00
|
|
|
const CParamNode& templateRoot = m_templateLoader.GetTemplateFileData(templateName).GetChild("Entity");
|
2011-02-20 21:45:39 +01:00
|
|
|
if (!templateRoot.IsOk())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return &templateRoot;
|
|
|
|
}
|
|
|
|
|
2017-01-20 03:25:19 +01:00
|
|
|
bool CCmpTemplateManager::TemplateExists(const std::string& templateName) const
|
2015-01-02 00:10:24 +01:00
|
|
|
{
|
|
|
|
return m_templateLoader.TemplateExists(templateName);
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
const CParamNode* CCmpTemplateManager::LoadLatestTemplate(entity_id_t ent)
|
|
|
|
{
|
2010-05-25 20:17:12 +02:00
|
|
|
std::map<entity_id_t, std::string>::const_iterator it = m_LatestTemplates.find(ent);
|
2010-01-09 20:20:14 +01:00
|
|
|
if (it == m_LatestTemplates.end())
|
|
|
|
return NULL;
|
2017-01-15 19:41:53 +01:00
|
|
|
return LoadTemplate(ent, it->second);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 03:25:19 +01:00
|
|
|
std::string CCmpTemplateManager::GetCurrentTemplateName(entity_id_t ent) const
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-05-25 20:17:12 +02:00
|
|
|
std::map<entity_id_t, std::string>::const_iterator it = m_LatestTemplates.find(ent);
|
2010-01-09 20:20:14 +01:00
|
|
|
if (it == m_LatestTemplates.end())
|
2010-05-25 20:17:12 +02:00
|
|
|
return "";
|
2010-01-09 20:20:14 +01:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2017-01-20 03:25:19 +01:00
|
|
|
std::vector<std::string> CCmpTemplateManager::FindAllTemplates(bool includeActors) const
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2014-06-07 14:12:53 +02:00
|
|
|
ETemplatesType templatesType = includeActors ? ALL_TEMPLATES : SIMULATION_TEMPLATES;
|
|
|
|
return m_templateLoader.FindTemplates("", true, templatesType);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
2010-01-14 21:36:29 +01:00
|
|
|
|
2017-11-24 20:13:28 +01:00
|
|
|
std::vector<std::string> CCmpTemplateManager::FindUsedTemplates() const
|
|
|
|
{
|
|
|
|
std::vector<std::string> usedTemplates;
|
|
|
|
for (const std::pair<entity_id_t, std::string>& p : m_LatestTemplates)
|
|
|
|
if (std::find(usedTemplates.begin(), usedTemplates.end(), p.second) == usedTemplates.end())
|
|
|
|
usedTemplates.push_back(p.second);
|
|
|
|
return usedTemplates;
|
|
|
|
}
|
|
|
|
|
2010-11-07 01:12:55 +01:00
|
|
|
/**
|
|
|
|
* Get the list of entities using the specified template
|
|
|
|
*/
|
2017-01-20 03:25:19 +01:00
|
|
|
std::vector<entity_id_t> CCmpTemplateManager::GetEntitiesUsingTemplate(const std::string& templateName) const
|
2010-11-07 01:12:55 +01:00
|
|
|
{
|
|
|
|
std::vector<entity_id_t> entities;
|
2017-01-20 03:25:19 +01:00
|
|
|
for (const std::pair<entity_id_t, std::string>& p : m_LatestTemplates)
|
|
|
|
if (p.second == templateName)
|
|
|
|
entities.push_back(p.first);
|
|
|
|
|
2010-11-07 01:12:55 +01:00
|
|
|
return entities;
|
|
|
|
}
|