Changeset View
Changeset View
Standalone View
Standalone View
source/ps/XMB/XMBData.h
- This file was moved from source/ps/XML/XeroXMB.h.
/* Copyright (C) 2020 Wildfire Games. | /* Copyright (C) 2021 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 13 Lines | |||||
XMB is a binary representation of XML, with some limitations | XMB is a binary representation of XML, with some limitations | ||||
but much more efficiency (particularly for loading simple data | but much more efficiency (particularly for loading simple data | ||||
classes that don't need much initialisation). | classes that don't need much initialisation). | ||||
Main limitations: | Main limitations: | ||||
* Can't correctly handle mixed text/elements inside elements - | * Can't correctly handle mixed text/elements inside elements - | ||||
"<div> <b> Text </b> </div>" and "<div> Te<b/>xt </div>" are | "<div> <b> Text </b> </div>" and "<div> Te<b/>xt </div>" are | ||||
considered identical. | considered identical. | ||||
* Tries to avoid using strings - you usually have to load the | |||||
numeric IDs and use them instead. | |||||
Theoretical file structure: | Theoretical file structure: | ||||
XMB_File { | XMB_File { | ||||
char Header[4]; // because everyone has one; currently "XMB0" | char Header[4]; // because everyone has one; currently "XMB0" | ||||
u32 Version; | u32 Version; | ||||
int OffsetFromStartToElementNames; | |||||
int ElementNameCount; | int ElementNameCount; | ||||
ZStr8 ElementNames[]; | |||||
int OffsetFromStartToAttributeNames; | |||||
int AttributeNameCount; | int AttributeNameCount; | ||||
ZStr8 AttributeNames[]; | |||||
XMB_Node Root; | XMB_Node Root; | ||||
ZStr8 ElementNames[]; | |||||
ZStr8 AttributeNames[]; | |||||
} | } | ||||
XMB_Node { | XMB_Node { | ||||
0) int Length; // of entire struct, so it can be skipped over | 0) int Length; // of entire struct, so it can be skipped over | ||||
4) int ElementName; | 4) int ElementName; | ||||
8) int AttributeCount; | 8) int AttributeCount; | ||||
Show All 23 Lines | |||||
28) char* Text; // null-terminated UTF8 | 28) char* Text; // null-terminated UTF8 | ||||
} | } | ||||
*/ | */ | ||||
#ifndef INCLUDED_XEROXMB | #ifndef INCLUDED_XEROXMB | ||||
#define INCLUDED_XEROXMB | #define INCLUDED_XEROXMB | ||||
// Define to use a std::map for name lookups rather than a linear search. | #include "ps/CStr.h" | ||||
// (The map is usually slower.) | |||||
//#define XERO_USEMAP | |||||
wraitii: Removed this because the new way is necessarily faster | |||||
#include <string> | #include <string> | ||||
#include <string_view> | |||||
#ifdef XERO_USEMAP | class CXeromyces; | ||||
# include <map> | class XMBStorage; | ||||
#endif | |||||
#include "ps/CStr.h" | |||||
// File headers, to make sure it doesn't try loading anything other than an XMB | |||||
extern const char* HeaderMagicStr; | |||||
extern const char* UnfinishedHeaderMagicStr; | |||||
extern const u32 XMBVersion; | |||||
class XMBElement; | class XMBElement; | ||||
class XMBElementList; | class XMBElementList; | ||||
class XMBAttributeList; | class XMBAttributeList; | ||||
class XMBFile | class XMBData | ||||
{ | { | ||||
public: | public: | ||||
XMBFile() : m_Pointer(NULL) {} | XMBData() : m_Pointer(nullptr) {} | ||||
// Initialise from the contents of an XMB file. | /* | ||||
// FileData must remain allocated and unchanged while | * Initialise from the contents of an XMBStorage. | ||||
// the XMBFile is being used. | * @param doc must remain allocated and unchanged while | ||||
// @return indication of success; main cause for failure is attempting to | * the XMBData is being used. | ||||
// load a partially valid XMB file (e.g. if the game was interrupted | * @return indication of success; main cause for failure is attempting to | ||||
// while writing it), which we detect by checking the magic string. | * load a partially valid XMB file (e.g. if the game was interrupted | ||||
// It also fails when trying to load an XMB file with a different version. | * while writing it), which we detect by checking the magic string. | ||||
bool Initialise(const char* FileData); | * It also fails when trying to load an XMB file with a different version. | ||||
*/ | |||||
bool Initialise(const XMBStorage& doc); | |||||
// Returns the root element | // Returns the root element | ||||
XMBElement GetRoot() const; | XMBElement GetRoot() const; | ||||
// Returns internal ID for a given element/attribute string. | // Returns internal ID for a given element/attribute string. | ||||
int GetElementID(const char* Name) const; | int GetElementID(const char* Name) const; | ||||
int GetAttributeID(const char* Name) const; | int GetAttributeID(const char* Name) const; | ||||
// For lazy people (e.g. me) when speed isn't vital: | // Returns element/attribute string for a given internal ID. | ||||
const char* GetElementString(const int ID) const; | |||||
const char* GetAttributeString(const int ID) const; | |||||
// Returns element/attribute string for a given internal ID | std::string_view GetElementStringView(const int ID) const; | ||||
std::string GetElementString(const int ID) const; | std::string_view GetAttributeStringView(const int ID) const; | ||||
std::string GetAttributeString(const int ID) const; | |||||
private: | private: | ||||
const char* m_Pointer; | const char* m_Pointer; | ||||
#ifdef XERO_USEMAP | |||||
std::map<std::string, int> m_ElementNames; | |||||
std::map<std::string, int> m_AttributeNames; | |||||
#else | |||||
int m_ElementNameCount; | int m_ElementNameCount; | ||||
int m_AttributeNameCount; | int m_AttributeNameCount; | ||||
const char* m_ElementPointer; | const char* m_ElementPointer; | ||||
const char* m_AttributePointer; | const char* m_AttributePointer; | ||||
#endif | |||||
std::string ReadZStr8(); | |||||
}; | }; | ||||
class XMBElement | class XMBElement | ||||
{ | { | ||||
public: | public: | ||||
XMBElement() | XMBElement() | ||||
: m_Pointer(0) {} | : m_Pointer(0) {} | ||||
▲ Show 20 Lines • Show All 144 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Removed this because the new way is necessarily faster