From c944142b367ecc46c4eb8f74eb9efde98db2184c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 7 May 2021 00:33:04 +0300 Subject: [PATCH] Fall back to .snX if no .sX save state found --- Cocoa/Document.h | 2 +- Cocoa/Document.m | 19 +++++++++++++------ Cocoa/GBView.m | 2 +- SDL/main.c | 31 +++++++++++++++++++++++-------- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/Cocoa/Document.h b/Cocoa/Document.h index 8847385..19228e5 100644 --- a/Cocoa/Document.h +++ b/Cocoa/Document.h @@ -54,6 +54,6 @@ -(void) writeMemory:(uint16_t) addr value:(uint8_t)value; -(void) performAtomicBlock: (void (^)())block; -(void) connectLinkCable:(NSMenuItem *)sender; -- (bool)loadStateFile:(const char *)path; +-(int)loadStateFile:(const char *)path noErrorOnNotFound:(bool)noErrorOnFileNotFound; @end diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 8977ce9..297547c 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -1278,26 +1278,33 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency) } } -- (bool)loadStateFile:(const char *)path +- (int)loadStateFile:(const char *)path noErrorOnNotFound:(bool)noErrorOnFileNotFound; { - bool __block success = false; + int __block result = false; NSString *error = [self captureOutputForBlock:^{ - success = GB_load_state(&gb, path) == 0; + result = GB_load_state(&gb, path); }]; - if (!success) { + if (result == ENOENT && noErrorOnFileNotFound) { + return ENOENT; + } + + if (result) { NSBeep(); } if (error) { [GBWarningPopover popoverWithContents:error onWindow:self.mainWindow]; } - return success; + return result; } - (IBAction)loadState:(id)sender { - [self loadStateFile:[[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:[NSString stringWithFormat:@"s%ld", (long)[sender tag] ]].path.UTF8String]; + int ret = [self loadStateFile:[[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:[NSString stringWithFormat:@"s%ld", (long)[sender tag]]].path.UTF8String noErrorOnNotFound:true]; + if (ret == ENOENT) { + [self loadStateFile:[[self.fileURL URLByDeletingPathExtension] URLByAppendingPathExtension:[NSString stringWithFormat:@"sn%ld", (long)[sender tag]]].path.UTF8String noErrorOnNotFound:false]; + } } - (IBAction)clearConsole:(id)sender diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 5c1922c..04b3543 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -647,7 +647,7 @@ static const uint8_t workboy_vk_to_key[] = { if ( [[pboard types] containsObject:NSURLPboardType] ) { NSURL *fileURL = [NSURL URLFromPasteboard:pboard]; - return [_document loadStateFile:fileURL.fileSystemRepresentation]; + return [_document loadStateFile:fileURL.fileSystemRepresentation noErrorOnNotFound:false]; } return false; diff --git a/SDL/main.c b/SDL/main.c index 97884d5..221bad1 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -63,7 +64,7 @@ static void start_capturing_logs(void) GB_set_log_callback(&gb, log_capture_callback); } -static const char *end_capturing_logs(bool show_popup, bool should_exit, uint32_t popup_flags) +static const char *end_capturing_logs(bool show_popup, bool should_exit, uint32_t popup_flags, const char *title) { GB_set_log_callback(&gb, NULL); if (captured_log[0] == 0) { @@ -72,7 +73,7 @@ static const char *end_capturing_logs(bool show_popup, bool should_exit, uint32_ } else { if (show_popup) { - SDL_ShowSimpleMessageBox(popup_flags, "Error", captured_log, window); + SDL_ShowSimpleMessageBox(popup_flags, title, captured_log, window); } if (should_exit) { exit(1); @@ -429,7 +430,7 @@ static bool handle_pending_command(void) switch (pending_command) { case GB_SDL_LOAD_STATE_COMMAND: case GB_SDL_SAVE_STATE_COMMAND: { - char save_path[strlen(filename) + 4]; + char save_path[strlen(filename) + 5]; char save_extension[] = ".s0"; save_extension[2] += command_parameter; replace_extension(filename, strlen(filename), save_path, save_extension); @@ -437,19 +438,33 @@ static bool handle_pending_command(void) start_capturing_logs(); bool success; if (pending_command == GB_SDL_LOAD_STATE_COMMAND) { - success = GB_load_state(&gb, save_path) == 0; + int result = GB_load_state(&gb, save_path); + if (result == ENOENT) { + char save_extension[] = ".sn0"; + save_extension[3] += command_parameter; + replace_extension(filename, strlen(filename), save_path, save_extension); + start_capturing_logs(); + result = GB_load_state(&gb, save_path); + } + success = result == 0; } else { success = GB_save_state(&gb, save_path) == 0; } - end_capturing_logs(true, false, success? SDL_MESSAGEBOX_INFORMATION : SDL_MESSAGEBOX_ERROR); + end_capturing_logs(true, + false, + success? SDL_MESSAGEBOX_INFORMATION : SDL_MESSAGEBOX_ERROR, + success? "Notice" : "Error"); return false; } case GB_SDL_LOAD_STATE_FROM_FILE_COMMAND: start_capturing_logs(); bool success = GB_load_state(&gb, dropped_state_file) == 0; - end_capturing_logs(true, false, success? SDL_MESSAGEBOX_INFORMATION : SDL_MESSAGEBOX_ERROR); + end_capturing_logs(true, + false, + success? SDL_MESSAGEBOX_INFORMATION : SDL_MESSAGEBOX_ERROR, + success? "Notice" : "Error"); SDL_free(dropped_state_file); return false; @@ -489,7 +504,7 @@ static void load_boot_rom(GB_gameboy_t *gb, GB_boot_rom_t type) if (use_built_in) { start_capturing_logs(); GB_load_boot_rom(gb, resource_path(names[type])); - end_capturing_logs(true, false, SDL_MESSAGEBOX_ERROR); + end_capturing_logs(true, false, SDL_MESSAGEBOX_ERROR, "Error"); } } @@ -562,7 +577,7 @@ restart: else { GB_load_rom(&gb, filename); } - end_capturing_logs(true, error, SDL_MESSAGEBOX_WARNING); + end_capturing_logs(true, error, SDL_MESSAGEBOX_WARNING, "Warning"); /* Configure battery */