Use vector instead of ustring

This commit is contained in:
Unknown 2018-04-08 07:27:35 +02:00
parent fb21462e8d
commit 0292669a5e
8 changed files with 22 additions and 35 deletions

View File

@ -1607,8 +1607,8 @@ void TileGenerator::createImage()
void TileGenerator::processMapBlock(const DB::Block &block) void TileGenerator::processMapBlock(const DB::Block &block)
{ {
const BlockPos &pos = block.first; const BlockPos &pos = block.first;
const unsigned char *data = block.second.c_str(); const unsigned char *data = block.second.data();
size_t length = block.second.length(); size_t length = block.second.size();
uint8_t version = readU8(data, 0, length); uint8_t version = readU8(data, 0, length);
//uint8_t flags = readU8(data, 1, length); //uint8_t flags = readU8(data, 1, length);
@ -1628,8 +1628,8 @@ void TileGenerator::processMapBlock(const DB::Block &block)
checkDataLimit("zlib", dataOffset, 3, length); checkDataLimit("zlib", dataOffset, 3, length);
ZlibDecompressor decompressor(data, length); ZlibDecompressor decompressor(data, length);
decompressor.setSeekPos(dataOffset); decompressor.setSeekPos(dataOffset);
ustring mapData = decompressor.decompress(); auto mapData = decompressor.decompress();
ustring mapMetadata = decompressor.decompress(); auto mapMetadata = decompressor.decompress();
dataOffset = decompressor.seekPos(); dataOffset = decompressor.seekPos();
// Skip unused data // Skip unused data
@ -2021,12 +2021,12 @@ Color TileGenerator::computeMapHeightColor(int height)
return Color(int(r / n + 0.5), int(g / n + 0.5), int(b / n + 0.5)); return Color(int(r / n + 0.5), int(g / n + 0.5), int(b / n + 0.5));
} }
inline void TileGenerator::renderMapBlock(const ustring &mapBlock, const BlockPos &pos, int version) inline void TileGenerator::renderMapBlock(const std::vector<unsigned char> &mapBlock, const BlockPos &pos, int version)
{ {
checkBlockNodeDataLimit(version, mapBlock.length()); checkBlockNodeDataLimit(version, mapBlock.size());
int xBegin = worldBlockX2StoredX(pos.x()); int xBegin = worldBlockX2StoredX(pos.x());
int zBegin = worldBlockZ2StoredY(pos.z()); int zBegin = worldBlockZ2StoredY(pos.z());
const unsigned char *mapData = mapBlock.c_str(); const unsigned char *mapData = mapBlock.data();
int minY = (pos.y() < m_reqYMin) ? 16 : (pos.y() > m_reqYMin) ? 0 : m_reqYMinNode; int minY = (pos.y() < m_reqYMin) ? 16 : (pos.y() > m_reqYMin) ? 0 : m_reqYMinNode;
int maxY = (pos.y() > m_reqYMax) ? -1 : (pos.y() < m_reqYMax) ? 15 : m_reqYMaxNode; int maxY = (pos.y() > m_reqYMax) ? -1 : (pos.y() < m_reqYMax) ? 15 : m_reqYMaxNode;
bool renderedAnything = false; bool renderedAnything = false;

View File

@ -25,7 +25,7 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "types.h"
#include "PixelAttributes.h" #include "PixelAttributes.h"
#include "BlockPos.h" #include "BlockPos.h"
#include "Color.h" #include "Color.h"
@ -186,7 +186,7 @@ private:
void pushPixelRows(PixelAttributes &pixelAttributes, int zPosLimit); void pushPixelRows(PixelAttributes &pixelAttributes, int zPosLimit);
void scalePixelRows(PixelAttributes &pixelAttributes, PixelAttributes &pixelAttributesScaled, int zPosLimit); void scalePixelRows(PixelAttributes &pixelAttributes, PixelAttributes &pixelAttributesScaled, int zPosLimit);
void processMapBlock(const DB::Block &block); void processMapBlock(const DB::Block &block);
void renderMapBlock(const ustring &mapBlock, const BlockPos &pos, int version); void renderMapBlock(const std::vector<unsigned char> &mapBlock, const BlockPos &pos, int version);
void renderScale(); void renderScale();
void renderHeightScale(); void renderHeightScale();
void renderOrigin(); void renderOrigin();

View File

@ -32,14 +32,14 @@ std::size_t ZlibDecompressor::seekPos() const
return m_seekPos; return m_seekPos;
} }
ustring ZlibDecompressor::decompress() std::vector<unsigned char> ZlibDecompressor::decompress()
{ {
const unsigned char *data = m_data + m_seekPos; const unsigned char *data = m_data + m_seekPos;
const std::size_t size = m_size - m_seekPos; const std::size_t size = m_size - m_seekPos;
ustring buffer; std::vector<unsigned char> buffer;
const size_t BUFSIZE = 128 * 1024; const size_t BUFSIZE = 256 * 1024;
uint8_t temp_buffer[BUFSIZE]; unsigned char temp_buffer[BUFSIZE];
z_stream strm; z_stream strm;
strm.zalloc = Z_NULL; strm.zalloc = Z_NULL;
@ -56,9 +56,9 @@ ustring ZlibDecompressor::decompress()
int ret = 0; int ret = 0;
do { do {
strm.avail_out = BUFSIZE; strm.avail_out = BUFSIZE;
strm.next_out = temp_buffer; strm.next_out = &temp_buffer[0];
ret = inflate(&strm, Z_NO_FLUSH); ret = inflate(&strm, Z_NO_FLUSH);
buffer += ustring(reinterpret_cast<unsigned char *>(temp_buffer), BUFSIZE - strm.avail_out); buffer.insert(buffer.end(), &temp_buffer[0], &temp_buffer[BUFSIZE - strm.avail_out]);
} while (ret == Z_OK); } while (ret == Z_OK);
if (ret != Z_STREAM_END) { if (ret != Z_STREAM_END) {
throw DecompressError(strm.msg); throw DecompressError(strm.msg);

View File

@ -12,7 +12,7 @@
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
#include "types.h" #include <vector>
class ZlibDecompressor class ZlibDecompressor
@ -27,7 +27,7 @@ public:
~ZlibDecompressor(); ~ZlibDecompressor();
void setSeekPos(std::size_t seekPos); void setSeekPos(std::size_t seekPos);
std::size_t seekPos() const; std::size_t seekPos() const;
ustring decompress(); std::vector<unsigned char> decompress();
private: private:
const unsigned char *m_data; const unsigned char *m_data;

View File

@ -8,7 +8,6 @@
#include <iomanip> #include <iomanip>
#include <ctime> #include <ctime>
#include "porting.h" #include "porting.h"
#include "types.h"
#define DATAVERSION_STATEMENT "PRAGMA data_version" #define DATAVERSION_STATEMENT "PRAGMA data_version"
#define BLOCKPOSLIST_STATEMENT "SELECT pos, rowid FROM blocks" #define BLOCKPOSLIST_STATEMENT "SELECT pos, rowid FROM blocks"
@ -211,7 +210,7 @@ int DBSQLite3::getBlockPosListRows()
DB::Block DBSQLite3::getBlockOnPos(const BlockPos &pos) DB::Block DBSQLite3::getBlockOnPos(const BlockPos &pos)
{ {
Block block(pos,reinterpret_cast<const unsigned char *>("")); Block block(pos, {});
int result = 0; int result = 0;
m_blocksQueriedCount++; m_blocksQueriedCount++;
@ -234,7 +233,7 @@ DB::Block DBSQLite3::getBlockOnPos(const BlockPos &pos)
if(result == SQLITE_ROW) { if(result == SQLITE_ROW) {
const unsigned char *data = reinterpret_cast<const unsigned char *>(sqlite3_column_blob(statement, 1)); const unsigned char *data = reinterpret_cast<const unsigned char *>(sqlite3_column_blob(statement, 1));
int size = sqlite3_column_bytes(statement, 1); int size = sqlite3_column_bytes(statement, 1);
block = Block(pos, ustring(data, size)); block.second.assign(&data[0], &data[size]);
m_blocksReadCount++; m_blocksReadCount++;
break; break;
} else if (result == SQLITE_BUSY) { // Wait some time and try again } else if (result == SQLITE_BUSY) { // Wait some time and try again

View File

@ -15,14 +15,13 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "types.h"
class DBSQLite3 : public DB { class DBSQLite3 : public DB {
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
typedef std::unordered_map<int64_t, ustring> BlockCache; typedef std::unordered_map<int64_t, std::vector<unsigned char>> BlockCache;
typedef std::unordered_set<int64_t> BlockIdSet; typedef std::unordered_set<int64_t> BlockIdSet;
#else #else
typedef std::map<int64_t, ustring> BlockCache; typedef std::map<int64_t, std::vector<unsigned char>> BlockCache;
typedef std::set<int64_t> BlockIdSet; typedef std::set<int64_t> BlockIdSet;
#endif #endif
public: public:

View File

@ -6,14 +6,13 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "types.h"
#include "BlockPos.h" #include "BlockPos.h"
class DB { class DB {
public: public:
virtual ~DB() {} virtual ~DB() {}
typedef std::pair<BlockPos, ustring> Block; typedef std::pair<BlockPos, std::vector<unsigned char>> Block;
typedef std::vector<BlockPos> BlockPosList; typedef std::vector<BlockPos> BlockPosList;
virtual const BlockPosList &getBlockPosList()=0; virtual const BlockPosList &getBlockPosList()=0;
virtual const BlockPosList &getBlockPosList(BlockPos, BlockPos) { return getBlockPosList(); } virtual const BlockPosList &getBlockPosList(BlockPos, BlockPos) { return getBlockPosList(); }

View File

@ -1,10 +0,0 @@
#ifndef TYPES_H
#define TYPES_H
#include <string>
typedef std::basic_string<unsigned char> ustring;
#endif // TYPES_H