clang warnings fix + minor refactor

This commit is contained in:
MihailRis 2024-04-15 12:16:04 +03:00
parent 3ebd8f3062
commit adf54b8a38
8 changed files with 53 additions and 49 deletions

View File

@ -120,7 +120,7 @@ assetload::postfunc assetload::font(
for (size_t i = 0; i <= 4; i++) { for (size_t i = 0; i <= 4; i++) {
std::string name = filename + "_" + std::to_string(i) + ".png"; std::string name = filename + "_" + std::to_string(i) + ".png";
name = paths->find(name).string(); name = paths->find(name).string();
pages->push_back(std::move(imageio::read(name))); pages->push_back(imageio::read(name));
} }
return [=](auto assets) { return [=](auto assets) {
int res = pages->at(0)->getHeight() / 16; int res = pages->at(0)->getHeight() / 16;

View File

@ -41,7 +41,7 @@ WorldConverter::~WorldConverter() {
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 (!WorldRegions::parseRegionFilename(name, x, z)) {
logger.error() << "could not parse name " << name; logger.error() << "could not parse name " << name;
return; return;
} }

View File

@ -29,8 +29,6 @@
#define WORLD_FORMAT_MAGIC ".VOXWLD" #define WORLD_FORMAT_MAGIC ".VOXWLD"
const size_t BUFFER_SIZE_UNKNOWN = -1;
WorldFiles::WorldFiles(fs::path directory) : directory(directory), regions(directory) { WorldFiles::WorldFiles(fs::path directory) : directory(directory), regions(directory) {
} }
@ -51,26 +49,6 @@ void WorldFiles::createDirectories() {
fs::create_directories(directory / fs::path("content")); fs::create_directories(directory / fs::path("content"));
} }
/// @brief Extract X and Z from 'X_Z.bin' region file name.
/// @param name source region file name
/// @param x parsed X destination
/// @param z parsed Z destination
/// @return false if std::invalid_argument or std::out_of_range occurred
bool WorldFiles::parseRegionFilename(const std::string& name, int& x, int& z) {
size_t sep = name.find('_');
if (sep == std::string::npos || sep == 0 || sep == name.length()-1)
return false;
try {
x = std::stoi(name.substr(0, sep));
z = std::stoi(name.substr(sep+1));
} catch (std::invalid_argument& err) {
return false;
} catch (std::out_of_range& err) {
return false;
}
return true;
}
fs::path WorldFiles::getPlayerFile() const { fs::path WorldFiles::getPlayerFile() const {
return directory/fs::path("player.json"); return directory/fs::path("player.json");
} }

View File

@ -63,7 +63,6 @@ public:
fs::path getFolder() const; fs::path getFolder() const;
static const inline std::string WORLD_FILE = "world.json"; static const inline std::string WORLD_FILE = "world.json";
static bool parseRegionFilename(const std::string& name, int& x, int& y);
WorldRegions& getRegions() { WorldRegions& getRegions() {
return regions; return regions;

View File

@ -28,6 +28,26 @@ regfile::regfile(fs::path filename) : file(filename) {
} }
} }
std::unique_ptr<ubyte[]> regfile::read(int index, uint32_t& length) {
size_t file_size = file.length();
size_t table_offset = file_size - REGION_CHUNKS_COUNT * 4;
uint32_t offset;
file.seekg(table_offset + index * 4);
file.read((char*)(&offset), 4);
offset = dataio::read_int32_big((const ubyte*)(&offset), 0);
if (offset == 0){
return nullptr;
}
file.seekg(offset);
file.read((char*)(&offset), 4);
length = dataio::read_int32_big((const ubyte*)(&offset), 0);
auto data = std::make_unique<ubyte[]>(length);
file.read((char*)data.get(), length);
return data;
}
WorldRegion::WorldRegion() { WorldRegion::WorldRegion() {
chunksData = new ubyte*[REGION_CHUNKS_COUNT]{}; chunksData = new ubyte*[REGION_CHUNKS_COUNT]{};
sizes = new uint32_t[REGION_CHUNKS_COUNT]{}; sizes = new uint32_t[REGION_CHUNKS_COUNT]{};
@ -136,31 +156,10 @@ std::unique_ptr<ubyte[]> WorldRegions::readChunkData(
uint32_t& length, uint32_t& length,
regfile* rfile regfile* rfile
){ ){
if (generatorTestMode)
return nullptr;
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);
int chunkIndex = localZ * REGION_SIZE + localX; int chunkIndex = localZ * REGION_SIZE + localX;
return rfile->read(chunkIndex, length);
files::rafile& file = rfile->file;
size_t file_size = file.length();
size_t table_offset = file_size - REGION_CHUNKS_COUNT * 4;
uint32_t offset;
file.seekg(table_offset + chunkIndex * 4);
file.read((char*)(&offset), 4);
offset = dataio::read_int32_big((const ubyte*)(&offset), 0);
if (offset == 0){
return nullptr;
}
file.seekg(offset);
file.read((char*)(&offset), 4);
length = dataio::read_int32_big((const ubyte*)(&offset), 0);
auto data = std::make_unique<ubyte[]>(length);
file.read((char*)data.get(), length);
return data;
} }
/// @brief Read missing chunks data (null pointers) from region file /// @brief Read missing chunks data (null pointers) from region file
@ -181,6 +180,9 @@ ubyte* WorldRegions::getData(
int x, int z, int layer, int x, int z, int layer,
uint32_t& size uint32_t& size
) { ) {
if (generatorTestMode) {
return nullptr;
}
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);
@ -454,3 +456,18 @@ void WorldRegions::write() {
writeRegions(layer.layer); writeRegions(layer.layer);
} }
} }
bool WorldRegions::parseRegionFilename(const std::string& name, int& x, int& z) {
size_t sep = name.find('_');
if (sep == std::string::npos || sep == 0 || sep == name.length()-1)
return false;
try {
x = std::stoi(name.substr(0, sep));
z = std::stoi(name.substr(sep+1));
} catch (std::invalid_argument& err) {
return false;
} catch (std::out_of_range& err) {
return false;
}
return true;
}

View File

@ -62,6 +62,9 @@ struct regfile {
bool inUse = false; bool inUse = false;
regfile(fs::path filename); regfile(fs::path filename);
regfile(const regfile&) = delete;
std::unique_ptr<ubyte[]> read(int index, uint32_t& length);
}; };
using regionsmap = std::unordered_map<glm::ivec2, std::unique_ptr<WorldRegion>>; using regionsmap = std::unordered_map<glm::ivec2, std::unique_ptr<WorldRegion>>;
@ -150,6 +153,13 @@ public:
fs::path getRegionsFolder(int layer) const; fs::path getRegionsFolder(int layer) const;
void write(); void write();
/// @brief Extract X and Z from 'X_Z.bin' region file name.
/// @param name source region file name
/// @param x parsed X destination
/// @param z parsed Z destination
/// @return false if std::invalid_argument or std::out_of_range occurred
static bool parseRegionFilename(const std::string& name, int& x, int& y);
}; };
#endif // FILES_WORLD_REGIONS_H_ #endif // FILES_WORLD_REGIONS_H_

View File

@ -9,7 +9,7 @@
class Content; class Content;
class Assets; class Assets;
class UiDocument; class UiDocument;
class UVRegion; struct UVRegion;
using uidocuments_map = std::unordered_map<std::string, std::shared_ptr<UiDocument>>; using uidocuments_map = std::unordered_map<std::string, std::shared_ptr<UiDocument>>;

View File

@ -16,7 +16,7 @@ class Chunks;
class VoxelsVolume; class VoxelsVolume;
class ChunksStorage; class ChunksStorage;
class ContentGfxCache; class ContentGfxCache;
class UVRegion; struct UVRegion;
class BlocksRenderer { class BlocksRenderer {
static const glm::vec3 SUN_VECTOR; static const glm::vec3 SUN_VECTOR;