From d45ff4c269f409164ed66a1d29f502e177ecb31e Mon Sep 17 00:00:00 2001 From: Misa Date: Sun, 21 Jun 2020 14:26:40 -0700 Subject: [PATCH] Abstract assets mounting to FileSystemUtils.cpp The assets mounting code was put directly in editorclass::load(), but now it's in a neat little function so it can be called from multiple places without having to call editorclass::load(). --- desktop_version/src/FileSystemUtils.cpp | 38 +++++++++++++++++++++++++ desktop_version/src/FileSystemUtils.h | 1 + desktop_version/src/editor.cpp | 34 +--------------------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index f38734eb..1d6448f9 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -166,6 +166,44 @@ void FILESYSTEM_mount(const char *fname) } } +void FILESYSTEM_mountassets(const char* path) +{ + const std::string _path(path); + + std::string zippath = "levels/" + _path.substr(7,_path.size()-14) + ".data.zip"; + std::string dirpath = "levels/" + _path.substr(7,_path.size()-14) + "/"; + std::string zip_path; + const char* cstr = PHYSFS_getRealDir(_path.c_str()); + + if (cstr) { + zip_path = cstr; + } + + if (cstr && FILESYSTEM_directoryExists(zippath.c_str())) { + printf("Custom asset directory exists at %s\n", zippath.c_str()); + FILESYSTEM_mount(zippath.c_str()); + graphics.reloadresources(); + } else if (zip_path != "data.zip" && !endsWith(zip_path, "/data.zip") && endsWith(zip_path, ".zip")) { + printf("Custom asset directory is .zip at %s\n", zip_path.c_str()); + PHYSFS_File* zip = PHYSFS_openRead(zip_path.c_str()); + zip_path += ".data.zip"; + if (zip == NULL) { + printf("error loading .zip: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); + } else if (PHYSFS_mountHandle(zip, zip_path.c_str(), "/", 0) == 0) { + printf("error mounting .zip: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); + } else { + graphics.assetdir = zip_path; + } + graphics.reloadresources(); + } else if (FILESYSTEM_directoryExists(dirpath.c_str())) { + printf("Custom asset directory exists at %s\n",dirpath.c_str()); + FILESYSTEM_mount(dirpath.c_str()); + graphics.reloadresources(); + } else { + printf("Custom asset directory does not exist\n"); + } +} + void FILESYSTEM_unmountassets() { if (graphics.assetdir != "") diff --git a/desktop_version/src/FileSystemUtils.h b/desktop_version/src/FileSystemUtils.h index d494cc85..e08a24cb 100644 --- a/desktop_version/src/FileSystemUtils.h +++ b/desktop_version/src/FileSystemUtils.h @@ -15,6 +15,7 @@ char *FILESYSTEM_getUserLevelDirectory(); bool FILESYSTEM_directoryExists(const char *fname); void FILESYSTEM_mount(const char *fname); +void FILESYSTEM_mountassets(const char *path); void FILESYSTEM_unmountassets(); void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, diff --git a/desktop_version/src/editor.cpp b/desktop_version/src/editor.cpp index 7300c5d2..ccb2c964 100644 --- a/desktop_version/src/editor.cpp +++ b/desktop_version/src/editor.cpp @@ -1623,39 +1623,7 @@ bool editorclass::load(std::string& _path) } FILESYSTEM_unmountassets(); - - std::string zippath = "levels/" + _path.substr(7,_path.size()-14) + ".data.zip"; - std::string dirpath = "levels/" + _path.substr(7,_path.size()-14) + "/"; - std::string zip_path; - const char* cstr = PHYSFS_getRealDir(_path.c_str()); - - if (cstr) { - zip_path = cstr; - } - - if (cstr && FILESYSTEM_directoryExists(zippath.c_str())) { - printf("Custom asset directory exists at %s\n", zippath.c_str()); - FILESYSTEM_mount(zippath.c_str()); - graphics.reloadresources(); - } else if (zip_path != "data.zip" && !endsWith(zip_path, "/data.zip") && endsWith(zip_path, ".zip")) { - printf("Custom asset directory is .zip at %s\n", zip_path.c_str()); - PHYSFS_File* zip = PHYSFS_openRead(zip_path.c_str()); - zip_path += ".data.zip"; - if (zip == NULL) { - printf("error loading .zip: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); - } else if (PHYSFS_mountHandle(zip, zip_path.c_str(), "/", 0) == 0) { - printf("error mounting .zip: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); - } else { - graphics.assetdir = zip_path; - } - graphics.reloadresources(); - } else if (FILESYSTEM_directoryExists(dirpath.c_str())) { - printf("Custom asset directory exists at %s\n",dirpath.c_str()); - FILESYSTEM_mount(dirpath.c_str()); - graphics.reloadresources(); - } else { - printf("Custom asset directory does not exist\n"); - } + FILESYSTEM_mountassets(_path.c_str()); tinyxml2::XMLDocument doc; if (!FILESYSTEM_loadTiXml2Document(_path.c_str(), doc))