From 30a8ddf2d367a0510595dd1bd85fbbc88d9493f4 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 1 Jan 2025 18:25:30 +0300 Subject: [PATCH] fix app.* functions & update 'world' test --- dev/tests/chunks.lua | 3 +- dev/tests/world.lua | 4 ++ src/logic/EngineController.cpp | 109 +++++++++++++++++++++------------ src/logic/EngineController.hpp | 4 ++ src/main.cpp | 2 +- 5 files changed, 81 insertions(+), 41 deletions(-) diff --git a/dev/tests/chunks.lua b/dev/tests/chunks.lua index 018493e7..fcbaeb1f 100644 --- a/dev/tests/chunks.lua +++ b/dev/tests/chunks.lua @@ -1,4 +1,4 @@ -local util = require("core:tests_util") +local util = require "core:tests_util" util.create_demo_world() app.set_setting("chunks.load-distance", 3) @@ -27,3 +27,4 @@ end player.delete(pid2) app.close_world(true) +app.delete_world("demo") diff --git a/dev/tests/world.lua b/dev/tests/world.lua index 32f56ad2..e5830f2e 100644 --- a/dev/tests/world.lua +++ b/dev/tests/world.lua @@ -1,6 +1,7 @@ -- Create/close/open/close world -- Open +app.reconfig_packs({"base"}, {}) app.new_world("demo", "2019", "core:default") assert(world.is_open()) assert(world.get_generator() == "core:default") @@ -18,6 +19,9 @@ assert(world.is_open()) assert(world.get_total_time() > 0.0) assert(world.get_seed() == 2019) app.tick() +app.reconfig_packs({}, {"base"}) +app.tick() -- Close app.close_world(true) +app.delete_world("demo") diff --git a/src/logic/EngineController.cpp b/src/logic/EngineController.cpp index 3c9fa093..7671ae55 100644 --- a/src/logic/EngineController.cpp +++ b/src/logic/EngineController.cpp @@ -7,6 +7,7 @@ #include "engine/Engine.hpp" #include "coders/commons.hpp" #include "debug/Logger.hpp" +#include "coders/json.hpp" #include "content/ContentReport.hpp" #include "files/WorldConverter.hpp" #include "files/WorldFiles.hpp" @@ -32,14 +33,21 @@ EngineController::EngineController(Engine& engine) : engine(engine) { void EngineController::deleteWorld(const std::string& name) { fs::path folder = engine.getPaths().getWorldFolderByName(name); + + auto deletion = [&]() { + logger.info() << "deleting " << folder; + fs::remove_all(folder); + }; + + if (engine.isHeadless()) { + deletion(); + return; + } guiutil::confirm( engine.getGUI()->getMenu(), langs::get(L"delete-confirm", L"world") + L" (" + util::str2wstr_utf8(folder.u8string()) + L")", - [=]() { - logger.info() << "deleting " << folder.u8string(); - fs::remove_all(folder); - } + deletion ); } @@ -63,10 +71,10 @@ std::shared_ptr create_converter( content, report, [&engine, postRunnable]() { - auto menu = engine.getGUI()->getMenu(); - menu->reset(); - menu->setPage("main", false); - engine.getGUI()->postRunnable([=]() { postRunnable(); }); + //auto menu = engine.getGUI()->getMenu(); + //menu->reset(); + //menu->setPage("main", false); + engine.postRunnable([=]() { postRunnable(); }); }, mode, true @@ -119,20 +127,6 @@ static void show_convert_request( ); } -static void show_content_missing( - Engine& engine, const std::shared_ptr& report -) { - auto root = dv::object(); - auto& contentEntries = root.list("content"); - for (auto& entry : report->getMissingContent()) { - std::string contentName = ContentType_name(entry.type); - auto& contentEntry = contentEntries.object(); - contentEntry["type"] = contentName; - contentEntry["name"] = entry.name; - } - menus::show(engine, "reports/missing_content", {std::move(root)}); -} - static bool load_world_content(Engine& engine, const fs::path& folder) { if (engine.isHeadless()) { engine.loadWorldContent(folder); @@ -163,6 +157,36 @@ static void load_world( } } +static dv::value create_missing_content_report( + const std::shared_ptr& report +) { + auto root = dv::object(); + auto& contentEntries = root.list("content"); + for (auto& entry : report->getMissingContent()) { + std::string contentName = ContentType_name(entry.type); + auto& contentEntry = contentEntries.object(); + contentEntry["type"] = contentName; + contentEntry["name"] = entry.name; + } + return root; +} + +void EngineController::onMissingContent(const std::shared_ptr& report) { + if (engine.isHeadless()) { + throw std::runtime_error( + "missing content: " + + json::stringify(create_missing_content_report(report), true) + ); + } else { + engine.setScreen(std::make_shared(engine)); + menus::show( + engine, + "reports/missing_content", + {create_missing_content_report(report)} + ); + } +} + void EngineController::openWorld(const std::string& name, bool confirmConvert) { const auto& paths = engine.getPaths(); auto folder = paths.getWorldsFolder() / fs::u8path(name); @@ -180,21 +204,23 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) { folder, engine.getSettings().debug); if (auto report = World::checkIndices(worldFiles, content)) { if (report->hasMissingContent()) { - engine.setScreen(std::make_shared(engine)); - show_content_missing(engine, report); + onMissingContent(report); } else { if (confirmConvert) { - menus::show_process_panel( + auto task = create_converter( engine, - create_converter( - engine, - worldFiles, - content, - report, - [=]() { openWorld(name, false); } - ), - L"Converting world..." + worldFiles, + content, + report, + [=]() { openWorld(name, false); } ); + if (engine.isHeadless()) { + task->waitForEnd(); + } else { + menus::show_process_panel( + engine, task, L"Converting world..." + ); + } } else { show_convert_request(engine, content, report, std::move(worldFiles), [=]() { openWorld(name, false); @@ -255,10 +281,9 @@ void EngineController::createWorld( } void EngineController::reopenWorld(World* world) { - std::string wname = world->wfile->getFolder().filename().u8string(); - engine.setScreen(nullptr); - engine.setScreen(std::make_shared(engine)); - openWorld(wname, true); + std::string name = world->wfile->getFolder().filename().u8string(); + engine.onWorldClosed(); + openWorld(name, true); } void EngineController::reconfigPacks( @@ -316,7 +341,13 @@ void EngineController::reconfigPacks( } for (const auto& id : packsToRemove) { manager.exclude(id); - names.erase(std::find(names.begin(), names.end(), id)); + const auto& found = std::find(names.begin(), names.end(), id); + if (found != names.end()) { + names.erase(found); + } else { + logger.warning() + << "attempt to remove non-installed pack: " << id; + } } wfile.removeIndices(packsToRemove); wfile.writePacks(manager.getAll(names)); @@ -324,7 +355,7 @@ void EngineController::reconfigPacks( } }; - if (hasIndices) { + if (hasIndices && !engine.isHeadless()) { guiutil::confirm( engine.getGUI()->getMenu(), langs::get(L"remove-confirm", L"pack") + L" (" + diff --git a/src/logic/EngineController.hpp b/src/logic/EngineController.hpp index 9f3631d2..948a8fce 100644 --- a/src/logic/EngineController.hpp +++ b/src/logic/EngineController.hpp @@ -2,13 +2,17 @@ #include #include +#include class Engine; class World; +class ContentReport; class LevelController; class EngineController { Engine& engine; + + void onMissingContent(const std::shared_ptr& report); public: EngineController(Engine& engine); diff --git a/src/main.cpp b/src/main.cpp index c9732ca1..b3623258 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,7 +26,7 @@ int main(int argc, char** argv) { } catch (const initialize_error& err) { logger.error() << "could not to initialize engine\n" << err.what(); } -#ifdef NDEBUG +#if defined(NDEBUG) and defined(_WIN32) catch (const std::exception& err) { logger.error() << "uncaught exception: " << err.what(); debug::Logger::flush();