wip
This commit is contained in:
@@ -340,6 +340,84 @@ fn set_game_install_state_from_path(game_db: &mut GameDB, path: &Path, installed
|
||||
}
|
||||
}
|
||||
|
||||
async fn refresh_games_list(app_handle: &AppHandle) {
|
||||
let state = app_handle.state::<LanSpreadState>();
|
||||
|
||||
let games_folder_lock = state.games_folder.clone();
|
||||
let games_db_lock = state.games.clone();
|
||||
|
||||
let games_folder = games_folder_lock.read().await.clone();
|
||||
|
||||
if games_folder.is_empty() {
|
||||
log::debug!("Games folder not set; skipping games list refresh");
|
||||
return;
|
||||
}
|
||||
|
||||
let path = PathBuf::from(&games_folder);
|
||||
|
||||
let mut installed_paths: Vec<PathBuf> = Vec::new();
|
||||
if path.exists() {
|
||||
match std::fs::read_dir(&path) {
|
||||
Ok(entries) => {
|
||||
for entry_result in entries {
|
||||
match entry_result {
|
||||
Ok(entry) => match entry.file_type() {
|
||||
Ok(file_type) if file_type.is_dir() => {
|
||||
let dir_path = entry.path();
|
||||
if dir_path.join("version.ini").exists() {
|
||||
installed_paths.push(dir_path);
|
||||
}
|
||||
}
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
log::warn!(
|
||||
"Failed to read file type for entry {}: {e}",
|
||||
entry.path().display()
|
||||
);
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
log::warn!("Failed to read directory entry in {}: {e}", path.display());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Failed to read game dir {}: {e}", path.display());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log::error!("game dir {} does not exist", path.display());
|
||||
}
|
||||
|
||||
let mut game_db = games_db_lock.write().await;
|
||||
|
||||
if game_db.games.is_empty() {
|
||||
log::debug!("Game database empty during refresh; skipping emit");
|
||||
return;
|
||||
}
|
||||
|
||||
game_db.set_all_uninstalled();
|
||||
|
||||
for dir_path in &installed_paths {
|
||||
set_game_install_state_from_path(&mut game_db, dir_path, true);
|
||||
}
|
||||
|
||||
let games_to_emit = game_db
|
||||
.all_games()
|
||||
.into_iter()
|
||||
.cloned()
|
||||
.collect::<Vec<Game>>();
|
||||
|
||||
drop(game_db);
|
||||
|
||||
if let Err(e) = app_handle.emit("games-list-updated", Some(games_to_emit)) {
|
||||
log::error!("Failed to emit games-list-updated event: {e}");
|
||||
} else {
|
||||
log::info!("Emitted games-list-updated event");
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn update_game_directory(app_handle: tauri::AppHandle, path: String) -> tauri::Result<()> {
|
||||
log::info!("update_game_directory: {path}");
|
||||
@@ -374,42 +452,7 @@ async fn update_game_directory(app_handle: tauri::AppHandle, path: String) -> ta
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let entries = match path.read_dir() {
|
||||
Ok(entries) => entries,
|
||||
Err(e) => {
|
||||
log::error!("Failed to read game dir: {e}");
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let mut game_db = app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.inner()
|
||||
.games
|
||||
.write()
|
||||
.await;
|
||||
|
||||
// Reset all games to uninstalled
|
||||
game_db.set_all_uninstalled();
|
||||
|
||||
// update game_db with installed games from real game directory
|
||||
entries.into_iter().for_each(|entry| {
|
||||
if let Ok(entry) = entry
|
||||
&& let Ok(path_type) = entry.file_type()
|
||||
&& path_type.is_dir()
|
||||
{
|
||||
let path = entry.path();
|
||||
if path.join("version.ini").exists() {
|
||||
set_game_install_state_from_path(&mut game_db, &path, true);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if let Err(e) = app_handle.emit("games-list-updated", Some(game_db.all_games())) {
|
||||
log::error!("Failed to emit games-list-updated event: {e}");
|
||||
}
|
||||
});
|
||||
refresh_games_list(&app_handle).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -422,14 +465,8 @@ async fn update_game_db(games: Vec<Game>, app: AppHandle) {
|
||||
let state = app.state::<LanSpreadState>();
|
||||
|
||||
// Store games list
|
||||
*state.games.write().await = GameDB::from(games.clone());
|
||||
|
||||
// Tell Frontend about new games list
|
||||
if let Err(e) = app.emit("games-list-updated", Some(games)) {
|
||||
log::error!("Failed to emit games-list-updated event: {e}");
|
||||
} else {
|
||||
log::info!("Emitted games-list-updated event");
|
||||
}
|
||||
*state.games.write().await = GameDB::from(games);
|
||||
refresh_games_list(&app).await;
|
||||
}
|
||||
|
||||
fn add_final_slash(path: &str) -> String {
|
||||
@@ -571,6 +608,8 @@ pub fn run() {
|
||||
*state.games.write().await = GameDB::from(games);
|
||||
}
|
||||
|
||||
refresh_games_list(&app_handle_clone).await;
|
||||
|
||||
// Only start peer system when we have a valid games directory
|
||||
match start_peer(games_folder, tx_peer_event_clone) {
|
||||
Ok(peer_ctrl) => {
|
||||
|
||||
Reference in New Issue
Block a user