diff --git a/frontends/sdl/src/main.rs b/frontends/sdl/src/main.rs index 5b7c56dff6ef43a311b536c7b9359545301d46b6..8e45d89039119370117c901af1026ae95dfdba1a 100644 --- a/frontends/sdl/src/main.rs +++ b/frontends/sdl/src/main.rs @@ -14,7 +14,7 @@ use boytacean::{ ppu::PaletteInfo, rom::Cartridge, serial::{NullDevice, SerialDevice}, - state::{load_state_file, save_state_file}, + state::StateManager, util::{replace_ext, write_file}, }; use chrono::Utc; @@ -286,7 +286,7 @@ impl Emulator { } fn save_state(&mut self, file_path: &str) { - if let Err(message) = save_state_file(file_path, &mut self.system) { + if let Err(message) = StateManager::save_file(file_path, &mut self.system) { println!("Error saving state: {}", message) } else { println!("Saved state into: {}", file_path) @@ -294,7 +294,7 @@ impl Emulator { } fn load_state(&mut self, file_path: &str) { - if let Err(message) = load_state_file(file_path, &mut self.system) { + if let Err(message) = StateManager::load_file(file_path, &mut self.system) { println!("Error loading state: {}", message) } else { println!("Loaded state from: {}", file_path) diff --git a/src/state.rs b/src/state.rs index e671bd80e77c7a66b1d768d32d7c244050203342..df9717e4306357ee5a5050ea81747f7bd7698f03 100644 --- a/src/state.rs +++ b/src/state.rs @@ -966,37 +966,41 @@ impl Default for BeesMbc { } } -pub fn save_state_file(file_path: &str, gb: &mut GameBoy) -> Result<(), String> { - let mut file = match File::create(file_path) { - Ok(file) => file, - Err(_) => return Err(format!("Failed to open file: {}", file_path)), - }; - let data = save_state(gb)?; - file.write_all(&data).unwrap(); - Ok(()) -} - -pub fn save_state(gb: &mut GameBoy) -> Result<Vec<u8>, String> { - let mut data: Vec<u8> = vec![]; - BeesState::from_gb(gb).write(&mut data); - Ok(data) -} - -pub fn load_state_file(file_path: &str, gb: &mut GameBoy) -> Result<(), String> { - let mut file = match File::open(file_path) { - Ok(file) => file, - Err(_) => return Err(format!("Failed to open file: {}", file_path)), - }; - let mut data = vec![]; - file.read_to_end(&mut data).unwrap(); - load_state(&data, gb)?; - Ok(()) -} - -pub fn load_state(data: &[u8], gb: &mut GameBoy) -> Result<(), String> { - let mut state = BeesState::default(); - state.read(&mut Cursor::new(data.to_vec())); - state.to_gb(gb)?; - print!("{}", state); - Ok(()) +pub struct StateManager; + +impl StateManager { + pub fn save_file(file_path: &str, gb: &mut GameBoy) -> Result<(), String> { + let mut file = match File::create(file_path) { + Ok(file) => file, + Err(_) => return Err(format!("Failed to open file: {}", file_path)), + }; + let data = Self::save(gb)?; + file.write_all(&data).unwrap(); + Ok(()) + } + + pub fn save(gb: &mut GameBoy) -> Result<Vec<u8>, String> { + let mut data: Vec<u8> = vec![]; + BeesState::from_gb(gb).write(&mut data); + Ok(data) + } + + pub fn load_file(file_path: &str, gb: &mut GameBoy) -> Result<(), String> { + let mut file = match File::open(file_path) { + Ok(file) => file, + Err(_) => return Err(format!("Failed to open file: {}", file_path)), + }; + let mut data = vec![]; + file.read_to_end(&mut data).unwrap(); + Self::load(&data, gb)?; + Ok(()) + } + + pub fn load(data: &[u8], gb: &mut GameBoy) -> Result<(), String> { + let mut state = BeesState::default(); + state.read(&mut Cursor::new(data.to_vec())); + state.to_gb(gb)?; + print!("{}", state); + Ok(()) + } }