2016-05-17 13:21:09 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2017-01-22 12:40:32 +00:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
2019-02-22 21:31:54 +00:00
|
|
|
* Copyright (C) 2016-2019 - Brad Parker
|
2016-05-17 13:21:09 +00:00
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2016-06-07 15:28:51 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <direct.h>
|
|
|
|
#else
|
2016-06-04 17:38:55 +00:00
|
|
|
#include <unistd.h>
|
2016-06-07 15:28:51 +00:00
|
|
|
#endif
|
2016-05-17 13:21:09 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-09-05 22:56:00 +00:00
|
|
|
#include <compat/strl.h>
|
2016-09-19 02:18:40 +00:00
|
|
|
#include <retro_assert.h>
|
2016-05-17 13:21:09 +00:00
|
|
|
#include <lists/string_list.h>
|
2017-12-11 13:56:58 +00:00
|
|
|
#include <streams/interface_stream.h>
|
2016-05-17 13:21:09 +00:00
|
|
|
#include <streams/file_stream.h>
|
2016-09-19 00:57:23 +00:00
|
|
|
#include <rthreads/rthreads.h>
|
2016-06-04 17:38:55 +00:00
|
|
|
#include <file/file_path.h>
|
2016-10-02 08:03:35 +00:00
|
|
|
#include <retro_miscellaneous.h>
|
2017-05-16 02:00:37 +00:00
|
|
|
#include <string/stdstring.h>
|
2016-05-17 13:21:09 +00:00
|
|
|
|
2016-09-19 17:27:19 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../core.h"
|
|
|
|
#endif
|
|
|
|
|
2016-10-03 12:47:47 +00:00
|
|
|
#ifdef HAVE_NETWORKING
|
2016-10-03 16:24:28 +00:00
|
|
|
#include "../network/netplay/netplay.h"
|
2016-10-03 12:47:47 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-20 02:16:58 +00:00
|
|
|
#include "../content.h"
|
2016-05-17 13:21:09 +00:00
|
|
|
#include "../core.h"
|
2016-09-19 02:18:40 +00:00
|
|
|
#include "../file_path_special.h"
|
2016-06-20 04:34:30 +00:00
|
|
|
#include "../configuration.h"
|
2016-05-17 13:21:09 +00:00
|
|
|
#include "../msg_hash.h"
|
2016-09-28 05:17:14 +00:00
|
|
|
#include "../retroarch.h"
|
2016-05-17 13:21:09 +00:00
|
|
|
#include "../verbosity.h"
|
|
|
|
#include "tasks_internal.h"
|
2018-07-25 23:19:14 +00:00
|
|
|
#include "../managers/cheat_manager.h"
|
2016-05-17 13:21:09 +00:00
|
|
|
|
2019-04-26 19:58:40 +00:00
|
|
|
#ifdef HAVE_LIBNX
|
|
|
|
#define SAVE_STATE_CHUNK 4096 * 10
|
|
|
|
#else
|
2016-10-02 08:03:35 +00:00
|
|
|
#define SAVE_STATE_CHUNK 4096
|
2019-04-26 19:58:40 +00:00
|
|
|
#endif
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2018-09-07 23:09:56 +00:00
|
|
|
static bool save_state_in_background = false;
|
2016-09-19 02:22:02 +00:00
|
|
|
static struct string_list *task_save_files = NULL;
|
|
|
|
|
2016-09-19 00:57:23 +00:00
|
|
|
struct ram_type
|
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
int type;
|
|
|
|
};
|
|
|
|
|
2016-06-07 17:33:01 +00:00
|
|
|
struct save_state_buf
|
|
|
|
{
|
|
|
|
void* data;
|
|
|
|
char path[PATH_MAX_LENGTH];
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
2016-09-23 00:48:10 +00:00
|
|
|
struct sram_block
|
|
|
|
{
|
|
|
|
unsigned type;
|
|
|
|
void *data;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2017-12-11 13:56:58 +00:00
|
|
|
intfstream_t *file;
|
2016-10-02 08:03:35 +00:00
|
|
|
char path[PATH_MAX_LENGTH];
|
|
|
|
void *data;
|
|
|
|
void *undo_data;
|
|
|
|
ssize_t size;
|
|
|
|
ssize_t undo_size;
|
|
|
|
ssize_t written;
|
|
|
|
ssize_t bytes_read;
|
|
|
|
bool load_to_backup_buffer;
|
|
|
|
bool autoload;
|
2016-10-04 21:58:05 +00:00
|
|
|
bool autosave;
|
2016-10-02 20:04:09 +00:00
|
|
|
bool undo_save;
|
2016-10-04 21:58:05 +00:00
|
|
|
bool mute;
|
2016-12-29 05:09:58 +00:00
|
|
|
int state_slot;
|
|
|
|
bool thumbnail_enable;
|
2017-05-13 17:37:16 +00:00
|
|
|
bool has_valid_framebuffer;
|
2016-10-02 08:03:35 +00:00
|
|
|
} save_task_state_t;
|
|
|
|
|
|
|
|
typedef save_task_state_t load_task_data_t;
|
|
|
|
|
2016-06-09 04:02:19 +00:00
|
|
|
/* Holds the previous saved state
|
|
|
|
* Can be restored to disk with undo_save_state(). */
|
|
|
|
static struct save_state_buf undo_save_buf;
|
2016-06-07 17:33:01 +00:00
|
|
|
|
2016-06-09 04:02:19 +00:00
|
|
|
/* Holds the data from before a load_state() operation
|
|
|
|
* Can be restored with undo_load_state(). */
|
|
|
|
static struct save_state_buf undo_load_buf;
|
2016-06-07 17:33:01 +00:00
|
|
|
|
2016-09-19 00:57:23 +00:00
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
typedef struct autosave autosave_t;
|
|
|
|
|
|
|
|
/* Autosave support. */
|
|
|
|
struct autosave_st
|
|
|
|
{
|
|
|
|
autosave_t **list;
|
|
|
|
unsigned num;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct autosave
|
|
|
|
{
|
|
|
|
volatile bool quit;
|
2017-09-29 20:40:31 +00:00
|
|
|
size_t bufsize;
|
|
|
|
unsigned interval;
|
|
|
|
void *buffer;
|
|
|
|
const void *retro_buffer;
|
|
|
|
const char *path;
|
2016-09-19 00:57:23 +00:00
|
|
|
slock_t *lock;
|
|
|
|
slock_t *cond_lock;
|
|
|
|
scond_t *cond;
|
|
|
|
sthread_t *thread;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct autosave_st autosave_state;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* autosave_thread:
|
|
|
|
* @data : pointer to autosave object
|
|
|
|
*
|
|
|
|
* Callback function for (threaded) autosave.
|
|
|
|
**/
|
|
|
|
static void autosave_thread(void *data)
|
|
|
|
{
|
|
|
|
autosave_t *save = (autosave_t*)data;
|
|
|
|
|
2016-12-27 19:11:22 +00:00
|
|
|
while (!save->quit)
|
2016-09-19 00:57:23 +00:00
|
|
|
{
|
|
|
|
bool differ;
|
|
|
|
|
|
|
|
slock_lock(save->lock);
|
2017-05-16 02:00:37 +00:00
|
|
|
differ = string_is_not_equal_fast(save->buffer, save->retro_buffer,
|
|
|
|
save->bufsize);
|
2016-09-19 00:57:23 +00:00
|
|
|
if (differ)
|
|
|
|
memcpy(save->buffer, save->retro_buffer, save->bufsize);
|
|
|
|
slock_unlock(save->lock);
|
|
|
|
|
|
|
|
if (differ)
|
|
|
|
{
|
|
|
|
/* Should probably deal with this more elegantly. */
|
2019-08-11 09:14:45 +00:00
|
|
|
RFILE *file = filestream_open(save->path,
|
2017-12-11 11:53:47 +00:00
|
|
|
RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE);
|
2016-09-19 00:57:23 +00:00
|
|
|
|
|
|
|
if (file)
|
|
|
|
{
|
2019-08-11 09:14:45 +00:00
|
|
|
filestream_write(file, save->buffer, save->bufsize);
|
|
|
|
filestream_flush(file);
|
|
|
|
filestream_close(file);
|
2016-09-19 00:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
slock_lock(save->cond_lock);
|
|
|
|
|
2017-10-02 22:25:39 +00:00
|
|
|
if (!save->quit)
|
2019-08-11 09:14:45 +00:00
|
|
|
{
|
2017-09-05 20:39:12 +00:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER <= 1200
|
2019-08-11 09:14:45 +00:00
|
|
|
int64_t timeout_us = 1000000;
|
2017-10-02 22:25:39 +00:00
|
|
|
#else
|
2019-08-11 09:14:45 +00:00
|
|
|
int64_t timeout_us = 1000000LL;
|
2017-10-02 22:25:39 +00:00
|
|
|
#endif
|
2019-08-11 09:14:45 +00:00
|
|
|
scond_wait_timeout(save->cond, save->cond_lock,
|
|
|
|
save->interval * timeout_us);
|
|
|
|
}
|
2016-09-19 00:57:23 +00:00
|
|
|
|
|
|
|
slock_unlock(save->cond_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* autosave_new:
|
|
|
|
* @path : path to autosave file
|
|
|
|
* @data : pointer to buffer
|
|
|
|
* @size : size of @data buffer
|
|
|
|
* @interval : interval at which saves should be performed.
|
|
|
|
*
|
|
|
|
* Create and initialize autosave object.
|
|
|
|
*
|
|
|
|
* Returns: pointer to new autosave_t object if successful, otherwise
|
|
|
|
* NULL.
|
|
|
|
**/
|
|
|
|
static autosave_t *autosave_new(const char *path,
|
|
|
|
const void *data, size_t size,
|
|
|
|
unsigned interval)
|
|
|
|
{
|
2019-05-23 11:05:15 +00:00
|
|
|
void *buf = NULL;
|
2017-09-29 20:40:31 +00:00
|
|
|
autosave_t *handle = (autosave_t*)malloc(sizeof(*handle));
|
2016-09-19 00:57:23 +00:00
|
|
|
if (!handle)
|
2019-05-23 11:05:15 +00:00
|
|
|
return NULL;
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2017-09-29 20:40:31 +00:00
|
|
|
handle->quit = false;
|
2017-05-13 17:37:16 +00:00
|
|
|
handle->bufsize = size;
|
|
|
|
handle->interval = interval;
|
|
|
|
handle->retro_buffer = data;
|
2017-09-29 20:40:31 +00:00
|
|
|
handle->path = path;
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2019-05-23 11:05:15 +00:00
|
|
|
buf = malloc(size);
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
free(handle);
|
2019-05-24 01:14:30 +00:00
|
|
|
return NULL;
|
2019-05-23 11:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handle->buffer = buf;
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2016-12-27 19:11:22 +00:00
|
|
|
memcpy(handle->buffer, handle->retro_buffer, handle->bufsize);
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2017-05-13 17:37:16 +00:00
|
|
|
handle->lock = slock_new();
|
|
|
|
handle->cond_lock = slock_new();
|
|
|
|
handle->cond = scond_new();
|
|
|
|
handle->thread = sthread_create(autosave_thread, handle);
|
2016-09-19 00:57:23 +00:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* autosave_free:
|
|
|
|
* @handle : pointer to autosave object
|
|
|
|
*
|
|
|
|
* Frees autosave object.
|
|
|
|
**/
|
|
|
|
static void autosave_free(autosave_t *handle)
|
|
|
|
{
|
|
|
|
slock_lock(handle->cond_lock);
|
|
|
|
handle->quit = true;
|
|
|
|
slock_unlock(handle->cond_lock);
|
|
|
|
scond_signal(handle->cond);
|
|
|
|
sthread_join(handle->thread);
|
|
|
|
|
|
|
|
slock_free(handle->lock);
|
|
|
|
slock_free(handle->cond_lock);
|
|
|
|
scond_free(handle->cond);
|
|
|
|
|
2016-12-27 14:14:27 +00:00
|
|
|
if (handle->buffer)
|
|
|
|
free(handle->buffer);
|
|
|
|
handle->buffer = NULL;
|
2016-09-19 00:57:23 +00:00
|
|
|
}
|
|
|
|
|
2017-05-07 15:45:42 +00:00
|
|
|
bool autosave_init(void)
|
2016-09-19 00:57:23 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
2017-04-28 19:03:04 +00:00
|
|
|
autosave_t **list = NULL;
|
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
unsigned autosave_interval = settings->uints.autosave_interval;
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2017-04-28 19:03:04 +00:00
|
|
|
if (autosave_interval < 1 || !task_save_files)
|
2017-05-07 15:45:42 +00:00
|
|
|
return false;
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2017-09-29 20:40:31 +00:00
|
|
|
list = (autosave_t**)
|
|
|
|
calloc(task_save_files->size,
|
|
|
|
sizeof(*autosave_state.list));
|
|
|
|
|
2016-09-19 00:57:23 +00:00
|
|
|
if (!list)
|
2017-05-07 15:45:42 +00:00
|
|
|
return false;
|
2016-09-19 00:57:23 +00:00
|
|
|
|
|
|
|
autosave_state.list = list;
|
2017-02-26 09:33:03 +00:00
|
|
|
autosave_state.num = (unsigned)task_save_files->size;
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2016-09-19 02:22:02 +00:00
|
|
|
for (i = 0; i < task_save_files->size; i++)
|
2016-09-19 00:57:23 +00:00
|
|
|
{
|
|
|
|
retro_ctx_memory_info_t mem_info;
|
2017-05-07 15:45:42 +00:00
|
|
|
autosave_t *auto_st = NULL;
|
|
|
|
const char *path = task_save_files->elems[i].data;
|
|
|
|
unsigned type = task_save_files->elems[i].attr.i;
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2017-05-07 15:45:42 +00:00
|
|
|
mem_info.id = type;
|
2016-09-19 00:57:23 +00:00
|
|
|
|
|
|
|
core_get_memory(&mem_info);
|
|
|
|
|
|
|
|
if (mem_info.size <= 0)
|
|
|
|
continue;
|
|
|
|
|
2017-05-07 15:45:42 +00:00
|
|
|
auto_st = autosave_new(path,
|
2016-09-19 00:57:23 +00:00
|
|
|
mem_info.data,
|
|
|
|
mem_info.size,
|
2017-04-28 19:03:04 +00:00
|
|
|
autosave_interval);
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2017-05-07 15:45:42 +00:00
|
|
|
if (!auto_st)
|
|
|
|
{
|
2016-09-19 00:57:23 +00:00
|
|
|
RARCH_WARN("%s\n", msg_hash_to_str(MSG_AUTOSAVE_FAILED));
|
2017-05-07 15:45:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
autosave_state.list[i] = auto_st;
|
2016-09-19 00:57:23 +00:00
|
|
|
}
|
2017-05-07 15:45:42 +00:00
|
|
|
|
|
|
|
return true;
|
2016-09-19 00:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void autosave_deinit(void)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < autosave_state.num; i++)
|
2016-12-27 14:14:27 +00:00
|
|
|
{
|
|
|
|
autosave_t *handle = autosave_state.list[i];
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
autosave_free(handle);
|
|
|
|
free(autosave_state.list[i]);
|
|
|
|
}
|
2016-12-27 14:16:42 +00:00
|
|
|
autosave_state.list[i] = NULL;
|
2016-12-27 14:14:27 +00:00
|
|
|
}
|
2016-09-19 00:57:23 +00:00
|
|
|
|
2016-12-27 14:14:27 +00:00
|
|
|
free(autosave_state.list);
|
2016-09-19 00:57:23 +00:00
|
|
|
|
|
|
|
autosave_state.list = NULL;
|
|
|
|
autosave_state.num = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-04 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* autosave_lock:
|
|
|
|
*
|
|
|
|
* Lock autosave.
|
|
|
|
**/
|
|
|
|
void autosave_lock(void)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < autosave_state.num; i++)
|
|
|
|
{
|
2016-12-27 14:16:42 +00:00
|
|
|
autosave_t *handle = autosave_state.list[i];
|
|
|
|
if (handle)
|
|
|
|
slock_lock(handle->lock);
|
2016-10-04 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* autosave_unlock:
|
|
|
|
*
|
|
|
|
* Unlocks autosave.
|
|
|
|
**/
|
|
|
|
void autosave_unlock(void)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < autosave_state.num; i++)
|
|
|
|
{
|
2016-12-27 14:16:42 +00:00
|
|
|
autosave_t *handle = autosave_state.list[i];
|
|
|
|
if (handle)
|
|
|
|
slock_unlock(handle->lock);
|
2016-10-04 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-11 10:53:21 +00:00
|
|
|
#endif
|
2016-10-04 07:56:58 +00:00
|
|
|
|
2016-09-22 14:28:45 +00:00
|
|
|
/**
|
|
|
|
* undo_load_state:
|
|
|
|
* Revert to the state before a state was loaded.
|
|
|
|
*
|
|
|
|
* Returns: true if successful, false otherwise.
|
|
|
|
**/
|
|
|
|
bool content_undo_load_state(void)
|
2016-06-07 17:33:01 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
2016-09-22 14:28:45 +00:00
|
|
|
retro_ctx_serialize_info_t serial_info;
|
|
|
|
size_t temp_data_size;
|
|
|
|
bool ret = false;
|
2016-06-07 17:33:01 +00:00
|
|
|
unsigned num_blocks = 0;
|
2016-09-22 14:28:45 +00:00
|
|
|
void* temp_data = NULL;
|
|
|
|
struct sram_block *blocks = NULL;
|
2016-06-07 17:33:01 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
|
2016-09-22 14:28:45 +00:00
|
|
|
RARCH_LOG("%s: \"%s\".\n",
|
|
|
|
msg_hash_to_str(MSG_LOADING_STATE),
|
|
|
|
undo_load_buf.path);
|
|
|
|
|
|
|
|
RARCH_LOG("%s: %u %s.\n",
|
|
|
|
msg_hash_to_str(MSG_STATE_SIZE),
|
2019-07-14 11:22:59 +00:00
|
|
|
(unsigned int)undo_load_buf.size,
|
2016-09-22 14:28:45 +00:00
|
|
|
msg_hash_to_str(MSG_BYTES));
|
|
|
|
|
2017-12-12 07:55:31 +00:00
|
|
|
/* TODO/FIXME - This checking of SRAM overwrite,
|
2016-10-22 03:13:37 +00:00
|
|
|
* the backing up of it and
|
2017-12-12 07:55:31 +00:00
|
|
|
* its flushing could all be in their
|
2016-10-22 03:13:37 +00:00
|
|
|
* own functions... */
|
2017-04-28 11:43:47 +00:00
|
|
|
if (settings->bools.block_sram_overwrite && task_save_files
|
2016-09-19 02:22:02 +00:00
|
|
|
&& task_save_files->size)
|
2016-06-07 17:33:01 +00:00
|
|
|
{
|
|
|
|
RARCH_LOG("%s.\n",
|
|
|
|
msg_hash_to_str(MSG_BLOCKING_SRAM_OVERWRITE));
|
2016-09-22 14:28:45 +00:00
|
|
|
blocks = (struct sram_block*)
|
2016-09-19 02:22:02 +00:00
|
|
|
calloc(task_save_files->size, sizeof(*blocks));
|
2016-06-07 17:33:01 +00:00
|
|
|
|
2016-09-22 14:28:45 +00:00
|
|
|
if (blocks)
|
2016-06-07 17:33:01 +00:00
|
|
|
{
|
2017-02-26 09:33:03 +00:00
|
|
|
num_blocks = (unsigned)task_save_files->size;
|
2016-06-07 17:33:01 +00:00
|
|
|
for (i = 0; i < num_blocks; i++)
|
2016-09-22 14:28:45 +00:00
|
|
|
blocks[i].type = task_save_files->elems[i].attr.i;
|
2016-06-07 17:33:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_blocks; i++)
|
|
|
|
{
|
|
|
|
retro_ctx_memory_info_t mem_info;
|
|
|
|
|
2016-09-22 14:28:45 +00:00
|
|
|
mem_info.id = blocks[i].type;
|
2016-06-07 17:33:01 +00:00
|
|
|
core_get_memory(&mem_info);
|
|
|
|
|
2016-09-22 14:28:45 +00:00
|
|
|
blocks[i].size = mem_info.size;
|
2016-06-07 17:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_blocks; i++)
|
2016-09-22 14:28:45 +00:00
|
|
|
if (blocks[i].size)
|
|
|
|
blocks[i].data = malloc(blocks[i].size);
|
2016-06-07 17:33:01 +00:00
|
|
|
|
|
|
|
/* Backup current SRAM which is overwritten by unserialize. */
|
|
|
|
for (i = 0; i < num_blocks; i++)
|
|
|
|
{
|
2016-09-22 14:28:45 +00:00
|
|
|
if (blocks[i].data)
|
|
|
|
{
|
|
|
|
retro_ctx_memory_info_t mem_info;
|
|
|
|
const void *ptr = NULL;
|
2016-06-07 17:33:01 +00:00
|
|
|
|
2016-09-22 14:28:45 +00:00
|
|
|
mem_info.id = blocks[i].type;
|
2016-09-22 14:10:51 +00:00
|
|
|
|
2016-09-22 14:28:45 +00:00
|
|
|
core_get_memory(&mem_info);
|
2016-09-22 14:10:51 +00:00
|
|
|
|
2016-09-22 14:28:45 +00:00
|
|
|
ptr = mem_info.data;
|
|
|
|
if (ptr)
|
|
|
|
memcpy(blocks[i].data, ptr, blocks[i].size);
|
|
|
|
}
|
2016-09-22 12:38:50 +00:00
|
|
|
}
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2016-09-19 00:44:00 +00:00
|
|
|
/* We need to make a temporary copy of the buffer, to allow the swap below */
|
|
|
|
temp_data = malloc(undo_load_buf.size);
|
|
|
|
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 */
|
2016-10-04 21:58:05 +00:00
|
|
|
content_save_state("RAM", false, false);
|
2016-09-19 00:44:00 +00:00
|
|
|
|
|
|
|
ret = core_unserialize(&serial_info);
|
|
|
|
|
|
|
|
/* Clean up the temporary copy */
|
|
|
|
free(temp_data);
|
|
|
|
temp_data = NULL;
|
|
|
|
|
2016-09-22 14:28:45 +00:00
|
|
|
/* Flush back. */
|
|
|
|
for (i = 0; i < num_blocks; i++)
|
|
|
|
{
|
|
|
|
if (blocks[i].data)
|
|
|
|
{
|
|
|
|
retro_ctx_memory_info_t mem_info;
|
2016-10-22 03:13:37 +00:00
|
|
|
void *ptr = NULL;
|
2016-09-22 14:28:45 +00:00
|
|
|
|
|
|
|
mem_info.id = blocks[i].type;
|
|
|
|
|
|
|
|
core_get_memory(&mem_info);
|
|
|
|
|
|
|
|
ptr = mem_info.data;
|
|
|
|
if (ptr)
|
|
|
|
memcpy(ptr, blocks[i].data, blocks[i].size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_blocks; i++)
|
|
|
|
free(blocks[i].data);
|
2016-09-22 12:38:50 +00:00
|
|
|
free(blocks);
|
2016-06-07 17:33:01 +00:00
|
|
|
|
2016-06-30 04:11:55 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
2016-06-07 17:33:01 +00:00
|
|
|
RARCH_ERR("%s \"%s\".\n",
|
2016-06-08 22:03:49 +00:00
|
|
|
msg_hash_to_str(MSG_FAILED_TO_UNDO_LOAD_STATE),
|
2016-06-09 15:14:40 +00:00
|
|
|
undo_load_buf.path);
|
2016-10-02 08:03:35 +00:00
|
|
|
}
|
2016-06-07 17:33:01 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-08 07:00:32 +00:00
|
|
|
static void undo_save_state_cb(retro_task_t *task,
|
|
|
|
void *task_data,
|
|
|
|
void *user_data, const char *error)
|
2016-06-07 17:33:01 +00:00
|
|
|
{
|
2019-03-01 17:05:14 +00:00
|
|
|
save_task_state_t *state = (save_task_state_t*)task_data;
|
|
|
|
|
2016-06-07 17:33:01 +00:00
|
|
|
/* Wipe the save file buffer as it's intended to be one use only */
|
2016-06-09 04:02:19 +00:00
|
|
|
undo_save_buf.path[0] = '\0';
|
2016-06-11 16:21:09 +00:00
|
|
|
undo_save_buf.size = 0;
|
2016-06-30 04:11:55 +00:00
|
|
|
if (undo_save_buf.data)
|
|
|
|
{
|
2016-06-09 04:02:19 +00:00
|
|
|
free(undo_save_buf.data);
|
|
|
|
undo_save_buf.data = NULL;
|
2016-06-07 17:33:01 +00:00
|
|
|
}
|
2019-03-01 17:05:14 +00:00
|
|
|
|
|
|
|
free(state);
|
2016-06-07 17:33:01 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
/**
|
|
|
|
* task_save_handler_finished:
|
|
|
|
* @task : the task to finish
|
|
|
|
* @state : the state associated with this task
|
|
|
|
*
|
|
|
|
* Close the save state file and finish the task.
|
|
|
|
**/
|
|
|
|
static void task_save_handler_finished(retro_task_t *task,
|
|
|
|
save_task_state_t *state)
|
|
|
|
{
|
2016-12-01 21:38:56 +00:00
|
|
|
save_task_state_t *task_data = NULL;
|
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_finished(task, true);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2017-12-11 13:56:58 +00:00
|
|
|
intfstream_close(state->file);
|
2017-12-11 19:24:00 +00:00
|
|
|
free(state->file);
|
2016-06-07 17:33:01 +00:00
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
if (!task_get_error(task) && task_get_cancelled(task))
|
|
|
|
task_set_error(task, strdup("Task canceled"));
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2016-12-01 21:38:56 +00:00
|
|
|
task_data = (save_task_state_t*)calloc(1, sizeof(*task_data));
|
|
|
|
memcpy(task_data, state, sizeof(*state));
|
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_data(task, task_data);
|
2016-12-01 21:38:56 +00:00
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
if (state->data)
|
2016-10-02 20:04:09 +00:00
|
|
|
{
|
|
|
|
if (state->undo_save && state->data == undo_save_buf.data)
|
|
|
|
undo_save_buf.data = NULL;
|
2016-10-02 08:03:35 +00:00
|
|
|
free(state->data);
|
2016-10-02 20:04:09 +00:00
|
|
|
state->data = NULL;
|
|
|
|
}
|
2016-05-17 13:21:09 +00:00
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
free(state);
|
|
|
|
}
|
2016-05-17 13:21:09 +00:00
|
|
|
|
2019-05-12 07:51:45 +00:00
|
|
|
static void *get_serialized_data(const char *path, size_t serial_size)
|
2018-09-07 23:09:56 +00:00
|
|
|
{
|
|
|
|
retro_ctx_serialize_info_t serial_info;
|
|
|
|
bool ret = false;
|
|
|
|
void *data = NULL;
|
|
|
|
|
2019-03-01 17:05:14 +00:00
|
|
|
if (!serial_size)
|
|
|
|
return NULL;
|
|
|
|
|
2018-09-07 23:09:56 +00:00
|
|
|
data = malloc(serial_size);
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
RARCH_LOG("%s: %d %s.\n",
|
|
|
|
msg_hash_to_str(MSG_STATE_SIZE),
|
|
|
|
(int)serial_size,
|
|
|
|
msg_hash_to_str(MSG_BYTES));
|
|
|
|
|
|
|
|
serial_info.data = data;
|
|
|
|
serial_info.size = serial_size;
|
|
|
|
ret = core_serialize(&serial_info);
|
2019-03-01 17:05:14 +00:00
|
|
|
|
|
|
|
if (!ret)
|
2018-09-07 23:09:56 +00:00
|
|
|
{
|
2019-03-01 17:05:14 +00:00
|
|
|
free(data);
|
|
|
|
return NULL;
|
2018-09-07 23:09:56 +00:00
|
|
|
}
|
2019-03-01 17:05:14 +00:00
|
|
|
|
|
|
|
return data;
|
2018-09-07 23:09:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
/**
|
|
|
|
* task_save_handler:
|
|
|
|
* @task : the task being worked on
|
|
|
|
*
|
|
|
|
* Write a chunk of data to the save state file.
|
|
|
|
**/
|
|
|
|
static void task_save_handler(retro_task_t *task)
|
|
|
|
{
|
|
|
|
int written;
|
|
|
|
ssize_t remaining;
|
2016-10-22 03:13:37 +00:00
|
|
|
save_task_state_t *state = (save_task_state_t*)task->state;
|
2016-05-17 13:21:09 +00:00
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
if (!state->file)
|
|
|
|
{
|
2018-10-04 14:24:10 +00:00
|
|
|
state->file = intfstream_open_file(
|
|
|
|
state->path, RETRO_VFS_FILE_ACCESS_WRITE,
|
2017-12-11 11:53:47 +00:00
|
|
|
RETRO_VFS_FILE_ACCESS_HINT_NONE);
|
2016-05-17 13:21:09 +00:00
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
if (!state->file)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-07 23:09:56 +00:00
|
|
|
if (!state->data)
|
2019-03-01 17:05:14 +00:00
|
|
|
state->data = get_serialized_data(state->path, state->size);
|
2018-09-07 23:09:56 +00:00
|
|
|
|
2016-10-22 03:13:37 +00:00
|
|
|
remaining = MIN(state->size - state->written, SAVE_STATE_CHUNK);
|
2018-09-07 23:09:56 +00:00
|
|
|
|
2019-05-23 11:05:15 +00:00
|
|
|
if (state->data)
|
|
|
|
written = (int)intfstream_write(state->file,
|
2018-08-31 23:41:21 +00:00
|
|
|
(uint8_t*)state->data + state->written, remaining);
|
2018-09-07 23:09:56 +00:00
|
|
|
else
|
2019-05-23 11:05:15 +00:00
|
|
|
written = 0;
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
state->written += written;
|
2016-10-04 21:58:05 +00:00
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_progress(task, (state->written / (float)state->size) * 100);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
if (task_get_cancelled(task) || written != remaining)
|
2016-06-30 04:11:55 +00:00
|
|
|
{
|
2018-10-04 14:24:10 +00:00
|
|
|
char err[8192];
|
2016-10-15 23:31:06 +00:00
|
|
|
|
|
|
|
err[0] = '\0';
|
2016-10-02 20:04:09 +00:00
|
|
|
|
|
|
|
if (state->undo_save)
|
|
|
|
{
|
|
|
|
RARCH_ERR("%s \"%s\".\n",
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_UNDO_SAVE_STATE),
|
|
|
|
undo_save_buf.path);
|
|
|
|
|
|
|
|
snprintf(err, sizeof(err), "%s \"%s\".",
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_UNDO_SAVE_STATE),
|
|
|
|
"RAM");
|
|
|
|
}
|
|
|
|
else
|
2018-10-04 14:24:10 +00:00
|
|
|
snprintf(err, sizeof(err),
|
|
|
|
"%s %s",
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_SAVE_STATE_TO), state->path);
|
2016-10-02 20:04:09 +00:00
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_error(task, strdup(err));
|
2016-10-02 08:03:35 +00:00
|
|
|
task_save_handler_finished(task, state);
|
|
|
|
return;
|
|
|
|
}
|
2016-06-09 15:14:40 +00:00
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
if (state->written == state->size)
|
|
|
|
{
|
2016-12-27 01:14:35 +00:00
|
|
|
char *msg = NULL;
|
2016-10-15 23:31:06 +00:00
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
task_free_title(task);
|
2016-06-11 16:21:09 +00:00
|
|
|
|
2016-10-02 20:04:09 +00:00
|
|
|
if (state->undo_save)
|
2016-12-27 01:14:35 +00:00
|
|
|
msg = strdup(msg_hash_to_str(MSG_RESTORED_OLD_SAVE_STATE));
|
2016-12-29 05:09:58 +00:00
|
|
|
else if (state->state_slot < 0)
|
2016-12-27 01:14:35 +00:00
|
|
|
msg = strdup(msg_hash_to_str(MSG_SAVED_STATE_TO_SLOT_AUTO));
|
2016-10-02 08:03:35 +00:00
|
|
|
else
|
2016-12-27 23:44:19 +00:00
|
|
|
{
|
|
|
|
char new_msg[128];
|
|
|
|
new_msg[0] = '\0';
|
|
|
|
|
|
|
|
snprintf(new_msg, sizeof(new_msg), msg_hash_to_str(MSG_SAVED_STATE_TO_SLOT),
|
2016-12-29 05:09:58 +00:00
|
|
|
state->state_slot);
|
2016-12-27 23:44:19 +00:00
|
|
|
msg = strdup(new_msg);
|
|
|
|
}
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
if (!task_get_mute(task) && msg)
|
2017-10-12 04:26:26 +00:00
|
|
|
{
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_title(task, msg);
|
2017-10-12 04:26:26 +00:00
|
|
|
msg = NULL;
|
|
|
|
}
|
2016-10-04 21:58:05 +00:00
|
|
|
|
2016-10-02 20:04:09 +00:00
|
|
|
task_save_handler_finished(task, state);
|
2016-12-27 19:11:22 +00:00
|
|
|
|
2017-10-11 20:49:13 +00:00
|
|
|
if (!string_is_empty(msg))
|
|
|
|
free(msg);
|
|
|
|
|
2016-12-27 19:11:22 +00:00
|
|
|
return;
|
2016-10-02 20:04:09 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2016-10-02 20:04:09 +00:00
|
|
|
/**
|
|
|
|
* task_push_undo_save_state:
|
|
|
|
* @path : file path of the save state
|
|
|
|
* @data : the save state data to write
|
|
|
|
* @size : the total size of the save state
|
|
|
|
*
|
|
|
|
* Create a new task to undo the last save of the content state.
|
|
|
|
**/
|
2016-10-04 09:19:24 +00:00
|
|
|
static bool task_push_undo_save_state(const char *path, void *data, size_t size)
|
2016-10-02 20:04:09 +00:00
|
|
|
{
|
2018-11-22 14:45:52 +00:00
|
|
|
retro_task_t *task = task_init();
|
2016-10-02 20:04:09 +00:00
|
|
|
save_task_state_t *state = (save_task_state_t*)calloc(1, sizeof(*state));
|
2016-12-29 05:09:58 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2016-10-02 20:04:09 +00:00
|
|
|
|
|
|
|
if (!task || !state)
|
2016-10-04 09:02:01 +00:00
|
|
|
goto error;
|
2016-10-02 20:04:09 +00:00
|
|
|
|
|
|
|
strlcpy(state->path, path, sizeof(state->path));
|
2017-05-13 17:37:16 +00:00
|
|
|
state->data = data;
|
|
|
|
state->size = size;
|
|
|
|
state->undo_save = true;
|
|
|
|
state->state_slot = settings->ints.state_slot;
|
|
|
|
state->has_valid_framebuffer = video_driver_cached_frame_has_valid_framebuffer();
|
|
|
|
|
|
|
|
task->type = TASK_TYPE_BLOCKING;
|
|
|
|
task->state = state;
|
|
|
|
task->handler = task_save_handler;
|
|
|
|
task->callback = undo_save_state_cb;
|
|
|
|
task->title = strdup(msg_hash_to_str(MSG_UNDOING_SAVE_STATE));
|
2016-10-02 20:04:09 +00:00
|
|
|
|
2017-05-14 18:43:39 +00:00
|
|
|
task_queue_push(task);
|
2016-10-04 09:02:01 +00:00
|
|
|
|
2016-10-04 09:19:24 +00:00
|
|
|
return true;
|
2016-10-04 09:02:01 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (data)
|
|
|
|
free(data);
|
2016-10-04 09:16:02 +00:00
|
|
|
if (state)
|
|
|
|
free(state);
|
2016-10-04 09:02:01 +00:00
|
|
|
if (task)
|
|
|
|
free(task);
|
2016-10-04 09:19:24 +00:00
|
|
|
|
|
|
|
return false;
|
2016-10-02 20:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* undo_save_state:
|
|
|
|
* Reverts the last save operation
|
|
|
|
*
|
|
|
|
* Returns: true if successful, false otherwise.
|
|
|
|
**/
|
|
|
|
bool content_undo_save_state(void)
|
|
|
|
{
|
2016-10-04 09:19:24 +00:00
|
|
|
return task_push_undo_save_state(undo_save_buf.path,
|
2016-12-27 19:11:22 +00:00
|
|
|
undo_save_buf.data,
|
|
|
|
undo_save_buf.size);
|
2016-10-02 20:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* task_load_handler_finished:
|
|
|
|
* @task : the task to finish
|
|
|
|
* @state : the state associated with this task
|
|
|
|
*
|
|
|
|
* Close the loaded state file and finish the task.
|
|
|
|
**/
|
|
|
|
static void task_load_handler_finished(retro_task_t *task,
|
|
|
|
save_task_state_t *state)
|
|
|
|
{
|
|
|
|
load_task_data_t *task_data = NULL;
|
2016-10-22 03:13:37 +00:00
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_finished(task, true);
|
2016-10-02 20:04:09 +00:00
|
|
|
|
2016-10-02 23:31:07 +00:00
|
|
|
if (state->file)
|
2017-12-11 19:24:00 +00:00
|
|
|
{
|
2017-12-11 13:56:58 +00:00
|
|
|
intfstream_close(state->file);
|
2017-12-11 19:24:00 +00:00
|
|
|
free(state->file);
|
|
|
|
}
|
2016-10-02 20:04:09 +00:00
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
if (!task_get_error(task) && task_get_cancelled(task))
|
|
|
|
task_set_error(task, strdup("Task canceled"));
|
2016-10-02 20:04:09 +00:00
|
|
|
|
|
|
|
task_data = (load_task_data_t*)calloc(1, sizeof(*task_data));
|
2019-01-10 21:49:23 +00:00
|
|
|
|
|
|
|
if (!task_data)
|
|
|
|
return;
|
|
|
|
|
2016-10-02 20:04:09 +00:00
|
|
|
memcpy(task_data, state, sizeof(*task_data));
|
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_data(task, task_data);
|
2016-10-02 20:04:09 +00:00
|
|
|
|
|
|
|
free(state);
|
2016-10-02 08:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* task_load_handler:
|
|
|
|
* @task : the task being worked on
|
|
|
|
*
|
|
|
|
* Load a chunk of data from the save state file.
|
|
|
|
**/
|
|
|
|
static void task_load_handler(retro_task_t *task)
|
|
|
|
{
|
2016-10-22 03:13:37 +00:00
|
|
|
ssize_t remaining, bytes_read;
|
2016-10-02 08:03:35 +00:00
|
|
|
save_task_state_t *state = (save_task_state_t*)task->state;
|
|
|
|
|
|
|
|
if (!state->file)
|
2016-05-17 13:21:09 +00:00
|
|
|
{
|
2017-12-11 13:56:58 +00:00
|
|
|
state->file = intfstream_open_file(state->path,
|
2017-12-11 11:15:00 +00:00
|
|
|
RETRO_VFS_FILE_ACCESS_READ,
|
2017-12-11 11:53:47 +00:00
|
|
|
RETRO_VFS_FILE_ACCESS_HINT_NONE);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
if (!state->file)
|
2016-10-02 23:31:07 +00:00
|
|
|
goto error;
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2017-12-11 13:56:58 +00:00
|
|
|
if (intfstream_seek(state->file, 0, SEEK_END) != 0)
|
2016-10-02 23:31:07 +00:00
|
|
|
goto error;
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2017-12-11 13:56:58 +00:00
|
|
|
state->size = intfstream_tell(state->file);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
if (state->size < 0)
|
2016-10-02 23:31:07 +00:00
|
|
|
goto error;
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2017-12-11 13:56:58 +00:00
|
|
|
intfstream_rewind(state->file);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
state->data = malloc(state->size + 1);
|
|
|
|
|
|
|
|
if (!state->data)
|
2016-10-02 23:31:07 +00:00
|
|
|
goto error;
|
2016-05-17 13:21:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 03:13:37 +00:00
|
|
|
remaining = MIN(state->size - state->bytes_read, SAVE_STATE_CHUNK);
|
2017-12-11 13:56:58 +00:00
|
|
|
bytes_read = intfstream_read(state->file,
|
2016-10-22 03:13:37 +00:00
|
|
|
(uint8_t*)state->data + state->bytes_read, remaining);
|
2016-10-02 08:03:35 +00:00
|
|
|
state->bytes_read += bytes_read;
|
2016-10-04 12:35:08 +00:00
|
|
|
|
|
|
|
if (state->size > 0)
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_progress(task, (state->bytes_read / (float)state->size) * 100);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
if (task_get_cancelled(task) || bytes_read != remaining)
|
2016-10-02 08:03:35 +00:00
|
|
|
{
|
|
|
|
if (state->autoload)
|
|
|
|
{
|
2018-09-28 19:40:17 +00:00
|
|
|
char *msg = (char*)malloc(8192 * sizeof(char));
|
2016-10-15 23:31:06 +00:00
|
|
|
|
|
|
|
msg[0] = '\0';
|
|
|
|
|
2017-09-10 19:58:05 +00:00
|
|
|
snprintf(msg,
|
2018-09-28 19:40:17 +00:00
|
|
|
8192 * sizeof(char),
|
2017-09-10 19:58:05 +00:00
|
|
|
"%s \"%s\" %s.",
|
2016-10-22 03:10:13 +00:00
|
|
|
msg_hash_to_str(MSG_AUTOLOADING_SAVESTATE_FROM),
|
|
|
|
state->path,
|
|
|
|
msg_hash_to_str(MSG_FAILED));
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_error(task, strdup(msg));
|
2017-09-11 00:22:46 +00:00
|
|
|
free(msg);
|
2016-10-02 08:03:35 +00:00
|
|
|
}
|
|
|
|
else
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_error(task, strdup(msg_hash_to_str(MSG_FAILED_TO_LOAD_STATE)));
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
free(state->data);
|
|
|
|
state->data = NULL;
|
|
|
|
task_load_handler_finished(task, state);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->bytes_read == state->size)
|
|
|
|
{
|
2018-10-04 14:24:10 +00:00
|
|
|
size_t sizeof_msg = 8192;
|
|
|
|
char *msg = (char*)malloc(sizeof_msg * sizeof(char));
|
2016-10-15 23:31:06 +00:00
|
|
|
|
2018-10-04 14:24:10 +00:00
|
|
|
msg[0] = '\0';
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
task_free_title(task);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
if (state->autoload)
|
2018-10-04 14:24:10 +00:00
|
|
|
snprintf(msg, sizeof_msg,
|
2017-09-10 19:58:05 +00:00
|
|
|
"%s \"%s\" %s.",
|
2016-10-22 03:10:13 +00:00
|
|
|
msg_hash_to_str(MSG_AUTOLOADING_SAVESTATE_FROM),
|
|
|
|
state->path,
|
|
|
|
msg_hash_to_str(MSG_SUCCEEDED));
|
2016-10-02 08:03:35 +00:00
|
|
|
else
|
|
|
|
{
|
2016-12-29 05:09:58 +00:00
|
|
|
if (state->state_slot < 0)
|
2017-09-10 19:58:05 +00:00
|
|
|
strlcpy(msg, msg_hash_to_str(MSG_LOADED_STATE_FROM_SLOT_AUTO),
|
2018-10-04 14:24:10 +00:00
|
|
|
sizeof_msg);
|
2016-10-02 08:03:35 +00:00
|
|
|
else
|
2018-10-04 14:24:10 +00:00
|
|
|
snprintf(msg, sizeof_msg,
|
2017-09-10 19:58:05 +00:00
|
|
|
msg_hash_to_str(MSG_LOADED_STATE_FROM_SLOT),
|
2016-12-29 05:09:58 +00:00
|
|
|
state->state_slot);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
if (!task_get_mute(task))
|
|
|
|
task_set_title(task, strdup(msg));
|
2016-12-27 01:14:35 +00:00
|
|
|
|
2017-09-10 19:58:05 +00:00
|
|
|
free(msg);
|
2016-10-02 08:03:35 +00:00
|
|
|
task_load_handler_finished(task, state);
|
2016-12-27 19:11:22 +00:00
|
|
|
|
|
|
|
return;
|
2016-10-02 08:03:35 +00:00
|
|
|
}
|
2016-10-02 23:31:07 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
task_load_handler_finished(task, state);
|
2016-05-17 13:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-02 08:03:35 +00:00
|
|
|
* content_load_state_cb:
|
2016-05-17 13:21:09 +00:00
|
|
|
* @path : path that state will be loaded from.
|
|
|
|
* Load a state from disk to memory.
|
|
|
|
*
|
|
|
|
**/
|
2019-02-08 07:00:32 +00:00
|
|
|
static void content_load_state_cb(retro_task_t *task,
|
|
|
|
void *task_data,
|
|
|
|
void *user_data, const char *error)
|
2016-05-17 13:21:09 +00:00
|
|
|
{
|
2016-10-15 23:31:06 +00:00
|
|
|
retro_ctx_serialize_info_t serial_info;
|
2016-09-22 14:28:45 +00:00
|
|
|
unsigned i;
|
2016-10-02 08:03:35 +00:00
|
|
|
bool ret;
|
2016-10-15 23:31:06 +00:00
|
|
|
load_task_data_t *load_data = (load_task_data_t*)task_data;
|
|
|
|
ssize_t size = load_data->size;
|
2016-12-27 19:11:22 +00:00
|
|
|
unsigned num_blocks = 0;
|
2016-10-15 23:31:06 +00:00
|
|
|
void *buf = load_data->data;
|
2016-12-27 19:11:22 +00:00
|
|
|
struct sram_block *blocks = NULL;
|
2016-10-15 23:31:06 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
|
2016-05-17 13:21:09 +00:00
|
|
|
RARCH_LOG("%s: \"%s\".\n",
|
|
|
|
msg_hash_to_str(MSG_LOADING_STATE),
|
2016-10-02 08:03:35 +00:00
|
|
|
load_data->path);
|
2016-05-17 13:21:09 +00:00
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
if (size < 0 || !buf)
|
2016-05-17 13:21:09 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
RARCH_LOG("%s: %u %s.\n",
|
|
|
|
msg_hash_to_str(MSG_STATE_SIZE),
|
|
|
|
(unsigned)size,
|
|
|
|
msg_hash_to_str(MSG_BYTES));
|
|
|
|
|
2016-06-07 17:33:01 +00:00
|
|
|
/* This means we're backing up the file in memory, so content_undo_save_state()
|
|
|
|
can restore it */
|
2016-10-02 08:03:35 +00:00
|
|
|
if (load_data->load_to_backup_buffer)
|
2016-06-30 04:11:55 +00:00
|
|
|
{
|
2016-06-07 17:33:01 +00:00
|
|
|
/* If we were previously backing up a file, let go of it first */
|
2016-06-30 04:11:55 +00:00
|
|
|
if (undo_save_buf.data)
|
|
|
|
{
|
2016-06-09 04:02:19 +00:00
|
|
|
free(undo_save_buf.data);
|
|
|
|
undo_save_buf.data = NULL;
|
2016-06-07 17:33:01 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 04:02:19 +00:00
|
|
|
undo_save_buf.data = malloc(size);
|
2016-06-11 16:21:09 +00:00
|
|
|
if (!undo_save_buf.data)
|
|
|
|
goto error;
|
2016-06-08 16:07:04 +00:00
|
|
|
|
2016-06-11 16:21:09 +00:00
|
|
|
memcpy(undo_save_buf.data, buf, size);
|
2016-06-09 04:02:19 +00:00
|
|
|
undo_save_buf.size = size;
|
2016-10-02 08:03:35 +00:00
|
|
|
strlcpy(undo_save_buf.path, load_data->path, sizeof(undo_save_buf.path));
|
2016-06-07 17:33:01 +00:00
|
|
|
|
|
|
|
free(buf);
|
2016-10-02 08:03:35 +00:00
|
|
|
free(load_data);
|
|
|
|
return;
|
2016-06-07 17:33:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 11:43:47 +00:00
|
|
|
if (settings->bools.block_sram_overwrite && task_save_files
|
2016-09-22 14:28:45 +00:00
|
|
|
&& task_save_files->size)
|
|
|
|
{
|
|
|
|
RARCH_LOG("%s.\n",
|
|
|
|
msg_hash_to_str(MSG_BLOCKING_SRAM_OVERWRITE));
|
|
|
|
blocks = (struct sram_block*)
|
|
|
|
calloc(task_save_files->size, sizeof(*blocks));
|
|
|
|
|
|
|
|
if (blocks)
|
|
|
|
{
|
2017-02-26 09:33:03 +00:00
|
|
|
num_blocks = (unsigned)task_save_files->size;
|
2016-09-22 14:28:45 +00:00
|
|
|
for (i = 0; i < num_blocks; i++)
|
|
|
|
blocks[i].type = task_save_files->elems[i].attr.i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_blocks; i++)
|
|
|
|
{
|
|
|
|
retro_ctx_memory_info_t mem_info;
|
|
|
|
|
|
|
|
mem_info.id = blocks[i].type;
|
|
|
|
core_get_memory(&mem_info);
|
|
|
|
|
|
|
|
blocks[i].size = mem_info.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_blocks; i++)
|
|
|
|
if (blocks[i].size)
|
|
|
|
blocks[i].data = malloc(blocks[i].size);
|
|
|
|
|
|
|
|
/* Backup current SRAM which is overwritten by unserialize. */
|
|
|
|
for (i = 0; i < num_blocks; i++)
|
|
|
|
{
|
|
|
|
if (blocks[i].data)
|
|
|
|
{
|
|
|
|
retro_ctx_memory_info_t mem_info;
|
|
|
|
const void *ptr = NULL;
|
|
|
|
|
|
|
|
mem_info.id = blocks[i].type;
|
|
|
|
|
|
|
|
core_get_memory(&mem_info);
|
|
|
|
|
|
|
|
ptr = mem_info.data;
|
|
|
|
if (ptr)
|
|
|
|
memcpy(blocks[i].data, ptr, blocks[i].size);
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 13:21:09 +00:00
|
|
|
|
|
|
|
serial_info.data_const = buf;
|
|
|
|
serial_info.size = size;
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2016-06-07 17:33:01 +00:00
|
|
|
/* Backup the current state so we can undo this load */
|
2016-10-04 21:58:05 +00:00
|
|
|
content_save_state("RAM", false, false);
|
2016-05-17 13:21:09 +00:00
|
|
|
|
2016-09-19 00:44:00 +00:00
|
|
|
ret = core_unserialize(&serial_info);
|
2016-05-17 13:21:09 +00:00
|
|
|
|
2016-09-22 14:28:45 +00:00
|
|
|
/* Flush back. */
|
|
|
|
for (i = 0; i < num_blocks; i++)
|
|
|
|
{
|
|
|
|
if (blocks[i].data)
|
|
|
|
{
|
|
|
|
retro_ctx_memory_info_t mem_info;
|
|
|
|
void *ptr = NULL;
|
|
|
|
|
|
|
|
mem_info.id = blocks[i].type;
|
|
|
|
|
|
|
|
core_get_memory(&mem_info);
|
|
|
|
|
|
|
|
ptr = mem_info.data;
|
|
|
|
if (ptr)
|
|
|
|
memcpy(ptr, blocks[i].data, blocks[i].size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_blocks; i++)
|
|
|
|
free(blocks[i].data);
|
2016-09-22 12:38:50 +00:00
|
|
|
free(blocks);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2016-05-17 13:21:09 +00:00
|
|
|
if (!ret)
|
|
|
|
goto error;
|
|
|
|
|
2016-06-01 00:40:58 +00:00
|
|
|
free(buf);
|
2016-10-02 08:03:35 +00:00
|
|
|
free(load_data);
|
2016-06-01 00:40:58 +00:00
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
return;
|
2016-05-17 13:21:09 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
RARCH_ERR("%s \"%s\".\n",
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_LOAD_STATE),
|
2016-10-02 08:03:35 +00:00
|
|
|
load_data->path);
|
|
|
|
if (buf)
|
|
|
|
free(buf);
|
|
|
|
free(load_data);
|
|
|
|
}
|
|
|
|
|
2016-12-01 21:38:56 +00:00
|
|
|
/**
|
|
|
|
* save_state_cb:
|
|
|
|
*
|
|
|
|
* Called after the save state is done. Takes a screenshot if needed.
|
|
|
|
**/
|
2019-02-08 07:00:32 +00:00
|
|
|
static void save_state_cb(retro_task_t *task,
|
|
|
|
void *task_data,
|
|
|
|
void *user_data, const char *error)
|
2016-12-01 21:38:56 +00:00
|
|
|
{
|
2019-05-22 03:54:35 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2016-12-01 21:38:56 +00:00
|
|
|
save_task_state_t *state = (save_task_state_t*)task_data;
|
|
|
|
char *path = strdup(state->path);
|
|
|
|
|
2016-12-29 05:09:58 +00:00
|
|
|
if (state->thumbnail_enable)
|
2019-05-22 03:54:35 +00:00
|
|
|
take_screenshot(settings->paths.directory_screenshot,
|
|
|
|
path, true, state->has_valid_framebuffer, false, true);
|
2016-12-01 21:38:56 +00:00
|
|
|
|
|
|
|
free(path);
|
2019-03-01 17:05:14 +00:00
|
|
|
free(state);
|
2016-12-01 21:38:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
/**
|
|
|
|
* task_push_save_state:
|
|
|
|
* @path : file path of the save state
|
|
|
|
* @data : the save state data to write
|
|
|
|
* @size : the total size of the save state
|
|
|
|
*
|
|
|
|
* Create a new task to save the content state.
|
|
|
|
**/
|
2016-10-04 21:58:05 +00:00
|
|
|
static void task_push_save_state(const char *path, void *data, size_t size, bool autosave)
|
2016-10-02 08:03:35 +00:00
|
|
|
{
|
2018-11-22 14:45:52 +00:00
|
|
|
retro_task_t *task = task_init();
|
2016-10-02 08:03:35 +00:00
|
|
|
save_task_state_t *state = (save_task_state_t*)calloc(1, sizeof(*state));
|
2016-12-29 05:09:58 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
if (!task || !state)
|
2016-10-04 09:02:01 +00:00
|
|
|
goto error;
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
strlcpy(state->path, path, sizeof(state->path));
|
2016-12-29 05:09:58 +00:00
|
|
|
state->data = data;
|
|
|
|
state->size = size;
|
|
|
|
state->autosave = autosave;
|
|
|
|
state->mute = autosave; /* don't show OSD messages if we are auto-saving */
|
2017-04-28 11:43:47 +00:00
|
|
|
state->thumbnail_enable = settings->bools.savestate_thumbnail_enable;
|
2017-04-28 17:12:48 +00:00
|
|
|
state->state_slot = settings->ints.state_slot;
|
2017-05-13 17:37:16 +00:00
|
|
|
state->has_valid_framebuffer = video_driver_cached_frame_has_valid_framebuffer();
|
2016-12-29 05:09:58 +00:00
|
|
|
|
|
|
|
task->type = TASK_TYPE_BLOCKING;
|
|
|
|
task->state = state;
|
|
|
|
task->handler = task_save_handler;
|
|
|
|
task->callback = save_state_cb;
|
2017-01-23 13:15:26 +00:00
|
|
|
task->title = strdup(msg_hash_to_str(MSG_SAVING_STATE));
|
2016-12-29 05:09:58 +00:00
|
|
|
task->mute = state->mute;
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2019-03-01 17:05:14 +00:00
|
|
|
if (!task_queue_push(task))
|
|
|
|
{
|
|
|
|
/* Another blocking task is already active. */
|
|
|
|
if (data)
|
|
|
|
free(data);
|
|
|
|
if (task->title)
|
|
|
|
task_free_title(task);
|
|
|
|
free(task);
|
|
|
|
free(state);
|
|
|
|
}
|
2016-10-04 09:02:01 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (data)
|
|
|
|
free(data);
|
2016-10-04 09:16:02 +00:00
|
|
|
if (state)
|
|
|
|
free(state);
|
2016-10-04 09:02:01 +00:00
|
|
|
if (task)
|
2019-03-01 17:05:14 +00:00
|
|
|
{
|
|
|
|
if (task->title)
|
|
|
|
task_free_title(task);
|
2016-10-04 09:02:01 +00:00
|
|
|
free(task);
|
2019-03-01 17:05:14 +00:00
|
|
|
}
|
2016-10-02 08:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_load_and_save_state_cb:
|
|
|
|
* @path : path that state will be loaded from.
|
|
|
|
* Load then save a state.
|
|
|
|
*
|
|
|
|
**/
|
2019-02-08 07:00:32 +00:00
|
|
|
static void content_load_and_save_state_cb(retro_task_t *task,
|
|
|
|
void *task_data,
|
|
|
|
void *user_data, const char *error)
|
2016-10-02 08:03:35 +00:00
|
|
|
{
|
|
|
|
load_task_data_t *load_data = (load_task_data_t*)task_data;
|
2016-10-04 09:02:01 +00:00
|
|
|
char *path = strdup(load_data->path);
|
|
|
|
void *data = load_data->undo_data;
|
|
|
|
size_t size = load_data->undo_size;
|
2016-10-04 21:58:05 +00:00
|
|
|
bool autosave = load_data->autosave;
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2019-02-08 07:00:32 +00:00
|
|
|
content_load_state_cb(task, task_data, user_data, error);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2016-10-04 21:58:05 +00:00
|
|
|
task_push_save_state(path, data, size, autosave);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* task_push_load_and_save_state:
|
|
|
|
* @path : file path of the save state
|
|
|
|
* @data : the save state data to write
|
|
|
|
* @size : the total size of the save state
|
|
|
|
* @load_to_backup_buffer : If true, the state will be loaded into undo_save_buf.
|
|
|
|
*
|
2016-10-02 18:51:30 +00:00
|
|
|
* Create a new task to load current state first into a backup buffer (for undo)
|
|
|
|
* and then save the content state.
|
2016-10-02 08:03:35 +00:00
|
|
|
**/
|
2016-10-22 03:10:13 +00:00
|
|
|
static void task_push_load_and_save_state(const char *path, void *data,
|
|
|
|
size_t size, bool load_to_backup_buffer, bool autosave)
|
2016-10-02 08:03:35 +00:00
|
|
|
{
|
2019-05-23 11:05:15 +00:00
|
|
|
retro_task_t *task = NULL;
|
|
|
|
settings_t *settings = NULL;
|
|
|
|
save_task_state_t *state = (save_task_state_t*)
|
|
|
|
calloc(1, sizeof(*state));
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2019-05-23 11:05:15 +00:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
task = task_init();
|
|
|
|
|
|
|
|
if (!task)
|
|
|
|
{
|
|
|
|
free(state);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
settings = config_get_ptr();
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
strlcpy(state->path, path, sizeof(state->path));
|
|
|
|
state->load_to_backup_buffer = load_to_backup_buffer;
|
2017-02-19 01:10:37 +00:00
|
|
|
state->undo_size = size;
|
|
|
|
state->undo_data = data;
|
|
|
|
state->autosave = autosave;
|
2019-05-23 11:05:15 +00:00
|
|
|
state->mute = autosave; /* don't show OSD messages if we
|
|
|
|
are auto-saving */
|
|
|
|
if (load_to_backup_buffer)
|
|
|
|
state->mute = true;
|
|
|
|
state->state_slot = settings->ints.state_slot;
|
|
|
|
state->has_valid_framebuffer =
|
|
|
|
video_driver_cached_frame_has_valid_framebuffer();
|
2017-02-19 01:10:37 +00:00
|
|
|
|
|
|
|
task->state = state;
|
|
|
|
task->type = TASK_TYPE_BLOCKING;
|
|
|
|
task->handler = task_load_handler;
|
|
|
|
task->callback = content_load_and_save_state_cb;
|
|
|
|
task->title = strdup(msg_hash_to_str(MSG_LOADING_STATE));
|
|
|
|
task->mute = state->mute;
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2019-03-01 17:05:14 +00:00
|
|
|
if (!task_queue_push(task))
|
|
|
|
{
|
|
|
|
/* Another blocking task is already active. */
|
|
|
|
if (data)
|
|
|
|
free(data);
|
|
|
|
if (task->title)
|
|
|
|
task_free_title(task);
|
|
|
|
free(task);
|
|
|
|
free(state);
|
|
|
|
}
|
2016-10-02 08:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_save_state:
|
|
|
|
* @path : path of saved state that shall be written to.
|
|
|
|
* @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.
|
|
|
|
**/
|
2016-10-04 21:58:05 +00:00
|
|
|
bool content_save_state(const char *path, bool save_to_disk, bool autosave)
|
2016-10-02 08:03:35 +00:00
|
|
|
{
|
|
|
|
retro_ctx_size_info_t info;
|
|
|
|
void *data = NULL;
|
|
|
|
|
|
|
|
core_serialize_size(&info);
|
|
|
|
|
2018-08-31 23:41:21 +00:00
|
|
|
if (info.size == 0)
|
|
|
|
return false;
|
|
|
|
|
2019-03-01 17:05:14 +00:00
|
|
|
if (!save_state_in_background)
|
2018-09-07 23:09:56 +00:00
|
|
|
{
|
|
|
|
RARCH_LOG("%s: \"%s\".\n",
|
|
|
|
msg_hash_to_str(MSG_SAVING_STATE),
|
|
|
|
path);
|
2018-08-31 23:41:21 +00:00
|
|
|
|
2019-03-01 17:05:14 +00:00
|
|
|
data = get_serialized_data(path, info.size);
|
2018-08-31 23:41:21 +00:00
|
|
|
|
2018-09-07 23:09:56 +00:00
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
RARCH_ERR("%s \"%s\".\n",
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_SAVE_STATE_TO),
|
|
|
|
path);
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-31 23:41:21 +00:00
|
|
|
|
2018-09-07 23:09:56 +00:00
|
|
|
RARCH_LOG("%s: %d %s.\n",
|
|
|
|
msg_hash_to_str(MSG_STATE_SIZE),
|
|
|
|
(int)info.size,
|
|
|
|
msg_hash_to_str(MSG_BYTES));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (save_to_disk)
|
2016-10-02 08:03:35 +00:00
|
|
|
{
|
2019-05-31 04:43:20 +00:00
|
|
|
if (path_is_valid(path) && !autosave)
|
2016-10-02 08:03:35 +00:00
|
|
|
{
|
2018-09-07 23:09:56 +00:00
|
|
|
/* Before overwritting the savestate file, load it into a buffer
|
|
|
|
to allow undo_save_state() to work */
|
|
|
|
/* TODO/FIXME - Use msg_hash_to_str here */
|
|
|
|
RARCH_LOG("%s ...\n",
|
|
|
|
msg_hash_to_str(MSG_FILE_ALREADY_EXISTS_SAVING_TO_BACKUP_BUFFER));
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2018-09-07 23:09:56 +00:00
|
|
|
task_push_load_and_save_state(path, data, info.size, true, autosave);
|
2016-10-02 08:03:35 +00:00
|
|
|
}
|
|
|
|
else
|
2018-09-07 23:09:56 +00:00
|
|
|
task_push_save_state(path, data, info.size, autosave);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-12 07:51:45 +00:00
|
|
|
if (!data)
|
2019-03-01 17:05:14 +00:00
|
|
|
data = get_serialized_data(path, info.size);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2019-05-12 07:51:45 +00:00
|
|
|
if (!data)
|
2018-09-07 23:09:56 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("%s \"%s\".\n",
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_SAVE_STATE_TO),
|
|
|
|
path);
|
2019-03-01 17:05:14 +00:00
|
|
|
return false;
|
2018-09-07 23:09:56 +00:00
|
|
|
}
|
|
|
|
/* save_to_disk is false, which means we are saving the state
|
|
|
|
in undo_load_buf to allow content_undo_load_state() to restore it */
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2018-09-07 23:09:56 +00:00
|
|
|
/* If we were holding onto an old state already, clean it up first */
|
|
|
|
if (undo_load_buf.data)
|
|
|
|
{
|
|
|
|
free(undo_load_buf.data);
|
|
|
|
undo_load_buf.data = NULL;
|
|
|
|
}
|
2018-08-31 23:41:21 +00:00
|
|
|
|
2018-09-07 23:09:56 +00:00
|
|
|
undo_load_buf.data = malloc(info.size);
|
|
|
|
if (!undo_load_buf.data)
|
|
|
|
{
|
2016-10-02 08:03:35 +00:00
|
|
|
free(data);
|
2018-09-07 23:09:56 +00:00
|
|
|
return false;
|
2016-10-02 08:03:35 +00:00
|
|
|
}
|
2018-09-07 23:09:56 +00:00
|
|
|
|
|
|
|
memcpy(undo_load_buf.data, data, info.size);
|
2016-10-02 08:03:35 +00:00
|
|
|
free(data);
|
2018-09-07 23:09:56 +00:00
|
|
|
undo_load_buf.size = info.size;
|
|
|
|
strlcpy(undo_load_buf.path, path, sizeof(undo_load_buf.path));
|
2016-10-02 08:03:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 23:09:56 +00:00
|
|
|
return true;
|
2016-10-02 08:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_load_state:
|
|
|
|
* @path : path that state will be loaded from.
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
**/
|
2016-10-22 03:10:13 +00:00
|
|
|
bool content_load_state(const char *path,
|
|
|
|
bool load_to_backup_buffer, bool autoload)
|
2016-10-02 08:03:35 +00:00
|
|
|
{
|
2018-11-22 14:45:52 +00:00
|
|
|
retro_task_t *task = task_init();
|
2017-05-07 15:52:48 +00:00
|
|
|
save_task_state_t *state = (save_task_state_t*)calloc(1, sizeof(*state));
|
|
|
|
settings_t *settings = config_get_ptr();
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
if (!task || !state)
|
2016-10-04 09:02:01 +00:00
|
|
|
goto error;
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
strlcpy(state->path, path, sizeof(state->path));
|
|
|
|
state->load_to_backup_buffer = load_to_backup_buffer;
|
2016-10-22 03:10:13 +00:00
|
|
|
state->autoload = autoload;
|
2017-04-28 17:12:48 +00:00
|
|
|
state->state_slot = settings->ints.state_slot;
|
2019-05-23 11:05:15 +00:00
|
|
|
state->has_valid_framebuffer =
|
|
|
|
video_driver_cached_frame_has_valid_framebuffer();
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2017-04-28 17:12:48 +00:00
|
|
|
task->type = TASK_TYPE_BLOCKING;
|
|
|
|
task->state = state;
|
|
|
|
task->handler = task_load_handler;
|
|
|
|
task->callback = content_load_state_cb;
|
|
|
|
task->title = strdup(msg_hash_to_str(MSG_LOADING_STATE));
|
2016-10-02 08:03:35 +00:00
|
|
|
|
2017-05-14 18:43:39 +00:00
|
|
|
task_queue_push(task);
|
2016-10-02 08:03:35 +00:00
|
|
|
|
|
|
|
return true;
|
2016-10-04 09:02:01 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (state)
|
|
|
|
free(state);
|
|
|
|
if (task)
|
|
|
|
free(task);
|
|
|
|
|
|
|
|
return false;
|
2016-05-17 13:21:09 +00:00
|
|
|
}
|
2016-06-04 17:38:55 +00:00
|
|
|
|
|
|
|
bool content_rename_state(const char *origin, const char *dest)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2017-12-14 19:05:46 +00:00
|
|
|
if (filestream_exists(dest))
|
2017-12-14 18:29:36 +00:00
|
|
|
filestream_delete(dest);
|
2016-06-05 01:04:01 +00:00
|
|
|
|
2017-12-14 18:29:36 +00:00
|
|
|
ret = filestream_rename(origin, dest);
|
2016-06-04 17:38:55 +00:00
|
|
|
if (!ret)
|
2016-06-05 01:04:01 +00:00
|
|
|
return true;
|
|
|
|
|
2016-06-19 19:41:00 +00:00
|
|
|
RARCH_LOG("Error %d renaming file %s\n", ret, origin);
|
2016-06-05 01:04:01 +00:00
|
|
|
return false;
|
2016-06-04 17:38:55 +00:00
|
|
|
}
|
2016-06-08 19:27:54 +00:00
|
|
|
|
|
|
|
/*
|
2016-10-02 08:03:35 +00:00
|
|
|
*
|
2016-06-11 16:21:09 +00:00
|
|
|
* TODO/FIXME: Figure out when and where this should be called.
|
2019-05-23 11:05:15 +00:00
|
|
|
* As it is, when e.g. closing Gambatte, we get the
|
|
|
|
* same printf message 4 times.
|
2016-06-08 19:27:54 +00:00
|
|
|
*/
|
2016-06-23 05:23:17 +00:00
|
|
|
bool content_reset_savestate_backups(void)
|
2016-06-08 19:27:54 +00:00
|
|
|
{
|
2016-06-09 04:02:19 +00:00
|
|
|
if (undo_save_buf.data)
|
2016-06-08 19:27:54 +00:00
|
|
|
{
|
2016-06-09 04:02:19 +00:00
|
|
|
free(undo_save_buf.data);
|
|
|
|
undo_save_buf.data = NULL;
|
2016-06-08 19:27:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 04:02:19 +00:00
|
|
|
undo_save_buf.path[0] = '\0';
|
2016-06-30 04:11:55 +00:00
|
|
|
undo_save_buf.size = 0;
|
2016-06-08 19:27:54 +00:00
|
|
|
|
2016-06-09 04:02:19 +00:00
|
|
|
if (undo_load_buf.data)
|
2016-06-08 19:27:54 +00:00
|
|
|
{
|
2016-06-09 04:02:19 +00:00
|
|
|
free(undo_load_buf.data);
|
|
|
|
undo_load_buf.data = NULL;
|
2016-06-08 19:27:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 04:02:19 +00:00
|
|
|
undo_load_buf.path[0] = '\0';
|
2016-06-30 04:11:55 +00:00
|
|
|
undo_load_buf.size = 0;
|
2016-06-08 19:27:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-10 19:12:43 +00:00
|
|
|
|
2016-06-30 04:11:55 +00:00
|
|
|
bool content_undo_load_buf_is_empty(void)
|
2016-06-10 19:12:43 +00:00
|
|
|
{
|
|
|
|
return undo_load_buf.data == NULL || undo_load_buf.size == 0;
|
|
|
|
}
|
|
|
|
|
2016-06-30 04:11:55 +00:00
|
|
|
bool content_undo_save_buf_is_empty(void)
|
2016-06-10 19:12:43 +00:00
|
|
|
{
|
|
|
|
return undo_save_buf.data == NULL || undo_save_buf.size == 0;
|
|
|
|
}
|
2016-09-19 00:57:23 +00:00
|
|
|
|
|
|
|
static bool content_get_memory(retro_ctx_memory_info_t *mem_info,
|
2016-09-23 00:47:05 +00:00
|
|
|
struct ram_type *ram, unsigned slot)
|
2016-09-19 00:57:23 +00:00
|
|
|
{
|
2016-09-19 02:22:02 +00:00
|
|
|
ram->type = task_save_files->elems[slot].attr.i;
|
|
|
|
ram->path = task_save_files->elems[slot].data;
|
2016-09-19 00:57:23 +00:00
|
|
|
|
|
|
|
mem_info->id = ram->type;
|
|
|
|
|
|
|
|
core_get_memory(mem_info);
|
|
|
|
|
|
|
|
if (!mem_info->data || mem_info->size == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_load_ram_file:
|
|
|
|
* @path : path of RAM state that will be loaded from.
|
|
|
|
* @type : type of memory
|
|
|
|
*
|
|
|
|
* Load a RAM state from disk to memory.
|
|
|
|
*/
|
|
|
|
bool content_load_ram_file(unsigned slot)
|
|
|
|
{
|
2018-04-12 22:18:11 +00:00
|
|
|
int64_t rc;
|
2016-09-23 00:47:05 +00:00
|
|
|
struct ram_type ram;
|
2016-09-19 00:57:23 +00:00
|
|
|
retro_ctx_memory_info_t mem_info;
|
|
|
|
void *buf = NULL;
|
|
|
|
|
|
|
|
if (!content_get_memory(&mem_info, &ram, slot))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!filestream_read_file(ram.path, &buf, &rc))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (rc > 0)
|
|
|
|
{
|
|
|
|
if (rc > (ssize_t)mem_info.size)
|
|
|
|
{
|
|
|
|
RARCH_WARN("SRAM is larger than implementation expects, "
|
|
|
|
"doing partial load (truncating %u %s %s %u).\n",
|
|
|
|
(unsigned)rc,
|
|
|
|
msg_hash_to_str(MSG_BYTES),
|
|
|
|
msg_hash_to_str(MSG_TO),
|
|
|
|
(unsigned)mem_info.size);
|
|
|
|
rc = mem_info.size;
|
|
|
|
}
|
2018-04-12 22:47:42 +00:00
|
|
|
memcpy(mem_info.data, buf, (size_t)rc);
|
2016-09-19 00:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-20 02:08:55 +00:00
|
|
|
/**
|
|
|
|
* dump_to_file_desperate:
|
|
|
|
* @data : pointer to data buffer.
|
|
|
|
* @size : size of @data.
|
|
|
|
* @type : type of file to be saved.
|
|
|
|
*
|
|
|
|
* Attempt to save valuable RAM data somewhere.
|
|
|
|
**/
|
|
|
|
static bool dump_to_file_desperate(const void *data,
|
|
|
|
size_t size, unsigned type)
|
|
|
|
{
|
|
|
|
time_t time_;
|
2019-05-12 07:36:52 +00:00
|
|
|
char *timebuf;
|
|
|
|
char *path;
|
|
|
|
char *application_data = (char*)malloc(PATH_MAX_LENGTH * sizeof(char));
|
|
|
|
application_data[0] = '\0';
|
2016-09-20 02:08:55 +00:00
|
|
|
|
|
|
|
if (!fill_pathname_application_data(application_data,
|
2017-09-10 19:58:05 +00:00
|
|
|
PATH_MAX_LENGTH * sizeof(char)))
|
2019-05-12 07:36:52 +00:00
|
|
|
{
|
|
|
|
free(application_data);
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 02:08:55 +00:00
|
|
|
|
|
|
|
time(&time_);
|
|
|
|
|
2019-05-12 07:36:52 +00:00
|
|
|
timebuf = (char*)malloc(256 * sizeof(char));
|
|
|
|
timebuf[0] = '\0';
|
|
|
|
|
2017-09-10 19:58:05 +00:00
|
|
|
strftime(timebuf,
|
|
|
|
256 * sizeof(char),
|
|
|
|
"%Y-%m-%d-%H-%M-%S", localtime(&time_));
|
2019-05-12 07:36:52 +00:00
|
|
|
|
|
|
|
path = (char*)malloc(PATH_MAX_LENGTH * sizeof(char));
|
|
|
|
path[0] = '\0';
|
|
|
|
snprintf(path,
|
|
|
|
PATH_MAX_LENGTH * sizeof(char),
|
2019-05-23 11:05:15 +00:00
|
|
|
"%s/RetroArch-recovery-%u%s",
|
|
|
|
application_data, type,
|
|
|
|
timebuf);
|
2016-09-20 02:08:55 +00:00
|
|
|
|
2017-09-10 19:58:05 +00:00
|
|
|
free(application_data);
|
|
|
|
free(timebuf);
|
2019-05-12 07:36:52 +00:00
|
|
|
|
|
|
|
if (!filestream_write_file(path, data, size))
|
|
|
|
{
|
|
|
|
free(path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-20 02:08:55 +00:00
|
|
|
RARCH_WARN("Succeeded in saving RAM data to \"%s\".\n", path);
|
2017-09-10 19:58:05 +00:00
|
|
|
free(path);
|
2016-09-20 02:08:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-19 00:57:23 +00:00
|
|
|
/**
|
|
|
|
* content_save_ram_file:
|
|
|
|
* @path : path of RAM state that shall be written to.
|
|
|
|
* @type : type of memory
|
|
|
|
*
|
|
|
|
* Save a RAM state from memory to disk.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool content_save_ram_file(unsigned slot)
|
|
|
|
{
|
2016-09-23 00:47:05 +00:00
|
|
|
struct ram_type ram;
|
2016-09-19 00:57:23 +00:00
|
|
|
retro_ctx_memory_info_t mem_info;
|
|
|
|
|
|
|
|
if (!content_get_memory(&mem_info, &ram, slot))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RARCH_LOG("%s #%u %s \"%s\".\n",
|
|
|
|
msg_hash_to_str(MSG_SAVING_RAM_TYPE),
|
|
|
|
ram.type,
|
|
|
|
msg_hash_to_str(MSG_TO),
|
|
|
|
ram.path);
|
|
|
|
|
2017-09-10 19:58:05 +00:00
|
|
|
if (!filestream_write_file(
|
|
|
|
ram.path, mem_info.data, mem_info.size))
|
2016-09-19 00:57:23 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("%s.\n",
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_SAVE_SRAM));
|
|
|
|
RARCH_WARN("Attempting to recover ...\n");
|
|
|
|
|
2016-10-02 08:03:35 +00:00
|
|
|
/* In case the file could not be written to,
|
2016-09-19 00:57:23 +00:00
|
|
|
* the fallback function 'dump_to_file_desperate'
|
|
|
|
* will be called. */
|
2017-09-10 19:58:05 +00:00
|
|
|
if (!dump_to_file_desperate(
|
|
|
|
mem_info.data, mem_info.size, ram.type))
|
2016-09-19 00:57:23 +00:00
|
|
|
{
|
|
|
|
RARCH_WARN("Failed ... Cannot recover save file.\n");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RARCH_LOG("%s \"%s\".\n",
|
|
|
|
msg_hash_to_str(MSG_SAVED_SUCCESSFULLY_TO),
|
|
|
|
ram.path);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-19 02:05:44 +00:00
|
|
|
|
2016-09-19 02:09:27 +00:00
|
|
|
bool event_save_files(void)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2019-03-01 17:05:14 +00:00
|
|
|
cheat_manager_save_game_specific_cheats();
|
2017-12-12 07:55:31 +00:00
|
|
|
if (!task_save_files ||
|
2017-09-10 19:58:05 +00:00
|
|
|
!rarch_ctl(RARCH_CTL_IS_SRAM_USED, NULL))
|
2016-09-19 02:09:27 +00:00
|
|
|
return false;
|
|
|
|
|
2016-09-19 02:22:02 +00:00
|
|
|
for (i = 0; i < task_save_files->size; i++)
|
2016-09-19 02:09:27 +00:00
|
|
|
content_save_ram_file(i);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-19 02:05:44 +00:00
|
|
|
bool event_load_save_files(void)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2017-12-12 07:55:31 +00:00
|
|
|
if (!task_save_files ||
|
2017-09-10 19:58:05 +00:00
|
|
|
rarch_ctl(RARCH_CTL_IS_SRAM_LOAD_DISABLED, NULL))
|
2016-09-19 02:05:44 +00:00
|
|
|
return false;
|
|
|
|
|
2016-09-19 02:22:02 +00:00
|
|
|
for (i = 0; i < task_save_files->size; i++)
|
2016-09-19 02:05:44 +00:00
|
|
|
content_load_ram_file(i);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-19 02:18:40 +00:00
|
|
|
|
2017-05-07 15:52:48 +00:00
|
|
|
void path_init_savefile_rtc(const char *savefile_path)
|
2016-09-19 02:18:40 +00:00
|
|
|
{
|
|
|
|
union string_list_elem_attr attr;
|
2017-09-10 19:58:05 +00:00
|
|
|
char *savefile_name_rtc = (char*)
|
|
|
|
malloc(PATH_MAX_LENGTH * sizeof(char));
|
2016-09-19 02:18:40 +00:00
|
|
|
|
2016-10-21 03:57:40 +00:00
|
|
|
savefile_name_rtc[0] = '\0';
|
|
|
|
|
2016-09-19 02:18:40 +00:00
|
|
|
attr.i = RETRO_MEMORY_SAVE_RAM;
|
2017-05-07 15:52:48 +00:00
|
|
|
string_list_append(task_save_files, savefile_path, attr);
|
2016-09-19 02:18:40 +00:00
|
|
|
|
|
|
|
/* Infer .rtc save path from save ram path. */
|
|
|
|
attr.i = RETRO_MEMORY_RTC;
|
|
|
|
fill_pathname(savefile_name_rtc,
|
2017-05-07 15:52:48 +00:00
|
|
|
savefile_path,
|
2016-09-19 02:18:40 +00:00
|
|
|
file_path_str(FILE_PATH_RTC_EXTENSION),
|
2017-09-10 19:58:05 +00:00
|
|
|
PATH_MAX_LENGTH * sizeof(char));
|
2016-09-19 02:22:02 +00:00
|
|
|
string_list_append(task_save_files, savefile_name_rtc, attr);
|
2017-09-10 19:58:05 +00:00
|
|
|
free(savefile_name_rtc);
|
2016-09-19 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void path_deinit_savefile(void)
|
|
|
|
{
|
2016-09-19 02:22:02 +00:00
|
|
|
if (task_save_files)
|
|
|
|
string_list_free(task_save_files);
|
|
|
|
task_save_files = NULL;
|
2016-09-19 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void path_init_savefile_new(void)
|
|
|
|
{
|
2016-09-19 02:22:02 +00:00
|
|
|
task_save_files = string_list_new();
|
|
|
|
retro_assert(task_save_files);
|
2016-09-19 02:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *savefile_ptr_get(void)
|
|
|
|
{
|
2016-09-19 02:22:02 +00:00
|
|
|
return task_save_files;
|
2016-09-19 02:18:40 +00:00
|
|
|
}
|
2018-09-07 23:09:56 +00:00
|
|
|
|
|
|
|
void set_save_state_in_background(bool state)
|
|
|
|
{
|
2019-03-01 17:05:14 +00:00
|
|
|
save_state_in_background = state;
|
2018-09-07 23:09:56 +00:00
|
|
|
}
|