feat: add command to set osu! user config values with improved error handling

This commit is contained in:
HorizonCode 2025-07-03 15:04:25 +02:00
parent cfb0851065
commit d6958dd15d
3 changed files with 34 additions and 31 deletions

View File

@ -3,7 +3,8 @@ use std::path::PathBuf;
use winreg::RegKey; use winreg::RegKey;
use winreg::enums::*; use winreg::enums::*;
use crate::utils::{check_folder_completeness, get_osu_user_config, get_osu_config}; use crate::utils::set_osu_user_config_value;
use crate::utils::{check_folder_completeness, get_osu_config, get_osu_user_config};
#[tauri::command] #[tauri::command]
pub fn get_hwid() -> String { pub fn get_hwid() -> String {
@ -207,3 +208,12 @@ pub fn get_osu_release_stream(folder: String) -> String {
.and_then(|config| config.get("_ReleaseStream").cloned()) .and_then(|config| config.get("_ReleaseStream").cloned())
.unwrap_or_else(|| "Stable40".to_string()); .unwrap_or_else(|| "Stable40".to_string());
} }
#[tauri::command]
pub fn set_osu_config_value(
osu_folder_path: String,
key: String,
value: String,
) -> Result<bool, String> {
set_osu_user_config_value(&osu_folder_path, &key, &value)
}

View File

@ -2,10 +2,10 @@
use tauri::Manager; use tauri::Manager;
pub mod utils; pub mod utils;
mod commands; pub mod commands;
use crate::commands::{ use crate::commands::{
find_osu_installation, get_beatmapsets_count, get_hwid, get_osu_release_stream, find_osu_installation, get_beatmapsets_count, get_hwid, get_osu_release_stream,
get_osu_version, get_skins_count, valid_osu_folder, get_osu_version, get_skins_count, valid_osu_folder, set_osu_config_value
}; };
#[cfg_attr(mobile, tauri::mobile_entry_point)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
@ -30,7 +30,8 @@ pub fn run() {
get_beatmapsets_count, get_beatmapsets_count,
get_skins_count, get_skins_count,
get_osu_version, get_osu_version,
get_osu_release_stream get_osu_release_stream,
set_osu_config_value
]) ])
.plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_dialog::init())

View File

@ -1,5 +1,4 @@
use std::fs; use std::fs;
use std::io::Write;
use std::path::Path; use std::path::Path;
pub fn check_folder_completeness<P: AsRef<Path>>(folder_path: P, required_files: &[&str]) -> f32 { pub fn check_folder_completeness<P: AsRef<Path>>(folder_path: P, required_files: &[&str]) -> f32 {
@ -47,51 +46,44 @@ pub fn get_osu_user_config<P: AsRef<Path>>(
return Some(config_map); return Some(config_map);
} }
pub fn set_osu_user_config_value<P: AsRef<Path>>( pub fn set_osu_user_config_value(
osu_folder_path: P, osu_folder_path: &str,
key: &str, key: &str,
value: &str, value: &str,
) -> std::io::Result<()> { ) -> Result<bool, String> {
// Determine the config file path
let current_user = std::env::var("USERNAME").unwrap_or_else(|_| "Admin".to_string()); let current_user = std::env::var("USERNAME").unwrap_or_else(|_| "Admin".to_string());
let osu_config_path = osu_folder_path let osu_config_path = Path::new(osu_folder_path).join(format!("osu!.{}.cfg", current_user));
.as_ref()
.join(format!("osu!.{}.cfg", current_user));
// Read existing config into a Vec of lines if !osu_config_path.exists() {
let mut lines = if osu_config_path.exists() { return Ok(false);
fs::read_to_string(&osu_config_path)? }
.lines()
.map(|line| line.to_string())
.collect::<Vec<String>>()
} else {
Vec::new()
};
let mut found = false; let mut lines = fs::read_to_string(&osu_config_path)
.map_err(|e| e.to_string())?
.lines()
.map(|line| line.to_string())
.collect::<Vec<String>>();
let mut found_key = false;
for line in lines.iter_mut() { for line in lines.iter_mut() {
if let Some((existing_key, _)) = line.split_once(" = ") { if let Some((existing_key, _)) = line.split_once(" = ") {
if existing_key.trim() == key { if existing_key.trim() == key {
*line = format!("{} = {}", key, value); *line = format!("{} = {}", key, value);
found = true; found_key = true;
break; break;
} }
} }
} }
// If the key was not found, append it if !found_key {
if !found {
lines.push(format!("{} = {}", key, value)); lines.push(format!("{} = {}", key, value));
} }
// Write back the file let new_content = lines.join("\n") + "\n";
let mut file = fs::File::create(&osu_config_path)?; fs::write(&osu_config_path, new_content).map_err(|e| e.to_string())?;
for line in lines {
writeln!(file, "{}", line)?;
}
Ok(()) Ok(true)
} }
pub fn get_osu_config<P: AsRef<Path>>( pub fn get_osu_config<P: AsRef<Path>>(