feat: implement osu! updater command and enhance folder validation error messages
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
use hardware_id::get_id;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use sysinfo::System;
|
||||
use winreg::RegKey;
|
||||
use winreg::enums::*;
|
||||
|
||||
use crate::utils::get_window_title_by_pid;
|
||||
use crate::utils::set_osu_user_config_value;
|
||||
use crate::utils::{check_folder_completeness, get_osu_config, get_osu_user_config};
|
||||
|
||||
@@ -217,3 +222,43 @@ pub fn set_osu_config_value(
|
||||
) -> Result<bool, String> {
|
||||
set_osu_user_config_value(&osu_folder_path, &key, &value)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn run_osu_updater(osu_path: String) -> Result<(), String> {
|
||||
let mut updater_process = Command::new(osu_path.clone())
|
||||
.arg("-repair")
|
||||
.spawn()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
|
||||
let mut sys = System::new_all();
|
||||
sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
|
||||
|
||||
let mut termination_thread_running = true;
|
||||
|
||||
while termination_thread_running {
|
||||
for (_, process) in sys.processes() {
|
||||
if process.name() == "osu!.exe" {
|
||||
let process_id = process.pid();
|
||||
let window_title = get_window_title_by_pid(process_id);
|
||||
|
||||
if !window_title.is_empty() {
|
||||
if let Ok(_) = process.kill_and_wait() {
|
||||
termination_thread_running = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !termination_thread_running {
|
||||
break;
|
||||
}
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
}
|
||||
|
||||
updater_process.wait().map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@@ -1,11 +1,11 @@
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
use tauri::Manager;
|
||||
|
||||
pub mod utils;
|
||||
pub mod commands;
|
||||
pub mod utils;
|
||||
use crate::commands::{
|
||||
find_osu_installation, get_beatmapsets_count, get_hwid, get_osu_release_stream,
|
||||
get_osu_version, get_skins_count, valid_osu_folder, set_osu_config_value
|
||||
get_osu_version, get_skins_count, run_osu_updater, set_osu_config_value, valid_osu_folder,
|
||||
};
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
@@ -31,7 +31,8 @@ pub fn run() {
|
||||
get_skins_count,
|
||||
get_osu_version,
|
||||
get_osu_release_stream,
|
||||
set_osu_config_value
|
||||
set_osu_config_value,
|
||||
run_osu_updater
|
||||
])
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
|
@@ -1,5 +1,13 @@
|
||||
use std::ffi::OsString;
|
||||
use std::fs;
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
use std::path::Path;
|
||||
use std::ptr;
|
||||
use sysinfo::Pid;
|
||||
use winapi::{
|
||||
shared::minwindef::LPARAM,
|
||||
um::winuser::{FindWindowW, GetWindowTextW, GetWindowThreadProcessId},
|
||||
};
|
||||
|
||||
pub fn check_folder_completeness<P: AsRef<Path>>(folder_path: P, required_files: &[&str]) -> f32 {
|
||||
let mut found = 0;
|
||||
@@ -112,3 +120,28 @@ pub fn get_osu_config<P: AsRef<Path>>(
|
||||
|
||||
return Some(config_map);
|
||||
}
|
||||
|
||||
pub fn get_window_title_by_pid(pid: Pid) -> String {
|
||||
let mut window_title = String::new();
|
||||
|
||||
unsafe {
|
||||
let hwnd = FindWindowW(ptr::null_mut(), ptr::null_mut());
|
||||
|
||||
if hwnd.is_null() {
|
||||
return String::new();
|
||||
}
|
||||
|
||||
let mut process_id = 0;
|
||||
GetWindowThreadProcessId(hwnd, &mut process_id);
|
||||
|
||||
if process_id == pid.as_u32() {
|
||||
let mut title = vec![0u16; 512];
|
||||
let length = GetWindowTextW(hwnd, title.as_mut_ptr(), title.len() as i32);
|
||||
|
||||
let title = OsString::from_wide(&title[..length as usize]);
|
||||
window_title = title.to_string_lossy().into_owned();
|
||||
}
|
||||
}
|
||||
|
||||
window_title
|
||||
}
|
||||
|
Reference in New Issue
Block a user