From bb111f0d0b63be0ad4082f3dd7ccbee718a4ca7b Mon Sep 17 00:00:00 2001 From: Michel Heily Date: Fri, 31 Jan 2020 14:47:27 +0200 Subject: [PATCH] feat(save_override): Add option to override save type via commandline Former-commit-id: c4a00744ba20baf5f68072d6b9583616f61417e8 --- src/core/cartridge/backup/mod.rs | 17 +++++++++++++++++ src/core/cartridge/mod.rs | 1 + src/plat/sdl2/cli.yml | 11 +++++++++++ src/plat/sdl2/main.rs | 11 ++++++++++- 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/core/cartridge/backup/mod.rs b/src/core/cartridge/backup/mod.rs index 95369cc..f67c17a 100644 --- a/src/core/cartridge/backup/mod.rs +++ b/src/core/cartridge/backup/mod.rs @@ -1,4 +1,5 @@ use std::fmt; +use std::convert::TryFrom; mod backup_file; pub use backup_file::BackupFile; @@ -15,6 +16,22 @@ pub enum BackupType { AutoDetect = 5, } +impl TryFrom<&str> for BackupType { + type Error = String; + + fn try_from(s: &str) -> Result { + use BackupType::*; + match s { + "autodetect" => Ok(AutoDetect), + "sram" => Ok(Sram), + "flash128k" => Ok(Flash1M), + "flash64k" => Ok(Flash512), + "eeprom" => Ok(Eeprom), + _ => Err(format!("{} is not a valid save type", s)) + } + } +} + pub trait BackupMemoryInterface: Sized + fmt::Debug { fn write(&mut self, offset: usize, value: u8); fn read(&self, offset: usize) -> u8; diff --git a/src/core/cartridge/mod.rs b/src/core/cartridge/mod.rs index 8e96e02..045e935 100644 --- a/src/core/cartridge/mod.rs +++ b/src/core/cartridge/mod.rs @@ -9,6 +9,7 @@ mod backup; use backup::eeprom::SpiController; use backup::flash::Flash; use backup::{BackupFile, BackupMemoryInterface}; +pub use backup::BackupType; mod builder; pub use builder::GamepakBuilder; diff --git a/src/plat/sdl2/cli.yml b/src/plat/sdl2/cli.yml index 59a7748..565e53e 100644 --- a/src/plat/sdl2/cli.yml +++ b/src/plat/sdl2/cli.yml @@ -13,6 +13,17 @@ args: help: Sets the game-rom file to use required: false index: 1 + - save_type: + short: s + help: Override save type, useful for troublemaking games that fool the auto detection + required: false + default_value: autodetect + possible_values: + - sram + - flash128k + - flash64k + - eeprom + - autodetect - skip_bios: long: skip-bios help: Skip running bios and start from the ROM instead diff --git a/src/plat/sdl2/main.rs b/src/plat/sdl2/main.rs index 231de5f..b2b607f 100644 --- a/src/plat/sdl2/main.rs +++ b/src/plat/sdl2/main.rs @@ -17,6 +17,8 @@ use std::path::{Path, PathBuf}; use std::process; use std::time; +use std::convert::TryFrom; + #[macro_use] extern crate clap; @@ -34,6 +36,7 @@ use input::create_input; use video::{create_video_interface, SCREEN_HEIGHT, SCREEN_WIDTH}; extern crate rustboyadvance_ng; +use rustboyadvance_ng::core::cartridge::BackupType; use rustboyadvance_ng::prelude::*; use rustboyadvance_ng::util::FpsCounter; @@ -123,7 +126,13 @@ fn main() -> Result<(), Box> { let mut savestate_path = get_savestate_path(&Path::new(&rom_path)); let mut rom_name = Path::new(&rom_path).file_name().unwrap().to_str().unwrap(); - let gamepak = GamepakBuilder::new().file(Path::new(&rom_path)).build()?; + + let gamepak = GamepakBuilder::new() + .save_type(BackupType::try_from( + matches.value_of("save_type").unwrap(), + )?) + .file(Path::new(&rom_path)) + .build()?; let mut gba = GameBoyAdvance::new( arm7tdmi::Core::new(),