forked from akanyan/STARTLINER
125 lines
4.4 KiB
Rust
125 lines
4.4 KiB
Rust
mod cmd;
|
|
mod model;
|
|
mod pkg;
|
|
mod pkg_store;
|
|
mod profile;
|
|
mod util;
|
|
mod start;
|
|
mod liner;
|
|
mod download_handler;
|
|
mod appdata;
|
|
|
|
use closure::closure;
|
|
use appdata::AppData;
|
|
use pkg::PkgKey;
|
|
use pkg_store::PackageStore;
|
|
use profile::Profile;
|
|
use tauri::{Listener, Manager};
|
|
use tauri_plugin_deep_link::DeepLinkExt;
|
|
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");
|
|
|
|
log::info!(
|
|
"Running from {}",
|
|
std::env::current_dir()
|
|
.unwrap_or_default()
|
|
.to_str()
|
|
.unwrap_or_default()
|
|
);
|
|
|
|
try_join!(
|
|
fs::create_dir_all(util::config_dir()),
|
|
fs::create_dir_all(util::pkg_dir()),
|
|
fs::create_dir_all(util::cache_dir())
|
|
).expect("Unable to create working directories");
|
|
|
|
tauri::Builder::default()
|
|
.plugin(tauri_plugin_single_instance::init(|app, args, _cwd| {
|
|
let _ = app
|
|
.get_webview_window("main")
|
|
.expect("No main window")
|
|
.set_focus();
|
|
if args.len() == 2 {
|
|
// Todo deindent this chimera
|
|
let url = &args[1];
|
|
if &url[..13] == "rainycolor://" {
|
|
let regex = regex::Regex::new(
|
|
r"rainycolor://v1/install/rainy\.patafour\.zip/([^/]+)/([^/]+)/[0-9]+\.[0-9]+\.[0-9]+/"
|
|
).expect("Invalid regex");
|
|
if let Some(caps) = regex.captures(url) {
|
|
if caps.len() == 3 {
|
|
let apph = app.clone();
|
|
let key = PkgKey(format!("{}-{}", caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()));
|
|
tauri::async_runtime::spawn(async move {
|
|
let mutex = apph.state::<Mutex<AppData>>();
|
|
let mut appd = mutex.lock().await;
|
|
_ = appd.pkgs.fetch_listings().await;
|
|
if let Err(e) = appd.pkgs.install_package(&key, true, true).await {
|
|
log::warn!("Fail: {}", e.to_string());
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}))
|
|
.plugin(tauri_plugin_deep_link::init())
|
|
.plugin(tauri_plugin_dialog::init())
|
|
.plugin(tauri_plugin_shell::init())
|
|
.plugin(tauri_plugin_opener::init())
|
|
.setup(|app| {
|
|
let app_data = AppData {
|
|
profile: Profile::load(),
|
|
pkgs: PackageStore::new(app.handle().clone())
|
|
};
|
|
|
|
app.manage(Mutex::new(app_data));
|
|
app.deep_link().register_all()?;
|
|
|
|
let apph = app.handle();
|
|
|
|
app.listen("download-end", closure!(clone apph, |ev| {
|
|
let raw = ev.payload();
|
|
let key = PkgKey(raw[1..raw.len()-1].to_owned());
|
|
let apph = apph.clone();
|
|
tauri::async_runtime::spawn(async move {
|
|
let mutex = apph.state::<Mutex<AppData>>();
|
|
let mut appd = mutex.lock().await;
|
|
_ = appd.pkgs.install_package(&key, true, false).await;
|
|
});
|
|
}));
|
|
|
|
app.listen("install-end", closure!(clone apph, |ev| {
|
|
let payload = serde_json::from_str::<pkg_store::Payload>(&ev.payload());
|
|
let apph = apph.clone();
|
|
tauri::async_runtime::spawn(async move {
|
|
let mutex = apph.state::<Mutex<AppData>>();
|
|
let mut appd = mutex.lock().await;
|
|
_ = appd.toggle_package(payload.unwrap().pkg, true);
|
|
});
|
|
}));
|
|
|
|
Ok(())
|
|
})
|
|
.invoke_handler(tauri::generate_handler![
|
|
cmd::get_package,
|
|
cmd::get_all_packages,
|
|
cmd::reload_all_packages,
|
|
cmd::fetch_listings,
|
|
cmd::install_package,
|
|
cmd::delete_package,
|
|
cmd::toggle_package,
|
|
cmd::get_current_profile,
|
|
cmd::init_profile,
|
|
cmd::save_profile,
|
|
cmd::startline,
|
|
cmd::set_cfg
|
|
])
|
|
.run(tauri::generate_context!())
|
|
.expect("error while running tauri application");
|
|
}
|