diff --git a/application/application.cpp b/application/application.cpp index 66192746d..22293bb3c 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -922,6 +922,9 @@ void eu07_application::exit() auto const step = [closing](char const *What) { WriteLog(std::format("Closing: {} at {} ms", What, std::chrono::duration_cast(std::chrono::steady_clock::now() - closing).count())); }; + // the files found in another case, counted over the whole run: the vehicles, sounds and textures loaded after the scenery + // count as well as those loaded with it + LogFilesInAnotherCase(); step("the sleepers laid in the background"); // the sleepers laid in the background read the scenery, which goes below track_sleepers::stop_background(); diff --git a/application/editormode.cpp b/application/editormode.cpp index 7c9231d2b..e5144b410 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -8684,7 +8684,8 @@ void editor_mode::starter_trainset(std::vector &Trailing) auto const &layer{scene::Layers.layer(static_cast(handle))}; if (layer.created) continue; - std::ifstream input{Global.asCurrentSceneryPath + layer.name, std::ios_base::binary}; + // the file as it's named on the disk, as node_layers::path() reads it + std::ifstream input{FileOnDisk(Global.asCurrentSceneryPath + layer.name), std::ios_base::binary}; std::string const content{std::istreambuf_iterator(input), std::istreambuf_iterator()}; if (std::regex_search(content, trainset)) return; diff --git a/editor/editorTerrainStreamer.cpp b/editor/editorTerrainStreamer.cpp index a8f569aaa..d887b14b9 100644 --- a/editor/editorTerrainStreamer.cpp +++ b/editor/editorTerrainStreamer.cpp @@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/. #include "simulation/simulation.h" #include "utilities/Globals.h" #include "utilities/Logs.h" +#include "utilities/utilities.h" #include #include @@ -99,17 +100,19 @@ terrain_streamer::~terrain_streamer() std::string terrain_streamer::pack_path(heightmap::pack_key const &Key) const { - return m_directory + heightmap::pack_file_name(Key); + // read and written as it's named on the disk + return FileOnDisk(m_directory + heightmap::pack_file_name(Key)); } bool terrain_streamer::open(std::string const &Name) { close(); m_name = Name; - m_directory = "terrain/" + Name + "/"; + // the folder as it's named on the disk, the editor writes the files of the terrain into it + m_directory = FileOnDiskToWrite("terrain/" + Name) + "/"; m_manifest = heightmap::manifest(); m_manifestchanged = false; - auto const manifestpath{m_directory + "terrain.yaml"}; + auto const manifestpath{FileOnDisk(m_directory + "terrain.yaml")}; std::error_code ec; if (std::filesystem::exists(manifestpath, ec)) { @@ -2306,7 +2309,8 @@ bool terrain_streamer::save(std::string *Error) return false; }; std::error_code ec; - auto const manifestpath{m_directory + "terrain.yaml"}; + // written over as it's named on the disk, not joined by a second one next to it + auto const manifestpath{FileOnDisk(m_directory + "terrain.yaml")}; if (m_manifestchanged || modified() || false == std::filesystem::exists(manifestpath, ec)) { std::string error; diff --git a/model/Model3d.cpp b/model/Model3d.cpp index a17ed2718..b9937a77c 100644 --- a/model/Model3d.cpp +++ b/model/Model3d.cpp @@ -1876,6 +1876,11 @@ bool TModel3d::LoadFromFile(std::string const &FileName, bool dynamic) // the files as they're named on the disk, the name given is in lowercase auto const textfile{FileCased(name + ".t3d")}; auto const binaryfile{FileCased(asBinary)}; + // the binary file made of the text one: written over where it's there in another case, made next to the text otherwise + if (false == binaryfile.empty()) + asBinary = binaryfile; + else if (false == textfile.empty()) + asBinary = textfile.substr(0, textfile.size() - 4) + ".e3d"; // Hirek: Jesli mamy ustawione priorityLoadText3D na yes to wpierw ladujemy t3d if (Global.priorityLoadText3D && (false == textfile.empty())) diff --git a/scene/scene.cpp b/scene/scene.cpp index 03b10d8f1..f997e9762 100644 --- a/scene/scene.cpp +++ b/scene/scene.cpp @@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/. #include "world/EvLaunch.h" #include "utilities/Timer.h" #include "utilities/Logs.h" +#include "utilities/utilities.h" #include "scene/sn_utils.h" #include "scene/regionfile.h" #include "scene/sceneterrain.h" @@ -1302,7 +1303,8 @@ basic_region::wire_over( glm::dvec3 const &Point, glm::dvec3 const &Front, doubl bool basic_region::is_scene( std::string const &Scenariofile ) const { - auto const filename { region_file_name( Scenariofile ) }; + // the file as it's named on the disk, the names the scenery includes come in lowercase + auto const filename { FileOnDisk( region_file_name( Scenariofile ) ) }; if( false == FileExists( filename ) ) { return false; @@ -1323,7 +1325,8 @@ basic_region::is_scene( std::string const &Scenariofile ) const { void basic_region::serialize( std::string const &Scenariofile ) const { - auto const filename { region_file_name( Scenariofile ) }; + // a file there in another case is written over, not joined by a second one next to it + auto const filename { FileOnDiskToWrite( region_file_name( Scenariofile ) ) }; // the file is written under a temporary name and takes the final one only when complete, so a write cut short // (a crash, the process killed) leaves no partial file to be read on the next start @@ -1359,7 +1362,8 @@ basic_region::serialize( std::string const &Scenariofile ) const { bool basic_region::deserialize( std::string const &Scenariofile ) { - auto const filename { region_file_name( Scenariofile ) }; + // the file as it's named on the disk, the names the scenery includes come in lowercase + auto const filename { FileOnDisk( region_file_name( Scenariofile ) ) }; if( false == FileExists( filename ) ) { Global.file_binary_terrain_state = false; diff --git a/scene/scenelayers.cpp b/scene/scenelayers.cpp index e729fac7b..e7a72f3a9 100644 --- a/scene/scenelayers.cpp +++ b/scene/scenelayers.cpp @@ -559,9 +559,8 @@ bool node_layers::is_descendant(layer_handle Layer, layer_handle const Ancestor) std::string node_layers::path(layer_handle const Layer) const { - auto const named{Global.asCurrentSceneryPath + layer(Layer).name}; - auto const cased{FileCased(named)}; - return cased.empty() ? named : cased; + // read and written as it's named on the disk; a new file is made in the folders as they're named there + return FileOnDiskToWrite(Global.asCurrentSceneryPath + layer(Layer).name); } // stores current state of the layer file, to detect later whether it was changed behind our back @@ -619,7 +618,8 @@ layer_handle node_layers::create(std::string Name, layer_handle Parent, std::str return null_handle; } auto const taken{std::any_of(std::begin(m_layers), std::end(m_layers), [&](basic_layer const &Layer) { return Layer.name == Name && false == Layer.dead; })}; - if (taken || FileExists(Global.asCurrentSceneryPath + Name)) + // a file there in another case counts as well, path() would write over it + if (taken || false == FileCased(Global.asCurrentSceneryPath + Name).empty()) { Reason = format(STR_C("the file \"%s\" already exists"), Name.c_str()); return null_handle; diff --git a/scene/scenelayerssave.cpp b/scene/scenelayerssave.cpp index 5d52689e9..06fb75423 100644 --- a/scene/scenelayerssave.cpp +++ b/scene/scenelayerssave.cpp @@ -1986,7 +1986,7 @@ save_result node_layers::save(std::vector const &Rootstatements, st terrainfile.erase(0, 1); } erase_extension(terrainfile); - if (std::filesystem::remove(Global.asCurrentSceneryPath + terrainfile + ".sbt", error)) + if (std::filesystem::remove(FileOnDisk(Global.asCurrentSceneryPath + terrainfile + ".sbt"), error)) { WriteLog("Scenery save: binary terrain file removed, it'll be generated again on the next load"); } diff --git a/scripting/luasandbox.cpp b/scripting/luasandbox.cpp index 16e7f70ba..13b1c1bc5 100644 --- a/scripting/luasandbox.cpp +++ b/scripting/luasandbox.cpp @@ -11,6 +11,7 @@ http://mozilla.org/MPL/2.0/. #include "scripting/luasandbox.h" #include "scripting/scriptscreenrules.h" +#include "utilities/utilities.h" #include #include @@ -134,7 +135,12 @@ std::optional read_file(std::filesystem::path const &Folder, std::s } // a folder or a device (CON, NUL, COM1...) isn't a regular file. in the code page of the system, as the game opens // its files - auto const path{Folder / with_forward_slashes(Path)}; + auto path{Folder / with_forward_slashes(Path)}; +#ifndef _WIN32 + // named in another case on the disk, as the game finds its other files (on Windows the name is found as it is, and + // the folder of the player's files may be named outside of the code page) + path = FileOnDisk(path.string()); +#endif std::error_code error; if (false == std::filesystem::is_regular_file(path, error)) { diff --git a/scripting/scriptscreens.cpp b/scripting/scriptscreens.cpp index 224a2f27e..47c35bc4b 100644 --- a/scripting/scriptscreens.cpp +++ b/scripting/scriptscreens.cpp @@ -108,8 +108,8 @@ std::optional read_regular_file(std::string const &Path) { // a folder or a device (CON, NUL, COM1...) isn't a regular file std::error_code error; - // in the code page of the system, as the game opens its files - std::filesystem::path const path{Path}; + // in the code page of the system, as the game opens its files; named in another case on the disk on Linux + std::filesystem::path const path{FileOnDisk(Path)}; if (false == std::filesystem::is_regular_file(path, error)) { return std::nullopt; diff --git a/tests/unit/logcapture.h b/tests/unit/logcapture.h new file mode 100644 index 000000000..393f74e98 --- /dev/null +++ b/tests/unit/logcapture.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +// engine_stubs.cpp: keeps every line logged from now on (true), or stops keeping them and forgets them (false), and +// the lines kept so far +void unit_tests_log_capture(bool Enabled); +std::vector unit_tests_logged_lines(); + +namespace test { + +// keeps the lines written to the log while it's alive, so a test can tell what was logged. one at a time +class log_capture { + public: + log_capture() { unit_tests_log_capture(true); } + ~log_capture() { unit_tests_log_capture(false); } + log_capture(log_capture const &) = delete; + log_capture &operator=(log_capture const &) = delete; + + // the lines written so far + std::vector lines() const { return unit_tests_logged_lines(); } +}; + +} // namespace test diff --git a/tests/unit/test_lua_sandbox.cpp b/tests/unit/test_lua_sandbox.cpp index bc0753162..80b1e0294 100644 --- a/tests/unit/test_lua_sandbox.cpp +++ b/tests/unit/test_lua_sandbox.cpp @@ -161,6 +161,20 @@ TEST_CASE("lua sandbox: a scenery reads its scripts only from its own folder") { } } +TEST_CASE("lua sandbox: a script and a file of the scenery named in another case are read (GE#542)") { + test::scratch_folder const root{"lua_sandbox_case"}; + root.write("scenery/Scripts/Module.LUA", "return 5"); + root.write("scenery/Data.TXT", "text"); + auto const folder{root.path("scenery") + "/"}; + // the names as the scenery asks for them, the folder and the files named in another case on the disk + CHECK(lua_sandbox::read_script_file(folder, "scripts/module.lua") == std::optional{"return 5"}); + lua_sandbox::file_folders const folders{folder, {}}; + sandboxed_state const lua; + lua_sandbox::open_file_library(lua.get(), &folders); + CHECK(lua.run("return eu07.read_file('data.txt')") == "text"); + CHECK(lua.run("return eu07.read_file('nothing.txt')") == "nil"); +} + TEST_CASE("lua sandbox: a script larger than the limit isn't read") { test::scratch_folder const root{"lua_sandbox_size"}; root.write("scenery/limit.lua", std::string(static_cast(lua_sandbox::max_script_file), ' ')); diff --git a/tests/unit/test_script_screens.cpp b/tests/unit/test_script_screens.cpp index af8678f2e..a2de5ce95 100644 --- a/tests/unit/test_script_screens.cpp +++ b/tests/unit/test_script_screens.cpp @@ -1,6 +1,8 @@ #include "stdafx.h" #include "doctest.h" +#include "scratch.h" +#include "scripting/scriptscreenprogram.h" #include "scripting/scriptscreenrules.h" #include "utilities/parser.h" @@ -244,3 +246,13 @@ TEST_CASE("script screens: a repetition right before the final $ counts its long CHECK(lua_pattern_cost("[ ]*$", std::string_view{words}, false, true) == doctest::Approx((words.size() + 1.0) * (words.size() + 1.0) * 5)); CHECK(lua_pattern_cost(".-b", std::string(1'000'000, 'a'), false, true) > max_pattern_cost); } + +TEST_CASE("script screens: a file of the screen's folder named in another case is read (GE#542)") { + test::scratch_folder const folder{"screen_file_case"}; + folder.write("Font/Test.PNG", "png"); + // the name as the screen asks for it, the folder and the file named in another case on the disk + auto const content{script_screens::read_screen_file(folder.path(), "font/test.png")}; + REQUIRE(content.has_value()); + CHECK(*content == "png"); + CHECK_FALSE(script_screens::read_screen_file(folder.path(), "font/nothing.png").has_value()); +} diff --git a/tests/unit/test_utilities.cpp b/tests/unit/test_utilities.cpp index d9309fa46..0986519e8 100644 --- a/tests/unit/test_utilities.cpp +++ b/tests/unit/test_utilities.cpp @@ -1,5 +1,6 @@ #include "stdafx.h" #include "doctest.h" +#include "logcapture.h" #include "scratch.h" #include @@ -30,6 +31,16 @@ struct debug_mode { ~debug_mode() { DebugModeFlag = before; } }; +// puts logs.filecase back as it was +struct file_case_log { + bool const before{Global.FileCaseLog}; + + explicit file_case_log(bool const Enabled) { Global.FileCaseLog = Enabled; } + file_case_log(file_case_log const &) = delete; + file_case_log &operator=(file_case_log const &) = delete; + ~file_case_log() { Global.FileCaseLog = before; } +}; + // the polish letters in windows-1250, as in test_win1250.cpp std::string const kWin1250{"\xA5\xC6\xCA\xA3\xD1\xD3\x8C\x8F\xAF\xB9\xE6\xEA\xB3\xF1\xF3\x9C\x9F\xBF"}; @@ -716,6 +727,98 @@ TEST_CASE("utilities: a terrain file is found in another case, by its text or by CHECK(FileStemOnDisk(scenery, "nmt/nowhere_ter", extensions) == "nmt/nowhere_ter"); } +TEST_CASE("utilities: a file is written over in another case, or made in its folders as they're named on the disk") { + test::scratch_folder const folder{"utilities_cased_write"}; + folder.write("Scenery/Braniewo/Base.scm", "x"); + // on Windows the names are given back as asked, the file system finding them in any case; elsewhere as they're on the disk +#ifdef _WIN32 + bool const cased{false}; +#else + bool const cased{true}; +#endif + + // a file there in another case: written under its name on the disk, not joined by a second one next to it + auto const existing{FileOnDiskToWrite(folder.path("scenery/braniewo/base.scm"))}; + CHECK(existing == folder.path(cased ? "Scenery/Braniewo/Base.scm" : "scenery/braniewo/base.scm")); + CHECK(std::filesystem::exists(existing)); + // a new file: the name given, in the folders as they're on the disk, so no second folder is made next to the one there + auto const added{FileOnDiskToWrite(folder.path("scenery/braniewo/nowa.scm"))}; + CHECK(added == folder.path(cased ? "Scenery/Braniewo/nowa.scm" : "scenery/braniewo/nowa.scm")); + CHECK(added.ends_with("/nowa.scm")); + CHECK(std::filesystem::is_directory(std::filesystem::path{added}.parent_path())); + // folders missing in any case: named as given, to be made in the nearest one there is + auto const deeper{FileOnDiskToWrite(folder.path("scenery/braniewo/teren/nowy/x.btf"))}; + CHECK(deeper == folder.path(cased ? "Scenery/Braniewo/teren/nowy/x.btf" : "scenery/braniewo/teren/nowy/x.btf")); + CHECK(deeper.ends_with("/teren/nowy/x.btf")); + CHECK(std::filesystem::is_directory(std::filesystem::path{deeper}.parent_path().parent_path().parent_path())); + // the folders written the Windows way as well + auto const backslashed{FileOnDiskToWrite(folder.path() + "scenery\\braniewo\\nowa.scm")}; + CHECK(backslashed.ends_with("/nowa.scm")); + CHECK(std::filesystem::is_directory(std::filesystem::path{backslashed}.parent_path())); + // a name with no folder, and none there + CHECK(FileOnDiskToWrite("eu07_unit_test_no_such_file.xyz") == "eu07_unit_test_no_such_file.xyz"); +} + +TEST_CASE("utilities: a file found only in another case is logged once with logs.filecase, the folders of a file to write are not") { + test::scratch_folder const folder{"utilities_cased_log"}; + folder.write("Scenery/Braniewo/Base.scm", "x"); + file_case_log const opted{true}; + test::log_capture const log; + + // asked for again, and to be written over + CHECK_FALSE(FileCased(folder.path("scenery/braniewo/base.scm")).empty()); + CHECK_FALSE(FileCased(folder.path("scenery/braniewo/base.scm")).empty()); + CHECK_FALSE(FileOnDiskToWrite(folder.path("scenery/braniewo/base.scm")).empty()); + // asked for in its case on the disk + CHECK_FALSE(FileCased(folder.path("Scenery/Braniewo/Base.scm")).empty()); + // a new file, in folders named in another case + CHECK_FALSE(FileOnDiskToWrite(folder.path("scenery/braniewo/nowa.scm")).empty()); + CHECK_FALSE(FileOnDiskToWrite(folder.path("scenery/braniewo/teren/nowa.scm")).empty()); + + auto const lines{log.lines()}; +#ifdef _WIN32 + // the file system finds the names in any case, the name asked for is the one given back: nothing to log + CHECK(lines.empty()); +#else + REQUIRE(lines.size() == 1); + CHECK(lines.front().find(folder.path("Scenery/Braniewo/Base.scm")) != std::string::npos); +#endif +} + +TEST_CASE("utilities: the files found only in another case are counted, and without logs.filecase only the count is logged") { + test::scratch_folder const folder{"utilities_cased_count"}; + folder.write("Scenery/Kozki/Base.scm", "x"); + folder.write("Textures/Kozki/Tor.dds", "x"); + file_case_log const opted{false}; + test::log_capture const log; + [[maybe_unused]] auto const before{FilesInAnotherCase()}; + + // two files in another case, one asked for twice, one asked for in its own case, one which isn't there in any case + CHECK_FALSE(FileCased(folder.path("scenery/kozki/base.scm")).empty()); + CHECK_FALSE(FileCased(folder.path("scenery/kozki/base.scm")).empty()); + CHECK_FALSE(FileOnDiskToWrite(folder.path("textures/kozki/tor.dds")).empty()); + CHECK_FALSE(FileCased(folder.path("Scenery/Kozki/Base.scm")).empty()); + CHECK(FileCased(folder.path("scenery/kozki/nothing.scm")).empty()); + // not one of them on its own + CHECK(log.lines().empty()); + + LogFilesInAnotherCase(); + auto const lines{log.lines()}; +#ifdef _WIN32 + // nothing found in another case, nothing to count nor to log + CHECK(FilesInAnotherCase() == 0); + CHECK(lines.empty()); +#else + auto const count{FilesInAnotherCase()}; + CHECK(count == before + 2); + // the one line, with the count of the whole run so far and the way to have them listed + REQUIRE(lines.size() == 1); + CHECK(lines.front().find(": " + std::to_string(count) + " (") != std::string::npos); + CHECK(lines.front().find("logs.filecase yes") != std::string::npos); + CHECK(lines.front().find("Kozki") == std::string::npos); +#endif +} + TEST_CASE("utilities: the time a file was changed is given, zero for a file which isn't there") { test::scratch_folder const folder{"utilities_modified"}; auto const name{folder.write("fresh.txt", "x")}; diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 4ec2f1197..6f7749517 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -1550,6 +1550,12 @@ bool global_settings::ConfigParseDebug(cParser& Parser, const std::string& token return true; } + if (token == "logs.filecase") + { + ParseOne(Parser, FileCaseLog); + return true; + } + if (token == "rollfix") { ParseOne(Parser, bRollFix); @@ -2551,6 +2557,7 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "crashdump.full", crashdump_full ); export_as_text( Output, "showsystemconsole", ShowSystemConsole ); export_as_text( Output, "logs.filter", DisabledLogTypes ); + export_as_text( Output, "logs.filecase", FileCaseLog ); Output << "mousescale " << fMouseXScale << " " diff --git a/utilities/Globals.h b/utilities/Globals.h index b35b15c8b..34d79cf16 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -130,6 +130,9 @@ struct global_settings { std::optional BlackBox; bool ShowSystemConsole{ false }; // toggle visibility of the OS console window with log output unsigned int DisabledLogTypes{ 0 }; + // logs.filecase: each file found on the disk only in another case than asked for (not on Windows) is written to the log; + // without it only their count is, once, at the closing + bool FileCaseLog{ false }; bool ParserLogIncludes{ true }; // simulation bool RealisticControlMode{ false }; // controls ability to steer the vehicle from outside views diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index d38e4baa0..4f74dfa68 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -19,6 +19,9 @@ Copyright (C) 2007-2014 Maciej Cierniak #include #include #include +#include +#include +#include //#ifndef WIN32 //#include //#endif @@ -753,6 +756,18 @@ struct cased_path std::chrono::steady_clock::time_point checked{}; }; std::unordered_map cased_paths; +// transparent hasher, so the names can be looked up with any string type without a temporary std::string +struct cased_hash +{ + using is_transparent = void; + std::size_t operator()(std::string_view const Text) const noexcept { return std::hash{}(Text); } +}; +// the files found only in another case than asked for, counted once each by the name asked for +std::unordered_set> &cased_reported() +{ + static std::unordered_set> reported; + return reported; +} // the names in the folder into Cached by their lowercase, with the time the folder was changed and the time they were read. a named // function rather than a lambda capturing Cached, which Sonar takes for a reference to null (S2259) @@ -861,7 +876,11 @@ bool cased_folder_path(std::string const &Folder, std::string &Cased) } // namespace #endif -std::string FileCased(std::filesystem::path const &Filename) +namespace +{ + +// FileCased(), the file found only in another case counted, and written to the log on request, when Report +std::string file_cased(std::filesystem::path const &Filename, [[maybe_unused]] bool const Report) { std::error_code error; if (std::filesystem::exists(Filename, error)) @@ -892,6 +911,7 @@ std::string FileCased(std::filesystem::path const &Filename) if (file.empty()) return {}; std::string cased; + auto const asked{folder + file}; { std::scoped_lock const lock{cased_mutex}; if (false == cased_folder_path(folder, cased)) @@ -901,16 +921,75 @@ std::string FileCased(std::filesystem::path const &Filename) return {}; cased += name; } - return std::filesystem::exists(cased, error) ? cased : std::string{}; + if (false == std::filesystem::exists(cased, error)) + return {}; + if (Report && (cased != asked)) + { + // counted once it's known to be there, and written to the log outside of the lock + bool first{false}; + { + std::scoped_lock const lock{cased_mutex}; + first = cased_reported().insert(asked).second; + } + // the parser asks for the names in lowercase, so these are mostly the files not named in lowercase on the disk. as they load + // fine, each is written to the log only on request (logs.filecase); their count is, at the closing (LogFilesInAnotherCase()) + if (first && Global.FileCaseLog) + WriteLog("File \"" + asked + "\" is \"" + cased + "\" on disk, in another case", logtype::file); + } + return cased; #endif } +// FileOnDiskToWrite(), the file found only in another case counted and logged as by file_cased() when Report; the folders never are +std::string on_disk_to_write(std::string const &Filename, bool const Report) +{ + if (auto const cased{file_cased(Filename, Report)}; false == cased.empty()) + return cased; + // a new file: its folder as it's named on the disk, or the nearest one on the way to it which is there, the rest as given + auto const split{Filename.find_last_of("/\\")}; + if ((split == std::string::npos) || (split == 0)) + return Filename; + return on_disk_to_write(Filename.substr(0, split), false) + '/' + Filename.substr(split + 1); +} + +} // namespace + +std::string FileCased(std::filesystem::path const &Filename) +{ + return file_cased(Filename, true); +} + std::string FileOnDisk(std::string const &Filename) { auto cased{FileCased(Filename)}; return cased.empty() ? Filename : cased; } +std::string FileOnDiskToWrite(std::string const &Filename) +{ + return on_disk_to_write(Filename, true); +} + +std::size_t FilesInAnotherCase() +{ +#ifdef _WIN32 + return 0; +#else + std::scoped_lock const lock{cased_mutex}; + return cased_reported().size(); +#endif +} + +void LogFilesInAnotherCase() +{ + auto const count{FilesInAnotherCase()}; + if (count == 0) + return; + WriteLog(std::format("Names asked for and found on disk only in another case: {}{}", count, + Global.FileCaseLog ? ", each logged above" : " (\"logs.filecase yes\" in eu07.ini logs each)"), + logtype::file); +} + std::string FileStemOnDisk(std::string const &Folder, std::string const &Name, std::vector const &Extensions) { for (auto const &extension : Extensions) diff --git a/utilities/utilities.h b/utilities/utilities.h index 588aff742..a71063694 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -259,10 +259,18 @@ template <> bool extract_value(bool &Variable, std::string const &Key, std::stri 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 +// empty when there's none. on Windows, finding the names in any case itself, the file given or none. a file found only in another +// case is counted, and written to the log once with logs.filecase in eu07.ini 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 file to write: as it's named on the disk when it's there in any case, otherwise a new file of the name given in the folders +// as they're named on the disk, those missing in any case named as given, to be made +std::string FileOnDiskToWrite(std::string const &Filename); +// how many names asked for so far were found on the disk only in another case; none on Windows +std::size_t FilesInAnotherCase(); +// one line with that count in the log, none when it's 0 +void LogFilesInAnotherCase(); // the name (relative to the folder, without extension) as it's named on the disk with the first of the extensions it has a file with, // found in any case; the name given when there's none std::string FileStemOnDisk(std::string const &Folder, std::string const &Name, std::vector const &Extensions); diff --git a/world/Event.cpp b/world/Event.cpp index 87cb7a0cd..dbbe3c1c1 100644 --- a/world/Event.cpp +++ b/world/Event.cpp @@ -33,6 +33,7 @@ http://mozilla.org/MPL/2.0/. #include "rendering/renderer.h" #include "utilities/Timer.h" #include "utilities/Logs.h" +#include "utilities/utilities.h" #include "widgets/map_objects.h" #include "physics/vehicle.h" @@ -1662,7 +1663,7 @@ animation_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) { // animacja z pliku VMD { m_animationfilename = token; - std::ifstream file( paths::models + m_animationfilename, std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws ); + std::ifstream file( FileOnDisk( paths::models + m_animationfilename ), std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws ); auto size = file.tellg(); // ios::ate already positioned us at the end of the file file.seekg( 0, std::ios::beg ); // rewind the caret afterwards // animation size, previously held in param 7 diff --git a/world/RailMaterials.cpp b/world/RailMaterials.cpp index 3ed0fa380..d5bda97bf 100644 --- a/world/RailMaterials.cpp +++ b/world/RailMaterials.cpp @@ -11,6 +11,7 @@ http://mozilla.org/MPL/2.0/. #include "world/TrackRails.h" #include "rendering/renderer.h" +#include "utilities/utilities.h" #include "stb/stb_image.h" namespace track_rails { @@ -92,7 +93,7 @@ int dxt_block_size( std::uint32_t const Fourcc ) { void sample_dds( std::string const &File, glm::vec2 const &Band, colour_mean &Mean ) { - std::ifstream input( File, std::ios::binary ); + std::ifstream input( FileOnDisk( File ), std::ios::binary ); std::array header {}; if( !input.read( reinterpret_cast( header.data() ), header.size() ) || false == std::equal( header.begin(), header.begin() + 4, "DDS " ) ) { return; @@ -132,7 +133,7 @@ void sample_image( std::string const &File, glm::vec2 const &Band, colour_mean & int width { 0 }; int height { 0 }; int components { 0 }; - auto *pixels { stbi_load( File.c_str(), &width, &height, &components, 4 ) }; + auto *pixels { stbi_load( FileOnDisk( File ).c_str(), &width, &height, &components, 4 ) }; if( pixels == nullptr ) { return; }