feat: add function to set osu! user config values
This commit is contained in:
parent
8f92af0a87
commit
cfb0851065
@ -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]
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn check_folder_completeness<P: AsRef<Path>>(folder_path: P, required_files: &[&str]) -> f32 {
|
||||
@ -45,6 +47,53 @@ pub fn get_osu_user_config<P: AsRef<Path>>(
|
||||
return Some(config_map);
|
||||
}
|
||||
|
||||
pub fn set_osu_user_config_value<P: AsRef<Path>>(
|
||||
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::<Vec<String>>()
|
||||
} 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<P: AsRef<Path>>(
|
||||
osu_folder_path: P,
|
||||
) -> Option<std::collections::HashMap<String, String>> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user