diff --git a/plastic_ui/src/main.rs b/plastic_ui/src/main.rs index f6c1d51..b865020 100644 --- a/plastic_ui/src/main.rs +++ b/plastic_ui/src/main.rs @@ -89,31 +89,28 @@ impl App { } 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 self.nes.is_empty() { - return; + if let Some(path) = self.get_save_state_path(slot) { + let file = fs::File::open(&path).unwrap(); + self.nes.load_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); + fn get_save_state_path(&self, slot: u8) -> Option { + if self.nes.is_empty() { + return None; + } - let file = fs::File::open(&path).unwrap(); + let base_saved_states_dir = base_save_state_folder()?; + let filename = self.nes.save_state_file_name(slot)?; - self.nes.load_state(&file).unwrap(); + Some(base_saved_states_dir.join(filename)) } fn handle_input(&mut self, ctx: &egui::Context) {