From 45c916cde3ed916c1453437515f3a20ae40242b0 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 3 Apr 2026 16:49:17 +0200 Subject: [PATCH] Launcher commands --- src-tauri/src/commands.rs | 81 ++++++++++++++++++++++++++++++ src-tauri/src/lib.rs | 102 ++++++++++++++++++++++++++++++++++---- src-tauri/src/main.rs | 18 ++++++- 3 files changed, 189 insertions(+), 12 deletions(-) create mode 100644 src-tauri/src/commands.rs diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs new file mode 100644 index 0000000..a88cdc7 --- /dev/null +++ b/src-tauri/src/commands.rs @@ -0,0 +1,81 @@ +use tauri::State; + +use crate::{GameVersion, LauncherSettings, Mod, SharedState}; + +#[tauri::command] +pub fn get_versions(state: State) -> Vec { + let state = state.lock().unwrap(); + state.versions.clone() +} + +#[tauri::command] +pub fn get_installed_mods(state: State, version_id: String) -> Vec { + let state = state.lock().unwrap(); + state + .mods + .iter() + .filter(|m| m.supported_versions.contains(&version_id)) + .cloned() + .collect() +} + +#[tauri::command] +pub fn toggle_mod(state: State, mod_id: String) { + let mut state = state.lock().unwrap(); + if let Some(mod_item) = state.mods.iter_mut().find(|m| m.id == mod_id) { + mod_item.enabled = !mod_item.enabled; + } +} + +#[tauri::command] +pub fn discover_mods(query: String) -> Vec { + let all = vec![Mod { + id: "modmenu".into(), + name: "Mod Menu".into(), + version: "7.2.2".into(), + author: "TerraformersMC".into(), + description: "Adds a mod menu.".into(), + enabled: false, + icon: None, + supported_versions: vec!["1.20.1".into(), "1.20.4".into(), "1.21".into()], + }]; + + if query.is_empty() { + return all; + } + + all.into_iter() + .filter(|m| m.name.to_lowercase().contains(&query.to_lowercase())) + .collect() +} + +#[tauri::command] +pub fn install_mod(state: State, mut mod_item: Mod) { + let mut state = state.lock().unwrap(); + + if !state.mods.iter().any(|m| m.id == mod_item.id) { + mod_item.enabled = true; + state.mods.push(mod_item); + } +} + +#[tauri::command] +pub fn get_settings(state: State) -> LauncherSettings { + state.lock().unwrap().settings.clone() +} + +#[tauri::command] +pub fn update_settings(state: State, new_settings: LauncherSettings) { + let mut state = state.lock().unwrap(); + state.settings = new_settings; +} + +#[tauri::command] +pub fn launch_game(version_id: String) -> Result { + println!("Launching Saturn Client with version {}", version_id); + + // Replace this with real launcher logic + std::thread::sleep(std::time::Duration::from_secs(2)); + + Ok("Game launched successfully!".into()) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4a277ef..fed28c7 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,14 +1,94 @@ -// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ -#[tauri::command] -fn greet(name: &str) -> String { - format!("Hello, {}! You've been greeted from Rust!", name) +pub mod commands; + +use serde::{Deserialize, Serialize}; +use std::sync::Mutex; + +// --- Types --- +#[derive(Clone, Serialize, Deserialize)] +pub struct Mod { + pub id: String, + pub name: String, + pub version: String, + pub author: String, + pub description: String, + pub enabled: bool, + pub icon: Option, + pub supported_versions: Vec, } -#[cfg_attr(mobile, tauri::mobile_entry_point)] -pub fn run() { - tauri::Builder::default() - .plugin(tauri_plugin_opener::init()) - .invoke_handler(tauri::generate_handler![greet]) - .run(tauri::generate_context!()) - .expect("error while running tauri application"); +#[derive(Clone, Serialize, Deserialize)] +pub struct GameVersion { + pub id: String, + pub name: String, + pub release_date: String, + pub r#type: String, // "release" | "snapshot" } + +#[derive(Clone, Serialize, Deserialize)] +pub struct LauncherSettings { + pub jvm_arguments: String, + pub max_memory: u32, +} + +// --- App State --- +pub struct AppState { + pub versions: Vec, + pub mods: Vec, + pub settings: LauncherSettings, +} + +impl Default for AppState { + fn default() -> Self { + Self { + versions: vec![ + GameVersion { + id: "1.20.1".into(), + name: "Fabric 1.20.1".into(), + release_date: "2023-06-07".into(), + r#type: "release".into(), + }, + GameVersion { + id: "1.20.4".into(), + name: "Fabric 1.20.4".into(), + release_date: "2023-12-07".into(), + r#type: "release".into(), + }, + GameVersion { + id: "1.21".into(), + name: "Fabric 1.21".into(), + release_date: "2024-06-13".into(), + r#type: "release".into(), + }, + ], + mods: vec![ + Mod { + id: "sodium".into(), + name: "Sodium".into(), + version: "0.5.8".into(), + author: "jellysquid3".into(), + description: "Modern rendering engine for Minecraft.".into(), + enabled: true, + icon: None, + supported_versions: vec!["1.20.1".into(), "1.20.4".into(), "1.21".into()], + }, + Mod { + id: "iris".into(), + name: "Iris".into(), + version: "1.7.0".into(), + author: "coderbot".into(), + description: "A modern shaders mod for Minecraft.".into(), + enabled: true, + icon: None, + supported_versions: vec!["1.20.1".into(), "1.20.4".into(), "1.21".into()], + }, + ], + settings: LauncherSettings { + jvm_arguments: "-XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200" + .into(), + max_memory: 4096, + }, + } + } +} + +type SharedState = Mutex; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index db87958..60fc664 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,6 +1,22 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +use saturn_launcher_lib::commands::*; + +#[cfg_attr(mobile, tauri::mobile_entry_point)] fn main() { - saturn_launcher_lib::run() + tauri::Builder::default() + .plugin(tauri_plugin_opener::init()) + .invoke_handler(tauri::generate_handler![ + get_versions, + get_installed_mods, + toggle_mod, + discover_mods, + install_mod, + get_settings, + update_settings, + launch_game + ]) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); }