Pyrogenesis  trunk
XMBStorage.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 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_XMBSTORAGE
19 #define INCLUDED_XMBSTORAGE
20 
22 
23 #include <memory>
24 
25 typedef struct _xmlDoc xmlDoc;
26 typedef xmlDoc* xmlDocPtr;
27 
28 struct IVFS;
29 typedef std::shared_ptr<IVFS> PIVFS;
30 
31 class Path;
32 typedef Path VfsPath;
33 
34 /**
35  * Storage for XMBData
36  */
38 {
39 public:
40  // File headers, to make sure it doesn't try loading anything other than an XMB
41  static const char* HeaderMagicStr;
42  static const char* UnfinishedHeaderMagicStr;
43  static const u32 XMBVersion;
44 
45  XMBStorage() = default;
46 
47  /**
48  * Read an XMB file on disk.
49  */
50  bool ReadFromFile(const PIVFS& vfs, const VfsPath& filename);
51 
52  /**
53  * Parse an XML document into XMB.
54  *
55  * Main limitations:
56  * - Can't correctly handle mixed text/elements inside elements -
57  * "<div> <b> Text </b> </div>" and "<div> Te<b/>xt </div>" are
58  * considered identical.
59  */
60  bool LoadXMLDoc(const xmlDocPtr doc);
61 
62  /**
63  * Parse a Javascript value into XMB.
64  * The syntax is similar to ParamNode, but supports multiple children with the same name, to match XML.
65  * You need to pass the name of the root object, as unlike XML this cannot be recovered from the value.
66  * The following JS object:
67  * {
68  * "a": 5,
69  * "b": "test",
70  * "x": {
71  * // Like ParamNode, _string is used for the value.
72  * "_string": "value",
73  * // Like ParamNode, attributes are prefixed with @.
74  * "@param": "something",
75  * "y": 3
76  * },
77  * // Every array item is parsed as a child.
78  * "object": [
79  * "a",
80  * "b",
81  * { "_string": "c" },
82  * { "child": "value" },
83  * ],
84  * // Same but without the array.
85  * "child@0@": 1,
86  * "child@1@": 2
87  * }
88  * will parse like the following XML:
89  * <a>5
90  * <b>test</b>
91  * <x param="something">value
92  * <y>3</y>
93  * </x>
94  * <object>a</object>
95  * <object>b</object>
96  * <object>c</object>
97  * <object><child>value</child></object>
98  * <child>1</child>
99  * <child>2</child>
100  * </a>
101  * See also tests for some other examples.
102  */
103  bool LoadJSValue(const ScriptInterface& scriptInterface, JS::HandleValue value, const std::string& rootName);
104 
105  std::shared_ptr<u8> m_Buffer;
106  size_t m_Size = 0;
107 };
108 
109 
110 #endif // INCLUDED_XMBSTORAGE
Path VfsPath
Definition: XMBStorage.h:31
std::shared_ptr< u8 > m_Buffer
Definition: XMBStorage.h:105
static const char * UnfinishedHeaderMagicStr
Definition: XMBStorage.h:42
xmlDoc * xmlDocPtr
Definition: XMBStorage.h:26
Storage for XMBData.
Definition: XMBStorage.h:37
std::shared_ptr< IVFS > PIVFS
Definition: vfs.h:220
XMBStorage()=default
uint32_t u32
Definition: types.h:39
Definition: path.h:79
bool LoadJSValue(const ScriptInterface &scriptInterface, JS::HandleValue value, const std::string &rootName)
Parse a Javascript value into XMB.
Definition: XMBStorage.cpp:463
static const char * HeaderMagicStr
Definition: XMBStorage.h:41
static const u32 XMBVersion
Definition: XMBStorage.h:43
size_t m_Size
Definition: XMBStorage.h:106
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:71
std::shared_ptr< IVFS > PIVFS
Definition: XMBStorage.h:28
Definition: vfs.h:76
Definition: vfs_util.cpp:39
struct _xmlDoc xmlDoc
Definition: XMBStorage.h:25
bool LoadXMLDoc(const xmlDocPtr doc)
Parse an XML document into XMB.
Definition: XMBStorage.cpp:450
bool ReadFromFile(const PIVFS &vfs, const VfsPath &filename)
Read an XMB file on disk.
Definition: XMBStorage.cpp:437