diff --git a/src/core/cartridge/backup/eeprom.rs b/src/core/cartridge/backup/eeprom.rs index 62eba1d..d199bea 100644 --- a/src/core/cartridge/backup/eeprom.rs +++ b/src/core/cartridge/backup/eeprom.rs @@ -287,21 +287,23 @@ pub struct EepromController { impl EepromController { pub fn new(path: Option) -> EepromController { let mut detect = true; - - let eeprom_type = if let Some(path) = &path { - let metadata = fs::metadata(&path).unwrap(); - let human_size = bytesize::ByteSize::b(metadata.len()); - let eeprom_type = match metadata.len() { - 512 => EepromType::Eeprom512, - 8192 => EepromType::Eeprom8k, - _ => panic!("invalid file size ({}) for eeprom save", human_size), - }; - detect = false; - info!("save file is size {}, assuming eeprom type is {:?}", human_size, eeprom_type); - eeprom_type - } else { - EepromType::Eeprom512 - }; + let mut eeprom_type = EepromType::Eeprom512; + if let Some(path) = &path { + if let Ok(metadata) = fs::metadata(&path) { + let human_size = bytesize::ByteSize::b(metadata.len()); + let assumed_type = match metadata.len() { + 512 => EepromType::Eeprom512, + 8192 => EepromType::Eeprom8k, + _ => panic!("invalid file size ({}) for eeprom save", human_size), + }; + detect = false; + info!( + "save file is size {}, assuming eeprom type is {:?}", + human_size, assumed_type + ); + eeprom_type = assumed_type; + } + } let mut result = EepromController::new_with_type(path, eeprom_type); result.detect = detect; diff --git a/src/core/cartridge/builder.rs b/src/core/cartridge/builder.rs index 63067ad..d3eaa52 100644 --- a/src/core/cartridge/builder.rs +++ b/src/core/cartridge/builder.rs @@ -94,10 +94,12 @@ impl GamepakBuilder { } if self.save_type == BackupType::AutoDetect { - let detected = detect_backup_type(&bytes)?; - info!("Detected Backup: {:?}", detected); - - self.save_type = detected; + if let Some(detected) = detect_backup_type(&bytes) { + info!("Detected Backup: {:?}", detected); + self.save_type = detected; + } else { + warn!("could not detect backup save type"); + } } let backup = create_backup(self.save_type, self.path); @@ -126,25 +128,22 @@ fn create_backup(backup_type: BackupType, rom_path: Option) -> BackupMe BackupType::Flash1M => BackupMedia::Flash(Flash::new(backup_path, FlashSize::Flash128k)), BackupType::Sram => BackupMedia::Sram(BackupFile::new(0x8000, backup_path)), BackupType::Eeprom => BackupMedia::Eeprom(EepromController::new(backup_path)), - BackupType::AutoDetect => panic!("called create_backup with backup_type==AutoDetect"), + BackupType::AutoDetect => BackupMedia::Undetected, } } -fn detect_backup_type(bytes: &[u8]) -> GBAResult { +fn detect_backup_type(bytes: &[u8]) -> Option { const ID_STRINGS: &'static [&'static str] = &["EEPROM", "SRAM", "FLASH_", "FLASH512_", "FLASH1M_"]; for i in 0..5 { let search = TwoWaySearcher::new(ID_STRINGS[i].as_bytes()); match search.search_in(bytes) { - Some(_) => return Ok(BackupType::from_u8(i as u8).unwrap()), + Some(_) => return Some(BackupType::from_u8(i as u8).unwrap()), _ => {} } } - warn!("could not detect backup save type"); - return Err(GBAError::CartridgeLoadError( - "could not detect backup save type".to_string(), - )); + None } fn load_rom(path: &Path) -> GBAResult> {