runtime fixes by codex
This commit is contained in:
@@ -22,11 +22,11 @@ struct LanSpreadState {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn request_games(state: tauri::State<LanSpreadState>) {
|
||||
async fn request_games(state: tauri::State<'_, LanSpreadState>) -> tauri::Result<()> {
|
||||
log::debug!("request_games");
|
||||
|
||||
let peer_ctrl =
|
||||
tauri::async_runtime::block_on(async { state.inner().peer_ctrl.read().await.clone() });
|
||||
let peer_ctrl_arc = state.inner().peer_ctrl.clone();
|
||||
let peer_ctrl = peer_ctrl_arc.read().await.clone();
|
||||
|
||||
if let Some(peer_ctrl) = peer_ctrl {
|
||||
if let Err(e) = peer_ctrl.send(PeerCommand::ListGames) {
|
||||
@@ -35,26 +35,31 @@ fn request_games(state: tauri::State<LanSpreadState>) {
|
||||
} else {
|
||||
log::warn!("Peer system not initialized yet");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn install_game(id: String, state: tauri::State<LanSpreadState>) -> bool {
|
||||
let already_in_download = tauri::async_runtime::block_on(async {
|
||||
if state.inner().games_in_download.read().await.contains(&id) {
|
||||
async fn install_game(id: String, state: tauri::State<'_, LanSpreadState>) -> tauri::Result<bool> {
|
||||
let games_in_download = state.inner().games_in_download.clone();
|
||||
let already_in_download = {
|
||||
let guard = games_in_download.read().await;
|
||||
if guard.contains(&id) {
|
||||
log::warn!("Game is already downloading: {id}");
|
||||
return true;
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if already_in_download {
|
||||
return false;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let peer_ctrl =
|
||||
tauri::async_runtime::block_on(async { state.inner().peer_ctrl.read().await.clone() });
|
||||
let peer_ctrl_arc = state.inner().peer_ctrl.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)) {
|
||||
log::error!("Failed to send message to peer: {e:?}");
|
||||
}
|
||||
@@ -62,7 +67,9 @@ fn install_game(id: String, state: tauri::State<LanSpreadState>) -> bool {
|
||||
} else {
|
||||
log::warn!("Peer system not initialized yet");
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
Ok(handled)
|
||||
}
|
||||
|
||||
/// 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]
|
||||
fn update_game(id: String, state: tauri::State<LanSpreadState>) -> bool {
|
||||
let already_in_download = tauri::async_runtime::block_on(async {
|
||||
if state.inner().games_in_download.read().await.contains(&id) {
|
||||
async fn update_game(id: String, state: tauri::State<'_, LanSpreadState>) -> tauri::Result<bool> {
|
||||
let games_in_download = state.inner().games_in_download.clone();
|
||||
let already_in_download = {
|
||||
let guard = games_in_download.read().await;
|
||||
if guard.contains(&id) {
|
||||
log::warn!("Game is already downloading/updating: {id}");
|
||||
return true;
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if already_in_download {
|
||||
return false;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
// Get the games folder
|
||||
let games_folder =
|
||||
tauri::async_runtime::block_on(async { state.inner().games_folder.read().await.clone() });
|
||||
let games_folder_lock = state.inner().games_folder.clone();
|
||||
let games_folder = {
|
||||
let guard = games_folder_lock.read().await;
|
||||
guard.clone()
|
||||
};
|
||||
|
||||
let games_folder = PathBuf::from(games_folder);
|
||||
let game_path = games_folder.join(&id);
|
||||
@@ -162,15 +175,15 @@ fn update_game(id: String, state: tauri::State<LanSpreadState>) -> bool {
|
||||
Ok(path) => path,
|
||||
Err(e) => {
|
||||
log::error!("Failed to backup game folder for {id}: {e}");
|
||||
return false;
|
||||
return Ok(false);
|
||||
}
|
||||
};
|
||||
|
||||
log::info!("Starting update for game: {id}");
|
||||
|
||||
// Start the download process
|
||||
let peer_ctrl =
|
||||
tauri::async_runtime::block_on(async { state.inner().peer_ctrl.read().await.clone() });
|
||||
let peer_ctrl_arc = state.inner().peer_ctrl.clone();
|
||||
let peer_ctrl = peer_ctrl_arc.read().await.clone();
|
||||
|
||||
if let Some(peer_ctrl) = peer_ctrl {
|
||||
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}");
|
||||
}
|
||||
|
||||
return false;
|
||||
return Ok(false);
|
||||
}
|
||||
true
|
||||
Ok(true)
|
||||
} else {
|
||||
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")]
|
||||
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;
|
||||
|
||||
const FIRST_START_DONE_FILE: &str = ".softlan_first_start_done";
|
||||
|
||||
let games_folder =
|
||||
tauri::async_runtime::block_on(async { state.inner().games_folder.read().await.clone() });
|
||||
let games_folder_lock = state.inner().games_folder.clone();
|
||||
let games_folder = {
|
||||
let guard = games_folder_lock.read().await;
|
||||
guard.clone()
|
||||
};
|
||||
|
||||
let games_folder = PathBuf::from(games_folder);
|
||||
if !games_folder.exists() {
|
||||
log::error!("games_folder {games_folder:?} does not exist");
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let game_path = games_folder.join(id.clone());
|
||||
@@ -249,7 +268,7 @@ fn run_game_windows(id: String, state: tauri::State<LanSpreadState>) {
|
||||
|
||||
if !result {
|
||||
log::error!("failed to run game_setup.cmd");
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[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")]
|
||||
run_game_windows(id, state);
|
||||
{
|
||||
run_game_windows(id, state).await?;
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
let _ = state;
|
||||
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) {
|
||||
@@ -316,18 +341,21 @@ fn set_game_install_state_from_path(game_db: &mut GameDB, path: &Path, installed
|
||||
}
|
||||
|
||||
#[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}");
|
||||
|
||||
let peer_ctrl = tauri::async_runtime::block_on(async {
|
||||
app_handle
|
||||
let peer_ctrl_lock = app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.inner()
|
||||
.peer_ctrl
|
||||
.read()
|
||||
.await
|
||||
.clone()
|
||||
});
|
||||
.clone();
|
||||
let games_folder_lock = app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.inner()
|
||||
.games_folder
|
||||
.clone();
|
||||
|
||||
let peer_ctrl = peer_ctrl_lock.read().await.clone();
|
||||
|
||||
if let Some(peer_ctrl) = peer_ctrl
|
||||
&& 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 = app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.inner()
|
||||
.games_folder
|
||||
.write()
|
||||
.await;
|
||||
|
||||
let mut games_folder = games_folder_lock.write().await;
|
||||
games_folder.clone_from(&path);
|
||||
});
|
||||
}
|
||||
|
||||
let path = PathBuf::from(path);
|
||||
if !path.exists() {
|
||||
log::error!("game dir {} does not exist", path.display());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let entries = match path.read_dir() {
|
||||
Ok(entries) => entries,
|
||||
Err(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}");
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
// 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) => {
|
||||
log::error!("Failed to initialize peer system: {e}");
|
||||
|
||||
Reference in New Issue
Block a user