From 9c8ecdb0f404d36cf36289fae1a16b4ae23c79e1 Mon Sep 17 00:00:00 2001 From: Misa Date: Sat, 3 Apr 2021 02:14:15 -0700 Subject: [PATCH] Return early if FILESYSTEM_mountassets() fails This way, we avoid the unnecessary graphics.reloadresources() call - if we can't mount assets, why bother reloading resources? The return type of FILESYSTEM_mount() has been changed from void to bool to indicate success, accomodating its callers accordingly. --- desktop_version/src/FileSystemUtils.cpp | 32 +++++++++++++++---------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index fdd79a35..f4513400 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -213,7 +213,7 @@ static bool FILESYSTEM_exists(const char *fname) return PHYSFS_exists(fname); } -static void FILESYSTEM_mount(const char *fname) +static bool FILESYSTEM_mount(const char *fname) { const char* real_dir = PHYSFS_getRealDir(fname); const char* dir_separator; @@ -225,7 +225,7 @@ static void FILESYSTEM_mount(const char *fname) "Could not mount %s: real directory doesn't exist\n", fname ); - return; + return false; } dir_separator = PHYSFS_getDirSeparator(); @@ -235,11 +235,11 @@ static void FILESYSTEM_mount(const char *fname) if (!PHYSFS_mount(path, NULL, 0)) { printf("Error mounting: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); + return false; } - else - { - SDL_strlcpy(assetDir, path, sizeof(assetDir)); - } + + SDL_strlcpy(assetDir, path, sizeof(assetDir)); + return true; } void FILESYSTEM_loadZip(const char* filename) @@ -298,7 +298,10 @@ void FILESYSTEM_mountassets(const char* path) { printf("Custom asset directory is .data.zip at %s\n", zip_data); - FILESYSTEM_mount(zip_data); + if (!FILESYSTEM_mount(zip_data)) + { + return; + } graphics.reloadresources(); } @@ -321,18 +324,18 @@ void FILESYSTEM_mountassets(const char* path) "Error loading .zip: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()) ); + return; } - else if (PHYSFS_mountHandle(zip, zip_data, "/", 0) == 0) + if (PHYSFS_mountHandle(zip, zip_data, "/", 0) == 0) { printf( "Error mounting .zip: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()) ); + return; } - else - { - SDL_strlcpy(assetDir, zip_data, sizeof(assetDir)); - } + + SDL_strlcpy(assetDir, zip_data, sizeof(assetDir)); graphics.reloadresources(); } @@ -340,7 +343,10 @@ void FILESYSTEM_mountassets(const char* path) { printf("Custom asset directory exists at %s\n", dir); - FILESYSTEM_mount(dir); + if (!FILESYSTEM_mount(dir)) + { + return; + } graphics.reloadresources(); }