From 4f2448daed4d2e1a7542d2eeb773b9895c3213a7 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 11 Aug 2024 18:21:12 +0300 Subject: [PATCH] refactor --- src/content/ContentLUT.cpp | 8 ++++++++ src/content/ContentLUT.hpp | 6 +++++- src/files/WorldConverter.cpp | 8 ++++---- src/files/WorldConverter.hpp | 6 +++--- src/files/WorldFiles.hpp | 2 +- src/files/WorldRegions.cpp | 4 ++-- src/logic/EngineController.cpp | 26 +++++++++++++------------- src/world/World.cpp | 11 +++++------ src/world/World.hpp | 10 +++++----- 9 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/content/ContentLUT.cpp b/src/content/ContentLUT.cpp index 490fbbb4..b39dc21b 100644 --- a/src/content/ContentLUT.cpp +++ b/src/content/ContentLUT.cpp @@ -7,6 +7,8 @@ #include "files/files.hpp" #include "items/ItemDef.hpp" #include "voxels/Block.hpp" +#include "world/World.hpp" +#include "files/WorldFiles.hpp" #include "Content.hpp" ContentLUT::ContentLUT( @@ -24,8 +26,14 @@ static constexpr size_t get_entries_count( } std::shared_ptr ContentLUT::create( + const std::shared_ptr& worldFiles, const fs::path& filename, const Content* content ) { + auto worldInfo = worldFiles->readWorldInfo(); + if (!worldInfo.has_value()) { + return nullptr; + } + auto root = files::read_json(filename); auto blocklist = root->list("blocks"); auto itemlist = root->list("items"); diff --git a/src/content/ContentLUT.hpp b/src/content/ContentLUT.hpp index 972a77cf..83c15415 100644 --- a/src/content/ContentLUT.hpp +++ b/src/content/ContentLUT.hpp @@ -17,6 +17,8 @@ struct contententry { std::string name; }; +struct WorldFiles; + template class ContentUnitLUT { std::vector indices; @@ -100,7 +102,9 @@ public: ContentLUT(const ContentIndices* indices, size_t blocks, size_t items); static std::shared_ptr create( - const fs::path& filename, const Content* content + const std::shared_ptr& worldFiles, + const fs::path& filename, + const Content* content ); inline bool hasContentReorder() const { diff --git a/src/files/WorldConverter.cpp b/src/files/WorldConverter.cpp index 637e3890..8e23e432 100644 --- a/src/files/WorldConverter.cpp +++ b/src/files/WorldConverter.cpp @@ -32,11 +32,11 @@ public: }; WorldConverter::WorldConverter( - const fs::path& folder, + const std::shared_ptr& worldFiles, const Content* content, std::shared_ptr lut ) - : wfile(std::make_unique(folder)), + : wfile(worldFiles), lut(std::move(lut)), content(content) { fs::path regionsFolder = @@ -56,13 +56,13 @@ WorldConverter::~WorldConverter() { } std::shared_ptr WorldConverter::startTask( - const fs::path& folder, + const std::shared_ptr& worldFiles, const Content* content, const std::shared_ptr& lut, const runnable& onDone, bool multithreading ) { - auto converter = std::make_shared(folder, content, lut); + auto converter = std::make_shared(worldFiles, content, lut); if (!multithreading) { converter->setOnComplete([=]() { converter->write(); diff --git a/src/files/WorldConverter.hpp b/src/files/WorldConverter.hpp index 20d7c326..c16151e8 100644 --- a/src/files/WorldConverter.hpp +++ b/src/files/WorldConverter.hpp @@ -22,7 +22,7 @@ struct convert_task { }; class WorldConverter : public Task { - std::unique_ptr wfile; + std::shared_ptr wfile; std::shared_ptr const lut; const Content* const content; std::queue tasks; @@ -33,7 +33,7 @@ class WorldConverter : public Task { void convertRegion(const fs::path& file) const; public: WorldConverter( - const fs::path& folder, + const std::shared_ptr& worldFiles, const Content* content, std::shared_ptr lut ); @@ -52,7 +52,7 @@ public: uint getWorkDone() const override; static std::shared_ptr startTask( - const fs::path& folder, + const std::shared_ptr& worldFiles, const Content* content, const std::shared_ptr& lut, const runnable& onDone, diff --git a/src/files/WorldFiles.hpp b/src/files/WorldFiles.hpp index 12753d0c..e0e78920 100644 --- a/src/files/WorldFiles.hpp +++ b/src/files/WorldFiles.hpp @@ -34,7 +34,6 @@ class WorldFiles { bool doWriteLights = true; fs::path getWorldFile() const; - fs::path getIndicesFile() const; fs::path getPacksFile() const; void writeWorldInfo(const WorldInfo& info); @@ -45,6 +44,7 @@ public: ~WorldFiles(); fs::path getPlayerFile() const; + fs::path getIndicesFile() const; fs::path getResourcesFile() const; void createDirectories(); diff --git a/src/files/WorldRegions.cpp b/src/files/WorldRegions.cpp index 854be0be..d47d64c9 100644 --- a/src/files/WorldRegions.cpp +++ b/src/files/WorldRegions.cpp @@ -20,12 +20,12 @@ regfile::regfile(fs::path filename) : file(std::move(filename)) { file.read(header, REGION_HEADER_SIZE); // avoid of use strcmp_s - if (std::string(header, strlen(REGION_FORMAT_MAGIC)) != + if (std::string(header, std::strlen(REGION_FORMAT_MAGIC)) != REGION_FORMAT_MAGIC) { throw std::runtime_error("invalid region file magic number"); } version = header[8]; - if (uint(version) > REGION_FORMAT_VERSION) { + if (static_cast(version) > REGION_FORMAT_VERSION) { throw illegal_region_format( "region format " + std::to_string(version) + " is not supported" ); diff --git a/src/logic/EngineController.cpp b/src/logic/EngineController.cpp index b68b476e..7e0d59c3 100644 --- a/src/logic/EngineController.cpp +++ b/src/logic/EngineController.cpp @@ -44,13 +44,13 @@ void EngineController::deleteWorld(const std::string& name) { std::shared_ptr create_converter( Engine* engine, - const fs::path& folder, + const std::shared_ptr& worldFiles, const Content* content, const std::shared_ptr& lut, const runnable& postRunnable ) { return WorldConverter::startTask( - folder, + worldFiles, content, lut, [=]() { @@ -67,7 +67,7 @@ void show_convert_request( Engine* engine, const Content* content, const std::shared_ptr& lut, - const fs::path& folder, + const std::shared_ptr& worldFiles, const runnable& postRunnable ) { guiutil::confirm( @@ -75,7 +75,7 @@ void show_convert_request( langs::get(L"world.convert-request"), [=]() { auto converter = - create_converter(engine, folder, content, lut, postRunnable); + create_converter(engine, worldFiles, content, lut, postRunnable); menus::show_process_panel( engine, converter, L"Converting world..." ); @@ -106,13 +106,13 @@ static bool loadWorldContent(Engine* engine, const fs::path& folder) { }); } -static void loadWorld(Engine* engine, const fs::path& folder) { +static void loadWorld(Engine* engine, const std::shared_ptr& worldFiles) { try { auto content = engine->getContent(); auto& packs = engine->getContentPacks(); auto& settings = engine->getSettings(); - auto level = World::load(folder, settings, content, packs); + auto level = World::load(worldFiles, settings, content, packs); engine->setScreen( std::make_shared(engine, std::move(level)) ); @@ -133,9 +133,9 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) { } auto* content = engine->getContent(); - - std::shared_ptr lut(World::checkIndices(folder, content)); - if (lut) { + auto worldFiles = std::make_shared( + folder, engine->getSettings().debug); + if (auto lut = World::checkIndices(worldFiles, content)) { if (lut->hasMissingContent()) { engine->setScreen(std::make_shared(engine)); show_content_missing(engine, lut); @@ -145,7 +145,7 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) { engine, create_converter( engine, - folder, + worldFiles, content, lut, [=]() { openWorld(name, false); } @@ -153,14 +153,14 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) { L"Converting world..." ); } else { - show_convert_request(engine, content, lut, folder, [=]() { + show_convert_request(engine, content, lut, std::move(worldFiles), [=]() { openWorld(name, false); }); } } - } else { - loadWorld(engine, folder); + return; } + loadWorld(engine, std::move(worldFiles)); } inline uint64_t str2seed(const std::string& seedstr) { diff --git a/src/world/World.cpp b/src/world/World.cpp index 4e00183b..f8358960 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -27,7 +27,7 @@ world_load_error::world_load_error(const std::string& message) World::World( WorldInfo info, - std::unique_ptr worldFiles, + const std::shared_ptr& worldFiles, const Content* content, const std::vector& packs ) : info(std::move(info)), @@ -102,12 +102,11 @@ std::unique_ptr World::create( } std::unique_ptr World::load( - const fs::path& directory, + const std::shared_ptr& worldFilesPtr, EngineSettings& settings, const Content* content, const std::vector& packs ) { - auto worldFilesPtr = std::make_unique(directory, settings.debug); auto worldFiles = worldFilesPtr.get(); auto info = worldFiles->readWorldInfo(); if (!info.has_value()) { @@ -156,11 +155,11 @@ std::unique_ptr World::load( } std::shared_ptr World::checkIndices( - const fs::path& directory, const Content* content + const std::shared_ptr& worldFiles, const Content* content ) { - fs::path indicesFile = directory / fs::path("indices.json"); + fs::path indicesFile = worldFiles->getIndicesFile(); if (fs::is_regular_file(indicesFile)) { - return ContentLUT::create(indicesFile, content); + return ContentLUT::create(worldFiles, indicesFile, content); } return nullptr; } diff --git a/src/world/World.hpp b/src/world/World.hpp index 6556dbcc..f5443b2a 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -63,11 +63,11 @@ class World { void writeResources(const Content* content); public: - std::unique_ptr wfile; + std::shared_ptr wfile; World( WorldInfo info, - std::unique_ptr wfile, + const std::shared_ptr& worldFiles, const Content* content, const std::vector& packs ); @@ -86,7 +86,7 @@ public: /// @param content current Content instance /// @return ContentLUT if world convert required else nullptr static std::shared_ptr checkIndices( - const fs::path& directory, const Content* content + const std::shared_ptr& worldFiles, const Content* content ); /// @brief Create new world @@ -110,7 +110,7 @@ public: ); /// @brief Load an existing world - /// @param directory root world directory + /// @param worldFiles world files manager /// @param settings current engine settings /// @param content current engine Content instance /// with all world content-packs applied @@ -118,7 +118,7 @@ public: /// @return Level instance containing World instance /// @throws world_load_error on world.json load error static std::unique_ptr load( - const fs::path& directory, + const std::shared_ptr& worldFiles, EngineSettings& settings, const Content* content, const std::vector& packs