Fix panic panic when loading eeprom ROMs while save file does not exist

Former-commit-id: 42bc08b6008169062c9d814a4e48e13eacb44a91
This commit is contained in:
Michel Heily 2020-02-01 13:20:14 +02:00
parent 7848cb609b
commit 71cccf7504
2 changed files with 27 additions and 26 deletions

View file

@ -287,21 +287,23 @@ pub struct EepromController {
impl EepromController {
pub fn new(path: Option<PathBuf>) -> 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;

View file

@ -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<PathBuf>) -> 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<BackupType> {
fn detect_backup_type(bytes: &[u8]) -> Option<BackupType> {
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<Vec<u8>> {