Pyrogenesis  trunk
Xeromyces.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 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"
28 ERROR_GROUP(Xeromyces);
29 ERROR_TYPE(Xeromyces, XMLOpenFailed);
30 ERROR_TYPE(Xeromyces, XMLParseError);
31 
32 #include "XeroXMB.h"
33 
34 #include "lib/file/vfs/vfs.h"
35 
36 class RelaxNGValidator;
37 class WriteBuffer;
38 
39 typedef struct _xmlDoc xmlDoc;
40 typedef xmlDoc* xmlDocPtr;
41 
42 class CXeromyces : public XMBFile
43 {
44  friend class TestXeroXMB;
45 public:
46  /**
47  * Load from an XML file (with invisible XMB caching).
48  */
49  PSRETURN Load(const PIVFS& vfs, const VfsPath& filename, const std::string& validatorName = "");
50 
51  /**
52  * Load from an in-memory XML string (with no caching).
53  */
54  PSRETURN LoadString(const char* xml, const std::string& validatorName = "");
55 
56  /**
57  * Convert the given XML file into an XMB in the archive cache.
58  * Returns the XMB path in @p archiveCachePath.
59  * Returns false on error.
60  */
61  bool GenerateCachedXMB(const PIVFS& vfs, const VfsPath& sourcePath, VfsPath& archiveCachePath, const std::string& validatorName = "");
62 
63  /**
64  * Call once when initialising the program, to load libxml2.
65  * This should be run in the main thread, before any thread uses libxml2.
66  */
67  static void Startup();
68 
69  /**
70  * Call once when shutting down the program, to unload libxml2.
71  */
72  static void Terminate();
73 
74  static bool AddValidator(const PIVFS& vfs, const std::string& name, const VfsPath& grammarPath);
75 
76  static bool ValidateEncoded(const std::string& name, const std::wstring& filename, const std::string& document);
77 
78 private:
79  static RelaxNGValidator& GetValidator(const std::string& name);
80 
81  PSRETURN ConvertFile(const PIVFS& vfs, const VfsPath& filename, const VfsPath& xmbPath, const std::string& validatorName);
82 
83  bool ReadXMBFile(const PIVFS& vfs, const VfsPath& filename);
84 
85  static PSRETURN CreateXMB(const xmlDocPtr doc, WriteBuffer& writeBuffer);
86 
87  shared_ptr<u8> m_XMBBuffer;
88 };
89 
90 
91 #define _XERO_MAKE_UID2__(p,l) p ## l
92 #define _XERO_MAKE_UID1__(p,l) _XERO_MAKE_UID2__(p,l)
93 
94 #define _XERO_CHILDREN _XERO_MAKE_UID1__(_children_, __LINE__)
95 #define _XERO_I _XERO_MAKE_UID1__(_i_, __LINE__)
96 
97 #define XERO_ITER_EL(parent_element, child_element) \
98  for (XMBElement child_element : parent_element.GetChildNodes())
99 
100 #define XERO_ITER_ATTR(parent_element, attribute) \
101  for (XMBAttribute attribute : parent_element.GetAttributes())
102 
103 #endif // INCLUDED_XEROMYCES
PSRETURN ConvertFile(const PIVFS &vfs, const VfsPath &filename, const VfsPath &xmbPath, const std::string &validatorName)
Definition: Xeromyces.cpp:159
xml
Definition: tests.py:119
shared_ptr< IVFS > PIVFS
Definition: vfs.h:226
xmlDoc * xmlDocPtr
Definition: Xeromyces.h:40
static bool AddValidator(const PIVFS &vfs, const std::string &name, const VfsPath &grammarPath)
Definition: Xeromyces.cpp:74
static bool ValidateEncoded(const std::string &name, const std::wstring &filename, const std::string &document)
Definition: Xeromyces.cpp:94
friend class TestXeroXMB
Definition: Xeromyces.h:44
bool ReadXMBFile(const PIVFS &vfs, const VfsPath &filename)
Definition: Xeromyces.cpp:201
u32 PSRETURN
Definition: Errors.h:75
Definition: path.h:77
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename, const std::string &validatorName="")
Load from an XML file (with invisible XMB caching).
Definition: Xeromyces.cpp:110
static void Terminate()
Call once when shutting down the program, to unload libxml2.
Definition: Xeromyces.cpp:63
static RelaxNGValidator & GetValidator(const std::string &name)
NOTE: Callers MUST acquire the g_ValidatorCacheLock before calling this.
Definition: Xeromyces.cpp:103
struct _xmlDoc xmlDoc
Definition: RelaxNG.h:26
shared_ptr< u8 > m_XMBBuffer
Definition: Xeromyces.h:87
Definition: XeroXMB.h:115
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:150
Definition: RelaxNG.h:31
Definition: Xeromyces.h:42
ERROR_TYPE(Xeromyces, XMLOpenFailed)
static void Startup()
Call once when initialising the program, to load libxml2.
Definition: Xeromyces.cpp:53
Definition: vfs_util.cpp:39
Definition: write_buffer.h:28
ERROR_GROUP(Xeromyces)
xmlDoc * xmlDocPtr
Definition: RelaxNG.h:27
PSRETURN LoadString(const char *xml, const std::string &validatorName="")
Load from an in-memory XML string (with no caching).
Definition: Xeromyces.cpp:220
static PSRETURN CreateXMB(const xmlDocPtr doc, WriteBuffer &writeBuffer)
Definition: Xeromyces.cpp:373