diff --git a/common/datastore/CDataStore.cpp b/common/datastore/CDataStore.cpp index 96a5126..72d96e2 100644 --- a/common/datastore/CDataStore.cpp +++ b/common/datastore/CDataStore.cpp @@ -5,12 +5,36 @@ #include #include +CDataStore::CDataStore() { + this->m_data = nullptr; + this->m_base = 0; + this->m_alloc = 0; + this->m_size = 0; + this->m_read = -1; +} + +CDataStore::CDataStore(uint8_t* data, uint32_t size) { + this->m_data = data; + this->m_base = 0; + this->m_alloc = -1; + this->m_size = size; + this->m_read = 0; +} + +CDataStore::CDataStore(uint8_t* data, uint32_t size, uint32_t alloc) { + this->m_data = data; + this->m_base = 0; + this->m_alloc = alloc; + this->m_size = size; + this->m_read = -1; +} + CDataStore::~CDataStore() { this->Destroy(); } void CDataStore::Destroy() { - if (this->m_alloc != -1) { + if (!this->IsReadOnly()) { this->InternalDestroy(this->m_data, this->m_base, this->m_alloc); } } @@ -133,6 +157,44 @@ CDataStore& CDataStore::Get(float& val) { return *this; } +CDataStore& CDataStore::GetArray(uint8_t* val, uint32_t count) { + STORM_ASSERT(val || !count); // TODO this is a validation macro + STORM_ASSERT(this->IsFinal()); + + if (this->IsValid()) { + auto remaining = count; + + while (remaining > 0) { + uint32_t available = this->m_size - this->m_read; + uint32_t toRead = (available < remaining) ? available : remaining; + + if (toRead >= this->m_alloc) { + toRead = this->m_alloc; + } + + if (toRead <= 1) { + toRead = 1; + } + + if (!this->FetchRead(this->m_read, toRead)) { + break; + } + + auto src = this->m_data + (this->m_read - this->m_base); + + if (val != src) { + memcpy(val, src, toRead); + } + + remaining -= toRead; + val += toRead; + this->m_read += toRead; + } + } + + return *this; +} + void CDataStore::GetBufferParams(const void** data, uint32_t* size, uint32_t* alloc) const { if (data) { *data = this->m_data; @@ -188,6 +250,12 @@ CDataStore& CDataStore::GetString(char* val, uint32_t maxChars) { return *this; } +void CDataStore::Initialize() { + if (!this->IsReadOnly()) { + this->InternalInitialize(this->m_data, this->m_base, this->m_alloc); + } +} + void CDataStore::InternalDestroy(uint8_t*& data, uint32_t& base, uint32_t& alloc) { if (alloc && data) { SMemFree(data, __FILE__, __LINE__, 0); @@ -215,10 +283,18 @@ int32_t CDataStore::InternalFetchWrite(uint32_t pos, uint32_t bytes, uint8_t*& d return 1; } -int32_t CDataStore::IsFinal() { +int32_t CDataStore::IsFinal() const { return this->m_read != -1; } +int32_t CDataStore::IsReadOnly() const { + return this->m_alloc == -1; +} + +int32_t CDataStore::IsValid() const { + return this->m_read <= this->m_size; +} + int32_t CDataStore::IsRead() const { return this->m_size == this->m_read; } @@ -334,7 +410,7 @@ CDataStore& CDataStore::PutString(const char* val) { } void CDataStore::Reset() { - if (this->m_alloc == -1) { + if (this->IsReadOnly()) { this->m_data = nullptr; this->m_alloc = 0; } @@ -343,6 +419,12 @@ void CDataStore::Reset() { this->m_read = -1; } +void CDataStore::Seek(uint32_t pos) { + STORM_ASSERT(this->IsFinal()); + + this->m_read = pos; +} + CDataStore& CDataStore::Set(uint32_t pos, uint16_t val) { STORM_ASSERT(!this->IsFinal()); STORM_ASSERT(pos + sizeof(val) <= this->m_size); @@ -360,10 +442,10 @@ void CDataStore::SetSize(uint32_t size) { this->m_size = size; } -uint32_t CDataStore::Size() { +uint32_t CDataStore::Size() const { return this->m_size; } -bool CDataStore::Sub8CBBF0(uint32_t a2) { - return this->m_read <= this->m_size && this->m_size - this->m_read >= a2; +uint32_t CDataStore::Tell() const { + return this->m_read; } diff --git a/common/datastore/CDataStore.hpp b/common/datastore/CDataStore.hpp index cd8a6fd..fd7cc1d 100644 --- a/common/datastore/CDataStore.hpp +++ b/common/datastore/CDataStore.hpp @@ -5,13 +5,6 @@ class CDataStore { public: - // Member variables - uint8_t* m_data = nullptr; - uint32_t m_base = 0; - uint32_t m_alloc = 0; - uint32_t m_size = 0; - uint32_t m_read = -1; - // Virtual member functions virtual void InternalInitialize(uint8_t*& data, uint32_t& base, uint32_t& alloc) {}; virtual void InternalDestroy(uint8_t*& data, uint32_t& base, uint32_t& alloc); @@ -26,6 +19,9 @@ class CDataStore { virtual uint32_t GetHeaderSpace(); // Member functions + CDataStore(); + CDataStore(uint8_t* data, uint32_t size); + CDataStore(uint8_t* data, uint32_t size, uint32_t alloc); void Destroy(); int32_t FetchRead(uint32_t pos, uint32_t bytes); int32_t FetchWrite(uint32_t pos, uint32_t bytes, const char* fileName, int32_t lineNumber); @@ -34,9 +30,13 @@ class CDataStore { CDataStore& Get(uint32_t& val); CDataStore& Get(uint64_t& val); CDataStore& Get(float& val); + CDataStore& GetArray(uint8_t* val, uint32_t count); CDataStore& GetDataInSitu(void*& val, uint32_t bytes); CDataStore& GetString(char* val, uint32_t maxChars); - int32_t IsFinal(); + void Initialize(); + int32_t IsFinal() const; + int32_t IsReadOnly() const; + int32_t IsValid() const; CDataStore& Put(uint8_t val); CDataStore& Put(uint16_t val); CDataStore& Put(uint32_t val); @@ -45,10 +45,19 @@ class CDataStore { CDataStore& PutArray(const uint8_t* val, uint32_t count); CDataStore& PutData(const void* val, uint32_t bytes); CDataStore& PutString(const char* val); + void Seek(uint32_t pos); CDataStore& Set(uint32_t pos, uint16_t val); void SetSize(uint32_t size); - uint32_t Size(); - bool Sub8CBBF0(uint32_t a2); + uint32_t Size() const; + uint32_t Tell() const; + + private: + // Member variables + uint8_t* m_data; + uint32_t m_base; + uint32_t m_alloc; + uint32_t m_size; + uint32_t m_read; }; #endif diff --git a/common/datastore/CDataStoreCache.hpp b/common/datastore/CDataStoreCache.hpp index d467126..f456002 100644 --- a/common/datastore/CDataStoreCache.hpp +++ b/common/datastore/CDataStoreCache.hpp @@ -18,9 +18,8 @@ class CDataStoreCache : public CDataStore { // Member functions CDataStoreCache() { - this->InternalInitialize(this->m_data, this->m_base, this->m_alloc); + this->Initialize(); } - void Destroy(); }; template @@ -28,13 +27,6 @@ CDataStoreCache::~CDataStoreCache() { this->Destroy(); } -template -void CDataStoreCache::Destroy() { - if (this->m_alloc != -1) { - this->InternalDestroy(this->m_data, this->m_base, this->m_alloc); - } -} - template void CDataStoreCache::InternalInitialize(uint8_t*& data, uint32_t& base, uint32_t& alloc) { data = this->m_cache; @@ -44,7 +36,7 @@ void CDataStoreCache::InternalInitialize(uint8_t*& data, uint32_t& base, u template void CDataStoreCache::InternalDestroy(uint8_t*& data, uint32_t& base, uint32_t& alloc) { if (data && data != this->m_cache) { - SMemFree(data); + STORM_FREE(data); } data = nullptr; diff --git a/common/prop/Types.hpp b/common/prop/Types.hpp index baedff2..281670a 100644 --- a/common/prop/Types.hpp +++ b/common/prop/Types.hpp @@ -3,7 +3,23 @@ enum PROPERTY { PROP_EVENTCONTEXT = 0, - // TODO + PROP_HANDLETABLE = 1, + PROP_EVENTSTATE = 2, + PROP_TIMERS = 3, + PROP_STRINGSTATE = 4, + PROP_JASS = 5, + PROP_JVM = 6, + PROP_TEXTURES = 7, + PROP_MODELS = 8, + PROP_AGILE = 9, + PROP_TEMPEST = 10, + PROP_RAIN = 11, + PROP_IPSE = 12, + PROP_APPLICATION = 13, + PROP_NET = 14, + PROP_WORLD = 15, + PROP_BATTLENET = 16, + PROP_COLLISION = 17, PROPERTIES, }; diff --git a/common/xml/XMLNode.cpp b/common/xml/XMLNode.cpp index f19d9f4..92f0200 100644 --- a/common/xml/XMLNode.cpp +++ b/common/xml/XMLNode.cpp @@ -1,15 +1,15 @@ #include "common/xml/XMLNode.hpp" #include -XMLNode::XMLNode() { - // TODO +XMLNode::XMLNode(XMLNode* parent, const char* name) { + this->Init(parent, name); } XMLNode::~XMLNode() { // TODO } -const char* XMLNode::GetAttributeByName(const char* name) { +const char* XMLNode::GetAttributeByName(const char* name) const { for (int32_t i = 0; i < this->m_attributes.Count(); i++) { auto& attribute = this->m_attributes[i]; @@ -25,7 +25,11 @@ const char* XMLNode::GetBody() const { return this->m_body; } -XMLNode* XMLNode::GetChildByName(const char* name) { +const XMLNode* XMLNode::GetChild() const { + return this->m_child; +} + +const XMLNode* XMLNode::GetChildByName(const char* name) const { auto child = this->m_child; if (!child) { @@ -43,27 +47,31 @@ XMLNode* XMLNode::GetChildByName(const char* name) { return child; } -const char* XMLNode::GetName() { +const char* XMLNode::GetName() const { return this->m_name.GetString(); } +const XMLNode* XMLNode::GetSibling() const { + return this->m_next; +} + void XMLNode::Init(XMLNode* parent, const char* name) { - RCString(this->m_name); - TSGrowableArray(this->m_attributes); this->m_parent = parent; this->m_child = nullptr; + this->m_name.Copy(name); + this->m_body = nullptr; this->m_bodyLen = 0; if (this->m_parent && this->m_parent->m_body) { - this->m_next = nullptr; this->m_userData = nullptr; + this->m_next = nullptr; this->m_offset = this->m_parent->m_bodyLen; } else { - this->m_offset = 0; - this->m_next = nullptr; this->m_userData = nullptr; + this->m_next = nullptr; + this->m_offset = 0; } } diff --git a/common/xml/XMLNode.hpp b/common/xml/XMLNode.hpp index 3a82ad5..4c006d6 100644 --- a/common/xml/XMLNode.hpp +++ b/common/xml/XMLNode.hpp @@ -20,12 +20,14 @@ class XMLNode { XMLNode* m_next; // Member functions - XMLNode(); + XMLNode(XMLNode* parent, const char* name); ~XMLNode(); - const char* GetAttributeByName(const char* name); + const char* GetAttributeByName(const char* name) const; const char* GetBody() const; - XMLNode* GetChildByName(const char* name); - const char* GetName(); + const XMLNode* GetChild() const; + const XMLNode* GetChildByName(const char* name) const; + const char* GetName() const; + const XMLNode* GetSibling() const; void Init(XMLNode* parent, const char* name); void SetAttribute(const char* name, const char* value); }; diff --git a/common/xml/XMLTree.cpp b/common/xml/XMLTree.cpp index b2629dc..159e5a7 100644 --- a/common/xml/XMLTree.cpp +++ b/common/xml/XMLTree.cpp @@ -15,8 +15,7 @@ void begin_element(void* userData, const XML_Char* name, const XML_Char** atts) // TODO allocate node off of node heap // XMLNode* node = XMLNode::s_XMLNodeHeap->GetData(0, __FILE__, __LINE__); - node = new XMLNode; - node->Init(tree->leaf, name); + node = new XMLNode(tree->leaf, name); } auto leaf = tree->leaf; diff --git a/test/DataStore.cpp b/test/DataStore.cpp index de671b2..0ea639a 100644 --- a/test/DataStore.cpp +++ b/test/DataStore.cpp @@ -93,7 +93,7 @@ TEST_CASE("CDataStore::Get", "[datastore]") { msg.GetString(readVal, 3); REQUIRE(SStrCmp(readVal, "foo", STORM_MAX_STR) == 0); - REQUIRE(msg.m_read == 3); + REQUIRE(msg.Tell() == 3); } SECTION("gets string honoring maxchars of 0") { @@ -105,7 +105,7 @@ TEST_CASE("CDataStore::Get", "[datastore]") { msg.Finalize(); msg.GetString(readVal, 0); - REQUIRE(msg.m_read == 0); + REQUIRE(msg.Tell() == 0); } }