18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/world/SleeperPack.cpp
jerrrrycho 7059644209 fix(editor): a model file named outside the code page no longer crashes the first track selection (#470)
* fix(editor): a model file named outside the code page no longer crashes the first track selection

The first selection of a track builds the track property list, and with
it the sleeper and timber model choices. pack_models() walked the whole
models/ tree and turned the name of every .t3d/.e3d into a narrow string
before looking at whether it was a sleeper at all. On Windows that goes
through the ANSI code page, so a single model file anywhere under models/
named with a character the code page lacks threw std::system_error
"No mapping for the Unicode character exists in the target multi-byte
code page" out of the editor frame and the editor stopped (GE#560).

The conversions are now in a try, as in the editor's other folder scans:
a file whose name can't be converted is left out (the game couldn't load
it by that name anyway) and the scan goes on. Names that convert give the
same list as before.

The scan itself moved to world/SleeperPack.cpp in the core library as
pack_models(root), which pack_models() calls once with models/, so the
unit tests can run it on a folder of their own.

* fix(launcher): a vehicle folder named outside the code page is skipped by the skin scan

scan_textures() keeps the folder of each textures.txt as a wide path and
get_vehicle() turns it into a narrow name with no try. On Windows a
folder under dynamic/ named with a character the code page lacks threw
the same std::system_error out of the editor's vehicle tool, the
launcher and the vehicle picker. Such a folder is now left out of the
scan, as the model scan does with such a file.
2026-11-25 05:13:18 +01:00

92 lines
3.5 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "world/TrackSleepers.h"
#include "utilities/utilities.h"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iterator>
// the models of sleepers and timbers found among the files of a folder, by their names; apart from the models drawn, so the
// unit tests can look through a folder of their own
namespace track_sleepers {
namespace {
// the model of sleepers or timbers of the file added to those found, once by its name, what goes wrong left in the error
void add_pack_model( std::filesystem::directory_entry const &Entry, std::filesystem::path const &Root, std::error_code &Error, std::vector<pack_model> &Models ) {
namespace fs = std::filesystem;
if( false == Entry.is_regular_file( Error ) ) {
return;
}
// a name with a character the code page lacks throws as it's turned into the narrow names the models are loaded by, which
// couldn't open the file anyway: the file is left out, not the rest of the folder with it
std::string extension;
std::string name;
try {
extension = ToLower( Entry.path().extension().string() );
if( extension != ".t3d" && extension != ".e3d" ) {
return;
}
name = ToLower( fs::relative( Entry.path(), Root, Error ).replace_extension().generic_string() );
}
catch( std::exception const & ) {
return;
}
if( Error || name.empty() ) {
return;
}
if( name.find( "podklad" ) == std::string::npos && name.find( "podrozjazd" ) == std::string::npos
&& name.find( "sleeper" ) == std::string::npos && name.find( "timber" ) == std::string::npos ) {
return;
}
// a timber model has the parts it's put together from, told by their names in the file
std::ifstream file { Entry.path(), std::ios::binary };
std::string const content { std::istreambuf_iterator<char>( file ), std::istreambuf_iterator<char>() };
auto const has = [ &extension, &content ]( std::string const &Part ) {
return (
extension == ".t3d" ?
content.find( "Name: " + Part + "\n" ) != std::string::npos || content.find( "Name: " + Part + " " ) != std::string::npos
|| content.find( "Name: " + Part + "\r" ) != std::string::npos :
content.find( std::string { '\0' } + Part + '\0' ) != std::string::npos ); };
auto const timber { has( "begin" ) && has( "main" ) && has( "end" ) };
if( std::ranges::none_of( Models, [ &name ]( pack_model const &Model ) { return Model.name == name; } ) ) {
Models.emplace_back( std::move( name ), timber );
}
}
} // namespace
std::vector<pack_model> pack_models( std::filesystem::path const &Root ) {
namespace fs = std::filesystem;
std::vector<pack_model> models;
std::error_code error;
fs::recursive_directory_iterator entry { Root, fs::directory_options::follow_directory_symlink | fs::directory_options::skip_permission_denied, error };
fs::recursive_directory_iterator const end;
while( entry != end ) {
if( error ) {
break;
}
add_pack_model( *entry, Root, error, models );
entry.increment( error );
}
std::ranges::sort( models, []( pack_model const &Left, pack_model const &Right ) {
return ( Left.timber != Right.timber ? Left.timber : Left.name < Right.name ); } );
return models;
}
} // namespace track_sleepers