diff --git a/plastic_tui/src/ui.rs b/plastic_tui/src/ui.rs index 7a9fb5c..3ac7a65 100644 --- a/plastic_tui/src/ui.rs +++ b/plastic_tui/src/ui.rs @@ -161,32 +161,31 @@ impl Ui { } fn save_state(&mut self, slot: u8) { - if self.nes.is_empty() { - return; + if let Some(path) = self.get_save_state_path(slot) { + let file = fs::File::create(&path).unwrap(); + self.nes.save_state(&file).unwrap(); } - - let base_saved_states_dir = base_save_state_folder().unwrap(); - let filename = self.nes.save_state_file_name(slot).unwrap(); - let path = base_saved_states_dir.join(&filename); - - let file = fs::File::create(&path).unwrap(); - - self.nes.save_state(&file).unwrap(); } fn load_state(&mut self, slot: u8) { + if let Some(path) = self.get_save_state_path(slot) { + let file = fs::File::open(&path).unwrap(); + self.nes.load_state(&file).unwrap(); + } + } + + fn get_save_state_path(&self, slot: u8) -> Option { if self.nes.is_empty() { - return; + return None; } - let base_saved_states_dir = base_save_state_folder().unwrap(); - let filename = self.nes.save_state_file_name(slot).unwrap(); - let path = base_saved_states_dir.join(&filename); + let base_saved_states_dir = base_save_state_folder()?; + let filename = self.nes.save_state_file_name(slot)?; + + Some(base_saved_states_dir.join(filename)) +} - let file = fs::File::open(&path).unwrap(); - self.nes.load_state(&file).unwrap(); - } fn reset_menu(&mut self) { let mut save_state_items = Vec::with_capacity(10); @@ -536,7 +535,6 @@ impl Ui { thread::sleep(remaining); } } - disable_raw_mode().unwrap(); execute!( io::stdout(),