runtime fixes by codex

This commit is contained in:
2025-11-12 23:42:34 +01:00
parent cc2c4ea8f3
commit 9141954d9b
@@ -22,11 +22,11 @@ struct LanSpreadState {
} }
#[tauri::command] #[tauri::command]
fn request_games(state: tauri::State<LanSpreadState>) { async fn request_games(state: tauri::State<'_, LanSpreadState>) -> tauri::Result<()> {
log::debug!("request_games"); log::debug!("request_games");
let peer_ctrl = let peer_ctrl_arc = state.inner().peer_ctrl.clone();
tauri::async_runtime::block_on(async { state.inner().peer_ctrl.read().await.clone() }); let peer_ctrl = peer_ctrl_arc.read().await.clone();
if let Some(peer_ctrl) = peer_ctrl { if let Some(peer_ctrl) = peer_ctrl {
if let Err(e) = peer_ctrl.send(PeerCommand::ListGames) { if let Err(e) = peer_ctrl.send(PeerCommand::ListGames) {
@@ -35,26 +35,31 @@ fn request_games(state: tauri::State<LanSpreadState>) {
} else { } else {
log::warn!("Peer system not initialized yet"); log::warn!("Peer system not initialized yet");
} }
Ok(())
} }
#[tauri::command] #[tauri::command]
fn install_game(id: String, state: tauri::State<LanSpreadState>) -> bool { async fn install_game(id: String, state: tauri::State<'_, LanSpreadState>) -> tauri::Result<bool> {
let already_in_download = tauri::async_runtime::block_on(async { let games_in_download = state.inner().games_in_download.clone();
if state.inner().games_in_download.read().await.contains(&id) { let already_in_download = {
let guard = games_in_download.read().await;
if guard.contains(&id) {
log::warn!("Game is already downloading: {id}"); log::warn!("Game is already downloading: {id}");
return true; true
} else {
false
} }
false };
});
if already_in_download { if already_in_download {
return false; return Ok(false);
} }
let peer_ctrl = let peer_ctrl_arc = state.inner().peer_ctrl.clone();
tauri::async_runtime::block_on(async { state.inner().peer_ctrl.read().await.clone() }); let peer_ctrl = peer_ctrl_arc.read().await.clone();
if let Some(peer_ctrl) = peer_ctrl { let handled = if let Some(peer_ctrl) = peer_ctrl {
if let Err(e) = peer_ctrl.send(PeerCommand::GetGame(id)) { if let Err(e) = peer_ctrl.send(PeerCommand::GetGame(id)) {
log::error!("Failed to send message to peer: {e:?}"); log::error!("Failed to send message to peer: {e:?}");
} }
@@ -62,7 +67,9 @@ fn install_game(id: String, state: tauri::State<LanSpreadState>) -> bool {
} else { } else {
log::warn!("Peer system not initialized yet"); log::warn!("Peer system not initialized yet");
false false
} };
Ok(handled)
} }
/// Backup the current game folder by renaming it to `___TO_BE_DELETE___GameNameHere` /// Backup the current game folder by renaming it to `___TO_BE_DELETE___GameNameHere`
@@ -137,22 +144,28 @@ fn cleanup_backup_folder(backup_path: &Path) -> eyre::Result<()> {
} }
#[tauri::command] #[tauri::command]
fn update_game(id: String, state: tauri::State<LanSpreadState>) -> bool { async fn update_game(id: String, state: tauri::State<'_, LanSpreadState>) -> tauri::Result<bool> {
let already_in_download = tauri::async_runtime::block_on(async { let games_in_download = state.inner().games_in_download.clone();
if state.inner().games_in_download.read().await.contains(&id) { let already_in_download = {
let guard = games_in_download.read().await;
if guard.contains(&id) {
log::warn!("Game is already downloading/updating: {id}"); log::warn!("Game is already downloading/updating: {id}");
return true; true
} else {
false
} }
false };
});
if already_in_download { if already_in_download {
return false; return Ok(false);
} }
// Get the games folder // Get the games folder
let games_folder = let games_folder_lock = state.inner().games_folder.clone();
tauri::async_runtime::block_on(async { state.inner().games_folder.read().await.clone() }); let games_folder = {
let guard = games_folder_lock.read().await;
guard.clone()
};
let games_folder = PathBuf::from(games_folder); let games_folder = PathBuf::from(games_folder);
let game_path = games_folder.join(&id); let game_path = games_folder.join(&id);
@@ -162,15 +175,15 @@ fn update_game(id: String, state: tauri::State<LanSpreadState>) -> bool {
Ok(path) => path, Ok(path) => path,
Err(e) => { Err(e) => {
log::error!("Failed to backup game folder for {id}: {e}"); log::error!("Failed to backup game folder for {id}: {e}");
return false; return Ok(false);
} }
}; };
log::info!("Starting update for game: {id}"); log::info!("Starting update for game: {id}");
// Start the download process // Start the download process
let peer_ctrl = let peer_ctrl_arc = state.inner().peer_ctrl.clone();
tauri::async_runtime::block_on(async { state.inner().peer_ctrl.read().await.clone() }); let peer_ctrl = peer_ctrl_arc.read().await.clone();
if let Some(peer_ctrl) = peer_ctrl { if let Some(peer_ctrl) = peer_ctrl {
if let Err(e) = peer_ctrl.send(PeerCommand::GetGame(id.clone())) { if let Err(e) = peer_ctrl.send(PeerCommand::GetGame(id.clone())) {
@@ -181,12 +194,12 @@ fn update_game(id: String, state: tauri::State<LanSpreadState>) -> bool {
log::error!("Failed to restore backup after download failure: {restore_err}"); log::error!("Failed to restore backup after download failure: {restore_err}");
} }
return false; return Ok(false);
} }
true Ok(true)
} else { } else {
log::warn!("Peer system not initialized yet"); log::warn!("Peer system not initialized yet");
false Ok(false)
} }
} }
@@ -216,18 +229,24 @@ fn run_as_admin(file: &str, params: &str, dir: &str) -> bool {
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
fn run_game_windows(id: String, state: tauri::State<LanSpreadState>) { async fn run_game_windows(
id: String,
state: tauri::State<'_, LanSpreadState>,
) -> tauri::Result<()> {
use std::fs::File; use std::fs::File;
const FIRST_START_DONE_FILE: &str = ".softlan_first_start_done"; const FIRST_START_DONE_FILE: &str = ".softlan_first_start_done";
let games_folder = let games_folder_lock = state.inner().games_folder.clone();
tauri::async_runtime::block_on(async { state.inner().games_folder.read().await.clone() }); let games_folder = {
let guard = games_folder_lock.read().await;
guard.clone()
};
let games_folder = PathBuf::from(games_folder); let games_folder = PathBuf::from(games_folder);
if !games_folder.exists() { if !games_folder.exists() {
log::error!("games_folder {games_folder:?} does not exist"); log::error!("games_folder {games_folder:?} does not exist");
return; return Ok(());
} }
let game_path = games_folder.join(id.clone()); let game_path = games_folder.join(id.clone());
@@ -249,7 +268,7 @@ fn run_game_windows(id: String, state: tauri::State<LanSpreadState>) {
if !result { if !result {
log::error!("failed to run game_setup.cmd"); log::error!("failed to run game_setup.cmd");
return; return Ok(());
} }
if let Err(e) = File::create(&first_start_done_file) { if let Err(e) = File::create(&first_start_done_file) {
@@ -272,18 +291,24 @@ fn run_game_windows(id: String, state: tauri::State<LanSpreadState>) {
log::error!("failed to run game_start.cmd"); log::error!("failed to run game_start.cmd");
} }
} }
Ok(())
} }
#[tauri::command] #[tauri::command]
fn run_game(id: String, state: tauri::State<LanSpreadState>) { async fn run_game(id: String, state: tauri::State<'_, LanSpreadState>) -> tauri::Result<()> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
run_game_windows(id, state); {
run_game_windows(id, state).await?;
}
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
{ {
let _ = state; let _ = state;
log::error!("run_game not implemented for this platform: id={id}"); log::error!("run_game not implemented for this platform: id={id}");
} }
Ok(())
} }
fn set_game_install_state_from_path(game_db: &mut GameDB, path: &Path, installed: bool) { fn set_game_install_state_from_path(game_db: &mut GameDB, path: &Path, installed: bool) {
@@ -316,18 +341,21 @@ fn set_game_install_state_from_path(game_db: &mut GameDB, path: &Path, installed
} }
#[tauri::command] #[tauri::command]
fn update_game_directory(app_handle: tauri::AppHandle, path: String) { async fn update_game_directory(app_handle: tauri::AppHandle, path: String) -> tauri::Result<()> {
log::info!("update_game_directory: {path}"); log::info!("update_game_directory: {path}");
let peer_ctrl = tauri::async_runtime::block_on(async { let peer_ctrl_lock = app_handle
app_handle .state::<LanSpreadState>()
.state::<LanSpreadState>() .inner()
.inner() .peer_ctrl
.peer_ctrl .clone();
.read() let games_folder_lock = app_handle
.await .state::<LanSpreadState>()
.clone() .inner()
}); .games_folder
.clone();
let peer_ctrl = peer_ctrl_lock.read().await.clone();
if let Some(peer_ctrl) = peer_ctrl if let Some(peer_ctrl) = peer_ctrl
&& let Err(e) = peer_ctrl.send(PeerCommand::SetGameDir(path.clone())) && let Err(e) = peer_ctrl.send(PeerCommand::SetGameDir(path.clone()))
@@ -336,28 +364,21 @@ fn update_game_directory(app_handle: tauri::AppHandle, path: String) {
} }
{ {
tauri::async_runtime::block_on(async { let mut games_folder = games_folder_lock.write().await;
let mut games_folder = app_handle games_folder.clone_from(&path);
.state::<LanSpreadState>()
.inner()
.games_folder
.write()
.await;
games_folder.clone_from(&path);
});
} }
let path = PathBuf::from(path); let path = PathBuf::from(path);
if !path.exists() { if !path.exists() {
log::error!("game dir {} does not exist", path.display()); log::error!("game dir {} does not exist", path.display());
return Ok(());
} }
let entries = match path.read_dir() { let entries = match path.read_dir() {
Ok(entries) => entries, Ok(entries) => entries,
Err(e) => { Err(e) => {
log::error!("Failed to read game dir: {e}"); log::error!("Failed to read game dir: {e}");
return; return Ok(());
} }
}; };
@@ -389,6 +410,8 @@ fn update_game_directory(app_handle: tauri::AppHandle, path: String) {
log::error!("Failed to emit games-list-updated event: {e}"); log::error!("Failed to emit games-list-updated event: {e}");
} }
}); });
Ok(())
} }
async fn update_game_db(games: Vec<Game>, app: AppHandle) { async fn update_game_db(games: Vec<Game>, app: AppHandle) {
@@ -556,7 +579,9 @@ pub fn run() {
log::info!("Peer system initialized successfully with games directory"); log::info!("Peer system initialized successfully with games directory");
// Start peer discovery and request games from other peers // Start peer discovery and request games from other peers
request_games(state); if let Err(e) = request_games(state).await {
log::error!("Failed to request games after peer init: {e}");
}
} }
Err(e) => { Err(e) => {
log::error!("Failed to initialize peer system: {e}"); log::error!("Failed to initialize peer system: {e}");