From b5b5fdd8a2e3614ff152915eafd11408cd8f43b7 Mon Sep 17 00:00:00 2001 From: Nunuhara Cabbage Date: Sat, 5 Sep 2020 09:56:04 -0700 Subject: [PATCH] Allow running with no arguments xsystem4 can now be run from within a game directory with no arguments. Alternatively, the game directory can be passed instead of the path to System40.ini. --- README.md | 11 +++++--- src/system4.c | 63 +++++++++++++++++++++++++++++---------------- subprojects/libsys4 | 2 +- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index ff967d5..1592db8 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,12 @@ Finally install it to your system (optional), Running ------- -You can run a game by passing the path to its "System40.ini" to the xsystem4 -executable. +You can run a game by passing the path to its game directory to the xsystem4 +executable, - build/src/xsystem4 /path/to/System40.ini + build/src/xsystem4 /path/to/game_directory + +Alternatively, run xsystem4 from within the game directory, + + cd /path/to/game_directory + xsystem4 diff --git a/src/system4.c b/src/system4.c index 0e7caa9..213f8b7 100644 --- a/src/system4.c +++ b/src/system4.c @@ -27,6 +27,7 @@ #include "system4.h" #include "system4/ain.h" #include "system4/ald.h" +#include "system4/file.h" #include "system4/ini.h" #include "system4/instructions.h" #include "system4/string.h" @@ -294,6 +295,34 @@ static void config_init(void) mkdir_p(new_save_dir); } +static void config_init_with_ini(const char *ini_path) +{ + read_config(ini_path); + if (!config.ain_filename) + ERROR("No AIN filename specified in %s", ini_path); + char *tmp = strdup(ini_path); + config.game_dir = strdup(dirname(tmp)); + free(tmp); + config_init(); +} + +static void config_init_with_dir(const char *dir) +{ + char path[PATH_MAX]; + snprintf(path, PATH_MAX, "%s/System40.ini", dir); + config_init_with_ini(path); +} + +static void config_init_with_ain(const char *ain_path) +{ + char *basename_tmp = strdup(ain_path); + char *dirname_tmp = strdup(ain_path); + config.ain_filename = strdup(basename(basename_tmp)); + config.game_dir = strdup(dirname(dirname_tmp)); + free(basename_tmp); + free(dirname_tmp); + config_init(); +} #if (!defined(_WIN32) && !defined(__WIN32__)) #include @@ -373,7 +402,6 @@ enum { int main(int argc, char *argv[]) { initialize_instructions(); - size_t len; char *ainfile; int err = AIN_SUCCESS; bool audit = false; @@ -431,33 +459,24 @@ int main(int argc, char *argv[]) argc -= optind; argv += optind; - // TODO: if no argument given, try to read System40.ini/AliceStart.ini from current dir - if (argc != 1) { - ERROR("Wrong number of arguments"); - return 1; - } - - len = strlen(argv[0]); - if (len > 4 && !strcasecmp(&argv[0][len - 4], ".ini")) { - read_config(argv[0]); - if (!config.ain_filename) - ERROR("No AIN filename specified in %s", argv[0]); - - config.game_dir = strdup(dirname(argv[0])); - ainfile = gamedir_path(config.ain_filename); - } else if (len > 4 && !strcasecmp(&argv[0][len - 4], ".ain")) { - ainfile = strdup(argv[0]); - config.ain_filename = strdup(basename(argv[0])); - config.game_dir = strdup(dirname(argv[0])); - } else { - ERROR("Not an AIN/INI file: %s", &argv[0][len - 4]); + if (argc < 1) { + config_init_with_dir("."); + } else if (argc > 1) { + usage(); + ERROR("Too many arguments"); + } else if (is_directory(argv[0])) { + config_init_with_dir(argv[0]); + } else if (!strcasecmp(file_extension(argv[0]), "ini")) { + config_init_with_ini(argv[0]); + } else if (!strcasecmp(file_extension(argv[0]), "ain")) { + config_init_with_ain(argv[0]); } + ainfile = gamedir_path(config.ain_filename); #if (!defined(_WIN32) && !defined(__WIN32__)) signal(SIGCHLD, cleanup); #endif - config_init(); read_user_config(); // NOTE: some command line options are handled here so that they diff --git a/subprojects/libsys4 b/subprojects/libsys4 index b4bd43e..c6c2e92 160000 --- a/subprojects/libsys4 +++ b/subprojects/libsys4 @@ -1 +1 @@ -Subproject commit b4bd43e519771f3af3ac6ec656dfd2a0bb23e865 +Subproject commit c6c2e92e4062a54f4c2d8e025bd3a070d52162b1