Create CONTENT_CTL_LOAD_RAM_FILE and CONTENT_CTL_SAVE_RAM_FILE

This commit is contained in:
twinaphex 2016-01-25 07:15:05 +01:00
parent bb36ad2772
commit 5f3b583452
3 changed files with 62 additions and 42 deletions

View File

@ -431,8 +431,13 @@ static bool event_load_save_files(void)
return false;
for (i = 0; i < global->savefiles->size; i++)
load_ram_file(global->savefiles->elems[i].data,
global->savefiles->elems[i].attr.i);
{
ram_type_t ram;
ram.path = global->savefiles->elems[i].data;
ram.type = global->savefiles->elems[i].attr.i;
content_ctl(CONTENT_CTL_LOAD_RAM_FILE, &ram);
}
return true;
}
@ -1411,14 +1416,16 @@ bool event_cmd_ctl(enum event_command cmd, void *data)
for (i = 0; i < global->savefiles->size; i++)
{
unsigned type = global->savefiles->elems[i].attr.i;
const char *path = global->savefiles->elems[i].data;
ram_type_t ram;
ram.type = global->savefiles->elems[i].attr.i;
ram.path = global->savefiles->elems[i].data;
RARCH_LOG("%s #%u %s \"%s\".\n",
msg_hash_to_str(MSG_SAVING_RAM_TYPE),
type,
ram.type,
msg_hash_to_str(MSG_TO),
path);
save_ram_file(path, type);
ram.path);
content_ctl(CONTENT_CTL_SAVE_RAM_FILE, &ram);
}
}
return true;

View File

@ -296,18 +296,18 @@ error:
*
* Load a RAM state from disk to memory.
*/
void load_ram_file(const char *path, int type)
static bool load_ram_file(ram_type_t *ram)
{
ssize_t rc;
void *buf = NULL;
size_t size = core.retro_get_memory_size(type);
void *data = core.retro_get_memory_data(type);
size_t size = core.retro_get_memory_size(ram->type);
void *data = core.retro_get_memory_data(ram->type);
if (size == 0 || !data)
return;
return false;
if (!read_file(path, &buf, &rc))
return;
if (!read_file(ram->path, &buf, &rc))
return false;
if (rc > 0)
{
@ -325,6 +325,8 @@ void load_ram_file(const char *path, int type)
if (buf)
free(buf);
return true;
}
/**
@ -337,26 +339,28 @@ void load_ram_file(const char *path, int type)
* In case the file could not be written to, a fallback function
* 'dump_to_file_desperate' will be called.
*/
void save_ram_file(const char *path, int type)
static bool save_ram_file(ram_type_t *ram)
{
size_t size = core.retro_get_memory_size(type);
void *data = core.retro_get_memory_data(type);
size_t size = core.retro_get_memory_size(ram->type);
void *data = core.retro_get_memory_data(ram->type);
if (!data || size == 0)
return;
return false;
if (!retro_write_file(path, data, size))
if (!retro_write_file(ram->path, data, size))
{
RARCH_ERR("%s.\n",
msg_hash_to_str(MSG_FAILED_TO_SAVE_SRAM));
RARCH_WARN("Attempting to recover ...\n");
dump_to_file_desperate(data, size, type);
return;
dump_to_file_desperate(data, size, ram->type);
return false;
}
RARCH_LOG("%s \"%s\".\n",
msg_hash_to_str(MSG_SAVED_SUCCESSFULLY_TO),
path);
ram->path);
return true;
}
/* Load the content into memory. */
@ -752,6 +756,22 @@ bool content_ctl(enum content_ctl_state state, void *data)
switch(state)
{
case CONTENT_CTL_LOAD_RAM_FILE:
{
ram_type_t *ram = (ram_type_t*)data;
if (!ram)
return false;
return load_ram_file(ram);
}
break;
case CONTENT_CTL_SAVE_RAM_FILE:
{
ram_type_t *ram = (ram_type_t*)data;
if (!ram)
return false;
save_ram_file(ram);
}
break;
case CONTENT_CTL_DOES_NOT_NEED_CONTENT:
return core_does_not_need_content;
case CONTENT_CTL_SET_DOES_NOT_NEED_CONTENT:

View File

@ -45,6 +45,15 @@ enum content_ctl_state
CONTENT_CTL_GET_CRC,
/* Load a RAM state from disk to memory. */
CONTENT_CTL_LOAD_RAM_FILE,
/* Save a RAM state from memory to disk.
*
* In case the file could not be written to, a fallback function
* 'dump_to_file_desperate' will be called. */
CONTENT_CTL_SAVE_RAM_FILE,
/* Load a state from disk to memory. */
CONTENT_CTL_LOAD_STATE,
@ -55,27 +64,11 @@ enum content_ctl_state
CONTENT_CTL_TEMPORARY_FREE
};
/**
* 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.
*/
void load_ram_file(const char *path, int type);
/**
* 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.
*
* In case the file could not be written to, a fallback function
* 'dump_to_file_desperate' will be called.
*/
void save_ram_file(const char *path, int type);
typedef struct ram_type
{
const char *path;
int type;
} ram_type_t;
bool content_ctl(enum content_ctl_state state, void *data);