Files
Dreierpack-Manager/src/modern/ui/engine_files.inc
T

81 lines
9.4 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
fs::path App::engine_files_directory() const {
return (config_.userdata.empty()?root_/"userdata":config_.userdata)/"careers"/content::sha256(world_->manifest().at("id").get<std::string>());
}
void App::engine_files_start(std::string label,std::function<EngineFilesReply()> work) {
if(engine_files_busy_||engine_busy_)return;engine_files_busy_=true;files_feedback_=std::move(label);engine_files_job_=std::async(std::launch::async,std::move(work));refresh();
}
void App::engine_files_begin(const std::string &mode) {
if(engine_files_busy_||engine_busy_)return;
if(!world_){const auto *active=mod(active_mod_);if(!active||active->type!="Modern"||!active->error.empty()){message("Bitte zuerst eine spielbare Modern-Mod aktivieren.");return;}load_world(active->file);}
reset_files();files_native_=true;files_mode_=mode;menu_=mode;files_world_=world_;files_name_="Spielstand 1";engine_files_metadata_=Json();
const auto directory=engine_files_directory(),file=world_->source();auto link=engine_link_;
engine_files_start("Spielstände werden geladen …",[directory,file,link,mode]{auto store=make_career_files(directory,file);EngineFilesReply result;result.rows=store.list();result.feedback="";result.operation="list";if(mode=="load"&&!result.rows.empty())result.selected=result.rows.front().id;if(link)result.metadata=link->file_metadata();return result;});
}
void App::engine_files_select(const std::string &id) {
files_selected_=id;files_entry_.reset();for(const auto &row:files_rows_)if(row.id==id){files_entry_=row;break;}files_feedback_.clear();files_reveal_selected_=true;focus_after_render_=id.empty()?"files-name":"files-confirm";refresh();
}
void App::engine_files_poll() {
if(!engine_files_busy_||engine_files_job_.wait_for(std::chrono::milliseconds(0))!=std::future_status::ready)return;
engine_files_busy_=false;
try {
auto result=engine_files_job_.get();
if(result.operation=="loaded") {
engine_toto_context_=Json();engine_toto_quote_=Json();engine_reports_context_=Json();engine_almanac_context_=Json();engine_friendly_read_=Json();engine_friendly_stage_=0;engine_calendar_season_.clear();engine_calendar_day_=-1;engine_move_id_.clear();report_detail_kind_.clear();engine_player_.clear();lineup_selected_.clear();engine_opening_state_=Json();engine_opening_read_=Json();
engine_mode_=true;engine_dirty_=engine_base_dirty_=false;engine_accept(std::move(*result.engine));source_.reset();entry_catalog_.reset();engine_files_current_id_=result.selected;engine_files_pending_load_.reset();scene_="engine";menu_.clear();reset_files();refresh();return;
}
files_rows_=std::move(result.rows);if(result.metadata.is_object())engine_files_metadata_=std::move(result.metadata);
if(result.operation=="saved") {
engine_accept(std::move(*result.engine));engine_files_current_id_=result.selected;
const auto pending=engine_files_pending_load_;engine_files_pending_load_.reset();menu_.clear();reset_files();
if(pending&&!closed_)engine_files_load(*pending,true);refresh();return;
}
if(result.operation=="deleted"&&engine_files_current_id_==result.selected){engine_files_current_id_.clear();if(engine_mode_)engine_dirty_=true;result.selected.clear();}
else if(result.operation=="renamed"&&engine_files_current_id_==files_selected_)engine_files_current_id_=result.selected;
if(!result.selected.empty())engine_files_select(result.selected);else {files_selected_.clear();files_entry_.reset();}
files_feedback_=std::move(result.feedback);refresh();
}catch(const std::exception &e){files_feedback_=e.what();engine_exit_after_save_=false;engine_files_pending_load_.reset();refresh();}
}
void App::engine_files_load(DemoFileEntry row,bool confirmed) {
if(engine_files_busy_||engine_busy_)return;
if(engine_mode_&&engine_dirty_&&!confirmed) {
confirmation("Die aktuelle Partie enthält ungespeicherte Änderungen. Vor dem Laden speichern?",[this,row]{engine_files_load(row,true);});popup_accept_="Ohne Speichern laden";popup_extra_=btn("files-save-before-load","Zuerst speichern","primary");engine_files_pending_load_=row;return;
}
const auto directory=engine_files_directory(),file=world_->source();auto link=engine_link_;
engine_files_start("Spielstand wird geprüft und geladen …",[directory,file,link,row]{auto store=make_career_files(directory,file);const auto current=store.read(row.id,row.token);(void)current;auto loaded=link;if(loaded)loaded->load_same_world(store.file(row.id),row.token);else {loaded=EngineBridge::open(file,store.file(row.id));if(store.read(row.id,row.token).token!=row.token)throw editor::Error("Der Spielstand wurde inzwischen geändert.");}auto bundle=loaded->snapshot();EngineFilesReply result;result.operation="loaded";result.selected=row.id;result.engine=EngineReply{loaded,bundle,{{"messageType","loaded"}},link?nullptr:Catalog::open(file)};return result;});
}
bool App::engine_files_action(const std::string &action,Rml::Element *target) {
if(!action.starts_with("files-"))return false;
if(engine_files_busy_||engine_busy_)return true;
const auto id=target?target->GetAttribute<Rml::String>("data-id",""):std::string();
try {
if(action=="files-close"){engine_exit_after_save_=false;engine_files_pending_load_.reset();menu_.clear();reset_files();refresh();return true;}
if(action=="files-new"){engine_files_select("");return true;}
if(action=="files-select"){engine_files_select(id);return true;}
const auto directory=engine_files_directory(),file=world_->source();
if(action=="files-load"){if(files_entry_&&files_entry_->valid())engine_files_load(*files_entry_);return true;}
if(action=="files-save") {
if(!engine_mode_||!engine_fresh_||!engine_files_metadata_.is_object())return true;
const auto name=files_entry_?files_entry_->name:CareerFiles::clean_name(files_name_);auto chosen=files_entry_;
if(!chosen)for(const auto &row:files_rows_)try{if(DemoFiles::name_key(row.name)==DemoFiles::name_key(name)){chosen=row;break;}}catch(...){}
auto link=engine_link_;const auto save=[this,directory,file,name,chosen,link]{engine_files_start("Spielstand wird gespeichert …",[directory,file,name,chosen,link]{auto store=make_career_files(directory,file);const auto row=store.put(name,[link](const auto &path){(void)link->save_copy(path);},chosen?chosen->id:std::string(),chosen?chosen->token:std::string());EngineFilesReply result;result.operation="saved";result.selected=row.id;result.engine=EngineReply{link,link->snapshot(),{{"messageType","saved"}},nullptr};return result;});};
if(chosen)confirmation("Spielstand „"+chosen->name+"“ überschreiben?",save);else save();return true;
}
if(action=="files-rename"&&files_entry_&&files_entry_->valid()) {
const auto row=*files_entry_;text_dialog("Neue Bezeichnung",row.name,[this,directory,file,row](const auto &name){engine_files_start("Spielstand wird umbenannt …",[directory,file,row,name]{auto store=make_career_files(directory,file);const auto renamed=store.rename(row.id,row.token,name);EngineFilesReply r;r.operation="renamed";r.selected=renamed.id;r.rows=store.list();r.feedback="Spielstand umbenannt.";return r;});});return true;
}
if(action=="files-delete"&&files_entry_&&!files_entry_->token.empty()) {
const auto row=*files_entry_;confirmation("Spielstand „"+row.name+"“ wirklich löschen?",[this,directory,file,row]{engine_files_start("Spielstand wird gelöscht …",[directory,file,row]{auto store=make_career_files(directory,file);store.remove(row.id,row.token);EngineFilesReply r;r.operation="deleted";r.selected=row.id;r.rows=store.list();r.feedback="Spielstand gelöscht.";return r;});});return true;
}
if(action=="files-import") {
auto input=config_.file_import;if(input.empty()){NFD::Guard guard;NFD::UniquePath selected;const nfdfilteritem_t filter[]={{"OpenBMH-Spielstand","obs"}};const auto result=NFD::OpenDialog(selected,filter,1);if(result==NFD_CANCEL)return true;if(result!=NFD_OKAY)throw editor::Error("Die Dateiauswahl konnte nicht geöffnet werden.");input=editor::path(selected.get());}
engine_files_start("Spielstand wird importiert …",[directory,file,input]{auto store=make_career_files(directory,file);const auto imported=store.import_file(input);EngineFilesReply r;r.operation="imported";r.selected=imported.id;r.rows=store.list();r.feedback="Spielstand importiert. Die aktive Partie bleibt unverändert.";return r;});return true;
}
if(action=="files-export"&&files_entry_&&files_entry_->valid()) {
const auto row=*files_entry_;auto output=config_.file_export;if(output.empty()){NFD::Guard guard;NFD::UniquePath selected;const nfdfilteritem_t filter[]={{"OpenBMH-Spielstand","obs"}};const auto name=row.name+".obs";const auto result=NFD::SaveDialog(selected,filter,1,nullptr,name.c_str());if(result==NFD_CANCEL)return true;if(result!=NFD_OKAY)throw editor::Error("Die Dateiauswahl konnte nicht geöffnet werden.");output=editor::path(selected.get());}
const auto copy=[this,directory,file,row,output]{engine_files_start("Spielstand wird exportiert …",[directory,file,row,output]{auto store=make_career_files(directory,file);store.export_file(row.id,row.token,output,fs::exists(output));EngineFilesReply r;r.operation="exported";r.selected=row.id;r.rows=store.list();r.feedback="Spielstand exportiert.";return r;});};if(fs::exists(output))confirmation("Vorhandene Exportdatei ersetzen?",copy);else copy();return true;
}
}catch(const std::exception &e){files_feedback_=e.what();refresh();}
return true;
}