Skip to content

Commit

Permalink
- fallback to lowercase names if file not found
Browse files Browse the repository at this point in the history
  • Loading branch information
dgengin committed Nov 12, 2019
1 parent 6c499d9 commit f51b78a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -16,6 +16,12 @@
/CMakeCache.txt
/CMakeFiles
/cmake_install.cmake
/d2char
/d2data
/d2music
/d2sfx
/d2speech
/d2video
/Debug
/Debug Clang
/Debug Code Analysis
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 38 additions & 1 deletion src/PhysFSStream.cpp
Expand Up @@ -20,10 +20,22 @@
//distribution.

#include "PhysFSStream.h"
#include "Utils/Utils.h"

#ifdef FALLBACK_TO_LOWERCASE_FILENAME
static constexpr bool FALLBACK_TO_LOWERCASE = false;
#else
static constexpr bool FALLBACK_TO_LOWERCASE = true;
#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()
Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/PhysFSStream.h
Expand Up @@ -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;
Expand Down

0 comments on commit f51b78a

Please sign in to comment.