[client] run game
This commit is contained in:
		| @@ -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<LanSpreadState>) -> bool { | ||||
|     true | ||||
| } | ||||
|  | ||||
| #[tauri::command] | ||||
| fn run_game(id: String, state: tauri::State<LanSpreadState>) { | ||||
|     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) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user