Allow configurable save files/states.

This commit is contained in:
Themaister 2011-08-24 15:47:39 +02:00
parent 0730a34a5b
commit 0694d8884f
3 changed files with 121 additions and 74 deletions

View File

@ -20,6 +20,7 @@
#include <assert.h> #include <assert.h>
#include "strl.h" #include "strl.h"
#include "config.def.h" #include "config.def.h"
#include "file.h"
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include "config.h"
@ -390,7 +391,13 @@ static void parse_config_file(void)
CONFIG_GET_STRING(audio.dsp_plugin, "audio_dsp_plugin"); CONFIG_GET_STRING(audio.dsp_plugin, "audio_dsp_plugin");
CONFIG_GET_STRING(input.driver, "input_driver"); CONFIG_GET_STRING(input.driver, "input_driver");
CONFIG_GET_STRING(libsnes, "libsnes_path"); CONFIG_GET_STRING(libsnes, "libsnes_path");
CONFIG_GET_STRING(screenshot_directory, "screenshot_directory"); CONFIG_GET_STRING(screenshot_directory, "screenshot_directory");
if (*g_settings.screenshot_directory && !path_is_directory(g_settings.screenshot_directory))
{
SSNES_WARN("screenshot_directory is not an existing directory, ignoring ...\n");
*g_settings.screenshot_directory = '\0';
}
CONFIG_GET_BOOL(rewind_enable, "rewind_enable"); CONFIG_GET_BOOL(rewind_enable, "rewind_enable");
@ -404,6 +411,32 @@ static void parse_config_file(void)
CONFIG_GET_STRING(cheat_database, "cheat_database_path"); CONFIG_GET_STRING(cheat_database, "cheat_database_path");
char *dir_path;
if (!g_extern.has_set_save_path && config_get_string(conf, "savefile_directory", &dir_path))
{
if (path_is_directory(dir_path))
{
strlcpy(g_extern.savefile_name_srm, dir_path, sizeof(g_extern.savefile_name_srm));
fill_pathname_dir(g_extern.savefile_name_srm, g_extern.basename, ".srm", sizeof(g_extern.savefile_name_srm));
}
else
SSNES_WARN("savefile_directory is not a directory, ignoring ...!\n");
free(dir_path);
}
if (!g_extern.has_set_state_path && config_get_string(conf, "savestate_directory", &dir_path))
{
if (path_is_directory(dir_path))
{
strlcpy(g_extern.savestate_name, dir_path, sizeof(g_extern.savestate_name));
fill_pathname_dir(g_extern.savestate_name, g_extern.basename, ".state", sizeof(g_extern.savestate_name));
}
else
SSNES_WARN("savestate_directory is not a directory, ignoring ...\n");
free(dir_path);
}
read_keybinds(conf); read_keybinds(conf);
config_file_free(conf); config_file_free(conf);

154
ssnes.c
View File

@ -426,6 +426,80 @@ static void set_basename(const char *path)
*dst = '\0'; *dst = '\0';
} }
static void set_paths(const char *path)
{
set_basename(path);
SSNES_LOG("Opening file: \"%s\"\n", path);
g_extern.rom_file = fopen(path, "rb");
if (g_extern.rom_file == NULL)
{
SSNES_ERR("Could not open file: \"%s\"\n", path);
exit(1);
}
if (!g_extern.has_set_save_path)
fill_pathname_noext(g_extern.savefile_name_srm, g_extern.basename, ".srm", sizeof(g_extern.savefile_name_srm));
if (!g_extern.has_set_state_path)
fill_pathname_noext(g_extern.savestate_name, g_extern.basename, ".state", sizeof(g_extern.savestate_name));
if (path_is_directory(g_extern.savefile_name_srm))
{
fill_pathname_dir(g_extern.savefile_name_srm, g_extern.basename, ".srm", sizeof(g_extern.savefile_name_srm));
SSNES_LOG("Redirecting save file to \"%s\".\n", g_extern.savefile_name_srm);
}
if (path_is_directory(g_extern.savestate_name))
{
fill_pathname_dir(g_extern.savestate_name, g_extern.basename, ".state", sizeof(g_extern.savestate_name));
SSNES_LOG("Redirecting save state to \"%s\".\n", g_extern.savestate_name);
}
if (*g_extern.config_path && path_is_directory(g_extern.config_path))
{
fill_pathname_dir(g_extern.config_path, g_extern.basename, ".cfg", sizeof(g_extern.config_path));
SSNES_LOG("Redirecting config file to \"%s\".\n", g_extern.config_path);
if (!path_file_exists(g_extern.config_path))
{
*g_extern.config_path = '\0';
SSNES_LOG("Did not find config file. Using system default.\n");
}
}
}
static void verify_stdin_paths(void)
{
if (strlen(g_extern.savefile_name_srm) == 0)
{
SSNES_ERR("Need savefile path argument (--save) when reading rom from stdin.\n");
print_help();
exit(1);
}
else if (strlen(g_extern.savestate_name) == 0)
{
SSNES_ERR("Need savestate path argument (--savestate) when reading rom from stdin.\n");
print_help();
exit(1);
}
if (path_is_directory(g_extern.savefile_name_srm))
{
SSNES_ERR("Cannot specify directory for path argument (--save) when reading from stdin.\n");
print_help();
exit(1);
}
else if (path_is_directory(g_extern.savestate_name))
{
SSNES_ERR("Cannot specify directory for path argument (--savestate) when reading from stdin.\n");
print_help();
exit(1);
}
else if (path_is_directory(g_extern.config_path))
{
SSNES_ERR("Cannot specify directory for config file (--config) when reading from stdin.\n");
print_help();
exit(1);
}
}
static void parse_input(int argc, char *argv[]) static void parse_input(int argc, char *argv[])
{ {
if (argc < 2) if (argc < 2)
@ -634,7 +708,7 @@ static void parse_input(int argc, char *argv[])
switch (val) switch (val)
{ {
case 'p': case 'p':
g_extern.netplay_port = strtol(optarg, NULL, 0); g_extern.netplay_port = strtoul(optarg, NULL, 0);
break; break;
case 'B': case 'B':
strlcpy(g_extern.bps_name, optarg, sizeof(g_extern.bps_name)); strlcpy(g_extern.bps_name, optarg, sizeof(g_extern.bps_name));
@ -656,77 +730,9 @@ static void parse_input(int argc, char *argv[])
} }
if (optind < argc) if (optind < argc)
{ set_paths(argv[optind]);
set_basename(argv[optind]); else
verify_stdin_paths();
SSNES_LOG("Opening file: \"%s\"\n", argv[optind]);
g_extern.rom_file = fopen(argv[optind], "rb");
if (g_extern.rom_file == NULL)
{
SSNES_ERR("Could not open file: \"%s\"\n", argv[optind]);
exit(1);
}
if (!g_extern.has_set_save_path)
fill_pathname_noext(g_extern.savefile_name_srm, g_extern.basename, ".srm", sizeof(g_extern.savefile_name_srm));
if (!g_extern.has_set_state_path)
fill_pathname_noext(g_extern.savestate_name, g_extern.basename, ".state", sizeof(g_extern.savestate_name));
if (path_is_directory(g_extern.savefile_name_srm))
{
fill_pathname_dir(g_extern.savefile_name_srm, g_extern.basename, ".srm", sizeof(g_extern.savefile_name_srm));
SSNES_LOG("Redirecting save file to \"%s\".\n", g_extern.savefile_name_srm);
}
if (path_is_directory(g_extern.savestate_name))
{
fill_pathname_dir(g_extern.savestate_name, g_extern.basename, ".state", sizeof(g_extern.savestate_name));
SSNES_LOG("Redirecting save state to \"%s\".\n", g_extern.savestate_name);
}
if (*g_extern.config_path && path_is_directory(g_extern.config_path))
{
fill_pathname_dir(g_extern.config_path, g_extern.basename, ".cfg", sizeof(g_extern.config_path));
SSNES_LOG("Redirecting config file to \"%s\".\n", g_extern.config_path);
if (!path_file_exists(g_extern.config_path))
{
*g_extern.config_path = '\0';
SSNES_LOG("Did not find config file. Using system default.\n");
}
}
}
else // Read ROM from stdin.
{
if (strlen(g_extern.savefile_name_srm) == 0)
{
SSNES_ERR("Need savefile path argument (--save) when reading rom from stdin.\n");
print_help();
exit(1);
}
else if (strlen(g_extern.savestate_name) == 0)
{
SSNES_ERR("Need savestate path argument (--savestate) when reading rom from stdin.\n");
print_help();
exit(1);
}
if (path_is_directory(g_extern.savefile_name_srm))
{
SSNES_ERR("Cannot specify directory for path argument (--save) when reading from stdin.\n");
print_help();
exit(1);
}
else if (path_is_directory(g_extern.savestate_name))
{
SSNES_ERR("Cannot specify directory for path argument (--savestate) when reading from stdin.\n");
print_help();
exit(1);
}
else if (path_is_directory(g_extern.config_path))
{
SSNES_ERR("Cannot specify directory for config file (--config) when reading from stdin.\n");
print_help();
exit(1);
}
}
} }
// TODO: Add rest of the controllers. // TODO: Add rest of the controllers.
@ -1127,7 +1133,7 @@ static void check_savestates(void)
bool should_savestate = driver.input->key_pressed(driver.input_data, SSNES_SAVE_STATE_KEY); bool should_savestate = driver.input->key_pressed(driver.input_data, SSNES_SAVE_STATE_KEY);
if (should_savestate && !old_should_savestate) if (should_savestate && !old_should_savestate)
{ {
char save_path[strlen(g_extern.savestate_name) * 2]; char save_path[MAXPATHLEN];
if (g_extern.state_slot > 0) if (g_extern.state_slot > 0)
snprintf(save_path, sizeof(save_path), "%s%u", g_extern.savestate_name, g_extern.state_slot); snprintf(save_path, sizeof(save_path), "%s%u", g_extern.savestate_name, g_extern.state_slot);
@ -1153,7 +1159,7 @@ static void check_savestates(void)
bool should_loadstate = driver.input->key_pressed(driver.input_data, SSNES_LOAD_STATE_KEY); bool should_loadstate = driver.input->key_pressed(driver.input_data, SSNES_LOAD_STATE_KEY);
if (!should_savestate && should_loadstate && !old_should_loadstate) if (!should_savestate && should_loadstate && !old_should_loadstate)
{ {
char load_path[strlen(g_extern.savestate_name) * 2]; char load_path[MAXPATHLEN];
if (g_extern.state_slot > 0) if (g_extern.state_slot > 0)
snprintf(load_path, sizeof(load_path), "%s%u", g_extern.savestate_name, g_extern.state_slot); snprintf(load_path, sizeof(load_path), "%s%u", g_extern.savestate_name, g_extern.state_slot);

View File

@ -1,5 +1,13 @@
##### Config file for SSNES ##### Config file for SSNES
# Save all save files (*.srm) to this directory. This includes related files like .bsv, .rtc, .psrm, etc ...
# This will be overridden by explicit command line options.
# savefile_directory =
# Save all save states (*.state) to this directory.
# This will be overridden by explicit command line options.
# savestate_directory =
## If enabled, load libsnes from a dynamic location. ## If enabled, load libsnes from a dynamic location.
# libsnes_path = "/path/to/libsnes.so" # libsnes_path = "/path/to/libsnes.so"