Make it so only one instance can be loaded

This commit is contained in:
2025-07-23 16:28:25 -07:00
parent 28002900f9
commit 6788e91f13
4 changed files with 29 additions and 8 deletions

View File

@@ -26,6 +26,7 @@ zip = "4.3.0"
libc = "0.2.174"
tauri-plugin-dialog = "2.3.1"
tauri-plugin-notification = "2.3.0"
sysinfo = "0.36.1"
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-single-instance = "2.3.2"

View File

@@ -10,6 +10,7 @@ use std::{
process::Command,
time::Duration,
};
use sysinfo::System;
use tauri::{AppHandle, Emitter, Manager};
use tauri_plugin_dialog::{DialogExt, MessageDialogKind};
use tauri_plugin_opener::OpenerExt;
@@ -48,6 +49,17 @@ pub async fn unzip_to_dir(zip_path: PathBuf, out_dir: PathBuf) -> zip::result::Z
.map_err(|e| zip::result::ZipError::Io(std::io::Error::new(std::io::ErrorKind::Other, e)))?
}
fn is_running_by_path(path: &PathBuf) -> bool {
let sys = System::new_all();
sys.processes().values().any(|proc| {
if let Some(exe) = proc.exe() {
exe == path
} else {
false
}
})
}
#[allow(unused_variables)]
#[tauri::command]
async fn download(
@@ -192,6 +204,14 @@ fn launch_game(app: AppHandle, name: String, executable: String, wine: bool) {
.current_dir(&game_folder)
.spawn()
} else {
if is_running_by_path(&game_path) {
app.dialog()
.message(format!("The version {} is already running.", name))
.kind(MessageDialogKind::Error)
.title("Game already running")
.show(|_| {});
return;
}
Command::new(&game_path).current_dir(&game_folder).spawn()
};