diff options
| -rw-r--r-- | src-tauri/src/core/config.rs | 58 | ||||
| -rw-r--r-- | src-tauri/src/core/mod.rs | 1 | ||||
| -rw-r--r-- | src-tauri/src/main.rs | 45 |
3 files changed, 98 insertions, 6 deletions
diff --git a/src-tauri/src/core/config.rs b/src-tauri/src/core/config.rs new file mode 100644 index 0000000..5558786 --- /dev/null +++ b/src-tauri/src/core/config.rs @@ -0,0 +1,58 @@ +use serde::{Deserialize, Serialize}; +use std::fs; +use std::path::PathBuf; +use std::sync::Mutex; +use tauri::{AppHandle, Manager}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct LauncherConfig { + pub min_memory: u32, // in MB + pub max_memory: u32, // in MB + pub java_path: String, + pub width: u32, + pub height: u32, +} + +impl Default for LauncherConfig { + fn default() -> Self { + Self { + min_memory: 1024, + max_memory: 2048, + java_path: "java".to_string(), + width: 854, + height: 480, + } + } +} + +pub struct ConfigState { + pub config: Mutex<LauncherConfig>, + pub file_path: PathBuf, +} + +impl ConfigState { + pub fn new(app_handle: &AppHandle) -> Self { + let app_dir = app_handle.path().app_data_dir().unwrap(); + let config_path = app_dir.join("config.json"); + + let config = if config_path.exists() { + let content = fs::read_to_string(&config_path).unwrap_or_default(); + serde_json::from_str(&content).unwrap_or_default() + } else { + LauncherConfig::default() + }; + + Self { + config: Mutex::new(config), + file_path: config_path, + } + } + + pub fn save(&self) -> Result<(), String> { + let config = self.config.lock().unwrap(); + let content = serde_json::to_string_pretty(&*config).map_err(|e| e.to_string())?; + fs::create_dir_all(self.file_path.parent().unwrap()).map_err(|e| e.to_string())?; + fs::write(&self.file_path, content).map_err(|e| e.to_string())?; + Ok(()) + } +} diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index c642161..6f7c3fe 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -3,3 +3,4 @@ pub mod auth; pub mod downloader; pub mod game_version; pub mod rules; +pub mod config; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 13ac6df..91b61ed 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -13,14 +13,17 @@ mod utils; #[tauri::command] async fn start_game( window: Window, - state: State<'_, core::auth::AccountState>, + auth_state: State<'_, core::auth::AccountState>, + config_state: State<'_, core::config::ConfigState>, version_id: String ) -> Result<String, String> { println!("Backend received StartGame for {}", version_id); // Check for active account - let account = state.active_account.lock().unwrap().clone() + let account = auth_state.active_account.lock().unwrap().clone() .ok_or("No active account found. Please login first.")?; + + let config = config_state.config.lock().unwrap().clone(); // Get App Data Directory (e.g., ~/.local/share/com.dropout.launcher or similar) // The identifier is set in tauri.conf.json. @@ -232,8 +235,8 @@ async fn start_game( // We inject standard convenient defaults. // TODO: Parse 'arguments.jvm' from version.json for full compatibility (Mac M1 support etc) args.push(format!("-Djava.library.path={}", natives_path)); - args.push("-Xmx2G".to_string()); // Default memory - args.push("-Xms1G".to_string()); + args.push(format!("-Xmx{}M", config.max_memory)); + args.push(format!("-Xms{}M", config.min_memory)); args.push("-cp".to_string()); args.push(classpath); @@ -322,7 +325,7 @@ async fn start_game( println!("Launch Args: {:?}", args); // Spawn the process - let mut command = Command::new("java"); + let mut command = Command::new(&config.java_path); command.args(&args); command.current_dir(&game_dir); // Run in game directory command.stdout(Stdio::piped()); @@ -389,11 +392,41 @@ async fn logout(state: State<'_, core::auth::AccountState>) -> Result<(), String Ok(()) } +#[tauri::command] +async fn get_settings( + state: State<'_, core::config::ConfigState>, +) -> Result<core::config::LauncherConfig, String> { + Ok(state.config.lock().unwrap().clone()) +} + +#[tauri::command] +async fn save_settings( + state: State<'_, core::config::ConfigState>, + config: core::config::LauncherConfig, +) -> Result<(), String> { + *state.config.lock().unwrap() = config; + state.save()?; + Ok(()) +} + fn main() { tauri::Builder::default() .plugin(tauri_plugin_shell::init()) .manage(core::auth::AccountState::new()) - .invoke_handler(tauri::generate_handler![start_game, get_versions, login_offline, get_active_account, logout]) + .setup(|app| { + let config_state = core::config::ConfigState::new(app.handle()); + app.manage(config_state); + Ok(()) + }) + .invoke_handler(tauri::generate_handler![ + start_game, + get_versions, + login_offline, + get_active_account, + logout, + get_settings, + save_settings + ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } |