From d670cc2e53941f0ae2033e760d5ccc5f0870d33f Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Tue, 17 Nov 2026 04:36:02 +0100 Subject: [PATCH 1/5] fix(files): the files #362 left are found and written in another case on Linux (GE#542) were still opened by the name the scenery gives, in lowercase, and some were written by it, so on Linux a new file went to a new lowercase folder next to the real one, or a second file was made next to the one there. Opened through FileOnDisk() now: the .vmd animations of the events, the Lua script of the scenery, the Lua and WASM screens and the files they read from their folder, the textures read for the colour of the rails, and the .sbt files a scenery includes with the terrain directive. FileOnDiskToWrite() gives the file to write: the one there in any case under its name on the disk, otherwise the name given in the folders as they are on the disk, the folders missing in any case named as given. The editor's layers (node_layers::path()), the .sbt the game writes and the heightmap terrain folder go through it; the editor removes an .sbt there in another case. The .e3d made of a .t3d is written next to the text, or over the .e3d there in another case. FileCased() writes each file found only in another case to the log once (logtype::file), the list for the authors of the data to name the files as they're asked for. Nothing changes on Windows: FileCased() gives the name back as it is there, and nothing is logged. --- editor/editorTerrainStreamer.cpp | 4 +++- model/Model3d.cpp | 5 +++++ scene/scene.cpp | 10 +++++++--- scene/scenelayers.cpp | 5 ++--- scene/scenelayerssave.cpp | 2 +- scripting/luasandbox.cpp | 8 +++++++- scripting/scriptscreens.cpp | 4 ++-- tests/unit/test_lua_sandbox.cpp | 14 ++++++++++++++ tests/unit/test_script_screens.cpp | 12 ++++++++++++ tests/unit/test_utilities.cpp | 26 ++++++++++++++++++++++++++ utilities/utilities.cpp | 26 +++++++++++++++++++++++++- utilities/utilities.h | 6 +++++- world/Event.cpp | 3 ++- world/RailMaterials.cpp | 5 +++-- 14 files changed, 114 insertions(+), 16 deletions(-) diff --git a/editor/editorTerrainStreamer.cpp b/editor/editorTerrainStreamer.cpp index a8f569aaa..647d44906 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 @@ -106,7 +107,8 @@ 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"}; diff --git a/model/Model3d.cpp b/model/Model3d.cpp index 61899cca3..a9434dbb6 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..95cedb315 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 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/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..a6cac5410 100644 --- a/tests/unit/test_utilities.cpp +++ b/tests/unit/test_utilities.cpp @@ -716,6 +716,32 @@ 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"); + + // 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(ToLower(existing) == ToLower(folder.path("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(ToLower(added) == ToLower(folder.path("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(ToLower(deeper) == ToLower(folder.path("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: 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/utilities.cpp b/utilities/utilities.cpp index 1adb80a5b..df4a5b3eb 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -19,6 +19,7 @@ Copyright (C) 2007-2014 Maciej Cierniak #include #include #include +#include //#ifndef WIN32 //#include //#endif @@ -732,6 +733,8 @@ struct cased_path std::chrono::steady_clock::time_point checked{}; }; std::unordered_map cased_paths; +// the files found only in another case than asked for, written to the log once each +std::unordered_set cased_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) @@ -871,6 +874,8 @@ std::string FileCased(std::filesystem::path const &Filename) if (file.empty()) return {}; std::string cased; + auto const asked{folder + file}; + bool report{false}; { std::scoped_lock const lock{cased_mutex}; if (false == cased_folder_path(folder, cased)) @@ -879,8 +884,16 @@ std::string FileCased(std::filesystem::path const &Filename) if (name.empty()) return {}; cased += name; + report = (cased != asked) && cased_reported.insert(asked).second; } - return std::filesystem::exists(cased, error) ? cased : std::string{}; + if (false == std::filesystem::exists(cased, error)) + return {}; + if (report) + { + // the list for the authors of the data, to name the files as the sceneries and the vehicles ask for them + WriteLog("File \"" + asked + "\" found as \"" + cased + "\", named in another case", logtype::file); + } + return cased; #endif } @@ -890,6 +903,17 @@ std::string FileOnDisk(std::string const &Filename) return cased.empty() ? Filename : cased; } +std::string FileOnDiskToWrite(std::string const &Filename) +{ + if (auto const cased{FileCased(Filename)}; 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 FileOnDiskToWrite(Filename.substr(0, split)) + '/' + Filename.substr(split + 1); +} + 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..dffcd5df9 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -259,10 +259,14 @@ 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 written to the log, once 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); // 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; } From 40cc6a2c93f8e59dc951790f77a5d2bed947ea6b Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Fri, 20 Nov 2026 07:59:15 +0100 Subject: [PATCH 2/5] refactor(utilities): the case report list without Sonar issues The set of files already reported sits behind a function instead of a mutable global, with a transparent hasher. No change in behaviour. --- utilities/utilities.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index df4a5b3eb..cc7012e30 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -19,6 +19,8 @@ Copyright (C) 2007-2014 Maciej Cierniak #include #include #include +#include +#include #include //#ifndef WIN32 //#include @@ -733,8 +735,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, written to the log once each -std::unordered_set cased_reported; +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) @@ -884,7 +896,7 @@ std::string FileCased(std::filesystem::path const &Filename) if (name.empty()) return {}; cased += name; - report = (cased != asked) && cased_reported.insert(asked).second; + report = (cased != asked) && cased_reported().insert(asked).second; } if (false == std::filesystem::exists(cased, error)) return {}; From 67a7e3876c5909828c3cd7d76a86cfcf54d037c8 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Tue, 24 Nov 2026 17:53:46 +0100 Subject: [PATCH 3/5] fix(files): a new layer isn't made over a file there in another case, and the review's smaller points - node_layers::create() refuses a name a file is there with in any case: on Linux path() wrote over it. - A name is marked as logged only once the file is known to be there. - The log line says the file is on disk in another case; the folders FileOnDiskToWrite() looks up aren't logged. - The editor start reads the layers, and the heightmap terrain its terrain.yaml and pack files, as they're named on the disk. - The write test compares the names exactly; a test of the log line once per name, with the lines kept by the test log stub. --- application/editormode.cpp | 3 ++- editor/editorTerrainStreamer.cpp | 8 +++--- scene/scenelayers.cpp | 3 ++- tests/unit/logcapture.h | 25 +++++++++++++++++ tests/unit/test_utilities.cpp | 38 +++++++++++++++++++++++--- utilities/utilities.cpp | 46 +++++++++++++++++++++++--------- 6 files changed, 102 insertions(+), 21 deletions(-) create mode 100644 tests/unit/logcapture.h 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 647d44906..d887b14b9 100644 --- a/editor/editorTerrainStreamer.cpp +++ b/editor/editorTerrainStreamer.cpp @@ -100,7 +100,8 @@ 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) @@ -111,7 +112,7 @@ bool terrain_streamer::open(std::string const &Name) 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)) { @@ -2308,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/scene/scenelayers.cpp b/scene/scenelayers.cpp index 95cedb315..e7a72f3a9 100644 --- a/scene/scenelayers.cpp +++ b/scene/scenelayers.cpp @@ -618,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/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_utilities.cpp b/tests/unit/test_utilities.cpp index a6cac5410..13924cf25 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 @@ -719,19 +720,25 @@ TEST_CASE("utilities: a terrain file is found in another case, by its text or by 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(ToLower(existing) == ToLower(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(ToLower(added) == ToLower(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(ToLower(deeper) == ToLower(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 @@ -742,6 +749,31 @@ TEST_CASE("utilities: a file is written over in another case, or made in its fol 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, the folders of a file to write are not") { + test::scratch_folder const folder{"utilities_cased_log"}; + folder.write("Scenery/Braniewo/Base.scm", "x"); + 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 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/utilities.cpp b/utilities/utilities.cpp index cc7012e30..233b7eb26 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -855,7 +855,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 written to the log 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)) @@ -887,7 +891,6 @@ std::string FileCased(std::filesystem::path const &Filename) return {}; std::string cased; auto const asked{folder + file}; - bool report{false}; { std::scoped_lock const lock{cased_mutex}; if (false == cased_folder_path(folder, cased)) @@ -896,19 +899,42 @@ std::string FileCased(std::filesystem::path const &Filename) if (name.empty()) return {}; cased += name; - report = (cased != asked) && cased_reported().insert(asked).second; } if (false == std::filesystem::exists(cased, error)) return {}; - if (report) + if (Report && (cased != asked)) { - // the list for the authors of the data, to name the files as the sceneries and the vehicles ask for them - WriteLog("File \"" + asked + "\" found as \"" + cased + "\", named in another case", logtype::file); + // marked as reported once it's known to be there, and written to the log outside of the lock + std::unique_lock lock{cased_mutex}; + auto const first{cased_reported().insert(asked).second}; + lock.unlock(); + // the parser asks for the names in lowercase, so these are mostly the files not named in lowercase on the disk + if (first) + WriteLog("File \"" + asked + "\" is \"" + cased + "\" on disk, in another case", logtype::file); } return cased; #endif } +// FileOnDiskToWrite(), the file found only in another case written to the log 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)}; @@ -917,13 +943,7 @@ std::string FileOnDisk(std::string const &Filename) std::string FileOnDiskToWrite(std::string const &Filename) { - if (auto const cased{FileCased(Filename)}; 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 FileOnDiskToWrite(Filename.substr(0, split)) + '/' + Filename.substr(split + 1); + return on_disk_to_write(Filename, true); } std::string FileStemOnDisk(std::string const &Folder, std::string const &Name, std::vector const &Extensions) From 77affa28d59b0a140e01376a13c672b819b46ab5 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 25 Nov 2026 11:34:26 +0100 Subject: [PATCH 4/5] fix(files): the files found in another case are counted, and listed only with logs.filecase These files load fine since #362, so a run no longer gets a log line for each of them. It gets one line with their count when the game closes, none when there are none; "logs.filecase yes" in eu07.ini logs each name, once, as before. - The count is written at the closing, as it covers the whole run: the vehicles, sounds and textures loaded after the scenery count too, which a line at the end of loading would miss. - An eu07.ini key rather than a log type of its own: logs.filter sets all the types at once, so a type off by default would come on for anyone with "logs.filter 0". - On Windows nothing is found in another case, so nothing is counted or logged. --- application/application.cpp | 3 +++ tests/unit/test_utilities.cpp | 47 ++++++++++++++++++++++++++++++++++- utilities/Globals.cpp | 7 ++++++ utilities/Globals.h | 3 +++ utilities/utilities.cpp | 33 +++++++++++++++++++----- utilities/utilities.h | 6 ++++- 6 files changed, 91 insertions(+), 8 deletions(-) diff --git a/application/application.cpp b/application/application.cpp index fa74c3d7d..69387e13e 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/tests/unit/test_utilities.cpp b/tests/unit/test_utilities.cpp index 13924cf25..0986519e8 100644 --- a/tests/unit/test_utilities.cpp +++ b/tests/unit/test_utilities.cpp @@ -31,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"}; @@ -749,9 +759,10 @@ TEST_CASE("utilities: a file is written over in another case, or made in its fol 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, the folders of a file to write are not") { +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 @@ -774,6 +785,40 @@ TEST_CASE("utilities: a file found only in another case is logged once, the fold #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 5706ee232..7ef5e1a39 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 233b7eb26..b2d3dd445 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -741,7 +741,7 @@ 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, written to the log once each +// 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; @@ -858,7 +858,7 @@ bool cased_folder_path(std::string const &Folder, std::string &Cased) namespace { -// FileCased(), the file found only in another case written to the log when Report +// 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; @@ -904,19 +904,20 @@ std::string file_cased(std::filesystem::path const &Filename, [[maybe_unused]] b return {}; if (Report && (cased != asked)) { - // marked as reported once it's known to be there, and written to the log outside of the lock + // counted once it's known to be there, and written to the log outside of the lock std::unique_lock lock{cased_mutex}; auto const first{cased_reported().insert(asked).second}; lock.unlock(); - // the parser asks for the names in lowercase, so these are mostly the files not named in lowercase on the disk - if (first) + // 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 written to the log when Report; the folders never are +// 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()) @@ -946,6 +947,26 @@ 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("Files found on disk only in another case than asked for: {}{}", 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 dffcd5df9..a71063694 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -260,13 +260,17 @@ 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. a file found only in another -// case is written to the log, once +// 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); From f2aa5d5852663ab3c1c6015c7f3f7c72e0248879 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Thu, 26 Nov 2026 05:32:39 +0100 Subject: [PATCH 5/5] fix: count the names asked for in the case summary line, and scope the lock --- utilities/utilities.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index b2d3dd445..ae4a9a423 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -905,9 +905,11 @@ std::string file_cased(std::filesystem::path const &Filename, [[maybe_unused]] b if (Report && (cased != asked)) { // counted once it's known to be there, and written to the log outside of the lock - std::unique_lock lock{cased_mutex}; - auto const first{cased_reported().insert(asked).second}; - lock.unlock(); + 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) @@ -962,7 +964,7 @@ void LogFilesInAnotherCase() auto const count{FilesInAnotherCase()}; if (count == 0) return; - WriteLog(std::format("Files found on disk only in another case than asked for: {}{}", count, + 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); }