diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 29fa9af2..c7f3b2c7 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -337,23 +337,31 @@ bool FILESYSTEM_loadTiXml2Document(const char *name, tinyxml2::XMLDocument& doc) return true; } -std::vector FILESYSTEM_getLevelDirFileNames() -{ - std::vector list; - char **fileList = PHYSFS_enumerateFiles("/levels"); - char **i; - std::string builtLocation; +static PHYSFS_EnumerateCallbackResult enumerateCallback( + void* data, + const char* origdir, + const char* filename +) { + void (*callback)(const char*) = (void (*)(const char*)) data; + char builtLocation[MAX_PATH]; - for (i = fileList; *i != NULL; i++) - { - builtLocation = "levels/"; - builtLocation += *i; - list.push_back(builtLocation); - } + SDL_snprintf( + builtLocation, + sizeof(builtLocation), + "%s/%s", + origdir, + filename + ); - PHYSFS_freeList(fileList); + callback(builtLocation); - return list; + return PHYSFS_ENUM_OK; +} + +void FILESYSTEM_enumerateLevelDirFileNames( + void (*callback)(const char* filename) +) { + PHYSFS_enumerate("levels", enumerateCallback, (void*) callback); } static void PLATFORM_getOSDirectory(char* output) diff --git a/desktop_version/src/FileSystemUtils.h b/desktop_version/src/FileSystemUtils.h index 254383f4..6cc4c781 100644 --- a/desktop_version/src/FileSystemUtils.h +++ b/desktop_version/src/FileSystemUtils.h @@ -1,8 +1,7 @@ #ifndef FILESYSTEMUTILS_H #define FILESYSTEMUTILS_H -#include -#include +#include // Forward declaration, including the entirety of tinyxml2.h across all files this file is included in is unnecessary namespace tinyxml2 { class XMLDocument; } @@ -25,7 +24,7 @@ void FILESYSTEM_freeMemory(unsigned char **mem); bool FILESYSTEM_saveTiXml2Document(const char *name, tinyxml2::XMLDocument& doc); bool FILESYSTEM_loadTiXml2Document(const char *name, tinyxml2::XMLDocument& doc); -std::vector FILESYSTEM_getLevelDirFileNames(); +void FILESYSTEM_enumerateLevelDirFileNames(void (*callback)(const char* filename)); bool FILESYSTEM_openDirectoryEnabled(); bool FILESYSTEM_openDirectory(const char *dname); diff --git a/desktop_version/src/editor.cpp b/desktop_version/src/editor.cpp index ff41995f..4d18ec3d 100644 --- a/desktop_version/src/editor.cpp +++ b/desktop_version/src/editor.cpp @@ -77,28 +77,28 @@ static bool compare_nocase (std::string first, std::string second) return false; } -void editorclass::loadZips() +static void levelZipCallback(const char* filename) { - directoryList = FILESYSTEM_getLevelDirFileNames(); - bool needsReload = false; + std::string filename_ = filename; - for(size_t i = 0; i < directoryList.size(); i++) + if (endsWith(filename_, ".zip")) { - if (endsWith(directoryList[i], ".zip")) { - PHYSFS_File* zip = PHYSFS_openRead(directoryList[i].c_str()); - if (!PHYSFS_mountHandle(zip, directoryList[i].c_str(), "levels", 1)) { - printf( - "Could not mount %s: %s\n", - filename_.c_str(), - PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()) - ); - } else { - needsReload = true; - } + PHYSFS_File* zip = PHYSFS_openRead(filename_.c_str()); + + if (!PHYSFS_mountHandle(zip, filename_.c_str(), "levels", 1)) + { + printf( + "Could not mount %s: %s\n", + filename_.c_str(), + PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()) + ); } } +} - if (needsReload) directoryList = FILESYSTEM_getLevelDirFileNames(); +void editorclass::loadZips() +{ + FILESYSTEM_enumerateLevelDirFileNames(levelZipCallback); } static void replace_all(std::string& str, const std::string& from, const std::string& to) @@ -206,24 +206,26 @@ TAG_FINDER(find_website, "website"); #undef TAG_FINDER +static void levelMetaDataCallback(const char* filename) +{ + extern editorclass ed; + LevelMetaData temp; + std::string filename_ = filename; + + if (ed.getLevelMetaData(filename_, temp)) + { + ed.ListOfMetaData.push_back(temp); + } +} + void editorclass::getDirectoryData() { ListOfMetaData.clear(); - directoryList.clear(); loadZips(); - directoryList = FILESYSTEM_getLevelDirFileNames(); - - for(size_t i = 0; i < directoryList.size(); i++) - { - LevelMetaData temp; - if (getLevelMetaData( directoryList[i], temp)) - { - ListOfMetaData.push_back(temp); - } - } + FILESYSTEM_enumerateLevelDirFileNames(levelMetaDataCallback); for(size_t i = 0; i < ListOfMetaData.size(); i++) { @@ -232,7 +234,6 @@ void editorclass::getDirectoryData() if(compare_nocase(ListOfMetaData[i].title, ListOfMetaData[k].title )) { std::swap(ListOfMetaData[i] , ListOfMetaData[k]); - std::swap(directoryList[i], directoryList[k]); } } } diff --git a/desktop_version/src/editor.h b/desktop_version/src/editor.h index 3078cd2d..5f64ec2e 100644 --- a/desktop_version/src/editor.h +++ b/desktop_version/src/editor.h @@ -110,7 +110,6 @@ class editorclass{ std::string Desc3; std::string website; - std::vector directoryList; std::vector ListOfMetaData; void loadZips(); diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index 99015a6d..55e4cd9b 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -298,9 +298,6 @@ int main(int argc, char *argv[]) game.playassets = playassets; game.menustart = true; - ed.directoryList.clear(); - ed.directoryList.push_back(playtestname); - LevelMetaData meta; if (ed.getLevelMetaData(playtestname, meta)) { ed.ListOfMetaData.clear();