Refactoring Install paths for a frontend experience
This commit is contained in:
@@ -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<SharedState>, version: String) {
|
||||
|
||||
save_state(&state);
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn install_paths(
|
||||
app: &AppHandle,
|
||||
state: &SharedState,
|
||||
paths: &Vec<String>,
|
||||
) -> 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(())
|
||||
}
|
||||
|
||||
@@ -102,33 +102,3 @@ pub fn save_state(state: &SharedState) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn install_paths(state: &SharedState, paths: &Vec<PathBuf>) {
|
||||
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}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+25
-9
@@ -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::<SharedState>().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::<Vec<_>>(),
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
println!("Failed emitting files to frontend");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -80,3 +80,7 @@ export async function getVersion(): Promise<string> {
|
||||
export async function updateVersion(version: string): Promise<void> {
|
||||
await invoke("update_version", { version });
|
||||
}
|
||||
|
||||
export async function installPaths(version: string, paths: string[]): Promise<void> {
|
||||
await invoke("install_paths", { version, paths });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user