From e00273f740f7fc095bc02c7ce6d0ba132a14c9bc Mon Sep 17 00:00:00 2001 From: Luke Date: Sat, 22 Jun 2024 14:20:50 -0700 Subject: [PATCH] fix: fixed roms path to default to current directory, if valid, and canonicalize --- tetanes/src/nes/config.rs | 2 +- tetanes/src/nes/event.rs | 17 ++++++++--------- tetanes/src/opts.rs | 5 ++++- tetanes/src/platform.rs | 2 +- tetanes/src/sys/platform/os.rs | 8 +++++--- tetanes/src/sys/platform/wasm.rs | 2 +- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/tetanes/src/nes/config.rs b/tetanes/src/nes/config.rs index 42fd81af..6dc975ec 100644 --- a/tetanes/src/nes/config.rs +++ b/tetanes/src/nes/config.rs @@ -112,7 +112,7 @@ impl Default for RendererConfig { }, zoom: 1.0, recent_roms: HashSet::default(), - roms_path: None, + roms_path: std::env::current_dir().ok(), show_perf_stats: false, show_messages: true, show_menubar: true, diff --git a/tetanes/src/nes/event.rs b/tetanes/src/nes/event.rs index 3b397379..b7b61021 100644 --- a/tetanes/src/nes/event.rs +++ b/tetanes/src/nes/event.rs @@ -16,7 +16,7 @@ use crate::{ use anyhow::anyhow; use egui::ViewportId; use serde::{Deserialize, Serialize}; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use tetanes_core::{ action::Action as DeckAction, apu::Channel, @@ -504,13 +504,12 @@ impl Running { UiEvent::Message((ty, msg)) => self.renderer.add_message(ty, msg), UiEvent::Error(err) => self.renderer.on_error(anyhow!(err)), UiEvent::LoadRomDialog => { - let dir = self - .cfg - .renderer - .roms_path - .as_deref() - .unwrap_or_else(|| Path::new(".")); - match open_file_dialog("Load ROM", "NES ROMs", &["nes"], dir) { + match open_file_dialog( + "Load ROM", + "NES ROMs", + &["nes"], + self.cfg.renderer.roms_path.as_ref(), + ) { Ok(maybe_path) => { if let Some(path) = maybe_path { self.nes_event(EmulationEvent::LoadRomPath(path)); @@ -527,7 +526,7 @@ impl Running { "Load Replay", "Replay Recording", &["replay"], - Config::default_data_dir(), + Some(Config::default_data_dir()), ) { Ok(maybe_path) => { if let Some(path) = maybe_path { diff --git a/tetanes/src/opts.rs b/tetanes/src/opts.rs index c478e886..d1852022 100644 --- a/tetanes/src/opts.rs +++ b/tetanes/src/opts.rs @@ -147,7 +147,10 @@ impl Opts { cfg.audio.enabled = !self.silent && cfg.audio.enabled; - cfg.renderer.roms_path = self.path.or(cfg.renderer.roms_path); + cfg.renderer.roms_path = self + .path + .or(cfg.renderer.roms_path) + .and_then(|path| path.canonicalize().ok()); cfg.renderer.fullscreen = self.fullscreen || cfg.renderer.fullscreen; Ok(cfg) diff --git a/tetanes/src/platform.rs b/tetanes/src/platform.rs index 471bea68..3b6fe729 100644 --- a/tetanes/src/platform.rs +++ b/tetanes/src/platform.rs @@ -34,7 +34,7 @@ pub fn open_file_dialog( title: impl Into, name: impl Into, extensions: &[impl ToString], - dir: impl AsRef, + dir: Option>, ) -> anyhow::Result> { platform::open_file_dialog_impl(title, name, extensions, dir) } diff --git a/tetanes/src/sys/platform/os.rs b/tetanes/src/sys/platform/os.rs index 2c332c15..f236539b 100644 --- a/tetanes/src/sys/platform/os.rs +++ b/tetanes/src/sys/platform/os.rs @@ -23,12 +23,14 @@ pub fn open_file_dialog_impl( title: impl Into, name: impl Into, extensions: &[impl ToString], - dir: impl AsRef, + dir: Option>, ) -> anyhow::Result> { - let dialog = rfd::FileDialog::new() + let mut dialog = rfd::FileDialog::new() .set_title(title) - .set_directory(dir.as_ref()) .add_filter(name, extensions); + if let Some(dir) = dir { + dialog = dialog.set_directory(dir.as_ref()); + } Ok(dialog.pick_file()) } diff --git a/tetanes/src/sys/platform/wasm.rs b/tetanes/src/sys/platform/wasm.rs index c981bb62..b8316496 100644 --- a/tetanes/src/sys/platform/wasm.rs +++ b/tetanes/src/sys/platform/wasm.rs @@ -48,7 +48,7 @@ pub fn open_file_dialog_impl( _title: impl Into, _name: impl Into, extensions: &[impl ToString], - _dir: impl AsRef, + _dir: Option>, ) -> anyhow::Result> { let input_id = match extensions[0].to_string().as_str() { "nes" => html_ids::ROM_INPUT,