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