From 7b3596d226f657d8cc39e21ddd0dd50a90cf39bb Mon Sep 17 00:00:00 2001 From: BTMuli Date: Sat, 3 Jan 2026 13:48:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E9=87=87=E7=94=A8invoke?= =?UTF-8?q?=E8=80=8C=E9=9D=9Ecommand=E5=90=AF=E5=8A=A8=E6=B8=B8=E6=88=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 - pnpm-lock.yaml | 10 ---- src-tauri/Cargo.lock | 63 ---------------------- src-tauri/Cargo.toml | 5 -- src-tauri/capabilities/SubWindow.json | 2 - src-tauri/capabilities/TeyvatGuide.json | 7 --- src-tauri/src/commands.rs | 42 +++++++++++++++ src-tauri/src/lib.rs | 6 +-- src/components/app/t-sidebar.vue | 17 ++---- src/components/pageConfig/tc-gameBadge.vue | 19 ++----- 10 files changed, 54 insertions(+), 118 deletions(-) diff --git a/package.json b/package.json index f53ca294..7ce04a9a 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,6 @@ "@tauri-apps/plugin-opener": "^2.5.2", "@tauri-apps/plugin-os": "^2.3.2", "@tauri-apps/plugin-process": "^2.3.1", - "@tauri-apps/plugin-shell": "^2.3.3", "@tauri-apps/plugin-sql": "^2.3.1", "ajv": "^8.17.1", "artplayer": "^5.3.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5c4f6311..65e1800d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -50,9 +50,6 @@ importers: '@tauri-apps/plugin-process': specifier: ^2.3.1 version: 2.3.1 - '@tauri-apps/plugin-shell': - specifier: ^2.3.3 - version: 2.3.3 '@tauri-apps/plugin-sql': specifier: ^2.3.1 version: 2.3.1 @@ -1428,9 +1425,6 @@ packages: '@tauri-apps/plugin-process@2.3.1': resolution: {integrity: sha512-nCa4fGVaDL/B9ai03VyPOjfAHRHSBz5v6F/ObsB73r/dA3MHHhZtldaDMIc0V/pnUw9ehzr2iEG+XkSEyC0JJA==} - '@tauri-apps/plugin-shell@2.3.3': - resolution: {integrity: sha512-Xod+pRcFxmOWFWEnqH5yZcA7qwAMuaaDkMR1Sply+F8VfBj++CGnj2xf5UoialmjZ2Cvd8qrvSCbU+7GgNVsKQ==} - '@tauri-apps/plugin-sql@2.3.1': resolution: {integrity: sha512-iNgHnFIR+jRkx9INKVKepzMlxXtNkJUaWuhagFjT4dOttPaNyRnVHgwTjpqZhyVjiklDh2UdEPAJkQKiCPAekw==} @@ -5448,10 +5442,6 @@ snapshots: dependencies: '@tauri-apps/api': 2.9.1 - '@tauri-apps/plugin-shell@2.3.3': - dependencies: - '@tauri-apps/api': 2.9.1 - '@tauri-apps/plugin-sql@2.3.1': dependencies: '@tauri-apps/api': 2.9.1 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 5816a2da..f2d4c50f 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -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" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 31c0d0d4..bfbfeb5f 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -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" diff --git a/src-tauri/capabilities/SubWindow.json b/src-tauri/capabilities/SubWindow.json index dc2cedc9..b28c6d1c 100644 --- a/src-tauri/capabilities/SubWindow.json +++ b/src-tauri/capabilities/SubWindow.json @@ -29,8 +29,6 @@ "log:default", "log:allow-log", "opener:default", - "shell:default", - "shell:allow-open", "sql:default", "sql:allow-execute", "sql:allow-load", diff --git a/src-tauri/capabilities/TeyvatGuide.json b/src-tauri/capabilities/TeyvatGuide.json index cd97e864..887457a5 100644 --- a/src-tauri/capabilities/TeyvatGuide.json +++ b/src-tauri/capabilities/TeyvatGuide.json @@ -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"] diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 923edfaf..1cc39ae7 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -144,3 +144,45 @@ pub async fn quit_app(app_handle: AppHandle) { pub fn read_text_scale() -> Result { 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()) + } +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index dd5eb37e..853666b9 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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")] diff --git a/src/components/app/t-sidebar.vue b/src/components/app/t-sidebar.vue index 1b70904b..232af9c7 100644 --- a/src/components/app/t-sidebar.vue +++ b/src/components/app/t-sidebar.vue @@ -319,7 +319,6 @@ import { event, path, webviewWindow } from "@tauri-apps/api"; import { invoke } from "@tauri-apps/api/core"; import type { Event, UnlistenFn } from "@tauri-apps/api/event"; import { exists } from "@tauri-apps/plugin-fs"; -import { Command } from "@tauri-apps/plugin-shell"; import mhyClient from "@utils/TGClient.js"; import TGLogger from "@utils/TGLogger.js"; import { isRunInAdmin } from "@utils/toolFunc.js"; @@ -733,18 +732,10 @@ async function tryLaunchGame(): Promise { const isInAdmin = await isRunInAdmin(); if (!isInAdmin) { showSnackbar.success(`成功获取ticket:${resp},正在启动应用...`); - const cmd = Command.create( - "exec-sh", - [ - "-Command", - `Start-Process -FilePath '${gamePath}' -ArgumentList 'login_auth_ticket=${resp}' -Verb RunAs`, - ], - { cwd: gameDir.value, encoding: "utf-8" }, - ); - const result = await cmd.execute(); - if (result.stderr) { - await TGLogger.Error(`[sidebar][tryLaunchGame] 启动游戏本体失败!`); - showSnackbar.error(`启动游戏本体失败,代码:${result.code}`); + try { + await invoke("launch_game", { path: gamePath, ticket: resp }); + } catch (error) { + showSnackbar.error(`${error}`); } } else { try { diff --git a/src/components/pageConfig/tc-gameBadge.vue b/src/components/pageConfig/tc-gameBadge.vue index e882d1b7..f3a331b5 100644 --- a/src/components/pageConfig/tc-gameBadge.vue +++ b/src/components/pageConfig/tc-gameBadge.vue @@ -21,8 +21,8 @@ import passportReq from "@req/passportReq.js"; import useAppStore from "@store/app.js"; import useUserStore from "@store/user.js"; import { path } from "@tauri-apps/api"; +import { invoke } from "@tauri-apps/api/core"; import { exists } from "@tauri-apps/plugin-fs"; -import { Command } from "@tauri-apps/plugin-shell"; import TGLogger from "@utils/TGLogger.js"; import { storeToRefs } from "pinia"; @@ -57,19 +57,10 @@ async function tryPlayGame(): Promise { return; } showSnackbar.success(`成功获取ticket:${resp},正在启动应用...`); - const cmd = Command.create( - "exec-sh", - [ - "-Command", - `Start-Process -FilePath '${gamePath}' -ArgumentList 'login_auth_ticket=${resp}' -Verb RunAs`, - ], - { cwd: gameDir.value, encoding: "utf-8" }, - ); - const result = await cmd.execute(); - if (result.stderr) { - await TGLogger.Error(`[config][gameBadge] 启动游戏本体失败!`); - console.error(result.stderr); - showSnackbar.error(`[${result.code}] ${result.stderr}`); + try { + await invoke("launch_game", { path: gamePath, ticket: resp }); + } catch (error) { + showSnackbar.error(`${error}`); } }