Line data Source code
1 : /* Copyright (C) 2023 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_ICMPTEMPLATEMANAGER
19 : #define INCLUDED_ICMPTEMPLATEMANAGER
20 :
21 : #include "simulation2/system/Interface.h"
22 :
23 : #include <vector>
24 :
25 : /**
26 : * Template manager: Handles the loading of entity template files for the initialisation
27 : * and deserialization of entity components.
28 : *
29 : * Template names are intentionally restricted to ASCII strings for storage/serialization
30 : * efficiency (we have a lot of strings so this is significant);
31 : * they correspond to filenames so they shouldn't contain non-ASCII anyway.
32 : */
33 22 : class ICmpTemplateManager : public IComponent
34 : {
35 : public:
36 : /**
37 : * Loads the template XML file identified by 'templateName' (including inheritance
38 : * from parent XML files) for use with a new entity 'ent'.
39 : * The returned CParamNode must not be used for any entities other than 'ent'.
40 : *
41 : * If templateName is of the form "actor|foo" then it will load a default
42 : * stationary entity template that uses actor "foo". (This is a convenience to
43 : * avoid the need for hundreds of tiny decorative-object entity templates.)
44 : *
45 : * If templateName is of the form "preview|foo" then it will load a template
46 : * based on entity template "foo" with the non-graphical components removed.
47 : * (This is for previewing construction/placement of units.)
48 : *
49 : * If templateName is of the form "corpse|foo" then it will load a template
50 : * like "preview|foo" but with corpse-related components included.
51 : *
52 : * If templateName is of the form "foundation|foo" then it will load a template
53 : * based on entity template "foo" with various components removed and a few changed
54 : * and added. (This is for constructing foundations of buildings.)
55 : *
56 : * @return NULL on error
57 : */
58 : virtual const CParamNode* LoadTemplate(entity_id_t ent, const std::string& templateName) = 0;
59 :
60 : /**
61 : * Loads the template XML file identified by 'templateName' (including inheritance
62 : * from parent XML files). The templateName syntax is the same as LoadTemplate.
63 : *
64 : * @return NULL on error
65 : */
66 : virtual const CParamNode* GetTemplate(const std::string& templateName) = 0;
67 :
68 : /**
69 : * Like GetTemplate, except without doing the XML validation (so it's faster but
70 : * may return invalid templates).
71 : *
72 : * @return NULL on error
73 : */
74 : virtual const CParamNode* GetTemplateWithoutValidation(const std::string& templateName) = 0;
75 :
76 : /**
77 : * Check if the template XML file exists, without trying to load it.
78 : */
79 : virtual bool TemplateExists(const std::string& templateName) const = 0;
80 :
81 : /**
82 : * Returns the template most recently specified for the entity 'ent'.
83 : * Used during deserialization.
84 : *
85 : * @return NULL on error
86 : */
87 : virtual const CParamNode* LoadLatestTemplate(entity_id_t ent) = 0;
88 :
89 : /**
90 : * Returns the name of the template most recently specified for the entity 'ent'.
91 : */
92 : virtual std::string GetCurrentTemplateName(entity_id_t ent) const = 0;
93 :
94 : /**
95 : * Returns the list of entities having the specified template.
96 : */
97 : virtual std::vector<entity_id_t> GetEntitiesUsingTemplate(const std::string& templateName) const = 0;
98 :
99 : /**
100 : * Returns a list of strings that could be validly passed as @c templateName to LoadTemplate.
101 : * (This includes "actor|foo" etc names).
102 : * Intended for use by the map editor. This is likely to be quite slow.
103 : */
104 : virtual std::vector<std::string> FindAllTemplates(bool includeActors) const = 0;
105 :
106 : /**
107 : * Returns a list of strings that could be validly passed as @c templateName to LoadTemplate.
108 : * (Filters to only return templates with the correct root name.)
109 : * This is likely to be quite slow.
110 : */
111 : virtual std::vector<std::string> FindTemplatesWithRootName(bool includeActors, const std::string& rootName) = 0;
112 :
113 : /**
114 : * Returns some data of the civs from the templates.
115 : * Intended for use by the map editor.
116 : */
117 : virtual std::vector<std::vector<std::wstring>> GetCivData() = 0;
118 :
119 : /**
120 : * Returns a list of strings that could be validly passed as @c templateName to LoadTemplate.
121 : * Intended for use by the AI manager.
122 : */
123 : virtual std::vector<std::string> FindUsedTemplates() const = 0;
124 :
125 : /**
126 : * Permanently disable XML validation (intended solely for test cases).
127 : */
128 : virtual void DisableValidation() = 0;
129 :
130 : /*
131 : * TODO:
132 : * When an entity changes template (e.g. upgrades) or player ownership, it
133 : * should call some Reload(ent, templateName, playerID) function to load its new template.
134 : * When a file changes on disk, something should call Reload(templateName).
135 : *
136 : * Reloading should happen by sending a message to affected components (containing
137 : * their new CParamNode), then automatically updating this.template of scripted components.
138 : */
139 :
140 116 : DECLARE_INTERFACE_TYPE(TemplateManager)
141 : };
142 :
143 : #endif // INCLUDED_ICMPTEMPLATEMANAGER
|