Ref T348, util function to correct FSX/P3D model path

Related: Ref T317 Ref T247 Ref T335
This commit is contained in:
Klaus Basan
2018-09-13 01:57:53 +02:00
parent 7f78981be7
commit a07d18aa44
4 changed files with 53 additions and 2 deletions

View File

@@ -129,6 +129,14 @@ namespace BlackMisc
return path.left(path.lastIndexOf('/'));
}
QString CFileUtils::lastPathSegment(const QString &path)
{
if (path.isEmpty()) { return QStringLiteral(""); }
if (path.endsWith('/')) { return CFileUtils::lastPathSegment(path.left(path.length() - 1)); }
if (!path.contains('/')) { return path; }
return path.mid(path.lastIndexOf('/') + 1);
}
QString CFileUtils::appendFilePaths(const QString &path1, const QString &path2, const QString &path3)
{
return CFileUtils::appendFilePaths(CFileUtils::appendFilePaths(path1, path2), path3);

View File

@@ -79,9 +79,12 @@ namespace BlackMisc
//! \sa CNetworkUtils::buildUrl for URLs
static QString appendFilePathsAndFixUnc(const QString &path1, const QString &path2, const QString &path3);
//! Strip file from path a/b/c.json a/b
//! Strip file from path a/b/c.json a/b, return path
static QString stripFileFromPath(const QString &path);
//! Last path segment a/b/c => c
static QString lastPathSegment(const QString &path);
//! Normalize file path to Qt standard, e.g by turning \ to /
static QString normalizeFilePathToQtStandard(const QString &filePath);

View File

@@ -17,6 +17,8 @@
#include <QSettings>
#include <QStringList>
#include <QVariant>
#include <QFileInfo>
#include <QStringBuilder>
using namespace BlackConfig;
using namespace BlackMisc;
@@ -313,6 +315,38 @@ namespace BlackMisc
static const QStringList exclude;
return exclude;
}
bool CFsCommonUtil::adjustFileDirectory(CAircraftModel &model, const QString &simObjectsDirectory)
{
if (model.hasExistingCorrespondingFile()) { return true; }
if (simObjectsDirectory.isEmpty()) { return false; }
if (!model.hasFileName()) { return false; } // we can do nothing here
const QString simObjectsDirectoryFix = CFileUtils::fixWindowsUncPath(simObjectsDirectory);
const QDir dir(simObjectsDirectoryFix);
if (!dir.exists()) { return false; }
const QString lastSegment = QStringLiteral("/") % CFileUtils::lastPathSegment(simObjectsDirectoryFix) % QStringLiteral("/");
const int index = model.getFileName().lastIndexOf(lastSegment);
if (index < 0) { return false; }
const QString relPart = model.getFileName().mid(index + lastSegment.length());
if (relPart.isEmpty()) { return false; }
const QString newFile = CFileUtils::appendFilePathsAndFixUnc(simObjectsDirectory, relPart);
const QFileInfo nf(newFile);
if (!nf.exists()) { return false; }
model.setFileName(newFile);
return true;
}
bool CFsCommonUtil::adjustFileDirectory(CAircraftModel &model, const QStringList &simObjectsDirectories)
{
for (const QString &simObjectDir : simObjectsDirectories)
{
if (CFsCommonUtil::adjustFileDirectory(model, simObjectDir)) { return true; }
}
return false;
}
} // namespace
} // namespace
} // namespace

View File

@@ -12,8 +12,8 @@
#ifndef BLACKMISC_SIMULATION_FSCOMMONUTIL_H
#define BLACKMISC_SIMULATION_FSCOMMONUTIL_H
#include "blackmisc/simulation/aircraftmodel.h"
#include "blackmisc/blackmiscexport.h"
#include <QString>
class QStringList;
@@ -84,6 +84,12 @@ namespace BlackMisc
//! Exclude directories for aircraft objects
static const QStringList &fs9AircraftObjectsExcludeDirectoryPatterns();
//! Adjust file directory
static bool adjustFileDirectory(CAircraftModel &model, const QString &simObjectsDirectory);
//! Adjust file directory
static bool adjustFileDirectory(CAircraftModel &model, const QStringList &simObjectsDirectories);
};
} // namespace
} // namespace