feat: CLI
This commit is contained in:
@ -15,6 +15,7 @@ pub struct AppData {
|
||||
pub profile: Option<Profile>,
|
||||
pub pkgs: PackageStore,
|
||||
pub cfg: GlobalConfig,
|
||||
pub remain_open: bool,
|
||||
}
|
||||
|
||||
impl AppData {
|
||||
@ -32,6 +33,7 @@ impl AppData {
|
||||
profile,
|
||||
pkgs: PackageStore::new(apph.clone()),
|
||||
cfg,
|
||||
remain_open: true
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,18 +10,20 @@ mod download_handler;
|
||||
mod appdata;
|
||||
mod display;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use closure::closure;
|
||||
use appdata::AppData;
|
||||
use model::misc::Game;
|
||||
use pkg::PkgKey;
|
||||
use profile::Profile;
|
||||
use tauri::{Listener, Manager};
|
||||
use tauri_plugin_deep_link::DeepLinkExt;
|
||||
use tauri_plugin_cli::CliExt;
|
||||
use tokio::{sync::Mutex, fs, try_join};
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub async fn run(_args: Vec<String>) {
|
||||
simple_logger::init_with_env()
|
||||
.expect("Unable to initialize the logger");
|
||||
simple_logger::init_with_env().expect("Unable to initialize the logger");
|
||||
|
||||
log::info!(
|
||||
"Running from {}",
|
||||
@ -62,6 +64,7 @@ pub async fn run(_args: Vec<String>) {
|
||||
}
|
||||
}
|
||||
}))
|
||||
.plugin(tauri_plugin_cli::init())
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.plugin(tauri_plugin_deep_link::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
@ -72,7 +75,38 @@ pub async fn run(_args: Vec<String>) {
|
||||
|
||||
util::init_dirs(&apph);
|
||||
|
||||
let app_data = AppData::new(app.handle().clone());
|
||||
let mut app_data = AppData::new(app.handle().clone());
|
||||
let start_immediately;
|
||||
|
||||
if let Ok(matches) = app.cli().matches() {
|
||||
let start_arg = matches.args.get("start").expect("Invalid argument configuration");
|
||||
let game_arg = matches.args.get("game").expect("Invalid argument configuration");
|
||||
let name_arg = matches.args.get("name").expect("Invalid argument configuration");
|
||||
log::debug!("{:?} {:?} {:?}", start_arg, game_arg, name_arg);
|
||||
if start_arg.occurrences > 0 {
|
||||
start_immediately = true;
|
||||
app_data.remain_open = false;
|
||||
} else {
|
||||
tauri::WebviewWindowBuilder::new(app, "main", tauri::WebviewUrl::App("index.html".into()))
|
||||
.title("STARTLINER")
|
||||
.inner_size(600f64, 500f64)
|
||||
.min_inner_size(600f64, 500f64)
|
||||
.build()?;
|
||||
start_immediately = false;
|
||||
}
|
||||
|
||||
if game_arg.occurrences == 1 && name_arg.occurrences == 1 {
|
||||
let game = game_arg.value.as_str().unwrap();
|
||||
let name = name_arg.value.as_str().unwrap();
|
||||
|
||||
app_data.switch_profile(
|
||||
Game::from_str(game).ok_or_else(|| anyhow!("Invalid game"))?,
|
||||
name.to_owned()
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
return Err(anyhow!("Invalid command line arguments").into());
|
||||
}
|
||||
|
||||
app.manage(Mutex::new(app_data));
|
||||
app.deep_link().register_all()?;
|
||||
@ -103,6 +137,36 @@ pub async fn run(_args: Vec<String>) {
|
||||
});
|
||||
}));
|
||||
|
||||
app.listen("launch-end", closure!(clone apph, |_| {
|
||||
let apph = apph.clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let mutex = apph.state::<Mutex<AppData>>();
|
||||
let appd = mutex.lock().await;
|
||||
if !appd.remain_open {
|
||||
apph.exit(0);
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
if start_immediately == true {
|
||||
let apph_clone = apph.clone();
|
||||
tauri::async_runtime::spawn(async {
|
||||
let apph_clone_clone = apph_clone.clone();
|
||||
{
|
||||
let mtx = apph_clone.state::<Mutex<AppData>>();
|
||||
let mut appd = mtx.lock().await;
|
||||
if let Err(e) = appd.pkgs.reload_all().await {
|
||||
log::error!("Unable to reload packages: {}", e);
|
||||
apph_clone.exit(1);
|
||||
}
|
||||
}
|
||||
if let Err(e) = cmd::startline(apph_clone).await {
|
||||
log::error!("Unable to launch: {}", e);
|
||||
apph_clone_clone.exit(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
|
@ -110,6 +110,8 @@ pub async fn start(p: &Profile, app: AppHandle) -> Result<()> {
|
||||
amd_builder.env("OPENSSL_ia32cap", ":~0x20000000");
|
||||
}
|
||||
|
||||
pkill("amdaemon.exe").await;
|
||||
|
||||
log::info!("Launching amdaemon: {:?}", amd_builder);
|
||||
log::info!("Launching mu3: {:?}", game_builder);
|
||||
|
||||
@ -127,7 +129,9 @@ pub async fn start(p: &Profile, app: AppHandle) -> Result<()> {
|
||||
(game.wait().await.expect("mu3 failed to run"), "mu3.exe")
|
||||
});
|
||||
|
||||
_ = app.emit("launch-start", "");
|
||||
if let Err(e) = app.emit("launch-start", "") {
|
||||
log::warn!("Unable to emit launch-start: {}", e);
|
||||
}
|
||||
|
||||
let (rc, process_name) = set.join_next().await.expect("No spawn").expect("No result");
|
||||
|
||||
@ -143,7 +147,9 @@ pub async fn start(p: &Profile, app: AppHandle) -> Result<()> {
|
||||
|
||||
log::debug!("Fin");
|
||||
|
||||
_ = app.emit("launch-end", "");
|
||||
if let Err(e) = app.emit("launch-start", "") {
|
||||
log::warn!("Unable to emit launch-end: {}", e);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
|
Reference in New Issue
Block a user