feat: new config format
This commit is contained in:
140
rust/src/cmd.rs
140
rust/src/cmd.rs
@ -2,39 +2,42 @@ use log;
|
||||
use std::collections::HashMap;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::fs;
|
||||
|
||||
use tauri::{AppHandle, Manager, State};
|
||||
use crate::model::misc::Game;
|
||||
use crate::pkg::{Package, PkgKey};
|
||||
use crate::pkg_store::InstallResult;
|
||||
use crate::profile::Profile;
|
||||
use crate::profiles::ongeki::OngekiProfile;
|
||||
use crate::profiles::{AnyProfile, Profile, ProfileMeta, ProfilePaths};
|
||||
use crate::appdata::AppData;
|
||||
use crate::{liner, start, util};
|
||||
|
||||
use tauri::{AppHandle, Manager, State};
|
||||
use crate::util;
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn startline(app: AppHandle) -> Result<(), String> {
|
||||
log::debug!("invoke: startline");
|
||||
|
||||
let app_copy = app.clone();
|
||||
let state = app.state::<Mutex<AppData>>();
|
||||
let appd = state.lock().await;
|
||||
let mut hash = "".to_owned();
|
||||
|
||||
let mut appd = state.lock().await;
|
||||
if let Some(p) = &appd.profile {
|
||||
let hash = appd.sum_packages(p);
|
||||
liner::line_up(p, hash).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
hash = appd.sum_packages(p);
|
||||
}
|
||||
if let Some(p) = &mut appd.profile {
|
||||
p.line_up(app.clone(), hash).await
|
||||
.map_err(|e| format!("Lineup failed:\n{}", e))?;
|
||||
p.start(app.clone()).await
|
||||
.map_err(|e| format!("Startup failed:\n{}", e))?;
|
||||
|
||||
start::start(p, app_copy).await
|
||||
.map_err(|e| e.to_string())
|
||||
Ok(())
|
||||
} else {
|
||||
Err("No profile".to_owned())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn kill() -> Result<(), String> {
|
||||
start::pkill("amdaemon.exe").await;
|
||||
util::pkill("amdaemon.exe").await;
|
||||
// The start routine will kill the other process
|
||||
|
||||
Ok(())
|
||||
@ -108,13 +111,35 @@ pub async fn fetch_listings(state: State<'_, Mutex<AppData>>) -> Result<(), Stri
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn list_profiles() -> Result<Vec<(Game, String)>, String> {
|
||||
pub async fn list_profiles() -> Result<Vec<ProfileMeta>, String> {
|
||||
log::debug!("invoke: list_profiles");
|
||||
|
||||
let list = Profile::list().await.map_err(|e| e.to_string())?;
|
||||
let list = crate::profiles::list_profiles().await.map_err(|e| e.to_string())?;
|
||||
Ok(list)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn init_profile(
|
||||
state: State<'_, Mutex<AppData>>,
|
||||
game: Game,
|
||||
name: String
|
||||
) -> Result<(), String> {
|
||||
log::debug!("invoke: init_profile({}, {})", game, name);
|
||||
|
||||
let mut appd = state.lock().await;
|
||||
let new_profile = OngekiProfile::new(name)
|
||||
.map_err(|e| format!("Unable to create profile: {}", e))?;
|
||||
|
||||
fs::create_dir_all(new_profile.config_dir()).await
|
||||
.map_err(|e| format!("Unable to create the profile config directory: {}", e))?;
|
||||
fs::create_dir_all(new_profile.data_dir()).await
|
||||
.map_err(|e| format!("Unable to create the profile data directory: {}", e))?;
|
||||
|
||||
appd.profile = Some(AnyProfile::OngekiProfile(new_profile.clone()));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn load_profile(state: State<'_, Mutex<AppData>>, game: Game, name: String) -> Result<(), String> {
|
||||
log::debug!("invoke: load_profile({} {:?})", game, name);
|
||||
@ -125,7 +150,41 @@ pub async fn load_profile(state: State<'_, Mutex<AppData>>, game: Game, name: St
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_current_profile(state: State<'_, Mutex<AppData>>) -> Result<Option<Profile>, ()> {
|
||||
pub async fn rename_profile(
|
||||
state: State<'_, Mutex<AppData>>,
|
||||
profile: ProfileMeta,
|
||||
name: String
|
||||
) -> Result<(), String> {
|
||||
log::debug!("invoke: rename_profile({:?} {:?})", profile, name);
|
||||
|
||||
let new_meta = ProfileMeta {
|
||||
game: profile.game.clone(),
|
||||
name: name.clone()
|
||||
};
|
||||
|
||||
if new_meta.config_dir().exists() {
|
||||
return Err(format!("Profile {} already exists", &name));
|
||||
}
|
||||
|
||||
fs::rename(profile.config_dir(), new_meta.config_dir()).await
|
||||
.map_err(|e| format!("Unable to rename: {}", e))?;
|
||||
|
||||
if let Err(e) = fs::rename(profile.data_dir(), new_meta.data_dir()).await {
|
||||
log::warn!("Unable to move data dir {}->{}: {}", &profile.name, &name, e);
|
||||
}
|
||||
|
||||
let mut appd = state.lock().await;
|
||||
if let Some(current) = &mut appd.profile {
|
||||
if current.meta() == profile {
|
||||
current.rename(name);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_current_profile(state: State<'_, Mutex<AppData>>) -> Result<Option<AnyProfile>, ()> {
|
||||
log::debug!("invoke: get_current_profile");
|
||||
|
||||
let appd = state.lock().await;
|
||||
@ -133,41 +192,16 @@ pub async fn get_current_profile(state: State<'_, Mutex<AppData>>) -> Result<Opt
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn save_current_profile(state: State<'_, Mutex<AppData>>) -> Result<(), ()> {
|
||||
pub async fn save_current_profile(state: State<'_, Mutex<AppData>>, profile: AnyProfile) -> Result<(), String> {
|
||||
log::debug!("invoke: save_current_profile");
|
||||
|
||||
let appd = state.lock().await;
|
||||
if let Some(p) = &appd.profile {
|
||||
p.save().await;
|
||||
} else {
|
||||
log::warn!("No profile to save");
|
||||
}
|
||||
let mut appd = state.lock().await;
|
||||
profile.save().map_err(|e| e.to_string())?;
|
||||
appd.profile = Some(profile);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn init_profile(
|
||||
state: State<'_, Mutex<AppData>>,
|
||||
game: Game,
|
||||
name: String
|
||||
) -> Result<Profile, String> {
|
||||
log::debug!("invoke: init_profile({}, {})", game, name);
|
||||
|
||||
let mut appd = state.lock().await;
|
||||
let new_profile = Profile::new(game, name);
|
||||
|
||||
fs::create_dir_all(new_profile.config_dir()).await
|
||||
.map_err(|e| format!("Unable to create the profile config directory: {}", e))?;
|
||||
fs::create_dir_all(new_profile.data_dir()).await
|
||||
.map_err(|e| format!("Unable to create the profile data directory: {}", e))?;
|
||||
|
||||
appd.profile = Some(new_profile.clone());
|
||||
new_profile.save().await;
|
||||
|
||||
Ok(new_profile)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn list_platform_capabilities() -> Result<Vec<String>, ()> {
|
||||
log::debug!("invoke: list_platform_capabilities");
|
||||
@ -179,22 +213,6 @@ pub async fn list_platform_capabilities() -> Result<Vec<String>, ()> {
|
||||
return Ok(vec!["wine".to_owned()]);
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn set_cfg(
|
||||
state: State<'_, Mutex<AppData>>,
|
||||
key: String,
|
||||
value: serde_json::Value
|
||||
) -> Result<(), ()> {
|
||||
log::debug!("invoke: set_cfg({}, {})", key, value);
|
||||
|
||||
let mut appd = state.lock().await;
|
||||
if let Some(p) = &mut appd.profile {
|
||||
p.data.cfg.insert(key, value);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[cfg(target_os = "windows")]
|
||||
pub async fn list_displays() -> Result<Vec<(String, String)>, String> {
|
||||
|
Reference in New Issue
Block a user