Use vector instead of ustring

master
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)
{
const BlockPos &pos = block.first;
const unsigned char *data = block.second.c_str();
size_t length = block.second.length();
const unsigned char *data = block.second.data();
size_t length = block.second.size();
uint8_t version = readU8(data, 0, length);
//uint8_t flags = readU8(data, 1, length);
@ -1628,8 +1628,8 @@ void TileGenerator::processMapBlock(const DB::Block &block)
checkDataLimit("zlib", dataOffset, 3, length);
ZlibDecompressor decompressor(data, length);
decompressor.setSeekPos(dataOffset);
ustring mapData = decompressor.decompress();
ustring mapMetadata = decompressor.decompress();
auto mapData = decompressor.decompress();
auto mapMetadata = decompressor.decompress();
dataOffset = decompressor.seekPos();
// 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));
}
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 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 maxY = (pos.y() > m_reqYMax) ? -1 : (pos.y() < m_reqYMax) ? 15 : m_reqYMaxNode;
bool renderedAnything = false;

View File

@ -25,7 +25,7 @@
#include <string>
#include <iostream>
#include <sstream>
#include "types.h"
#include "PixelAttributes.h"
#include "BlockPos.h"
#include "Color.h"
@ -186,7 +186,7 @@ private:
void pushPixelRows(PixelAttributes &pixelAttributes, int zPosLimit);
void scalePixelRows(PixelAttributes &pixelAttributes, PixelAttributes &pixelAttributesScaled, int zPosLimit);
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 renderHeightScale();
void renderOrigin();

View File

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

View File

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

View File

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

View File

@ -15,14 +15,13 @@
#include <string>
#include <sstream>
#include "types.h"
class DBSQLite3 : public DB {
#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;
#else
typedef std::map<int64_t, ustring> BlockCache;
typedef std::map<int64_t, std::vector<unsigned char>> BlockCache;
typedef std::set<int64_t> BlockIdSet;
#endif
public:

View File

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