feat: sync with Whoa implementation

This commit is contained in:
VDm 2026-04-26 18:19:41 +04:00
parent d71c4a16bf
commit ac4b85990c
8 changed files with 153 additions and 45 deletions

View file

@ -5,12 +5,36 @@
#include <bc/Memory.hpp> #include <bc/Memory.hpp>
#include <storm/String.hpp> #include <storm/String.hpp>
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() { CDataStore::~CDataStore() {
this->Destroy(); this->Destroy();
} }
void CDataStore::Destroy() { void CDataStore::Destroy() {
if (this->m_alloc != -1) { if (!this->IsReadOnly()) {
this->InternalDestroy(this->m_data, this->m_base, this->m_alloc); this->InternalDestroy(this->m_data, this->m_base, this->m_alloc);
} }
} }
@ -133,6 +157,44 @@ CDataStore& CDataStore::Get(float& val) {
return *this; 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 { void CDataStore::GetBufferParams(const void** data, uint32_t* size, uint32_t* alloc) const {
if (data) { if (data) {
*data = this->m_data; *data = this->m_data;
@ -188,6 +250,12 @@ CDataStore& CDataStore::GetString(char* val, uint32_t maxChars) {
return *this; 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) { void CDataStore::InternalDestroy(uint8_t*& data, uint32_t& base, uint32_t& alloc) {
if (alloc && data) { if (alloc && data) {
SMemFree(data, __FILE__, __LINE__, 0); SMemFree(data, __FILE__, __LINE__, 0);
@ -215,10 +283,18 @@ int32_t CDataStore::InternalFetchWrite(uint32_t pos, uint32_t bytes, uint8_t*& d
return 1; return 1;
} }
int32_t CDataStore::IsFinal() { int32_t CDataStore::IsFinal() const {
return this->m_read != -1; 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 { int32_t CDataStore::IsRead() const {
return this->m_size == this->m_read; return this->m_size == this->m_read;
} }
@ -334,7 +410,7 @@ CDataStore& CDataStore::PutString(const char* val) {
} }
void CDataStore::Reset() { void CDataStore::Reset() {
if (this->m_alloc == -1) { if (this->IsReadOnly()) {
this->m_data = nullptr; this->m_data = nullptr;
this->m_alloc = 0; this->m_alloc = 0;
} }
@ -343,6 +419,12 @@ void CDataStore::Reset() {
this->m_read = -1; 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) { CDataStore& CDataStore::Set(uint32_t pos, uint16_t val) {
STORM_ASSERT(!this->IsFinal()); STORM_ASSERT(!this->IsFinal());
STORM_ASSERT(pos + sizeof(val) <= this->m_size); STORM_ASSERT(pos + sizeof(val) <= this->m_size);
@ -360,10 +442,10 @@ void CDataStore::SetSize(uint32_t size) {
this->m_size = size; this->m_size = size;
} }
uint32_t CDataStore::Size() { uint32_t CDataStore::Size() const {
return this->m_size; return this->m_size;
} }
bool CDataStore::Sub8CBBF0(uint32_t a2) { uint32_t CDataStore::Tell() const {
return this->m_read <= this->m_size && this->m_size - this->m_read >= a2; return this->m_read;
} }

View file

@ -5,13 +5,6 @@
class CDataStore { class CDataStore {
public: 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 member functions
virtual void InternalInitialize(uint8_t*& data, uint32_t& base, uint32_t& alloc) {}; virtual void InternalInitialize(uint8_t*& data, uint32_t& base, uint32_t& alloc) {};
virtual void InternalDestroy(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(); virtual uint32_t GetHeaderSpace();
// Member functions // Member functions
CDataStore();
CDataStore(uint8_t* data, uint32_t size);
CDataStore(uint8_t* data, uint32_t size, uint32_t alloc);
void Destroy(); void Destroy();
int32_t FetchRead(uint32_t pos, uint32_t bytes); int32_t FetchRead(uint32_t pos, uint32_t bytes);
int32_t FetchWrite(uint32_t pos, uint32_t bytes, const char* fileName, int32_t lineNumber); 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(uint32_t& val);
CDataStore& Get(uint64_t& val); CDataStore& Get(uint64_t& val);
CDataStore& Get(float& val); CDataStore& Get(float& val);
CDataStore& GetArray(uint8_t* val, uint32_t count);
CDataStore& GetDataInSitu(void*& val, uint32_t bytes); CDataStore& GetDataInSitu(void*& val, uint32_t bytes);
CDataStore& GetString(char* val, uint32_t maxChars); 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(uint8_t val);
CDataStore& Put(uint16_t val); CDataStore& Put(uint16_t val);
CDataStore& Put(uint32_t val); CDataStore& Put(uint32_t val);
@ -45,10 +45,19 @@ class CDataStore {
CDataStore& PutArray(const uint8_t* val, uint32_t count); CDataStore& PutArray(const uint8_t* val, uint32_t count);
CDataStore& PutData(const void* val, uint32_t bytes); CDataStore& PutData(const void* val, uint32_t bytes);
CDataStore& PutString(const char* val); CDataStore& PutString(const char* val);
void Seek(uint32_t pos);
CDataStore& Set(uint32_t pos, uint16_t val); CDataStore& Set(uint32_t pos, uint16_t val);
void SetSize(uint32_t size); void SetSize(uint32_t size);
uint32_t Size(); uint32_t Size() const;
bool Sub8CBBF0(uint32_t a2); 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 #endif

View file

@ -18,9 +18,8 @@ class CDataStoreCache : public CDataStore {
// Member functions // Member functions
CDataStoreCache() { CDataStoreCache() {
this->InternalInitialize(this->m_data, this->m_base, this->m_alloc); this->Initialize();
} }
void Destroy();
}; };
template <size_t size> template <size_t size>
@ -28,13 +27,6 @@ CDataStoreCache<size>::~CDataStoreCache() {
this->Destroy(); this->Destroy();
} }
template <size_t size>
void CDataStoreCache<size>::Destroy() {
if (this->m_alloc != -1) {
this->InternalDestroy(this->m_data, this->m_base, this->m_alloc);
}
}
template <size_t size> template <size_t size>
void CDataStoreCache<size>::InternalInitialize(uint8_t*& data, uint32_t& base, uint32_t& alloc) { void CDataStoreCache<size>::InternalInitialize(uint8_t*& data, uint32_t& base, uint32_t& alloc) {
data = this->m_cache; data = this->m_cache;
@ -44,7 +36,7 @@ void CDataStoreCache<size>::InternalInitialize(uint8_t*& data, uint32_t& base, u
template <size_t size> template <size_t size>
void CDataStoreCache<size>::InternalDestroy(uint8_t*& data, uint32_t& base, uint32_t& alloc) { void CDataStoreCache<size>::InternalDestroy(uint8_t*& data, uint32_t& base, uint32_t& alloc) {
if (data && data != this->m_cache) { if (data && data != this->m_cache) {
SMemFree(data); STORM_FREE(data);
} }
data = nullptr; data = nullptr;

View file

@ -3,7 +3,23 @@
enum PROPERTY { enum PROPERTY {
PROP_EVENTCONTEXT = 0, 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, PROPERTIES,
}; };

View file

@ -1,15 +1,15 @@
#include "common/xml/XMLNode.hpp" #include "common/xml/XMLNode.hpp"
#include <storm/String.hpp> #include <storm/String.hpp>
XMLNode::XMLNode() { XMLNode::XMLNode(XMLNode* parent, const char* name) {
// TODO this->Init(parent, name);
} }
XMLNode::~XMLNode() { XMLNode::~XMLNode() {
// TODO // 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++) { for (int32_t i = 0; i < this->m_attributes.Count(); i++) {
auto& attribute = this->m_attributes[i]; auto& attribute = this->m_attributes[i];
@ -25,7 +25,11 @@ const char* XMLNode::GetBody() const {
return this->m_body; 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; auto child = this->m_child;
if (!child) { if (!child) {
@ -43,27 +47,31 @@ XMLNode* XMLNode::GetChildByName(const char* name) {
return child; return child;
} }
const char* XMLNode::GetName() { const char* XMLNode::GetName() const {
return this->m_name.GetString(); return this->m_name.GetString();
} }
const XMLNode* XMLNode::GetSibling() const {
return this->m_next;
}
void XMLNode::Init(XMLNode* parent, const char* name) { void XMLNode::Init(XMLNode* parent, const char* name) {
RCString(this->m_name);
TSGrowableArray<XMLAttribute>(this->m_attributes);
this->m_parent = parent; this->m_parent = parent;
this->m_child = nullptr; this->m_child = nullptr;
this->m_name.Copy(name); this->m_name.Copy(name);
this->m_body = nullptr; this->m_body = nullptr;
this->m_bodyLen = 0; this->m_bodyLen = 0;
if (this->m_parent && this->m_parent->m_body) { if (this->m_parent && this->m_parent->m_body) {
this->m_next = nullptr;
this->m_userData = nullptr; this->m_userData = nullptr;
this->m_next = nullptr;
this->m_offset = this->m_parent->m_bodyLen; this->m_offset = this->m_parent->m_bodyLen;
} else { } else {
this->m_offset = 0;
this->m_next = nullptr;
this->m_userData = nullptr; this->m_userData = nullptr;
this->m_next = nullptr;
this->m_offset = 0;
} }
} }

View file

@ -20,12 +20,14 @@ class XMLNode {
XMLNode* m_next; XMLNode* m_next;
// Member functions // Member functions
XMLNode(); XMLNode(XMLNode* parent, const char* name);
~XMLNode(); ~XMLNode();
const char* GetAttributeByName(const char* name); const char* GetAttributeByName(const char* name) const;
const char* GetBody() const; const char* GetBody() const;
XMLNode* GetChildByName(const char* name); const XMLNode* GetChild() const;
const char* GetName(); const XMLNode* GetChildByName(const char* name) const;
const char* GetName() const;
const XMLNode* GetSibling() const;
void Init(XMLNode* parent, const char* name); void Init(XMLNode* parent, const char* name);
void SetAttribute(const char* name, const char* value); void SetAttribute(const char* name, const char* value);
}; };

View file

@ -15,8 +15,7 @@ void begin_element(void* userData, const XML_Char* name, const XML_Char** atts)
// TODO allocate node off of node heap // TODO allocate node off of node heap
// XMLNode* node = XMLNode::s_XMLNodeHeap->GetData(0, __FILE__, __LINE__); // XMLNode* node = XMLNode::s_XMLNodeHeap->GetData(0, __FILE__, __LINE__);
node = new XMLNode; node = new XMLNode(tree->leaf, name);
node->Init(tree->leaf, name);
} }
auto leaf = tree->leaf; auto leaf = tree->leaf;

View file

@ -93,7 +93,7 @@ TEST_CASE("CDataStore::Get", "[datastore]") {
msg.GetString(readVal, 3); msg.GetString(readVal, 3);
REQUIRE(SStrCmp(readVal, "foo", STORM_MAX_STR) == 0); REQUIRE(SStrCmp(readVal, "foo", STORM_MAX_STR) == 0);
REQUIRE(msg.m_read == 3); REQUIRE(msg.Tell() == 3);
} }
SECTION("gets string honoring maxchars of 0") { SECTION("gets string honoring maxchars of 0") {
@ -105,7 +105,7 @@ TEST_CASE("CDataStore::Get", "[datastore]") {
msg.Finalize(); msg.Finalize();
msg.GetString(readVal, 0); msg.GetString(readVal, 0);
REQUIRE(msg.m_read == 0); REQUIRE(msg.Tell() == 0);
} }
} }