mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 15:34:58 +01:00
fix(files): data named in another case is found on Linux, folders included
The parser and the loaders lowercase every name they read, while the data made on Windows has files and folders named in any case (scenery/Braniewo, textures/siec_trakcyjna_ph/.../STS_*.tga, sounds/sip/Gorki.wav). FileCased matched the file name only, so a folder in another case wasn't found. - FileCased walks every part of the path on Linux, the folder listings kept by their lowercase and read again once the folder changed; Windows as it was - FileExists finds the file in any case on Linux, and the variant with names and extensions gives back the name as it's on the disk, so textures, materials, sounds, station announcements and scripts open that name - models, sound captions, timetables and nodebank previews open the file as it's named on the disk; the texture format is told by the extension in any case
This commit is contained in:
@@ -216,9 +216,9 @@ openal_buffer::fetch_caption() {
|
|||||||
std::string captionfilename { name };
|
std::string captionfilename { name };
|
||||||
captionfilename.erase( captionfilename.rfind( '.' ) ); // obcięcie rozszerzenia
|
captionfilename.erase( captionfilename.rfind( '.' ) ); // obcięcie rozszerzenia
|
||||||
captionfilename += "-" + Global.asLang + ".txt"; // już może być w różnych językach
|
captionfilename += "-" + Global.asLang + ".txt"; // już może być w różnych językach
|
||||||
if( true == FileExists( captionfilename ) ) {
|
if( auto const casedfilename { FileCased( captionfilename ) }; false == casedfilename.empty() ) {
|
||||||
// wczytanie
|
// wczytanie
|
||||||
std::ifstream inputfile( captionfilename );
|
std::ifstream inputfile( casedfilename );
|
||||||
caption.assign( std::istreambuf_iterator<char>( inputfile ), std::istreambuf_iterator<char>() );
|
caption.assign( std::istreambuf_iterator<char>( inputfile ), std::istreambuf_iterator<char>() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -298,7 +298,9 @@ void image_cache::work()
|
|||||||
m_queue.pop_front();
|
m_queue.pop_front();
|
||||||
}
|
}
|
||||||
int width{0}, height{0}, components{0};
|
int width{0}, height{0}, components{0};
|
||||||
auto *pixels{stbi_load(image.path.c_str(), &width, &height, &components, 4)};
|
// the file as it's named on the disk, the path stays the key of the cache
|
||||||
|
auto const file{FileCased(image.path)};
|
||||||
|
auto *pixels{file.empty() ? nullptr : stbi_load(file.c_str(), &width, &height, &components, 4)};
|
||||||
if (pixels != nullptr && width > 0 && height > 0)
|
if (pixels != nullptr && width > 0 && height > 0)
|
||||||
{
|
{
|
||||||
// scaled down by the average of the pixels which make each one, the longer side to the size of the cache
|
// scaled down by the average of the pixels which make each one, the longer side to the size of the cache
|
||||||
|
|||||||
@@ -1702,24 +1702,27 @@ bool TModel3d::LoadFromFile(std::string const &FileName, bool dynamic)
|
|||||||
m_filename = name;
|
m_filename = name;
|
||||||
|
|
||||||
asBinary = name + ".e3d";
|
asBinary = name + ".e3d";
|
||||||
|
// 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)};
|
||||||
|
|
||||||
// Hirek: Jesli mamy ustawione priorityLoadText3D na yes to wpierw ladujemy t3d
|
// Hirek: Jesli mamy ustawione priorityLoadText3D na yes to wpierw ladujemy t3d
|
||||||
if (Global.priorityLoadText3D && FileExists(name + ".t3d"))
|
if (Global.priorityLoadText3D && (false == textfile.empty()))
|
||||||
{
|
{
|
||||||
WriteLog("Forced loading text model \"" + name + ".t3d\"");
|
WriteLog("Forced loading text model \"" + textfile + "\"");
|
||||||
LoadFromTextFile(name + ".t3d", dynamic);
|
LoadFromTextFile(textfile, dynamic);
|
||||||
if (!dynamic)
|
if (!dynamic)
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
else if (FileExists(asBinary))
|
else if (false == binaryfile.empty())
|
||||||
{
|
{
|
||||||
LoadFromBinFile(asBinary, dynamic);
|
LoadFromBinFile(binaryfile, dynamic);
|
||||||
asBinary = "";
|
asBinary = "";
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
else if (FileExists(name + ".t3d"))
|
else if (false == textfile.empty())
|
||||||
{
|
{
|
||||||
LoadFromTextFile(name + ".t3d", dynamic);
|
LoadFromTextFile(textfile, dynamic);
|
||||||
if (!dynamic)
|
if (!dynamic)
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -549,13 +549,15 @@ opengl_texture::load() {
|
|||||||
|
|
||||||
data_state = resource_state::loading;
|
data_state = resource_state::loading;
|
||||||
|
|
||||||
if( type == ".dds" ) { load_DDS(); }
|
// the extension as the file has it on the disk, in any case
|
||||||
else if( type == ".tga" ) { load_TGA(); }
|
auto const format { ToLower( type ) };
|
||||||
else if( type == ".png" ) { load_STBI(); }
|
if( format == ".dds" ) { load_DDS(); }
|
||||||
else if( type == ".ktx" ) { load_KTX(); }
|
else if( format == ".tga" ) { load_TGA(); }
|
||||||
else if( type == ".bmp" ) { load_STBI(); }
|
else if( format == ".png" ) { load_STBI(); }
|
||||||
else if( type == ".jpg" ) { load_STBI(); }
|
else if( format == ".ktx" ) { load_KTX(); }
|
||||||
else if( type == ".tex" ) { load_TEX(); }
|
else if( format == ".bmp" ) { load_STBI(); }
|
||||||
|
else if( format == ".jpg" ) { load_STBI(); }
|
||||||
|
else if( format == ".tex" ) { load_TEX(); }
|
||||||
else { goto fail; }
|
else { goto fail; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -578,6 +578,38 @@ TEST_CASE("utilities: a file is found by its name in another case") {
|
|||||||
CHECK(FileCased("eu07_unit_test_no_such_file.xyz").empty());
|
CHECK(FileCased("eu07_unit_test_no_such_file.xyz").empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("utilities: a file is found in folders named in another case, the way the parser gives the names in lowercase") {
|
||||||
|
test::scratch_folder const folder{"utilities_cased_folders"};
|
||||||
|
folder.write("Scenery/Braniewo/rozklady/Os91121.txt", "x");
|
||||||
|
folder.write("Textures/Tabl/L101-2m.BMP", "x");
|
||||||
|
|
||||||
|
// every part of the path in lowercase: the file as it's on the disk where the case matters, the name given where it doesn't
|
||||||
|
auto const timetable{FileCased(folder.path("scenery/braniewo/rozklady/os91121.txt"))};
|
||||||
|
REQUIRE_FALSE(timetable.empty());
|
||||||
|
CHECK(FileExists(timetable));
|
||||||
|
CHECK(ToLower(timetable) == ToLower(folder.path("Scenery/Braniewo/rozklady/Os91121.txt")));
|
||||||
|
CHECK(FileExists(folder.path("scenery/braniewo/rozklady/os91121.txt")));
|
||||||
|
// the folders written the Windows way as well
|
||||||
|
CHECK_FALSE(FileCased(folder.path() + "scenery\\braniewo\\rozklady\\os91121.txt").empty());
|
||||||
|
// a folder missing in any case
|
||||||
|
CHECK(FileCased(folder.path("scenery/nowhere/rozklady/os91121.txt")).empty());
|
||||||
|
CHECK_FALSE(FileExists(folder.path("scenery/nowhere/rozklady/os91121.txt")));
|
||||||
|
|
||||||
|
// among names and extensions: given back as named on the disk, so it's opened by that name, the extension too
|
||||||
|
auto const texture{FileExists(std::vector<std::string>{folder.path("textures/tabl/l101-2m")}, std::vector<std::string>{".dds", ".bmp"})};
|
||||||
|
REQUIRE_FALSE(texture.first.empty());
|
||||||
|
CHECK(ToLower(texture.second) == ".bmp");
|
||||||
|
CHECK(FileExists(texture.first + texture.second));
|
||||||
|
CHECK(ToLower(texture.first) == ToLower(folder.path("Textures/Tabl/L101-2m")));
|
||||||
|
|
||||||
|
// a file written after the folder was gone through is found as well
|
||||||
|
CHECK(FileCased(folder.path("textures/tabl/l7-2m.bmp")).empty());
|
||||||
|
folder.write("Textures/Tabl/L7-2m.bmp", "x");
|
||||||
|
auto const added{FileCased(folder.path("textures/tabl/l7-2m.bmp"))};
|
||||||
|
REQUIRE_FALSE(added.empty());
|
||||||
|
CHECK(FileExists(added));
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("utilities: the time a file was changed is given, zero for a file which isn't there") {
|
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"};
|
test::scratch_folder const folder{"utilities_modified"};
|
||||||
auto const name{folder.write("fresh.txt", "x")};
|
auto const name{folder.write("fresh.txt", "x")};
|
||||||
|
|||||||
@@ -513,9 +513,55 @@ template <> bool extract_value(bool &Variable, std::string const &Key, std::stri
|
|||||||
|
|
||||||
bool FileExists(std::string const &Filename)
|
bool FileExists(std::string const &Filename)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
return std::filesystem::exists(Filename);
|
return std::filesystem::exists(Filename);
|
||||||
|
#else
|
||||||
|
// the names come in lowercase from the parser, the files of the data are named in any case
|
||||||
|
return false == FileCased(Filename).empty();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
// the names in a folder by their lowercase, and the time the folder was changed when they were read
|
||||||
|
struct cased_folder
|
||||||
|
{
|
||||||
|
std::filesystem::file_time_type changed{};
|
||||||
|
std::unordered_map<std::string, std::string> names;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::mutex cased_mutex;
|
||||||
|
std::unordered_map<std::string, cased_folder> cased_folders;
|
||||||
|
|
||||||
|
// the name in the folder matching the one given in any case, empty when there's none. the folder is read again once it was
|
||||||
|
// changed, as the editor writes files while it runs. to be called with cased_mutex held
|
||||||
|
std::string cased_name(std::string const &Folder, std::string const &Name)
|
||||||
|
{
|
||||||
|
std::filesystem::path const folder{Folder.empty() ? std::string{"."} : Folder};
|
||||||
|
std::error_code error;
|
||||||
|
auto const changed{std::filesystem::last_write_time(folder, error)};
|
||||||
|
if (error)
|
||||||
|
return {};
|
||||||
|
auto &cached{cased_folders[Folder]};
|
||||||
|
if (cached.names.empty() || cached.changed != changed)
|
||||||
|
{
|
||||||
|
cached.changed = changed;
|
||||||
|
cached.names.clear();
|
||||||
|
for (std::filesystem::directory_iterator entry{folder, error}, end; (false == bool(error)) && (entry != end); entry.increment(error))
|
||||||
|
{
|
||||||
|
auto const name{entry->path().filename().string()};
|
||||||
|
cached.names.try_emplace(ToLower(name), name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auto const found{cached.names.find(ToLower(Name))};
|
||||||
|
return found != cached.names.end() ? found->second : std::string{};
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
#endif
|
||||||
|
|
||||||
std::string FileCased(std::filesystem::path const &Filename)
|
std::string FileCased(std::filesystem::path const &Filename)
|
||||||
{
|
{
|
||||||
std::error_code error;
|
std::error_code error;
|
||||||
@@ -526,13 +572,40 @@ std::string FileCased(std::filesystem::path const &Filename)
|
|||||||
// code page to throw on
|
// code page to throw on
|
||||||
return {};
|
return {};
|
||||||
#else
|
#else
|
||||||
// only the name of the file itself, a folder named in another case isn't found
|
// every part of the path in turn, the folders can be named in another case as well as the file
|
||||||
auto const directory{Filename.has_parent_path() ? Filename.parent_path() : std::filesystem::path{"."}};
|
auto const text{Filename.string()};
|
||||||
auto const wanted{ToLower(Filename.filename().string())};
|
std::string cased;
|
||||||
for (std::filesystem::directory_iterator entry{directory, error}, end; entry != end; entry.increment(error))
|
std::size_t start{0};
|
||||||
if (ToLower(entry->path().filename().string()) == wanted)
|
if ((false == text.empty()) && (text.front() == '/'))
|
||||||
return Filename.has_parent_path() ? (directory / entry->path().filename()).string() : entry->path().filename().string();
|
{
|
||||||
|
cased = "/";
|
||||||
|
start = 1;
|
||||||
|
}
|
||||||
|
while (start < text.size())
|
||||||
|
{
|
||||||
|
auto end{text.find_first_of("/\\", start)};
|
||||||
|
if (end == std::string::npos)
|
||||||
|
end = text.size();
|
||||||
|
auto const part{text.substr(start, end - start)};
|
||||||
|
start = end + 1;
|
||||||
|
if (part.empty())
|
||||||
|
continue;
|
||||||
|
if ((part == ".") || (part == "..") || std::filesystem::exists(cased + part, error))
|
||||||
|
{
|
||||||
|
cased += part;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::scoped_lock const lock{cased_mutex};
|
||||||
|
auto const name{cased_name(cased, part)};
|
||||||
|
if (name.empty())
|
||||||
return {};
|
return {};
|
||||||
|
cased += name;
|
||||||
|
}
|
||||||
|
if (end < text.size())
|
||||||
|
cased += '/';
|
||||||
|
}
|
||||||
|
return std::filesystem::exists(cased, error) ? cased : std::string{};
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -543,10 +616,19 @@ std::pair<std::string, std::string> FileExists(std::vector<std::string> const &N
|
|||||||
{
|
{
|
||||||
for (auto const &extension : Extensions)
|
for (auto const &extension : Extensions)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
if (FileExists(name + extension))
|
if (FileExists(name + extension))
|
||||||
{
|
{
|
||||||
return {name, extension};
|
return {name, extension};
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
// given back as it's named on the disk, so it's opened by that name
|
||||||
|
if (auto const cased{FileCased(name + extension)}; cased.size() >= extension.size())
|
||||||
|
{
|
||||||
|
auto const split{cased.size() - extension.size()};
|
||||||
|
return {cased.substr(0, split), cased.substr(split)};
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// nothing found
|
// nothing found
|
||||||
|
|||||||
@@ -222,10 +222,11 @@ template <typename Type_> bool extract_value(Type_ &Variable, std::string const
|
|||||||
template <> bool extract_value(bool &Variable, std::string const &Key, std::string const &Input, std::string const &Default);
|
template <> bool extract_value(bool &Variable, std::string const &Key, std::string const &Input, std::string const &Default);
|
||||||
|
|
||||||
bool FileExists(std::string const &Filename);
|
bool FileExists(std::string const &Filename);
|
||||||
// the file as it's named on the disk when only the case of its name differs, as in the sceneries made on Windows; empty when
|
// 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;
|
||||||
// there's none. the folders have to be named in their case; 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
|
||||||
std::string FileCased(std::filesystem::path const &Filename);
|
std::string FileCased(std::filesystem::path const &Filename);
|
||||||
|
|
||||||
|
// the first name with one of the extensions which has a file, in the order given, as the file is named on the disk
|
||||||
std::pair<std::string, std::string> FileExists(std::vector<std::string> const &Names, std::vector<std::string> const &Extensions);
|
std::pair<std::string, std::string> FileExists(std::vector<std::string> const &Names, std::vector<std::string> const &Extensions);
|
||||||
|
|
||||||
// returns time of last modification for specified file
|
// returns time of last modification for specified file
|
||||||
|
|||||||
@@ -277,6 +277,15 @@ bool TTrainParameters::LoadTTfile(std::string const &scnpath, int iPlus, double
|
|||||||
// Ra 2014-09: ustalić zasady wyznaczenia pierwotnego pliku przy przesuniętych rozkładach
|
// Ra 2014-09: ustalić zasady wyznaczenia pierwotnego pliku przy przesuniętych rozkładach
|
||||||
// (kolejny pociąg dostaje numer +2)
|
// (kolejny pociąg dostaje numer +2)
|
||||||
fin.open(s.c_str()); // otwieranie pliku
|
fin.open(s.c_str()); // otwieranie pliku
|
||||||
|
if (false == fin.is_open())
|
||||||
|
{
|
||||||
|
// the name of the train comes in lowercase, the file can be named in another case
|
||||||
|
if (auto const cased{FileCased(s)}; false == cased.empty())
|
||||||
|
{
|
||||||
|
fin.clear();
|
||||||
|
fin.open(cased);
|
||||||
|
}
|
||||||
|
}
|
||||||
replace_slashes(s);
|
replace_slashes(s);
|
||||||
|
|
||||||
if (!fin.is_open())
|
if (!fin.is_open())
|
||||||
|
|||||||
Reference in New Issue
Block a user