From d670cc2e53941f0ae2033e760d5ccc5f0870d33f Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Tue, 17 Nov 2026 04:36:02 +0100 Subject: [PATCH] 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; }