2011-01-17 19:54:58 +00:00
|
|
|
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
|
2011-01-23 19:29:28 +00:00
|
|
|
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
|
2010-12-24 00:33:40 +00:00
|
|
|
*
|
|
|
|
* Some code herein may be based on code found in BSNES.
|
|
|
|
*
|
|
|
|
* SSNES 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.
|
|
|
|
*
|
|
|
|
* SSNES 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 SSNES.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-12-24 00:26:36 +00:00
|
|
|
#include "file.h"
|
|
|
|
#include "general.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
2011-11-30 16:32:30 +00:00
|
|
|
#include "libsnes.hpp"
|
2010-12-24 00:26:36 +00:00
|
|
|
#include <string.h>
|
2011-08-22 15:05:27 +00:00
|
|
|
#include <assert.h>
|
2011-08-22 16:45:38 +00:00
|
|
|
#include <time.h>
|
2010-12-30 12:54:49 +00:00
|
|
|
#include "dynamic.h"
|
2011-02-02 11:10:27 +00:00
|
|
|
#include "movie.h"
|
2011-03-23 22:31:33 +00:00
|
|
|
#include "ups.h"
|
2011-08-17 22:05:56 +00:00
|
|
|
#include "bps.h"
|
2011-04-03 20:29:36 +00:00
|
|
|
#include "strl.h"
|
2010-12-24 00:26:36 +00:00
|
|
|
|
2011-04-17 10:29:58 +00:00
|
|
|
#ifdef HAVE_XML
|
|
|
|
#include "sha256.h"
|
|
|
|
#endif
|
|
|
|
|
2011-01-27 21:43:12 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
2011-03-29 21:21:56 +00:00
|
|
|
#include <windows.h>
|
2011-08-22 16:13:30 +00:00
|
|
|
#include <shlwapi.h>
|
2011-03-29 16:04:41 +00:00
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
2011-08-22 15:05:27 +00:00
|
|
|
#include <sys/stat.h>
|
2011-03-29 16:04:41 +00:00
|
|
|
#include <dirent.h>
|
2011-01-27 21:43:12 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-23 22:31:33 +00:00
|
|
|
// Generic file loader.
|
2011-05-18 19:49:23 +00:00
|
|
|
ssize_t read_file(const char *path, void **buf)
|
2011-03-23 22:31:33 +00:00
|
|
|
{
|
|
|
|
void *rom_buf = NULL;
|
|
|
|
FILE *file = fopen(path, "rb");
|
|
|
|
if (!file)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
long len = ftell(file);
|
|
|
|
ssize_t rc = 0;
|
|
|
|
rewind(file);
|
2011-06-24 11:29:30 +00:00
|
|
|
rom_buf = malloc(len + 1);
|
2011-03-23 22:31:33 +00:00
|
|
|
if (!rom_buf)
|
|
|
|
{
|
|
|
|
SSNES_ERR("Couldn't allocate memory!\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rc = fread(rom_buf, 1, len, file)) < len)
|
|
|
|
SSNES_WARN("Didn't read whole file.\n");
|
|
|
|
|
|
|
|
*buf = rom_buf;
|
2011-06-26 15:05:29 +00:00
|
|
|
// Allow for easy reading of strings to be safe.
|
|
|
|
// Will only work with sane character formatting (Unix).
|
|
|
|
((char*)rom_buf)[len] = '\0';
|
2011-03-23 22:31:33 +00:00
|
|
|
fclose(file);
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (file)
|
|
|
|
fclose(file);
|
|
|
|
free(rom_buf);
|
|
|
|
*buf = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-06-26 15:05:29 +00:00
|
|
|
// Reads file content as one string.
|
|
|
|
bool read_file_string(const char *path, char **buf)
|
|
|
|
{
|
|
|
|
*buf = NULL;
|
|
|
|
FILE *file = fopen(path, "r");
|
|
|
|
if (!file)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
long len = ftell(file) + 2; // Takes account of being able to read in EOF and '\0' at end.
|
|
|
|
rewind(file);
|
|
|
|
|
|
|
|
*buf = calloc(len, sizeof(char));
|
|
|
|
if (!*buf)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
char *ptr = *buf;
|
|
|
|
|
|
|
|
while (ptr && !feof(file))
|
|
|
|
{
|
|
|
|
size_t bufsize = (size_t)(((ptrdiff_t)*buf + (ptrdiff_t)len) - (ptrdiff_t)ptr);
|
|
|
|
fgets(ptr, bufsize, file);
|
|
|
|
|
|
|
|
ptr = strchr(ptr, '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = strchr(ptr, EOF);
|
|
|
|
if (ptr) *ptr = '\0';
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (file)
|
|
|
|
fclose(file);
|
|
|
|
if (*buf)
|
|
|
|
free(*buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-17 22:05:56 +00:00
|
|
|
enum patch_type
|
|
|
|
{
|
|
|
|
PATCH_NONE,
|
|
|
|
PATCH_UPS,
|
|
|
|
PATCH_BPS
|
|
|
|
};
|
|
|
|
|
|
|
|
static void patch_rom(uint8_t **buf, ssize_t *size)
|
|
|
|
{
|
|
|
|
uint8_t *ret_buf = *buf;
|
|
|
|
ssize_t ret_size = *size;
|
|
|
|
|
|
|
|
ssize_t patch_size = 0;
|
|
|
|
void *patch_data = NULL;
|
|
|
|
enum patch_type type = PATCH_NONE;
|
|
|
|
|
2011-08-17 22:24:57 +00:00
|
|
|
if (g_extern.ups_pref && g_extern.bps_pref)
|
|
|
|
{
|
|
|
|
SSNES_WARN("Both UPS and BPS patch explicitly defined, ignoring both ...\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_extern.bps_pref && *g_extern.ups_name && (patch_size = read_file(g_extern.ups_name, &patch_data)) >= 0)
|
2011-08-17 22:05:56 +00:00
|
|
|
type = PATCH_UPS;
|
2011-08-17 22:24:57 +00:00
|
|
|
else if (!g_extern.ups_pref && *g_extern.bps_name && (patch_size = read_file(g_extern.bps_name, &patch_data)) >= 0)
|
2011-08-17 22:05:56 +00:00
|
|
|
type = PATCH_BPS;
|
|
|
|
|
|
|
|
if (type == PATCH_NONE)
|
|
|
|
{
|
2011-08-17 22:24:57 +00:00
|
|
|
SSNES_LOG("Did not find a valid ROM patch.\n");
|
2011-08-17 22:05:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case PATCH_UPS:
|
|
|
|
SSNES_LOG("Found UPS file in \"%s\", attempting to patch ...\n", g_extern.ups_name);
|
|
|
|
break;
|
|
|
|
case PATCH_BPS:
|
|
|
|
SSNES_LOG("Found BPS file in \"%s\", attempting to patch ...\n", g_extern.bps_name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return; // Should not happen, but.
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t target_size = ret_size * 4; // Just to be sure ...
|
|
|
|
uint8_t *patched_rom = malloc(target_size);
|
|
|
|
if (!patched_rom)
|
|
|
|
{
|
|
|
|
SSNES_ERR("Failed to allocate memory for patched ROM ...\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case PATCH_UPS:
|
|
|
|
{
|
|
|
|
ups_error_t err = ups_apply_patch(patch_data, patch_size, ret_buf, ret_size, patched_rom, &target_size);
|
|
|
|
if (err == UPS_SUCCESS)
|
|
|
|
{
|
|
|
|
SSNES_LOG("ROM patched successfully (UPS)!\n");
|
|
|
|
success = true;
|
|
|
|
}
|
2011-08-17 22:37:43 +00:00
|
|
|
else
|
|
|
|
SSNES_ERR("Failed to patch UPS: Error #%u\n", (unsigned)err);
|
|
|
|
|
2011-08-17 22:05:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PATCH_BPS:
|
|
|
|
{
|
|
|
|
bps_error_t err = bps_apply_patch(patch_data, patch_size, ret_buf, ret_size, patched_rom, &target_size);
|
|
|
|
if (err == BPS_SUCCESS)
|
|
|
|
{
|
|
|
|
SSNES_LOG("ROM patched successfully (BPS)!\n");
|
|
|
|
success = true;
|
|
|
|
}
|
2011-08-17 22:37:43 +00:00
|
|
|
else
|
|
|
|
SSNES_ERR("Failed to patch BPS: Error #%u\n", (unsigned)err);
|
|
|
|
|
2011-08-17 22:05:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
free(ret_buf);
|
|
|
|
*buf = patched_rom;
|
|
|
|
*size = target_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (patch_data)
|
|
|
|
free(patch_data);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
*buf = ret_buf;
|
|
|
|
*size = ret_size;
|
|
|
|
if (patch_data)
|
|
|
|
free(patch_data);
|
|
|
|
}
|
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
// Load SNES rom only. Applies a hack for headered ROMs.
|
|
|
|
static ssize_t read_rom_file(FILE* file, void** buf)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
2011-03-23 22:31:33 +00:00
|
|
|
ssize_t ret = 0;
|
|
|
|
uint8_t *ret_buf = NULL;
|
|
|
|
|
2010-12-24 00:26:36 +00:00
|
|
|
if (file == NULL) // stdin
|
|
|
|
{
|
2011-01-27 21:43:12 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
setmode(0, O_BINARY);
|
|
|
|
#endif
|
|
|
|
|
2010-12-24 00:26:36 +00:00
|
|
|
SSNES_LOG("Reading ROM from stdin ...\n");
|
|
|
|
size_t buf_size = 0xFFFFF; // Some initial guesstimate.
|
|
|
|
size_t buf_ptr = 0;
|
2011-03-23 22:31:33 +00:00
|
|
|
uint8_t *rom_buf = malloc(buf_size);
|
2010-12-24 00:26:36 +00:00
|
|
|
if (rom_buf == NULL)
|
|
|
|
{
|
|
|
|
SSNES_ERR("Couldn't allocate memory!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-10-15 10:56:48 +00:00
|
|
|
for (;;)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
|
|
|
size_t ret = fread(rom_buf + buf_ptr, 1, buf_size - buf_ptr, stdin);
|
|
|
|
buf_ptr += ret;
|
|
|
|
|
|
|
|
// We've reached the end
|
|
|
|
if (buf_ptr < buf_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
rom_buf = realloc(rom_buf, buf_size * 2);
|
|
|
|
if (rom_buf == NULL)
|
|
|
|
{
|
|
|
|
SSNES_ERR("Couldn't allocate memory!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf_size *= 2;
|
|
|
|
}
|
|
|
|
|
2011-03-23 22:31:33 +00:00
|
|
|
ret_buf = rom_buf;
|
2010-12-24 00:26:36 +00:00
|
|
|
ret = buf_ptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fseek(file, 0, SEEK_END);
|
2011-03-23 22:31:33 +00:00
|
|
|
ret = ftell(file);
|
2010-12-24 00:26:36 +00:00
|
|
|
rewind(file);
|
|
|
|
|
2011-03-23 22:31:33 +00:00
|
|
|
void *rom_buf = malloc(ret);
|
|
|
|
if (rom_buf == NULL)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("Couldn't allocate memory!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-23 22:31:33 +00:00
|
|
|
if (fread(rom_buf, 1, ret, file) < ret)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("Didn't read whole file.\n");
|
|
|
|
free(rom_buf);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-23 22:31:33 +00:00
|
|
|
|
|
|
|
ret_buf = rom_buf;
|
2010-12-24 00:26:36 +00:00
|
|
|
}
|
2011-02-02 11:10:27 +00:00
|
|
|
|
2011-04-07 19:06:31 +00:00
|
|
|
// Remove copier header if present (512 first bytes).
|
2011-03-23 22:31:33 +00:00
|
|
|
if ((ret & 0x7fff) == 512)
|
|
|
|
{
|
|
|
|
memmove(ret_buf, ret_buf + 512, ret - 512);
|
|
|
|
ret -= 512;
|
|
|
|
}
|
2011-01-18 14:34:37 +00:00
|
|
|
|
2011-08-17 22:05:56 +00:00
|
|
|
// Attempt to apply a patch :)
|
|
|
|
patch_rom(&ret_buf, &ret);
|
|
|
|
|
2011-03-23 22:31:33 +00:00
|
|
|
g_extern.cart_crc = crc32_calculate(ret_buf, ret);
|
2011-04-17 10:29:58 +00:00
|
|
|
#ifdef HAVE_XML
|
|
|
|
sha256_hash(g_extern.sha256, ret_buf, ret);
|
|
|
|
SSNES_LOG("SHA256 sum: %s\n", g_extern.sha256);
|
|
|
|
#endif
|
2011-03-23 22:31:33 +00:00
|
|
|
*buf = ret_buf;
|
|
|
|
return ret;
|
2010-12-24 00:26:36 +00:00
|
|
|
}
|
|
|
|
|
2011-03-23 22:31:33 +00:00
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
// Dump stuff to file.
|
2011-01-23 22:09:54 +00:00
|
|
|
static bool dump_to_file(const char *path, const void *data, size_t size)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
2011-01-18 14:34:37 +00:00
|
|
|
FILE *file = fopen(path, "wb");
|
|
|
|
if (!file)
|
2011-01-23 22:09:54 +00:00
|
|
|
return false;
|
2010-12-24 00:26:36 +00:00
|
|
|
else
|
|
|
|
{
|
2011-08-22 15:35:31 +00:00
|
|
|
bool ret = fwrite(data, 1, size, file) == size;
|
2011-01-18 14:34:37 +00:00
|
|
|
fclose(file);
|
2011-08-22 15:35:31 +00:00
|
|
|
return ret;
|
2010-12-24 00:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 16:45:38 +00:00
|
|
|
static const char *ramtype2str(int type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case SNES_MEMORY_CARTRIDGE_RAM:
|
|
|
|
case SNES_MEMORY_GAME_BOY_RAM:
|
|
|
|
case SNES_MEMORY_BSX_RAM:
|
|
|
|
return ".srm";
|
|
|
|
|
|
|
|
case SNES_MEMORY_CARTRIDGE_RTC:
|
|
|
|
case SNES_MEMORY_GAME_BOY_RTC:
|
|
|
|
return ".rtc";
|
|
|
|
|
|
|
|
case SNES_MEMORY_BSX_PRAM:
|
|
|
|
return ".pram";
|
|
|
|
|
|
|
|
case SNES_MEMORY_SUFAMI_TURBO_A_RAM:
|
|
|
|
return ".aram";
|
|
|
|
case SNES_MEMORY_SUFAMI_TURBO_B_RAM:
|
|
|
|
return ".bram";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 15:35:31 +00:00
|
|
|
// Attempt to save valuable RAM data somewhere ...
|
2011-08-22 16:45:38 +00:00
|
|
|
static void dump_to_file_desperate(const void *data, size_t size, int type)
|
2011-08-22 15:35:31 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
const char *base = getenv("APPDATA");
|
|
|
|
#else
|
|
|
|
const char *base = getenv("HOME");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!base)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
char path[MAXPATHLEN];
|
2011-08-22 16:45:38 +00:00
|
|
|
snprintf(path, sizeof(path), "%s/SSNES-recovery-", base);
|
|
|
|
char timebuf[MAXPATHLEN];
|
|
|
|
|
|
|
|
time_t time_;
|
|
|
|
time(&time_);
|
|
|
|
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d-%H-%M-%S", localtime(&time_));
|
|
|
|
strlcat(path, timebuf, sizeof(path));
|
|
|
|
strlcat(path, ramtype2str(type), sizeof(path));
|
2011-08-22 15:35:31 +00:00
|
|
|
|
|
|
|
if (dump_to_file(path, data, size))
|
2011-08-22 16:45:38 +00:00
|
|
|
SSNES_WARN("Succeeded in saving RAM data to \"%s\". Phew! :D\n", path);
|
2011-08-22 15:35:31 +00:00
|
|
|
else
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
SSNES_WARN("Failed ... Tough luck ... :(\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool save_state(const char *path)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
2011-01-18 14:34:37 +00:00
|
|
|
SSNES_LOG("Saving state: \"%s\".\n", path);
|
|
|
|
size_t size = psnes_serialize_size();
|
|
|
|
if (size == 0)
|
2011-01-23 22:09:54 +00:00
|
|
|
return false;
|
2010-12-24 00:26:36 +00:00
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
void *data = malloc(size);
|
|
|
|
if (!data)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
2011-01-18 14:34:37 +00:00
|
|
|
SSNES_ERR("Failed to allocate memory for save state buffer.\n");
|
2011-01-23 22:09:54 +00:00
|
|
|
return false;
|
2010-12-24 00:26:36 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
SSNES_LOG("State size: %d bytes.\n", (int)size);
|
|
|
|
psnes_serialize(data, size);
|
2011-01-23 22:09:54 +00:00
|
|
|
bool ret = dump_to_file(path, data, size);
|
2011-01-18 14:34:37 +00:00
|
|
|
free(data);
|
2011-01-27 22:05:20 +00:00
|
|
|
if (!ret)
|
|
|
|
SSNES_ERR("Failed to save state to \"%s\".\n", path);
|
2011-01-23 22:09:54 +00:00
|
|
|
return ret;
|
2011-01-18 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2011-08-22 15:35:31 +00:00
|
|
|
bool load_state(const char *path)
|
2011-01-18 14:34:37 +00:00
|
|
|
{
|
|
|
|
SSNES_LOG("Loading state: \"%s\".\n", path);
|
|
|
|
void *buf = NULL;
|
|
|
|
ssize_t size = read_file(path, &buf);
|
|
|
|
if (size < 0)
|
2011-01-23 22:09:54 +00:00
|
|
|
{
|
2011-01-27 22:05:20 +00:00
|
|
|
SSNES_ERR("Failed to load state from \"%s\".\n", path);
|
2011-01-23 22:09:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-01-18 14:34:37 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SSNES_LOG("State size: %d bytes.\n", (int)size);
|
2011-09-16 13:32:21 +00:00
|
|
|
|
|
|
|
uint8_t *block_buf[2] = {NULL, NULL};
|
|
|
|
int block_type[2] = {-1, -1};
|
|
|
|
unsigned block_size[2] = {0};
|
|
|
|
|
|
|
|
if (g_settings.block_sram_overwrite)
|
|
|
|
{
|
|
|
|
SSNES_LOG("Blocking SRAM overwrite!\n");
|
|
|
|
switch (g_extern.game_type)
|
|
|
|
{
|
|
|
|
case SSNES_CART_NORMAL:
|
|
|
|
block_type[0] = SNES_MEMORY_CARTRIDGE_RAM;
|
|
|
|
block_type[1] = SNES_MEMORY_CARTRIDGE_RTC;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_CART_BSX:
|
|
|
|
case SSNES_CART_BSX_SLOTTED:
|
|
|
|
block_type[0] = SNES_MEMORY_BSX_RAM;
|
|
|
|
block_type[1] = SNES_MEMORY_BSX_PRAM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_CART_SUFAMI:
|
|
|
|
block_type[0] = SNES_MEMORY_SUFAMI_TURBO_A_RAM;
|
|
|
|
block_type[1] = SNES_MEMORY_SUFAMI_TURBO_B_RAM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_CART_SGB:
|
|
|
|
block_type[0] = SNES_MEMORY_GAME_BOY_RAM;
|
|
|
|
block_type[1] = SNES_MEMORY_GAME_BOY_RTC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 2; i++)
|
|
|
|
if (block_type[i] != -1)
|
|
|
|
block_size[i] = psnes_get_memory_size(block_type[i]);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 2; i++)
|
|
|
|
if (block_size[i])
|
|
|
|
block_buf[i] = malloc(block_size[i]);
|
|
|
|
|
|
|
|
// Backup current SRAM which is overwritten by unserialize.
|
|
|
|
for (unsigned i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if (block_buf[i])
|
|
|
|
{
|
|
|
|
const uint8_t *ptr = psnes_get_memory_data(block_type[i]);
|
|
|
|
if (ptr)
|
|
|
|
memcpy(block_buf[i], ptr, block_size[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
psnes_unserialize(buf, size);
|
2011-09-16 13:32:21 +00:00
|
|
|
|
|
|
|
// Flush back :D
|
|
|
|
for (unsigned i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if (block_buf[i])
|
|
|
|
{
|
|
|
|
uint8_t *ptr = psnes_get_memory_data(block_type[i]);
|
|
|
|
if (ptr)
|
|
|
|
memcpy(ptr, block_buf[i], block_size[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 2; i++)
|
|
|
|
if (block_buf[i])
|
|
|
|
free(block_buf[i]);
|
2011-01-18 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
2011-01-23 22:09:54 +00:00
|
|
|
return true;
|
2011-01-18 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2011-08-22 15:35:31 +00:00
|
|
|
void load_ram_file(const char *path, int type)
|
2011-01-18 14:34:37 +00:00
|
|
|
{
|
2010-12-30 12:54:49 +00:00
|
|
|
size_t size = psnes_get_memory_size(type);
|
|
|
|
uint8_t *data = psnes_get_memory_data(type);
|
2010-12-24 00:26:36 +00:00
|
|
|
|
|
|
|
if (size == 0 || !data)
|
|
|
|
return;
|
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
void *buf = NULL;
|
|
|
|
ssize_t rc = read_file(path, &buf);
|
|
|
|
if (rc <= size)
|
2011-10-30 15:58:45 +00:00
|
|
|
memcpy(data, buf, rc);
|
2010-12-24 00:26:36 +00:00
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
free(buf);
|
2010-12-24 00:26:36 +00:00
|
|
|
}
|
|
|
|
|
2011-08-22 15:35:31 +00:00
|
|
|
void save_ram_file(const char *path, int type)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
2010-12-30 12:54:49 +00:00
|
|
|
size_t size = psnes_get_memory_size(type);
|
|
|
|
uint8_t *data = psnes_get_memory_data(type);
|
2010-12-24 00:26:36 +00:00
|
|
|
|
2011-04-07 19:06:31 +00:00
|
|
|
if (data && size > 0)
|
2011-08-22 15:35:31 +00:00
|
|
|
{
|
|
|
|
if (!dump_to_file(path, data, size))
|
|
|
|
{
|
|
|
|
SSNES_ERR("Failed to save SNES RAM!\n");
|
|
|
|
SSNES_WARN("Attempting to recover ...\n");
|
2011-08-22 16:45:38 +00:00
|
|
|
dump_to_file_desperate(data, size, type);
|
2011-08-22 15:35:31 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-24 00:26:36 +00:00
|
|
|
}
|
2011-01-12 17:05:57 +00:00
|
|
|
|
2011-11-02 18:31:36 +00:00
|
|
|
static char *load_xml_map(const char *path)
|
2011-06-24 11:29:30 +00:00
|
|
|
{
|
2011-06-26 15:05:29 +00:00
|
|
|
char *xml_buf = NULL;
|
2011-06-24 11:29:30 +00:00
|
|
|
if (*path)
|
|
|
|
{
|
2011-06-26 15:05:29 +00:00
|
|
|
if (!read_file_string(path, &xml_buf))
|
2011-06-24 11:29:30 +00:00
|
|
|
SSNES_LOG("Did not find XML memory map in \"%s\"\n", path);
|
|
|
|
else
|
|
|
|
SSNES_LOG("Found XML memory map in \"%s\"\n", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return xml_buf;
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:05:57 +00:00
|
|
|
static bool load_sgb_rom(void)
|
|
|
|
{
|
2011-01-12 20:57:55 +00:00
|
|
|
void *rom_buf = NULL;
|
2011-01-12 17:05:57 +00:00
|
|
|
ssize_t rom_len = 0;
|
|
|
|
|
2011-01-12 20:57:55 +00:00
|
|
|
FILE *extra_rom = NULL;
|
|
|
|
void *extra_rom_buf = NULL;
|
2011-01-12 17:05:57 +00:00
|
|
|
ssize_t extra_rom_len = 0;
|
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
if ((rom_len = read_rom_file(g_extern.rom_file, &rom_buf)) == -1)
|
2011-01-12 17:05:57 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("Could not read ROM file.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:06:31 +00:00
|
|
|
if ((extra_rom_len = read_file(g_extern.gb_rom_path, &extra_rom_buf)) == -1)
|
2011-01-12 17:05:57 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("Cannot read GameBoy rom.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-06-24 11:29:30 +00:00
|
|
|
char *xml_buf = load_xml_map(g_extern.xml_name);
|
|
|
|
|
2011-01-12 17:05:57 +00:00
|
|
|
if (!psnes_load_cartridge_super_game_boy(
|
2011-06-24 11:29:30 +00:00
|
|
|
xml_buf, rom_buf, rom_len,
|
2011-01-12 17:05:57 +00:00
|
|
|
NULL, extra_rom_buf, extra_rom_len))
|
|
|
|
{
|
|
|
|
SSNES_ERR("Cannot load SGB/GameBoy rom.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-06-24 11:29:30 +00:00
|
|
|
if (xml_buf)
|
|
|
|
free(xml_buf);
|
|
|
|
|
2011-01-12 17:05:57 +00:00
|
|
|
if (g_extern.rom_file)
|
|
|
|
fclose(g_extern.rom_file);
|
|
|
|
if (extra_rom)
|
|
|
|
fclose(extra_rom);
|
|
|
|
free(rom_buf);
|
|
|
|
free(extra_rom_buf);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (g_extern.rom_file)
|
|
|
|
fclose(g_extern.rom_file);
|
|
|
|
if (extra_rom)
|
|
|
|
fclose(extra_rom);
|
|
|
|
free(rom_buf);
|
|
|
|
free(extra_rom_buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-12 20:57:55 +00:00
|
|
|
static bool load_bsx_rom(bool slotted)
|
|
|
|
{
|
|
|
|
void *rom_buf = NULL;
|
|
|
|
ssize_t rom_len = 0;
|
|
|
|
|
|
|
|
FILE *extra_rom = NULL;
|
|
|
|
void *extra_rom_buf = NULL;
|
|
|
|
ssize_t extra_rom_len = 0;
|
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
if ((rom_len = read_rom_file(g_extern.rom_file, &rom_buf)) == -1)
|
2011-01-12 20:57:55 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("Could not read ROM file.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:06:31 +00:00
|
|
|
if ((extra_rom_len = read_file(g_extern.bsx_rom_path, &extra_rom_buf)) == -1)
|
2011-01-12 20:57:55 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("Cannot read BSX game rom.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-06-24 11:29:30 +00:00
|
|
|
char *xml_buf = load_xml_map(g_extern.xml_name);
|
|
|
|
|
2011-01-12 20:57:55 +00:00
|
|
|
if (slotted)
|
|
|
|
{
|
|
|
|
if (!psnes_load_cartridge_bsx_slotted(
|
2011-06-24 11:29:30 +00:00
|
|
|
xml_buf, rom_buf, rom_len,
|
2011-01-29 18:53:23 +00:00
|
|
|
NULL, extra_rom_buf, extra_rom_len))
|
|
|
|
{
|
|
|
|
SSNES_ERR("Cannot load BSX slotted rom.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2011-01-12 20:57:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!psnes_load_cartridge_bsx(
|
|
|
|
NULL, rom_buf, rom_len,
|
|
|
|
NULL, extra_rom_buf, extra_rom_len))
|
|
|
|
{
|
|
|
|
SSNES_ERR("Cannot load BSX rom.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-24 11:29:30 +00:00
|
|
|
if (xml_buf)
|
|
|
|
free(xml_buf);
|
|
|
|
|
2011-01-12 20:57:55 +00:00
|
|
|
if (g_extern.rom_file)
|
|
|
|
fclose(g_extern.rom_file);
|
|
|
|
if (extra_rom)
|
|
|
|
fclose(extra_rom);
|
|
|
|
free(rom_buf);
|
|
|
|
free(extra_rom_buf);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (g_extern.rom_file)
|
|
|
|
fclose(g_extern.rom_file);
|
|
|
|
if (extra_rom)
|
|
|
|
fclose(extra_rom);
|
|
|
|
free(rom_buf);
|
|
|
|
free(extra_rom_buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool load_sufami_rom(void)
|
|
|
|
{
|
|
|
|
void *rom_buf = NULL;
|
|
|
|
ssize_t rom_len = 0;
|
|
|
|
|
|
|
|
FILE *extra_rom[2] = {NULL};
|
|
|
|
void *extra_rom_buf[2] = {NULL};
|
|
|
|
ssize_t extra_rom_len[2] = {0};
|
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
if ((rom_len = read_rom_file(g_extern.rom_file, &rom_buf)) == -1)
|
2011-01-12 20:57:55 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("Could not read ROM file.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *roms[2] = { g_extern.sufami_rom_path[0], g_extern.sufami_rom_path[1] };
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if (strlen(roms[i]) > 0)
|
|
|
|
{
|
2011-04-07 19:06:31 +00:00
|
|
|
if ((extra_rom_len[i] = read_file(roms[i], &extra_rom_buf[i])) == -1)
|
2011-01-12 20:57:55 +00:00
|
|
|
{
|
2011-04-07 19:06:31 +00:00
|
|
|
SSNES_ERR("Cannot read Sufami game rom.\n");
|
2011-01-12 20:57:55 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-24 11:29:30 +00:00
|
|
|
char *xml_buf = load_xml_map(g_extern.xml_name);
|
|
|
|
|
2011-01-12 20:57:55 +00:00
|
|
|
if (!psnes_load_cartridge_sufami_turbo(
|
2011-06-24 11:29:30 +00:00
|
|
|
xml_buf, rom_buf, rom_len,
|
2011-01-12 20:57:55 +00:00
|
|
|
NULL, extra_rom_buf[0], extra_rom_len[0],
|
|
|
|
NULL, extra_rom_buf[1], extra_rom_len[1]))
|
|
|
|
{
|
|
|
|
SSNES_ERR("Cannot load Sufami Turbo rom.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-06-24 11:29:30 +00:00
|
|
|
if (xml_buf)
|
|
|
|
free(xml_buf);
|
2011-01-12 20:57:55 +00:00
|
|
|
|
|
|
|
if (g_extern.rom_file)
|
|
|
|
fclose(g_extern.rom_file);
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if (extra_rom[i])
|
|
|
|
fclose(extra_rom[i]);
|
|
|
|
free(extra_rom_buf[i]);
|
|
|
|
}
|
|
|
|
free(rom_buf);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (g_extern.rom_file)
|
|
|
|
fclose(g_extern.rom_file);
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if (extra_rom[i])
|
|
|
|
fclose(extra_rom[i]);
|
|
|
|
free(extra_rom_buf[i]);
|
|
|
|
}
|
|
|
|
free(rom_buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:05:57 +00:00
|
|
|
static bool load_normal_rom(void)
|
|
|
|
{
|
2011-01-12 20:57:55 +00:00
|
|
|
void *rom_buf = NULL;
|
2011-01-12 17:05:57 +00:00
|
|
|
ssize_t rom_len = 0;
|
|
|
|
|
2011-01-18 14:34:37 +00:00
|
|
|
if ((rom_len = read_rom_file(g_extern.rom_file, &rom_buf)) == -1)
|
2011-01-12 17:05:57 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("Could not read ROM file.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_extern.rom_file != NULL)
|
|
|
|
fclose(g_extern.rom_file);
|
|
|
|
|
|
|
|
SSNES_LOG("ROM size: %d bytes\n", (int)rom_len);
|
|
|
|
|
2011-06-24 11:29:30 +00:00
|
|
|
char *xml_buf = load_xml_map(g_extern.xml_name);
|
|
|
|
|
|
|
|
if (!psnes_load_cartridge_normal(xml_buf, rom_buf, rom_len))
|
2011-01-12 17:05:57 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("ROM file is not valid!\n");
|
|
|
|
free(rom_buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-24 11:29:30 +00:00
|
|
|
if (xml_buf)
|
|
|
|
free(xml_buf);
|
|
|
|
|
2011-01-12 17:05:57 +00:00
|
|
|
free(rom_buf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-12 20:57:55 +00:00
|
|
|
|
|
|
|
bool init_rom_file(enum ssnes_game_type type)
|
2011-01-12 17:05:57 +00:00
|
|
|
{
|
2011-01-12 20:57:55 +00:00
|
|
|
switch (type)
|
2011-01-12 17:05:57 +00:00
|
|
|
{
|
|
|
|
case SSNES_CART_SGB:
|
|
|
|
if (!load_sgb_rom())
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_CART_NORMAL:
|
|
|
|
if (!load_normal_rom())
|
|
|
|
return false;
|
|
|
|
break;
|
2011-01-12 20:57:55 +00:00
|
|
|
|
|
|
|
case SSNES_CART_BSX:
|
|
|
|
if (!load_bsx_rom(false))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_CART_BSX_SLOTTED:
|
|
|
|
if (!load_bsx_rom(true))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_CART_SUFAMI:
|
|
|
|
if (!load_sufami_rom())
|
|
|
|
return false;
|
|
|
|
break;
|
2011-01-12 17:05:57 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
SSNES_ERR("Invalid ROM type!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-29 21:21:56 +00:00
|
|
|
// Yep, this is C alright ;)
|
2011-11-03 23:07:53 +00:00
|
|
|
char **dir_list_new(const char *dir, const char *ext)
|
2011-03-29 16:04:41 +00:00
|
|
|
{
|
|
|
|
size_t cur_ptr = 0;
|
|
|
|
size_t cur_size = 32;
|
|
|
|
char **dir_list = NULL;
|
2011-03-29 21:21:56 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WIN32_FIND_DATAW ffd;
|
|
|
|
HANDLE hFind = INVALID_HANDLE_VALUE;
|
|
|
|
|
2011-08-22 16:13:30 +00:00
|
|
|
wchar_t wchar_buf[MAXPATHLEN];
|
|
|
|
char utf8_buf[MAXPATHLEN];
|
2011-03-29 21:21:56 +00:00
|
|
|
|
2011-08-22 16:13:30 +00:00
|
|
|
if (strlcpy(utf8_buf, dir, sizeof(utf8_buf)) >= sizeof(utf8_buf))
|
|
|
|
goto error;
|
|
|
|
if (strlcat(utf8_buf, "/*", sizeof(utf8_buf)) >= sizeof(utf8_buf))
|
|
|
|
goto error;
|
2011-09-27 13:31:25 +00:00
|
|
|
|
|
|
|
if (ext)
|
|
|
|
{
|
|
|
|
if (strlcat(utf8_buf, ext, sizeof(utf8_buf)) >= sizeof(utf8_buf))
|
|
|
|
goto error;
|
|
|
|
}
|
2011-03-29 21:21:56 +00:00
|
|
|
|
2011-08-22 16:13:30 +00:00
|
|
|
if (MultiByteToWideChar(CP_UTF8, 0, utf8_buf, -1, wchar_buf, MAXPATHLEN) == 0)
|
|
|
|
goto error;
|
2011-03-29 21:21:56 +00:00
|
|
|
|
|
|
|
hFind = FindFirstFileW(wchar_buf, &ffd);
|
|
|
|
if (hFind == INVALID_HANDLE_VALUE)
|
|
|
|
goto error;
|
|
|
|
#else
|
2011-03-29 16:04:41 +00:00
|
|
|
DIR *directory = NULL;
|
|
|
|
const struct dirent *entry = NULL;
|
2011-03-29 21:21:56 +00:00
|
|
|
|
2011-03-29 16:04:41 +00:00
|
|
|
directory = opendir(dir);
|
|
|
|
if (!directory)
|
|
|
|
goto error;
|
2011-03-29 21:21:56 +00:00
|
|
|
#endif
|
2011-03-29 16:04:41 +00:00
|
|
|
|
|
|
|
dir_list = calloc(cur_size, sizeof(char*));
|
|
|
|
if (!dir_list)
|
|
|
|
goto error;
|
|
|
|
|
2011-03-29 21:21:56 +00:00
|
|
|
#ifdef _WIN32 // Hard to read? Blame non-POSIX heathens!
|
|
|
|
do
|
|
|
|
#else
|
2011-03-29 16:04:41 +00:00
|
|
|
while ((entry = readdir(directory)))
|
2011-03-29 21:21:56 +00:00
|
|
|
#endif
|
2011-03-29 16:04:41 +00:00
|
|
|
{
|
|
|
|
// Not a perfect search of course, but hopefully good enough in practice.
|
2011-03-29 21:21:56 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
continue;
|
2011-09-20 10:05:21 +00:00
|
|
|
if (WideCharToMultiByte(CP_UTF8, 0, ffd.cFileName, -1, utf8_buf, MAXPATHLEN, NULL, NULL) == 0)
|
2011-08-22 16:13:30 +00:00
|
|
|
continue;
|
2011-03-29 21:21:56 +00:00
|
|
|
if (ext && !strstr(utf8_buf, ext))
|
|
|
|
continue;
|
|
|
|
#else
|
2011-03-29 16:04:41 +00:00
|
|
|
if (ext && !strstr(entry->d_name, ext))
|
|
|
|
continue;
|
2011-03-29 21:21:56 +00:00
|
|
|
#endif
|
2011-03-29 16:04:41 +00:00
|
|
|
|
2011-08-22 16:13:30 +00:00
|
|
|
dir_list[cur_ptr] = malloc(MAXPATHLEN);
|
2011-03-29 16:04:41 +00:00
|
|
|
if (!dir_list[cur_ptr])
|
|
|
|
goto error;
|
|
|
|
|
2011-08-22 16:13:30 +00:00
|
|
|
strlcpy(dir_list[cur_ptr], dir, MAXPATHLEN);
|
|
|
|
strlcat(dir_list[cur_ptr], "/", MAXPATHLEN);
|
2011-06-21 00:33:59 +00:00
|
|
|
#ifdef _WIN32
|
2011-08-22 16:13:30 +00:00
|
|
|
strlcat(dir_list[cur_ptr], utf8_buf, MAXPATHLEN);
|
2011-06-21 00:33:59 +00:00
|
|
|
#else
|
2011-08-22 16:13:30 +00:00
|
|
|
strlcat(dir_list[cur_ptr], entry->d_name, MAXPATHLEN);
|
2011-06-21 00:33:59 +00:00
|
|
|
#endif
|
2011-03-29 16:04:41 +00:00
|
|
|
|
|
|
|
cur_ptr++;
|
|
|
|
if (cur_ptr + 1 == cur_size) // Need to reserve for NULL.
|
|
|
|
{
|
|
|
|
cur_size *= 2;
|
|
|
|
dir_list = realloc(dir_list, cur_size * sizeof(char*));
|
|
|
|
if (!dir_list)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
// Make sure it's all NULL'd out since we cannot rely on realloc to do this.
|
|
|
|
memset(dir_list + cur_ptr, 0, (cur_size - cur_ptr) * sizeof(char*));
|
|
|
|
}
|
|
|
|
}
|
2011-03-29 21:21:56 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
while (FindNextFileW(hFind, &ffd) != 0);
|
|
|
|
#endif
|
2011-03-29 16:04:41 +00:00
|
|
|
|
2011-03-29 21:21:56 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
FindClose(hFind);
|
|
|
|
#else
|
2011-03-29 16:04:41 +00:00
|
|
|
closedir(directory);
|
2011-03-29 21:21:56 +00:00
|
|
|
#endif
|
2011-03-29 16:04:41 +00:00
|
|
|
return dir_list;
|
|
|
|
|
|
|
|
error:
|
2011-03-29 21:21:56 +00:00
|
|
|
SSNES_ERR("Failed to open directory: \"%s\"\n", dir);
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE)
|
|
|
|
FindClose(hFind);
|
|
|
|
#else
|
2011-03-29 16:04:41 +00:00
|
|
|
if (directory)
|
|
|
|
closedir(directory);
|
2011-03-29 21:21:56 +00:00
|
|
|
#endif
|
2011-03-29 16:04:41 +00:00
|
|
|
dir_list_free(dir_list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dir_list_free(char **dir_list)
|
|
|
|
{
|
|
|
|
if (!dir_list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char **orig = dir_list;
|
|
|
|
while (*dir_list)
|
|
|
|
free(*dir_list++);
|
|
|
|
free(orig);
|
|
|
|
}
|
2011-08-22 15:05:27 +00:00
|
|
|
|
|
|
|
bool path_is_directory(const char *path)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2011-08-22 16:13:30 +00:00
|
|
|
wchar_t buf[MAXPATHLEN];
|
|
|
|
if (MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, MAXPATHLEN) == 0)
|
|
|
|
return false;
|
|
|
|
return PathIsDirectoryW(buf) == FILE_ATTRIBUTE_DIRECTORY;
|
2011-08-22 15:05:27 +00:00
|
|
|
#else
|
|
|
|
struct stat buf;
|
|
|
|
if (stat(path, &buf) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return S_ISDIR(buf.st_mode);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-08-24 06:14:03 +00:00
|
|
|
bool path_file_exists(const char *path)
|
|
|
|
{
|
|
|
|
FILE *dummy = fopen(path, "rb");
|
|
|
|
if (dummy)
|
|
|
|
{
|
|
|
|
fclose(dummy);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-22 15:05:27 +00:00
|
|
|
void fill_pathname(char *out_path, const char *in_path, const char *replace, size_t size)
|
|
|
|
{
|
2011-09-20 09:50:13 +00:00
|
|
|
char tmp_path[MAXPATHLEN];
|
|
|
|
assert(strlcpy(tmp_path, in_path, sizeof(tmp_path)) < sizeof(tmp_path));
|
2011-08-22 15:05:27 +00:00
|
|
|
char *tok = strrchr(tmp_path, '.');
|
|
|
|
if (tok != NULL)
|
|
|
|
*tok = '\0';
|
|
|
|
assert(strlcpy(out_path, tmp_path, size) < size);
|
|
|
|
assert(strlcat(out_path, replace, size) < size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fill_pathname_noext(char *out_path, const char *in_path, const char *replace, size_t size)
|
|
|
|
{
|
|
|
|
assert(strlcpy(out_path, in_path, size) < size);
|
|
|
|
assert(strlcat(out_path, replace, size) < size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fill_pathname_dir(char *in_dir, const char *in_basename, const char *replace, size_t size)
|
|
|
|
{
|
|
|
|
assert(strlcat(in_dir, "/", size) < size);
|
|
|
|
|
|
|
|
const char *base = strrchr(in_basename, '/');
|
|
|
|
if (!base)
|
|
|
|
base = strrchr(in_basename, '\\');
|
|
|
|
|
|
|
|
if (base)
|
|
|
|
base++;
|
|
|
|
else
|
|
|
|
base = in_basename;
|
|
|
|
|
|
|
|
assert(strlcat(in_dir, base, size) < size);
|
|
|
|
assert(strlcat(in_dir, replace, size) < size);
|
|
|
|
}
|