Fall back to .snX if no .sX save state found

This commit is contained in:
Lior Halphon 2021-05-07 00:33:04 +03:00
parent a4a8ad00d5
commit c944142b36
4 changed files with 38 additions and 16 deletions

View File

@ -54,6 +54,6 @@
-(void) writeMemory:(uint16_t) addr value:(uint8_t)value; -(void) writeMemory:(uint16_t) addr value:(uint8_t)value;
-(void) performAtomicBlock: (void (^)())block; -(void) performAtomicBlock: (void (^)())block;
-(void) connectLinkCable:(NSMenuItem *)sender; -(void) connectLinkCable:(NSMenuItem *)sender;
- (bool)loadStateFile:(const char *)path; -(int)loadStateFile:(const char *)path noErrorOnNotFound:(bool)noErrorOnFileNotFound;
@end @end

View File

@ -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 = NSString *error =
[self captureOutputForBlock:^{ [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(); NSBeep();
} }
if (error) { if (error) {
[GBWarningPopover popoverWithContents:error onWindow:self.mainWindow]; [GBWarningPopover popoverWithContents:error onWindow:self.mainWindow];
} }
return success; return result;
} }
- (IBAction)loadState:(id)sender - (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 - (IBAction)clearConsole:(id)sender

View File

@ -647,7 +647,7 @@ static const uint8_t workboy_vk_to_key[] = {
if ( [[pboard types] containsObject:NSURLPboardType] ) { if ( [[pboard types] containsObject:NSURLPboardType] ) {
NSURL *fileURL = [NSURL URLFromPasteboard:pboard]; NSURL *fileURL = [NSURL URLFromPasteboard:pboard];
return [_document loadStateFile:fileURL.fileSystemRepresentation]; return [_document loadStateFile:fileURL.fileSystemRepresentation noErrorOnNotFound:false];
} }
return false; return false;

View File

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <signal.h> #include <signal.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include <OpenDialog/open_dialog.h> #include <OpenDialog/open_dialog.h>
#include <SDL.h> #include <SDL.h>
#include <Core/gb.h> #include <Core/gb.h>
@ -63,7 +64,7 @@ static void start_capturing_logs(void)
GB_set_log_callback(&gb, log_capture_callback); 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); GB_set_log_callback(&gb, NULL);
if (captured_log[0] == 0) { if (captured_log[0] == 0) {
@ -72,7 +73,7 @@ static const char *end_capturing_logs(bool show_popup, bool should_exit, uint32_
} }
else { else {
if (show_popup) { if (show_popup) {
SDL_ShowSimpleMessageBox(popup_flags, "Error", captured_log, window); SDL_ShowSimpleMessageBox(popup_flags, title, captured_log, window);
} }
if (should_exit) { if (should_exit) {
exit(1); exit(1);
@ -429,7 +430,7 @@ static bool handle_pending_command(void)
switch (pending_command) { switch (pending_command) {
case GB_SDL_LOAD_STATE_COMMAND: case GB_SDL_LOAD_STATE_COMMAND:
case GB_SDL_SAVE_STATE_COMMAND: { case GB_SDL_SAVE_STATE_COMMAND: {
char save_path[strlen(filename) + 4]; char save_path[strlen(filename) + 5];
char save_extension[] = ".s0"; char save_extension[] = ".s0";
save_extension[2] += command_parameter; save_extension[2] += command_parameter;
replace_extension(filename, strlen(filename), save_path, save_extension); replace_extension(filename, strlen(filename), save_path, save_extension);
@ -437,19 +438,33 @@ static bool handle_pending_command(void)
start_capturing_logs(); start_capturing_logs();
bool success; bool success;
if (pending_command == GB_SDL_LOAD_STATE_COMMAND) { 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 { else {
success = GB_save_state(&gb, save_path) == 0; 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; return false;
} }
case GB_SDL_LOAD_STATE_FROM_FILE_COMMAND: case GB_SDL_LOAD_STATE_FROM_FILE_COMMAND:
start_capturing_logs(); start_capturing_logs();
bool success = GB_load_state(&gb, dropped_state_file) == 0; 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); SDL_free(dropped_state_file);
return false; return false;
@ -489,7 +504,7 @@ static void load_boot_rom(GB_gameboy_t *gb, GB_boot_rom_t type)
if (use_built_in) { if (use_built_in) {
start_capturing_logs(); start_capturing_logs();
GB_load_boot_rom(gb, resource_path(names[type])); 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 { else {
GB_load_rom(&gb, filename); GB_load_rom(&gb, filename);
} }
end_capturing_logs(true, error, SDL_MESSAGEBOX_WARNING); end_capturing_logs(true, error, SDL_MESSAGEBOX_WARNING, "Warning");
/* Configure battery */ /* Configure battery */