diff --git a/Makefile.common b/Makefile.common index c8403e62e9..5ea6f35f23 100644 --- a/Makefile.common +++ b/Makefile.common @@ -127,6 +127,7 @@ OBJ += frontend/frontend.o \ libretro-common/queues/task_queue.o \ tasks/tasks_internal.o \ tasks/task_content.o \ + tasks/task_save_ram.o \ tasks/task_file_transfer.o \ tasks/task_image.o \ libretro-common/encodings/encoding_utf.o \ diff --git a/griffin/griffin.c b/griffin/griffin.c index e3f58309f3..1817ee4adb 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -813,6 +813,7 @@ NETPLAY DATA RUNLOOP ============================================================ */ #include "../tasks/task_content.c" +#include "../tasks/task_save_ram.c" #include "../tasks/task_image.c" #include "../tasks/task_file_transfer.c" #ifdef HAVE_ZLIB diff --git a/tasks/task_content.c b/tasks/task_content.c index f415aea20e..89314e0f0d 100644 --- a/tasks/task_content.c +++ b/tasks/task_content.c @@ -1011,7 +1011,7 @@ static bool read_content_file(unsigned i, const char *path, void **buf, * * Attempt to save valuable RAM data somewhere. **/ -static bool dump_to_file_desperate(const void *data, +bool dump_to_file_desperate(const void *data, size_t size, unsigned type) { time_t time_; @@ -1211,98 +1211,6 @@ error: return false; } -/** - * 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(ram_type_t *ram) -{ - ssize_t rc; - retro_ctx_memory_info_t mem_info; - void *buf = NULL; - - if (!ram) - return false; - - mem_info.id = ram->type; - - core_get_memory(&mem_info); - - if (mem_info.size == 0 || !mem_info.data) - 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; - } - memcpy(mem_info.data, buf, rc); - } - - if (buf) - free(buf); - - return true; -} - -/** - * 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(ram_type_t *ram) -{ - retro_ctx_memory_info_t mem_info; - - if (!ram) - return false; - - mem_info.id = ram->type; - - core_get_memory(&mem_info); - - if (!mem_info.data || mem_info.size == 0) - return false; - - if (!filestream_write_file(ram->path, mem_info.data, mem_info.size)) - { - RARCH_ERR("%s.\n", - msg_hash_to_str(MSG_FAILED_TO_SAVE_SRAM)); - RARCH_WARN("Attempting to recover ...\n"); - - /* In case the file could not be written to, - * the fallback function 'dump_to_file_desperate' - * will be called. */ - if (!dump_to_file_desperate(mem_info.data, mem_info.size, ram->type)) - { - 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; -} - /* Load the content into memory. */ static bool load_content_into_memory( struct retro_game_info *info, diff --git a/tasks/task_save_ram.c b/tasks/task_save_ram.c new file mode 100644 index 0000000000..e7b63f7cfa --- /dev/null +++ b/tasks/task_save_ram.c @@ -0,0 +1,122 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2011-2016 - Daniel De Matteis + * + * 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 . + */ +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "../core.h" +#include "../msg_hash.h" +#include "../verbosity.h" +#include "tasks_internal.h" + +/* TODO/FIXME - turn this into actual task */ + +/** + * 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(ram_type_t *ram) +{ + ssize_t rc; + retro_ctx_memory_info_t mem_info; + void *buf = NULL; + + if (!ram) + return false; + + mem_info.id = ram->type; + + core_get_memory(&mem_info); + + if (mem_info.size == 0 || !mem_info.data) + 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; + } + memcpy(mem_info.data, buf, rc); + } + + if (buf) + free(buf); + + return true; +} + +/** + * 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(ram_type_t *ram) +{ + retro_ctx_memory_info_t mem_info; + + if (!ram) + return false; + + mem_info.id = ram->type; + + core_get_memory(&mem_info); + + if (!mem_info.data || mem_info.size == 0) + return false; + + if (!filestream_write_file(ram->path, mem_info.data, mem_info.size)) + { + RARCH_ERR("%s.\n", + msg_hash_to_str(MSG_FAILED_TO_SAVE_SRAM)); + RARCH_WARN("Attempting to recover ...\n"); + + /* In case the file could not be written to, + * the fallback function 'dump_to_file_desperate' + * will be called. */ + if (!dump_to_file_desperate(mem_info.data, mem_info.size, ram->type)) + { + 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; +} diff --git a/tasks/tasks_internal.h b/tasks/tasks_internal.h index 3064170395..02606b7cc3 100644 --- a/tasks/tasks_internal.h +++ b/tasks/tasks_internal.h @@ -141,6 +141,10 @@ void rarch_task_file_load_handler(retro_task_t *task); /* TODO/FIXME - turn this into actual task */ bool take_screenshot(void); +bool content_save_ram_file(ram_type_t *ram); +bool content_load_ram_file(ram_type_t *ram); +bool dump_to_file_desperate(const void *data, + size_t size, unsigned type); #ifdef __cplusplus }