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

chore: improved quality of cheat codes

Added more error handling.
parent 89c5fb0b
No related branches found
No related tags found
No related merge requests found
Pipeline #3710 failed
......@@ -41,18 +41,18 @@ impl GameGenie {
self.codes.contains_key(&addr)
}
pub fn get_addr(&self, addr: u16) -> &GameGenieCode {
self.codes.get(&addr).unwrap()
pub fn get_addr(&self, addr: u16) -> Result<&GameGenieCode, String> {
match self.codes.get(&addr) {
Some(code) => Ok(code),
None => Err(format!("Invalid address: {}", addr)),
}
}
pub fn add_code(&mut self, code: &str) -> Result<&GameGenieCode, String> {
let genie_code = match GameGenieCode::from_code(code, None) {
Ok(genie_code) => genie_code,
Err(message) => return Err(message),
};
let genie_code = GameGenieCode::from_code(code, None)?;
let addr = genie_code.addr;
self.codes.insert(addr, genie_code);
Ok(self.get_addr(addr))
self.get_addr(addr)
}
}
......
......@@ -48,18 +48,18 @@ impl GameShark {
self.codes.clear();
}
pub fn get_addr(&self, addr: u16) -> &GameSharkCode {
self.codes.get(&addr).unwrap()
pub fn get_addr(&self, addr: u16) -> Result<&GameSharkCode, String> {
match self.codes.get(&addr) {
Some(code) => Ok(code),
None => Err(format!("Invalid address: {}", addr)),
}
}
pub fn add_code(&mut self, code: &str) -> Result<&GameSharkCode, String> {
let genie_code = match GameSharkCode::from_code(code, &self.rom_type) {
Ok(genie_code) => genie_code,
Err(message) => return Err(message),
};
let addr = genie_code.addr;
self.codes.insert(addr, genie_code);
Ok(self.get_addr(addr))
let shark_code = GameSharkCode::from_code(code, &self.rom_type)?;
let addr = shark_code.addr;
self.codes.insert(addr, shark_code);
self.get_addr(addr)
}
pub fn writes(&self) -> Vec<(u16, u16, u8)> {
......@@ -89,7 +89,7 @@ impl Default for GameShark {
#[derive(Clone)]
pub struct GameSharkCode {
/// The Game Genie code that is going to be applied to the ROM.
/// The GameShark code that is going to be applied to the ROM.
code: String,
/// The RAM bank that the cheat code is going to be applied to,
......
......@@ -1023,7 +1023,7 @@ pub static GAME_GENIE: Mbc = Mbc {
// retrieves the Game Genie code that matches the current address
// keep in mind that this assumes that no more that one code is
// registered for the same memory address
let genie_code = game_genie.get_addr(addr);
let genie_code = game_genie.get_addr(addr).unwrap();
// obtains the current byte that is stored at the address using
// the MBC, this value will probably be patched
......
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