Skip to content

Commit 3d2dc37

Browse files
committed
fix: the problem of system operation fn failing when the software runs
for a long time resolve #44
1 parent 2403bfa commit 3d2dc37

4 files changed

Lines changed: 34 additions & 4 deletions

File tree

src-tauri/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
pub mod command_ext;
12
pub mod fs;
23
pub mod game_cover;
34
pub mod game_monitor;
45
pub mod launch;
5-
pub mod legacy_migration;
66
pub mod logs;
77
pub mod scan;

src-tauri/src/utils/command_ext.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::process::{Command, Stdio};
2+
3+
/// 为 GUI 环境下启动子进程提供统一的标准流处理。
4+
pub trait CommandGuiExt {
5+
/// 在 Windows GUI 进程中切断标准流继承,避免无控制台句柄导致的启动失败。
6+
fn gui_safe(&mut self) -> &mut Self;
7+
}
8+
9+
impl CommandGuiExt for Command {
10+
fn gui_safe(&mut self) -> &mut Self {
11+
#[cfg(target_os = "windows")]
12+
{
13+
self.stdin(Stdio::null())
14+
.stdout(Stdio::null())
15+
.stderr(Stdio::null())
16+
}
17+
18+
#[cfg(not(target_os = "windows"))]
19+
{
20+
self
21+
}
22+
}
23+
}

src-tauri/src/utils/fs.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(target_os = "windows")]
2+
use crate::utils::command_ext::CommandGuiExt;
13
use sea_orm::DatabaseConnection;
24
use serde::{Deserialize, Serialize};
35
use std::fs;
@@ -245,14 +247,18 @@ pub async fn open_directory(dir_path: String) -> Result<(), String> {
245247
// 虽然 Windows 系统本身支持正斜杠,但 Explorer 更喜欢原生的反斜杠格式
246248
let normalized_path = dir_path.replace('/', "\\");
247249

248-
let result = Command::new("explorer").arg(&normalized_path).spawn();
250+
let result = Command::new("explorer")
251+
.arg(&normalized_path)
252+
.gui_safe()
253+
.spawn();
249254

250255
match result {
251256
Ok(_) => Ok(()),
252257
Err(e) => {
253258
// 如果 explorer 失败,尝试使用 cmd /c start
254259
let fallback_result = Command::new("cmd")
255260
.args(["/c", "start", "", &normalized_path])
261+
.gui_safe()
256262
.spawn();
257263

258264
match fallback_result {

src-tauri/src/utils/launch/windows.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::database::dto::GameLaunchOptions;
2+
use crate::utils::command_ext::CommandGuiExt;
23
use crate::utils::game_monitor::{monitor_game, stop_game_session};
34
use serde::{Deserialize, Serialize};
45
use std::path::Path;
@@ -236,7 +237,7 @@ pub async fn launch_game<R: Runtime>(
236237
command.args(arguments);
237238
}
238239

239-
match command.spawn() {
240+
match command.gui_safe().spawn() {
240241
Ok(child) => {
241242
let process_id = child.id();
242243

@@ -386,7 +387,7 @@ async fn start_magpie_for_game(
386387
let mut command = Command::new(&magpie_path);
387388
command.arg("-t"); // tray mode
388389

389-
match command.spawn() {
390+
match command.gui_safe().spawn() {
390391
Ok(_child) => {
391392
info!("Magpie启动成功,等待游戏窗口加载...");
392393
}

0 commit comments

Comments
 (0)