diff --git a/Cocoa/Document.h b/Cocoa/Document.h index 1117c26f..93537887 100644 --- a/Cocoa/Document.h +++ b/Cocoa/Document.h @@ -3,6 +3,8 @@ #include "GBImageView.h" #include "GBSplitView.h" +@class GBCheatWindowController; + @interface Document : NSDocument @property (strong) IBOutlet GBView *view; @property (strong) IBOutlet NSTextView *consoleOutput; @@ -34,6 +36,7 @@ @property (strong) IBOutlet GBSplitView *debuggerSplitView; @property (strong) IBOutlet NSBox *debuggerVerticalLine; @property (strong) IBOutlet NSPanel *cheatsWindow; +@property (strong) IBOutlet GBCheatWindowController *cheatWindowController; -(uint8_t) readMemory:(uint16_t) addr; -(void) writeMemory:(uint16_t) addr value:(uint8_t)value; diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 3c83fab9..823204c2 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -7,6 +7,7 @@ #include "HexFiend/HexFiend.h" #include "GBMemoryByteArray.h" #include "GBWarningPopover.h" +#include "GBCheatWindowController.h" /* Todo: The general Objective-C coding style conflicts with SameBoy's. This file needs a cleanup. */ /* Todo: Split into category files! This is so messy!!! */ @@ -359,6 +360,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) self.audioClient = nil; self.view.mouseHidingEnabled = NO; GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); + GB_save_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]); stopping = false; } @@ -643,6 +645,8 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) NSString *rom_warnings = [self captureOutputForBlock:^{ GB_load_rom(&gb, [self.fileName UTF8String]); GB_load_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); + GB_load_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]); + [self.cheatWindowController.cheatsTable reloadData]; GB_debugger_clear_symbols(&gb); GB_debugger_load_symbol_file(&gb, [[[NSBundle mainBundle] pathForResource:@"registers" ofType:@"sym"] UTF8String]); GB_debugger_load_symbol_file(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sym"] UTF8String]); diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index f096bb34..f1f2f5a1 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -9,6 +9,8 @@ + + @@ -1056,7 +1058,7 @@ - + diff --git a/Core/cheats.c b/Core/cheats.c index fa9b6a7a..0525816f 100644 --- a/Core/cheats.c +++ b/Core/cheats.c @@ -2,6 +2,7 @@ #include "cheats.h" #include #include +#include static inline uint8_t hash_addr(uint16_t addr) { @@ -238,3 +239,78 @@ void GB_update_cheat(GB_gameboy_t *gb, const GB_cheat_t *_cheat, const char *des cheat->description[sizeof(cheat->description) - 1] = 0; } } + +#define CHEAT_MAGIC 'SBCh' + +void GB_load_cheats(GB_gameboy_t *gb, const char *path) +{ + FILE *f = fopen(path, "rb"); + if (!f) { + return; + } + + uint32_t magic = 0; + uint32_t struct_size = 0; + fread(&magic, sizeof(magic), 1, f); + fread(&struct_size, sizeof(struct_size), 1, f); + if (magic != CHEAT_MAGIC && magic != __builtin_bswap32(CHEAT_MAGIC)) { + GB_log(gb, "The file is not a SameBoy cheat database"); + return; + } + + if (struct_size != sizeof(GB_cheat_t)) { + GB_log(gb, "This cheat database is not compatible with this version of SameBoy"); + return; + } + + // Remove all cheats first + while (gb->cheats) { + GB_remove_cheat(gb, gb->cheats[0]); + } + + GB_cheat_t cheat; + while (fread(&cheat, sizeof(cheat), 1, f)) { + if (magic == __builtin_bswap32(CHEAT_MAGIC)) { + cheat.address = __builtin_bswap16(cheat.address); + cheat.bank = __builtin_bswap16(cheat.bank); + } + cheat.description[sizeof(cheat.description) - 1] = 0; + GB_add_cheat(gb, cheat.description, cheat.address, cheat.bank, cheat.value, cheat.old_value, cheat.use_old_value, cheat.enabled); + } + + return; +} + +int GB_save_cheats(GB_gameboy_t *gb, const char *path) +{ + if (!gb->cheat_count) return 0; // Nothing to save. + FILE *f = fopen(path, "wb"); + if (!f) { + GB_log(gb, "Could not dump cheat database: %s.\n", strerror(errno)); + return errno; + } + + uint32_t magic = CHEAT_MAGIC; + uint32_t struct_size = sizeof(GB_cheat_t); + + if (fwrite(&magic, sizeof(magic), 1, f) != 1) { + fclose(f); + return EIO; + } + + if (fwrite(&struct_size, sizeof(struct_size), 1, f) != 1) { + fclose(f); + return EIO; + } + + for (size_t i = 0; i cheat_count; i++) { + if (fwrite(gb->cheats[i], sizeof(*gb->cheats[i]), 1, f) != 1) { + fclose(f); + return EIO; + } + } + + errno = 0; + fclose(f); + return errno; +} diff --git a/Core/cheats.h b/Core/cheats.h index be5b39ae..13b7d7b4 100644 --- a/Core/cheats.h +++ b/Core/cheats.h @@ -13,6 +13,8 @@ const GB_cheat_t *const *GB_get_cheats(GB_gameboy_t *gb, size_t *size); void GB_remove_cheat(GB_gameboy_t *gb, const GB_cheat_t *cheat); bool GB_cheats_enabled(GB_gameboy_t *gb); void GB_set_cheats_enabled(GB_gameboy_t *gb, bool enabled); +void GB_load_cheats(GB_gameboy_t *gb, const char *path); +int GB_save_cheats(GB_gameboy_t *gb, const char *path); #ifdef GB_INTERNAL void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value);