diff --git a/audio/audio.cpp b/audio/audio.cpp index c9aa56675..ad53d95de 100644 --- a/audio/audio.cpp +++ b/audio/audio.cpp @@ -35,7 +35,8 @@ decoded_sound decode_sound( std::string const &File ) { SF_INFO si; si.format = 0; - SNDFILE *sf = sf_open(File.c_str(), SFM_READ, &si); + // the file as it's named on the disk, the name of the buffer is in lowercase + SNDFILE *sf = sf_open(FileOnDisk(File).c_str(), SFM_READ, &si); if (sf == nullptr) throw std::runtime_error("sound: sf_open failed"); diff --git a/model/Texture.cpp b/model/Texture.cpp index e09e8c8ab..96fb3778d 100644 --- a/model/Texture.cpp +++ b/model/Texture.cpp @@ -549,15 +549,13 @@ opengl_texture::load() { data_state = resource_state::loading; - // the extension as the file has it on the disk, in any case - auto const format { ToLower( type ) }; - if( format == ".dds" ) { load_DDS(); } - else if( format == ".tga" ) { load_TGA(); } - else if( format == ".png" ) { load_STBI(); } - else if( format == ".ktx" ) { load_KTX(); } - else if( format == ".bmp" ) { load_STBI(); } - else if( format == ".jpg" ) { load_STBI(); } - else if( format == ".tex" ) { load_TEX(); } + if( type == ".dds" ) { load_DDS(); } + else if( type == ".tga" ) { load_TGA(); } + else if( type == ".png" ) { load_STBI(); } + else if( type == ".ktx" ) { load_KTX(); } + else if( type == ".bmp" ) { load_STBI(); } + else if( type == ".jpg" ) { load_STBI(); } + else if( type == ".tex" ) { load_TEX(); } else { goto fail; } } @@ -642,7 +640,7 @@ void opengl_texture::load_STBI() { int x, y, n; stbi_set_flip_vertically_on_load(1); - uint8_t *image = stbi_load((name + type).c_str(), &x, &y, &n, 4); + uint8_t *image = stbi_load(FileOnDisk(name + type).c_str(), &x, &y, &n, 4); if (!image) { data_state = resource_state::failed; @@ -820,7 +818,7 @@ DDSURFACEDESC2 opengl_texture::deserialize_ddsd(std::istream &s) void opengl_texture::load_DDS() { - std::ifstream file( name + type, std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws ); + std::ifstream file( FileOnDisk( name + type ), std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws ); std::size_t filesize = static_cast(file.tellg()); // ios::ate already positioned us at the end of the file file.seekg( 0, std::ios::beg ); // rewind the caret afterwards @@ -940,7 +938,7 @@ opengl_texture::load_DDS() { if( true == Global.gfx_texture_streaming ) { // the data is read by a worker thread, everything else about the texture is known already request = std::make_shared(); - request->file = name + type; + request->file = FileOnDisk( name + type ); request->offset = static_cast( file.tellg() ) + offset; request->size = datasize; request->flip = flip; @@ -1002,7 +1000,7 @@ opengl_texture::load_DDS() { void opengl_texture::load_KTX() { - std::ifstream file( name + type, std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws ); + std::ifstream file( FileOnDisk( name + type ), std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws ); std::size_t filesize = static_cast(file.tellg()); // ios::ate already positioned us at the end of the file file.seekg( 0, std::ios::beg ); // rewind the caret afterwards @@ -1063,7 +1061,7 @@ opengl_texture::load_KTX() { void opengl_texture::load_TEX() { - std::ifstream file( name + type, std::ios::binary ); file.unsetf( std::ios::skipws ); + std::ifstream file( FileOnDisk( name + type ), std::ios::binary ); file.unsetf( std::ios::skipws ); char head[ 5 ]; file.read( head, 4 ); @@ -1110,7 +1108,7 @@ opengl_texture::load_TEX() { void opengl_texture::load_TGA() { - std::ifstream file( name + type, std::ios::binary ); file.unsetf( std::ios::skipws ); + std::ifstream file( FileOnDisk( name + type ), std::ios::binary ); file.unsetf( std::ios::skipws ); // Read the header of the TGA, compare it with the known headers for compressed and uncompressed TGAs unsigned char tgaheader[ 18 ]; diff --git a/scripting/PyInt.cpp b/scripting/PyInt.cpp index 666609b0e..fac540019 100644 --- a/scripting/PyInt.cpp +++ b/scripting/PyInt.cpp @@ -443,7 +443,7 @@ auto python_taskqueue::run_file(std::string const &File, std::string const &Path return false; } - std::ifstream inputfile{filepath + fileextension}; + std::ifstream inputfile{FileOnDisk(filepath + fileextension)}; std::string input; input.assign(std::istreambuf_iterator(inputfile), std::istreambuf_iterator()); diff --git a/tests/unit/test_utilities.cpp b/tests/unit/test_utilities.cpp index b8ad0b95c..45a919568 100644 --- a/tests/unit/test_utilities.cpp +++ b/tests/unit/test_utilities.cpp @@ -586,28 +586,28 @@ TEST_CASE("utilities: a file is found in folders named in another case, the way // every part of the path in lowercase: the file as it's on the disk where the case matters, the name given where it doesn't auto const timetable{FileCased(folder.path("scenery/braniewo/rozklady/os91121.txt"))}; REQUIRE_FALSE(timetable.empty()); - CHECK(FileExists(timetable)); + CHECK(std::filesystem::exists(timetable)); CHECK(ToLower(timetable) == ToLower(folder.path("Scenery/Braniewo/rozklady/Os91121.txt"))); - CHECK(FileExists(folder.path("scenery/braniewo/rozklady/os91121.txt"))); + CHECK(FileOnDisk(folder.path("scenery/braniewo/rozklady/os91121.txt")) == timetable); // the folders written the Windows way as well CHECK_FALSE(FileCased(folder.path() + "scenery\\braniewo\\rozklady\\os91121.txt").empty()); // a folder missing in any case CHECK(FileCased(folder.path("scenery/nowhere/rozklady/os91121.txt")).empty()); - CHECK_FALSE(FileExists(folder.path("scenery/nowhere/rozklady/os91121.txt"))); + CHECK(FileOnDisk(folder.path("scenery/nowhere/rozklady/os91121.txt")) == folder.path("scenery/nowhere/rozklady/os91121.txt")); - // among names and extensions: given back as named on the disk, so it's opened by that name, the extension too + // among names and extensions: found in any case, given back as named, the name being the key of the banks auto const texture{FileExists(std::vector{folder.path("textures/tabl/l101-2m")}, std::vector{".dds", ".bmp"})}; REQUIRE_FALSE(texture.first.empty()); - CHECK(ToLower(texture.second) == ".bmp"); - CHECK(FileExists(texture.first + texture.second)); - CHECK(ToLower(texture.first) == ToLower(folder.path("Textures/Tabl/L101-2m"))); + CHECK(texture.first == folder.path("textures/tabl/l101-2m")); + CHECK(texture.second == ".bmp"); + CHECK(std::filesystem::exists(FileOnDisk(texture.first + texture.second))); // a file written after the folder was gone through is found as well CHECK(FileCased(folder.path("textures/tabl/l7-2m.bmp")).empty()); folder.write("Textures/Tabl/L7-2m.bmp", "x"); auto const added{FileCased(folder.path("textures/tabl/l7-2m.bmp"))}; REQUIRE_FALSE(added.empty()); - CHECK(FileExists(added)); + CHECK(std::filesystem::exists(added)); } TEST_CASE("utilities: the time a file was changed is given, zero for a file which isn't there") { diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index 85b40d330..deb63f2bd 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -513,22 +513,19 @@ template <> bool extract_value(bool &Variable, std::string const &Key, std::stri bool FileExists(std::string const &Filename) { -#ifdef _WIN32 return std::filesystem::exists(Filename); -#else - // the names come in lowercase from the parser, the files of the data are named in any case - return false == FileCased(Filename).empty(); -#endif } #ifndef _WIN32 namespace { -// the names in a folder by their lowercase, and the time the folder was changed when they were read +// the names in a folder by their lowercase, the time the folder was changed and the time they were read struct cased_folder { + bool read{false}; std::filesystem::file_time_type changed{}; + std::filesystem::file_time_type listed{}; std::unordered_map names; }; @@ -536,7 +533,8 @@ std::mutex cased_mutex; std::unordered_map cased_folders; // the name in the folder matching the one given in any case, empty when there's none. the folder is read again once it was -// changed, as the editor writes files while it runs. to be called with cased_mutex held +// changed, as the editor writes files while it runs; a name missing from a folder changed shortly before it was read is looked +// for again, the time of the change being as coarse as the clock of the file system. to be called with cased_mutex held std::string cased_name(std::string const &Folder, std::string const &Name) { std::filesystem::path const folder{Folder.empty() ? std::string{"."} : Folder}; @@ -545,17 +543,27 @@ std::string cased_name(std::string const &Folder, std::string const &Name) if (error) return {}; auto &cached{cased_folders[Folder]}; - if (cached.names.empty() || cached.changed != changed) - { - cached.changed = changed; + auto const wanted{ToLower(Name)}; + auto const read{[&]() { cached.names.clear(); + cached.changed = changed; + cached.listed = std::filesystem::file_time_type::clock::now(); for (std::filesystem::directory_iterator entry{folder, error}, end; (false == bool(error)) && (entry != end); entry.increment(error)) { auto const name{entry->path().filename().string()}; cached.names.try_emplace(ToLower(name), name); } + // a listing cut short isn't kept + cached.read = (false == bool(error)); + }}; + if ((false == cached.read) || (cached.changed != changed)) + read(); + auto found{cached.names.find(wanted)}; + if ((found == cached.names.end()) && (cached.listed - cached.changed < std::chrono::seconds{2})) + { + read(); + found = cached.names.find(wanted); } - auto const found{cached.names.find(ToLower(Name))}; return found != cached.names.end() ? found->second : std::string{}; } @@ -609,6 +617,12 @@ std::string FileCased(std::filesystem::path const &Filename) #endif } +std::string FileOnDisk(std::string const &Filename) +{ + auto cased{FileCased(Filename)}; + return cased.empty() ? Filename : cased; +} + std::pair FileExists(std::vector const &Names, std::vector const &Extensions) { @@ -616,19 +630,11 @@ std::pair FileExists(std::vector const &N { for (auto const &extension : Extensions) { -#ifdef _WIN32 - if (FileExists(name + extension)) + // found in any case, given back as named here: the name stays the key of the banks, the file is opened by FileOnDisk() + if (false == FileCased(name + extension).empty()) { return {name, extension}; } -#else - // given back as it's named on the disk, so it's opened by that name - if (auto const cased{FileCased(name + extension)}; cased.size() >= extension.size()) - { - auto const split{cased.size() - extension.size()}; - return {cased.substr(0, split), cased.substr(split)}; - } -#endif } } // nothing found diff --git a/utilities/utilities.h b/utilities/utilities.h index 2e347b933..d15e03234 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -225,8 +225,11 @@ bool FileExists(std::string const &Filename); // the file as it's named on the disk when only the case of its name or of its folders differs, as in the sceneries made on Windows; // empty when there's none. on Windows, finding the names in any case itself, the file given or none std::string FileCased(std::filesystem::path const &Filename); +// the file to open: as it's named on the disk, the name given when there's none +std::string FileOnDisk(std::string const &Filename); -// the first name with one of the extensions which has a file, in the order given, as the file is named on the disk +// the first name with one of the extensions which has a file in any case, in the order given; given back as named here, the file +// is opened by FileOnDisk() std::pair FileExists(std::vector const &Names, std::vector const &Extensions); // returns time of last modification for specified file