Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ParamNode.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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_PARAMNODE
19#define INCLUDED_PARAMNODE
20
22#include "maths/Fixed.h"
23#include "ps/Errors.h"
24
25#include <cstddef>
26#include <iosfwd>
27#include <js/TypeDecls.h>
28#include <map>
29#include <memory>
30#include <string>
31
32class CStrIntern;
33class XMBData;
34class XMBElement;
35namespace Script { class Request; }
36
37/**
38 * An entity initialisation parameter node.
39 * Each node has a text value, plus a number of named child nodes (in a tree structure).
40 * Child nodes are unordered, and there cannot be more than one with the same name.
41 * Nodes are immutable.
42 *
43 * Nodes can be initialised from XML files. Child elements are mapped onto child nodes.
44 * Attributes are mapped onto child nodes with names prefixed by "@"
45 * (e.g. the XML <code>&lt;a b="c">&lt;d/>&lt;/a></code> is loaded as a node with two
46 * child nodes, one called "@b" and one called "d").
47 *
48 * They can also be initialised from @em multiple XML files,
49 * which is used by ICmpTemplateManager for entity template inheritance.
50 * Loading one XML file like:
51 * @code
52 * <Entity>
53 * <Example1>
54 * <A attr="value">text</A>
55 * </Example1>
56 * <Example2>
57 * <B/>
58 * </Example2>
59 * <Example3>
60 * <C/>
61 * </Example3>
62 * <Example4 datatype="tokens">
63 * one two three
64 * </Example4>
65 * <Example5>
66 * <E/>
67 * <F>
68 * <I>test</I>
69 * </F>
70 * <H>
71 * <J>example</J>
72 * </H>
73 * </Example5>
74 * </Entity>
75 * @endcode
76 * then a second like:
77 * @code
78 * <Entity>
79 * <Example1>
80 * <A>example</A> <!-- replace the content of the old A element -->
81 * <D>new</D> <!-- add a new child to the old Example1 element -->
82 * </Example1>
83 * <Example2 disable=""/> <!-- delete the old Example2 element -->
84 * <Example3 replace=""> <!-- replace all the old children of the Example3 element -->
85 * <D>new</D>
86 * </Example3>
87 * <Example4 datatype="tokens"> <!-- treat as space-separated lists of tokens to merge -->
88 * four <!-- add a token to the parent's set -->
89 * -two <!-- remove a token from the parent's set -->
90 * </Example4>
91 * <Example5 filtered=""> <!-- drop all children of this node that are not in this file -->
92 * <F merge=""> <!-- only add this element if it is also present in the parent -->
93 * <K>example</K> <!-- if F is present merge its children normally -->
94 * </F>
95 * <G merge=""/> <!-- keep the G element of the parent if it exists -->
96 * <H>
97 * <J>text</J>
98 * </H>
99 * </Example5>
100 * </Entity>
101 * @endcode
102 * is equivalent to loading a single file like:
103 * @code
104 * <Entity>
105 * <Example1>
106 * <A attr="value">example</A>
107 * <D>new</D>
108 * </Example1>
109 * <Example3>
110 * <D>new</D>
111 * </Example3>
112 * <Example4>
113 * one three four
114 * </Example4>
115 * <Example5>
116 * <F>
117 * <I>test</I>
118 * <K>example</K>
119 * </F>
120 * <H>
121 * <J>text</J>
122 * </H>
123 * </Example5>
124 * </Entity>
125 * @endcode
126 *
127 * Parameter nodes can be translated to JavaScript objects. The previous example will become the object:
128 * @code
129 * { "Entity": {
130 * "Example1": {
131 * "A": { "@attr": "value", "_string": "example" },
132 * "D": "new"
133 * },
134 * "Example3": {
135 * "D": "new"
136 * },
137 * "Example4": { "@datatype": "tokens", "_string": "one three four" },
138 * "Example5": {
139 * "F": {
140 * "I": "test",
141 * "K": "example"
142 * },
143 * "H": {
144 * "J": "text"
145 * }
146 * }
147 * }
148 * }
149 * @endcode
150 * (Note the special @c _string for the hopefully-rare cases where a node contains both child nodes and text.)
151 */
153{
154public:
155 typedef std::map<std::string, CParamNode> ChildrenMap;
156
157 /**
158 * Constructs a new, empty node.
159 */
160 CParamNode(bool isOk = true);
161
162 /**
163 * Loads the XML data specified by @a file into the node @a ret.
164 * Any existing data in @a ret will be overwritten or else kept, so this
165 * can be called multiple times to build up a node from multiple inputs.
166 *
167 * @param sourceIdentifier Optional; string you can pass along to indicate the source of
168 * the data getting loaded. Used for output to log messages if an error occurs.
169 */
170 static void LoadXML(CParamNode& ret, const XMBData& xmb, const wchar_t* sourceIdentifier = NULL);
171
172 /**
173 * Loads the XML data specified by @a path into the node @a ret.
174 * Any existing data in @a ret will be overwritten or else kept, so this
175 * can be called multiple times to build up a node from multiple inputs.
176 */
177 static void LoadXML(CParamNode& ret, const VfsPath& path, const std::string& validatorName);
178
179 /**
180 * See LoadXML, but parses the XML string @a xml.
181 * @return error code if parsing failed, else @c PSRETURN_OK
182 *
183 * @param sourceIdentifier Optional; string you can pass along to indicate the source of
184 * the data getting loaded. Used for output to log messages if an error occurs.
185 */
186 static PSRETURN LoadXMLString(CParamNode& ret, const char* xml, const wchar_t* sourceIdentifier = NULL);
187
188 /**
189 * Returns the (unique) child node with the given name, or a node with IsOk() == false if there is none.
190 */
191 const CParamNode& GetChild(const char* name) const;
192 // (Children are returned as const in order to allow future optimisations, where we assume
193 // a node is always modified explicitly and not indirectly via its children, e.g. to cache JS::Values)
194
195 /**
196 * Returns the only child node, or a node with IsOk() == false if there is none.
197 * This is mainly useful for the root node.
198 */
199 const CParamNode& GetOnlyChild() const;
200
201 /**
202 * Returns true if this is a valid CParamNode, false if it represents a non-existent node
203 */
204 bool IsOk() const;
205
206 /**
207 * Returns the content of this node as a wstring
208 */
209 const std::wstring ToWString() const;
210
211 /**
212 * Returns the content of this node as an UTF8 string
213 */
214 const std::string& ToString() const;
215
216 /**
217 * Returns the content of this node as an internalized 8-bit string. Should only be used for
218 * predictably small and frequently-used strings.
219 */
220 const CStrIntern ToUTF8Intern() const;
221
222 /**
223 * Parses the content of this node as an integer
224 */
225 int ToInt() const;
226
227 /**
228 * Parses the content of this node as a fixed-point number
229 */
230 fixed ToFixed() const;
231
232 /**
233 * Parses the content of this node as a floating-point number
234 */
235 float ToFloat() const;
236
237 /**
238 * Parses the content of this node as a boolean ("true" == true, anything else == false)
239 */
240 bool ToBool() const;
241
242 /**
243 * Returns the content of this node and its children as an XML string
244 */
245 std::string ToXMLString() const;
246
247 /**
248 * Write the content of this node and its children as an XML string, to the stream
249 */
250 void ToXMLString(std::ostream& strm) const;
251
252 /**
253 * Returns a JS::Value representation of this node and its children.
254 * If @p cacheValue is true, then the same JS::Value will be returned each time
255 * this is called (regardless of whether you passed the same @p cx - be careful
256 * to only use the cache in one context).
257 * Cached object values are frozen, using DeepFreezeObject, so that it's safe to
258 * share between components and to reconstruct on deserialization.
259 * When caching, the lifetime of @p cx must be longer than the lifetime of this node.
260 * The cache will be reset if *this* node is modified (e.g. by LoadXML),
261 * but *not* if any child nodes are modified (so don't do that).
262 */
263 void ToJSVal(const Script::Request& rq, bool cacheValue, JS::MutableHandleValue ret) const;
264
265 /**
266 * Returns the names/nodes of the children of this node, ordered by name
267 */
268 const ChildrenMap& GetChildren() const;
269
270 /**
271 * Escapes a string so that it is well-formed XML content/attribute text.
272 * (Replaces "&" with "&amp;" etc)
273 */
274 static std::string EscapeXMLString(const std::string& str);
275
276 std::string m_Name;
277 std::uint32_t m_Index;
278
279private:
280
281 /**
282 * Overlays the specified data onto this node. See class documentation for the concept and examples.
283 *
284 * @param xmb Representation of the XMB file containing an element with the data to apply.
285 * @param element Element inside the specified @p xmb file containing the data to apply.
286 * @param sourceIdentifier Optional; string you can pass along to indicate the source of
287 * the data getting applied. Used for output to log messages if an error occurs.
288 */
289 void ApplyLayer(const XMBData& xmb, const XMBElement& element, const wchar_t* sourceIdentifier = NULL);
290
291 void ResetScriptVal();
292
293 void ConstructJSVal(const Script::Request& rq, JS::MutableHandleValue ret) const;
294
295 std::string m_Value;
297 bool m_IsOk;
298
299 /**
300 * Caches the ToJSVal script representation of this node.
301 */
302 mutable std::shared_ptr<JS::PersistentRootedValue> m_ScriptVal;
303};
304
305#endif // INCLUDED_PARAMNODE
std::uint32_t PSRETURN
Definition: Errors.h:75
A simple fixed-point number class.
Definition: Fixed.h:129
An entity initialisation parameter node.
Definition: ParamNode.h:153
std::string m_Value
Definition: ParamNode.h:295
const CStrIntern ToUTF8Intern() const
Returns the content of this node as an internalized 8-bit string.
Definition: ParamNode.cpp:290
const ChildrenMap & GetChildren() const
Returns the names/nodes of the children of this node, ordered by name.
Definition: ParamNode.cpp:318
std::string ToXMLString() const
Returns the content of this node and its children as an XML string.
Definition: ParamNode.cpp:348
static PSRETURN LoadXMLString(CParamNode &ret, const char *xml, const wchar_t *sourceIdentifier=NULL)
See LoadXML, but parses the XML string xml.
Definition: ParamNode.cpp:75
std::string m_Name
Definition: ParamNode.h:276
bool m_IsOk
Definition: ParamNode.h:297
static std::string EscapeXMLString(const std::string &str)
Escapes a string so that it is well-formed XML content/attribute text.
Definition: ParamNode.cpp:323
bool ToBool() const
Parses the content of this node as a boolean ("true" == true, anything else == false)
Definition: ParamNode.cpp:310
float ToFloat() const
Parses the content of this node as a floating-point number.
Definition: ParamNode.cpp:305
std::map< std::string, CParamNode > ChildrenMap
Definition: ParamNode.h:155
void ApplyLayer(const XMBData &xmb, const XMBElement &element, const wchar_t *sourceIdentifier=NULL)
Overlays the specified data onto this node.
Definition: ParamNode.cpp:87
const CParamNode & GetChild(const char *name) const
Returns the (unique) child node with the given name, or a node with IsOk() == false if there is none.
Definition: ParamNode.cpp:267
void ResetScriptVal()
Definition: ParamNode.cpp:470
ChildrenMap m_Childs
Definition: ParamNode.h:296
const std::string & ToString() const
Returns the content of this node as an UTF8 string.
Definition: ParamNode.cpp:285
static void LoadXML(CParamNode &ret, const XMBData &xmb, const wchar_t *sourceIdentifier=NULL)
Loads the XML data specified by file into the node ret.
Definition: ParamNode.cpp:60
int ToInt() const
Parses the content of this node as an integer.
Definition: ParamNode.cpp:295
bool IsOk() const
Returns true if this is a valid CParamNode, false if it represents a non-existent node.
Definition: ParamNode.cpp:275
const CParamNode & GetOnlyChild() const
Returns the only child node, or a node with IsOk() == false if there is none.
Definition: ParamNode.cpp:258
CParamNode(bool isOk=true)
Constructs a new, empty node.
Definition: ParamNode.cpp:55
fixed ToFixed() const
Parses the content of this node as a fixed-point number.
Definition: ParamNode.cpp:300
std::shared_ptr< JS::PersistentRootedValue > m_ScriptVal
Caches the ToJSVal script representation of this node.
Definition: ParamNode.h:302
void ConstructJSVal(const Script::Request &rq, JS::MutableHandleValue ret) const
Definition: ParamNode.cpp:406
void ToJSVal(const Script::Request &rq, bool cacheValue, JS::MutableHandleValue ret) const
Returns a JS::Value representation of this node and its children.
Definition: ParamNode.cpp:387
std::uint32_t m_Index
Definition: ParamNode.h:277
const std::wstring ToWString() const
Returns the content of this node as a wstring.
Definition: ParamNode.cpp:280
Interned 8-bit strings.
Definition: CStrIntern.h:42
Definition: path.h:84
Definition: Request.h:51
Definition: XMBData.h:95
Definition: XMBData.h:135
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
def xml
Definition: tests.py:138