From dcee3d55b28e85c99c15d02a6d957c550cee0106 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Fri, 15 Nov 2024 11:53:25 +0100 Subject: [PATCH] [client] run game --- .../src-tauri/src/lib.rs | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/crates/lanspread-tauri-deno-ts/src-tauri/src/lib.rs b/crates/lanspread-tauri-deno-ts/src-tauri/src/lib.rs index d0ab4eb..083a6c9 100644 --- a/crates/lanspread-tauri-deno-ts/src-tauri/src/lib.rs +++ b/crates/lanspread-tauri-deno-ts/src-tauri/src/lib.rs @@ -2,7 +2,7 @@ use std::{ collections::HashSet, fs::File, net::SocketAddr, - path::{Path, PathBuf}, + path::{self, Path, PathBuf}, sync::Arc, }; @@ -56,6 +56,49 @@ fn install_game(id: String, state: tauri::State) -> bool { true } +#[tauri::command] +fn run_game(id: String, state: tauri::State) { + const FIRST_START_DONE_FILE: &str = ".softlan_first_start_done"; + + log::error!("run_game {id}"); + + let games_dir = + tauri::async_runtime::block_on(async { state.inner().games_dir.lock().await.clone() }); + + let games_dir = PathBuf::from(games_dir); + if !games_dir.exists() { + log::error!("games_dir {games_dir:?} does not exist"); + return; + } + + let game_path = games_dir.join(id); + + let game_setup_bin = game_path.join("game_setup.cmd"); + let game_start_bin = game_path.join("game_start.cmd"); + + let first_start_done_file = game_path.join(FIRST_START_DONE_FILE); + if !first_start_done_file.exists() && game_setup_bin.exists() { + if let Err(e) = std::process::Command::new(game_setup_bin) + .current_dir(&game_path) + .spawn() + { + log::error!("failed to run game_setup.cmd: {e}"); + return; + } else if let Err(e) = std::fs::File::create(FIRST_START_DONE_FILE) { + log::error!("failed to create {first_start_done_file:?}: {e}"); + } + } + + if game_start_bin.exists() { + if let Err(e) = std::process::Command::new(game_start_bin) + .current_dir(&game_path) + .spawn() + { + log::error!("failed to run game_start.cmd: {e}"); + } + } +} + fn set_game_install_state_from_path(game_db: &mut GameDB, path: &Path, installed: bool) { if let Some(file_name) = path.file_name() { if let Some(file_name) = file_name.to_str() { @@ -282,8 +325,9 @@ pub fn run() { .plugin(tauri_logger_builder.build()) .plugin(tauri_plugin_shell::init()) .invoke_handler(tauri::generate_handler![ - install_game, request_games, + install_game, + run_game, update_game_directory ]) .manage(lanspread_state)