Pyrogenesis  trunk
TemplateLoader.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 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 
23 
25 {
29 };
30 
31 /**
32  * Template loader: Handles the loading of entity template files for:
33  * - the initialisation and deserialization of entity components in the
34  * simulation (CmpTemplateManager).
35  * - access to actor templates, obstruction data, etc. in RMS/RMGEN
36  * - access to various templates in the GUI, to display faction specificities
37  *
38  * Template names are intentionally restricted to ASCII strings for storage/serialization
39  * efficiency (we have a lot of strings so this is significant);
40  * they correspond to filenames so they shouldn't contain non-ASCII anyway.
41  *
42  *
43  * TODO: Find a way to validate templates outside of the simulation.
44  */
46 {
47 public:
49  {
50  }
51 
52  /**
53  * Provides the file data for requested template.
54  */
55  const CParamNode& GetTemplateFileData(const std::string& templateName);
56 
57  /**
58  * Check if the template XML file exits, without trying to load it.
59  */
60  bool TemplateExists(const std::string& templateName) const;
61 
62  /**
63  * Returns a list of strings that could be validly passed as @c templateName to LoadTemplateFile.
64  * (This includes "actor|foo" etc names).
65  */
66  std::vector<std::string> FindTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType) const;
67 
68  std::vector<std::string> FindPlaceableTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType, ScriptInterface& scriptInterface) const;
69 
70 private:
71  /**
72  * (Re)loads the given template, regardless of whether it exists already,
73  * and saves into m_TemplateFileData. Also loads any parents that are not yet
74  * loaded. Returns false on error.
75  * @param templateName XML filename to load (not a |-separated string)
76  */
77  bool LoadTemplateFile(const std::string& templateName, int depth);
78 
79  /**
80  * Constructs a standard static-decorative-object template for the given actor
81  */
82  void ConstructTemplateActor(const std::string& actorName, CParamNode& out);
83 
84  /**
85  * Map from template name (XML filename or special |-separated string) to the most recently
86  * loaded non-broken template data. This includes files that will fail schema validation.
87  * (Failed loads won't remove existing entries under the same name, so we behave more nicely
88  * when hotloading broken files)
89  */
90  std::map<std::string, CParamNode> m_TemplateFileData;
91 };
92 
93 #endif // INCLUDED_TEMPLATELOADER
An entity initialisation parameter node.
Definition: ParamNode.h:148
Template loader: Handles the loading of entity template files for:
Definition: TemplateLoader.h:45
std::vector< std::string > FindPlaceableTemplates(const std::string &path, bool includeSubdirectories, ETemplatesType templatesType, ScriptInterface &scriptInterface) const
Definition: TemplateLoader.cpp:155
const CParamNode & GetTemplateFileData(const std::string &templateName)
Provides the file data for requested template.
Definition: TemplateLoader.cpp:259
void ConstructTemplateActor(const std::string &actorName, CParamNode &out)
Constructs a standard static-decorative-object template for the given actor.
Definition: TemplateLoader.cpp:271
bool LoadTemplateFile(const std::string &templateName, int depth)
(Re)loads the given template, regardless of whether it exists already, and saves into m_TemplateFileD...
Definition: TemplateLoader.cpp:32
ETemplatesType
Definition: TemplateLoader.h:24
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:419
Definition: TemplateLoader.h:28
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:238
bool TemplateExists(const std::string &templateName) const
Check if the template XML file exits, without trying to load it.
Definition: TemplateLoader.cpp:148
Definition: TemplateLoader.h:26
Definition: TemplateLoader.h:27
CTemplateLoader()
Definition: TemplateLoader.h:48
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptInterface.h:71
std::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:90