file access cross-platform compatibility fixes

This commit is contained in:
tmj-fstate
2018-03-29 03:48:21 +02:00
parent 64dea17e6f
commit 6ffced1d90
12 changed files with 40 additions and 30 deletions

View File

@@ -1733,7 +1733,8 @@ TDynamicObject::Init(std::string Name, // nazwa pojazdu, np. "EU07-424"
)
{ // Ustawienie początkowe pojazdu
iDirection = (Reversed ? 0 : 1); // Ra: 0, jeśli ma być wstawiony jako obrócony tyłem
asBaseDir = "dynamic\\" + BaseDir + "\\"; // McZapkie-310302
asBaseDir = szDynamicPath + BaseDir + "/"; // McZapkie-310302
replace_slashes( asBaseDir );
asName = Name;
std::string asAnimName = ""; // zmienna robocza do wyszukiwania osi i wózków
// Ra: zmieniamy znaczenie obsady na jednoliterowe, żeby dosadzić kierownika
@@ -1768,9 +1769,9 @@ TDynamicObject::Init(std::string Name, // nazwa pojazdu, np. "EU07-424"
if (!MoverParameters->LoadFIZ(asBaseDir))
{ // jak wczytanie CHK się nie uda, to błąd
if (ConversionError == 666)
ErrorLog( "Bad vehicle: failed do locate definition file \"" + BaseDir + "\\" + Type_Name + ".fiz" + "\"" );
ErrorLog( "Bad vehicle: failed do locate definition file \"" + BaseDir + "/" + Type_Name + ".fiz" + "\"" );
else {
ErrorLog( "Bad vehicle: failed to load definition from file \"" + BaseDir + "\\" + Type_Name + ".fiz\" (error " + to_string( ConversionError ) + ")" );
ErrorLog( "Bad vehicle: failed to load definition from file \"" + BaseDir + "/" + Type_Name + ".fiz\" (error " + to_string( ConversionError ) + ")" );
}
return 0.0; // zerowa długość to brak pojazdu
}
@@ -1780,8 +1781,7 @@ TDynamicObject::Init(std::string Name, // nazwa pojazdu, np. "EU07-424"
(fVel > 0 ? 1 : -1) * Cab *
(iDirection ? 1 : -1))) // jak jedzie lub obsadzony to gotowy do drogi
{
Error("Parameters mismatch: dynamic object " + asName + " from\n" + BaseDir + "\\" +
Type_Name);
Error("Parameters mismatch: dynamic object " + asName + " from \"" + BaseDir + "/" + Type_Name + "\"" );
return 0.0; // zerowa długość to brak pojazdu
}
// ustawienie pozycji hamulca
@@ -4173,11 +4173,16 @@ void TDynamicObject::LoadMMediaFile( std::string BaseDir, std::string TypeName,
m_materialdata.multi_textures = 0; // czy jest wiele tekstur wymiennych?
parser.getTokens();
parser >> asModel;
replace_slashes( asModel );
if( asModel[asModel.size() - 1] == '#' ) // Ra 2015-01: nie podoba mi siê to
{ // model wymaga wielu tekstur wymiennych
m_materialdata.multi_textures = 1;
asModel.erase( asModel.length() - 1 );
}
// name can contain leading slash, erase it to avoid creation of double slashes when the name is combined with current directory
if( asModel[ 0 ] == '/' ) {
asModel.erase( 0, 1 );
}
std::size_t i = asModel.find( ',' );
if ( i != std::string::npos )
{ // Ra 2015-01: może szukać przecinka w nazwie modelu, a po przecinku była by liczba tekstur?

View File

@@ -436,8 +436,8 @@ int main(int argc, char *argv[])
#endif
if( Global.iConvertModels < 0 ) {
Global.iConvertModels = -Global.iConvertModels;
World.CreateE3D( "models\\" ); // rekurencyjne przeglądanie katalogów
World.CreateE3D( "dynamic\\", true );
World.CreateE3D( szModelPath ); // rekurencyjne przeglądanie katalogów
World.CreateE3D( szDynamicPath, true );
} // po zrobieniu E3D odpalamy normalnie scenerię, by ją zobaczyć
#ifdef _WIN32
Console::On(); // włączenie konsoli

View File

@@ -476,9 +476,8 @@ void TEvent::Load(cParser *parser, Math3D::vector3 const &org)
}
else if (token.substr(token.length() - 4, 4) == ".vmd") // na razie tu, może będzie inaczej
{ // animacja z pliku VMD
// TFileStream *fs = new TFileStream( "models\\" + AnsiString( token.c_str() ), fmOpenRead );
{
std::ifstream file( "models\\" + token, std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws );
std::ifstream file( szModelPath + token, 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
Params[ 7 ].asInt = size;

View File

@@ -81,12 +81,13 @@ TModelsManager::GetModel(std::string const &Name, bool const Dynamic)
// - wczytanie modelu animowanego - Init() - sprawdzić
std::string const buftp { Global.asCurrentTexturePath }; // zapamiętanie aktualnej ścieżki do tekstur,
std::string filename { Name };
if( Name.find( '/' ) != std::string::npos ) {
if( ( false == Dynamic )
&& ( Name.find( '/' ) != std::string::npos ) ) {
// pobieranie tekstur z katalogu, w którym jest model
// when loading vehicles the path is set by the calling routine, so we can skip it here
Global.asCurrentTexturePath += Name;
Global.asCurrentTexturePath.erase( Global.asCurrentTexturePath.rfind( "/" ) + 1 );
Global.asCurrentTexturePath.erase( Global.asCurrentTexturePath.rfind( '/' ) + 1 );
}
erase_extension( filename );
filename = ToLower( filename );

View File

@@ -479,7 +479,7 @@ void TPythonScreens::update()
void TPythonScreens::setLookupPath(std::string const &path)
{
_lookupPath = path;
std::replace(_lookupPath.begin(), _lookupPath.end(), '\\', '/');
replace_slashes( _lookupPath );
}
TPythonScreens::TPythonScreens()

View File

@@ -761,10 +761,7 @@ texture_manager::create( std::string Filename, bool const Loadnow ) {
}
erase_extension( Filename );
// change slashes to cross-platform
std::replace(
std::begin( Filename ), std::end( Filename ),
'\\', '/' );
replace_slashes( Filename );
std::vector<std::string> extensions { { ".dds" }, { ".tga" }, { ".bmp" }, { ".ext" } };

View File

@@ -1955,7 +1955,7 @@ void TWorld::CreateE3D(std::string const &Path, bool Dynamic)
// launch recursive search for sub-directories...
if( filename == "." ) { continue; }
if( filename == ".." ) { continue; }
CreateE3D( Path + filename + "\\", Dynamic );
CreateE3D( Path + filename + "/", Dynamic );
}
else {
// process the file

View File

@@ -133,10 +133,7 @@ buffer_manager::create( std::string const &Filename ) {
auto filename { ToLower( Filename ) };
erase_extension( filename );
// convert slashes
std::replace(
std::begin( filename ), std::end( filename ),
'\\', '/' );
replace_slashes( filename );
audio::buffer_handle lookup { null_handle };
std::string filelookup;

View File

@@ -17,8 +17,8 @@ http://mozilla.org/MPL/2.0/.
// helper, returns potential path part from provided file name
std::string path( std::string const &Filename ) {
return (
Filename.rfind( '\\' ) != std::string::npos ?
Filename.substr( 0, Filename.rfind( '\\' ) + 1 ) :
Filename.rfind( '/' ) != std::string::npos ?
Filename.substr( 0, Filename.rfind( '/' ) + 1 ) :
"" );
}
@@ -111,12 +111,9 @@ material_manager::create( std::string const &Filename, bool const Loadnow ) {
filename.erase( filename.find( '|' ) ); // po | może być nazwa kolejnej tekstury
erase_extension( filename );
filename += ".mat";
replace_slashes( filename );
// change slashes to llinux-compatible
std::replace(
std::begin( filename ), std::end( filename ),
'\\', '/' );
filename += ".mat";
// try to locate requested material in the databank
auto const databanklookup = find_in_databank( filename );

View File

@@ -10,5 +10,5 @@
void
cStars::init() {
m_stars = TModelsManager::GetModel( "models\\skydome_stars.t3d", false );
m_stars = TModelsManager::GetModel( "skydome_stars.t3d", false );
}

View File

@@ -334,3 +334,12 @@ erase_extension( std::string &Filename ) {
}
return false;
}
// potentially replaces backward slashes in provided file path with unix-compatible forward slashes
void
replace_slashes( std::string &Filename ) {
std::replace(
std::begin( Filename ), std::end( Filename ),
'\\', '/' );
}

View File

@@ -28,6 +28,7 @@ http://mozilla.org/MPL/2.0/.
#define szSceneryPath "scenery/"
#define szTexturePath "textures/"
#define szModelPath "models/"
#define szDynamicPath "dynamic/"
#define szSoundPath "sounds/"
#define MAKE_ID4(a,b,c,d) (((std::uint32_t)(d)<<24)|((std::uint32_t)(c)<<16)|((std::uint32_t)(b)<<8)|(std::uint32_t)(a))
@@ -183,6 +184,10 @@ std::time_t last_modified( std::string const &Filename );
bool
erase_extension( std::string &Filename );
// potentially replaces backward slashes in provided file path with unix-compatible forward slashes
void
replace_slashes( std::string &Filename );
template <typename Type_>
void SafeDelete( Type_ &Pointer ) {
delete Pointer;