migrate from std::filesystem::path to io::path (WIP)

This commit is contained in:
MihailRis
2025-01-30 22:23:13 +03:00
parent 1e22882284
commit e0314803c0
72 changed files with 1189 additions and 791 deletions
+7 -7
View File
@@ -78,7 +78,7 @@ void World::write(Level* level) {
std::unique_ptr<Level> World::create(
const std::string& name,
const std::string& generator,
const fs::path& directory,
const io::path& directory,
uint64_t seed,
EngineSettings& settings,
const Content& content,
@@ -98,7 +98,7 @@ std::unique_ptr<Level> World::create(
logger.info() << "created nameless world";
} else {
logger.info() << "created world '" << name << "' ("
<< directory.u8string() << ")";
<< directory.string() << ")";
}
logger.info() << "world seed: " << seed << " generator: " << generator;
return std::make_unique<Level>(std::move(world), content, settings);
@@ -116,7 +116,7 @@ std::unique_ptr<Level> World::load(
throw world_load_error("could not to find world.json");
}
logger.info() << "loading world " << info->name << " ("
<< worldFilesPtr->getFolder().u8string() << ")";
<< worldFilesPtr->getFolder().string() << ")";
logger.info() << "world version: " << info->major << "." << info->minor
<< " seed: " << info->seed
<< " generator: " << info->generator;
@@ -129,8 +129,8 @@ std::unique_ptr<Level> World::load(
auto level = std::make_unique<Level>(std::move(world), content, settings);
fs::path file = wfile->getPlayerFile();
if (!fs::is_regular_file(file)) {
io::path file = wfile->getPlayerFile();
if (!io::is_regular_file(file)) {
logger.warning() << "player.json does not exists";
level->players->create();
} else {
@@ -149,8 +149,8 @@ std::unique_ptr<Level> World::load(
std::shared_ptr<ContentReport> World::checkIndices(
const std::shared_ptr<WorldFiles>& worldFiles, const Content* content
) {
fs::path indicesFile = worldFiles->getIndicesFile();
if (fs::is_regular_file(indicesFile)) {
io::path indicesFile = worldFiles->getIndicesFile();
if (io::is_regular_file(indicesFile)) {
return ContentReport::create(worldFiles, indicesFile, content);
}
return nullptr;
+2 -4
View File
@@ -1,10 +1,10 @@
#pragma once
#include <filesystem>
#include <memory>
#include <string>
#include <vector>
#include "io/fwd.hpp"
#include "content/ContentPack.hpp"
#include "interfaces/Serializable.hpp"
#include "typedefs.hpp"
@@ -16,8 +16,6 @@ class Level;
class ContentReport;
struct EngineSettings;
namespace fs = std::filesystem;
class world_load_error : public std::runtime_error {
public:
world_load_error(const std::string& message);
@@ -100,7 +98,7 @@ public:
static std::unique_ptr<Level> create(
const std::string& name,
const std::string& generator,
const fs::path& directory,
const io::path& directory,
uint64_t seed,
EngineSettings& settings,
const Content& content,
+8 -8
View File
@@ -6,8 +6,8 @@
#define REGION_FORMAT_MAGIC ".VOXREG"
static fs::path get_region_filename(int x, int z) {
return fs::path(std::to_string(x) + "_" + std::to_string(z) + ".bin");
static io::path get_region_filename(int x, int z) {
return std::to_string(x) + "_" + std::to_string(z) + ".bin";
}
/// @brief Read missing chunks data (null pointers) from region file
@@ -25,7 +25,7 @@ static void fetch_chunks(WorldRegion* region, int x, int z, regfile* file) {
}
}
regfile::regfile(fs::path filename) : file(std::move(filename)) {
regfile::regfile(io::path filename) : file(std::move(filename)) {
if (file.length() < REGION_HEADER_SIZE)
throw std::runtime_error("incomplete region file header");
char header[REGION_HEADER_SIZE];
@@ -98,8 +98,8 @@ regfile_ptr RegionsLayer::getRegFile(glm::ivec2 coord, bool create) {
}
regfile_ptr RegionsLayer::createRegFile(glm::ivec2 coord) {
auto file = folder / get_region_filename(coord[0], coord[1]);
if (!fs::exists(file)) {
auto file = folder / get_region_filename(coord[0], coord[1]);
if (!io::exists(file)) {
return nullptr;
}
if (openRegFiles.size() == MAX_OPEN_REGION_FILES) {
@@ -138,7 +138,7 @@ WorldRegion* RegionsLayer::getRegion(int x, int z) {
return found->second.get();
}
fs::path RegionsLayer::getRegionFilePath(int x, int z) const {
io::path RegionsLayer::getRegionFilePath(int x, int z) const {
return folder / get_region_filename(x, z);
}
@@ -179,7 +179,7 @@ ubyte* RegionsLayer::getData(int x, int z, uint32_t& size, uint32_t& srcSize) {
}
void RegionsLayer::writeRegion(int x, int z, WorldRegion* entry) {
fs::path filename = folder / get_region_filename(x, z);
io::path filename = folder / get_region_filename(x, z);
glm::ivec2 regcoord(x, z);
if (auto regfile = getRegFile(regcoord, false)) {
@@ -193,7 +193,7 @@ void RegionsLayer::writeRegion(int x, int z, WorldRegion* entry) {
char header[REGION_HEADER_SIZE] = REGION_FORMAT_MAGIC;
header[8] = REGION_FORMAT_VERSION;
header[9] = static_cast<ubyte>(compression); // FIXME
std::ofstream file(filename, std::ios::out | std::ios::binary);
std::ofstream file(io::resolve(filename), std::ios::out | std::ios::binary);
file.write(header, REGION_HEADER_SIZE);
size_t offset = REGION_HEADER_SIZE;
+9 -9
View File
@@ -39,17 +39,17 @@ void WorldConverter::addRegionsTasks(
) {
const auto& regions = wfile->getRegions();
auto regionsFolder = regions.getRegionsFolder(layerid);
if (!fs::is_directory(regionsFolder)) {
if (!io::is_directory(regionsFolder)) {
return;
}
for (const auto& file : fs::directory_iterator(regionsFolder)) {
for (const auto& file : fs::directory_iterator(io::resolve(regionsFolder))) {
int x, z;
std::string name = file.path().stem().string();
if (!WorldRegions::parseRegionFilename(name, x, z)) {
logger.error() << "could not parse region name " << name;
continue;
}
tasks.push(ConvertTask {taskType, file.path(), x, z, layerid});
tasks.push(ConvertTask {taskType, file.path().u8string(), x, z, layerid});
}
}
@@ -182,7 +182,7 @@ std::shared_ptr<Task> WorldConverter::startTask(
}
void WorldConverter::upgradeRegion(
const fs::path& file, int x, int z, RegionLayerIndex layer
const io::path& file, int x, int z, RegionLayerIndex layer
) const {
auto path = wfile->getRegions().getRegionFilePath(layer, x, z);
auto bytes = io::read_bytes_buffer(path);
@@ -190,7 +190,7 @@ void WorldConverter::upgradeRegion(
io::write_bytes(path, buffer.data(), buffer.size());
}
void WorldConverter::convertVoxels(const fs::path& file, int x, int z) const {
void WorldConverter::convertVoxels(const io::path& file, int x, int z) const {
logger.info() << "converting voxels region " << x << "_" << z;
wfile->getRegions().processRegion(x, z, REGION_LAYER_VOXELS,
[=](std::unique_ptr<ubyte[]> data, uint32_t*) {
@@ -199,15 +199,15 @@ void WorldConverter::convertVoxels(const fs::path& file, int x, int z) const {
});
}
void WorldConverter::convertInventories(const fs::path& file, int x, int z) const {
void WorldConverter::convertInventories(const io::path& file, int x, int z) const {
logger.info() << "converting inventories region " << x << "_" << z;
wfile->getRegions().processInventories(x, z, [=](Inventory* inventory) {
inventory->convert(report.get());
});
}
void WorldConverter::convertPlayer(const fs::path& file) const {
logger.info() << "converting player " << file.u8string();
void WorldConverter::convertPlayer(const io::path& file) const {
logger.info() << "converting player " << file.string();
auto map = io::read_json(file);
Player::convert(map, report.get());
io::write_json(file, map);
@@ -242,7 +242,7 @@ void WorldConverter::convertBlocksData(int x, int z, const ContentReport& report
}
void WorldConverter::convert(const ConvertTask& task) const {
if (!fs::is_regular_file(task.file)) return;
if (!io::is_regular_file(task.file)) return;
switch (task.type) {
case ConvertTaskType::UPGRADE_REGION:
+6 -8
View File
@@ -1,16 +1,14 @@
#pragma once
#include <filesystem>
#include <memory>
#include <queue>
#include "delegates.hpp"
#include "interfaces/Task.hpp"
#include "io/io.hpp"
#include "world/files/world_regions_fwd.hpp"
#include "typedefs.hpp"
namespace fs = std::filesystem;
class Content;
class ContentReport;
class WorldFiles;
@@ -30,7 +28,7 @@ enum class ConvertTaskType {
struct ConvertTask {
ConvertTaskType type;
fs::path file;
io::path file;
/// @brief region coords
int x, z;
@@ -53,10 +51,10 @@ class WorldConverter : public Task {
ConvertMode mode;
void upgradeRegion(
const fs::path& file, int x, int z, RegionLayerIndex layer) const;
void convertPlayer(const fs::path& file) const;
void convertVoxels(const fs::path& file, int x, int z) const;
void convertInventories(const fs::path& file, int x, int z) const;
const io::path& file, int x, int z, RegionLayerIndex layer) const;
void convertPlayer(const io::path& file) const;
void convertVoxels(const io::path& file, int x, int z) const;
void convertInventories(const io::path& file, int x, int z) const;
void convertBlocksData(int x, int z, const ContentReport& report) const;
void addRegionsTasks(
+23 -23
View File
@@ -36,11 +36,11 @@
static debug::Logger logger("world-files");
WorldFiles::WorldFiles(const fs::path& directory)
WorldFiles::WorldFiles(const io::path& directory)
: directory(directory), regions(directory) {
}
WorldFiles::WorldFiles(const fs::path& directory, const DebugSettings& settings)
WorldFiles::WorldFiles(const io::path& directory, const DebugSettings& settings)
: WorldFiles(directory) {
generatorTestMode = settings.generatorTestMode.get();
doWriteLights = settings.doWriteLights.get();
@@ -51,28 +51,28 @@ WorldFiles::WorldFiles(const fs::path& directory, const DebugSettings& settings)
WorldFiles::~WorldFiles() = default;
void WorldFiles::createDirectories() {
fs::create_directories(directory / fs::path("data"));
fs::create_directories(directory / fs::path("content"));
io::create_directories(directory / "data");
io::create_directories(directory / "content");
}
fs::path WorldFiles::getPlayerFile() const {
return directory / fs::path("player.json");
io::path WorldFiles::getPlayerFile() const {
return directory / "player.json";
}
fs::path WorldFiles::getResourcesFile() const {
return directory / fs::path("resources.json");
io::path WorldFiles::getResourcesFile() const {
return directory / "resources.json";
}
fs::path WorldFiles::getWorldFile() const {
return directory / fs::path(WORLD_FILE);
io::path WorldFiles::getWorldFile() const {
return directory / WORLD_FILE;
}
fs::path WorldFiles::getIndicesFile() const {
return directory / fs::path("indices.json");
io::path WorldFiles::getIndicesFile() const {
return directory / "indices.json";
}
fs::path WorldFiles::getPacksFile() const {
return directory / fs::path("packs.list");
io::path WorldFiles::getPacksFile() const {
return directory / "packs.list";
}
void WorldFiles::write(
@@ -80,7 +80,7 @@ void WorldFiles::write(
) {
if (world) {
writeWorldInfo(world->getInfo());
if (!fs::exists(getPacksFile())) {
if (!io::exists(getPacksFile())) {
writePacks(world->getPacks());
}
}
@@ -147,8 +147,8 @@ void WorldFiles::writeWorldInfo(const WorldInfo& info) {
}
std::optional<WorldInfo> WorldFiles::readWorldInfo() {
fs::path file = getWorldFile();
if (!fs::is_regular_file(file)) {
io::path file = getWorldFile();
if (!io::is_regular_file(file)) {
logger.warning() << "world.json does not exists";
return std::nullopt;
}
@@ -175,8 +175,8 @@ static void read_resources_data(
}
bool WorldFiles::readResourcesData(const Content& content) {
fs::path file = getResourcesFile();
if (!fs::is_regular_file(file)) {
io::path file = getResourcesFile();
if (!io::is_regular_file(file)) {
logger.warning() << "resources.json does not exists";
return false;
}
@@ -192,9 +192,9 @@ bool WorldFiles::readResourcesData(const Content& content) {
}
void WorldFiles::patchIndicesFile(const dv::value& map) {
fs::path file = getIndicesFile();
if (!fs::is_regular_file(file)) {
logger.error() << file.filename().u8string() << " does not exists";
io::path file = getIndicesFile();
if (!io::is_regular_file(file)) {
logger.error() << file.name() << " does not exists";
return;
}
auto root = io::read_json(file);
@@ -230,6 +230,6 @@ void WorldFiles::removeIndices(const std::vector<std::string>& packs) {
io::write_json(getIndicesFile(), root);
}
fs::path WorldFiles::getFolder() const {
io::path WorldFiles::getFolder() const {
return directory;
}
+9 -12
View File
@@ -1,6 +1,5 @@
#pragma once
#include <filesystem>
#include <glm/glm.hpp>
#include <optional>
#include <memory>
@@ -24,28 +23,26 @@ class World;
struct WorldInfo;
struct DebugSettings;
namespace fs = std::filesystem;
class WorldFiles {
fs::path directory;
io::path directory;
WorldRegions regions;
bool generatorTestMode = false;
bool doWriteLights = true;
fs::path getWorldFile() const;
fs::path getPacksFile() const;
io::path getWorldFile() const;
io::path getPacksFile() const;
void writeWorldInfo(const WorldInfo& info);
void writeIndices(const ContentIndices* indices);
public:
WorldFiles(const fs::path& directory);
WorldFiles(const fs::path& directory, const DebugSettings& settings);
WorldFiles(const io::path& directory);
WorldFiles(const io::path& directory, const DebugSettings& settings);
~WorldFiles();
fs::path getPlayerFile() const;
fs::path getIndicesFile() const;
fs::path getResourcesFile() const;
io::path getPlayerFile() const;
io::path getIndicesFile() const;
io::path getResourcesFile() const;
void createDirectories();
std::optional<WorldInfo> readWorldInfo();
@@ -70,7 +67,7 @@ public:
void removeIndices(const std::vector<std::string>& packs);
/// @return world folder
fs::path getFolder() const;
io::path getFolder() const;
WorldRegions& getRegions() {
return regions;
+12 -12
View File
@@ -57,24 +57,24 @@ glm::u32vec2 WorldRegion::getChunkDataSize(uint x, uint z) {
return sizes[z * REGION_SIZE + x];
}
WorldRegions::WorldRegions(const fs::path& directory) : directory(directory) {
WorldRegions::WorldRegions(const io::path& directory) : directory(directory) {
for (size_t i = 0; i < REGION_LAYERS_COUNT; i++) {
layers[i].layer = static_cast<RegionLayerIndex>(i);
}
auto& voxels = layers[REGION_LAYER_VOXELS];
voxels.folder = directory / fs::path("regions");
voxels.folder = directory / "regions";
voxels.compression = compression::Method::EXTRLE16;
auto& lights = layers[REGION_LAYER_LIGHTS];
lights.folder = directory / fs::path("lights");
lights.folder = directory / "lights";
lights.compression = compression::Method::EXTRLE8;
layers[REGION_LAYER_INVENTORIES].folder =
directory / fs::path("inventories");
layers[REGION_LAYER_ENTITIES].folder = directory / fs::path("entities");
directory / "inventories";
layers[REGION_LAYER_ENTITIES].folder = directory / "entities";
auto& blocksData = layers[REGION_LAYER_BLOCKS_DATA];
blocksData.folder = directory / fs::path("blocksdata");
blocksData.folder = directory / "blocksdata";
}
WorldRegions::~WorldRegions() = default;
@@ -388,17 +388,17 @@ void WorldRegions::processRegion(
}
}
const fs::path& WorldRegions::getRegionsFolder(RegionLayerIndex layerid) const {
const io::path& WorldRegions::getRegionsFolder(RegionLayerIndex layerid) const {
return layers[layerid].folder;
}
fs::path WorldRegions::getRegionFilePath(RegionLayerIndex layerid, int x, int z) const {
io::path WorldRegions::getRegionFilePath(RegionLayerIndex layerid, int x, int z) const {
return layers[layerid].getRegionFilePath(x, z);
}
void WorldRegions::writeAll() {
for (auto& layer : layers) {
fs::create_directories(layer.folder);
io::create_directories(layer.folder);
layer.writeAll();
}
}
@@ -409,9 +409,9 @@ void WorldRegions::deleteRegion(RegionLayerIndex layerid, int x, int z) {
throw std::runtime_error("region file is currently in use");
}
auto file = layer.getRegionFilePath(x, z);
if (fs::exists(file)) {
logger.info() << "remove region file " << file.u8string();
fs::remove(file);
if (io::exists(file)) {
logger.info() << "remove region file " << file.string();
io::remove(file);
}
}
+7 -10
View File
@@ -1,7 +1,6 @@
#pragma once
#include <condition_variable>
#include <filesystem>
#include <functional>
#include <glm/glm.hpp>
#include <memory>
@@ -19,8 +18,6 @@
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp>
namespace fs = std::filesystem;
inline constexpr uint REGION_HEADER_SIZE = 10;
inline constexpr uint REGION_SIZE_BIT = 5;
@@ -58,7 +55,7 @@ struct regfile {
int version;
bool inUse = false;
regfile(fs::path filename);
regfile(io::path filename);
regfile(const regfile&) = delete;
std::unique_ptr<ubyte[]> read(int index, uint32_t& size, uint32_t& srcSize);
@@ -121,7 +118,7 @@ struct RegionsLayer {
RegionLayerIndex layer;
/// @brief Regions layer folder
fs::path folder;
io::path folder;
compression::Method compression = compression::Method::NONE;
@@ -146,7 +143,7 @@ struct RegionsLayer {
WorldRegion* getRegion(int x, int z);
WorldRegion* getOrCreateRegion(int x, int z);
fs::path getRegionFilePath(int x, int z) const;
io::path getRegionFilePath(int x, int z) const;
/// @brief Get chunk data. Read from file if not loaded yet.
/// @param x chunk x coord
@@ -178,14 +175,14 @@ struct RegionsLayer {
class WorldRegions {
/// @brief World directory
fs::path directory;
io::path directory;
RegionsLayer layers[REGION_LAYERS_COUNT] {};
public:
bool generatorTestMode = false;
bool doWriteLights = true;
WorldRegions(const fs::path& directory);
WorldRegions(const io::path& directory);
WorldRegions(const WorldRegions&) = delete;
~WorldRegions();
@@ -241,9 +238,9 @@ public:
/// @brief Get regions directory by layer index
/// @param layerid layer index
/// @return directory path
const fs::path& getRegionsFolder(RegionLayerIndex layerid) const;
const io::path& getRegionsFolder(RegionLayerIndex layerid) const;
fs::path getRegionFilePath(RegionLayerIndex layerid, int x, int z) const;
io::path getRegionFilePath(RegionLayerIndex layerid, int x, int z) const;
/// @brief Write all region layers
void writeAll();