From afedd103e8ac3b72c1bd578c8cefdf3e34d9bd3c Mon Sep 17 00:00:00 2001 From: A-lex-Ra Date: Mon, 18 Dec 2023 20:48:03 +0600 Subject: [PATCH] refactor a bit --- src/definitions.cpp | 2 +- src/files/WorldFiles.cpp | 13 ++++++------- src/files/files.cpp | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/definitions.cpp b/src/definitions.cpp index 40dcb105..d8550354 100644 --- a/src/definitions.cpp +++ b/src/definitions.cpp @@ -11,7 +11,7 @@ using glm::vec3; // All in-game definitions (blocks, items, etc..) -void setup_definitions(ContentBuilder* builder) { +void setup_definitions(ContentBuilder* builder) { // Strange function, need to REDO ? Block* block = new Block("core:air", "air"); block->replaceable = true; block->drawGroup = 1; diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 7125a187..5264313a 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -238,22 +238,18 @@ ubyte* WorldFiles::getData(unordered_map& regions, int localX = x - (regionX * REGION_SIZE); int localZ = z - (regionZ * REGION_SIZE); - WorldRegion* region = getRegion(regions, regionX, regionZ); - if (region == nullptr) { - region = new WorldRegion(); - regions[ivec2(regionX, regionZ)] = region; - } + WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ); ubyte* data = region->get(localX, localZ); if (data == nullptr) { uint32_t size; data = readChunkData(x, z, size, folder/getRegionFilename(regionX, regionZ)); - if (data) { + if (data != nullptr) { region->put(localX, localZ, data, size); } } - if (data) { + if (data != nullptr) { return decompress(data, region->getSize(localX, localZ), CHUNK_DATA_LEN); } return nullptr; @@ -291,6 +287,9 @@ ubyte* WorldFiles::readChunkData(int x, int z, uint32_t& length, path filename){ ubyte* data = new ubyte[length]; input.read((char*)data, length); input.close(); + if (data == nullptr) { + std::cerr << "ERROR: failed to read data of chunk x("<< x <<"), z("<< z <<")" << std::endl; + } return data; } diff --git a/src/files/files.cpp b/src/files/files.cpp index a9b6fb88..399c548e 100644 --- a/src/files/files.cpp +++ b/src/files/files.cpp @@ -50,7 +50,7 @@ char* files::read_bytes(path filename, size_t& length) { length = input.tellg(); input.seekg(0, std::ios_base::beg); - unique_ptr data {new char[length]}; + unique_ptr data(new char[length]); input.read(data.get(), length); input.close(); return data.release();