Newer
Older
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
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)?;
Ok(())
}