diff --git a/src/pc/fs/fs.c b/src/pc/fs/fs.c index 37f85ed..b9e117f 100644 --- a/src/pc/fs/fs.c +++ b/src/pc/fs/fs.c @@ -441,3 +441,30 @@ bool fs_sys_mkdir(const char *name) { return mkdir(name, 0777) == 0; #endif } + +bool fs_sys_copy_file(const char *oldname, const char *newname) { + uint8_t buf[2048]; + + FILE *fin = fopen(oldname, "rb"); + if (!fin) return false; + + FILE *fout = fopen(newname, "wb"); + if (!fout) { + fclose(fin); + return false; + } + + bool ret = true; + size_t rx; + while ((rx = fread(buf, 1, sizeof(buf), fin)) > 0) { + if (!fwrite(buf, rx, 1, fout)) { + ret = false; + break; + } + } + + fclose(fout); + fclose(fin); + + return ret; +} diff --git a/src/pc/fs/fs.h b/src/pc/fs/fs.h index c776514..25d176d 100644 --- a/src/pc/fs/fs.h +++ b/src/pc/fs/fs.h @@ -20,6 +20,8 @@ #define FS_TEXTUREDIR "gfx" #define FS_SOUNDDIR "sound" +#define SAVE_FILENAME "sm64_save_file.bin" + extern char fs_gamedir[]; extern char fs_writepath[]; @@ -131,5 +133,6 @@ fs_pathlist_t fs_sys_enumerate(const char *base, const bool recur); bool fs_sys_file_exists(const char *name); bool fs_sys_dir_exists(const char *name); bool fs_sys_mkdir(const char *name); // creates with 0777 by default +bool fs_sys_copy_file(const char *oldname, const char *newname); #endif // _SM64_FS_H_ diff --git a/src/pc/platform.c b/src/pc/platform.c index 6fc86c9..e68370a 100644 --- a/src/pc/platform.c +++ b/src/pc/platform.c @@ -7,6 +7,7 @@ #include "cliopts.h" #include "fs/fs.h" +#include "configfile.h" /* NULL terminated list of platform specific read-only data paths */ /* priority is top first */ @@ -84,20 +85,56 @@ void sys_fatal(const char *fmt, ...) { // we can just ask SDL for most of this shit if we have it #include +// TEMPORARY: check the old save folder and copy contents to the new path +// this will be removed after a while +static inline bool copy_userdata(const char *userdir) { + char oldpath[SYS_MAX_PATH] = { 0 }; + char path[SYS_MAX_PATH] = { 0 }; + + // check if a save already exists in the new folder + snprintf(path, sizeof(path), "%s/" SAVE_FILENAME, userdir); + if (fs_sys_file_exists(path)) return false; + + // check if a save exists in the old folder ('pc' instead of 'ex') + strncpy(oldpath, path, sizeof(oldpath)); + const unsigned int len = strlen(userdir); + oldpath[len - 2] = 'p'; oldpath[len - 1] = 'c'; + if (!fs_sys_file_exists(oldpath)) return false; + + printf("old save detected at '%s', copying to '%s'\n", oldpath, path); + + bool ret = fs_sys_copy_file(oldpath, path); + + // also try to copy the config + path[len] = oldpath[len] = 0; + strncat(path, "/" CONFIGFILE_DEFAULT, sizeof(path) - 1); + strncat(oldpath, "/" CONFIGFILE_DEFAULT, sizeof(oldpath) - 1); + fs_sys_copy_file(oldpath, path); + + return ret; +} + const char *sys_user_path(void) { static char path[SYS_MAX_PATH] = { 0 }; - // get it from SDL - char *sdlpath = SDL_GetPrefPath("", "sm64pc"); + + // get the new pref path from SDL + char *sdlpath = SDL_GetPrefPath("", "sm64ex"); 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 (!fs_sys_dir_exists(path) && !fs_sys_mkdir(path)) - path[0] = 0; + path[0] = 0; // somehow failed, we got no user path + else + copy_userdata(path); // TEMPORARY: try to copy old saves, if any } + return path; } diff --git a/src/pc/ultra_reimplementation.c b/src/pc/ultra_reimplementation.c index 43c9445..0389776 100644 --- a/src/pc/ultra_reimplementation.c +++ b/src/pc/ultra_reimplementation.c @@ -5,8 +5,6 @@ #include "platform.h" #include "fs/fs.h" -#define SAVE_FILENAME "sm64_save_file.bin" - #ifdef TARGET_WEB #include #endif