// 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" #include "modern/content/ui_contract.hpp" #include "modern/content/world_save.hpp" #include "modern/content/world_expand.hpp" #include "modern/ui/model.hpp" #include "modern/ui/career_files.hpp" #include "content/digest.hpp" #include namespace bmh::modern_ui { namespace { Json from_engine(const cli::Json &v) { return Json::parse(cli::dump_json(v)); } } namespace { Json session_snapshot(modern::app::ModernSession &session,uint64_t revision) { Json bundle=Json::object(); for(const auto *kind:{"session","desktop","camps","squad","lineup","tactics","training","bank","stadium","credit","environment","advertising","contracts","friendly-plan","toto","transfer","achievements","news","balance","desktop-widgets","indoor-standings","settings","manager-menu"})bundle[kind]=from_engine(session.ui_read(cli::Json::Object{{"kind",kind}},revision)); validate_engine_desk(bundle);const auto &season=bundle.at("desktop").at("meta").at("season"); bundle["calendar"]=from_engine(session.ui_read(cli::Json::Object{{"kind","calendar"},{"from",season.at("startDate").get()},{"until",season.at("endDate").get()}},revision)); bundle["scheduling"]=from_engine(session.ui_read(cli::Json::Object{{"kind","scheduling"}},revision));return bundle; } } 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(const fs::path &world,const Json &setup,const Json &managers,uint32_t seed) : package(modern::open_world_package(world)) { const auto integer=[&](const char* key,int low,int high){const auto &v=setup.at(key);if(!v.is_number_integer()||v.get()()>high)throw editor::Error("Ungültige Grundeinstellung: "+std::string(key));return v.get();}; ui::SetupOptions options;options.mode=integer("mode",3,3);options.duration=integer("duration",0,5);options.difficulty=integer("difficulty",0,9);options.managers=integer("players",1,4); if(!setup.at("league").is_boolean()||!setup.at("world").is_boolean())throw editor::Error("Ungültige Wettbewerbswahl.");options.league=setup.at("league");options.world=setup.at("world"); if((!options.league&&!options.world)||!managers.is_array()||managers.size()!=size_t(options.managers))throw editor::Error("Die Manager- oder Wettbewerbswahl ist unvollständig."); std::vector choices;std::vector names,styles;std::set clubs;std::set portraits; for(const auto &m:managers) { if(!m.at("name").is_string()||!m.at("club").is_string())throw editor::Error("Managername oder Verein fehlt.");const auto name=normalized_manager_name(m.at("name").get());if(!name)throw editor::Error("Bitte einen gültigen Managernamen angeben."); const auto id=m.at("club").get();const auto *club=package.world->club(id);const modern::WorldLeague *league=nullptr; if(club)for(const auto &l:package.world->leagues)if(l.id==club->competition)league=&l; if(!club||!league||league->index<0||league->index>4||club->legacy_index<0||!clubs.insert(id).second)throw editor::Error("Dieser Verein ist für die Managerwahl nicht verfügbar."); const auto &portrait=m.at("portrait"),&strength=m.at("strength_choice");if(!portrait.is_number_integer()||portrait.get()<0||portrait.get()>11||!portraits.insert(portrait.get()).second||!strength.is_number_integer()||strength.get()<0||strength.get()>2)throw editor::Error("Ungültige Managerdarstellung oder Startstärke."); const auto style=m.value("portrait_style",std::string("photo"));if(style!="photo"&&style!="painted")throw editor::Error("Ungültige Porträtart."); ui::ManagerChoice choice;choice.slot=int(choices.size());choice.master_club=club->legacy_index;choice.start_league=league->index;choice.strength=strength;choice.portrait=portrait; try {choice.name=ui::utf8_to_original(*name);}catch(const std::exception&){choice.name="Manager "+std::to_string(choice.slot+1);} if(choice.name.empty()||choice.name.size()>24)choice.name="Manager "+std::to_string(choice.slot+1); choices.push_back(choice);names.push_back(*name);styles.push_back(style); } modern::set_current_world(package.world); try { mount=std::make_unique(world,package.classic);session=std::make_unique(mount->root(),seed);std::string error; for(const auto &choice:choices)if(!session->confirm_manager(options,choice,error))throw editor::Error("Die Managerwahl wurde abgewiesen: "+error); auto &state=session->state();modern::ensure_ui_identities(state); for(size_t i=0;ifinish(result,error))throw editor::Error("Die Partie konnte nicht gestartet werden: "+error); for(size_t i=0;iui_read(cli::Json::Object{{"kind","session"}},revision));const auto &all=status.at("data").at("managers");if(all.at(i).at("teamId")!=managers.at(i).at("club"))throw editor::Error("Die Engine hat einen anderen Verein zugeordnet.");} }catch(...){session.reset();mount.reset();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))); } std::shared_ptr EngineBridge::create(const fs::path &world,const Json &setup,const Json &managers,uint32_t seed) { if(modern::current_world())throw editor::Error("Bitte die laufende Partie beenden, bevor eine neue begonnen wird."); return std::shared_ptr(new EngineBridge(std::make_unique(world,setup,managers,seed))); } Json EngineBridge::snapshot() const {return session_snapshot(*impl_->session,impl_->revision);} Json EngineBridge::command(const Json &request) { return from_engine(impl_->session->ui_command(cli::parse_json(request.dump()), impl_->revision)); } Json EngineBridge::read(const Json &request) const { return from_engine(impl_->session->ui_read(cli::parse_json(request.dump()), impl_->revision)); } Json EngineBridge::read_live(const Json &request) const { return from_engine(impl_->session->read_live(cli::parse_json(request.dump()))); } CareerFiles make_career_files(const fs::path &directory,const fs::path &world) { const auto model=modern::open_world_package(world).world; return CareerFiles(directory,[model](const fs::path &file){ try{return from_engine(modern::read_world_career_metadata(file,*model));} catch(const std::exception &e){const std::string reason=e.what();if(reason.starts_with("the save belongs to another world"))throw editor::Error("Dieser Spielstand gehört zu einer anderen Mod oder Datensatzversion.");throw editor::Error("Dieser Spielstand ist beschädigt oder wird nicht unterstützt.");} }); } Json EngineBridge::file_metadata() const { const auto reply=read({{"kind","file-metadata"}}); if(reply.value("messageType","")!="snapshot")throw editor::Error("Spielstandangaben nicht verfügbar."); return reply.at("data"); } Json EngineBridge::load_same_world(const fs::path &path,const std::string &expected_token) { const auto check=[&]{if(!expected_token.empty()&&content::sha256(editor::read_file(path,256*1024*1024))!=expected_token)throw editor::Error("Dieser Spielstand wurde inzwischen geändert. Bitte erneut auswählen.");}; check();(void)modern::load_world_career(path,*impl_->package.world); try { auto staged=std::make_unique(impl_->mount->root(),1u,path,false); (void)session_snapshot(*staged,0);check(); impl_->session=std::move(staged);impl_->input=fs::weakly_canonical(path);impl_->revision=0; }catch(...){modern::install_world_dimensions(*impl_->package.world);throw;} return {{"messageType","loaded"}}; } 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 &manager = bundle.at("desktop").at("data").at("manager"); const auto slot = manager.value("runtimeSlot", 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