Skip to content

Commit

Permalink
Fix a bug when using the Reset binding where the save would be rest…
Browse files Browse the repository at this point in the history
…ored to the one originally loaded.
  • Loading branch information
Arignir committed Dec 19, 2023
1 parent 8762812 commit 72a4c52
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
4 changes: 3 additions & 1 deletion include/app/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ struct app {
struct launch_config *launch_config;
struct game_entry *game_entry;

char *game_path;

FILE *backup_file;

bool is_started;
Expand Down Expand Up @@ -380,10 +382,10 @@ void app_config_push_recent_rom(struct app *app, char const *path);
/* emulator.c */
void app_emulator_process_all_notifs(struct app *app);
bool app_emulator_configure(struct app *app, char const *rom_path);
void app_emulator_reset(struct app *app);
void app_emulator_stop(struct app *app);
void app_emulator_run(struct app *app);
void app_emulator_pause(struct app *app);
void app_emulator_reset(struct app *app);
void app_emulator_exit(struct app *app);
void app_emulator_key(struct app *app, enum keys key, bool pressed);
void app_emulator_speed(struct app *app, uint32_t);
Expand Down
96 changes: 47 additions & 49 deletions source/app/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ app_emulator_unconfigure(
free(app->emulation.game_entry);
app->emulation.game_entry = NULL;
}

if (app->emulation.game_path) {
free(app->emulation.game_path);
app->emulation.game_path = NULL;
}
}

static
Expand Down Expand Up @@ -364,51 +369,22 @@ app_emulator_configure_backup(
}

/*
** Update the launch configuration with the current settings and print the emulator's configuration.
*/
static
void
app_emulator_configure_settings(
struct app *app
) {
app->emulation.launch_config->skip_bios = app->emulation.skip_bios;
app->emulation.launch_config->speed = app->emulation.speed;
app->emulation.launch_config->audio_frequency = GBA_CYCLES_PER_SECOND / app->audio.resample_frequency;

if (app->emulation.rtc.autodetect) {
app->emulation.launch_config->rtc = (bool)(app->emulation.game_entry->flags & GAME_ENTRY_FLAGS_RTC);
} else {
app->emulation.launch_config->rtc = app->emulation.rtc.enabled;
}

if (app->emulation.backup_storage.autodetect) {
app->emulation.launch_config->backup_storage.type = app->emulation.game_entry->storage;
} else {
app->emulation.launch_config->backup_storage.type = app->emulation.backup_storage.type;
}

logln(HS_INFO, "Emulator's configuration:");
logln(HS_INFO, " Skip BIOS: %s", app->emulation.launch_config->skip_bios ? "true" : "false");
logln(HS_INFO, " Backup storage: %s", backup_storage_names[app->emulation.launch_config->backup_storage.type]);
logln(HS_INFO, " Rtc: %s", app->emulation.launch_config->rtc ? "true" : "false");
logln(HS_INFO, " Speed: %i", app->emulation.speed);
logln(HS_INFO, " Audio Frequency: %iHz (%i cycles)", app->audio.resample_frequency, app->emulation.launch_config->audio_frequency);
}

/*
** Update the GBA's launch configuration to load a new game
** Update the GBA's launch configuration to load a new game & reset the emulator.
**
** This function abstracts all the different step needed to load a game:
** - Read the BIOS/ROM files
** - Extracting the game code
** - Performing a database lookup to identify the features of the game
** - Update the gba's launch configuration
** - Reset the emulator
** - Wait for the reset notification
*/
bool
app_emulator_configure(
struct app *app,
char const *rom_path
) {
struct message_reset event;
char *backup_path;
char *extension;
size_t basename_len;
Expand Down Expand Up @@ -483,25 +459,29 @@ app_emulator_configure(
app->emulation.game_entry = db_autodetect_game_features(app->emulation.launch_config->rom.data, app->emulation.launch_config->rom.size);
}

app_emulator_reset(app);

app_config_push_recent_rom(app, rom_path);

return (false);
}
app->emulation.game_path = strdup(rom_path);
app->emulation.launch_config->skip_bios = app->emulation.skip_bios;
app->emulation.launch_config->speed = app->emulation.speed;
app->emulation.launch_config->audio_frequency = GBA_CYCLES_PER_SECOND / app->audio.resample_frequency;

/*
** Reset the already-configured emulation.
*/
void
app_emulator_reset(
struct app *app
) {
struct message_reset event;
if (app->emulation.rtc.autodetect) {
app->emulation.launch_config->rtc = (bool)(app->emulation.game_entry->flags & GAME_ENTRY_FLAGS_RTC);
} else {
app->emulation.launch_config->rtc = app->emulation.rtc.enabled;
}

hs_assert(app->emulation.launch_config);
if (app->emulation.backup_storage.autodetect) {
app->emulation.launch_config->backup_storage.type = app->emulation.game_entry->storage;
} else {
app->emulation.launch_config->backup_storage.type = app->emulation.backup_storage.type;
}

app_emulator_configure_settings(app);
logln(HS_INFO, "Emulator's configuration:");
logln(HS_INFO, " Skip BIOS: %s", app->emulation.launch_config->skip_bios ? "true" : "false");
logln(HS_INFO, " Backup storage: %s", backup_storage_names[app->emulation.launch_config->backup_storage.type]);
logln(HS_INFO, " Rtc: %s", app->emulation.launch_config->rtc ? "true" : "false");
logln(HS_INFO, " Speed: %i", app->emulation.speed);
logln(HS_INFO, " Audio Frequency: %iHz (%i cycles)", app->audio.resample_frequency, app->emulation.launch_config->audio_frequency);

event.header.kind = MESSAGE_RESET;
event.header.size = sizeof(event);
Expand All @@ -519,6 +499,24 @@ app_emulator_reset(
channel_release(&app->emulation.gba->channels.messages);

app_emulator_wait_for_notif(app, NOTIFICATION_RESET);

app_config_push_recent_rom(app, rom_path);

return (false);
}

/*
** Reset the emulator with the same configuration and settings.
*/
void
app_emulator_reset(
struct app *app
) {
char *game_path;

game_path = strdup(app->emulation.game_path);
app_emulator_configure(app, game_path);
free(game_path);
}

/*
Expand Down

0 comments on commit 72a4c52

Please sign in to comment.