// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/engine_bridge.hpp" #include "modern/app/modern_session.hpp" #include "modern/content/world_package.hpp" namespace bmh::modern_ui { namespace { Json from_engine(const cli::Json &v) { return Json::parse(cli::dump_json(v)); } } struct EngineBridge::Impl { modern::WorldPackage package; std::unique_ptr mount; std::unique_ptr session; fs::path input; uint64_t revision = 0; Impl(const fs::path &world, const fs::path &save) : package(modern::open_world_package(world)), input(fs::weakly_canonical(save)) { if (!fs::is_regular_file(save)) throw editor::Error("Modern-Spielstand nicht gefunden."); modern::set_current_world(package.world); try { mount = std::make_unique(world, package.classic); session = std::make_unique(mount->root(), 1u, save, false); } catch (...) { modern::set_current_world(nullptr); throw; } } ~Impl() { session.reset(); mount.reset(); modern::set_current_world(nullptr); } }; EngineBridge::EngineBridge(std::unique_ptr p) : impl_(std::move(p)) {} EngineBridge::~EngineBridge() = default; std::shared_ptr EngineBridge::open(const fs::path &world, const fs::path &save) { return std::shared_ptr(new EngineBridge(std::make_unique(world, save))); } Json EngineBridge::snapshot() const { Json bundle = Json::object(); for (const auto *kind : {"session", "desktop", "camps"}) bundle[kind] = from_engine(impl_->session->ui_read(cli::Json::Object{{"kind", kind}}, impl_->revision)); validate_engine_desk(bundle); return bundle; } Json EngineBridge::command(const Json &request) { return from_engine(impl_->session->ui_command(cli::parse_json(request.dump()), impl_->revision)); } Json EngineBridge::save_copy(const fs::path &path) { if (fs::weakly_canonical(path) == impl_->input || fs::exists(path)) throw editor::Error("Bitte eine neue Datei wählen. Der geladene Spielstand wird nicht überschrieben."); const auto bundle = snapshot(); const auto slot = bundle.at("desktop").at("data").at("manager").at("slot").get(); std::string error; if (!impl_->session->save_game(path, slot, false, error)) throw editor::Error(error); return {{"messageType", "saved"}, {"path", editor::utf8(path)}}; } } // namespace bmh::modern_ui