0ad/source/ps/XML/XeroXMB.cpp

251 lines
5.9 KiB
C++
Raw Normal View History

2004-07-08 17:22:09 +02:00
#include "precompiled.h"
#include "Xeromyces.h"
#include "ps/utf16string.h"
2004-07-08 17:22:09 +02:00
const u32 HeaderMagic = 0x30424D58; // = "XMB0" (little-endian)
2004-07-08 17:22:09 +02:00
const char* HeaderMagicStr = "XMB0";
// Warning: May contain traces of pointer abuse
void XMBFile::Initialise(const char* FileData)
2004-07-08 17:22:09 +02:00
{
m_Pointer = FileData;
u32 Header = *(u32 *)m_Pointer; m_Pointer += 4;
debug_assert(Header == HeaderMagic && "Invalid XMB header!");
2004-07-08 17:22:09 +02:00
int i;
// 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)
2004-07-08 17:22:09 +02:00
#ifdef XERO_USEMAP
// Build a std::map of all the names->ids
u32 ElementNameCount = *(u32*)m_Pointer; m_Pointer += 4;
2004-07-08 17:22:09 +02:00
for (i = 0; i < ElementNameCount; ++i)
m_ElementNames[ReadZStrA()] = i;
2004-07-08 17:22:09 +02:00
u32 AttributeNameCount = *(u32*)m_Pointer; m_Pointer += 4;
2004-07-08 17:22:09 +02:00
for (i = 0; i < AttributeNameCount; ++i)
m_AttributeNames[ReadZStrA()] = i;
2004-07-08 17:22:09 +02:00
#else
// Ignore all the names for now, and skip over them
// (remembering the position of the first)
m_ElementNameCount = *(int*)m_Pointer; m_Pointer += 4;
m_ElementPointer = m_Pointer;
for (i = 0; i < m_ElementNameCount; ++i)
m_Pointer += 4 + *(int*)m_Pointer; // skip over the string
m_AttributeNameCount = *(int*)m_Pointer; m_Pointer += 4;
m_AttributePointer = m_Pointer;
for (i = 0; i < m_AttributeNameCount; ++i)
m_Pointer += 4 + *(int*)m_Pointer; // skip over the string
#endif
}
std::string XMBFile::ReadZStrA()
2004-07-08 17:22:09 +02:00
{
int Length = *(int*)m_Pointer;
m_Pointer += 4;
std::string String (m_Pointer); // reads up until the first NULL
2004-07-08 17:22:09 +02:00
m_Pointer += Length;
return String;
}
XMBElement XMBFile::getRoot() const
2004-07-08 17:22:09 +02:00
{
return XMBElement(m_Pointer);
}
#ifdef XERO_USEMAP
int XMBFile::getElementID(const char* Name) const
2004-07-08 17:22:09 +02:00
{
return m_ElementNames[Name];
}
int XMBFile::getAttributeID(const char* Name) const
2004-07-08 17:22:09 +02:00
{
return m_AttributeNames[Name];
}
#else // #ifdef XERO_USEMAP
int XMBFile::getElementID(const char* Name) const
2004-07-08 17:22:09 +02:00
{
const char* Pos = m_ElementPointer;
2004-07-08 17:22:09 +02:00
int len = (int)strlen(Name)+1; // count bytes, including null terminator
2004-07-08 17:22:09 +02: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
# Housekeeping and pathfinder enhancements / optimization when dealing with ranged actions. - Modified Xeromyces to no longer automatically convert element and attribute names to lowercase, so that we can have camelCase names. We should now be able to convert all the multi-word entity properties, like pass_through_allies, to camelCase, like passThroughAllies, which is more consistent with the rest of our JavaScript naming conventions. To support the existing code that assumes lowercase element names, I made the getElementID and getAttributeID methods (used in the EL and AT macros) ignore case, and I changed any code that directly accessed element names to use the right case. CEntityTemplate now converts Names_LikeThis to names_likeThis (changing each separate "word" in the name to camelCase). Changed the version letter in XMB filenames from A to B to support this without requiring people to delete old XMBs. - Enhanced the pathfinder's handling of contact paths, resulting in a very large speedup for actions like attacking, construction, etc. The problem was that the pathfinder used to not count a given state as the goal unless it was exactly coincident with the target location. This is fine when you order a unit to go exactly to a certain spot, but if you're ordering a unit to build, gather or attack something, then the target tile is impassable (because your target is there) and therefore the pathfinder never declares a state final. As a result, the pathfinder tries hundreds of extra tiles in case there is a long path that gets to the goal, and after failing to find any path that reaches the goal, it gives you one to the closest point it got to. To fix it, I made the pathfinder take into account a radius around the goal in which it's OK to be, which depends on the size of the target unit and the range of your action. This was SVN commit r4186.
2006-08-01 05:41:21 +02:00
if (*(int*)Pos == len && strnicmp(Pos+4, Name, len) == 0)
2004-07-08 17:22:09 +02:00
return i;
// If not, jump to the next string
Pos += 4 + *(int*)Pos;
}
// Failed
return -1;
}
int XMBFile::getAttributeID(const char* Name) const
2004-07-08 17:22:09 +02:00
{
const char* Pos = m_AttributePointer;
2004-07-08 17:22:09 +02:00
int len = (int)strlen(Name)+1; // count bytes, including null terminator
2004-07-08 17:22:09 +02: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
# Housekeeping and pathfinder enhancements / optimization when dealing with ranged actions. - Modified Xeromyces to no longer automatically convert element and attribute names to lowercase, so that we can have camelCase names. We should now be able to convert all the multi-word entity properties, like pass_through_allies, to camelCase, like passThroughAllies, which is more consistent with the rest of our JavaScript naming conventions. To support the existing code that assumes lowercase element names, I made the getElementID and getAttributeID methods (used in the EL and AT macros) ignore case, and I changed any code that directly accessed element names to use the right case. CEntityTemplate now converts Names_LikeThis to names_likeThis (changing each separate "word" in the name to camelCase). Changed the version letter in XMB filenames from A to B to support this without requiring people to delete old XMBs. - Enhanced the pathfinder's handling of contact paths, resulting in a very large speedup for actions like attacking, construction, etc. The problem was that the pathfinder used to not count a given state as the goal unless it was exactly coincident with the target location. This is fine when you order a unit to go exactly to a certain spot, but if you're ordering a unit to build, gather or attack something, then the target tile is impassable (because your target is there) and therefore the pathfinder never declares a state final. As a result, the pathfinder tries hundreds of extra tiles in case there is a long path that gets to the goal, and after failing to find any path that reaches the goal, it gives you one to the closest point it got to. To fix it, I made the pathfinder take into account a radius around the goal in which it's OK to be, which depends on the size of the target unit and the range of your action. This was SVN commit r4186.
2006-08-01 05:41:21 +02:00
if (*(int*)Pos == len && strnicmp(Pos+4, Name, len) == 0)
2004-07-08 17:22:09 +02:00
return i;
// If not, jump to the next string
Pos += 4 + *(int*)Pos;
}
// Failed
return -1;
}
#endif // #ifdef XERO_USEMAP / #else
// Relatively inefficient, so only use when
// laziness overcomes the need for speed
std::string XMBFile::getElementString(const int ID) const
2004-07-08 17:22:09 +02:00
{
const char* Pos = m_ElementPointer;
2004-07-08 17:22:09 +02:00
for (int i = 0; i < ID; ++i)
Pos += 4 + *(int*)Pos;
return std::string(Pos+4);
2004-07-08 17:22:09 +02:00
}
std::string XMBFile::getAttributeString(const int ID) const
2004-07-08 17:22:09 +02:00
{
const char* Pos = m_AttributePointer;
2004-07-08 17:22:09 +02:00
for (int i = 0; i < ID; ++i)
Pos += 4 + *(int*)Pos;
return std::string(Pos+4);
2004-07-08 17:22:09 +02:00
}
int XMBElement::getNodeName() const
2004-07-08 17:22:09 +02:00
{
return *(int*)(m_Pointer + 4); // == ElementName
}
XMBElementList XMBElement::getChildNodes() const
2004-07-08 17:22:09 +02:00
{
return XMBElementList(
m_Pointer + 20 + *(int*)(m_Pointer + 16), // == Children[]
*(int*)(m_Pointer + 12) // == ChildCount
);
}
XMBAttributeList XMBElement::getAttributes() const
2004-07-08 17:22:09 +02:00
{
return XMBAttributeList(
m_Pointer + 24 + *(int*)(m_Pointer + 20), // == Attributes[]
*(int*)(m_Pointer + 8) // == AttributeCount
);
}
utf16string XMBElement::getText() const
2004-07-08 17:22:09 +02:00
{
// Return empty string if there's no text
if (*(int*)(m_Pointer + 20) == 0)
return utf16string();
else
return utf16string((utf16_t*)(m_Pointer + 28));
2004-07-08 17:22:09 +02:00
}
int XMBElement::getLineNumber() const
{
// Make sure there actually was some text to record the line of
if (*(int*)(m_Pointer + 20) == 0)
return -1;
else
return *(int*)(m_Pointer + 24);
}
2004-07-08 17:22:09 +02:00
XMBElement XMBElementList::item(const int id)
{
debug_assert(id >= 0 && id < Count && "Element ID out of range");
const char* Pos;
2004-07-08 17:22:09 +02:00
// If access is sequential, don't bother scanning
// through all the nodes to find the next one
if (id == m_LastItemID+1)
{
Pos = m_LastPointer;
Pos += *(int*)Pos; // skip over the last node
}
else
{
Pos = m_Pointer;
// Skip over each preceding node
for (int i=0; i<id; ++i)
Pos += *(int*)Pos;
}
// Cache information about this node
m_LastItemID = id;
2004-07-08 17:22:09 +02:00
m_LastPointer = Pos;
return XMBElement(Pos);
}
utf16string XMBAttributeList::getNamedItem(const int AttributeName) const
2004-07-08 17:22:09 +02:00
{
const char* Pos = m_Pointer;
2004-07-08 17:22:09 +02:00
// Maybe not the cleverest algorithm, but it should be
// fast enough with half a dozen attributes:
for (int i = 0; i < Count; ++i)
{
if (*(int*)Pos == AttributeName)
return utf16string((utf16_t*)(Pos+8));
2004-07-08 17:22:09 +02:00
Pos += 8 + *(int*)(Pos+4); // Skip over the string
}
// Can't find attribute
return utf16string();
2004-07-08 17:22:09 +02:00
}
XMBAttribute XMBAttributeList::item(const int id)
{
debug_assert(id >= 0 && id < Count && "Attribute ID out of range");
const char* Pos;
2004-07-08 17:22:09 +02:00
// If access is sequential, don't bother scanning through
// all the nodes to find the right one
if (id == m_LastItemID+1)
{
Pos = m_LastPointer;
// Skip over the last attribute
Pos += 8 + *(int*)(Pos+4);
}
else
{
Pos = m_Pointer;
// Skip over each preceding attribute
for (int i=0; i<id; ++i)
Pos += 8 + *(int*)(Pos+4); // skip ID, length, and string data
}
// Cache information about this attribute
m_LastItemID = id;
m_LastPointer = Pos;
return XMBAttribute(*(int*)Pos, utf16string( (const utf16_t*)(Pos+8) ));
2004-07-08 17:22:09 +02:00
}