diff --git a/res/layouts/reports/missing_content.xml b/res/layouts/reports/missing_content.xml index cd8cacb4f..36cbd78e0 100644 --- a/res/layouts/reports/missing_content.xml +++ b/res/layouts/reports/missing_content.xml @@ -3,5 +3,6 @@ + diff --git a/res/layouts/reports/missing_content.xml.lua b/res/layouts/reports/missing_content.xml.lua index 8ea78a1c1..50ac29c2d 100644 --- a/res/layouts/reports/missing_content.xml.lua +++ b/res/layouts/reports/missing_content.xml.lua @@ -1,4 +1,18 @@ +content_report = nil + +function delete_content() + local units = {} + for i, entry in ipairs(content_report.content) do + table.insert(units, entry.name) + end + local request = { + units = units + } + app.delete_world_content(content_report.world, request, true) +end + function on_open(report) + content_report = report for i, entry in ipairs(report.content) do document.content_panel:add(gui.template("content_entry", entry)) end diff --git a/res/texts/en_US.txt b/res/texts/en_US.txt index 294758a7e..f2312fb34 100644 --- a/res/texts/en_US.txt +++ b/res/texts/en_US.txt @@ -11,6 +11,7 @@ error.dependency-version-not-met=Dependency pack version is not met. world.delete-confirm=Do you want to delete world forever? world.generators.default=Default world.generators.flat=Flat +world.delete-content-confirm=Delete missing content from the world __**(irreversible)**__? editor.info.tooltip=CTRL+S - Save\nCTRL+R - Run\nCTRL+Z - Undo\nCTRL+Y - Redo devtools.traceback=Traceback (most recent call first) diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index ab06b42dc..66431f92b 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -79,7 +79,9 @@ UiDocument* menus::show( auto documentPtr = UiDocument::read( engine.getGUI(), - scripting::get_root_environment(), + scripting::create_doc_environment( + scripting::get_root_environment(), fullname, true + ), fullname, file, "core:layouts/" + name diff --git a/src/graphics/ui/gui_util.cpp b/src/graphics/ui/gui_util.cpp index 13364b0ed..86c28864c 100644 --- a/src/graphics/ui/gui_util.cpp +++ b/src/graphics/ui/gui_util.cpp @@ -125,6 +125,7 @@ void guiutil::confirm( label->setSize(glm::vec2(600, 50)); label->setMultiline(true); label->setTextWrapping(true); + label->setMarkup("md"); panel->add(label); auto subpanel = std::make_shared(gui, glm::vec2(600, 53)); diff --git a/src/logic/EngineController.cpp b/src/logic/EngineController.cpp index 51b32e0c4..4d62f6cec 100644 --- a/src/logic/EngineController.cpp +++ b/src/logic/EngineController.cpp @@ -55,6 +55,32 @@ void EngineController::deleteWorld(const std::string& name) { ); } +void EngineController::deleteWorldContent( + const std::string& name, ContentRemovalRequest request, bool loadWorld +) { + io::path folder = engine.getPaths().getWorldFolderByName(name); + auto deletion = [this, folder, loadWorld, request=std::move(request)]() { + logger.info() << "deleting content from the world"; + WorldFiles worldFiles(folder); + worldFiles.deleteContent(std::move(request)); + if (loadWorld) { + openWorld(folder.name(), true); + } + }; + + if (engine.isHeadless()) { + deletion(); + return; + } + + guiutil::confirm( + engine, + langs::get(L"delete-content-confirm", L"world") + L" (" + + util::str2wstr_utf8(folder.string()) + L")", + deletion + ); +} + std::shared_ptr create_converter( Engine& engine, const std::shared_ptr& worldFiles, @@ -174,20 +200,20 @@ static dv::value create_missing_content_report( } void EngineController::onMissingContent( + const std::string& worldName, const std::shared_ptr& report ) { + auto fullReport = create_missing_content_report(report); + fullReport["world"] = worldName; + if (engine.isHeadless()) { throw std::runtime_error( "missing content: " + - util::quote(json::stringify(create_missing_content_report(report), true, " ")) + util::quote(json::stringify(std::move(fullReport), true, " ")) ); } else { engine.setScreen(std::make_shared(engine)); - menus::show( - engine, - "reports/missing_content", - {create_missing_content_report(report)} - ); + menus::show(engine, "reports/missing_content", {std::move(fullReport)}); } } @@ -234,7 +260,7 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) { return; } if (report->hasMissingContent()) { - onMissingContent(report); + onMissingContent(name, report); return; } diff --git a/src/logic/EngineController.hpp b/src/logic/EngineController.hpp index a40f3476f..147ba482f 100644 --- a/src/logic/EngineController.hpp +++ b/src/logic/EngineController.hpp @@ -9,12 +9,16 @@ class Engine; class World; class ContentReport; class LevelController; +struct ContentRemovalRequest; class EngineController { Engine& engine; int64_t localPlayer = -1; - void onMissingContent(const std::shared_ptr& report); + void onMissingContent( + const std::string& worldName, + const std::shared_ptr& report + ); public: EngineController(Engine& engine); @@ -27,6 +31,10 @@ public: /// @param name world name void deleteWorld(const std::string& name); + void deleteWorldContent( + const std::string& name, ContentRemovalRequest request, bool loadWorld + ); + void reconfigPacks( LevelController* controller, const std::vector& packsToAdd, diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index d8a63c720..0cb38a808 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -19,6 +19,7 @@ #include "util/stringutil.hpp" #include "window/Window.hpp" #include "world/Level.hpp" +#include "world/files/WorldFiles.hpp" #include #include @@ -327,6 +328,35 @@ static int l_delete_world(lua::State* L) { return 0; } +static ContentRemovalRequest parse_content_removal_request(lua::State* L, int idx) { + ContentRemovalRequest request; + if (!lua::istable(L, idx)) { + throw std::runtime_error("table expected as argument"); + } + if (!lua::getfield(L, "units", idx)) { + throw std::runtime_error("table with 'units' field expected"); + } + if (!lua::istable(L, -1)) { + throw std::runtime_error("'units' field must be a table"); + } + int len = lua::objlen(L, -1); + for (int i = 0; i < len; i++) { + lua::rawgeti(L, i + 1); + request.units.emplace_back(lua::require_lstring(L, -1)); + lua::pop(L); + } + return request; +} + +static int l_delete_world_content(lua::State* L) { + auto name = lua::require_string(L, 1); + auto controller = engine->getController(); + auto request = parse_content_removal_request(L, 2); + bool loadWorld = lua::toboolean(L, 3); + controller->deleteWorldContent(name, request, loadWorld); + return 0; +} + /// @brief Get engine version /// @return major, minor, [patch] static int l_get_version(lua::State* L) { @@ -538,6 +568,7 @@ const luaL_Reg applib[] = { {"save_world", lua::wrap}, {"close_world", lua::wrap}, {"delete_world", lua::wrap}, + {"delete_world_content", lua::wrap}, /// other {"get_version", lua::wrap}, {"create_memory_device", lua::wrap}, diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 6bca06cfa..64037775b 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -216,7 +216,7 @@ std::unique_ptr scripting::start_app_script(const io::path& script) { } [[nodiscard]] scriptenv scripting::create_doc_environment( - const scriptenv& parent, const std::string& name + const scriptenv& parent, const std::string& name, bool app ) { auto L = lua::get_main_state(); int id = lua::create_environment(L, *parent); @@ -226,6 +226,12 @@ std::unique_ptr scripting::start_app_script(const io::path& script) { lua::pushstring(L, name); lua::setfield(L, "DOC_NAME"); + if (app) { + if (lua::getregistry(L, "app")) { + lua::setfield(L, "app"); + } + } + if (lua::getglobal(L, "Document")) { if (lua::getfield(L, "new")) { lua::pushstring(L, name); diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index b4c00a622..65f8836e9 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -65,7 +65,7 @@ namespace scripting { scriptenv create_pack_environment(const ContentPack& pack); scriptenv create_environment(const scriptenv& parent); scriptenv create_doc_environment( - const scriptenv& parent, const std::string& name + const scriptenv& parent, const std::string& name, bool app = false ); void process_post_runnables(); diff --git a/src/world/files/WorldFiles.cpp b/src/world/files/WorldFiles.cpp index 8e471772a..c06df4160 100644 --- a/src/world/files/WorldFiles.cpp +++ b/src/world/files/WorldFiles.cpp @@ -235,3 +235,23 @@ void WorldFiles::removeIndices(const std::vector& packs) { io::path WorldFiles::getFolder() const { return directory; } + +static void replace_all( + dv::value& list, const std::string& unit, const std::string& placeholder +) { + for (uint i = 0; i < list.size(); i++) { + auto& name = list[i].asString(); + if (name == unit) { + list[i] = placeholder; + } + } +} + +void WorldFiles::deleteContent(const ContentRemovalRequest& request) { + auto root = io::read_json(getIndicesFile()); + for (const auto& unit : request.units) { + replace_all(root["blocks"], unit, CORE_AIR); + replace_all(root["items"], unit, CORE_EMPTY); + } + io::write_json(getIndicesFile(), root); +} diff --git a/src/world/files/WorldFiles.hpp b/src/world/files/WorldFiles.hpp index f14c3c33c..31f859e2d 100644 --- a/src/world/files/WorldFiles.hpp +++ b/src/world/files/WorldFiles.hpp @@ -23,6 +23,10 @@ class World; struct WorldInfo; struct DebugSettings; +struct ContentRemovalRequest { + std::vector units; +}; + class WorldFiles { io::path directory; WorldRegions regions; @@ -77,5 +81,7 @@ public: return doWriteLights; } + void deleteContent(const ContentRemovalRequest& request); + static const inline std::string WORLD_FILE = "world.json"; };