From d75b7c00232834cea1b7a756c9e62db1b5d422a0 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 28 Mar 2020 22:56:19 +0300 Subject: [PATCH] Feature request; allow loading prefs.bin relatively --- SDL/main.c | 10 +++++++--- Windows/stdio.h | 6 +++++- Windows/utf8_compat.c | 12 +++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/SDL/main.c b/SDL/main.c index 35c9a75..4b6afea 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -11,6 +11,7 @@ #ifndef _WIN32 #define AUDIO_FREQUENCY 96000 +#include #else #include /* Windows (well, at least my VM) can't handle 96KHz sound well :( */ @@ -686,9 +687,12 @@ int main(int argc, char **argv) SDL_EventState(SDL_DROPFILE, SDL_ENABLE); - char *prefs_dir = SDL_GetPrefPath("", "SameBoy"); - snprintf(prefs_path, sizeof(prefs_path) - 1, "%sprefs.bin", prefs_dir); - SDL_free(prefs_dir); + strcpy(prefs_path, resource_path("prefs.bin")); + if (access(prefs_path, R_OK | W_OK) != 0) { + char *prefs_dir = SDL_GetPrefPath("", "SameBoy"); + snprintf(prefs_path, sizeof(prefs_path) - 1, "%sprefs.bin", prefs_dir); + SDL_free(prefs_dir); + } FILE *prefs_file = fopen(prefs_path, "rb"); if (prefs_file) { diff --git a/Windows/stdio.h b/Windows/stdio.h index d68c956..0595b01 100755 --- a/Windows/stdio.h +++ b/Windows/stdio.h @@ -2,6 +2,10 @@ #include_next #include +int access(const char *filename, int mode); +#define R_OK 2 +#define W_OK 4 + #ifndef __MINGW32__ #ifndef __LIBRETRO__ static inline int vasprintf(char **str, const char *fmt, va_list args) @@ -72,4 +76,4 @@ static inline size_t getline(char **lineptr, size_t *n, FILE *stream) { return p - bufptr - 1; } -#define snprintf _snprintf \ No newline at end of file +#define snprintf _snprintf diff --git a/Windows/utf8_compat.c b/Windows/utf8_compat.c index 1005f22..0347211 100755 --- a/Windows/utf8_compat.c +++ b/Windows/utf8_compat.c @@ -1,6 +1,7 @@ #include #include #include +#include FILE *fopen(const char *filename, const char *mode) { @@ -11,4 +12,13 @@ FILE *fopen(const char *filename, const char *mode) MultiByteToWideChar(CP_UTF8, 0, mode, -1, w_mode, sizeof(w_mode) / sizeof(w_mode[0])); return _wfopen(w_filename, w_mode); -} \ No newline at end of file +} + +int access(const char *filename, int mode) +{ + wchar_t w_filename[MAX_PATH] = {0,}; + MultiByteToWideChar(CP_UTF8, 0, filename, -1, w_filename, sizeof(w_filename) / sizeof(w_filename[0])); + + return _waccess(w_filename, mode); +} +