mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-19 22:44:59 +01:00
fix(files): the banks keep the names asked for, only the opening takes the case of the disk
The textures, materials and sounds are kept by the names they're asked for. Given back as named on the disk, a file in another case missed the bank and was loaded again on every request. - FileExists with names and extensions finds the file in any case but gives back the name asked for; FileOnDisk gives the file to open, used where textures, sounds and scripts are opened - FileExists of a single name is a plain check again, the places checking it and opening the same name stay as they were - the texture format is told by the extension asked for again - a folder changed shortly before it was read is read again for a name missing from it, the time of a change being as coarse as the clock of the file system; a listing cut short isn't kept
This commit is contained in:
@@ -35,7 +35,8 @@ decoded_sound decode_sound( std::string const &File ) {
|
|||||||
SF_INFO si;
|
SF_INFO si;
|
||||||
si.format = 0;
|
si.format = 0;
|
||||||
|
|
||||||
SNDFILE *sf = sf_open(File.c_str(), SFM_READ, &si);
|
// the file as it's named on the disk, the name of the buffer is in lowercase
|
||||||
|
SNDFILE *sf = sf_open(FileOnDisk(File).c_str(), SFM_READ, &si);
|
||||||
|
|
||||||
if (sf == nullptr)
|
if (sf == nullptr)
|
||||||
throw std::runtime_error("sound: sf_open failed");
|
throw std::runtime_error("sound: sf_open failed");
|
||||||
|
|||||||
@@ -549,15 +549,13 @@ opengl_texture::load() {
|
|||||||
|
|
||||||
data_state = resource_state::loading;
|
data_state = resource_state::loading;
|
||||||
|
|
||||||
// the extension as the file has it on the disk, in any case
|
if( type == ".dds" ) { load_DDS(); }
|
||||||
auto const format { ToLower( type ) };
|
else if( type == ".tga" ) { load_TGA(); }
|
||||||
if( format == ".dds" ) { load_DDS(); }
|
else if( type == ".png" ) { load_STBI(); }
|
||||||
else if( format == ".tga" ) { load_TGA(); }
|
else if( type == ".ktx" ) { load_KTX(); }
|
||||||
else if( format == ".png" ) { load_STBI(); }
|
else if( type == ".bmp" ) { load_STBI(); }
|
||||||
else if( format == ".ktx" ) { load_KTX(); }
|
else if( type == ".jpg" ) { load_STBI(); }
|
||||||
else if( format == ".bmp" ) { load_STBI(); }
|
else if( type == ".tex" ) { load_TEX(); }
|
||||||
else if( format == ".jpg" ) { load_STBI(); }
|
|
||||||
else if( format == ".tex" ) { load_TEX(); }
|
|
||||||
else { goto fail; }
|
else { goto fail; }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -642,7 +640,7 @@ void opengl_texture::load_STBI()
|
|||||||
{
|
{
|
||||||
int x, y, n;
|
int x, y, n;
|
||||||
stbi_set_flip_vertically_on_load(1);
|
stbi_set_flip_vertically_on_load(1);
|
||||||
uint8_t *image = stbi_load((name + type).c_str(), &x, &y, &n, 4);
|
uint8_t *image = stbi_load(FileOnDisk(name + type).c_str(), &x, &y, &n, 4);
|
||||||
|
|
||||||
if (!image) {
|
if (!image) {
|
||||||
data_state = resource_state::failed;
|
data_state = resource_state::failed;
|
||||||
@@ -820,7 +818,7 @@ DDSURFACEDESC2 opengl_texture::deserialize_ddsd(std::istream &s)
|
|||||||
void
|
void
|
||||||
opengl_texture::load_DDS() {
|
opengl_texture::load_DDS() {
|
||||||
|
|
||||||
std::ifstream file( name + type, std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws );
|
std::ifstream file( FileOnDisk( name + type ), std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws );
|
||||||
std::size_t filesize = static_cast<size_t>(file.tellg()); // ios::ate already positioned us at the end of the file
|
std::size_t filesize = static_cast<size_t>(file.tellg()); // ios::ate already positioned us at the end of the file
|
||||||
file.seekg( 0, std::ios::beg ); // rewind the caret afterwards
|
file.seekg( 0, std::ios::beg ); // rewind the caret afterwards
|
||||||
|
|
||||||
@@ -940,7 +938,7 @@ opengl_texture::load_DDS() {
|
|||||||
if( true == Global.gfx_texture_streaming ) {
|
if( true == Global.gfx_texture_streaming ) {
|
||||||
// the data is read by a worker thread, everything else about the texture is known already
|
// the data is read by a worker thread, everything else about the texture is known already
|
||||||
request = std::make_shared<texture_request>();
|
request = std::make_shared<texture_request>();
|
||||||
request->file = name + type;
|
request->file = FileOnDisk( name + type );
|
||||||
request->offset = static_cast<std::size_t>( file.tellg() ) + offset;
|
request->offset = static_cast<std::size_t>( file.tellg() ) + offset;
|
||||||
request->size = datasize;
|
request->size = datasize;
|
||||||
request->flip = flip;
|
request->flip = flip;
|
||||||
@@ -1002,7 +1000,7 @@ opengl_texture::load_DDS() {
|
|||||||
|
|
||||||
void
|
void
|
||||||
opengl_texture::load_KTX() {
|
opengl_texture::load_KTX() {
|
||||||
std::ifstream file( name + type, std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws );
|
std::ifstream file( FileOnDisk( name + type ), std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws );
|
||||||
std::size_t filesize = static_cast<size_t>(file.tellg()); // ios::ate already positioned us at the end of the file
|
std::size_t filesize = static_cast<size_t>(file.tellg()); // ios::ate already positioned us at the end of the file
|
||||||
file.seekg( 0, std::ios::beg ); // rewind the caret afterwards
|
file.seekg( 0, std::ios::beg ); // rewind the caret afterwards
|
||||||
|
|
||||||
@@ -1063,7 +1061,7 @@ opengl_texture::load_KTX() {
|
|||||||
void
|
void
|
||||||
opengl_texture::load_TEX() {
|
opengl_texture::load_TEX() {
|
||||||
|
|
||||||
std::ifstream file( name + type, std::ios::binary ); file.unsetf( std::ios::skipws );
|
std::ifstream file( FileOnDisk( name + type ), std::ios::binary ); file.unsetf( std::ios::skipws );
|
||||||
|
|
||||||
char head[ 5 ];
|
char head[ 5 ];
|
||||||
file.read( head, 4 );
|
file.read( head, 4 );
|
||||||
@@ -1110,7 +1108,7 @@ opengl_texture::load_TEX() {
|
|||||||
void
|
void
|
||||||
opengl_texture::load_TGA() {
|
opengl_texture::load_TGA() {
|
||||||
|
|
||||||
std::ifstream file( name + type, std::ios::binary ); file.unsetf( std::ios::skipws );
|
std::ifstream file( FileOnDisk( name + type ), std::ios::binary ); file.unsetf( std::ios::skipws );
|
||||||
|
|
||||||
// Read the header of the TGA, compare it with the known headers for compressed and uncompressed TGAs
|
// Read the header of the TGA, compare it with the known headers for compressed and uncompressed TGAs
|
||||||
unsigned char tgaheader[ 18 ];
|
unsigned char tgaheader[ 18 ];
|
||||||
|
|||||||
@@ -443,7 +443,7 @@ auto python_taskqueue::run_file(std::string const &File, std::string const &Path
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ifstream inputfile{filepath + fileextension};
|
std::ifstream inputfile{FileOnDisk(filepath + fileextension)};
|
||||||
std::string input;
|
std::string input;
|
||||||
input.assign(std::istreambuf_iterator<char>(inputfile), std::istreambuf_iterator<char>());
|
input.assign(std::istreambuf_iterator<char>(inputfile), std::istreambuf_iterator<char>());
|
||||||
|
|
||||||
|
|||||||
@@ -586,28 +586,28 @@ TEST_CASE("utilities: a file is found in folders named in another case, the way
|
|||||||
// every part of the path in lowercase: the file as it's on the disk where the case matters, the name given where it doesn't
|
// 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"))};
|
auto const timetable{FileCased(folder.path("scenery/braniewo/rozklady/os91121.txt"))};
|
||||||
REQUIRE_FALSE(timetable.empty());
|
REQUIRE_FALSE(timetable.empty());
|
||||||
CHECK(FileExists(timetable));
|
CHECK(std::filesystem::exists(timetable));
|
||||||
CHECK(ToLower(timetable) == ToLower(folder.path("Scenery/Braniewo/rozklady/Os91121.txt")));
|
CHECK(ToLower(timetable) == ToLower(folder.path("Scenery/Braniewo/rozklady/Os91121.txt")));
|
||||||
CHECK(FileExists(folder.path("scenery/braniewo/rozklady/os91121.txt")));
|
CHECK(FileOnDisk(folder.path("scenery/braniewo/rozklady/os91121.txt")) == timetable);
|
||||||
// the folders written the Windows way as well
|
// the folders written the Windows way as well
|
||||||
CHECK_FALSE(FileCased(folder.path() + "scenery\\braniewo\\rozklady\\os91121.txt").empty());
|
CHECK_FALSE(FileCased(folder.path() + "scenery\\braniewo\\rozklady\\os91121.txt").empty());
|
||||||
// a folder missing in any case
|
// a folder missing in any case
|
||||||
CHECK(FileCased(folder.path("scenery/nowhere/rozklady/os91121.txt")).empty());
|
CHECK(FileCased(folder.path("scenery/nowhere/rozklady/os91121.txt")).empty());
|
||||||
CHECK_FALSE(FileExists(folder.path("scenery/nowhere/rozklady/os91121.txt")));
|
CHECK(FileOnDisk(folder.path("scenery/nowhere/rozklady/os91121.txt")) == folder.path("scenery/nowhere/rozklady/os91121.txt"));
|
||||||
|
|
||||||
// among names and extensions: given back as named on the disk, so it's opened by that name, the extension too
|
// among names and extensions: found in any case, given back as named, the name being the key of the banks
|
||||||
auto const texture{FileExists(std::vector<std::string>{folder.path("textures/tabl/l101-2m")}, std::vector<std::string>{".dds", ".bmp"})};
|
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());
|
REQUIRE_FALSE(texture.first.empty());
|
||||||
CHECK(ToLower(texture.second) == ".bmp");
|
CHECK(texture.first == folder.path("textures/tabl/l101-2m"));
|
||||||
CHECK(FileExists(texture.first + texture.second));
|
CHECK(texture.second == ".bmp");
|
||||||
CHECK(ToLower(texture.first) == ToLower(folder.path("Textures/Tabl/L101-2m")));
|
CHECK(std::filesystem::exists(FileOnDisk(texture.first + texture.second)));
|
||||||
|
|
||||||
// a file written after the folder was gone through is found as well
|
// a file written after the folder was gone through is found as well
|
||||||
CHECK(FileCased(folder.path("textures/tabl/l7-2m.bmp")).empty());
|
CHECK(FileCased(folder.path("textures/tabl/l7-2m.bmp")).empty());
|
||||||
folder.write("Textures/Tabl/L7-2m.bmp", "x");
|
folder.write("Textures/Tabl/L7-2m.bmp", "x");
|
||||||
auto const added{FileCased(folder.path("textures/tabl/l7-2m.bmp"))};
|
auto const added{FileCased(folder.path("textures/tabl/l7-2m.bmp"))};
|
||||||
REQUIRE_FALSE(added.empty());
|
REQUIRE_FALSE(added.empty());
|
||||||
CHECK(FileExists(added));
|
CHECK(std::filesystem::exists(added));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("utilities: the time a file was changed is given, zero for a file which isn't there") {
|
TEST_CASE("utilities: the time a file was changed is given, zero for a file which isn't there") {
|
||||||
|
|||||||
@@ -513,22 +513,19 @@ 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
|
#ifndef _WIN32
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
// the names in a folder by their lowercase, and the time the folder was changed when they were read
|
// the names in a folder by their lowercase, the time the folder was changed and the time they were read
|
||||||
struct cased_folder
|
struct cased_folder
|
||||||
{
|
{
|
||||||
|
bool read{false};
|
||||||
std::filesystem::file_time_type changed{};
|
std::filesystem::file_time_type changed{};
|
||||||
|
std::filesystem::file_time_type listed{};
|
||||||
std::unordered_map<std::string, std::string> names;
|
std::unordered_map<std::string, std::string> names;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -536,7 +533,8 @@ std::mutex cased_mutex;
|
|||||||
std::unordered_map<std::string, cased_folder> cased_folders;
|
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
|
// the name in the folder matching the one given in any case, empty when there's none. the folder is read again once it was
|
||||||
// changed, as the editor writes files while it runs. to be called with cased_mutex held
|
// changed, as the editor writes files while it runs; a name missing from a folder changed shortly before it was read is looked
|
||||||
|
// for again, the time of the change being as coarse as the clock of the file system. to be called with cased_mutex held
|
||||||
std::string cased_name(std::string const &Folder, std::string const &Name)
|
std::string cased_name(std::string const &Folder, std::string const &Name)
|
||||||
{
|
{
|
||||||
std::filesystem::path const folder{Folder.empty() ? std::string{"."} : Folder};
|
std::filesystem::path const folder{Folder.empty() ? std::string{"."} : Folder};
|
||||||
@@ -545,17 +543,27 @@ std::string cased_name(std::string const &Folder, std::string const &Name)
|
|||||||
if (error)
|
if (error)
|
||||||
return {};
|
return {};
|
||||||
auto &cached{cased_folders[Folder]};
|
auto &cached{cased_folders[Folder]};
|
||||||
if (cached.names.empty() || cached.changed != changed)
|
auto const wanted{ToLower(Name)};
|
||||||
{
|
auto const read{[&]() {
|
||||||
cached.changed = changed;
|
|
||||||
cached.names.clear();
|
cached.names.clear();
|
||||||
|
cached.changed = changed;
|
||||||
|
cached.listed = std::filesystem::file_time_type::clock::now();
|
||||||
for (std::filesystem::directory_iterator entry{folder, error}, end; (false == bool(error)) && (entry != end); entry.increment(error))
|
for (std::filesystem::directory_iterator entry{folder, error}, end; (false == bool(error)) && (entry != end); entry.increment(error))
|
||||||
{
|
{
|
||||||
auto const name{entry->path().filename().string()};
|
auto const name{entry->path().filename().string()};
|
||||||
cached.names.try_emplace(ToLower(name), name);
|
cached.names.try_emplace(ToLower(name), name);
|
||||||
}
|
}
|
||||||
|
// a listing cut short isn't kept
|
||||||
|
cached.read = (false == bool(error));
|
||||||
|
}};
|
||||||
|
if ((false == cached.read) || (cached.changed != changed))
|
||||||
|
read();
|
||||||
|
auto found{cached.names.find(wanted)};
|
||||||
|
if ((found == cached.names.end()) && (cached.listed - cached.changed < std::chrono::seconds{2}))
|
||||||
|
{
|
||||||
|
read();
|
||||||
|
found = cached.names.find(wanted);
|
||||||
}
|
}
|
||||||
auto const found{cached.names.find(ToLower(Name))};
|
|
||||||
return found != cached.names.end() ? found->second : std::string{};
|
return found != cached.names.end() ? found->second : std::string{};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -609,6 +617,12 @@ std::string FileCased(std::filesystem::path const &Filename)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string FileOnDisk(std::string const &Filename)
|
||||||
|
{
|
||||||
|
auto cased{FileCased(Filename)};
|
||||||
|
return cased.empty() ? Filename : cased;
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -616,19 +630,11 @@ 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
|
// found in any case, given back as named here: the name stays the key of the banks, the file is opened by FileOnDisk()
|
||||||
if (FileExists(name + extension))
|
if (false == FileCased(name + extension).empty())
|
||||||
{
|
{
|
||||||
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
|
||||||
|
|||||||
@@ -225,8 +225,11 @@ bool FileExists(std::string const &Filename);
|
|||||||
// the file as it's named on the disk when only the case of its name or of its folders differs, as in the sceneries made on Windows;
|
// 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
|
||||||
std::string FileCased(std::filesystem::path const &Filename);
|
std::string FileCased(std::filesystem::path const &Filename);
|
||||||
|
// the file to open: as it's named on the disk, the name given when there's none
|
||||||
|
std::string FileOnDisk(std::string const &Filename);
|
||||||
|
|
||||||
// the first name with one of the extensions which has a file, in the order given, as the file is named on the disk
|
// the first name with one of the extensions which has a file in any case, in the order given; given back as named here, the file
|
||||||
|
// is opened by FileOnDisk()
|
||||||
std::pair<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
|
||||||
|
|||||||
Reference in New Issue
Block a user