diff --git a/frontends/web/ts/gb.ts b/frontends/web/ts/gb.ts index f57b96a71f0f6ea32af765471a233d37a2b39e97..9ec6080eb70d052edaad7b085cf12ca170ab5342 100644 --- a/frontends/web/ts/gb.ts +++ b/frontends/web/ts/gb.ts @@ -720,7 +720,7 @@ export class GameboyEmulator extends EmulatorLogic implements Emulator { serializeState(): Uint8Array { if (!this.gameBoy) throw new Error("Unable to serialize state"); - return StateManager.save_wa(this.gameBoy, SaveStateFormat.Bos); + return StateManager.save_wa(this.gameBoy); } unserializeState(data: Uint8Array) { @@ -729,7 +729,7 @@ export class GameboyEmulator extends EmulatorLogic implements Emulator { } buildState(index: number, data: Uint8Array): SaveState { - const state = StateManager.read_bos_wa(data); + const state = StateManager.read_bos_auto_wa(data); return { index: index, timestamp: Number(state.timestamp_wa()), diff --git a/src/state.rs b/src/state.rs index 3155595971683c4d9e4402455c039d5a561601ff..ef44956f4d8597bd53f200b50938064e1de7fcb3 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1669,6 +1669,36 @@ impl StateManager { Ok(()) } + pub fn read_bos_auto(data: &[u8]) -> Result<BosState, Error> { + let data = &mut Cursor::new(data.to_vec()); + let format = if BoscState::is_bosc(data) { + SaveStateFormat::Bosc + } else if BosState::is_bos(data) { + SaveStateFormat::Bos + } else if BessState::is_bess(data) { + return Err(Error::CustomError(String::from( + "Incompatible save state file format (BESS)", + ))); + } else { + return Err(Error::CustomError(String::from( + "Unknown save state file format", + ))); + }; + match format { + SaveStateFormat::Bosc => { + let mut state = BoscState::default(); + state.read(data); + Ok(state.bos) + } + SaveStateFormat::Bos => { + let mut state = BosState::default(); + state.read(data); + Ok(state) + } + _ => unreachable!(), + } + } + pub fn read_bosc(data: &[u8]) -> Result<BoscState, Error> { let data = &mut Cursor::new(data.to_vec()); let mut state = BoscState::default(); @@ -1743,6 +1773,10 @@ impl StateManager { Self::load(data, gb, format).map_err(|e| e.to_string()) } + pub fn read_bos_auto_wa(data: &[u8]) -> Result<BosState, String> { + Self::read_bos_auto(data).map_err(|e| e.to_string()) + } + pub fn read_bosc_wa(data: &[u8]) -> Result<BoscState, String> { Self::read_bosc(data).map_err(|e| e.to_string()) }