mirror of
https://github.com/libretro/RetroArch
synced 2025-03-08 01:14:10 +00:00
Create task_save_ram.c
This commit is contained in:
parent
ce434a7f1c
commit
81c7a8ac04
@ -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 \
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
122
tasks/task_save_ram.c
Normal file
122
tasks/task_save_ram.c
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <streams/file_stream.h>
|
||||
|
||||
#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;
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user