memory related refactor
This commit is contained in:
parent
8384ecf2c7
commit
f5b3f3c33f
@ -10,21 +10,23 @@
|
||||
#include "../voxels/Chunk.h"
|
||||
#include "../content/ContentLUT.h"
|
||||
#include "../objects/Player.h"
|
||||
#include "../debug/Logger.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static debug::Logger logger("world-converter");
|
||||
|
||||
WorldConverter::WorldConverter(
|
||||
fs::path folder,
|
||||
const Content* content,
|
||||
std::shared_ptr<ContentLUT> lut
|
||||
) : lut(lut), content(content)
|
||||
) : wfile(std::make_unique<WorldFiles>(folder, DebugSettings {})),
|
||||
lut(lut),
|
||||
content(content)
|
||||
{
|
||||
DebugSettings settings;
|
||||
wfile = new WorldFiles(folder, settings);
|
||||
|
||||
fs::path regionsFolder = wfile->getRegionsFolder();
|
||||
fs::path regionsFolder = wfile->getRegionsFolder(REGION_LAYER_VOXELS);
|
||||
if (!fs::is_directory(regionsFolder)) {
|
||||
std::cerr << "nothing to convert" << std::endl;
|
||||
logger.error() << "nothing to convert";
|
||||
return;
|
||||
}
|
||||
tasks.push(convert_task {convert_task_type::player, wfile->getPlayerFile()});
|
||||
@ -34,34 +36,26 @@ WorldConverter::WorldConverter(
|
||||
}
|
||||
|
||||
WorldConverter::~WorldConverter() {
|
||||
delete wfile;
|
||||
}
|
||||
|
||||
void WorldConverter::convertRegion(fs::path file) {
|
||||
int x, z;
|
||||
std::string name = file.stem().string();
|
||||
if (!WorldFiles::parseRegionFilename(name, x, z)) {
|
||||
std::cerr << "could not parse name " << name << std::endl;
|
||||
logger.error() << "could not parse name " << name;
|
||||
return;
|
||||
}
|
||||
std::cout << "converting region " << name << std::endl;
|
||||
for (uint cz = 0; cz < REGION_SIZE; cz++) {
|
||||
for (uint cx = 0; cx < REGION_SIZE; cx++) {
|
||||
int gx = cx + x * REGION_SIZE;
|
||||
int gz = cz + z * REGION_SIZE;
|
||||
std::unique_ptr<ubyte[]> data (wfile->getChunk(gx, gz));
|
||||
if (data == nullptr)
|
||||
continue;
|
||||
if (lut) {
|
||||
Chunk::convert(data.get(), lut.get());
|
||||
}
|
||||
wfile->put(gx, gz, data.get());
|
||||
logger.info() << "converting region " << name;
|
||||
wfile->processRegionVoxels(x, z, [=](ubyte* data) {
|
||||
if (lut) {
|
||||
Chunk::convert(data, lut.get());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
void WorldConverter::convertPlayer(fs::path file) {
|
||||
std::cout << "converting player " << file.u8string() << std::endl;
|
||||
logger.info() << "converting player " << file.u8string();
|
||||
auto map = files::read_json(file);
|
||||
Player::convert(map.get(), lut.get());
|
||||
files::write_json(file, map.get());
|
||||
@ -88,7 +82,7 @@ void WorldConverter::convertNext() {
|
||||
}
|
||||
|
||||
void WorldConverter::write() {
|
||||
std::cout << "writing world" << std::endl;
|
||||
logger.info() << "writing world";
|
||||
wfile->write(nullptr, content);
|
||||
}
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ struct convert_task {
|
||||
};
|
||||
|
||||
class WorldConverter : public Task {
|
||||
WorldFiles* wfile;
|
||||
std::unique_ptr<WorldFiles> wfile;
|
||||
std::shared_ptr<ContentLUT> const lut;
|
||||
const Content* const content;
|
||||
std::queue<convert_task> tasks;
|
||||
|
||||
@ -101,6 +101,13 @@ WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings)
|
||||
doWriteLights(settings.doWriteLights)
|
||||
{
|
||||
compressionBuffer = std::make_unique<ubyte[]>(CHUNK_DATA_LEN * 2);
|
||||
// just ignore this
|
||||
for (uint i = 0; i < sizeof(layers)/sizeof(RegionsLayer); i++) {
|
||||
layers[i].layer = i;
|
||||
}
|
||||
layers[REGION_LAYER_VOXELS].folder = directory/fs::path("regions");
|
||||
layers[REGION_LAYER_LIGHTS].folder = directory/fs::path("lights");
|
||||
layers[REGION_LAYER_INVENTORIES].folder = directory/fs::path("inventories");
|
||||
}
|
||||
|
||||
WorldFiles::~WorldFiles() {
|
||||
@ -111,36 +118,40 @@ void WorldFiles::createDirectories() {
|
||||
fs::create_directories(directory / fs::path("content"));
|
||||
}
|
||||
|
||||
WorldRegion* WorldFiles::getRegion(regionsmap& regions, int x, int z) {
|
||||
auto found = regions.find(glm::ivec2(x, z));
|
||||
if (found == regions.end())
|
||||
WorldRegion* WorldFiles::getRegion(int x, int z, int layer) {
|
||||
RegionsLayer& regions = layers[layer];
|
||||
std::lock_guard lock(regions.mutex);
|
||||
auto found = regions.regions.find(glm::ivec2(x, z));
|
||||
if (found == regions.regions.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
WorldRegion* WorldFiles::getOrCreateRegion(regionsmap& regions, int x, int z) {
|
||||
WorldRegion* region = getRegion(regions, x, z);
|
||||
WorldRegion* WorldFiles::getOrCreateRegion(int x, int z, int layer) {
|
||||
RegionsLayer& regions = layers[layer];
|
||||
WorldRegion* region = getRegion(x, z, layer);
|
||||
if (region == nullptr) {
|
||||
std::lock_guard lock(regions.mutex);
|
||||
region = new WorldRegion();
|
||||
regions[glm::ivec2(x, z)].reset(region);
|
||||
regions.regions[glm::ivec2(x, z)].reset(region);
|
||||
}
|
||||
return region;
|
||||
}
|
||||
|
||||
ubyte* WorldFiles::compress(const ubyte* src, size_t srclen, size_t& len) {
|
||||
std::unique_ptr<ubyte[]> WorldFiles::compress(const ubyte* src, size_t srclen, size_t& len) {
|
||||
ubyte* buffer = this->compressionBuffer.get();
|
||||
|
||||
len = extrle::encode(src, srclen, buffer);
|
||||
ubyte* data = new ubyte[len];
|
||||
auto data = std::make_unique<ubyte[]>(len);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
data[i] = buffer[i];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
ubyte* WorldFiles::decompress(const ubyte* src, size_t srclen, size_t dstlen) {
|
||||
ubyte* decompressed = new ubyte[dstlen];
|
||||
extrle::decode(src, srclen, decompressed);
|
||||
std::unique_ptr<ubyte[]> WorldFiles::decompress(const ubyte* src, size_t srclen, size_t dstlen) {
|
||||
auto decompressed = std::make_unique<ubyte[]>(dstlen);
|
||||
extrle::decode(src, srclen, decompressed.get());
|
||||
return decompressed;
|
||||
}
|
||||
|
||||
@ -154,20 +165,42 @@ inline void calc_reg_coords(
|
||||
}
|
||||
|
||||
|
||||
/// @brief Compress and store chunk voxels data in region
|
||||
/// @brief Store chunk voxels data in region
|
||||
/// @param x chunk.x
|
||||
/// @param z chunk.z
|
||||
void WorldFiles::put(int x, int z, const ubyte* voxelData) {
|
||||
void WorldFiles::put(int x, int z, int layer, std::unique_ptr<ubyte[]> data, size_t size, bool rle) {
|
||||
if (rle) {
|
||||
size_t compressedSize;
|
||||
auto compressed = compress(data.get(), size, compressedSize);
|
||||
put(x, z, layer, std::move(compressed), compressedSize, false);
|
||||
return;
|
||||
}
|
||||
int regionX, regionZ, localX, localZ;
|
||||
calc_reg_coords(x, z, regionX, regionZ, localX, localZ);
|
||||
|
||||
/* Writing Voxels */ {
|
||||
WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ);
|
||||
region->setUnsaved(true);
|
||||
size_t compressedSize;
|
||||
ubyte* data = compress(voxelData, CHUNK_DATA_LEN, compressedSize);
|
||||
region->put(localX, localZ, data, compressedSize);
|
||||
WorldRegion* region = getOrCreateRegion(regionX, regionZ, layer);
|
||||
region->setUnsaved(true);
|
||||
region->put(localX, localZ, data.release(), size);
|
||||
}
|
||||
|
||||
std::unique_ptr<ubyte[]> write_inventories(Chunk* chunk, uint& datasize) {
|
||||
auto& inventories = chunk->inventories;
|
||||
ByteBuilder builder;
|
||||
builder.putInt32(inventories.size());
|
||||
for (auto& entry : inventories) {
|
||||
builder.putInt32(entry.first);
|
||||
auto map = entry.second->serialize();
|
||||
auto bytes = json::to_binary(map.get(), true);
|
||||
builder.putInt32(bytes.size());
|
||||
builder.put(bytes.data(), bytes.size());
|
||||
}
|
||||
auto datavec = builder.data();
|
||||
datasize = builder.size();
|
||||
auto data = std::make_unique<ubyte[]>(datasize);
|
||||
for (uint i = 0; i < datasize; i++) {
|
||||
data[i] = datavec[i];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/// @brief Store chunk (voxels and lights) in region (existing or new)
|
||||
@ -177,62 +210,22 @@ void WorldFiles::put(Chunk* chunk){
|
||||
int regionX, regionZ, localX, localZ;
|
||||
calc_reg_coords(chunk->x, chunk->z, regionX, regionZ, localX, localZ);
|
||||
|
||||
/* Writing voxels */ {
|
||||
size_t compressedSize;
|
||||
std::unique_ptr<ubyte[]> chunk_data (chunk->encode());
|
||||
ubyte* data = compress(chunk_data.get(), CHUNK_DATA_LEN, compressedSize);
|
||||
put(chunk->x, chunk->z, REGION_LAYER_VOXELS,
|
||||
chunk->encode(), CHUNK_DATA_LEN, true);
|
||||
|
||||
WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ);
|
||||
region->setUnsaved(true);
|
||||
region->put(localX, localZ, data, compressedSize);
|
||||
}
|
||||
// Writing lights cache
|
||||
if (doWriteLights && chunk->isLighted()) {
|
||||
size_t compressedSize;
|
||||
std::unique_ptr<ubyte[]> light_data (chunk->lightmap.encode());
|
||||
ubyte* data = compress(light_data.get(), LIGHTMAP_DATA_LEN, compressedSize);
|
||||
|
||||
WorldRegion* region = getOrCreateRegion(lights, regionX, regionZ);
|
||||
region->setUnsaved(true);
|
||||
region->put(localX, localZ, data, compressedSize);
|
||||
put(chunk->x, chunk->z, REGION_LAYER_LIGHTS,
|
||||
chunk->lightmap.encode(), LIGHTMAP_DATA_LEN, true);
|
||||
}
|
||||
// Writing block inventories
|
||||
if (!chunk->inventories.empty()){
|
||||
auto& inventories = chunk->inventories;
|
||||
ByteBuilder builder;
|
||||
builder.putInt32(inventories.size());
|
||||
for (auto& entry : inventories) {
|
||||
builder.putInt32(entry.first);
|
||||
auto map = entry.second->serialize();
|
||||
auto bytes = json::to_binary(map.get(), true);
|
||||
builder.putInt32(bytes.size());
|
||||
builder.put(bytes.data(), bytes.size());
|
||||
}
|
||||
WorldRegion* region = getOrCreateRegion(storages, regionX, regionZ);
|
||||
region->setUnsaved(true);
|
||||
|
||||
auto datavec = builder.data();
|
||||
uint datasize = builder.size();
|
||||
auto data = std::make_unique<ubyte[]>(datasize);
|
||||
for (uint i = 0; i < datasize; i++) {
|
||||
data[i] = datavec[i];
|
||||
}
|
||||
region->put(localX, localZ, data.release(), datasize);
|
||||
uint datasize;
|
||||
put(chunk->x, chunk->z, REGION_LAYER_INVENTORIES,
|
||||
write_inventories(chunk, datasize), datasize, false);
|
||||
}
|
||||
}
|
||||
|
||||
fs::path WorldFiles::getRegionsFolder() const {
|
||||
return directory/fs::path("regions");
|
||||
}
|
||||
|
||||
fs::path WorldFiles::getLightsFolder() const {
|
||||
return directory/fs::path("lights");
|
||||
}
|
||||
|
||||
fs::path WorldFiles::getInventoriesFolder() const {
|
||||
return directory/fs::path("inventories");
|
||||
}
|
||||
|
||||
fs::path WorldFiles::getRegionFilename(int x, int z) const {
|
||||
return fs::path(std::to_string(x) + "_" + std::to_string(z) + ".bin");
|
||||
}
|
||||
@ -273,25 +266,33 @@ fs::path WorldFiles::getPacksFile() const {
|
||||
return directory/fs::path("packs.list");
|
||||
}
|
||||
|
||||
ubyte* WorldFiles::getChunk(int x, int z){
|
||||
return getData(regions, getRegionsFolder(), x, z, REGION_LAYER_VOXELS, true);
|
||||
std::unique_ptr<ubyte[]> WorldFiles::getChunk(int x, int z){
|
||||
uint32_t size;
|
||||
auto* data = getData(x, z, REGION_LAYER_VOXELS, size);
|
||||
if (data == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return decompress(data, size, CHUNK_DATA_LEN);
|
||||
}
|
||||
|
||||
/// @brief Get cached lights for chunk at x,z
|
||||
/// @return lights data or nullptr
|
||||
light_t* WorldFiles::getLights(int x, int z) {
|
||||
std::unique_ptr<ubyte[]> data (getData(lights, getLightsFolder(), x, z, REGION_LAYER_LIGHTS, true));
|
||||
if (data == nullptr)
|
||||
std::unique_ptr<light_t[]> WorldFiles::getLights(int x, int z) {
|
||||
uint32_t size;
|
||||
auto* bytes = getData(x, z, REGION_LAYER_LIGHTS, size);
|
||||
if (bytes == nullptr)
|
||||
return nullptr;
|
||||
auto data = decompress(bytes, size, LIGHTMAP_DATA_LEN);
|
||||
return Lightmap::decode(data.get());
|
||||
}
|
||||
|
||||
chunk_inventories_map WorldFiles::fetchInventories(int x, int z) {
|
||||
chunk_inventories_map inventories;
|
||||
const ubyte* data = getData(storages, getInventoriesFolder(), x, z, REGION_LAYER_INVENTORIES, false);
|
||||
uint32_t bytesSize;
|
||||
const ubyte* data = getData(x, z, REGION_LAYER_INVENTORIES, bytesSize);
|
||||
if (data == nullptr)
|
||||
return inventories;
|
||||
ByteReader reader(data, BUFFER_SIZE_UNKNOWN);
|
||||
ByteReader reader(data, bytesSize);
|
||||
int count = reader.getInt32();
|
||||
for (int i = 0; i < count; i++) {
|
||||
uint index = reader.getInt32();
|
||||
@ -306,19 +307,16 @@ chunk_inventories_map WorldFiles::fetchInventories(int x, int z) {
|
||||
}
|
||||
|
||||
ubyte* WorldFiles::getData(
|
||||
regionsmap& regions,
|
||||
const fs::path& folder,
|
||||
int x, int z, int layer,
|
||||
bool compression
|
||||
uint32_t& size
|
||||
) {
|
||||
int regionX, regionZ, localX, localZ;
|
||||
calc_reg_coords(x, z, regionX, regionZ, localX, localZ);
|
||||
|
||||
WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ);
|
||||
WorldRegion* region = getOrCreateRegion(regionX, regionZ, layer);
|
||||
ubyte* data = region->getChunkData(localX, localZ);
|
||||
if (data == nullptr) {
|
||||
uint32_t size;
|
||||
auto regfile = getRegFile(glm::ivec3(regionX, regionZ, layer), folder);
|
||||
auto regfile = getRegFile(glm::ivec3(regionX, regionZ, layer));
|
||||
if (regfile != nullptr) {
|
||||
data = readChunkData(x, z, size, regfile.get());
|
||||
}
|
||||
@ -327,10 +325,7 @@ ubyte* WorldFiles::getData(
|
||||
}
|
||||
}
|
||||
if (data != nullptr) {
|
||||
size_t size = region->getChunkDataSize(localX, localZ);
|
||||
if (compression) {
|
||||
return decompress(data, size, CHUNK_DATA_LEN);
|
||||
}
|
||||
size = region->getChunkDataSize(localX, localZ);
|
||||
return data;
|
||||
}
|
||||
return nullptr;
|
||||
@ -349,7 +344,7 @@ void WorldFiles::closeRegFile(glm::ivec3 coord) {
|
||||
}
|
||||
|
||||
// Marks regfile as used and unmarks when shared_ptr dies
|
||||
std::shared_ptr<regfile> WorldFiles::getRegFile(glm::ivec3 coord, const fs::path& folder) {
|
||||
std::shared_ptr<regfile> WorldFiles::getRegFile(glm::ivec3 coord) {
|
||||
{
|
||||
std::lock_guard lock(regFilesMutex);
|
||||
const auto found = openRegFiles.find(coord);
|
||||
@ -361,11 +356,11 @@ std::shared_ptr<regfile> WorldFiles::getRegFile(glm::ivec3 coord, const fs::path
|
||||
return useRegFile(found->first);
|
||||
}
|
||||
}
|
||||
return createRegFile(coord, folder);
|
||||
return createRegFile(coord);
|
||||
}
|
||||
|
||||
std::shared_ptr<regfile> WorldFiles::createRegFile(glm::ivec3 coord, const fs::path& folder) {
|
||||
fs::path file = folder / getRegionFilename(coord[0], coord[1]);
|
||||
std::shared_ptr<regfile> WorldFiles::createRegFile(glm::ivec3 coord) {
|
||||
fs::path file = layers[coord[2]].folder/getRegionFilename(coord[0], coord[1]);
|
||||
if (!fs::exists(file)) {
|
||||
return nullptr;
|
||||
}
|
||||
@ -449,11 +444,11 @@ void WorldFiles::fetchChunks(WorldRegion* region, int x, int z, regfile* file) {
|
||||
/// @param z region Z
|
||||
/// @param layer used as third part of openRegFiles map key
|
||||
/// (see REGION_LAYER_* constants)
|
||||
void WorldFiles::writeRegion(int x, int z, WorldRegion* entry, fs::path folder, int layer){
|
||||
fs::path filename = folder/getRegionFilename(x, z);
|
||||
void WorldFiles::writeRegion(int x, int z, int layer, WorldRegion* entry){
|
||||
fs::path filename = layers[layer].folder/getRegionFilename(x, z);
|
||||
|
||||
glm::ivec3 regcoord(x, z, layer);
|
||||
if (auto regfile = getRegFile(regcoord, folder)) {
|
||||
if (auto regfile = getRegFile(regcoord)) {
|
||||
fetchChunks(entry, x, z, regfile.get());
|
||||
|
||||
std::lock_guard lock(regFilesMutex);
|
||||
@ -494,25 +489,20 @@ void WorldFiles::writeRegion(int x, int z, WorldRegion* entry, fs::path folder,
|
||||
}
|
||||
}
|
||||
|
||||
void WorldFiles::writeRegions(regionsmap& regions, const fs::path& folder, int layer) {
|
||||
for (auto& it : regions){
|
||||
void WorldFiles::writeRegions(int layer) {
|
||||
for (auto& it : layers[layer].regions){
|
||||
WorldRegion* region = it.second.get();
|
||||
if (region->getChunks() == nullptr || !region->isUnsaved())
|
||||
continue;
|
||||
glm::ivec2 key = it.first;
|
||||
writeRegion(key[0], key[1], region, folder, layer);
|
||||
writeRegion(key[0], key[1], layer, region);
|
||||
}
|
||||
}
|
||||
|
||||
void WorldFiles::write(const World* world, const Content* content) {
|
||||
fs::path regionsFolder = getRegionsFolder();
|
||||
fs::path lightsFolder = getLightsFolder();
|
||||
fs::path inventoriesFolder = getInventoriesFolder();
|
||||
|
||||
fs::create_directories(regionsFolder);
|
||||
fs::create_directories(inventoriesFolder);
|
||||
fs::create_directories(lightsFolder);
|
||||
|
||||
for (auto& layer : layers) {
|
||||
fs::create_directories(layer.folder);
|
||||
}
|
||||
if (world) {
|
||||
writeWorldInfo(world);
|
||||
if (!fs::exists(getPacksFile())) {
|
||||
@ -524,9 +514,9 @@ void WorldFiles::write(const World* world, const Content* content) {
|
||||
}
|
||||
|
||||
writeIndices(content->getIndices());
|
||||
writeRegions(regions, regionsFolder, REGION_LAYER_VOXELS);
|
||||
writeRegions(lights, lightsFolder, REGION_LAYER_LIGHTS);
|
||||
writeRegions(storages, inventoriesFolder, REGION_LAYER_INVENTORIES);
|
||||
for (auto& layer : layers) {
|
||||
writeRegions(layer.layer);
|
||||
}
|
||||
}
|
||||
|
||||
void WorldFiles::writePacks(const std::vector<ContentPack>& packs) {
|
||||
@ -603,3 +593,26 @@ void WorldFiles::removeIndices(const std::vector<std::string>& packs) {
|
||||
}
|
||||
files::write_json(getIndicesFile(), root.get());
|
||||
}
|
||||
|
||||
void WorldFiles::processRegionVoxels(int x, int z, regionproc func) {
|
||||
if (getRegion(x, z, REGION_LAYER_VOXELS)) {
|
||||
throw std::runtime_error("not implemented for in-memory regions");
|
||||
}
|
||||
auto regfile = getRegFile(glm::ivec3(x, z, REGION_LAYER_VOXELS));
|
||||
if (regfile == nullptr) {
|
||||
throw std::runtime_error("could not open region file");
|
||||
}
|
||||
for (uint cz = 0; cz < REGION_SIZE; cz++) {
|
||||
for (uint cx = 0; cx < REGION_SIZE; cx++) {
|
||||
int gx = cx + x * REGION_SIZE;
|
||||
int gz = cz + z * REGION_SIZE;
|
||||
uint32_t length;
|
||||
std::unique_ptr<ubyte[]> data (readChunkData(gx, gz, length, regfile.get()));
|
||||
if (data == nullptr)
|
||||
continue;
|
||||
if (func(data.get())) {
|
||||
put(gx, gz, REGION_LAYER_VOXELS, std::move(data), CHUNK_DATA_LEN, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,59 +73,62 @@ struct regfile {
|
||||
regfile(fs::path filename);
|
||||
};
|
||||
|
||||
typedef std::unordered_map<glm::ivec2, std::unique_ptr<WorldRegion>> regionsmap;
|
||||
using regionsmap = std::unordered_map<glm::ivec2, std::unique_ptr<WorldRegion>>;
|
||||
using regionproc = std::function<bool(ubyte*)>;
|
||||
|
||||
struct RegionsLayer {
|
||||
int layer;
|
||||
fs::path folder;
|
||||
regionsmap regions;
|
||||
std::mutex mutex;
|
||||
};
|
||||
|
||||
class WorldFiles {
|
||||
std::unordered_map<glm::ivec3, std::unique_ptr<regfile>> openRegFiles;
|
||||
std::mutex regFilesMutex;
|
||||
std::condition_variable regFilesCv;
|
||||
|
||||
RegionsLayer layers[3] {};
|
||||
|
||||
void writeWorldInfo(const World* world);
|
||||
fs::path getRegionFilename(int x, int y) const;
|
||||
fs::path getWorldFile() const;
|
||||
fs::path getIndicesFile() const;
|
||||
fs::path getPacksFile() const;
|
||||
|
||||
WorldRegion* getRegion(regionsmap& regions, int x, int z);
|
||||
WorldRegion* getOrCreateRegion(regionsmap& regions, int x, int z);
|
||||
WorldRegion* getRegion(int x, int z, int layer);
|
||||
WorldRegion* getOrCreateRegion(int x, int z, int layer);
|
||||
|
||||
/// @brief Compress buffer with extrle
|
||||
/// @param src source buffer
|
||||
/// @param srclen length of the source buffer
|
||||
/// @param len (out argument) length of result buffer
|
||||
/// @return compressed bytes array
|
||||
ubyte* compress(const ubyte* src, size_t srclen, size_t& len);
|
||||
std::unique_ptr<ubyte[]> compress(const ubyte* src, size_t srclen, size_t& len);
|
||||
|
||||
/// @brief Decompress buffer with extrle
|
||||
/// @param src compressed buffer
|
||||
/// @param srclen length of compressed buffer
|
||||
/// @param dstlen max expected length of source buffer
|
||||
/// @return decompressed bytes array
|
||||
ubyte* decompress(const ubyte* src, size_t srclen, size_t dstlen);
|
||||
std::unique_ptr<ubyte[]> decompress(const ubyte* src, size_t srclen, size_t dstlen);
|
||||
|
||||
ubyte* readChunkData(int x, int y, uint32_t& length, regfile* file);
|
||||
|
||||
void fetchChunks(WorldRegion* region, int x, int y, regfile* file);
|
||||
|
||||
void writeRegions(regionsmap& regions, const fs::path& folder, int layer);
|
||||
void writeRegions(int layer);
|
||||
|
||||
ubyte* getData(regionsmap& regions, const fs::path& folder, int x, int z, int layer, bool compression);
|
||||
ubyte* getData(int x, int z, int layer, uint32_t& size);
|
||||
|
||||
std::shared_ptr<regfile> getRegFile(glm::ivec3 coord, const fs::path& folder);
|
||||
std::shared_ptr<regfile> getRegFile(glm::ivec3 coord);
|
||||
void closeRegFile(glm::ivec3 coord);
|
||||
std::shared_ptr<regfile> useRegFile(glm::ivec3 coord);
|
||||
std::shared_ptr<regfile> createRegFile(glm::ivec3 coord, const fs::path& folder);
|
||||
|
||||
fs::path getLightsFolder() const;
|
||||
fs::path getInventoriesFolder() const;
|
||||
std::shared_ptr<regfile> createRegFile(glm::ivec3 coord);
|
||||
public:
|
||||
static bool parseRegionFilename(const std::string& name, int& x, int& y);
|
||||
fs::path getRegionsFolder() const;
|
||||
fs::path getPlayerFile() const;
|
||||
|
||||
regionsmap regions;
|
||||
regionsmap storages;
|
||||
regionsmap lights;
|
||||
fs::path directory;
|
||||
std::unique_ptr<ubyte[]> compressionBuffer;
|
||||
bool generatorTestMode;
|
||||
@ -137,15 +140,15 @@ public:
|
||||
void createDirectories();
|
||||
|
||||
void put(Chunk* chunk);
|
||||
void put(int x, int z, const ubyte* voxelData);
|
||||
void put(int x, int z, int layer, std::unique_ptr<ubyte[]> data, size_t size, bool rle);
|
||||
|
||||
ubyte* getChunk(int x, int z);
|
||||
light_t* getLights(int x, int z);
|
||||
std::unique_ptr<ubyte[]> getChunk(int x, int z);
|
||||
std::unique_ptr<light_t[]> getLights(int x, int z);
|
||||
chunk_inventories_map fetchInventories(int x, int z);
|
||||
|
||||
bool readWorldInfo(World* world);
|
||||
|
||||
void writeRegion(int x, int y, WorldRegion* entry, fs::path file, int layer);
|
||||
void writeRegion(int x, int y, int layer, WorldRegion* entry);
|
||||
|
||||
/// @brief Write all unsaved data to world files
|
||||
/// @param world target world
|
||||
@ -157,7 +160,13 @@ public:
|
||||
|
||||
void removeIndices(const std::vector<std::string>& packs);
|
||||
|
||||
void processRegionVoxels(int x, int z, regionproc func);
|
||||
|
||||
static const inline std::string WORLD_FILE = "world.json";
|
||||
|
||||
fs::path getRegionsFolder(int layer) {
|
||||
return layers[layer].folder;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* FILES_WORLDFILES_H_ */
|
||||
|
||||
@ -15,16 +15,16 @@ void Lightmap::set(const light_t* map) {
|
||||
|
||||
static_assert(sizeof(light_t) == 2, "replace dataio calls to new light_t");
|
||||
|
||||
ubyte* Lightmap::encode() const {
|
||||
ubyte* buffer = new ubyte[LIGHTMAP_DATA_LEN];
|
||||
std::unique_ptr<ubyte[]> Lightmap::encode() const {
|
||||
auto buffer = std::make_unique<ubyte[]>(LIGHTMAP_DATA_LEN);
|
||||
for (uint i = 0; i < CHUNK_VOL; i+=2) {
|
||||
buffer[i/2] = ((map[i] >> 12) & 0xF) | ((map[i+1] >> 8) & 0xF0);
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
light_t* Lightmap::decode(ubyte* buffer) {
|
||||
light_t* lights = new light_t[CHUNK_VOL];
|
||||
std::unique_ptr<light_t[]> Lightmap::decode(ubyte* buffer) {
|
||||
auto lights = std::make_unique<light_t[]>(CHUNK_VOL);
|
||||
for (uint i = 0; i < CHUNK_VOL; i+=2) {
|
||||
ubyte b = buffer[i/2];
|
||||
lights[i] = ((b & 0xF) << 12);
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
#include "../constants.h"
|
||||
#include "../typedefs.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
inline constexpr int LIGHTMAP_DATA_LEN = CHUNK_VOL/2;
|
||||
|
||||
// Lichtkarte
|
||||
@ -81,8 +83,8 @@ public:
|
||||
return (light >> (channel << 2)) & 0xF;
|
||||
}
|
||||
|
||||
ubyte* encode() const;
|
||||
static light_t* decode(ubyte* buffer);
|
||||
std::unique_ptr<ubyte[]> encode() const;
|
||||
static std::unique_ptr<light_t[]> decode(ubyte* buffer);
|
||||
};
|
||||
|
||||
#endif /* LIGHTING_LIGHTMAP_H_ */
|
||||
|
||||
@ -92,8 +92,8 @@ std::unique_ptr<Chunk> Chunk::clone() const {
|
||||
|
||||
Total size: (CHUNK_VOL * 4) bytes
|
||||
*/
|
||||
ubyte* Chunk::encode() const {
|
||||
ubyte* buffer = new ubyte[CHUNK_DATA_LEN];
|
||||
std::unique_ptr<ubyte[]> Chunk::encode() const {
|
||||
auto buffer = std::make_unique<ubyte[]>(CHUNK_DATA_LEN);
|
||||
for (uint i = 0; i < CHUNK_VOL; i++) {
|
||||
buffer[i] = voxels[i].id >> 8;
|
||||
buffer[CHUNK_VOL+i] = voxels[i].id & 0xFF;
|
||||
|
||||
@ -87,7 +87,7 @@ public:
|
||||
|
||||
inline void setReady(bool newState) {setFlags(ChunkFlag::READY, newState);}
|
||||
|
||||
ubyte* encode() const;
|
||||
std::unique_ptr<ubyte[]> encode() const;
|
||||
|
||||
/**
|
||||
* @return true if all is fine
|
||||
|
||||
@ -55,7 +55,7 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
|
||||
|
||||
auto chunk = std::make_shared<Chunk>(x, z);
|
||||
store(chunk);
|
||||
std::unique_ptr<ubyte[]> data(wfile->getChunk(chunk->x, chunk->z));
|
||||
auto data = wfile->getChunk(chunk->x, chunk->z);
|
||||
if (data) {
|
||||
chunk->decode(data.get());
|
||||
auto invs = wfile->fetchInventories(chunk->x, chunk->z);
|
||||
@ -67,7 +67,7 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
|
||||
verifyLoadedChunk(level->content->getIndices(), chunk.get());
|
||||
}
|
||||
|
||||
std::unique_ptr<light_t[]> lights (wfile->getLights(chunk->x, chunk->z));
|
||||
auto lights = wfile->getLights(chunk->x, chunk->z);
|
||||
if (lights) {
|
||||
chunk->lightmap.set(lights.get());
|
||||
chunk->setLoadedLights(true);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user