From 3d3da1cdcdb5e569a119ffdea394bcd7ebf14be3 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 11 Aug 2024 16:30:58 +0300 Subject: [PATCH 1/4] refactor World --- src/files/WorldFiles.cpp | 15 +++--- src/files/WorldFiles.hpp | 6 ++- src/frontend/debug_panel.cpp | 13 ++--- src/graphics/render/WorldRenderer.cpp | 12 +++-- src/logic/scripting/lua/libworld.cpp | 14 ++--- src/world/Level.cpp | 5 +- src/world/World.cpp | 73 +++++++++++++-------------- src/world/World.hpp | 47 ++++++++++------- 8 files changed, 102 insertions(+), 83 deletions(-) diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index b85e34bf..562c40c0 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -76,7 +76,7 @@ fs::path WorldFiles::getPacksFile() const { void WorldFiles::write(const World* world, const Content* content) { if (world) { - writeWorldInfo(world); + writeWorldInfo(world->getInfo()); if (!fs::exists(getPacksFile())) { writePacks(world->getPacks()); } @@ -116,19 +116,20 @@ void WorldFiles::writeIndices(const ContentIndices* indices) { files::write_json(getIndicesFile(), &root); } -void WorldFiles::writeWorldInfo(const World* world) { - files::write_json(getWorldFile(), world->serialize().get()); +void WorldFiles::writeWorldInfo(const WorldInfo& info) { + files::write_json(getWorldFile(), info.serialize().get()); } -bool WorldFiles::readWorldInfo(World* world) { +std::optional WorldFiles::readWorldInfo() { fs::path file = getWorldFile(); if (!fs::is_regular_file(file)) { logger.warning() << "world.json does not exists"; - return false; + return std::nullopt; } auto root = files::read_json(file); - world->deserialize(root.get()); - return true; + WorldInfo info {}; + info.deserialize(root.get()); + return info; } static void read_resources_data( diff --git a/src/files/WorldFiles.hpp b/src/files/WorldFiles.hpp index 311a5feb..12753d0c 100644 --- a/src/files/WorldFiles.hpp +++ b/src/files/WorldFiles.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -20,6 +21,7 @@ class Player; class Content; class ContentIndices; class World; +struct WorldInfo; struct DebugSettings; namespace fs = std::filesystem; @@ -35,7 +37,7 @@ class WorldFiles { fs::path getIndicesFile() const; fs::path getPacksFile() const; - void writeWorldInfo(const World* world); + void writeWorldInfo(const WorldInfo& info); void writeIndices(const ContentIndices* indices); public: WorldFiles(const fs::path& directory); @@ -46,7 +48,7 @@ public: fs::path getResourcesFile() const; void createDirectories(); - bool readWorldInfo(World* world); + std::optional readWorldInfo(); bool readResourcesData(const Content* content); /// @brief Write all unsaved data to world files diff --git a/src/frontend/debug_panel.cpp b/src/frontend/debug_panel.cpp index 4b01e32f..fa827724 100644 --- a/src/frontend/debug_panel.cpp +++ b/src/frontend/debug_panel.cpp @@ -160,9 +160,10 @@ std::shared_ptr create_debug_panel( sub->add(box, glm::vec2(20, 0)); panel->add(sub); } - panel->add(create_label([=](){ + auto& worldInfo = level->getWorld()->getInfo(); + panel->add(create_label([&](){ int hour, minute, second; - timeutil::from_value(level->getWorld()->daytime, hour, minute, second); + timeutil::from_value(worldInfo.daytime, hour, minute, second); std::wstring timeString = util::lfill(std::to_wstring(hour), 2, L'0') + L":" + @@ -171,14 +172,14 @@ std::shared_ptr create_debug_panel( })); { auto bar = std::make_shared(0.0f, 1.0f, 1.0f, 0.005f, 8); - bar->setSupplier([=]() {return level->getWorld()->daytime;}); - bar->setConsumer([=](double val) {level->getWorld()->daytime = val;}); + bar->setSupplier([&]() {return worldInfo.daytime;}); + bar->setConsumer([&](double val) {worldInfo.daytime = val;}); panel->add(bar); } { auto bar = std::make_shared(0.0f, 1.0f, 0.0f, 0.005f, 8); - bar->setSupplier([=]() {return level->getWorld()->fog;}); - bar->setConsumer([=](double val) {level->getWorld()->fog = val;}); + bar->setSupplier([&]() {return worldInfo.fog;}); + bar->setConsumer([&](double val) {worldInfo.fog = val;}); panel->add(bar); } { diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index b8eb5011..a293ae2f 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -161,7 +161,7 @@ void WorldRenderer::setupWorldShader( shader->uniform1f("u_gamma", settings.graphics.gamma.get()); shader->uniform1f("u_fogFactor", fogFactor); shader->uniform1f("u_fogCurve", settings.graphics.fogCurve.get()); - shader->uniform1f("u_dayTime", level->getWorld()->daytime); + shader->uniform1f("u_dayTime", level->getWorld()->getInfo().daytime); shader->uniform3f("u_cameraPos", camera->position); shader->uniform1i("u_cubemap", 1); @@ -338,8 +338,10 @@ void WorldRenderer::draw( const Viewport& vp = pctx.getViewport(); camera->aspect = vp.getWidth() / static_cast(vp.getHeight()); - const EngineSettings& settings = engine->getSettings(); - skybox->refresh(pctx, world->daytime, 1.0f + world->fog * 2.0f, 4); + const auto& settings = engine->getSettings(); + const auto& worldInfo = world->getInfo(); + + skybox->refresh(pctx, worldInfo.daytime, 1.0f + worldInfo.fog * 2.0f, 4); auto assets = engine->getAssets(); auto linesShader = assets->get("lines"); @@ -352,7 +354,7 @@ void WorldRenderer::draw( Window::clearDepth(); // Drawing background sky plane - skybox->draw(pctx, camera, assets, world->daytime, world->fog); + skybox->draw(pctx, camera, assets, worldInfo.daytime, worldInfo.fog); // Actually world render with depth buffer on { @@ -375,7 +377,7 @@ void WorldRenderer::draw( auto screenShader = assets->get("screen"); screenShader->use(); screenShader->uniform1f("u_timer", timer); - screenShader->uniform1f("u_dayTime", level->getWorld()->daytime); + screenShader->uniform1f("u_dayTime", worldInfo.daytime); postProcessing->render(pctx, screenShader); } diff --git a/src/logic/scripting/lua/libworld.cpp b/src/logic/scripting/lua/libworld.cpp index ab87ee1c..168edb28 100644 --- a/src/logic/scripting/lua/libworld.cpp +++ b/src/logic/scripting/lua/libworld.cpp @@ -42,27 +42,27 @@ static int l_world_get_list(lua::State* L) { } static int l_world_get_total_time(lua::State* L) { - return lua::pushnumber(L, level->getWorld()->totalTime); + return lua::pushnumber(L, level->getWorld()->getInfo().totalTime); } static int l_world_get_day_time(lua::State* L) { - return lua::pushnumber(L, level->getWorld()->daytime); + return lua::pushnumber(L, level->getWorld()->getInfo().daytime); } static int l_world_set_day_time(lua::State* L) { auto value = lua::tonumber(L, 1); - level->getWorld()->daytime = fmod(value, 1.0); + level->getWorld()->getInfo().daytime = std::fmod(value, 1.0); return 0; } static int l_world_set_day_time_speed(lua::State* L) { auto value = lua::tonumber(L, 1); - level->getWorld()->daytimeSpeed = std::abs(value); + level->getWorld()->getInfo().daytimeSpeed = std::abs(value); return 0; } static int l_world_get_day_time_speed(lua::State* L) { - return lua::pushnumber(L, level->getWorld()->daytimeSpeed); + return lua::pushnumber(L, level->getWorld()->getInfo().daytimeSpeed); } static int l_world_get_seed(lua::State* L) { @@ -76,12 +76,12 @@ static int l_world_exists(lua::State* L) { } static int l_world_is_day(lua::State* L) { - auto daytime = level->getWorld()->daytime; + auto daytime = level->getWorld()->getInfo().daytime; return lua::pushboolean(L, daytime >= 0.2 && daytime <= 0.8); } static int l_world_is_night(lua::State* L) { - auto daytime = level->getWorld()->daytime; + auto daytime = level->getWorld()->getInfo().daytime; return lua::pushboolean(L, daytime < 0.2 || daytime > 0.8); } diff --git a/src/world/Level.cpp b/src/world/Level.cpp index 3c8e9dba..6d9b1f03 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -29,6 +29,7 @@ Level::Level( events(std::make_unique()), entities(std::make_unique(this)), settings(settings) { + auto& worldInfo = world->getInfo(); auto& cameraIndices = content->getIndices(ResourceType::CAMERA); for (size_t i = 0; i < cameraIndices.size(); i++) { auto camera = std::make_shared(); @@ -46,8 +47,8 @@ Level::Level( cameras.push_back(std::move(camera)); } - if (world->nextEntityId) { - entities->setNextID(world->nextEntityId); + if (worldInfo.nextEntityId) { + entities->setNextID(worldInfo.nextEntityId); } auto inv = std::make_shared( world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE diff --git a/src/world/World.cpp b/src/world/World.cpp index e15837b8..4e00183b 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -26,29 +26,22 @@ world_load_error::world_load_error(const std::string& message) } World::World( - std::string name, - std::string generator, - const fs::path& directory, - uint64_t seed, - EngineSettings& settings, + WorldInfo info, + std::unique_ptr worldFiles, const Content* content, const std::vector& packs -) - : name(std::move(name)), - generator(std::move(generator)), - seed(seed), - content(content), - packs(packs), - wfile(std::make_unique(directory, settings.debug)) { -} +) : info(std::move(info)), + content(content), + packs(packs), + wfile(std::move(worldFiles)) {} World::~World() { } void World::updateTimers(float delta) { - daytime += delta * daytimeSpeed * DAYIME_SPECIFIC_SPEED; - daytime = fmod(daytime, 1.0f); - totalTime += delta; + info.daytime += delta * info.daytimeSpeed * DAYIME_SPECIFIC_SPEED; + info.daytime = std::fmod(info.daytime, 1.0f); + info.totalTime += delta; } void World::writeResources(const Content* content) { @@ -71,7 +64,7 @@ void World::writeResources(const Content* content) { void World::write(Level* level) { const Content* content = level->content; level->chunks->saveAll(); - nextEntityId = level->entities->peekNextID(); + info.nextEntityId = level->entities->peekNextID(); wfile->write(this, content); auto playerFile = dynamic::Map(); @@ -95,8 +88,15 @@ std::unique_ptr World::create( const Content* content, const std::vector& packs ) { + WorldInfo info {}; + info.name = name; + info.generator = generator; + info.seed = seed; auto world = std::make_unique( - name, generator, directory, seed, settings, content, packs + info, + std::make_unique(directory, settings.debug), + content, + packs ); return std::make_unique(std::move(world), content, settings); } @@ -107,20 +107,21 @@ std::unique_ptr World::load( 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()) { + throw world_load_error("could not to find world.json"); + } + logger.info() << "world version: " << info->major << "." << info->minor; + auto world = std::make_unique( - ".", - WorldGenerators::getDefaultGeneratorID(), - directory, - 0, - settings, + info.value(), + std::move(worldFilesPtr), content, packs ); auto& wfile = world->wfile; - - if (!wfile->readWorldInfo(world.get())) { - throw world_load_error("could not to find world.json"); - } wfile->readResourcesData(content); auto level = std::make_unique(std::move(world), content, settings); @@ -165,11 +166,11 @@ std::shared_ptr World::checkIndices( } void World::setName(const std::string& name) { - this->name = name; + this->info.name = name; } void World::setGenerator(const std::string& generator) { - this->generator = generator; + this->info.generator = generator; } bool World::hasPack(const std::string& id) const { @@ -180,26 +181,26 @@ bool World::hasPack(const std::string& id) const { } void World::setSeed(uint64_t seed) { - this->seed = seed; + this->info.seed = seed; } std::string World::getName() const { - return name; + return info.name; } uint64_t World::getSeed() const { - return seed; + return info.seed; } std::string World::getGenerator() const { - return generator; + return info.generator; } const std::vector& World::getPacks() const { return packs; } -void World::deserialize(dynamic::Map* root) { +void WorldInfo::deserialize(dynamic::Map* root) { name = root->get("name", name); generator = root->get("generator", generator); seed = root->get("seed", seed); @@ -208,10 +209,8 @@ void World::deserialize(dynamic::Map* root) { generator = WorldGenerators::getDefaultGeneratorID(); } if (auto verobj = root->map("version")) { - int major = 0, minor = -1; verobj->num("major", major); verobj->num("minor", minor); - logger.info() << "world version: " << major << "." << minor; } if (auto timeobj = root->map("time")) { timeobj->num("day-time", daytime); @@ -225,7 +224,7 @@ void World::deserialize(dynamic::Map* root) { nextEntityId = root->get("next-entity-id", 1); } -std::unique_ptr World::serialize() const { +std::unique_ptr WorldInfo::serialize() const { auto root = std::make_unique(); auto& versionobj = root->putMap("version"); diff --git a/src/world/World.hpp b/src/world/World.hpp index 7f30e52c..6556dbcc 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -24,20 +24,12 @@ public: world_load_error(const std::string& message); }; -/// @brief holds all world data except the level (chunks and objects) -class World : Serializable { +struct WorldInfo : public Serializable { std::string name; std::string generator; uint64_t seed; - const Content* const content; - std::vector packs; - int64_t nextInventoryId = 0; - void writeResources(const Content* content); -public: - std::unique_ptr wfile; - /// @brief Day/night loop timer in range 0..1 where /// 0.0 - is midnight and /// 0.5 - is noon @@ -54,12 +46,28 @@ public: entityid_t nextEntityId = 0; + int major = 0, minor = -1; + + std::unique_ptr serialize() const override; + void deserialize(dynamic::Map* src) override; +}; + +/// @brief holds all world data except the level (chunks and objects) +class World { + WorldInfo info {}; + + const Content* const content; + std::vector packs; + + int64_t nextInventoryId = 0; + + void writeResources(const Content* content); +public: + std::unique_ptr wfile; + World( - std::string name, - std::string generator, - const fs::path& directory, - uint64_t seed, - EngineSettings& settings, + WorldInfo info, + std::unique_ptr wfile, const Content* content, const std::vector& packs ); @@ -134,6 +142,14 @@ public: /// @brief Get world generator id std::string getGenerator() const; + WorldInfo& getInfo() { + return info; + } + + const WorldInfo& getInfo() const { + return info; + } + /// @brief Get vector of all content-packs installed in world const std::vector& getPacks() const; @@ -147,7 +163,4 @@ public: const Content* getContent() const { return content; } - - std::unique_ptr serialize() const override; - void deserialize(dynamic::Map* src) override; }; From 4f2448daed4d2e1a7542d2eeb773b9895c3213a7 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 11 Aug 2024 18:21:12 +0300 Subject: [PATCH 2/4] 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 From 3adf97b1b874dd02be9c3368d0473ff923a0ae3b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 11 Aug 2024 18:40:35 +0300 Subject: [PATCH 3/4] fix WorldFiles forward declaration --- src/content/ContentLUT.cpp | 3 ++- src/content/ContentLUT.hpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/content/ContentLUT.cpp b/src/content/ContentLUT.cpp index b39dc21b..8e670152 100644 --- a/src/content/ContentLUT.cpp +++ b/src/content/ContentLUT.cpp @@ -27,7 +27,8 @@ static constexpr size_t get_entries_count( std::shared_ptr ContentLUT::create( const std::shared_ptr& worldFiles, - const fs::path& filename, const Content* content + const fs::path& filename, + const Content* content ) { auto worldInfo = worldFiles->readWorldInfo(); if (!worldInfo.has_value()) { diff --git a/src/content/ContentLUT.hpp b/src/content/ContentLUT.hpp index 83c15415..f0b92c07 100644 --- a/src/content/ContentLUT.hpp +++ b/src/content/ContentLUT.hpp @@ -17,7 +17,7 @@ struct contententry { std::string name; }; -struct WorldFiles; +class WorldFiles; template class ContentUnitLUT { From e52f08f7de2b93d80eb2c058fb5d78ad7d798e5c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 11 Aug 2024 18:59:21 +0300 Subject: [PATCH 4/4] fix entities clones --- src/voxels/Chunks.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 417ba786..86311359 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -743,15 +743,14 @@ void Chunks::save(Chunk* chunk) { ) ); auto entities = level->entities->getAllInside(aabb); - dynamic::Map_sptr root = nullptr; + auto root = dynamic::create_map(); + root->put("data", level->entities->serialize(entities)); if (!entities.empty()) { - root = dynamic::create_map(); - root->put("data", level->entities->serialize(entities)); level->entities->despawn(std::move(entities)); chunk->flags.entities = true; } worldFiles->getRegions().put( - chunk, root ? json::to_binary(root, true) : std::vector() + chunk, json::to_binary(root, true) ); } }