Drag and drop to install #12
This commit is contained in:
+35
-4
@@ -2,11 +2,11 @@ pub mod commands;
|
|||||||
pub mod launcher;
|
pub mod launcher;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{fs, path::PathBuf, sync::Mutex};
|
use std::{fs, path::PathBuf, sync::{Arc, Mutex}};
|
||||||
|
|
||||||
use crate::launcher::LAUNCHER_DIR;
|
use crate::launcher::LAUNCHER_DIR;
|
||||||
|
|
||||||
type SharedState = Mutex<AppState>;
|
pub type SharedState = Arc<Mutex<AppState>>;
|
||||||
|
|
||||||
// --- Types ---
|
// --- Types ---
|
||||||
#[derive(Clone, Serialize, Deserialize)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
@@ -61,7 +61,7 @@ pub fn load_state() -> SharedState {
|
|||||||
if path.exists() {
|
if path.exists() {
|
||||||
match fs::read_to_string(&path) {
|
match fs::read_to_string(&path) {
|
||||||
Ok(content) => match serde_json::from_str::<AppState>(&content) {
|
Ok(content) => match serde_json::from_str::<AppState>(&content) {
|
||||||
Ok(state) => return Mutex::new(state),
|
Ok(state) => return Arc::new(Mutex::new(state)),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Failed to parse config, using default: {err}");
|
eprintln!("Failed to parse config, using default: {err}");
|
||||||
}
|
}
|
||||||
@@ -73,7 +73,7 @@ pub fn load_state() -> SharedState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If missing or failed → create default
|
// If missing or failed → create default
|
||||||
let default = Mutex::new(AppState::default());
|
let default = Arc::new(Mutex::new(AppState::default()));
|
||||||
save_state(&default);
|
save_state(&default);
|
||||||
|
|
||||||
default
|
default
|
||||||
@@ -101,3 +101,34 @@ 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}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+27
-2
@@ -1,7 +1,8 @@
|
|||||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||||
|
|
||||||
use saturn_launcher_lib::load_state;
|
use saturn_launcher_lib::{SharedState, install_paths, load_state};
|
||||||
|
use tauri::{Manager, WindowEvent};
|
||||||
|
|
||||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
fn main() {
|
fn main() {
|
||||||
@@ -28,7 +29,31 @@ fn main() {
|
|||||||
saturn_launcher_lib::launcher::auth::auth_remove,
|
saturn_launcher_lib::launcher::auth::auth_remove,
|
||||||
saturn_launcher_lib::launcher::auth::auth_validate,
|
saturn_launcher_lib::launcher::auth::auth_validate,
|
||||||
saturn_launcher_lib::launcher::auth::auth_get_valid
|
saturn_launcher_lib::launcher::auth::auth_get_valid
|
||||||
])
|
]).setup(|app| {
|
||||||
|
match app.get_webview_window("main") {
|
||||||
|
Some(window) => {
|
||||||
|
let state = app.state::<SharedState>().inner().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);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
None => {
|
||||||
|
println!("WARN: Drag and drop feature will not work");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
.run(tauri::generate_context!())
|
.run(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.expect("error while running tauri application");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,8 @@
|
|||||||
"width": 1000,
|
"width": 1000,
|
||||||
"height": 650,
|
"height": 650,
|
||||||
"backgroundColor": "#0a0a0a",
|
"backgroundColor": "#0a0a0a",
|
||||||
"visible": false
|
"visible": false,
|
||||||
|
"dragDropEnabled": true
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"security": {
|
"security": {
|
||||||
|
|||||||
Reference in New Issue
Block a user