Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
TemplateLoader.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_TEMPLATELOADER
19#define INCLUDED_TEMPLATELOADER
20
22
23#include <string>
24#include <string_view>
25#include <unordered_map>
26#include <vector>
27
29{
33};
34
35/**
36 * Template loader: Handles the loading of entity template files for:
37 * - the initialisation and deserialization of entity components in the
38 * simulation (CmpTemplateManager).
39 * - access to actor templates, obstruction data, etc. in RMS/RMGEN
40 * - access to various templates in the GUI, to display faction specificities
41 *
42 * Template names are intentionally restricted to ASCII strings for storage/serialization
43 * efficiency (we have a lot of strings so this is significant);
44 * they correspond to filenames so they shouldn't contain non-ASCII anyway.
45 *
46 *
47 * TODO: Find a way to validate templates outside of the simulation.
48 */
50{
51public:
53 {
54 }
55
56 /**
57 * Provides the file data for requested template.
58 */
59 const CParamNode& GetTemplateFileData(const std::string& templateName);
60
61 /**
62 * Check if the template XML file exits, without trying to load it.
63 */
64 bool TemplateExists(const std::string& templateName) const;
65
66 /**
67 * Returns a list of strings that could be validly passed as @c templateName to LoadTemplateFile.
68 * (This includes "actor|foo" etc names).
69 */
70 std::vector<std::string> FindTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType) const;
71
72 /**
73 * Returns a list of strings that could validly be passed as @c templateName to LoadTemplateFile.
74 * Not ignoring any special directories.
75 */
76 std::vector<std::string> FindTemplatesUnrestricted(const std::string& path, bool includeSubdirectories) const;
77
78private:
79 /**
80 * (Re)loads the given template, regardless of whether it exists already,
81 * and saves into m_TemplateFileData. Also loads any parents that are not yet
82 * loaded. Returns false on error.
83 * @param templateName - XML filename to load (may be a |-separated string)
84 * @param compositing - whether this template is an intermediary layer in a |-separated string.
85 * @param depth - the current recursion depth.
86 */
87 bool LoadTemplateFile(CParamNode& node, std::string_view templateName, bool compositing, int depth);
88
89 /**
90 * Constructs a standard static-decorative-object template for the given actor
91 */
92 void ConstructTemplateActor(std::string_view actorName, CParamNode& out);
93
94 /**
95 * Map from template name (XML filename or special |-separated string) to the most recently
96 * loaded non-broken template data. This includes files that will fail schema validation.
97 * (Failed loads won't remove existing entries under the same name, so we behave more nicely
98 * when hotloading broken files)
99 */
100 std::unordered_map<std::string, CParamNode> m_TemplateFileData;
101};
102
103#endif // INCLUDED_TEMPLATELOADER
ETemplatesType
Definition: TemplateLoader.h:29
@ ALL_TEMPLATES
Definition: TemplateLoader.h:30
@ SIMULATION_TEMPLATES
Definition: TemplateLoader.h:32
@ ACTOR_TEMPLATES
Definition: TemplateLoader.h:31
An entity initialisation parameter node.
Definition: ParamNode.h:153
Template loader: Handles the loading of entity template files for:
Definition: TemplateLoader.h:50
std::unordered_map< std::string, CParamNode > m_TemplateFileData
Map from template name (XML filename or special |-separated string) to the most recently loaded non-b...
Definition: TemplateLoader.h:100
bool TemplateExists(const std::string &templateName) const
Check if the template XML file exits, without trying to load it.
Definition: TemplateLoader.cpp:148
const CParamNode & GetTemplateFileData(const std::string &templateName)
Provides the file data for requested template.
Definition: TemplateLoader.cpp:187
std::vector< std::string > FindTemplatesUnrestricted(const std::string &path, bool includeSubdirectories) const
Returns a list of strings that could validly be passed as templateName to LoadTemplateFile.
Definition: TemplateLoader.cpp:176
void ConstructTemplateActor(std::string_view actorName, CParamNode &out)
Constructs a standard static-decorative-object template for the given actor.
Definition: TemplateLoader.cpp:201
CTemplateLoader()
Definition: TemplateLoader.h:52
std::vector< std::string > FindTemplates(const std::string &path, bool includeSubdirectories, ETemplatesType templatesType) const
Returns a list of strings that could be validly passed as templateName to LoadTemplateFile.
Definition: TemplateLoader.cpp:155
bool LoadTemplateFile(CParamNode &node, std::string_view templateName, bool compositing, int depth)
(Re)loads the given template, regardless of whether it exists already, and saves into m_TemplateFileD...
Definition: TemplateLoader.cpp:46
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407