mirror of
https://github.com/libretro/RetroArch
synced 2024-12-28 09:29:16 +00:00
cleanups
This commit is contained in:
parent
035f6ca210
commit
4ad2eba602
@ -1236,7 +1236,7 @@ static void command_event_load_auto_state(void)
|
||||
if (!path_file_exists(savestate_name_auto))
|
||||
return;
|
||||
|
||||
ret = content_load_state(savestate_name_auto);
|
||||
ret = content_load_state(savestate_name_auto, false);
|
||||
|
||||
RARCH_LOG("Found auto savestate in: %s\n", savestate_name_auto);
|
||||
|
||||
@ -1408,7 +1408,7 @@ static bool command_event_save_auto_state(void)
|
||||
fill_pathname_noext(savestate_name_auto, global->name.savestate,
|
||||
".auto", sizeof(savestate_name_auto));
|
||||
|
||||
ret = content_save_state((const char*)savestate_name_auto);
|
||||
ret = content_save_state((const char*)savestate_name_auto, true);
|
||||
RARCH_LOG("Auto save state to \"%s\" %s.\n", savestate_name_auto, ret ?
|
||||
"succeeded" : "failed");
|
||||
|
||||
@ -1579,7 +1579,7 @@ static void command_event_save_state(const char *path,
|
||||
{
|
||||
settings_t *settings = config_get_ptr();
|
||||
|
||||
if (!content_save_state(path))
|
||||
if (!content_save_state(path, true))
|
||||
{
|
||||
snprintf(s, len, "%s \"%s\".",
|
||||
msg_hash_to_str(MSG_FAILED_TO_SAVE_STATE_TO),
|
||||
@ -1623,7 +1623,7 @@ static void command_event_load_state(const char *path, char *s, size_t len)
|
||||
{
|
||||
settings_t *settings = config_get_ptr();
|
||||
|
||||
if (!content_load_state(path))
|
||||
if (!content_load_state(path, false))
|
||||
{
|
||||
snprintf(s, len, "%s \"%s\".",
|
||||
msg_hash_to_str(MSG_FAILED_TO_LOAD_STATE),
|
||||
|
@ -46,12 +46,10 @@ bool content_load_ram_file(unsigned slot);
|
||||
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 load_to_backup_buffer);
|
||||
bool content_load_state(const char* path, bool load_to_backup_buffer);
|
||||
|
||||
/* Save a state from memory to disk. */
|
||||
bool content_save_state(const char *path);
|
||||
bool content_save_state_with_backup(const char *path, bool save_to_disk);
|
||||
bool content_save_state(const char *path, bool save_to_disk);
|
||||
|
||||
/* Copy a save state. */
|
||||
bool content_rename_state(const char *origin, const char *dest);
|
||||
|
@ -42,18 +42,13 @@ struct save_state_buf
|
||||
size_t size;
|
||||
};
|
||||
|
||||
/*
|
||||
Holds a savestate which was stored on disk and was lost when
|
||||
content_save_state() wrote over it.
|
||||
Can be restored to disk with undo_save_state().
|
||||
*/
|
||||
static struct save_state_buf old_save_file;
|
||||
/* Holds the previous saved state
|
||||
* Can be restored to disk with undo_save_state(). */
|
||||
static struct save_state_buf undo_save_buf;
|
||||
|
||||
/*
|
||||
Represents the state which was lost when load_state() was called.
|
||||
Can be restored with undo_load_state().
|
||||
*/
|
||||
static struct save_state_buf old_state_buf;
|
||||
/* Holds the data from before a load_state() operation
|
||||
* Can be restored with undo_load_state(). */
|
||||
static struct save_state_buf undo_load_buf;
|
||||
|
||||
struct sram_block
|
||||
{
|
||||
@ -62,9 +57,15 @@ struct sram_block
|
||||
size_t size;
|
||||
};
|
||||
|
||||
/**
|
||||
* undo_load_state:
|
||||
* Revert to the state before a state was loaded.
|
||||
*
|
||||
* Returns: true if successful, false otherwise.
|
||||
**/
|
||||
bool content_undo_load_state()
|
||||
{
|
||||
if (old_state_buf.data == NULL || old_state_buf.size == 0)
|
||||
if (undo_load_buf.data == NULL || undo_load_buf.size == 0)
|
||||
return false;
|
||||
|
||||
unsigned i;
|
||||
@ -83,7 +84,7 @@ bool content_undo_load_state()
|
||||
|
||||
RARCH_LOG("%s: %u %s.\n",
|
||||
msg_hash_to_str(MSG_STATE_SIZE),
|
||||
old_state_buf.size,
|
||||
undo_load_buf.size,
|
||||
msg_hash_to_str(MSG_BYTES));
|
||||
|
||||
|
||||
@ -138,16 +139,16 @@ bool content_undo_load_state()
|
||||
}
|
||||
|
||||
/* 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);
|
||||
void* temp_data = malloc(undo_load_buf.size);
|
||||
size_t temp_data_size = undo_load_buf.size;
|
||||
memcpy(temp_data, undo_load_buf.data, undo_load_buf.size);
|
||||
|
||||
serial_info.data_const = temp_data;
|
||||
serial_info.size = temp_data_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);
|
||||
content_save_state("RAM", false);
|
||||
bool ret = core_unserialize(&serial_info);
|
||||
|
||||
/* Clean up the temporary copy */
|
||||
@ -185,18 +186,24 @@ bool content_undo_load_state()
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* undo_save_state:
|
||||
* Reverts the last save operation
|
||||
*
|
||||
* Returns: true if successful, false otherwise.
|
||||
**/
|
||||
bool content_undo_save_state()
|
||||
{
|
||||
bool ret = filestream_write_file(old_save_file.path, old_save_file.data, old_save_file.size);
|
||||
bool ret = filestream_write_file(undo_save_buf.path, undo_save_buf.data, undo_save_buf.size);
|
||||
|
||||
/* Wipe the save file buffer as it's intended to be one use only */
|
||||
old_save_file.path[0] = '\0';
|
||||
if (old_save_file.data) {
|
||||
free(old_save_file.data);
|
||||
old_save_file.data = NULL;
|
||||
undo_save_buf.path[0] = '\0';
|
||||
if (undo_save_buf.data) {
|
||||
free(undo_save_buf.data);
|
||||
undo_save_buf.data = NULL;
|
||||
}
|
||||
|
||||
old_save_file.data = 0;
|
||||
undo_save_buf.data = 0;
|
||||
|
||||
if (!ret)
|
||||
RARCH_ERR("%s \"%s\".\n",
|
||||
@ -212,13 +219,12 @@ bool content_undo_save_state()
|
||||
/**
|
||||
* save_state:
|
||||
* @path : path of saved state that shall be written to.
|
||||
* @save_to_disk: If false, saves the state onto old_state_buf.
|
||||
* @save_to_disk: If false, saves the state onto undo_load_buf.
|
||||
* Save a state from memory to disk.
|
||||
*
|
||||
* Returns: true if successful, false otherwise.
|
||||
**/
|
||||
bool content_save_state(const char *path) { content_save_state_with_backup(path, true);}
|
||||
bool content_save_state_with_backup(const char *path, bool save_to_disk)
|
||||
bool content_save_state(const char *path, bool save_to_disk)
|
||||
{
|
||||
retro_ctx_serialize_info_t serial_info;
|
||||
retro_ctx_size_info_t info;
|
||||
@ -251,26 +257,26 @@ bool content_save_state_with_backup(const char *path, bool save_to_disk)
|
||||
if (ret) {
|
||||
if (save_to_disk) {
|
||||
if (path_file_exists(path)) {
|
||||
content_load_state_with_backup(path, true);
|
||||
content_load_state(path, true);
|
||||
}
|
||||
|
||||
ret = filestream_write_file(path, data, info.size);
|
||||
}
|
||||
/* save_to_disk is false, which means we are saving the state
|
||||
in old_state_buf to allow content_undo_load_state() to restore it */
|
||||
in undo_load_buf to allow content_undo_load_state() to restore it */
|
||||
else
|
||||
{
|
||||
old_state_buf.path[0] = '\0';
|
||||
undo_load_buf.path[0] = '\0';
|
||||
|
||||
/* If we were holding onto an old state already, clean it up first */
|
||||
if (old_state_buf.data) {
|
||||
free(old_state_buf.data);
|
||||
old_state_buf.data = NULL;
|
||||
if (undo_load_buf.data) {
|
||||
free(undo_load_buf.data);
|
||||
undo_load_buf.data = NULL;
|
||||
}
|
||||
|
||||
old_state_buf.data = malloc(info.size);
|
||||
memcpy(old_state_buf.data, data, info.size);
|
||||
old_state_buf.size = info.size;
|
||||
undo_load_buf.data = malloc(info.size);
|
||||
memcpy(undo_load_buf.data, data, info.size);
|
||||
undo_load_buf.size = info.size;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -288,15 +294,14 @@ bool content_save_state_with_backup(const char *path, bool save_to_disk)
|
||||
/**
|
||||
* content_load_state:
|
||||
* @path : path that state will be loaded from.
|
||||
* @load_to_backup_buffer: If true, the state will be loaded into old_save_file.
|
||||
* @load_to_backup_buffer: If true, the state will be loaded into undo_save_buf.
|
||||
* Load a state from disk to memory.
|
||||
*
|
||||
* Returns: true if successful, false otherwise.
|
||||
*
|
||||
*
|
||||
**/
|
||||
bool content_load_state(const char* path) { content_load_state_with_backup(path, false); }
|
||||
bool content_load_state_with_backup(const char *path, bool load_to_backup_buffer)
|
||||
bool content_load_state(const char *path, bool load_to_backup_buffer)
|
||||
{
|
||||
unsigned i;
|
||||
ssize_t size;
|
||||
@ -323,18 +328,18 @@ bool content_load_state_with_backup(const char *path, bool load_to_backup_buffer
|
||||
/* This means we're backing up the file in memory, so content_undo_save_state()
|
||||
can restore it */
|
||||
if (load_to_backup_buffer) {
|
||||
strcpy(old_save_file.path, path);
|
||||
strcpy(undo_save_buf.path, path);
|
||||
|
||||
/* If we were previously backing up a file, let go of it first */
|
||||
if (old_save_file.data) {
|
||||
free(old_save_file.data);
|
||||
old_save_file.data = NULL;
|
||||
if (undo_save_buf.data) {
|
||||
free(undo_save_buf.data);
|
||||
undo_save_buf.data = NULL;
|
||||
}
|
||||
|
||||
old_save_file.data = malloc(size);
|
||||
memcpy(old_save_file.data, buf, size);
|
||||
undo_save_buf.data = malloc(size);
|
||||
memcpy(undo_save_buf.data, buf, size);
|
||||
|
||||
old_save_file.size = size;
|
||||
undo_save_buf.size = size;
|
||||
|
||||
free(buf);
|
||||
return true;
|
||||
@ -393,7 +398,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("RAM", false);
|
||||
content_save_state("RAM", false);
|
||||
ret = core_unserialize(&serial_info);
|
||||
|
||||
/* Flush back. */
|
||||
@ -456,23 +461,23 @@ bool content_reset_savestate_backups()
|
||||
{
|
||||
printf("Resetting undo buffers.\n");
|
||||
|
||||
if (old_save_file.data)
|
||||
if (undo_save_buf.data)
|
||||
{
|
||||
free(old_save_file.data);
|
||||
old_save_file.data = NULL;
|
||||
free(undo_save_buf.data);
|
||||
undo_save_buf.data = NULL;
|
||||
}
|
||||
|
||||
old_save_file.path[0] = '\0';
|
||||
old_save_file.size = 0;
|
||||
undo_save_buf.path[0] = '\0';
|
||||
undo_save_buf.size = 0;
|
||||
|
||||
if (old_state_buf.data)
|
||||
if (undo_load_buf.data)
|
||||
{
|
||||
free(old_state_buf.data);
|
||||
old_state_buf.data = NULL;
|
||||
free(undo_load_buf.data);
|
||||
undo_load_buf.data = NULL;
|
||||
}
|
||||
|
||||
old_state_buf.path[0] = '\0';
|
||||
old_state_buf.size = 0;
|
||||
undo_load_buf.path[0] = '\0';
|
||||
undo_load_buf.size = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user