Newer
Older
/// Top level manager structure containing the
/// entrypoint static methods for saving and loading
/// BEES states for the Game Boy.
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![];
let mut state = BeesState::from_gb(gb)?;
state.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)?;
Ok(())
}