From 1873f7aba57513d6ea5cb2577e827be2cd4b4397 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Mon, 25 May 2020 07:17:10 +0300 Subject: [PATCH] game now uses non-working directory paths by default saves by default go into XDG_DATA_HOME/sm64pc external data is read from the executable directory, if it's not found there on Unix systems the game will attempt to read it from some paths like /usr/local/share/sm64pc both save data and readonly data fall back to other options in case of a problem behavior can be overridden by specifying --datapath and --savepath on the CLI both of those will expand the exclamation point ('!') to the executable path, e. g. --savepath '!/save' --- Makefile | 8 +- src/game/options_menu.c | 2 +- src/pc/cliopts.c | 39 ++++---- src/pc/cliopts.h | 6 +- src/pc/configfile.c | 14 +++ src/pc/configfile.h | 2 +- src/pc/gfx/gfx_pc.c | 5 +- src/pc/pc_main.c | 4 +- src/pc/platform.c | 158 ++++++++++++++++++++++++++++++++ src/pc/platform.h | 16 ++++ src/pc/ultra_reimplementation.c | 9 +- 11 files changed, 234 insertions(+), 29 deletions(-) create mode 100644 src/pc/platform.c create mode 100644 src/pc/platform.h diff --git a/Makefile b/Makefile index e9ebc5d0..17b93a55 100644 --- a/Makefile +++ b/Makefile @@ -491,8 +491,8 @@ PYTHON := python3 SDLCONFIG := $(CROSS)sdl2-config ifeq ($(WINDOWS_BUILD),1) -CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) `$(SDLCONFIG) --cflags` -CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv `$(SDLCONFIG) --cflags` +CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) `$(SDLCONFIG) --cflags` -DUSE_SDL=2 +CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv `$(SDLCONFIG) --cflags` -DUSE_SDL=2 else ifeq ($(TARGET_WEB),1) CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -s USE_SDL=2 @@ -500,8 +500,8 @@ CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fn # Linux / Other builds below else -CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) `$(SDLCONFIG) --cflags` -CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv `$(SDLCONFIG) --cflags` +CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) `$(SDLCONFIG) --cflags` -DUSE_SDL=2 +CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv `$(SDLCONFIG) --cflags` -DUSE_SDL=2 endif # Check for enhancement options diff --git a/src/game/options_menu.c b/src/game/options_menu.c index 8d1661bf..4e08dbdb 100644 --- a/src/game/options_menu.c +++ b/src/game/options_menu.c @@ -496,7 +496,7 @@ void optmenu_toggle(void) { newcam_init_settings(); // load bettercam settings from config vars #endif controller_reconfigure(); // rebind using new config values - configfile_save(gCLIOpts.ConfigFile); + configfile_save(configfile_name()); } } diff --git a/src/pc/cliopts.c b/src/pc/cliopts.c index 0a0e6a55..bff335a2 100644 --- a/src/pc/cliopts.c +++ b/src/pc/cliopts.c @@ -2,6 +2,7 @@ #include "configfile.h" #include "cheats.h" #include "pc_main.h" +#include "platform.h" #include #include @@ -15,15 +16,27 @@ static void print_help(void) { printf("Super Mario 64 PC Port\n"); printf("%-20s\tEnables the cheat menu.\n", "--cheats"); printf("%-20s\tSaves the configuration file as CONFIGNAME.\n", "--configfile CONFIGNAME"); + printf("%-20s\tOverrides the default read-only data path ('!' expands to executable path).\n", "--datapath DATAPATH"); + printf("%-20s\tOverrides the default save/config path ('!' expands to executable path).\n", "--savepath SAVEPATH"); printf("%-20s\tStarts the game in full screen mode.\n", "--fullscreen"); printf("%-20s\tSkips the Peach and Castle intro when starting a new game.\n", "--skip-intro"); printf("%-20s\tStarts the game in windowed mode.\n", "--windowed"); } +static inline int arg_string(const char *name, const char *value, char *target) { + const unsigned int arglen = strlen(value); + if (arglen >= SYS_MAX_PATH) { + fprintf(stderr, "Supplied value for `%s` is too long.\n", name); + return 0; + } + strncpy(target, value, arglen); + target[arglen] = '\0'; + return 1; +} + void parse_cli_opts(int argc, char* argv[]) { // Initialize options with false values. memset(&gCLIOpts, 0, sizeof(gCLIOpts)); - strncpy(gCLIOpts.ConfigFile, CONFIGFILE_DEFAULT, sizeof(gCLIOpts.ConfigFile)); for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--skip-intro") == 0) // Skip Peach Intro @@ -38,25 +51,19 @@ void parse_cli_opts(int argc, char* argv[]) { else if (strcmp(argv[i], "--cheats") == 0) // Enable cheats menu Cheats.EnableCheats = true; + else if (strcmp(argv[i], "--configfile") == 0 && (i + 1) < argc) + arg_string("--configfile", argv[++i], gCLIOpts.ConfigFile); + + else if (strcmp(argv[i], "--datapath") == 0 && (i + 1) < argc) + arg_string("--datapath", argv[++i], gCLIOpts.DataPath); + + else if (strcmp(argv[i], "--savepath") == 0 && (i + 1) < argc) + arg_string("--savepath", argv[++i], gCLIOpts.SavePath); + // Print help else if (strcmp(argv[i], "--help") == 0) { print_help(); game_exit(); } - - else if (strcmp(argv[i], "--configfile") == 0) { - if (i+1 < argc) { - const unsigned int arglen = strlen(argv[i+1]); - if (arglen >= sizeof(gCLIOpts.ConfigFile)) { - fprintf(stderr, "Configuration file supplied has a name too long.\n"); - } else { - strncpy(gCLIOpts.ConfigFile, argv[i+1], arglen); - gCLIOpts.ConfigFile[arglen] = '\0'; - } - } - - // Skip the next string since it's the configuration file name. - i++; - } } } diff --git a/src/pc/cliopts.h b/src/pc/cliopts.h index d20dcb4f..2d084eda 100644 --- a/src/pc/cliopts.h +++ b/src/pc/cliopts.h @@ -1,10 +1,14 @@ #ifndef _CLIOPTS_H #define _CLIOPTS_H +#include "platform.h" + struct PCCLIOptions { unsigned int SkipIntro; unsigned int FullScreen; - char ConfigFile[1024]; + char ConfigFile[SYS_MAX_PATH]; + char SavePath[SYS_MAX_PATH]; + char DataPath[SYS_MAX_PATH]; }; extern struct PCCLIOptions gCLIOpts; diff --git a/src/pc/configfile.c b/src/pc/configfile.c index c1983855..588d075a 100644 --- a/src/pc/configfile.c +++ b/src/pc/configfile.c @@ -7,7 +7,9 @@ #include #include +#include "platform.h" #include "configfile.h" +#include "cliopts.h" #include "gfx/gfx_screen_config.h" #include "controller/controller_api.h" @@ -192,6 +194,18 @@ static unsigned int tokenize_string(char *str, int maxTokens, char **tokens) { return count; } +// Gets the config file path and caches it +const char *configfile_name(void) { + static char cfgpath[SYS_MAX_PATH] = { 0 }; + if (!cfgpath[0]) { + if (gCLIOpts.ConfigFile[0]) + snprintf(cfgpath, sizeof(cfgpath), "%s", gCLIOpts.ConfigFile); + else + snprintf(cfgpath, sizeof(cfgpath), "%s/%s", sys_save_path(), CONFIGFILE_DEFAULT); + } + return cfgpath; +} + // Loads the config file specified by 'filename' void configfile_load(const char *filename) { FILE *file; diff --git a/src/pc/configfile.h b/src/pc/configfile.h index 6f2c4a25..263d3907 100644 --- a/src/pc/configfile.h +++ b/src/pc/configfile.h @@ -4,7 +4,6 @@ #include #define CONFIGFILE_DEFAULT "sm64config.txt" -#define DATAPATH_DEFAULT "res" #define MAX_BINDS 3 #define MAX_VOLUME 127 @@ -51,5 +50,6 @@ extern bool configHUD; void configfile_load(const char *filename); void configfile_save(const char *filename); +const char *configfile_name(void); #endif diff --git a/src/pc/gfx/gfx_pc.c b/src/pc/gfx/gfx_pc.c index 210c34ff..5abe91e3 100644 --- a/src/pc/gfx/gfx_pc.c +++ b/src/pc/gfx/gfx_pc.c @@ -23,6 +23,7 @@ #include "gfx_rendering_api.h" #include "gfx_screen_config.h" +#include "../platform.h" #include "../configfile.h" #define SUPPORT_CHECK(x) assert(x) @@ -505,10 +506,10 @@ static void import_texture(int tile) { #ifdef EXTERNAL_TEXTURES // the "texture data" is actually a C string with the path to our texture in it // load it from an external image in our data path - static char fpath[1024]; + static char fpath[SYS_MAX_PATH]; int w, h; const char *texname = (const char*)rdp.loaded_texture[tile].addr; - snprintf(fpath, sizeof(fpath), "%s/%s.png", DATAPATH_DEFAULT, texname); + snprintf(fpath, sizeof(fpath), "%s/%s.png", sys_data_path(), texname); u8 *data = stbi_load(fpath, &w, &h, NULL, 4); if (!data) { fprintf(stderr, "texture not found: `%s`\n", fpath); diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index c4162c32..ddb83ce4 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -92,7 +92,7 @@ void audio_shutdown(void) { } void game_deinit(void) { - configfile_save(gCLIOpts.ConfigFile);; + configfile_save(configfile_name()); controller_shutdown(); audio_shutdown(); gfx_shutdown(); @@ -145,7 +145,7 @@ void main_func(void) { main_pool_init(pool, pool + sizeof(pool) / sizeof(pool[0])); gEffectsMemoryPool = mem_pool_init(0x4000, MEMORY_POOL_LEFT); - configfile_load(gCLIOpts.ConfigFile); + configfile_load(configfile_name()); wm_api = &gfx_sdl; rendering_api = &gfx_opengl_api; diff --git a/src/pc/platform.c b/src/pc/platform.c new file mode 100644 index 00000000..720c91c1 --- /dev/null +++ b/src/pc/platform.c @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#endif + +#include "cliopts.h" + +static inline bool dir_exists(const char *path) { + struct stat st; + return (stat(path, &st) == 0 && S_ISDIR(st.st_mode)); +} + +bool sys_mkdir(const char *name) { + #ifdef _WIN32 + return _mkdir(name) == 0; + #else + return mkdir(name, 0777) == 0; + #endif +} + +#if USE_SDL + +// we can just ask SDL for most of this shit if we have it +#include + +const char *sys_data_path(void) { + static char path[SYS_MAX_PATH] = { 0 }; + + if (!path[0]) { + // prefer the override, if it is set + // "!" expands to executable path + if (gCLIOpts.DataPath[0]) { + if (gCLIOpts.DataPath[0] == '!') + snprintf(path, sizeof(path), "%s%s", sys_exe_path(), gCLIOpts.DataPath + 1); + else + snprintf(path, sizeof(path), "%s", gCLIOpts.DataPath); + if (dir_exists(path)) return path; + printf("Warning: Specified data path ('%s') doesn't exist\n", path); + } + + // then the executable directory + snprintf(path, sizeof(path), "%s/" DATADIR, sys_exe_path()); + if (dir_exists(path)) return path; + + // then the save path + snprintf(path, sizeof(path), "%s/" DATADIR, sys_save_path()); + if (dir_exists(path)) return path; + + #if defined(__linux__) || defined(__unix__) + // on Linux/BSD try some common paths for read-only data + const char *try[] = { + "/usr/local/share/sm64pc/" DATADIR, + "/usr/share/sm64pc/" DATADIR, + "/opt/sm64pc/" DATADIR, + }; + for (unsigned i = 0; i < sizeof(try) / sizeof(try[0]); ++i) { + if (dir_exists(try[i])) { + strcpy(path, try[i]); + return path; + } + } + #endif + + // hope for the best + strcpy(path, "./" DATADIR); + } + + return path; +} + +const char *sys_save_path(void) { + static char path[SYS_MAX_PATH] = { 0 }; + + if (!path[0]) { + // if the override is set, use that + // "!" expands to executable path + if (gCLIOpts.SavePath[0]) { + if (gCLIOpts.SavePath[0] == '!') + snprintf(path, sizeof(path), "%s%s", sys_exe_path(), gCLIOpts.SavePath + 1); + else + snprintf(path, sizeof(path), "%s", gCLIOpts.SavePath); + if (!dir_exists(path) && !sys_mkdir(path)) { + printf("Warning: Specified save path ('%s') doesn't exist and can't be created\n", path); + path[0] = 0; // doesn't exist and no write access + } + } + + // didn't work? get it from SDL + if (!path[0]) { + char *sdlpath = SDL_GetPrefPath("", "sm64pc"); + if (sdlpath) { + const unsigned int len = strlen(sdlpath); + strncpy(path, sdlpath, sizeof(path)); + path[sizeof(path)-1] = 0; + SDL_free(sdlpath); + if (path[len-1] == '/' || path[len-1] == '\\') + path[len-1] = 0; // strip the trailing separator + if (!dir_exists(path) && !sys_mkdir(path)) + path[0] = 0; + } + } + + // if all else fails, just store near the EXE + if (!path[0]) + strcpy(path, sys_exe_path()); + + printf("Save path set to '%s'\n", path); + } + + return path; +} + +const char *sys_exe_path(void) { + static char path[SYS_MAX_PATH] = { 0 }; + + if (!path[0]) { + char *sdlpath = SDL_GetBasePath(); + if (sdlpath) { + // use the SDL path if it exists + const unsigned int len = strlen(sdlpath); + strncpy(path, sdlpath, sizeof(path)); + path[sizeof(path)-1] = 0; + SDL_free(sdlpath); + if (path[len-1] == '/' || path[len-1] == '\\') + path[len-1] = 0; // strip the trailing separator + } else { + // hope for the best + strcpy(path, "."); + } + printf("Executable path set to '%s'\n", path); + } + + return path; +} + +#else + +#warning "You might want to implement these functions for your platform" + +const char *sys_data_path(void) { + return "."; +} + +const char *sys_save_path(void) { + return "."; +} + +const char *sys_exe_path(void) { + return "."; +} + +#endif // platform switch diff --git a/src/pc/platform.h b/src/pc/platform.h new file mode 100644 index 00000000..dae51bf9 --- /dev/null +++ b/src/pc/platform.h @@ -0,0 +1,16 @@ +#ifndef _SM64_PLATFORM_H_ +#define _SM64_PLATFORM_H_ + +#include + +/* Platform-specific functions and whatnot */ + +#define DATADIR "res" +#define SYS_MAX_PATH 1024 // FIXME: define this on different platforms + +bool sys_mkdir(const char *name); // creates with 0777 by default +const char *sys_data_path(void); +const char *sys_save_path(void); +const char *sys_exe_path(void); + +#endif // _SM64_PLATFORM_H_ diff --git a/src/pc/ultra_reimplementation.c b/src/pc/ultra_reimplementation.c index 476e3590..c28894f6 100644 --- a/src/pc/ultra_reimplementation.c +++ b/src/pc/ultra_reimplementation.c @@ -2,6 +2,7 @@ #include #include "lib/src/libultra_internal.h" #include "macros.h" +#include "platform.h" #ifdef TARGET_WEB #include @@ -119,7 +120,9 @@ s32 osEepromLongRead(UNUSED OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) ret = 0; } #else - FILE *fp = fopen("sm64_save_file.bin", "rb"); + char save_path[SYS_MAX_PATH] = { 0 }; + snprintf(save_path, sizeof(save_path), "%s/sm64_save_file.bin", sys_save_path()); + FILE *fp = fopen(save_path, "rb"); if (fp == NULL) { return -1; } @@ -149,7 +152,9 @@ s32 osEepromLongWrite(UNUSED OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes }, content); s32 ret = 0; #else - FILE* fp = fopen("sm64_save_file.bin", "wb"); + char save_path[SYS_MAX_PATH] = { 0 }; + snprintf(save_path, sizeof(save_path), "%s/sm64_save_file.bin", sys_save_path()); + FILE *fp = fopen(save_path, "wb"); if (fp == NULL) { return -1; }