diff --git a/library/include/BitArray.h b/library/include/BitArray.h index 5aa46baa9..0389df796 100644 --- a/library/include/BitArray.h +++ b/library/include/BitArray.h @@ -27,6 +27,7 @@ distribution. #include "Export.h" #include #include +#include #include //#include namespace DFHack @@ -35,33 +36,42 @@ namespace DFHack class BitArray { public: - BitArray() + BitArray() : bits(NULL), size(0) {} + BitArray(const BitArray &other) : bits(NULL), size(0) { - bits = NULL; - size = 0; + *this = other; } ~BitArray() { - if(bits) - delete [] bits; + free(bits); } + void clear_all ( void ) { if(bits) memset(bits, 0, size); } - void extend (T index) + void resize (unsigned newsize) { - uint32_t newsize = (index / 8) + 1; - if (newsize <= size) + if (newsize == size) return; - uint8_t *newbits = new uint8_t[newsize]; - memset(newbits, 0, newsize); - memcpy(newbits, bits, size); - delete[] bits; - bits = newbits; + bits = (uint8_t*)realloc(bits, newsize); + if (newsize > size) + memset(bits+size, 0, newsize-size); size = newsize; } + BitArray &operator= (const BitArray &other) + { + resize(other.size); + memcpy(bits, other.bits, size); + return *this; + } + void extend (T index) + { + unsigned newsize = (index / 8) + 1; + if (newsize > size) + resize(newsize); + } void set (T index, bool value = true) { if(!value) @@ -107,7 +117,7 @@ namespace DFHack else return false; } /// WARNING: this can truncate long bit arrays - operator uint32_t () + uint32_t as_int () { if(!bits) return 0; @@ -147,4 +157,42 @@ namespace DFHack uint8_t * bits; uint32_t size; }; + + template + class DfArray + { + T *m_data; + unsigned short m_size; + public: + DfArray() : m_data(NULL), m_size(0) {} + ~DfArray() { free(m_data); } + + DfArray(const DfArray &other) : m_data(NULL), m_size(0) { + *this = other; + } + + T *data() { return m_data; } + const T *data() const { return m_data; } + unsigned size() const { return m_size; } + + T& operator[] (unsigned i) { return m_data[i]; } + const T& operator[] (unsigned i) const { return m_data[i]; } + + void resize(unsigned new_size) + { + if (new_size == m_size) + return; + m_data = (T*)realloc(m_data, sizeof(T)*new_size); + if (new_size > m_size) + memset(m_data+sizeof(T)*m_size, 0, sizeof(T)*(new_size - m_size)); + m_size = new_size; + } + + DfArray &operator= (const DfArray &other) + { + resize(other.size()); + memcpy(data(), other.data(), sizeof(T)*size()); + return *this; + } + }; } \ No newline at end of file diff --git a/library/include/DataDefs.h b/library/include/DataDefs.h index 7ef64ef5e..4e18f02f2 100644 --- a/library/include/DataDefs.h +++ b/library/include/DataDefs.h @@ -181,6 +181,7 @@ namespace df using DFHack::bitfield_item_info; using DFHack::enum_list_attr; using DFHack::BitArray; + using DFHack::DfArray; template class class_virtual_identity : public virtual_identity { diff --git a/library/modules/Maps.cpp b/library/modules/Maps.cpp index e64b71dc3..5a1ec5bad 100644 --- a/library/modules/Maps.cpp +++ b/library/modules/Maps.cpp @@ -145,7 +145,7 @@ bool Maps::ReadBlock40d(uint32_t x, uint32_t y, uint32_t z, mapblock40d * buffer buffer->local_feature = block->local_feature; buffer->mystery = block->unk2; buffer->origin = block; - buffer->blockflags.whole = block->flags; + buffer->blockflags.whole = block->flags.as_int(); return true; } return false; @@ -211,7 +211,7 @@ bool Maps::ReadBlockFlags(uint32_t x, uint32_t y, uint32_t z, t_blockflags &bloc df::map_block *block = getBlock(x,y,z); if (block) { - blockflags.whole = block->flags; + blockflags.whole = block->flags.as_int(); return true; } return false; diff --git a/library/xml b/library/xml index b3534e853..2444f376e 160000 --- a/library/xml +++ b/library/xml @@ -1 +1 @@ -Subproject commit b3534e853f99129b1664766f09f461d54be6cbfe +Subproject commit 2444f376e32783c9f7a7bf103b209e256e64ed80