mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-05-03 16:25:54 +08:00
refs #273, simulator context
* fixed loading of plugins * sorting filenames, and plugin names * better status messages when loading plugins
This commit is contained in:
@@ -44,6 +44,7 @@ namespace BlackCore
|
|||||||
{
|
{
|
||||||
simulatorPlugins.push_back(factory->getSimulatorInfo());
|
simulatorPlugins.push_back(factory->getSimulatorInfo());
|
||||||
}
|
}
|
||||||
|
simulatorPlugins.sortBy(&CSimulatorInfo::getShortName);
|
||||||
return simulatorPlugins;
|
return simulatorPlugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,13 +104,28 @@ namespace BlackCore
|
|||||||
if (this->m_simulator && this->m_simulator->getSimulatorInfo() == simulatorInfo) { return true; } // already loaded
|
if (this->m_simulator && this->m_simulator->getSimulatorInfo() == simulatorInfo) { return true; } // already loaded
|
||||||
if (simulatorInfo.isUnspecified()) { return false; }
|
if (simulatorInfo.isUnspecified()) { return false; }
|
||||||
|
|
||||||
|
// warning if we do not have any plugins
|
||||||
|
if (m_simulatorFactories.isEmpty())
|
||||||
|
{
|
||||||
|
this->getRuntime()->sendStatusMessage(CStatusMessage::getErrorMessage("No simulator plugins", CStatusMessage::TypeSimulator));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
ISimulatorFactory *factory = nullptr;
|
ISimulatorFactory *factory = nullptr;
|
||||||
QSet<ISimulatorFactory *>::iterator iterator = std::find_if(m_simulatorFactories.begin(), m_simulatorFactories.end(), [ = ](const ISimulatorFactory * factory)
|
QSet<ISimulatorFactory *>::iterator iterator = std::find_if(m_simulatorFactories.begin(), m_simulatorFactories.end(), [ = ](const ISimulatorFactory * factory)
|
||||||
{
|
{
|
||||||
return factory->getSimulatorInfo() == simulatorInfo;
|
return factory->getSimulatorInfo() == simulatorInfo;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (iterator == m_simulatorFactories.end()) { return false; }
|
// no plugin found
|
||||||
|
if (iterator == m_simulatorFactories.end())
|
||||||
|
{
|
||||||
|
QString m = QString("Plugin not found: '%1'").arg(simulatorInfo.toQString(true));
|
||||||
|
this->getRuntime()->sendStatusMessage(CStatusMessage::getErrorMessage(m, CStatusMessage::TypeSimulator));
|
||||||
|
qCritical() << m;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
factory = *iterator;
|
factory = *iterator;
|
||||||
Q_ASSERT(factory);
|
Q_ASSERT(factory);
|
||||||
|
|
||||||
@@ -122,6 +138,10 @@ namespace BlackCore
|
|||||||
connect(m_simulator, SIGNAL(statusChanged(ISimulator::Status)), this, SLOT(setConnectionStatus(ISimulator::Status)));
|
connect(m_simulator, SIGNAL(statusChanged(ISimulator::Status)), this, SLOT(setConnectionStatus(ISimulator::Status)));
|
||||||
connect(m_simulator, &ISimulator::aircraftModelChanged, this, &IContextSimulator::ownAircraftModelChanged);
|
connect(m_simulator, &ISimulator::aircraftModelChanged, this, &IContextSimulator::ownAircraftModelChanged);
|
||||||
asyncConnectTo(); // try to connect
|
asyncConnectTo(); // try to connect
|
||||||
|
|
||||||
|
QString m = QString("Simulator plugin loaded: '%1'").arg(this->m_simulator->getSimulatorInfo().toQString(true));
|
||||||
|
this->getRuntime()->sendStatusMessage(CStatusMessage::getInfoMessage(m, CStatusMessage::TypeSimulator));
|
||||||
|
qDebug() << m;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,13 +150,13 @@ namespace BlackCore
|
|||||||
Q_ASSERT(this->getIContextSettings());
|
Q_ASSERT(this->getIContextSettings());
|
||||||
if (!this->getIContextSettings()) return false;
|
if (!this->getIContextSettings()) return false;
|
||||||
|
|
||||||
CSimulatorInfoList drivers = this->getAvailableSimulatorPlugins();
|
CSimulatorInfoList plugin = this->getAvailableSimulatorPlugins();
|
||||||
if (drivers.size() == 1)
|
if (plugin.size() == 1)
|
||||||
{
|
{
|
||||||
// load, independent from settings, we have only driver
|
// load, independent from settings, we have only driver
|
||||||
return this->loadSimulatorPlugin(drivers.front());
|
return this->loadSimulatorPlugin(plugin.front());
|
||||||
}
|
}
|
||||||
else if (drivers.size() > 1)
|
else if (plugin.size() > 1)
|
||||||
{
|
{
|
||||||
return this->loadSimulatorPlugin(
|
return this->loadSimulatorPlugin(
|
||||||
this->getIContextSettings()->getSimulatorSettings().getSelectedDriver()
|
this->getIContextSettings()->getSimulatorSettings().getSelectedDriver()
|
||||||
@@ -150,7 +170,12 @@ namespace BlackCore
|
|||||||
|
|
||||||
void CContextSimulator::unloadSimulatorPlugin()
|
void CContextSimulator::unloadSimulatorPlugin()
|
||||||
{
|
{
|
||||||
if (m_simulator) { m_simulator->deleteLater(); }
|
if (m_simulator)
|
||||||
|
{
|
||||||
|
disconnect(m_simulator); // disconnect as receiver straight away
|
||||||
|
m_simulator->disconnectFrom(); // disconnect from simulator
|
||||||
|
m_simulator->deleteLater();
|
||||||
|
}
|
||||||
m_simulator = nullptr;
|
m_simulator = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,12 +201,14 @@ namespace BlackCore
|
|||||||
void CContextSimulator::addAircraftSituation(const CCallsign &callsign, const CAircraftSituation &initialSituation)
|
void CContextSimulator::addAircraftSituation(const CCallsign &callsign, const CAircraftSituation &initialSituation)
|
||||||
{
|
{
|
||||||
Q_ASSERT(this->m_simulator);
|
Q_ASSERT(this->m_simulator);
|
||||||
|
if (!this->m_simulator) return;
|
||||||
this->m_simulator->addAircraftSituation(callsign, initialSituation);
|
this->m_simulator->addAircraftSituation(callsign, initialSituation);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CContextSimulator::updateCockpitFromContext(const CAircraft &ownAircraft, const QString &originator)
|
void CContextSimulator::updateCockpitFromContext(const CAircraft &ownAircraft, const QString &originator)
|
||||||
{
|
{
|
||||||
Q_ASSERT(this->m_simulator);
|
Q_ASSERT(this->m_simulator);
|
||||||
|
if (!this->m_simulator) return;
|
||||||
|
|
||||||
// avoid loops
|
// avoid loops
|
||||||
if (originator.isEmpty() || originator == IContextSimulator::InterfaceName()) return;
|
if (originator.isEmpty() || originator == IContextSimulator::InterfaceName()) return;
|
||||||
@@ -220,6 +247,7 @@ namespace BlackCore
|
|||||||
|
|
||||||
void CContextSimulator::textMessagesReceived(const Network::CTextMessageList &textMessages)
|
void CContextSimulator::textMessagesReceived(const Network::CTextMessageList &textMessages)
|
||||||
{
|
{
|
||||||
|
if (!this->m_simulator) return;
|
||||||
foreach(CTextMessage tm, textMessages)
|
foreach(CTextMessage tm, textMessages)
|
||||||
{
|
{
|
||||||
if (!tm.isPrivateMessage()) continue;
|
if (!tm.isPrivateMessage()) continue;
|
||||||
@@ -238,32 +266,31 @@ namespace BlackCore
|
|||||||
if (this->loadSimulatorPlugin(driver))
|
if (this->loadSimulatorPlugin(driver))
|
||||||
{
|
{
|
||||||
QString m = QString("Driver loaded: '%1'").arg(driver.toQString(true));
|
QString m = QString("Driver loaded: '%1'").arg(driver.toQString(true));
|
||||||
if (this->getIContextApplication())
|
this->getRuntime()->sendStatusMessage(CStatusMessage::getInfoMessage(m, CStatusMessage::TypeSimulator));
|
||||||
this->getIContextApplication()->sendStatusMessage(CStatusMessage::getInfoMessage(m, CStatusMessage::TypeSimulator));
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
QString m = QString("Cannot load driver: '%1'").arg(driver.toQString(true));
|
QString m = QString("Cannot load driver: '%1'").arg(driver.toQString(true));
|
||||||
if (this->getIContextApplication())
|
this->getRuntime()->sendStatusMessage(CStatusMessage::getErrorMessage(m, CStatusMessage::TypeSimulator));
|
||||||
this->getIContextApplication()->sendStatusMessage(CStatusMessage::getErrorMessage(m, CStatusMessage::TypeSimulator));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CContextSimulator::findSimulatorPlugins()
|
void CContextSimulator::findSimulatorPlugins()
|
||||||
{
|
{
|
||||||
m_pluginsDir = QDir(qApp->applicationDirPath().append("/plugins/simulator"));
|
const QString path = qApp->applicationDirPath().append("/plugins/simulator");
|
||||||
|
m_pluginsDir = QDir(path);
|
||||||
if (!m_pluginsDir.exists())
|
if (!m_pluginsDir.exists())
|
||||||
{
|
{
|
||||||
qWarning() << "No plugin directory" << m_pluginsDir.currentPath();
|
qWarning() << "No plugin directory" << m_pluginsDir.currentPath();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach(QString fileName, m_pluginsDir.entryList(QDir::Files))
|
QStringList fileNames = m_pluginsDir.entryList(QDir::Files);
|
||||||
|
fileNames.sort(Qt::CaseInsensitive); // give a certain order, rather than random file order
|
||||||
|
foreach(QString fileName, fileNames)
|
||||||
{
|
{
|
||||||
if (!QLibrary::isLibrary(fileName))
|
if (!QLibrary::isLibrary(fileName)) { continue; }
|
||||||
continue;
|
|
||||||
|
|
||||||
QString pluginPath = m_pluginsDir.absoluteFilePath(fileName);
|
QString pluginPath = m_pluginsDir.absoluteFilePath(fileName);
|
||||||
QPluginLoader loader(pluginPath);
|
QPluginLoader loader(pluginPath);
|
||||||
QObject *plugin = loader.instance();
|
QObject *plugin = loader.instance();
|
||||||
@@ -275,7 +302,6 @@ namespace BlackCore
|
|||||||
CSimulatorInfo simulatorInfo = factory->getSimulatorInfo();
|
CSimulatorInfo simulatorInfo = factory->getSimulatorInfo();
|
||||||
qDebug() << "Found simulator plugin: " << simulatorInfo.toQString();
|
qDebug() << "Found simulator plugin: " << simulatorInfo.toQString();
|
||||||
m_simulatorFactories.insert(factory);
|
m_simulatorFactories.insert(factory);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user