From d6e2018e4bb9e9c3c3fcf4cab86b507ddc420c4c Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 8 Mar 2014 01:13:34 +0000 Subject: [PATCH] [Change] If ldata isn't found, try to be a bit more robust in finding it yah?! --- src/conf.c | 30 +++++++++++++++++++++++++----- src/lephisto.c | 2 +- src/lfile.c | 40 ++++++++++++++++++++++++++++------------ src/lfile.h | 4 ++-- src/nebulae.c | 2 +- src/pack.c | 2 +- src/player.c | 2 +- src/save.c | 2 +- 8 files changed, 60 insertions(+), 24 deletions(-) diff --git a/src/conf.c b/src/conf.c index 8cd6fff..c97727b 100644 --- a/src/conf.c +++ b/src/conf.c @@ -13,6 +13,7 @@ #include "input.h" #include "music.h" #include "nebulae.h" +#include "pack.h" #include "lfile.h" #include "conf.h" @@ -80,17 +81,36 @@ static void print_usage(char** argv) { LOG(" -v - Print the version and exit"); } -char dataVersion[PATH_MAX]; /** * @brief Set the default configuration. */ void conf_setDefaults(void) { - /* Global. */ + int i, nfiles; + char** files; + size_t len; + + /* Find data. */ if(lfile_fileExists("%s-%d.%d.%d", DATA_NAME, VMAJOR, VMINOR, VREV)) { - snprintf(dataVersion, 128, "%s-%d.%d.%d", DATA_NAME, VMAJOR, VMINOR, VREV); - data = dataVersion; - } else + data = malloc(PATH_MAX); + snprintf(data, PATH_MAX, "%s-%d.%d.%d", DATA_NAME, VMAJOR, VMINOR, VREV); + } else if(lfile_fileExists(DATA_DEF)) data = DATA_DEF; + else { + files = lfile_readDir(&nfiles, "."); + len = strlen(DATA_NAME); + for(i = 0; i < nfiles; i++) { + if(strncmp(files[i], DATA_NAME, len) == 0) { + /* Must be packfile. */ + if(pack_check(files[i])) + continue; + + data = strdup(files[i]); + } + } + for(i = 0; i < nfiles; i++) + free(files[i]); + free(files); + } /* GL. */ gl_screen.w = 800; gl_screen.h = 600; diff --git a/src/lephisto.c b/src/lephisto.c index a3a90fa..9cbbe49 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -132,7 +132,7 @@ int main(int argc, char** argv) { debug_sigInit(); /* Create the home directory if needed. */ - if(lfile_dirMakeExist(".")) + if(lfile_dirMakeExist("%s", lfile_basePath())) WARN("Unable to create lephisto directory '%s'", lfile_basePath()); /* Input must be initialized for config to work. */ diff --git a/src/lfile.c b/src/lfile.c index c0195e2..3dec4bb 100644 --- a/src/lfile.c +++ b/src/lfile.c @@ -60,18 +60,23 @@ char* lfile_basePath(void) { * @param path Path to create directory if it doesn't exist. * @return 0 on success. */ -int lfile_dirMakeExist(const char* path) { +int lfile_dirMakeExist(const char* path, ...) { char file[PATH_MAX]; + va_list ap; + size_t l; + + l = 0; + if(path == NULL) return -1; + else { /* Get the message. */ + va_start(ap, path); + vsnprintf(file, PATH_MAX-l, path, ap); + l = strlen(file); + va_end(ap); + } #ifdef LINUX struct stat buf; - if(strcmp(path, ".")==0) { - strncpy(file, lfile_basePath(), PATH_MAX); - file[PATH_MAX-1] = '\0'; - } - else - snprintf(file, PATH_MAX, "%s%s", lfile_basePath(), path); stat(file, &buf); if(!S_ISDIR(buf.st_mode)) if(mkdir(file, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { @@ -127,11 +132,22 @@ int lfile_fileExists(const char* path, ...) { * @param[out] lfiles Returns how many files there are. * @param path Directory to read. */ -char** lfile_readDir(int* lfiles, const char* path) { - char file[PATH_MAX]; +char** lfile_readDir(int* lfiles, const char* path, ...) { + char file[PATH_MAX], base[PATH_MAX]; char** files; + va_list ap; + size_t l; - snprintf(file, PATH_MAX, "%s%s", lfile_basePath(), path); + l = 0; + if(path == NULL) { + *lfiles = 0; + return NULL; + } else { /* Get the message. */ + va_start(ap, path); + vsnprintf(base, PATH_MAX-l, path, ap); + l = strlen(base); + va_end(ap); + } #ifdef LINUX int i, j, k, n; @@ -148,7 +164,7 @@ char** lfile_readDir(int* lfiles, const char* path) { tfiles = malloc(sizeof(char*)*mfiles); tt = malloc(sizeof(time_t)*mfiles); - d = opendir(file); + d = opendir(base); if(d == NULL) return NULL; @@ -161,7 +177,7 @@ char** lfile_readDir(int* lfiles, const char* path) { continue; /* Stat the file. */ - snprintf(file, PATH_MAX, "%s%s/%s", lfile_basePath(), path, name); + snprintf(file, PATH_MAX, "%s/%s", base, name); if(stat(file, &sb) == -1) continue; /* Unable to stat. */ diff --git a/src/lfile.h b/src/lfile.h index da61cde..287a46e 100644 --- a/src/lfile.h +++ b/src/lfile.h @@ -3,10 +3,10 @@ char* lfile_basePath(void); /* Create if doesn't exist, 0 success. */ -int lfile_dirMakeExist(const char* path); +int lfile_dirMakeExist(const char* path, ...); /* Return 1 on exit. */ int lfile_fileExists(const char* path, ...); -char** lfile_readDir(int* lfiles, const char* path); +char** lfile_readDir(int* lfiles, const char* path, ...); diff --git a/src/nebulae.c b/src/nebulae.c index 9851e05..ce1c629 100644 --- a/src/nebulae.c +++ b/src/nebulae.c @@ -493,7 +493,7 @@ static int nebu_generate(void) { /* Generate all the nebulae backgrounds. */ nebu = noise_genNebulaeMap(w, h, NEBULAE_Z, 5.); - lfile_dirMakeExist("gen"); + lfile_dirMakeExist("%sgen", lfile_basePath()); /* Save each nebulae as an image. */ for(i = 0; i < NEBULAE_Z; i++) { diff --git a/src/pack.c b/src/pack.c index 5caaa91..f6947d7 100644 --- a/src/pack.c +++ b/src/pack.c @@ -79,7 +79,7 @@ static off_t getfilesize(const char* filename) { * * @brief Check to see if a file is a packfile * @param filename Name of the file to check. - * @return 1 if it is a packfile, 0 if it isn't and -1 on error. + * @return 0 if it is a packfile, 1 if it isn't and -1 on error. */ int pack_check(const char* filename) { int ret; diff --git a/src/player.c b/src/player.c index 3572df5..b417b37 100644 --- a/src/player.c +++ b/src/player.c @@ -2042,7 +2042,7 @@ void player_screenshot(void) { int done; char filename[PATH_MAX]; - if(lfile_dirMakeExist("screenshots")) { + if(lfile_dirMakeExist("%sscreenshots", lfile_basePath())) { WARN("Aborting screenshots"); return; } diff --git a/src/save.c b/src/save.c index 98fc75c..3d40eec 100644 --- a/src/save.c +++ b/src/save.c @@ -111,7 +111,7 @@ void load_game_menu(void) { wid = window_create("Load Game", -1, -1, LOAD_WIDTH, LOAD_HEIGHT); /* Load the saves. */ - files = lfile_readDir(&lfiles, "saves"); + files = lfile_readDir(&lfiles, "%ssaves", lfile_basePath()); for(i = 0; i < lfiles; i++) { len = strlen(files[i]);