feat: phase 2

Newfound motivation
This commit is contained in:
2025-02-23 05:12:21 +01:00
parent fdf3679fbe
commit a29bce2227
36 changed files with 1367 additions and 615 deletions

View File

@ -1,30 +1,34 @@
mod cfg;
mod model;
mod cmd;
mod pkg_remote;
mod pkg_local;
mod util;
mod model;
mod pkg;
mod pkg_store;
mod profile;
mod util;
mod start;
mod liner;
mod download_handler;
mod appdata;
use tokio::{fs, try_join};
use tokio::sync::Mutex;
use model::Package;
use tauri::Manager;
use closure::closure;
use appdata::AppData;
use pkg::PkgKey;
use pkg_store::PackageStore;
use profile::Profile;
struct AppData {
profile: Option<Profile>,
mods_local: Vec<Package>,
mods_store: Vec<Package>,
}
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().unwrap();
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()
std::env::current_dir()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
);
try_join!(
@ -33,35 +37,87 @@ pub async fn run(args: Vec<String>) {
fs::create_dir_all(util::cache_dir())
).expect("Unable to create working directories");
let app_data = AppData {
profile: pkg_local::load_config(),
mods_local: pkg_local::walk_packages(true).await.expect("Unable to scan local packages"),
mods_store: [].to_vec(),
};
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).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())
};
if args.len() == 1 {
tauri::Builder::default()
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_opener::init())
.setup(|app| {
app.manage(Mutex::new(app_data));
Ok(())
})
.invoke_handler(tauri::generate_handler![
cmd::get_packages,
cmd::reload_packages,
cmd::get_listings,
cmd::download_package,
cmd::delete_package,
cmd::get_current_profile,
cmd::init_profile,
cmd::save_profile,
cmd::startline
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
} else {
panic!("Not implemented");
}
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).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
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}