From 5f3b583452ed45f401da890710d58c64026809e5 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Mon, 25 Jan 2016 07:15:05 +0100 Subject: [PATCH] Create CONTENT_CTL_LOAD_RAM_FILE and CONTENT_CTL_SAVE_RAM_FILE --- command_event.c | 21 ++++++++++++++------- content.c | 48 ++++++++++++++++++++++++++++++++++-------------- content.h | 35 ++++++++++++++--------------------- 3 files changed, 62 insertions(+), 42 deletions(-) diff --git a/command_event.c b/command_event.c index 7bc7dadb2e..378539ac6d 100644 --- a/command_event.c +++ b/command_event.c @@ -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; diff --git a/content.c b/content.c index 755ef80af0..987a27d5ce 100644 --- a/content.c +++ b/content.c @@ -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: diff --git a/content.h b/content.h index d3e3635248..99865aa031 100644 --- a/content.h +++ b/content.h @@ -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);