51 lines
2.3 KiB
C++
51 lines
2.3 KiB
C++
// 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<content::Mount> mount;
|
|
std::unique_ptr<modern::app::ModernSession> 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<content::Mount>(world, package.classic);
|
|
session = std::make_unique<modern::app::ModernSession>(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<Impl> p) : impl_(std::move(p)) {}
|
|
EngineBridge::~EngineBridge() = default;
|
|
std::shared_ptr<EngineBridge> EngineBridge::open(const fs::path &world, const fs::path &save) {
|
|
return std::shared_ptr<EngineBridge>(new EngineBridge(std::make_unique<Impl>(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<int>();
|
|
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
|