XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
|
*
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
*
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
*
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
|
*/
|
|
|
|
|
|
|
2004-07-08 08:22:09 -07:00
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
2008-06-15 10:24:32 -07:00
|
|
|
|
#include "lib/byte_order.h" // FOURCC_LE
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
#include "ps/XMB/XMBStorage.h"
|
|
|
|
|
|
#include "ps/XML/Xeromyces.h"
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
2015-01-23 12:39:40 -08:00
|
|
|
|
template<typename T>
|
|
|
|
|
|
static inline T read(const void* ptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
T ret;
|
|
|
|
|
|
memcpy(&ret, ptr, sizeof(T));
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
bool XMBData::Initialise(const XMBStorage& doc)
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
const char* start = reinterpret_cast<const char*>(doc.m_Buffer.get());
|
|
|
|
|
|
m_Pointer = start;
|
2009-03-23 14:53:17 -07:00
|
|
|
|
char Header[5] = { 0 };
|
2008-06-15 10:24:32 -07:00
|
|
|
|
strncpy_s(Header, 5, m_Pointer, 4);
|
|
|
|
|
|
m_Pointer += 4;
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
|
|
|
|
|
|
if (strcmp(Header, XMBStorage::UnfinishedHeaderMagicStr) == 0)
|
2008-06-15 10:24:32 -07:00
|
|
|
|
return false;
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
ENSURE(strcmp(Header, XMBStorage::HeaderMagicStr) == 0 && "Invalid XMB header!");
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
2015-01-23 12:39:40 -08:00
|
|
|
|
u32 Version = read<u32>(m_Pointer);
|
2014-06-14 16:41:33 -07:00
|
|
|
|
m_Pointer += 4;
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
if (Version != XMBStorage::XMBVersion)
|
2014-06-14 16:41:33 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
|
2006-01-29 10:23:47 -08:00
|
|
|
|
// FIXME Check that m_Pointer doesn't end up past the end of the buffer
|
|
|
|
|
|
// (it shouldn't be all that dangerous since we're only doing read-only
|
|
|
|
|
|
// access, but it might crash on an invalid file, reading a couple of
|
|
|
|
|
|
// billion random element names from RAM)
|
|
|
|
|
|
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
m_ElementPointer = start + read<u32>(m_Pointer); m_Pointer += 4;
|
2015-01-23 12:39:40 -08:00
|
|
|
|
m_ElementNameCount = read<int>(m_Pointer); m_Pointer += 4;
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
m_AttributePointer = start + read<u32>(m_Pointer); m_Pointer += 4;
|
2015-01-23 12:39:40 -08:00
|
|
|
|
m_AttributeNameCount = read<int>(m_Pointer); m_Pointer += 4;
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
// At this point m_Pointer points to the element start, as expected.
|
2008-06-15 10:24:32 -07:00
|
|
|
|
return true; // success
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
XMBElement XMBData::GetRoot() const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
|
|
|
|
|
return XMBElement(m_Pointer);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
int XMBData::GetElementID(const char* Name) const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2006-01-29 10:23:47 -08:00
|
|
|
|
const char* Pos = m_ElementPointer;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
2004-07-10 13:33:42 -07:00
|
|
|
|
int len = (int)strlen(Name)+1; // count bytes, including null terminator
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
|
|
|
|
|
// Loop through each string to find a match
|
|
|
|
|
|
for (int i = 0; i < m_ElementNameCount; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
// See if this could be the right string, checking its
|
|
|
|
|
|
// length and then its contents
|
2015-01-23 12:39:40 -08:00
|
|
|
|
if (read<int>(Pos) == len && strncasecmp(Pos+4, Name, len) == 0)
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
return static_cast<int>(Pos - m_ElementPointer);
|
2004-07-08 08:22:09 -07:00
|
|
|
|
// If not, jump to the next string
|
2015-01-23 12:39:40 -08:00
|
|
|
|
Pos += 4 + read<int>(Pos);
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
// Failed
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
int XMBData::GetAttributeID(const char* Name) const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2006-01-29 10:23:47 -08:00
|
|
|
|
const char* Pos = m_AttributePointer;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
2004-07-10 13:33:42 -07:00
|
|
|
|
int len = (int)strlen(Name)+1; // count bytes, including null terminator
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
|
|
|
|
|
// Loop through each string to find a match
|
|
|
|
|
|
for (int i = 0; i < m_AttributeNameCount; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
// See if this could be the right string, checking its
|
|
|
|
|
|
// length and then its contents
|
2015-01-23 12:39:40 -08:00
|
|
|
|
if (read<int>(Pos) == len && strncasecmp(Pos+4, Name, len) == 0)
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
return static_cast<int>(Pos - m_AttributePointer);
|
2004-07-08 08:22:09 -07:00
|
|
|
|
// If not, jump to the next string
|
2015-01-23 12:39:40 -08:00
|
|
|
|
Pos += 4 + read<int>(Pos);
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
// Failed
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
const char* XMBData::GetElementString(const int ID) const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
return reinterpret_cast<const char*>(m_ElementPointer + ID + 4);
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
const char* XMBData::GetAttributeString(const int ID) const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
return reinterpret_cast<const char*>(m_AttributePointer + ID + 4);
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
std::string_view XMBData::GetElementStringView(const int ID) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return std::string_view(reinterpret_cast<const char*>(m_ElementPointer + ID + 4), read<int>(m_ElementPointer + ID) - 1);
|
|
|
|
|
|
}
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 06:02:34 -07:00
|
|
|
|
std::string_view XMBData::GetAttributeStringView(const int ID) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return std::string_view(reinterpret_cast<const char*>(m_AttributePointer + ID + 4), read<int>(m_AttributePointer + ID) - 1);
|
|
|
|
|
|
}
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
|
int XMBElement::GetNodeName() const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
|
if (m_Pointer == NULL)
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
2015-01-23 12:39:40 -08:00
|
|
|
|
return read<int>(m_Pointer + 4); // == ElementName
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
|
XMBElementList XMBElement::GetChildNodes() const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
|
if (m_Pointer == NULL)
|
2015-05-31 17:29:35 -07:00
|
|
|
|
return XMBElementList(NULL, 0, NULL);
|
2010-01-22 12:03:14 -08:00
|
|
|
|
|
2004-07-08 08:22:09 -07:00
|
|
|
|
return XMBElementList(
|
2015-01-23 12:39:40 -08:00
|
|
|
|
m_Pointer + 20 + read<int>(m_Pointer + 16), // == Children[]
|
2015-05-31 17:29:35 -07:00
|
|
|
|
read<int>(m_Pointer + 12), // == ChildCount
|
|
|
|
|
|
m_Pointer + read<int>(m_Pointer) // == &Children[ChildCount]
|
2004-07-08 08:22:09 -07:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
|
XMBAttributeList XMBElement::GetAttributes() const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
|
if (m_Pointer == NULL)
|
2015-05-31 17:29:35 -07:00
|
|
|
|
return XMBAttributeList(NULL, 0, NULL);
|
2010-01-22 12:03:14 -08:00
|
|
|
|
|
2004-07-08 08:22:09 -07:00
|
|
|
|
return XMBAttributeList(
|
2015-01-23 12:39:40 -08:00
|
|
|
|
m_Pointer + 24 + read<int>(m_Pointer + 20), // == Attributes[]
|
2015-05-31 17:29:35 -07:00
|
|
|
|
read<int>(m_Pointer + 8), // == AttributeCount
|
|
|
|
|
|
m_Pointer + 20 + read<int>(m_Pointer + 16) // == &Attributes[AttributeCount] ( == &Children[])
|
2004-07-08 08:22:09 -07:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-02-17 12:08:20 -08:00
|
|
|
|
CStr8 XMBElement::GetText() const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2004-07-12 08:50:12 -07:00
|
|
|
|
// Return empty string if there's no text
|
2015-01-23 12:39:40 -08:00
|
|
|
|
if (m_Pointer == NULL || read<int>(m_Pointer + 20) == 0)
|
2011-02-17 12:08:20 -08:00
|
|
|
|
return CStr8();
|
|
|
|
|
|
|
2014-06-07 20:23:37 -07:00
|
|
|
|
return CStr8(m_Pointer + 28);
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
|
int XMBElement::GetLineNumber() const
|
2004-07-12 08:50:12 -07:00
|
|
|
|
{
|
2004-10-07 13:49:35 -07:00
|
|
|
|
// Make sure there actually was some text to record the line of
|
2015-01-23 12:39:40 -08:00
|
|
|
|
if (m_Pointer == NULL || read<int>(m_Pointer + 20) == 0)
|
2004-07-12 08:50:12 -07:00
|
|
|
|
return -1;
|
|
|
|
|
|
else
|
2015-01-23 12:39:40 -08:00
|
|
|
|
return read<int>(m_Pointer + 24);
|
2004-07-12 08:50:12 -07:00
|
|
|
|
}
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
|
XMBElement XMBElementList::GetFirstNamedItem(const int ElementName) const
|
|
|
|
|
|
{
|
|
|
|
|
|
const char* Pos = m_Pointer;
|
|
|
|
|
|
|
|
|
|
|
|
// Maybe not the cleverest algorithm, but it should be
|
|
|
|
|
|
// fast enough with half a dozen attributes:
|
2015-05-31 17:29:35 -07:00
|
|
|
|
for (size_t i = 0; i < m_Size; ++i)
|
2010-01-22 12:03:14 -08:00
|
|
|
|
{
|
2015-01-23 12:39:40 -08:00
|
|
|
|
int Length = read<int>(Pos);
|
|
|
|
|
|
int Name = read<int>(Pos+4);
|
2010-01-22 12:03:14 -08:00
|
|
|
|
if (Name == ElementName)
|
|
|
|
|
|
return XMBElement(Pos);
|
|
|
|
|
|
Pos += Length;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Can't find element
|
|
|
|
|
|
return XMBElement();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-05-31 17:29:35 -07:00
|
|
|
|
XMBElementList::iterator& XMBElementList::iterator::operator++()
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2015-05-31 17:29:35 -07:00
|
|
|
|
m_CurPointer += read<int>(m_CurPointer);
|
|
|
|
|
|
++m_CurItemID;
|
|
|
|
|
|
return (*this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
XMBElement XMBElementList::operator[](size_t id)
|
|
|
|
|
|
{
|
|
|
|
|
|
ENSURE(id < m_Size && "Element ID out of range");
|
2006-01-29 10:23:47 -08:00
|
|
|
|
const char* Pos;
|
2015-05-31 17:29:35 -07:00
|
|
|
|
size_t i;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
2015-05-31 17:29:35 -07:00
|
|
|
|
if (id < m_CurItemID)
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2015-05-31 17:29:35 -07:00
|
|
|
|
Pos = m_Pointer;
|
|
|
|
|
|
i = 0;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2015-05-31 17:29:35 -07:00
|
|
|
|
// If access is sequential, don't bother scanning
|
|
|
|
|
|
// through all the nodes to find the next one
|
|
|
|
|
|
Pos = m_CurPointer;
|
|
|
|
|
|
i = m_CurItemID;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
2015-05-31 17:29:35 -07:00
|
|
|
|
|
|
|
|
|
|
// Skip over each preceding node
|
|
|
|
|
|
for (; i < id; ++i)
|
|
|
|
|
|
Pos += read<int>(Pos);
|
|
|
|
|
|
|
2004-07-08 08:22:09 -07:00
|
|
|
|
// Cache information about this node
|
2015-05-31 17:29:35 -07:00
|
|
|
|
m_CurItemID = id;
|
|
|
|
|
|
m_CurPointer = Pos;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
|
|
|
|
|
return XMBElement(Pos);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-02-17 12:08:20 -08:00
|
|
|
|
CStr8 XMBAttributeList::GetNamedItem(const int AttributeName) const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2006-01-29 10:23:47 -08:00
|
|
|
|
const char* Pos = m_Pointer;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
|
|
|
|
|
// Maybe not the cleverest algorithm, but it should be
|
|
|
|
|
|
// fast enough with half a dozen attributes:
|
2015-05-31 17:29:35 -07:00
|
|
|
|
for (size_t i = 0; i < m_Size; ++i)
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2015-01-23 12:39:40 -08:00
|
|
|
|
if (read<int>(Pos) == AttributeName)
|
2014-06-07 20:23:37 -07:00
|
|
|
|
return CStr8(Pos+8);
|
2015-01-23 12:39:40 -08:00
|
|
|
|
Pos += 8 + read<int>(Pos+4); // Skip over the string
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Can't find attribute
|
2011-02-17 12:08:20 -08:00
|
|
|
|
return CStr8();
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-05-31 17:29:35 -07:00
|
|
|
|
XMBAttribute XMBAttributeList::iterator::operator*() const
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2015-05-31 17:29:35 -07:00
|
|
|
|
return XMBAttribute(read<int>(m_CurPointer), CStr8(m_CurPointer+8));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
XMBAttributeList::iterator& XMBAttributeList::iterator::operator++()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_CurPointer += 8 + read<int>(m_CurPointer+4); // skip ID, length, and string data
|
|
|
|
|
|
++m_CurItemID;
|
|
|
|
|
|
return (*this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
XMBAttribute XMBAttributeList::operator[](size_t id)
|
|
|
|
|
|
{
|
|
|
|
|
|
ENSURE(id < m_Size && "Attribute ID out of range");
|
2006-01-29 10:23:47 -08:00
|
|
|
|
const char* Pos;
|
2015-05-31 17:29:35 -07:00
|
|
|
|
size_t i;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
2015-05-31 17:29:35 -07:00
|
|
|
|
if (id < m_CurItemID)
|
2004-07-08 08:22:09 -07:00
|
|
|
|
{
|
2015-05-31 17:29:35 -07:00
|
|
|
|
Pos = m_Pointer;
|
|
|
|
|
|
i = 0;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2015-05-31 17:29:35 -07:00
|
|
|
|
// If access is sequential, don't bother scanning
|
|
|
|
|
|
// through all the nodes to find the next one
|
|
|
|
|
|
Pos = m_CurPointer;
|
|
|
|
|
|
i = m_CurItemID;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|
2015-05-31 17:29:35 -07:00
|
|
|
|
|
|
|
|
|
|
// Skip over each preceding attribute
|
|
|
|
|
|
for (; i < id; ++i)
|
|
|
|
|
|
Pos += 8 + read<int>(Pos+4); // skip ID, length, and string data
|
|
|
|
|
|
|
2004-07-08 08:22:09 -07:00
|
|
|
|
// Cache information about this attribute
|
2015-05-31 17:29:35 -07:00
|
|
|
|
m_CurItemID = id;
|
|
|
|
|
|
m_CurPointer = Pos;
|
2004-07-08 08:22:09 -07:00
|
|
|
|
|
2015-01-23 12:39:40 -08:00
|
|
|
|
return XMBAttribute(read<int>(Pos), CStr8(Pos+8));
|
2004-07-08 08:22:09 -07:00
|
|
|
|
}
|