♻️ 采用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

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")]