Fix panic panic when loading eeprom ROMs while save file does not exist
Former-commit-id: 42bc08b6008169062c9d814a4e48e13eacb44a91
This commit is contained in:
parent
7848cb609b
commit
71cccf7504
|
@ -287,21 +287,23 @@ pub struct EepromController {
|
||||||
impl EepromController {
|
impl EepromController {
|
||||||
pub fn new(path: Option<PathBuf>) -> EepromController {
|
pub fn new(path: Option<PathBuf>) -> EepromController {
|
||||||
let mut detect = true;
|
let mut detect = true;
|
||||||
|
let mut eeprom_type = EepromType::Eeprom512;
|
||||||
let eeprom_type = if let Some(path) = &path {
|
if let Some(path) = &path {
|
||||||
let metadata = fs::metadata(&path).unwrap();
|
if let Ok(metadata) = fs::metadata(&path) {
|
||||||
let human_size = bytesize::ByteSize::b(metadata.len());
|
let human_size = bytesize::ByteSize::b(metadata.len());
|
||||||
let eeprom_type = match metadata.len() {
|
let assumed_type = match metadata.len() {
|
||||||
512 => EepromType::Eeprom512,
|
512 => EepromType::Eeprom512,
|
||||||
8192 => EepromType::Eeprom8k,
|
8192 => EepromType::Eeprom8k,
|
||||||
_ => panic!("invalid file size ({}) for eeprom save", human_size),
|
_ => panic!("invalid file size ({}) for eeprom save", human_size),
|
||||||
};
|
};
|
||||||
detect = false;
|
detect = false;
|
||||||
info!("save file is size {}, assuming eeprom type is {:?}", human_size, eeprom_type);
|
info!(
|
||||||
eeprom_type
|
"save file is size {}, assuming eeprom type is {:?}",
|
||||||
} else {
|
human_size, assumed_type
|
||||||
EepromType::Eeprom512
|
);
|
||||||
};
|
eeprom_type = assumed_type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut result = EepromController::new_with_type(path, eeprom_type);
|
let mut result = EepromController::new_with_type(path, eeprom_type);
|
||||||
result.detect = detect;
|
result.detect = detect;
|
||||||
|
|
|
@ -94,10 +94,12 @@ impl GamepakBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.save_type == BackupType::AutoDetect {
|
if self.save_type == BackupType::AutoDetect {
|
||||||
let detected = detect_backup_type(&bytes)?;
|
if let Some(detected) = detect_backup_type(&bytes) {
|
||||||
info!("Detected Backup: {:?}", detected);
|
info!("Detected Backup: {:?}", detected);
|
||||||
|
self.save_type = detected;
|
||||||
self.save_type = detected;
|
} else {
|
||||||
|
warn!("could not detect backup save type");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let backup = create_backup(self.save_type, self.path);
|
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::Flash1M => BackupMedia::Flash(Flash::new(backup_path, FlashSize::Flash128k)),
|
||||||
BackupType::Sram => BackupMedia::Sram(BackupFile::new(0x8000, backup_path)),
|
BackupType::Sram => BackupMedia::Sram(BackupFile::new(0x8000, backup_path)),
|
||||||
BackupType::Eeprom => BackupMedia::Eeprom(EepromController::new(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] =
|
const ID_STRINGS: &'static [&'static str] =
|
||||||
&["EEPROM", "SRAM", "FLASH_", "FLASH512_", "FLASH1M_"];
|
&["EEPROM", "SRAM", "FLASH_", "FLASH512_", "FLASH1M_"];
|
||||||
|
|
||||||
for i in 0..5 {
|
for i in 0..5 {
|
||||||
let search = TwoWaySearcher::new(ID_STRINGS[i].as_bytes());
|
let search = TwoWaySearcher::new(ID_STRINGS[i].as_bytes());
|
||||||
match search.search_in(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");
|
None
|
||||||
return Err(GBAError::CartridgeLoadError(
|
|
||||||
"could not detect backup save type".to_string(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_rom(path: &Path) -> GBAResult<Vec<u8>> {
|
fn load_rom(path: &Path) -> GBAResult<Vec<u8>> {
|
||||||
|
|
Reference in a new issue