From 22ce98a40bf5a246a64ee61279734dd42c5560c7 Mon Sep 17 00:00:00 2001 From: dgengin Date: Tue, 12 Nov 2019 12:30:41 +0000 Subject: [PATCH] - fallback to lowercase names if file not found --- .gitignore | 6 ++++++ CMakeLists.txt | 5 +++++ src/PhysFSStream.cpp | 39 ++++++++++++++++++++++++++++++++++++++- src/PhysFSStream.h | 4 ++-- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 843d5039..fc27eba9 100755 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,12 @@ /CMakeCache.txt /CMakeFiles /cmake_install.cmake +/d2char +/d2data +/d2music +/d2sfx +/d2speech +/d2video /Debug /Debug Clang /Debug Code Analysis diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f5ab8a6..47225a50 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules") option(DGENGINE_MOVIE_SUPPORT "Enable Movie support" TRUE) option(DGENGINE_DIABLO_FORMAT_SUPPORT "Enable Diablo 1-2 file format support" TRUE) +option(DGENGINE_FALLBACK_TO_LOWERCASE_FILENAME "Enable falling back to all lowercase names if file is not found" TRUE) if(DGENGINE_MOVIE_SUPPORT) find_package(FFmpeg COMPONENTS avcodec avformat avutil swscale) @@ -422,6 +423,10 @@ else() add_definitions(-DNO_DIABLO_FORMAT_SUPPORT) endif() +if(DGENGINE_FALLBACK_TO_LOWERCASE_FILENAME) + add_definitions(-DFALLBACK_TO_LOWERCASE_FILENAME) +endif() + add_executable(${PROJECT_NAME} ${SOURCE_FILES}) target_link_libraries(${PROJECT_NAME} stdc++fs) diff --git a/src/PhysFSStream.cpp b/src/PhysFSStream.cpp index 1fc8b301..b2bbaaff 100755 --- a/src/PhysFSStream.cpp +++ b/src/PhysFSStream.cpp @@ -20,10 +20,22 @@ //distribution. #include "PhysFSStream.h" +#include "Utils/Utils.h" + +#ifdef FALLBACK_TO_LOWERCASE_FILENAME +static constexpr bool FALLBACK_TO_LOWERCASE = true; +#else +static constexpr bool FALLBACK_TO_LOWERCASE = false; +#endif + +sf::PhysFSStream::PhysFSStream(const std::string& fileName) +{ + load(fileName); +} sf::PhysFSStream::PhysFSStream(const char* fileName) { - file = PHYSFS_openRead(fileName); + load(fileName); } sf::PhysFSStream::~PhysFSStream() @@ -34,12 +46,37 @@ sf::PhysFSStream::~PhysFSStream() } } +bool sf::PhysFSStream::load(const std::string& fileName) +{ + if (file == nullptr) + { + file = PHYSFS_openRead(fileName.c_str()); + } + if constexpr (FALLBACK_TO_LOWERCASE == true) + { + if (file == nullptr) + { + auto lowerCaseFileName = Utils::toLower(fileName); + file = PHYSFS_openRead(lowerCaseFileName.c_str()); + } + } + return (file != nullptr); +} + bool sf::PhysFSStream::load(const char* fileName) { if (file == nullptr) { file = PHYSFS_openRead(fileName); } + if constexpr (FALLBACK_TO_LOWERCASE == true) + { + if (file == nullptr) + { + auto lowerCaseFileName = Utils::toLower(fileName); + file = PHYSFS_openRead(lowerCaseFileName.c_str()); + } + } return (file != nullptr); } diff --git a/src/PhysFSStream.h b/src/PhysFSStream.h index 5e561409..d3030616 100755 --- a/src/PhysFSStream.h +++ b/src/PhysFSStream.h @@ -33,11 +33,11 @@ namespace sf PHYSFS_File* file; public: - PhysFSStream(const std::string& fileName) : PhysFSStream(fileName.c_str()) {} + PhysFSStream(const std::string& fileName); PhysFSStream(const char* fileName); ~PhysFSStream() override; - bool load(const std::string& fileName) { return load(fileName.c_str()); } + bool load(const std::string& fileName); bool load(const char* fileName); sf::Int64 read(void* data, sf::Int64 size) noexcept override;