refactor: simplify stopOpenTabletDriver, cleanup code

This commit is contained in:
HorizonCode committed 2026-05-26 10:33:14 +02:00
1 parent 5b1e32d4eb
commit cc4697efbc
3 files changed
+105 -118

No files matched your search

+103 -116
View File
@@ -73,87 +73,86 @@ pub fn valid_osu_folder(folder: String) -> bool {
check_folder_completeness(path, &osu_folder_files) >= 70.0 check_folder_completeness(path, &osu_folder_files) >= 70.0
} }
#[cfg(not(windows))]
#[tauri::command]
pub fn find_osu_installation() -> Option<String> {
None
}
#[cfg(windows)] #[cfg(windows)]
#[tauri::command] #[tauri::command]
pub fn find_osu_installation() -> Option<String> { pub fn find_osu_installation() -> Option<String> {
use winreg::RegKey; #[cfg(windows)]
use winreg::enums::*; {
use winreg::RegKey;
use winreg::enums::*;
let hklm_registry_paths = ["SOFTWARE\\Classes\\osu\\DefaultIcon"]; let hklm_registry_paths = ["SOFTWARE\\Classes\\osu\\DefaultIcon"];
let hkcu_registry_paths = [ let hkcu_registry_paths = [
"Software\\Classes\\osustable.File.osk\\DefaultIcon", "Software\\Classes\\osustable.File.osk\\DefaultIcon",
"Software\\Classes\\osustable.File.osr\\DefaultIcon", "Software\\Classes\\osustable.File.osr\\DefaultIcon",
"Software\\Classes\\osustable.File.osz\\DefaultIcon", "Software\\Classes\\osustable.File.osz\\DefaultIcon",
]; ];
let osu_folder_files = [ let osu_folder_files = [
"avcodec-51.dll", "avcodec-51.dll",
"avformat-52.dll", "avformat-52.dll",
"avutil-49.dll", "avutil-49.dll",
"bass.dll", "bass.dll",
"bass_fx.dll", "bass_fx.dll",
"collection.db", "collection.db",
"d3dcompiler_47.dll", "d3dcompiler_47.dll",
"libEGL.dll", "libEGL.dll",
"libGLESv2.dll", "libGLESv2.dll",
"Microsoft.Ink.dll", "Microsoft.Ink.dll",
"OpenTK.dll", "OpenTK.dll",
"osu!.cfg", "osu!.cfg",
"osu!.db", "osu!.db",
"osu!.exe", "osu!.exe",
"osu!auth.dll", "osu!auth.dll",
"osu!gameplay.dll", "osu!gameplay.dll",
"osu!seasonal.dll", "osu!seasonal.dll",
"osu!ui.dll", "osu!ui.dll",
"presence.db", "presence.db",
"pthreadGC2.dll", "pthreadGC2.dll",
"scores.db", "scores.db",
]; ];
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
for reg_path in &hklm_registry_paths { for reg_path in &hklm_registry_paths {
if let Ok(subkey) = hklm.open_subkey_with_flags(reg_path, KEY_READ | KEY_WOW64_32KEY) { if let Ok(subkey) = hklm.open_subkey_with_flags(reg_path, KEY_READ | KEY_WOW64_32KEY) {
let value_names = [""]; let value_names = [""];
for value_name in &value_names { for value_name in &value_names {
if let Ok(value) = subkey.get_value::<String, _>(value_name) { if let Ok(value) = subkey.get_value::<String, _>(value_name) {
let trimmed = value.trim_matches('"'); let trimmed = value.trim_matches('"');
let stripped = trimmed.strip_suffix(",0").unwrap_or(trimmed); let stripped = trimmed.strip_suffix(",0").unwrap_or(trimmed);
let path = PathBuf::from(stripped.trim()); let path = PathBuf::from(stripped.trim());
if let Some(parent) = path.parent() { if let Some(parent) = path.parent() {
let match_percentage = check_folder_completeness(parent, &osu_folder_files); let match_percentage =
check_folder_completeness(parent, &osu_folder_files);
if match_percentage >= 70.0 { if match_percentage >= 70.0 {
return Some(parent.to_string_lossy().into()); return Some(parent.to_string_lossy().into());
}
} }
} }
} }
} }
} }
}
let hkcu = RegKey::predef(HKEY_CURRENT_USER); let hkcu = RegKey::predef(HKEY_CURRENT_USER);
for reg_path in &hkcu_registry_paths { for reg_path in &hkcu_registry_paths {
if let Ok(subkey) = hkcu.open_subkey_with_flags(reg_path, KEY_READ | KEY_WOW64_32KEY) { if let Ok(subkey) = hkcu.open_subkey_with_flags(reg_path, KEY_READ | KEY_WOW64_32KEY) {
let value_names = [""]; let value_names = [""];
for value_name in &value_names { for value_name in &value_names {
if let Ok(value) = subkey.get_value::<String, _>(value_name) { if let Ok(value) = subkey.get_value::<String, _>(value_name) {
let trimmed = value.trim_matches('"'); let trimmed = value.trim_matches('"');
let stripped = trimmed.strip_suffix(",1").unwrap_or(trimmed); let stripped = trimmed.strip_suffix(",1").unwrap_or(trimmed);
let path = PathBuf::from(stripped.trim()); let path = PathBuf::from(stripped.trim());
if let Some(parent) = path.parent() { if let Some(parent) = path.parent() {
let match_percentage = check_folder_completeness(parent, &osu_folder_files); let match_percentage =
check_folder_completeness(parent, &osu_folder_files);
if match_percentage >= 70.0 { if match_percentage >= 70.0 {
return Some(parent.to_string_lossy().into()); return Some(parent.to_string_lossy().into());
}
} }
} }
} }
@@ -881,7 +880,7 @@ pub struct PresenceButton {
pub async fn presence_update_button(button: PresenceButton) { pub async fn presence_update_button(button: PresenceButton) {
match (button.text.as_deref(), button.url.as_deref()) { match (button.text.as_deref(), button.url.as_deref()) {
(Some(text), Some(url)) => presence::set_button(text, url), (Some(text), Some(url)) => presence::set_button(text, url),
_ =>presence::clear_button() _ => presence::clear_button(),
} }
} }
@@ -956,33 +955,30 @@ pub async fn download_ezpp_launcher_update(app: AppHandle, url: String) -> Resul
#[cfg(windows)] #[cfg(windows)]
#[tauri::command] #[tauri::command]
pub async fn install_ezpp_launcher_update(app: AppHandle) -> Result<(), String> { pub async fn install_ezpp_launcher_update(app: AppHandle) -> Result<(), String> {
let temp_dir = app.path().temp_dir().expect("Failed to get temp directory"); #[cfg(windows)]
let file_path = temp_dir.join("ezpplauncher_update.exe"); {
if !file_path.exists() { let temp_dir = app.path().temp_dir().expect("Failed to get temp directory");
return Err("Update file does not exist".to_string()); let file_path = temp_dir.join("ezpplauncher_update.exe");
if !file_path.exists() {
return Err("Update file does not exist".to_string());
}
// run this app detached and exit
const DETACHED_PROCESS: u32 = 0x00000008;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
Command::new(&file_path)
.arg("/S")
.creation_flags(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)
.spawn()
.map_err(|e| format!("Failed to spawn updater: {}", e))?;
sleep(Duration::from_millis(250)).await;
app.exit(0x0100);
} }
// run this app detached and exit
const DETACHED_PROCESS: u32 = 0x00000008;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
Command::new(&file_path)
.arg("/S")
.creation_flags(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)
.spawn()
.map_err(|e| format!("Failed to spawn updater: {}", e))?;
sleep(Duration::from_millis(250)).await;
app.exit(0x0100);
Ok(())
}
#[cfg(not(windows))]
#[tauri::command]
pub async fn install_ezpp_launcher_update(_app: AppHandle) -> Result<(), String> {
Ok(()) Ok(())
} }
@@ -994,42 +990,33 @@ pub async fn run_open_tablet_driver(path: String) -> Result<(), String> {
} }
#[cfg(windows)] #[cfg(windows)]
const DETACHED_PROCESS: u32 = 0x00000008;
#[cfg(windows)]
const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
{ {
#[cfg(windows)] const DETACHED_PROCESS: u32 = 0x00000008;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
Command::new(&otd_path) Command::new(&otd_path)
.creation_flags(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP) .creation_flags(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)
.spawn() .spawn()
.map_err(|e| format!("Failed to start OpenTabletDriver: {}", e))?; .map_err(|e| format!("Failed to start OpenTabletDriver: {}", e))?;
#[cfg(not(windows))]
Command::new(&otd_path)
.spawn()
.map_err(|e| format!("Failed to start OpenTabletDriver: {}", e))?;
} }
Ok(()) Ok(())
} }
#[tauri::command] #[tauri::command]
pub async fn stop_open_tablet_driver(path: String) -> Result<(), String> { pub async fn stop_open_tablet_driver() -> Result<(), String> {
let binary_name = PathBuf::from(&path) #[cfg(windows)]
.file_name() {
.and_then(|n| n.to_str()) let mut sys = System::new_all();
.ok_or("Invalid OpenTabletDriver path")? sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
.to_string();
let mut sys = System::new_all(); let process = sys
sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true); .processes()
.values()
.find(|p| p.name().eq_ignore_ascii_case("OpenTabletDriver.Daemon.exe"))
.ok_or("OpenTabletDriver is not running")?;
let process = sys.processes().values().find(|p| { process.kill();
p.name().eq_ignore_ascii_case(&binary_name) }
}).ok_or("OpenTabletDriver is not running")?;
process.kill();
Ok(()) Ok(())
} }
@@ -1041,9 +1028,9 @@ pub fn is_open_tablet_driver_running() -> bool {
let mut sys = System::new_all(); let mut sys = System::new_all();
sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true); sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
sys.processes().values().any(|p| { sys.processes()
p.name().eq_ignore_ascii_case("OpenTabletDriver.Daemon.exe") .values()
}) .any(|p| p.name().eq_ignore_ascii_case("OpenTabletDriver.Daemon.exe"))
} }
#[cfg(not(windows))] #[cfg(not(windows))]
+1 -1
View File
@@ -148,4 +148,4 @@ export const getSkins = async (folder: string) =>
); );
export const startOpenTabletDriver = async (otdDaemonPath: string) => export const startOpenTabletDriver = async (otdDaemonPath: string) =>
await invoke('run_open_tablet_driver', { path: otdDaemonPath }); await invoke('run_open_tablet_driver', { path: otdDaemonPath });
export const stopOpenTabletDriver = async (otdDaemonPath: string) => await invoke('stop_open_tablet_driver', { path: otdDaemonPath }); export const stopOpenTabletDriver = async () => await invoke('stop_open_tablet_driver');
+1 -1
View File
@@ -638,7 +638,7 @@
cleanup = true; cleanup = true;
launchInfo = 'Cleaning up...'; launchInfo = 'Cleaning up...';
if (otd_run) await stopOpenTabletDriver($openTabletDriverPath); if (otd_run) await stopOpenTabletDriver();
setCurrentTimeGlobalMedia(0); setCurrentTimeGlobalMedia(0);
resumeGlobalMedia(); resumeGlobalMedia();