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'
This commit is contained in:
fgsfds 2020-05-25 07:17:10 +03:00
parent 9825b02f50
commit 1873f7aba5
11 changed files with 234 additions and 29 deletions

View File

@ -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

View File

@ -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());
}
}

View File

@ -2,6 +2,7 @@
#include "configfile.h"
#include "cheats.h"
#include "pc_main.h"
#include "platform.h"
#include <strings.h>
#include <stdlib.h>
@ -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++;
}
}
}

View File

@ -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;

View File

@ -7,7 +7,9 @@
#include <ctype.h>
#include <SDL2/SDL.h>
#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;

View File

@ -4,7 +4,6 @@
#include <stdbool.h>
#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

View File

@ -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);

View File

@ -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;

158
src/pc/platform.c Normal file
View File

@ -0,0 +1,158 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <direct.h>
#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 <SDL2/SDL.h>
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

16
src/pc/platform.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef _SM64_PLATFORM_H_
#define _SM64_PLATFORM_H_
#include <stdbool.h>
/* 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_

View File

@ -2,6 +2,7 @@
#include <string.h>
#include "lib/src/libultra_internal.h"
#include "macros.h"
#include "platform.h"
#ifdef TARGET_WEB
#include <emscripten.h>
@ -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;
}