Skip to content

Commit

Permalink
GUI: add --load-save-state arg to load a specific save state slot at …
Browse files Browse the repository at this point in the history
…launch (when used in combination with -f)
  • Loading branch information
jsgroth committed Oct 14, 2024
1 parent b07b36a commit cf86fad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
21 changes: 16 additions & 5 deletions frontend/jgenesis-gui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,26 +245,32 @@ impl<'a, T: Copy + FromStr> Widget for NumericTextEdit<'a, T> {
}
}

#[derive(Debug, Clone)]
pub struct LoadAtStartup {
pub file_path: String,
pub load_state_slot: Option<usize>,
}

pub struct App {
config: AppConfig,
state: AppState,
config_path: PathBuf,
emu_thread: EmuThreadHandle,
rom_list_thread: RomListThreadHandle,
startup_file_path: Option<String>,
load_at_startup: Option<LoadAtStartup>,
}

impl App {
#[must_use]
pub fn new(config_path: PathBuf, startup_file_path: Option<String>, ctx: Context) -> Self {
pub fn new(config_path: PathBuf, load_at_startup: Option<LoadAtStartup>, ctx: Context) -> Self {
let config = AppConfig::from_file(&config_path);
let state = AppState::from_config(&config);
let emu_thread = emuthread::spawn(ctx);

let rom_list_thread = RomListThreadHandle::spawn(Arc::clone(&state.rom_list));
rom_list_thread.request_scan(config.rom_search_dirs.clone());

Self { config, state, config_path, emu_thread, rom_list_thread, startup_file_path }
Self { config, state, config_path, emu_thread, rom_list_thread, load_at_startup }
}

fn open_file(&mut self, console: Option<Console>) {
Expand Down Expand Up @@ -1101,8 +1107,13 @@ impl eframe::App for App {
}

if self.state.rendered_first_frame {
if let Some(startup_file_path) = self.startup_file_path.take() {
self.launch_emulator(startup_file_path, None);
if let Some(load_at_startup) = self.load_at_startup.take() {
self.launch_emulator(load_at_startup.file_path, None);

if let Some(load_state_slot) = load_at_startup.load_state_slot {
self.emu_thread.send(EmuThreadCommand::LoadState { slot: load_state_slot });
}

self.state.close_on_emulator_exit = true;
}
}
Expand Down
27 changes: 21 additions & 6 deletions frontend/jgenesis-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;
use eframe::NativeOptions;
use egui::{Vec2, ViewportBuilder};
use env_logger::Env;
use jgenesis_gui::app::App;
use jgenesis_gui::app::{App, LoadAtStartup};
use std::path::PathBuf;

#[derive(Debug, Parser)]
Expand All @@ -15,6 +15,20 @@ struct Args {
/// when the emulator window is closed
#[arg(long = "file-path", short = 'f')]
startup_file_path: Option<String>,

/// In combination with -f, attempt to load the specified save state when launching the game.
/// This arg has no effect if -f/--file-path is not set
#[arg(long, value_name = "SLOT")]
load_save_state: Option<usize>,
}

impl Args {
fn load_at_startup(&self) -> Option<LoadAtStartup> {
self.startup_file_path.as_ref().map(|file_path| LoadAtStartup {
file_path: file_path.clone(),
load_state_slot: self.load_save_state,
})
}
}

// Attempt to detect if the application is running on a Steam Deck, and if it is then override
Expand Down Expand Up @@ -68,8 +82,10 @@ fn main() -> eframe::Result<()> {
#[cfg(all(unix, not(target_os = "macos")))]
steam_deck_dpi_hack();

let config_path =
args.config_path.map_or_else(jgenesis_native_config::default_config_path, PathBuf::from);
let config_path = args
.config_path
.as_ref()
.map_or_else(jgenesis_native_config::default_config_path, Into::<PathBuf>::into);
log::info!("Using config path '{}'", config_path.display());

if let Some(file_path) = &args.startup_file_path {
Expand All @@ -81,11 +97,10 @@ fn main() -> eframe::Result<()> {
..NativeOptions::default()
};

let load_at_startup = args.load_at_startup();
eframe::run_native(
"jgenesis",
options,
Box::new(|cc| {
Ok(Box::new(App::new(config_path, args.startup_file_path, cc.egui_ctx.clone())))
}),
Box::new(|cc| Ok(Box::new(App::new(config_path, load_at_startup, cc.egui_ctx.clone())))),
)
}

0 comments on commit cf86fad

Please sign in to comment.