From 892f2cea079e1a0f1ace0416c2b0195889d80058 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Thu, 3 Jul 2025 11:46:02 +0200 Subject: [PATCH] feat: add get_beatmapsets_count command and utility to retrieve osu! user config --- src-tauri/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++++--- src-tauri/src/utils.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 456e1b9..a891732 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -2,11 +2,13 @@ use hardware_id::get_id; use std::path::PathBuf; use tauri::Manager; -use winreg::enums::*; use winreg::RegKey; +use winreg::enums::*; -use crate::utils::check_folder_completeness; mod utils; +use crate::utils::check_folder_completeness; +use crate::utils::get_osu_user_config; + #[tauri::command] fn get_hwid() -> String { @@ -135,6 +137,38 @@ fn find_osu_installation() -> Option { return None; } +#[tauri::command] +fn get_beatmapsets_count(folder: String) -> Option { + let path = PathBuf::from(folder); + let osu_config = get_osu_user_config(path.clone()); + let songs_path = osu_config + .and_then(|config| config.get("Songs").cloned()) + .unwrap_or_else(|| path.join("Songs").to_string_lossy().into_owned()); + let songs_folder = PathBuf::from(songs_path); + + if !songs_folder.exists() { + return None; + } + + let mut count = 0; + if let Ok(entries) = std::fs::read_dir(songs_folder) { + for entry in entries.flatten() { + if entry.file_type().map_or(false, |ft| ft.is_dir()) { + let dir_path = entry.path(); + if let Ok(files) = std::fs::read_dir(&dir_path) { + for file in files.flatten() { + if file.path().extension().map_or(false, |ext| ext == "osu") { + count += 1; + break; + } + } + } + } + } + } + return Some(count); +} + #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { let mut builder = tauri::Builder::default(); @@ -153,7 +187,8 @@ pub fn run() { .invoke_handler(tauri::generate_handler![ get_hwid, find_osu_installation, - valid_osu_folder + valid_osu_folder, + get_beatmapsets_count ]) .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_dialog::init()) diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs index a40cc89..aee088c 100644 --- a/src-tauri/src/utils.rs +++ b/src-tauri/src/utils.rs @@ -21,4 +21,34 @@ pub fn check_folder_completeness>(folder_path: P, required_files: } else { (found as f32 / required_files.len() as f32) * 100.0 } -} \ No newline at end of file +} + +pub fn get_osu_user_config>( + osu_folder_path: P, +) -> Option> { + // Ensure the osu! folder path is valid + if !osu_folder_path.as_ref().exists() { + return None; + } + + // get the osu!{username}.cfg file from the osu! folder + 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)); + if !osu_config_path.exists() { + return None; + } + + // read the osu config and return it as a map, key and value are separated by ' = ' + let mut config_map = std::collections::HashMap::new(); + if let Ok(contents) = std::fs::read_to_string(osu_config_path) { + for line in contents.lines() { + if let Some((key, value)) = line.split_once(" = ") { + config_map.insert(key.trim().to_string(), value.trim().to_string()); + } + } + } + + return Some(config_map); +}