mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2026-03-16 04:03:17 +08:00
♻️ 采用invoke而非command启动游戏
This commit is contained in:
@@ -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",
|
||||
|
||||
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
@@ -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
|
||||
|
||||
63
src-tauri/Cargo.lock
generated
63
src-tauri/Cargo.lock
generated
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -29,8 +29,6 @@
|
||||
"log:default",
|
||||
"log:allow-log",
|
||||
"opener:default",
|
||||
"shell:default",
|
||||
"shell:allow-open",
|
||||
"sql:default",
|
||||
"sql:allow-execute",
|
||||
"sql:allow-load",
|
||||
|
||||
@@ -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"]
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -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<void> {
|
||||
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 {
|
||||
|
||||
@@ -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<void> {
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user