diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index f684de9..6817ab3 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -196,7 +196,7 @@ pub fn get_osu_version(folder: String) -> String { let osu_user_config = get_osu_user_config(path.clone()); return osu_user_config .and_then(|config| config.get("LastVersion").cloned()) - .unwrap_or_else(|| "failed.".to_string()); + .unwrap_or_else(|| "failed".to_string()); } #[tauri::command] diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs index b76dd16..30cd346 100644 --- a/src-tauri/src/utils.rs +++ b/src-tauri/src/utils.rs @@ -1,3 +1,5 @@ +use std::fs; +use std::io::Write; use std::path::Path; pub fn check_folder_completeness>(folder_path: P, required_files: &[&str]) -> f32 { @@ -45,6 +47,53 @@ pub fn get_osu_user_config>( return Some(config_map); } +pub fn set_osu_user_config_value>( + osu_folder_path: P, + key: &str, + value: &str, +) -> std::io::Result<()> { + // Determine the config file path + let current_user = std::env::var("USERNAME").unwrap_or_else(|_| "Admin".to_string()); + let osu_config_path = osu_folder_path + .as_ref() + .join(format!("osu!.{}.cfg", current_user)); + + // Read existing config into a Vec of lines + let mut lines = if osu_config_path.exists() { + fs::read_to_string(&osu_config_path)? + .lines() + .map(|line| line.to_string()) + .collect::>() + } else { + Vec::new() + }; + + let mut found = false; + + for line in lines.iter_mut() { + if let Some((existing_key, _)) = line.split_once(" = ") { + if existing_key.trim() == key { + *line = format!("{} = {}", key, value); + found = true; + break; + } + } + } + + // If the key was not found, append it + if !found { + lines.push(format!("{} = {}", key, value)); + } + + // Write back the file + let mut file = fs::File::create(&osu_config_path)?; + for line in lines { + writeln!(file, "{}", line)?; + } + + Ok(()) +} + pub fn get_osu_config>( osu_folder_path: P, ) -> Option> {