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