Skip to content
Snippets Groups Projects
Verified Commit f4982fb7 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

fix: BESS loading issue

parent 9928b893
No related branches found
No related tags found
No related merge requests found
Pipeline #3352 passed
......@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Breaking issue with Libretro frontend and Linux
* Fix `window_counter` issue in PPU
* Issue with BESS header testing
## [0.9.13] - 2023-08-01
......
......@@ -6,6 +6,7 @@ use std::{
fs::File,
io::{Cursor, Read, Seek, SeekFrom, Write},
mem::size_of,
vec,
};
use crate::{
......@@ -460,7 +461,7 @@ impl BessState {
/// buffer represents a valid BESS (Best Effort Save State)
/// file structure, thought magic string validation.
pub fn is_bess(data: &mut Cursor<Vec<u8>>) -> bool {
data.seek(SeekFrom::End(-8)).unwrap();
data.seek(SeekFrom::End(-4)).unwrap();
let mut buffer = [0x00; 4];
data.read_exact(&mut buffer).unwrap();
let magic = u32::from_le_bytes(buffer);
......@@ -911,8 +912,8 @@ impl Serialize for BessInfo {
impl State for BessInfo {
fn from_gb(gb: &mut GameBoy) -> Result<Self, String> {
Ok(Self::new(
&gb.cartridge_i().rom_data()[0x134..=0x143],
&gb.cartridge_i().rom_data()[0x14e..=0x14f],
&gb.cartridge_i().rom_data()[0x0134..=0x0143],
&gb.cartridge_i().rom_data()[0x014e..=0x014f],
))
}
......@@ -1495,7 +1496,7 @@ impl StateManager {
} else if BessState::is_bess(data) {
SaveStateFormat::Bess
} else {
return Err(String::from("Unknown state file"));
return Err(String::from("Unknown save state file format"));
}
}
};
......@@ -1528,7 +1529,7 @@ impl StateManager {
Ok(state)
}
/// Obtains the thumbnail of the state file, this thumbnail is
/// Obtains the thumbnail of the save state file, this thumbnail is
/// stored in raw RGB format.
/// This operation is currently only supported for the BOS format.
pub fn thumbnail(data: &[u8], format: Option<SaveStateFormat>) -> Result<Vec<u8>, String> {
......@@ -1541,7 +1542,7 @@ impl StateManager {
} else if BessState::is_bess(data) {
SaveStateFormat::Bess
} else {
return Err(String::from("Unknown state file"));
return Err(String::from("Unknown save state file format"));
}
}
};
......@@ -1555,3 +1556,29 @@ impl StateManager {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::gb::GameBoy;
#[test]
fn test_load_bos() {
let mut gb = GameBoy::default();
gb.load(true);
gb.load_rom_file("res/roms/test/firstwhite.gb", None);
let data = StateManager::save(&mut gb, Some(SaveStateFormat::Bos)).unwrap();
StateManager::load(&data, &mut gb, Some(SaveStateFormat::Bos)).unwrap();
StateManager::load(&data, &mut gb, None).unwrap();
}
#[test]
fn test_load_bess() {
let mut gb = GameBoy::default();
gb.load(true);
gb.load_rom_file("res/roms/test/firstwhite.gb", None);
let data = StateManager::save(&mut gb, Some(SaveStateFormat::Bess)).unwrap();
StateManager::load(&data, &mut gb, Some(SaveStateFormat::Bess)).unwrap();
StateManager::load(&data, &mut gb, None).unwrap();
}
}
......@@ -125,7 +125,7 @@ pub fn get_timestamp() -> u64 {
mod tests {
use std::path::Path;
use super::replace_ext;
use super::{capitalize, replace_ext};
#[test]
fn test_change_extension() {
......@@ -156,4 +156,22 @@ mod tests {
let new_path = replace_ext("/path/to/directory/", "dat");
assert_eq!(new_path, None);
}
#[test]
fn test_capitalize_empty_string() {
let result = capitalize("");
assert_eq!(result, "");
}
#[test]
fn test_capitalize_single_character() {
let result = capitalize("a");
assert_eq!(result, "A");
}
#[test]
fn test_capitalize_multiple_characters() {
let result = capitalize("hello, world!");
assert_eq!(result, "Hello, world!");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment