mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-23 05:45:35 +08:00
refs #438, allow to cancel parser
* added flag for parsing success * unload driver cancels parsing * assert for invoke * using waitForFinished
This commit is contained in:
@@ -21,6 +21,11 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
namespace FsCommon
|
namespace FsCommon
|
||||||
{
|
{
|
||||||
|
CAircraftCfgParser::~CAircraftCfgParser()
|
||||||
|
{
|
||||||
|
// that should be safe as long as the worker uses deleteLater (which it does)
|
||||||
|
if (this->m_parserWorker) { this->m_parserWorker->waitForFinished(); }
|
||||||
|
}
|
||||||
|
|
||||||
bool CAircraftCfgParser::changeRootDirectory(const QString &directory)
|
bool CAircraftCfgParser::changeRootDirectory(const QString &directory)
|
||||||
{
|
{
|
||||||
@@ -28,7 +33,6 @@ namespace BlackMisc
|
|||||||
if (directory.isEmpty() || !existsDir(directory)) { return false; }
|
if (directory.isEmpty() || !existsDir(directory)) { return false; }
|
||||||
|
|
||||||
m_rootDirectory = directory;
|
m_rootDirectory = directory;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,55 +40,61 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
if (mode == ModeAsync)
|
if (mode == ModeAsync)
|
||||||
{
|
{
|
||||||
if (m_parserWorker && !m_parserWorker->isFinished()) return;
|
if (m_parserWorker && !m_parserWorker->isFinished()) { return; }
|
||||||
|
|
||||||
auto rootDirectory = m_rootDirectory;
|
auto rootDirectory = m_rootDirectory;
|
||||||
auto excludedDirectories = m_excludedDirectories;
|
auto excludedDirectories = m_excludedDirectories;
|
||||||
|
|
||||||
m_parserWorker = BlackMisc::CWorker::fromTask(this, "CAircraftCfgParser::changeDirectory",
|
m_parserWorker = BlackMisc::CWorker::fromTask(this, "CAircraftCfgParser::changeDirectory",
|
||||||
[this, rootDirectory, excludedDirectories]()
|
[this, rootDirectory, excludedDirectories]()
|
||||||
{
|
{
|
||||||
auto aircraftCfgEntriesList = parseImpl(rootDirectory, excludedDirectories);
|
bool ok;
|
||||||
QMetaObject::invokeMethod(this, "ps_updateCfgEntriesList",
|
auto aircraftCfgEntriesList = parseImpl(rootDirectory, excludedDirectories, &ok);
|
||||||
|
if (!ok) { return; }
|
||||||
|
bool c = QMetaObject::invokeMethod(this, "ps_updateCfgEntriesList",
|
||||||
Q_ARG(BlackMisc::Simulation::FsCommon::CAircraftCfgEntriesList, aircraftCfgEntriesList));
|
Q_ARG(BlackMisc::Simulation::FsCommon::CAircraftCfgEntriesList, aircraftCfgEntriesList));
|
||||||
|
Q_ASSERT_X(c, Q_FUNC_INFO, "Cannot invoke ps_updateCfgEntriesList");
|
||||||
|
Q_UNUSED(c);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (mode == ModeBlocking)
|
else if (mode == ModeBlocking)
|
||||||
{
|
{
|
||||||
m_parsedCfgEntriesList = parseImpl(m_rootDirectory, m_excludedDirectories);
|
bool ok;
|
||||||
}
|
m_parsedCfgEntriesList = parseImpl(m_rootDirectory, m_excludedDirectories, &ok);
|
||||||
else
|
emit parsingFinished(ok);
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CAircraftCfgParser::ps_updateCfgEntriesList(const CAircraftCfgEntriesList &cfgEntriesList)
|
void CAircraftCfgParser::ps_updateCfgEntriesList(const CAircraftCfgEntriesList &cfgEntriesList)
|
||||||
{
|
{
|
||||||
m_parsedCfgEntriesList = cfgEntriesList;
|
m_parsedCfgEntriesList = cfgEntriesList;
|
||||||
emit parsingFinished();
|
emit parsingFinished(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
CAircraftCfgEntriesList CAircraftCfgParser::parseImpl(const QString &directory, const QStringList &excludeDirectories)
|
CAircraftCfgEntriesList CAircraftCfgParser::parseImpl(const QString &directory, const QStringList &excludeDirectories, bool *ok)
|
||||||
{
|
{
|
||||||
|
*ok = false;
|
||||||
if (m_cancelParsing) { return CAircraftCfgEntriesList(); }
|
if (m_cancelParsing) { return CAircraftCfgEntriesList(); }
|
||||||
|
|
||||||
// excluded?
|
// excluded?
|
||||||
for (const auto &excludeDir : excludeDirectories)
|
for (const auto &excludeDir : excludeDirectories)
|
||||||
{
|
{
|
||||||
|
if (m_cancelParsing) { return CAircraftCfgEntriesList(); }
|
||||||
if (directory.contains(excludeDir, Qt::CaseInsensitive))
|
if (directory.contains(excludeDir, Qt::CaseInsensitive))
|
||||||
{
|
{
|
||||||
CLogMessage(this).debug() << "Skipping directory " << directory;
|
CLogMessage(this).debug() << "Skipping directory " << directory;
|
||||||
|
*ok = true;
|
||||||
return CAircraftCfgEntriesList();
|
return CAircraftCfgEntriesList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// set directory with name filters, get aircraft.cfg and sub directories
|
// set directory with name filters, get aircraft.cfg and sub directories
|
||||||
QDir dir(directory, "aircraft.cfg", QDir::Name, QDir::Files | QDir::AllDirs);
|
QDir dir(directory, "aircraft.cfg", QDir::Name, QDir::Files | QDir::AllDirs);
|
||||||
if (!dir.exists()) return CAircraftCfgEntriesList(); // can happen if there are shortcuts or linked dirs not available
|
if (!dir.exists())
|
||||||
|
{
|
||||||
|
*ok = true;
|
||||||
|
return CAircraftCfgEntriesList(); // can happen if there are shortcuts or linked dirs not available
|
||||||
|
}
|
||||||
|
|
||||||
QString currentDir = dir.absolutePath();
|
QString currentDir = dir.absolutePath();
|
||||||
|
|
||||||
CAircraftCfgEntriesList result;
|
CAircraftCfgEntriesList result;
|
||||||
|
|
||||||
// Dirs last is crucial,since I will break recursion on "aircraft.cfg" level
|
// Dirs last is crucial,since I will break recursion on "aircraft.cfg" level
|
||||||
@@ -97,7 +107,17 @@ namespace BlackMisc
|
|||||||
QString nextDir = file.absoluteFilePath();
|
QString nextDir = file.absoluteFilePath();
|
||||||
if (currentDir.startsWith(nextDir, Qt::CaseInsensitive)) { continue; } // do not go up
|
if (currentDir.startsWith(nextDir, Qt::CaseInsensitive)) { continue; } // do not go up
|
||||||
if (dir == currentDir) { continue; } // do not recursively call same directory
|
if (dir == currentDir) { continue; } // do not recursively call same directory
|
||||||
result.push_back(parseImpl(nextDir, excludeDirectories));
|
|
||||||
|
bool dirOk;
|
||||||
|
const CAircraftCfgEntriesList subList(parseImpl(nextDir, excludeDirectories, &dirOk));
|
||||||
|
if (dirOk)
|
||||||
|
{
|
||||||
|
result.push_back(subList);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CLogMessage(this).warning("Parsing failed for %1") << nextDir;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -200,7 +220,6 @@ namespace BlackMisc
|
|||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
// store all entries
|
// store all entries
|
||||||
|
|
||||||
for (const CAircraftCfgEntries &e : tempEntries)
|
for (const CAircraftCfgEntries &e : tempEntries)
|
||||||
{
|
{
|
||||||
if (e.getTitle().isEmpty())
|
if (e.getTitle().isEmpty())
|
||||||
@@ -213,9 +232,14 @@ namespace BlackMisc
|
|||||||
newEntries.setAtcType(atcType);
|
newEntries.setAtcType(atcType);
|
||||||
result.push_back(newEntries);
|
result.push_back(newEntries);
|
||||||
}
|
}
|
||||||
|
*ok = true;
|
||||||
return result; // do not go any deeper in file tree, we found aircraft.cfg
|
return result; // do not go any deeper in file tree, we found aircraft.cfg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// all files finished,
|
||||||
|
// normally reached when no aircraft.cfg is found
|
||||||
|
*ok = true;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,16 +284,13 @@ namespace BlackMisc
|
|||||||
|
|
||||||
QString content(line.mid(index + 1).trimmed());
|
QString content(line.mid(index + 1).trimmed());
|
||||||
|
|
||||||
// fix "" strings, some are malformed and just contain " at beginning, end
|
// fix "" strings, some are malformed and just contain " at beginning, not at the end
|
||||||
if (content.endsWith('"')) { content.remove(content.size() - 1 , 1); }
|
if (content.endsWith('"')) { content.remove(content.size() - 1 , 1); }
|
||||||
if (content.startsWith('"')) { content.remove(0 , 1); }
|
if (content.startsWith('"')) { content.remove(0 , 1); }
|
||||||
|
|
||||||
// fix C style linebreaks
|
// fix C style linebreaks
|
||||||
content.replace("\\n", " ");
|
content.replace("\\n", " ");
|
||||||
content.replace("\\t", " ");
|
content.replace("\\t", " ");
|
||||||
|
|
||||||
// return
|
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,14 +24,12 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
namespace FsCommon
|
namespace FsCommon
|
||||||
{
|
{
|
||||||
|
|
||||||
//! Utility, parsing the aircraft.cfg files
|
//! Utility, parsing the aircraft.cfg files
|
||||||
class BLACKMISC_EXPORT CAircraftCfgParser : public QObject
|
class BLACKMISC_EXPORT CAircraftCfgParser : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
//! Parser mode
|
//! Parser mode
|
||||||
enum ParserMode
|
enum ParserMode
|
||||||
{
|
{
|
||||||
@@ -48,7 +46,7 @@ namespace BlackMisc
|
|||||||
{ }
|
{ }
|
||||||
|
|
||||||
//! Virtual destructor
|
//! Virtual destructor
|
||||||
virtual ~CAircraftCfgParser() {}
|
virtual ~CAircraftCfgParser();
|
||||||
|
|
||||||
//! Change the directory
|
//! Change the directory
|
||||||
bool changeRootDirectory(const QString &directory);
|
bool changeRootDirectory(const QString &directory);
|
||||||
@@ -69,20 +67,15 @@ namespace BlackMisc
|
|||||||
CAircraftCfgEntriesList getAircraftCfgEntriesList() const { return m_parsedCfgEntriesList; }
|
CAircraftCfgEntriesList getAircraftCfgEntriesList() const { return m_parsedCfgEntriesList; }
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
||||||
//! Parsing is finished
|
//! Parsing is finished
|
||||||
void parsingFinished();
|
void parsingFinished(bool success);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
void ps_updateCfgEntriesList(const BlackMisc::Simulation::FsCommon::CAircraftCfgEntriesList &cfgEntriesList);
|
void ps_updateCfgEntriesList(const BlackMisc::Simulation::FsCommon::CAircraftCfgEntriesList &cfgEntriesList);
|
||||||
|
CAircraftCfgEntriesList parseImpl(const QString &directory, const QStringList &excludeDirectories, bool *ok);
|
||||||
CAircraftCfgEntriesList parseImpl(const QString &directory, const QStringList &excludeDirectories = {});
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
//! Section within file
|
//! Section within file
|
||||||
|
|
||||||
enum FileSection
|
enum FileSection
|
||||||
{
|
{
|
||||||
General,
|
General,
|
||||||
@@ -104,12 +97,9 @@ namespace BlackMisc
|
|||||||
|
|
||||||
QString m_rootDirectory; //!< root directory parsing aircraft.cfg files
|
QString m_rootDirectory; //!< root directory parsing aircraft.cfg files
|
||||||
QStringList m_excludedDirectories;
|
QStringList m_excludedDirectories;
|
||||||
|
|
||||||
CAircraftCfgEntriesList m_parsedCfgEntriesList;
|
CAircraftCfgEntriesList m_parsedCfgEntriesList;
|
||||||
QPointer<BlackMisc::CWorker> m_parserWorker;
|
QPointer<BlackMisc::CWorker> m_parserWorker; //!< worker will destroy itself, so weak pointer
|
||||||
|
|
||||||
std::atomic<bool> m_cancelParsing = { false };
|
std::atomic<bool> m_cancelParsing = { false };
|
||||||
|
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ namespace BlackMisc
|
|||||||
void CAircraftMatcher::setModelMappingProvider(std::unique_ptr<IModelMappingsProvider> mappings)
|
void CAircraftMatcher::setModelMappingProvider(std::unique_ptr<IModelMappingsProvider> mappings)
|
||||||
{
|
{
|
||||||
m_mappingsProvider = std::move(mappings);
|
m_mappingsProvider = std::move(mappings);
|
||||||
if (m_matchingMode.testFlag(ModelMapping)) initMappings();
|
if (m_matchingMode.testFlag(ModelMapping)) { initMappings(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
void CAircraftMatcher::setMatchingModes(MatchingMode matchingModes)
|
void CAircraftMatcher::setMatchingModes(MatchingMode matchingModes)
|
||||||
@@ -157,9 +157,9 @@ namespace BlackMisc
|
|||||||
// finish
|
// finish
|
||||||
CLogMessage(this).info("Mapping system: %1 definitions for %2 installed models") << m_modelMappings.size()
|
CLogMessage(this).info("Mapping system: %1 definitions for %2 installed models") << m_modelMappings.size()
|
||||||
<< m_installedModels.size();
|
<< m_installedModels.size();
|
||||||
emit initializationFinished();
|
|
||||||
m_initInProgress = false;
|
m_initInProgress = false;
|
||||||
m_initialized = true;
|
m_initialized = true;
|
||||||
|
emit initializationFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CAircraftMatcher::initMappings()
|
void CAircraftMatcher::initMappings()
|
||||||
|
|||||||
@@ -42,12 +42,10 @@ namespace BlackMisc
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
||||||
//! Full init completed
|
//! Full init completed
|
||||||
void initializationFinished();
|
void initializationFinished();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
//! Enabled matching mode flags
|
//! Enabled matching mode flags
|
||||||
enum MatchingModeFlag
|
enum MatchingModeFlag
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -23,9 +23,10 @@ namespace BlackMisc
|
|||||||
worker->setObjectName(name);
|
worker->setObjectName(name);
|
||||||
|
|
||||||
worker->moveToThread(thread);
|
worker->moveToThread(thread);
|
||||||
QMetaObject::invokeMethod(worker, "ps_runTask");
|
bool s = QMetaObject::invokeMethod(worker, "ps_runTask");
|
||||||
|
Q_ASSERT_X(s, Q_FUNC_INFO, "cannot invoke");
|
||||||
|
Q_UNUSED(s);
|
||||||
thread->start();
|
thread->start();
|
||||||
|
|
||||||
return worker;
|
return worker;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,11 @@ namespace BlackSimPlugin
|
|||||||
if (aircraftCfg.isValid())
|
if (aircraftCfg.isValid())
|
||||||
{
|
{
|
||||||
m_modelMatcher.setInstalledModels(aircraftCfg.value<CAircraftCfgEntriesList>().toAircraftModelList());
|
m_modelMatcher.setInstalledModels(aircraftCfg.value<CAircraftCfgEntriesList>().toAircraftModelList());
|
||||||
|
m_modelMatcher.init();
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
// reading from settings would go here
|
||||||
|
//
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
connect(&m_aircraftCfgParser, &CAircraftCfgParser::parsingFinished, this, &CSimulatorFsCommon::ps_aircraftCfgParsingFinished);
|
connect(&m_aircraftCfgParser, &CAircraftCfgParser::parsingFinished, this, &CSimulatorFsCommon::ps_aircraftCfgParsingFinished);
|
||||||
@@ -55,8 +59,7 @@ namespace BlackSimPlugin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CSimulatorFsCommon::~CSimulatorFsCommon()
|
CSimulatorFsCommon::~CSimulatorFsCommon() { }
|
||||||
{ }
|
|
||||||
|
|
||||||
void CSimulatorFsCommon::ps_mapperInitialized()
|
void CSimulatorFsCommon::ps_mapperInitialized()
|
||||||
{
|
{
|
||||||
@@ -209,8 +212,15 @@ namespace BlackSimPlugin
|
|||||||
CSimulatorCommon::enableDebugMessages(driver, interpolator);
|
CSimulatorCommon::enableDebugMessages(driver, interpolator);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSimulatorFsCommon::ps_aircraftCfgParsingFinished()
|
void CSimulatorFsCommon::unload()
|
||||||
{
|
{
|
||||||
|
this->m_aircraftCfgParser.cancelParsing();
|
||||||
|
CSimulatorCommon::unload();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CSimulatorFsCommon::ps_aircraftCfgParsingFinished(bool success)
|
||||||
|
{
|
||||||
|
if (!success) { return; }
|
||||||
setPluginData(this, "aircraft_cfg", CVariant::from(m_aircraftCfgParser.getAircraftCfgEntriesList()));
|
setPluginData(this, "aircraft_cfg", CVariant::from(m_aircraftCfgParser.getAircraftCfgEntriesList()));
|
||||||
m_modelMatcher.setInstalledModels(m_aircraftCfgParser.getAircraftCfgEntriesList().toAircraftModelList());
|
m_modelMatcher.setInstalledModels(m_aircraftCfgParser.getAircraftCfgEntriesList().toAircraftModelList());
|
||||||
|
|
||||||
|
|||||||
@@ -76,6 +76,9 @@ namespace BlackSimPlugin
|
|||||||
//! \copydoc ISimulator::enableDebuggingMessages
|
//! \copydoc ISimulator::enableDebuggingMessages
|
||||||
virtual void enableDebugMessages(bool driver, bool interpolator) override;
|
virtual void enableDebugMessages(bool driver, bool interpolator) override;
|
||||||
|
|
||||||
|
//! \copydoc ISimulator::unload
|
||||||
|
virtual void unload() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//! Constructor
|
//! Constructor
|
||||||
CSimulatorFsCommon(const BlackMisc::Simulation::CSimulatorPluginInfo &info,
|
CSimulatorFsCommon(const BlackMisc::Simulation::CSimulatorPluginInfo &info,
|
||||||
@@ -99,7 +102,9 @@ namespace BlackSimPlugin
|
|||||||
BlackMisc::Aviation::CComSystem m_simCom2; //!< cockpit COM2 state in simulator
|
BlackMisc::Aviation::CComSystem m_simCom2; //!< cockpit COM2 state in simulator
|
||||||
BlackMisc::Aviation::CTransponder m_simTransponder; //!< cockpit xpdr state in simulator
|
BlackMisc::Aviation::CTransponder m_simTransponder; //!< cockpit xpdr state in simulator
|
||||||
|
|
||||||
|
// parser / matcher
|
||||||
BlackMisc::Simulation::FsCommon::CAircraftCfgParser m_aircraftCfgParser; //!< aircraft.cfg parser
|
BlackMisc::Simulation::FsCommon::CAircraftCfgParser m_aircraftCfgParser; //!< aircraft.cfg parser
|
||||||
|
BlackMisc::Simulation::FsCommon::CAircraftMatcher m_modelMatcher; //!< Model matcher
|
||||||
|
|
||||||
//! Set own model
|
//! Set own model
|
||||||
void setOwnAircraftModel(const BlackMisc::Simulation::CAircraftModel &model);
|
void setOwnAircraftModel(const BlackMisc::Simulation::CAircraftModel &model);
|
||||||
@@ -110,15 +115,12 @@ namespace BlackSimPlugin
|
|||||||
//! Reverse lookup
|
//! Reverse lookup
|
||||||
void reverseLookupIcaoData(BlackMisc::Simulation::CAircraftModel &model);
|
void reverseLookupIcaoData(BlackMisc::Simulation::CAircraftModel &model);
|
||||||
|
|
||||||
BlackMisc::Simulation::FsCommon::CAircraftMatcher m_modelMatcher; //!< Model matcher
|
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
|
|
||||||
//! Mapper has been initialized
|
//! Mapper has been initialized
|
||||||
void ps_mapperInitialized();
|
void ps_mapperInitialized();
|
||||||
|
|
||||||
//! aircraft.cfg files parsing is finished
|
//! aircraft.cfg files parsing is finished
|
||||||
void ps_aircraftCfgParsingFinished();
|
void ps_aircraftCfgParsingFinished(bool success);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
Reference in New Issue
Block a user