From ba100d1d0a8110879ccf72afb53f44ac2fcba677 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Apr 2024 15:09:58 +0300 Subject: [PATCH] minor refactor --- src/files/WorldFiles.cpp | 32 +++++++++++++------------------- src/files/WorldFiles.h | 4 ++-- src/frontend/menu/menu_pause.cpp | 2 +- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index f96878a6..4740a5a1 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -318,7 +318,10 @@ ubyte* WorldFiles::getData( ubyte* data = region->getChunkData(localX, localZ); if (data == nullptr) { uint32_t size; - data = readChunkData(x, z, size, folder, layer); + auto regfile = getRegFile(glm::ivec3(regionX, regionZ, layer), folder); + if (regfile != nullptr) { + data = readChunkData(x, z, size, regfile.get()); + } if (data != nullptr) { region->put(localX, localZ, data, size); } @@ -397,8 +400,7 @@ ubyte* WorldFiles::readChunkData( int x, int z, uint32_t& length, - fs::path folder, - int layer + regfile* rfile ){ if (generatorTestMode) return nullptr; @@ -406,12 +408,7 @@ ubyte* WorldFiles::readChunkData( int regionX, regionZ, localX, localZ; calc_reg_coords(x, z, regionX, regionZ, localX, localZ); int chunkIndex = localZ * REGION_SIZE + localX; - - glm::ivec3 coord(regionX, regionZ, layer); - auto rfile = getRegFile(coord, folder); - if (rfile == nullptr) { - return nullptr; - } + files::rafile& file = rfile->file; size_t file_size = file.length(); @@ -434,9 +431,7 @@ ubyte* WorldFiles::readChunkData( } /// @brief Read missing chunks data (null pointers) from region file -/// @param layer used as third part of openRegFiles map key -/// (see REGION_LAYER_* constants) -void WorldFiles::fetchChunks(WorldRegion* region, int x, int z, fs::path folder, int layer) { +void WorldFiles::fetchChunks(WorldRegion* region, int x, int z, regfile* file) { ubyte** chunks = region->getChunks(); uint32_t* sizes = region->getSizes(); @@ -444,7 +439,7 @@ void WorldFiles::fetchChunks(WorldRegion* region, int x, int z, fs::path folder, int chunk_x = (i % REGION_SIZE) + x * REGION_SIZE; int chunk_z = (i / REGION_SIZE) + z * REGION_SIZE; if (chunks[i] == nullptr) { - chunks[i] = readChunkData(chunk_x, chunk_z, sizes[i], folder, layer); + chunks[i] = readChunkData(chunk_x, chunk_z, sizes[i], file); } } } @@ -458,12 +453,11 @@ void WorldFiles::writeRegion(int x, int z, WorldRegion* entry, fs::path folder, fs::path filename = folder/getRegionFilename(x, z); glm::ivec3 regcoord(x, z, layer); - if (getRegFile(regcoord, folder)) { - fetchChunks(entry, x, z, folder, layer); - { - std::lock_guard lock(regFilesMutex); - closeRegFile(regcoord); - } + if (auto regfile = getRegFile(regcoord, folder)) { + fetchChunks(entry, x, z, regfile.get()); + + std::lock_guard lock(regFilesMutex); + closeRegFile(regcoord); } char header[REGION_HEADER_SIZE] = REGION_FORMAT_MAGIC; diff --git a/src/files/WorldFiles.h b/src/files/WorldFiles.h index 6628759e..d6a7d873 100644 --- a/src/files/WorldFiles.h +++ b/src/files/WorldFiles.h @@ -103,9 +103,9 @@ class WorldFiles { /// @return decompressed bytes array ubyte* decompress(const ubyte* src, size_t srclen, size_t dstlen); - ubyte* readChunkData(int x, int y, uint32_t& length, fs::path folder, int layer); + ubyte* readChunkData(int x, int y, uint32_t& length, regfile* file); - void fetchChunks(WorldRegion* region, int x, int y, fs::path folder, int layer); + void fetchChunks(WorldRegion* region, int x, int y, regfile* file); void writeRegions(regionsmap& regions, const fs::path& folder, int layer); diff --git a/src/frontend/menu/menu_pause.cpp b/src/frontend/menu/menu_pause.cpp index f31fec0c..bb7df2f9 100644 --- a/src/frontend/menu/menu_pause.cpp +++ b/src/frontend/menu/menu_pause.cpp @@ -140,7 +140,7 @@ void menus::remove_packs( runnable removeFunc = [=]() { controller->saveWorld(); - auto manager = engine->createPacksManager(world->wfile->directory);; + auto manager = engine->createPacksManager(world->wfile->directory); manager.scan(); auto names = PacksManager::getNames(world->getPacks());