♻️ 采用invoke而非command启动游戏

This commit is contained in:
BTMuli
2026-01-03 13:48:08 +08:00
parent a520d378a6
commit 7b3596d226
10 changed files with 54 additions and 118 deletions

63
src-tauri/Cargo.lock generated
View File

@@ -26,7 +26,6 @@ dependencies = [
"tauri-plugin-opener",
"tauri-plugin-os",
"tauri-plugin-process",
"tauri-plugin-shell",
"tauri-plugin-single-instance",
"tauri-plugin-sql",
"tauri-utils",
@@ -3864,16 +3863,6 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "os_pipe"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
dependencies = [
"libc",
"windows-sys 0.61.2",
]
[[package]]
name = "pango"
version = "0.18.3"
@@ -5475,44 +5464,12 @@ dependencies = [
"digest",
]
[[package]]
name = "shared_child"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e362d9935bc50f019969e2f9ecd66786612daae13e8f277be7bfb66e8bed3f7"
dependencies = [
"libc",
"sigchld",
"windows-sys 0.60.2",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "sigchld"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47106eded3c154e70176fc83df9737335c94ce22f821c32d17ed1db1f83badb1"
dependencies = [
"libc",
"os_pipe",
"signal-hook",
]
[[package]]
name = "signal-hook"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.8"
@@ -6391,26 +6348,6 @@ dependencies = [
"tauri-plugin",
]
[[package]]
name = "tauri-plugin-shell"
version = "2.3.3"
source = "git+ssh://git@github.com/tauri-apps/plugins-workspace.git?branch=v2#31415effdf5a9ced19934a681cb044a732174088"
dependencies = [
"encoding_rs",
"log",
"open",
"os_pipe",
"regex",
"schemars 0.8.22",
"serde",
"serde_json",
"shared_child",
"tauri",
"tauri-plugin",
"thiserror 2.0.17",
"tokio",
]
[[package]]
name = "tauri-plugin-single-instance"
version = "2.3.6"

View File

@@ -101,11 +101,6 @@ branch = "v2"
git = "ssh://git@github.com/tauri-apps/plugins-workspace.git"
branch = "v2"
# shell 插件
[dependencies.tauri-plugin-shell]
git = "ssh://git@github.com/tauri-apps/plugins-workspace.git"
branch = "v2"
# single-instance 插件
[dependencies.tauri-plugin-single-instance]
git = "ssh://git@github.com/tauri-apps/plugins-workspace.git"

View File

@@ -29,8 +29,6 @@
"log:default",
"log:allow-log",
"opener:default",
"shell:default",
"shell:allow-open",
"sql:default",
"sql:allow-execute",
"sql:allow-load",

View File

@@ -35,9 +35,6 @@
"opener:default",
"process:default",
"process:allow-exit",
"shell:default",
"shell:allow-open",
"shell:allow-execute",
"sql:default",
"sql:allow-load",
"sql:allow-execute",
@@ -58,10 +55,6 @@
{ "url": "https://*.hoyoverse.com/*" },
{ "url": "https://api.hakush.in/*" }
]
},
{
"identifier": "shell:allow-execute",
"allow": [{ "name": "exec-sh", "cmd": "powershell", "args": true }]
}
],
"platforms": ["windows", "macOS"]

View File

@@ -144,3 +144,45 @@ pub async fn quit_app(app_handle: AppHandle) {
pub fn read_text_scale() -> Result<f64, String> {
utils::read_text_scale_factor()
}
#[tauri::command]
pub fn launch_game(path: String, ticket: String) -> Result<(), String> {
#[cfg(target_os = "windows")]
{
// 依赖 widestring 和 windows-sys
use widestring::U16CString;
use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::UI::Shell::ShellExecuteW;
use windows_sys::Win32::UI::WindowsAndMessaging::SW_SHOWNORMAL;
// 构造参数字符串
let args = format!("login_auth_ticket={}", ticket);
// 转为 UTF-16 C 字符串
let operation =
U16CString::from_str("runas").map_err(|e| format!("encode operation error: {}", e))?;
let file = U16CString::from_str(&path).map_err(|e| format!("encode path error: {}", e))?;
let params = U16CString::from_str(&args).map_err(|e| format!("encode params error: {}", e))?;
// 调用 ShellExecuteW
unsafe {
let res = ShellExecuteW(
0 as HWND,
operation.as_ptr(),
file.as_ptr(),
params.as_ptr(),
std::ptr::null(),
SW_SHOWNORMAL,
);
let code = res as isize;
if code <= 32 {
return Err(format!("ShellExecuteW failed, code: {}.", code));
}
}
Ok(())
}
#[cfg(not(target_os = "windows"))]
{
Err("This command is only supported on Windows".into())
}
}

View File

@@ -13,8 +13,8 @@ mod yae;
use crate::client::create_mhy_client;
use crate::commands::{
create_window, execute_js, get_dir_size, hide_main_window, init_app, is_in_admin, quit_app,
read_text_scale,
create_window, execute_js, get_dir_size, hide_main_window, init_app, is_in_admin, launch_game,
quit_app, read_text_scale,
};
use tauri::{generate_context, generate_handler, Emitter, Manager, Window, WindowEvent};
@@ -82,7 +82,6 @@ pub fn run() {
.plugin(tauri_plugin_opener::init())
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_sql::Builder::default().build())
.plugin(plugins::custom_log::build_log_plugin())
.setup(|_app| {
@@ -107,6 +106,7 @@ pub fn run() {
hide_main_window,
quit_app,
read_text_scale,
launch_game,
#[cfg(target_os = "windows")]
yae::call_yae_dll,
#[cfg(target_os = "windows")]