Skip to content

Commit

Permalink
Both undo options print messages now. Undo Load State can now also be
Browse files Browse the repository at this point in the history
undone ad infinitum. Doing it in succession swaps the current state for
the backed up state.
  • Loading branch information
rz5 committed Jun 8, 2016
1 parent 8c5238c commit 9944ec9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
14 changes: 12 additions & 2 deletions command.c
Original file line number Diff line number Diff line change
Expand Up @@ -1601,8 +1601,14 @@ static void command_event_undo_save_state(char *s, size_t len)
{
snprintf(s, len, "%s \"%s\".",
msg_hash_to_str(MSG_FAILED_TO_UNDO_SAVE_STATE),
"from internal buffer");
"RAM");

return;
}

/* TODO/FIXME - use msg_hash_to_str here and there */
snprintf(s, len, "%s",
"Restored save state.");
}

/**
Expand Down Expand Up @@ -1639,8 +1645,12 @@ static void command_event_undo_load_state(char *s, size_t len)
{
snprintf(s, len, "%s \"%s\".",
msg_hash_to_str(MSG_FAILED_TO_UNDO_LOAD_STATE),
"from internal buffer");
"RAM");
return;
}
/* TODO/FIXME - use msg_hash_to_str here and there */
snprintf(s, len, "%s",
"Undid load state.");
}

static void command_event_main_state(unsigned cmd)
Expand Down
1 change: 1 addition & 0 deletions command.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ enum event_command
CMD_EVENT_UNLOAD_CORE,
CMD_EVENT_LOAD_STATE,
CMD_EVENT_UNDO_LOAD_STATE,
/* Rewrites a savestate on disk */
CMD_EVENT_UNDO_SAVE_STATE,
CMD_EVENT_SAVE_STATE,
CMD_EVENT_SAVE_STATE_DECREMENT,
Expand Down
5 changes: 4 additions & 1 deletion content.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool content_save_ram_file(unsigned slot);

/* Load a state from disk to memory. */
bool content_load_state(const char* path);
bool content_load_state_with_backup(const char* path, bool save_to_backup_buffer);
bool content_load_state_with_backup(const char* path, bool load_to_backup_buffer);

/* Save a state from memory to disk. */
bool content_save_state(const char *path);
Expand Down Expand Up @@ -78,6 +78,9 @@ void content_deinit(void);
* selected libretro core. */
bool content_init(void);

/* Resets the state and savefile backup buffers */
bool content_reset_savestate_backups();

RETRO_END_DECLS

#endif
41 changes: 24 additions & 17 deletions tasks/task_save_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct sram_block

bool content_undo_load_state()
{
if (old_state_buf.data == NULL)
if (old_state_buf.data == NULL || old_state_buf.size == 0)
return false;

unsigned i;
Expand All @@ -79,7 +79,7 @@ bool content_undo_load_state()

RARCH_LOG("%s: \"%s\".\n",
msg_hash_to_str(MSG_LOADING_STATE),
"from internal buffer");
"RAM");

RARCH_LOG("%s: %u %s.\n",
msg_hash_to_str(MSG_STATE_SIZE),
Expand Down Expand Up @@ -136,11 +136,25 @@ bool content_undo_load_state()
memcpy(blocks[i].data, ptr, blocks[i].size);
}
}

/* We need to make a temporary copy of the buffer, to allow the swap below */
void* temp_data = malloc(old_state_buf.size);
size_t temp_data_size = old_state_buf.size;
memcpy(temp_data, old_state_buf.data, old_state_buf.size);

serial_info.data_const = temp_data;
serial_info.size = temp_data_size;

serial_info.data_const = old_state_buf.data;
serial_info.size = old_state_buf.size;
/* Swap the current state with the backup state. This way, we can undo
what we're undoing */
content_save_state_with_backup("RAM", false);
bool ret = core_unserialize(&serial_info);

/* Clean up the temporary copy */
free(temp_data);
temp_data = NULL;
temp_data_size = 0;

/* Flush back. */
for (i = 0; i < num_blocks; i++)
{
Expand All @@ -166,16 +180,7 @@ bool content_undo_load_state()
if (!ret)
RARCH_ERR("%s \"%s\".\n",
msg_hash_to_str(MSG_FAILED_TO_UNDO_LOAD_STATE),
"from internal buffer");

/* Wipe the old state buffer, it's meant to be one use only */
old_state_buf.path[0] = '\0';
if (old_state_buf.data) {
free(old_state_buf.data);
old_state_buf.data = NULL;
}

old_state_buf.data = 0;
"RAM");

return ret;
}
Expand All @@ -196,7 +201,7 @@ bool content_undo_save_state()
if (!ret)
RARCH_ERR("%s \"%s\".\n",
msg_hash_to_str(MSG_FAILED_TO_UNDO_SAVE_STATE),
"from internal buffer");
"RAM");

return ret;
}
Expand Down Expand Up @@ -388,7 +393,7 @@ bool content_load_state_with_backup(const char *path, bool load_to_backup_buffer
serial_info.size = size;

/* Backup the current state so we can undo this load */
content_save_state_with_backup("internal buffer", false);
content_save_state_with_backup("RAM", false);
ret = core_unserialize(&serial_info);

/* Flush back. */
Expand Down Expand Up @@ -443,12 +448,14 @@ bool content_rename_state(const char *origin, const char *dest)
}

/*
* Resets the state and savefile backups
*
* TODO/FIXME: Figure out when and where this should be called
*
*/
bool content_reset_savestate_backups()
{
printf("Resetting undo buffers.\n");

if (old_save_file.data)
{
free(old_save_file.data);
Expand Down

0 comments on commit 9944ec9

Please sign in to comment.