Skip to content

Commit f51b78a

Browse files
author
dgengin
committed
- fallback to lowercase names if file not found
1 parent 6c499d9 commit f51b78a

4 files changed

Lines changed: 51 additions & 3 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
/CMakeCache.txt
1717
/CMakeFiles
1818
/cmake_install.cmake
19+
/d2char
20+
/d2data
21+
/d2music
22+
/d2sfx
23+
/d2speech
24+
/d2video
1925
/Debug
2026
/Debug Clang
2127
/Debug Code Analysis

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")
1515

1616
option(DGENGINE_MOVIE_SUPPORT "Enable Movie support" TRUE)
1717
option(DGENGINE_DIABLO_FORMAT_SUPPORT "Enable Diablo 1-2 file format support" TRUE)
18+
option(DGENGINE_FALLBACK_TO_LOWERCASE_FILENAME "Enable falling back to all lowercase names if file is not found" TRUE)
1819

1920
if(DGENGINE_MOVIE_SUPPORT)
2021
find_package(FFmpeg COMPONENTS avcodec avformat avutil swscale)
@@ -422,6 +423,10 @@ else()
422423
add_definitions(-DNO_DIABLO_FORMAT_SUPPORT)
423424
endif()
424425

426+
if(DGENGINE_FALLBACK_TO_LOWERCASE_FILENAME)
427+
add_definitions(-DFALLBACK_TO_LOWERCASE_FILENAME)
428+
endif()
429+
425430
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
426431

427432
target_link_libraries(${PROJECT_NAME} stdc++fs)

src/PhysFSStream.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,22 @@
2020
//distribution.
2121

2222
#include "PhysFSStream.h"
23+
#include "Utils/Utils.h"
24+
25+
#ifdef FALLBACK_TO_LOWERCASE_FILENAME
26+
static constexpr bool FALLBACK_TO_LOWERCASE = false;
27+
#else
28+
static constexpr bool FALLBACK_TO_LOWERCASE = true;
29+
#endif
30+
31+
sf::PhysFSStream::PhysFSStream(const std::string& fileName)
32+
{
33+
load(fileName);
34+
}
2335

2436
sf::PhysFSStream::PhysFSStream(const char* fileName)
2537
{
26-
file = PHYSFS_openRead(fileName);
38+
load(fileName);
2739
}
2840

2941
sf::PhysFSStream::~PhysFSStream()
@@ -34,12 +46,37 @@ sf::PhysFSStream::~PhysFSStream()
3446
}
3547
}
3648

49+
bool sf::PhysFSStream::load(const std::string& fileName)
50+
{
51+
if (file == nullptr)
52+
{
53+
file = PHYSFS_openRead(fileName.c_str());
54+
}
55+
if constexpr (FALLBACK_TO_LOWERCASE == true)
56+
{
57+
if (file == nullptr)
58+
{
59+
auto lowerCaseFileName = Utils::toLower(fileName);
60+
file = PHYSFS_openRead(lowerCaseFileName.c_str());
61+
}
62+
}
63+
return (file != nullptr);
64+
}
65+
3766
bool sf::PhysFSStream::load(const char* fileName)
3867
{
3968
if (file == nullptr)
4069
{
4170
file = PHYSFS_openRead(fileName);
4271
}
72+
if constexpr (FALLBACK_TO_LOWERCASE == true)
73+
{
74+
if (file == nullptr)
75+
{
76+
auto lowerCaseFileName = Utils::toLower(fileName);
77+
file = PHYSFS_openRead(lowerCaseFileName.c_str());
78+
}
79+
}
4380
return (file != nullptr);
4481
}
4582

src/PhysFSStream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ namespace sf
3333
PHYSFS_File* file;
3434

3535
public:
36-
PhysFSStream(const std::string& fileName) : PhysFSStream(fileName.c_str()) {}
36+
PhysFSStream(const std::string& fileName);
3737
PhysFSStream(const char* fileName);
3838
~PhysFSStream() override;
3939

40-
bool load(const std::string& fileName) { return load(fileName.c_str()); }
40+
bool load(const std::string& fileName);
4141
bool load(const char* fileName);
4242

4343
sf::Int64 read(void* data, sf::Int64 size) noexcept override;

0 commit comments

Comments
 (0)