RetroArch/file.c

558 lines
15 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
*
* 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 "file.h"
#include "general.h"
#include <stdlib.h>
#include "boolean.h"
#include "libretro.h"
#include <string.h>
#include <time.h>
#include "dynamic.h"
#include "movie.h"
#include "patch.h"
#include "compat/strl.h"
#include "hash.h"
2013-01-21 22:51:56 +00:00
#include "file_extract.h"
2013-04-11 20:35:15 +00:00
#ifdef _WIN32
#ifdef _XBOX
#include <xtl.h>
#define setmode _setmode
#define INVALID_FILE_ATTRIBUTES -1
2013-04-11 20:35:15 +00:00
#else
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#endif
#endif
2014-08-13 06:52:13 +00:00
static void patch_content(uint8_t **buf, ssize_t *size)
{
uint8_t *ret_buf = *buf;
ssize_t ret_size = *size;
const char *patch_desc = NULL;
const char *patch_path = NULL;
patch_error_t err = PATCH_UNKNOWN;
patch_func_t func = NULL;
ssize_t patch_size = 0;
void *patch_data = NULL;
bool success = false;
if (g_extern.ups_pref + g_extern.bps_pref + g_extern.ips_pref > 1)
{
RARCH_WARN("Several patches are explicitly defined, ignoring all ...\n");
return;
}
bool allow_bps = !g_extern.ups_pref && !g_extern.ips_pref;
bool allow_ups = !g_extern.bps_pref && !g_extern.ips_pref;
bool allow_ips = !g_extern.ups_pref && !g_extern.bps_pref;
2014-09-02 03:10:54 +00:00
if (allow_ups && *g_extern.ups_name
&& (patch_size = read_file(g_extern.ups_name, &patch_data)) >= 0)
{
patch_desc = "UPS";
patch_path = g_extern.ups_name;
func = ups_apply_patch;
}
2014-09-02 03:10:54 +00:00
else if (allow_bps && *g_extern.bps_name
&& (patch_size = read_file(g_extern.bps_name, &patch_data)) >= 0)
{
patch_desc = "BPS";
patch_path = g_extern.bps_name;
func = bps_apply_patch;
}
2014-09-02 03:10:54 +00:00
else if (allow_ips && *g_extern.ips_name
&& (patch_size = read_file(g_extern.ips_name, &patch_data)) >= 0)
{
patch_desc = "IPS";
patch_path = g_extern.ips_name;
func = ips_apply_patch;
}
else
{
2014-07-28 18:08:37 +00:00
RARCH_LOG("Did not find a valid content patch.\n");
return;
}
2014-09-02 03:10:54 +00:00
RARCH_LOG("Found %s file in \"%s\", attempting to patch ...\n",
patch_desc, patch_path);
2014-09-02 03:10:54 +00:00
size_t target_size = ret_size * 4; /* Just to be sure. */
2014-08-13 06:52:13 +00:00
uint8_t *patched_content = (uint8_t*)malloc(target_size);
if (!patched_content)
{
2014-07-28 18:08:37 +00:00
RARCH_ERR("Failed to allocate memory for patched content ...\n");
goto error;
}
2014-09-02 03:10:54 +00:00
err = func((const uint8_t*)patch_data, patch_size, ret_buf,
ret_size, patched_content, &target_size);
if (err == PATCH_SUCCESS)
{
2014-07-28 18:08:37 +00:00
RARCH_LOG("Content patched successfully (%s).\n", patch_desc);
success = true;
}
else
2014-09-02 03:10:54 +00:00
RARCH_ERR("Failed to patch %s: Error #%u\n", patch_desc,
(unsigned)err);
if (success)
{
free(ret_buf);
2014-08-13 06:52:13 +00:00
*buf = patched_content;
*size = target_size;
}
free(patch_data);
return;
error:
*buf = ret_buf;
*size = ret_size;
free(patch_data);
}
2014-08-13 06:52:13 +00:00
static ssize_t read_content_file(const char *path, void **buf)
{
uint8_t *ret_buf = NULL;
2014-09-02 15:05:15 +00:00
ssize_t ret = -1;
ret = read_file(path, (void**) &ret_buf);
2014-09-02 15:05:15 +00:00
if (ret <= 0)
return ret;
2014-09-02 03:10:54 +00:00
/* Attempt to apply a patch. */
if (!g_extern.block_patch)
2014-08-13 06:52:13 +00:00
patch_content(&ret_buf, &ret);
2014-08-12 01:19:02 +00:00
g_extern.content_crc = crc32_calculate(ret_buf, ret);
sha256_hash(g_extern.sha256, ret_buf, ret);
2014-09-02 03:10:54 +00:00
2013-01-21 22:51:56 +00:00
RARCH_LOG("CRC32: 0x%x, SHA256: %s\n",
2014-08-12 01:19:02 +00:00
(unsigned)g_extern.content_crc, g_extern.sha256);
*buf = ret_buf;
return ret;
}
2014-09-02 03:10:54 +00:00
/* Attempt to save valuable RAM data somewhere. */
static void dump_to_file_desperate(const void *data,
size_t size, unsigned type)
{
#if defined(_WIN32) && !defined(_XBOX)
const char *base = getenv("APPDATA");
#elif defined(__CELLOS_LV2__) || defined(_XBOX)
const char *base = NULL;
#else
const char *base = getenv("HOME");
#endif
if (!base)
goto error;
char path[PATH_MAX];
2014-04-04 12:58:42 +00:00
snprintf(path, sizeof(path), "%s/RetroArch-recovery-%u", base, type);
char timebuf[PATH_MAX];
time_t time_;
time(&time_);
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d-%H-%M-%S", localtime(&time_));
strlcat(path, timebuf, sizeof(path));
2013-01-21 22:51:56 +00:00
if (write_file(path, data, size))
RARCH_WARN("Succeeded in saving RAM data to \"%s\".\n", path);
else
goto error;
return;
error:
RARCH_WARN("Failed ... Cannot recover save file.\n");
}
bool save_state(const char *path)
{
RARCH_LOG("Saving state: \"%s\".\n", path);
size_t size = pretro_serialize_size();
if (size == 0)
return false;
void *data = malloc(size);
if (!data)
{
RARCH_ERR("Failed to allocate memory for save state buffer.\n");
return false;
}
RARCH_LOG("State size: %d bytes.\n", (int)size);
bool ret = pretro_serialize(data, size);
if (ret)
2013-01-21 22:51:56 +00:00
ret = write_file(path, data, size);
if (!ret)
RARCH_ERR("Failed to save state to \"%s\".\n", path);
free(data);
return ret;
}
2014-04-04 13:35:46 +00:00
struct sram_block
{
unsigned type;
void *data;
size_t size;
};
bool load_state(const char *path)
{
unsigned i;
void *buf = NULL;
ssize_t size = read_file(path, &buf);
RARCH_LOG("Loading state: \"%s\".\n", path);
if (size < 0)
{
RARCH_ERR("Failed to load state from \"%s\".\n", path);
return false;
}
bool ret = true;
RARCH_LOG("State size: %u bytes.\n", (unsigned)size);
2014-04-04 13:35:46 +00:00
struct sram_block *blocks = NULL;
unsigned num_blocks = 0;
2014-09-02 03:10:54 +00:00
if (g_settings.block_sram_overwrite && g_extern.savefiles
&& g_extern.savefiles->size)
{
RARCH_LOG("Blocking SRAM overwrite.\n");
2014-09-02 03:10:54 +00:00
blocks = (struct sram_block*)
calloc(g_extern.savefiles->size, sizeof(*blocks));
2014-04-04 13:35:46 +00:00
if (blocks)
{
2014-04-04 13:35:46 +00:00
num_blocks = g_extern.savefiles->size;
for (i = 0; i < num_blocks; i++)
blocks[i].type = g_extern.savefiles->elems[i].attr.i;
}
}
2014-04-04 13:35:46 +00:00
for (i = 0; i < num_blocks; i++)
blocks[i].size = pretro_get_memory_size(blocks[i].type);
2014-04-04 13:35:46 +00:00
for (i = 0; i < num_blocks; i++)
if (blocks[i].size)
blocks[i].data = malloc(blocks[i].size);
2014-09-02 03:10:54 +00:00
/* Backup current SRAM which is overwritten by unserialize. */
2014-04-04 13:35:46 +00:00
for (i = 0; i < num_blocks; i++)
{
2014-04-04 13:35:46 +00:00
if (blocks[i].data)
{
2014-04-04 13:35:46 +00:00
const void *ptr = pretro_get_memory_data(blocks[i].type);
if (ptr)
2014-04-04 13:35:46 +00:00
memcpy(blocks[i].data, ptr, blocks[i].size);
}
}
ret = pretro_unserialize(buf, size);
2014-09-02 03:10:54 +00:00
/* Flush back. */
2014-04-04 13:35:46 +00:00
for (i = 0; i < num_blocks; i++)
{
2014-04-04 13:35:46 +00:00
if (blocks[i].data)
{
2014-04-04 13:35:46 +00:00
void *ptr = pretro_get_memory_data(blocks[i].type);
if (ptr)
2014-04-04 13:35:46 +00:00
memcpy(ptr, blocks[i].data, blocks[i].size);
}
}
2014-04-04 13:35:46 +00:00
for (i = 0; i < num_blocks; i++)
free(blocks[i].data);
free(blocks);
return ret;
}
void load_ram_file(const char *path, int type)
{
size_t size = pretro_get_memory_size(type);
void *data = pretro_get_memory_data(type);
if (size == 0 || !data)
return;
void *buf = NULL;
ssize_t rc = read_file(path, &buf);
if (rc > 0)
{
if (rc > (ssize_t)size)
{
2013-10-13 16:53:12 +00:00
RARCH_WARN("SRAM is larger than implementation expects, doing partial load (truncating %u bytes to %u).\n",
(unsigned)rc, (unsigned)size);
rc = size;
}
memcpy(data, buf, rc);
}
free(buf);
}
void save_ram_file(const char *path, int type)
{
size_t size = pretro_get_memory_size(type);
void *data = pretro_get_memory_data(type);
if (data && size > 0)
{
2013-01-21 22:51:56 +00:00
if (!write_file(path, data, size))
{
RARCH_ERR("Failed to save SRAM.\n");
RARCH_WARN("Attempting to recover ...\n");
dump_to_file_desperate(data, size, type);
}
2012-11-16 10:47:39 +00:00
else
RARCH_LOG("Saved successfully to \"%s\".\n", path);
}
}
2014-09-02 03:10:54 +00:00
static bool load_content(const struct retro_subsystem_info *special,
const struct string_list *content)
{
2014-04-04 12:58:42 +00:00
unsigned i;
bool ret = true;
struct string_list* additional_path_allocs = string_list_new();
2014-09-02 03:10:54 +00:00
struct retro_game_info *info = (struct retro_game_info*)
calloc(content->size, sizeof(*info));
2014-04-04 12:58:42 +00:00
if (!info)
return false;
2014-07-28 17:45:00 +00:00
for (i = 0; i < content->size; i++)
2014-04-04 12:58:42 +00:00
{
2014-07-28 17:45:00 +00:00
const char *path = content->elems[i].data;
int attr = content->elems[i].attr.i;
2014-04-04 12:58:42 +00:00
bool need_fullpath = attr & 2;
2014-08-13 06:52:13 +00:00
bool require_content = attr & 4;
2013-01-21 20:01:12 +00:00
2014-08-13 06:52:13 +00:00
if (require_content && !*path)
{
2014-07-28 18:08:37 +00:00
RARCH_LOG("libretro core requires content, but nothing was provided.\n");
2013-01-21 20:01:12 +00:00
ret = false;
goto end;
}
2014-04-04 12:58:42 +00:00
info[i].path = *path ? path : NULL;
2014-09-02 03:10:54 +00:00
if (!need_fullpath && *path)
{
2014-09-02 03:10:54 +00:00
/* Load the content into memory. */
2014-07-28 18:08:37 +00:00
RARCH_LOG("Loading content file: %s.\n", path);
2014-09-02 03:10:54 +00:00
/* First content file is significant, attempt to do patching,
* CRC checking, etc. */
long size = i == 0 ?
read_content_file(path, (void**)&info[i].data) :
read_file(path, (void**)&info[i].data);
2014-04-04 12:58:42 +00:00
if (size < 0)
{
2014-07-28 18:08:37 +00:00
RARCH_ERR("Could not read content file \"%s\".\n", path);
2014-04-04 12:58:42 +00:00
ret = false;
goto end;
}
info[i].size = size;
}
2014-04-04 12:58:42 +00:00
else
{
RARCH_LOG("Content loading skipped. Implementation will"
" load it on its own.\n");
if (need_fullpath && path_contains_compressed_file(path))
{
RARCH_LOG("Compressed file in case of need_fullpath."
"Now extracting to temporary directory.\n");
if ((!strcmp(g_settings.extraction_directory,"")) ||
!path_is_directory(g_settings.extraction_directory))
{
RARCH_ERR("Tried extracting to extraction directory, but "
"extraction directory was not set or found. Exiting.\n");
rarch_assert(false);
}
char new_path[PATH_MAX];
union string_list_elem_attr attr;
attr.i = 0;
fill_pathname_join(new_path,g_settings.extraction_directory,
path_basename(path),sizeof(new_path));
read_compressed_file(path,NULL,new_path);
string_list_append(additional_path_allocs,new_path,attr);
info[i].path =
additional_path_allocs->elems
[additional_path_allocs->size -1 ].data;
/* g_extern.temporary_content is initialized in init_content_file
* The following part takes care of cleanup of the unzipped files
* after exit.
*/
rarch_assert(g_extern.temporary_content != NULL);
string_list_append(g_extern.temporary_content,
new_path, attr);
}
}
}
2014-04-04 12:58:42 +00:00
if (special)
2014-07-28 17:45:00 +00:00
ret = pretro_load_game_special(special->id, info, content->size);
else
2014-07-28 17:45:00 +00:00
ret = pretro_load_game(*content->elems[0].data ? info : NULL);
if (!ret)
RARCH_ERR("Failed to load game.\n");
end:
2014-07-28 17:45:00 +00:00
for (i = 0; i < content->size; i++)
2014-04-04 12:58:42 +00:00
free((void*)info[i].data);
string_list_free(additional_path_allocs);
2014-04-04 12:58:42 +00:00
free(info);
return ret;
}
2014-08-13 06:52:13 +00:00
bool init_content_file(void)
{
2014-04-04 12:58:42 +00:00
unsigned i;
g_extern.temporary_content = string_list_new();
if (!g_extern.temporary_content)
2014-04-04 12:58:42 +00:00
return false;
const struct retro_subsystem_info *special = NULL;
2014-04-04 12:58:42 +00:00
if (*g_extern.subsystem)
{
2014-09-02 03:10:54 +00:00
special = libretro_find_subsystem_info(g_extern.system.special,
g_extern.system.num_special, g_extern.subsystem);
2014-04-04 12:58:42 +00:00
if (!special)
{
2014-09-02 03:10:54 +00:00
RARCH_ERR(
"Failed to find subsystem \"%s\" in libretro implementation.\n",
2014-04-04 12:58:42 +00:00
g_extern.subsystem);
return false;
}
2014-04-04 12:58:42 +00:00
if (special->num_roms && !g_extern.subsystem_fullpaths)
{
2014-07-28 18:01:27 +00:00
RARCH_ERR("libretro core requires special content, but none were provided.\n");
2014-04-04 12:58:42 +00:00
return false;
}
else if (special->num_roms && special->num_roms
!= g_extern.subsystem_fullpaths->size)
2014-04-04 12:58:42 +00:00
{
2014-09-02 03:10:54 +00:00
RARCH_ERR("libretro core requires %u content files for subsystem \"%s\", but %u content files were provided.\n",
special->num_roms, special->desc,
2014-04-04 12:58:42 +00:00
(unsigned)g_extern.subsystem_fullpaths->size);
return false;
}
2014-09-02 03:10:54 +00:00
else if (!special->num_roms && g_extern.subsystem_fullpaths
&& g_extern.subsystem_fullpaths->size)
2014-04-04 12:58:42 +00:00
{
2014-09-02 03:10:54 +00:00
RARCH_ERR("libretro core takes no content for subsystem \"%s\", but %u content files were provided.\n",
special->desc,
2014-04-04 12:58:42 +00:00
(unsigned)g_extern.subsystem_fullpaths->size);
return false;
}
}
2014-04-04 12:58:42 +00:00
union string_list_elem_attr attr;
2014-04-14 10:09:04 +00:00
attr.i = 0;
2014-07-28 17:45:00 +00:00
struct string_list *content = (struct string_list*)string_list_new();
if (!content)
2014-04-04 12:58:42 +00:00
return false;
2014-04-04 12:58:42 +00:00
if (*g_extern.subsystem)
2013-01-21 22:51:56 +00:00
{
2014-04-04 12:58:42 +00:00
for (i = 0; i < g_extern.subsystem_fullpaths->size; i++)
2013-01-21 22:51:56 +00:00
{
2014-04-04 12:58:42 +00:00
attr.i = special->roms[i].block_extract;
attr.i |= special->roms[i].need_fullpath << 1;
attr.i |= special->roms[i].required << 2;
2014-09-02 03:10:54 +00:00
string_list_append(content,
g_extern.subsystem_fullpaths->elems[i].data, attr);
2013-01-21 22:51:56 +00:00
}
}
2014-04-04 12:58:42 +00:00
else
{
attr.i = g_extern.system.info.block_extract;
attr.i |= g_extern.system.info.need_fullpath << 1;
attr.i |= (!g_extern.system.no_content) << 2;
2014-09-02 03:10:54 +00:00
string_list_append(content,
g_extern.libretro_no_content ? "" : g_extern.fullpath, attr);
2014-04-04 12:58:42 +00:00
}
2013-01-21 22:51:56 +00:00
2014-04-04 12:58:42 +00:00
#ifdef HAVE_ZLIB
2014-09-02 03:10:54 +00:00
/* Try to extract all content we're going to load if appropriate. */
2014-07-28 17:45:00 +00:00
for (i = 0; i < content->size; i++)
{
2014-09-02 03:10:54 +00:00
/* Block extract check. */
2014-07-28 17:45:00 +00:00
if (content->elems[i].attr.i & 1)
2014-04-04 12:58:42 +00:00
continue;
2014-07-28 17:45:00 +00:00
const char *ext = path_get_extension(content->elems[i].data);
2014-04-04 12:58:42 +00:00
const char *valid_ext = special ?
special->roms[i].valid_extensions :
g_extern.system.info.valid_extensions;
2014-04-04 12:58:42 +00:00
if (ext && !strcasecmp(ext, "zip"))
{
char temporary_content[PATH_MAX];
2014-09-02 03:10:54 +00:00
strlcpy(temporary_content, content->elems[i].data,
sizeof(temporary_content));
if (!zlib_extract_first_content_file(temporary_content,
sizeof(temporary_content), valid_ext,
*g_settings.extraction_directory ?
g_settings.extraction_directory : NULL))
2014-04-04 12:58:42 +00:00
{
2014-09-02 03:10:54 +00:00
RARCH_ERR("Failed to extract content from zipped file: %s.\n",
temporary_content);
2014-07-28 17:45:00 +00:00
string_list_free(content);
return false;
2014-04-04 12:58:42 +00:00
}
string_list_set(content, i, temporary_content);
2014-09-02 03:10:54 +00:00
string_list_append(g_extern.temporary_content,
temporary_content, attr);
2014-04-04 12:58:42 +00:00
}
}
2014-04-04 12:58:42 +00:00
#endif
2014-09-02 03:10:54 +00:00
/* Set attr to need_fullpath as appropriate. */
2014-04-04 12:58:42 +00:00
2014-07-28 17:45:00 +00:00
bool ret = load_content(special, content);
string_list_free(content);
2014-04-04 12:58:42 +00:00
return ret;
}