Changeset View
Changeset View
Standalone View
Standalone View
source/ps/TemplateLoader.h
Show All 17 Lines | |||||
#ifndef INCLUDED_TEMPLATELOADER | #ifndef INCLUDED_TEMPLATELOADER | ||||
#define INCLUDED_TEMPLATELOADER | #define INCLUDED_TEMPLATELOADER | ||||
#include "simulation2/system/ParamNode.h" | #include "simulation2/system/ParamNode.h" | ||||
#include <string_view> | #include <string_view> | ||||
#include <unordered_map> | #include <unordered_map> | ||||
enum ETemplatesType | |||||
{ | |||||
ALL_TEMPLATES, | |||||
ACTOR_TEMPLATES, | |||||
SIMULATION_TEMPLATES | |||||
}; | |||||
/** | /** | ||||
* Template loader: Handles the loading of entity template files for: | * Template loader: Handles the loading of entity template files for: | ||||
* - the initialisation and deserialization of entity components in the | * - the initialisation and deserialization of entity components in the | ||||
* simulation (CmpTemplateManager). | * simulation (CmpTemplateManager). | ||||
* - access to actor templates, obstruction data, etc. in RMS/RMGEN | * - access to actor templates, obstruction data, etc. in RMS/RMGEN | ||||
* - access to various templates in the GUI, to display faction specificities | * - access to various templates in the GUI, to display faction specificities | ||||
* | * | ||||
* Template names are intentionally restricted to ASCII strings for storage/serialization | * Template names are intentionally restricted to ASCII strings for storage/serialization | ||||
* efficiency (we have a lot of strings so this is significant); | * efficiency (we have a lot of strings so this is significant); | ||||
* they correspond to filenames so they shouldn't contain non-ASCII anyway. | * they correspond to filenames so they shouldn't contain non-ASCII anyway. | ||||
* | * | ||||
* | * | ||||
* TODO: Find a way to validate templates outside of the simulation. | * TODO: Find a way to validate templates outside of the simulation. | ||||
*/ | */ | ||||
class CTemplateLoader | |||||
namespace TemplateLoader | |||||
{ | { | ||||
public: | |||||
CTemplateLoader() | enum class ETemplatesType | ||||
{ | { | ||||
} | ALL_TEMPLATES, | ||||
ACTOR_TEMPLATES, | |||||
SIMULATION_TEMPLATES | |||||
Stan: Wonder if we could use ALLTEMPLATEs = ACTOR_TEMPLATES & SIMULATION_TEMPLATES | |||||
}; | |||||
Not Done Inline Actionsenum class. vladislavbelov: `enum class`. | |||||
/** | /** | ||||
* Provides the file data for requested template. | * Provides the file data for requested template. | ||||
*/ | */ | ||||
const CParamNode& GetTemplateFileData(const std::string& templateName); | const CParamNode& GetTemplateFileData(const std::string& templateName); | ||||
Not Done Inline Actions& and * stick to the type not to the name. vladislavbelov: `&` and `*` stick to the type not to the name. | |||||
/** | /** | ||||
* Check if the template XML file exits, without trying to load it. | * Check if the template XML file exits, without trying to load it. | ||||
*/ | */ | ||||
bool TemplateExists(const std::string& templateName) const; | bool TemplateExists(const std::string& templateName); | ||||
/** | /** | ||||
* Returns a list of strings that could be validly passed as @c templateName to LoadTemplateFile. | * Returns a list of strings that could be validly passed as @c templateName to LoadTemplateFile. | ||||
* (This includes "actor|foo" etc names). | * (This includes "actor|foo" etc names). | ||||
*/ | */ | ||||
std::vector<std::string> FindTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType) const; | std::vector<std::string> FindTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType); | ||||
/** | /** | ||||
* Returns a list of strings that could validly be passed as @c templateName to LoadTemplateFile. | * Returns a list of strings that could validly be passed as @c templateName to LoadTemplateFile. | ||||
* Not ignoring any special directories. | * Not ignoring any special directories. | ||||
*/ | */ | ||||
std::vector<std::string> FindTemplatesUnrestricted(const std::string& path, bool includeSubdirectories) const; | std::vector<std::string> FindTemplatesUnrestricted(const std::string& path, bool includeSubdirectories); | ||||
private: | |||||
/** | |||||
* (Re)loads the given template, regardless of whether it exists already, | |||||
* and saves into m_TemplateFileData. Also loads any parents that are not yet | |||||
* loaded. Returns false on error. | |||||
* @param templateName - XML filename to load (may be a |-separated string) | |||||
* @param compositing - whether this template is an intermediary layer in a |-separated string. | |||||
* @param depth - the current recursion depth. | |||||
*/ | |||||
bool LoadTemplateFile(CParamNode& node, std::string_view templateName, bool compositing, int depth); | |||||
/** | |||||
* Constructs a standard static-decorative-object template for the given actor | |||||
*/ | |||||
void ConstructTemplateActor(std::string_view actorName, CParamNode& out); | |||||
/** | |||||
* Map from template name (XML filename or special |-separated string) to the most recently | |||||
* loaded non-broken template data. This includes files that will fail schema validation. | |||||
* (Failed loads won't remove existing entries under the same name, so we behave more nicely | |||||
* when hotloading broken files) | |||||
*/ | |||||
std::unordered_map<std::string, CParamNode> m_TemplateFileData; | |||||
}; | }; | ||||
#endif // INCLUDED_TEMPLATELOADER | #endif // INCLUDED_TEMPLATELOADER |
Wildfire Games · Phabricator
Wonder if we could use ALLTEMPLATEs = ACTOR_TEMPLATES & SIMULATION_TEMPLATES