memory related refactor

This commit is contained in:
MihailRis
2024-04-14 19:17:51 +03:00
parent 8384ecf2c7
commit f5b3f3c33f
9 changed files with 177 additions and 159 deletions
+17 -23
View File
@@ -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);
}