diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 0772634..3519f8c 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,7 +1,7 @@ -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use serde::Deserialize; -use tauri::State; +use tauri::{AppHandle, Emitter, State}; use tokio::fs; use crate::{launcher::LAUNCHER_DIR, save_state, LauncherSettings, Mod, SharedState}; @@ -118,9 +118,7 @@ pub async fn remove_mod(version: String, file_name: String) -> Result<(), String } // move file - fs::remove_file(&source) - .await - .map_err(|e| e.to_string())?; + fs::remove_file(&source).await.map_err(|e| e.to_string())?; Ok(()) } @@ -289,3 +287,44 @@ pub fn update_version(state: State, version: String) { save_state(&state); } + +#[tauri::command] +pub async fn install_paths( + app: &AppHandle, + state: &SharedState, + paths: &Vec, +) -> Result<(), String> { + for path in paths { + let file = Path::new(&path) + .file_name() + .and_then(|name| name.to_str()) + .unwrap_or_default(); + + if file.is_empty() { + println!("Error: Unable to obtain file"); + continue; + } + + let version_dir = LAUNCHER_DIR.data_dir().join(&state.lock().unwrap().version); + + let mc_child = if path.ends_with(".jar") { + Some("mods") + } else { + None + }; + + if let Some(mc_child) = mc_child { + match fs::copy(path, version_dir.join(mc_child).join(file)).await { + Ok(_) => { + app.emit("path-installed", path.to_string()) + .map_err(|e| e.to_string())?; + } + Err(e) => { + println!("Error: failed to copy mod {file:?}: {e}") + } + } + } + } + + Ok(()) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 3895212..ce569fd 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -102,33 +102,3 @@ pub fn save_state(state: &SharedState) { } } -pub fn install_paths(state: &SharedState, paths: &Vec) { - for path in paths { - if path.is_file() { - let file = path.file_name().unwrap_or_default().to_str().unwrap_or_default(); - - if file.is_empty() { - println!("Error: Unable to obtain file"); - continue; - } - - let version_dir = LAUNCHER_DIR.data_dir().join(&state.lock().unwrap().version); - - let mc_child = match path.extension().unwrap_or_default().to_str().unwrap_or_default() { - // Mod - "jar" => Some("mods"), - // Etc (won't install) - _ => None - }; - - if let Some(mc_child) = mc_child { - match fs::copy(path, version_dir.join(mc_child).join(file)) { - Ok(_) => {} - Err(e) => { - println!("Error: failed to copy mod {file:?}: {e}") - } - } - } - } - } -} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index b388c66..9389d96 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,8 +1,8 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use saturn_launcher_lib::{SharedState, install_paths, load_state}; -use tauri::{Manager, WindowEvent}; +use saturn_launcher_lib::load_state; +use tauri::{Emitter, Manager, WindowEvent}; #[cfg_attr(mobile, tauri::mobile_entry_point)] fn main() { @@ -30,19 +30,35 @@ fn main() { saturn_launcher_lib::launcher::auth::auth_remove, saturn_launcher_lib::launcher::auth::auth_validate, saturn_launcher_lib::launcher::auth::auth_get_valid - ]).setup(|app| { + ]) + .setup(|app| { match app.get_webview_window("main") { Some(window) => { - let state = app.state::().inner().clone(); + let handle = app.handle().clone(); window.on_window_event(move |event| { if let WindowEvent::DragDrop(file_event) = event { - match file_event { - tauri::DragDropEvent::Drop{paths, ..} => { - println!("Installing files: {:?}", paths); - install_paths(&state, paths); + if let tauri::DragDropEvent::Drop { paths, .. } = file_event { + println!("Installing files: {:?}", paths); + + if handle + .emit( + "install-paths", + paths + .iter() + .filter_map(|p| { + if p.is_file() { + Some(p.to_str()?.to_string()) + } else { + None + } + }) + .collect::>(), + ) + .is_err() + { + println!("Failed emitting files to frontend"); } - _ => {} } } }); diff --git a/src/lib/saturn.ts b/src/lib/saturn.ts index 8dc6d07..debd614 100644 --- a/src/lib/saturn.ts +++ b/src/lib/saturn.ts @@ -80,3 +80,7 @@ export async function getVersion(): Promise { export async function updateVersion(version: string): Promise { await invoke("update_version", { version }); } + +export async function installPaths(version: string, paths: string[]): Promise { + await invoke("install_paths", { version, paths }); +}