Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Xeromyces.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/*
19 Xeromyces file-loading interface.
20 Automatically creates and caches relatively
21 efficient binary representations of XML files.
22*/
23
24#ifndef INCLUDED_XEROMYCES
25#define INCLUDED_XEROMYCES
26
27#include "ps/Errors.h"
28ERROR_GROUP(Xeromyces);
29ERROR_TYPE(Xeromyces, XMLOpenFailed);
30ERROR_TYPE(Xeromyces, XMLParseError);
31ERROR_TYPE(Xeromyces, XMLValidationFailed);
32
33#include "ps/XMB/XMBData.h"
34#include "ps/XMB/XMBStorage.h"
35
36#include "lib/file/vfs/vfs.h"
37
39
40class CXeromyces : public XMBData
41{
42 friend class TestXMBData;
43 friend class XMBData;
44public:
45 /**
46 * Load from an XML file (with invisible XMB caching).
47 */
48 PSRETURN Load(const PIVFS& vfs, const VfsPath& filename, const std::string& validatorName = "");
49
50 /**
51 * Load from an in-memory XML string (with no caching).
52 */
53 PSRETURN LoadString(const char* xml, const std::string& validatorName = "");
54
55 /**
56 * Convert the given XML file into an XMB in the archive cache.
57 * Returns the XMB path in @p archiveCachePath.
58 * Returns false on error.
59 */
60 bool GenerateCachedXMB(const PIVFS& vfs, const VfsPath& sourcePath, VfsPath& archiveCachePath, const std::string& validatorName = "");
61
62 /**
63 * Call once when initialising the program, to load libxml2.
64 * This should be run in the main thread, before any thread uses libxml2.
65 */
66 static void Startup();
67
68 /**
69 * Call once when shutting down the program, to unload libxml2.
70 */
71 static void Terminate();
72
73 static bool AddValidator(const PIVFS& vfs, const std::string& name, const VfsPath& grammarPath);
74
75 static bool ValidateEncoded(const std::string& name, const std::string& filename, const std::string& document);
76
77private:
78 static RelaxNGValidator& GetValidator(const std::string& name);
79
80 PSRETURN ConvertFile(const PIVFS& vfs, const VfsPath& filename, const VfsPath& xmbPath, const std::string& validatorName);
81
83};
84
85
86#define _XERO_MAKE_UID2__(p,l) p ## l
87#define _XERO_MAKE_UID1__(p,l) _XERO_MAKE_UID2__(p,l)
88
89#define _XERO_CHILDREN _XERO_MAKE_UID1__(_children_, __LINE__)
90#define _XERO_I _XERO_MAKE_UID1__(_i_, __LINE__)
91
92#define XERO_ITER_EL(parent_element, child_element) \
93 for (XMBElement child_element : parent_element.GetChildNodes())
94
95#define XERO_ITER_ATTR(parent_element, attribute) \
96 for (XMBAttribute attribute : parent_element.GetAttributes())
97
98#endif // INCLUDED_XEROMYCES
u32 PSRETURN
Definition: Errors.h:75
ERROR_TYPE(Xeromyces, XMLOpenFailed)
ERROR_GROUP(Xeromyces)
Definition: Xeromyces.h:41
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename, const std::string &validatorName="")
Load from an XML file (with invisible XMB caching).
Definition: Xeromyces.cpp:114
PSRETURN LoadString(const char *xml, const std::string &validatorName="")
Load from an in-memory XML string (with no caching).
Definition: Xeromyces.cpp:206
PSRETURN ConvertFile(const PIVFS &vfs, const VfsPath &filename, const VfsPath &xmbPath, const std::string &validatorName)
Definition: Xeromyces.cpp:165
XMBStorage m_Data
Definition: Xeromyces.h:82
static void Terminate()
Call once when shutting down the program, to unload libxml2.
Definition: Xeromyces.cpp:67
static RelaxNGValidator & GetValidator(const std::string &name)
NOTE: Callers MUST acquire the g_ValidatorCacheLock before calling this.
Definition: Xeromyces.cpp:107
bool GenerateCachedXMB(const PIVFS &vfs, const VfsPath &sourcePath, VfsPath &archiveCachePath, const std::string &validatorName="")
Convert the given XML file into an XMB in the archive cache.
Definition: Xeromyces.cpp:156
static void Startup()
Call once when initialising the program, to load libxml2.
Definition: Xeromyces.cpp:57
static bool AddValidator(const PIVFS &vfs, const std::string &name, const VfsPath &grammarPath)
Definition: Xeromyces.cpp:78
friend class TestXMBData
Definition: Xeromyces.h:42
static bool ValidateEncoded(const std::string &name, const std::string &filename, const std::string &document)
Definition: Xeromyces.cpp:98
Definition: path.h:80
Definition: RelaxNG.h:30
Definition: XMBData.h:96
Storage for XMBData.
Definition: XMBStorage.h:38
def xml
Definition: tests.py:138
Definition: vfs_util.cpp:39
std::shared_ptr< IVFS > PIVFS
Definition: vfs.h:220