From f2681777fb21c134cd96ae1e06bf5266a59bfd5d Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Tue, 8 May 2012 11:31:54 +0000 Subject: Fast NBT writer (Saving a chunk is now about twice as fast) git-svn-id: http://mc-server.googlecode.com/svn/trunk@484 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/FastNBT.h | 58 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 9 deletions(-) (limited to 'source/FastNBT.h') diff --git a/source/FastNBT.h b/source/FastNBT.h index fd60fc671..a0fe7a863 100644 --- a/source/FastNBT.h +++ b/source/FastNBT.h @@ -214,19 +214,59 @@ public: void EndList(void); void AddByte (const AString & a_Name, unsigned char a_Value); - void AddShort (const AString & a_Name, unsigned char a_Value); - void AddInt (const AString & a_Name, unsigned char a_Value); - void AddLong (const AString & a_Name, unsigned char a_Value); - void AddFloat (const AString & a_Name, unsigned char a_Value); - void AddDouble (const AString & a_Name, unsigned char a_Value); - void AddString (const AString & a_Name, unsigned char a_Value); - void AddByteArray(const AString & a_Name, unsigned char a_Value); - void AddIntArray (const AString & a_Name, unsigned char a_Value); - + void AddShort (const AString & a_Name, Int16 a_Value); + void AddInt (const AString & a_Name, Int32 a_Value); + void AddLong (const AString & a_Name, Int64 a_Value); + void AddFloat (const AString & a_Name, float a_Value); + void AddDouble (const AString & a_Name, double a_Value); + void AddString (const AString & a_Name, const AString & a_Value); + void AddByteArray(const AString & a_Name, const char * a_Value, size_t a_NumElements); + void AddIntArray (const AString & a_Name, const int * a_Value, size_t a_NumElements); + + void AddByteArray(const AString & a_Name, const AString & a_Value) + { + AddByteArray(a_Name, a_Value.data(), a_Value.size()); + } + const AString & GetResult(void) const {return m_Result; } + void Finish(void); + protected: + + struct sParent + { + int m_Type; // TAG_Compound or TAG_List + int m_Pos; // for TAG_List, the position of the list count + int m_Count; // for TAG_List, the element count + } ; + + static const int MAX_STACK = 50; // Highliy doubtful that an NBT would be constructed this many levels deep + + // These two fields emulate a stack. A raw array is used due to speed issues - no reallocations are allowed. + sParent m_Stack[MAX_STACK]; + int m_CurrentStack; + AString m_Result; + + bool IsStackTopCompound(void) const { return (m_Stack[m_CurrentStack].m_Type == TAG_Compound); } + + void WriteString(const char * a_Data, short a_Length); + + inline void TagCommon(const AString & a_Name, eTagType a_Type) + { + if (IsStackTopCompound()) + { + // Compound: add the type and name: + m_Result.push_back((char)a_Type); + WriteString(a_Name.c_str(), a_Name.length()); + } + else + { + // List: add to the counter + m_Stack[m_CurrentStack].m_Count++; + } + } } ; -- cgit v1.2.3