diff --git a/src/blackcore/context_runtime.cpp b/src/blackcore/context_runtime.cpp index d70101925..10e16459b 100644 --- a/src/blackcore/context_runtime.cpp +++ b/src/blackcore/context_runtime.cpp @@ -409,9 +409,10 @@ namespace BlackCore Q_ASSERT(c); } - if (this->m_contextSimulator && this->m_contextSimulator->usingLocalObjects() && this->getCContextSimulator()->m_simulator) + // local simulator? + if (this->m_contextSimulator && this->m_contextSimulator->usingLocalObjects()) { - // only connect if simulator running locally, no round trips + // only connect if simulator runs locally, no round trips if (this->m_contextNetwork && this->m_contextNetwork->usingLocalObjects()) { c = connect(this->m_contextNetwork, &IContextNetwork::changedAircraftSituation, @@ -423,7 +424,7 @@ namespace BlackCore Q_ASSERT(c); } - // only if own aircraft running locally + // only if own aircraft runs locally if (this->m_contextOwnAircraft && this->m_contextOwnAircraft->usingLocalObjects()) { c = connect(this->m_contextOwnAircraft, &IContextOwnAircraft::changedAircraftCockpit, @@ -431,7 +432,7 @@ namespace BlackCore Q_ASSERT(c); } - // only if own aircraft running locally + // only if application runs locally if (this->m_contextApplication && this->m_contextApplication->usingLocalObjects()) { c = connect(this->m_contextApplication, &IContextApplication::statusMessage, @@ -442,11 +443,25 @@ namespace BlackCore this->getCContextSimulator(), &CContextSimulator::statusMessagesReceived); Q_ASSERT(c); } + + // connect local simulator and settings + if (this->m_contextSettings) + { + connect(this->m_contextSettings, &IContextSettings::changedSettings, this->m_contextSimulator, &IContextSimulator::settingsChanged); + if (this->m_contextSimulator->loadSimulatorPluginFromSettings()) + { + qDebug() << "Simulator driver loaded"; + } + else + { + qWarning() << "No simulator driver loaded"; + } + } } + // only where network and(!) own aircraft run locally if (this->m_contextNetwork && this->m_contextOwnAircraft && this->m_contextNetwork->usingLocalObjects() && this->m_contextOwnAircraft->usingLocalObjects()) { - // only where network and(!) own aircraft run locally c = this->connect(this->m_contextNetwork, &IContextNetwork::changedAtcStationOnlineConnectionStatus, this->getCContextOwnAircraft(), &CContextOwnAircraft::changedAtcStationOnlineConnectionStatus); Q_ASSERT(c); diff --git a/src/blackcore/context_simulator.h b/src/blackcore/context_simulator.h index dca6fc733..f74f7fbb9 100644 --- a/src/blackcore/context_simulator.h +++ b/src/blackcore/context_simulator.h @@ -87,12 +87,18 @@ namespace BlackCore //! Load specific simulator plugin virtual bool loadSimulatorPlugin(const BlackSim::CSimulatorInfo &simulatorInfo) = 0; + //! Load specific simulator plugin as set in settings + virtual bool loadSimulatorPluginFromSettings() = 0; + //! Unload simulator plugin virtual void unloadSimulatorPlugin() = 0; //! Simulator avialable? bool isSimulatorAvailable() const { return BlackMisc::CProject::isCompiledWithFlightSimulatorSupport() && !getSimulatorInfo().isUnspecified(); } + //! Settings have been changed + virtual void settingsChanged(uint type) = 0; + protected: //! \brief Constructor IContextSimulator(CRuntimeConfig::ContextMode mode, CRuntime *runtime) : CContext(mode, runtime) {} diff --git a/src/blackcore/context_simulator_impl.cpp b/src/blackcore/context_simulator_impl.cpp index 4eef29bd1..404a4562b 100644 --- a/src/blackcore/context_simulator_impl.cpp +++ b/src/blackcore/context_simulator_impl.cpp @@ -5,6 +5,8 @@ #include "context_simulator_impl.h" #include "context_ownaircraft.h" +#include "context_settings.h" +#include "context_application.h" #include #include #include "context_runtime.h" @@ -23,10 +25,10 @@ namespace BlackCore { m_updateTimer = new QTimer(this); findSimulatorPlugins(); - loadSimulatorPlugin(CSimulatorInfo::FSX()); - connect(m_updateTimer, &QTimer::timeout, this, &CContextSimulator::updateOwnAircraft); - asyncConnectTo(); + + // do not load plugin here, as it depends on settings + // it has to be guaranteed the settings are alredy loaded } CContextSimulator::~CContextSimulator() @@ -98,31 +100,57 @@ namespace BlackCore bool CContextSimulator::loadSimulatorPlugin(const CSimulatorInfo &simulatorInfo) { + if (this->m_simulator && this->m_simulator->getSimulatorInfo() == simulatorInfo) { return true; } // already loaded + if (simulatorInfo.isUnspecified()) { return false; } + ISimulatorFactory *factory = nullptr; QSet::iterator iterator = std::find_if(m_simulatorFactories.begin(), m_simulatorFactories.end(), [ = ](const ISimulatorFactory * factory) { return factory->getSimulatorInfo() == simulatorInfo; }); - if (iterator == m_simulatorFactories.end()) - return false; - + if (iterator == m_simulatorFactories.end()) { return false; } factory = *iterator; Q_ASSERT(factory); - m_simulator = factory->create(this); - Q_ASSERT(m_simulator); + ISimulator *newSimulator = factory->create(this); + Q_ASSERT(newSimulator); + + this->unloadSimulatorPlugin(); // old plugin unloaded + m_simulator = newSimulator; connect(m_simulator, SIGNAL(statusChanged(ISimulator::Status)), this, SLOT(setConnectionStatus(ISimulator::Status))); connect(m_simulator, &ISimulator::aircraftModelChanged, this, &IContextSimulator::ownAircraftModelChanged); + asyncConnectTo(); // try to connect return true; } + bool CContextSimulator::loadSimulatorPluginFromSettings() + { + Q_ASSERT(this->getIContextSettings()); + if (!this->getIContextSettings()) return false; + + CSimulatorInfoList drivers = this->getAvailableSimulatorPlugins(); + if (drivers.size() == 1) + { + // load, independent from settings, we have only driver + return this->loadSimulatorPlugin(drivers.front()); + } + else if (drivers.size() > 1) + { + return this->loadSimulatorPlugin( + this->getIContextSettings()->getSimulatorSettings().getSelectedDriver() + ); + } + else + { + return false; + } + } + void CContextSimulator::unloadSimulatorPlugin() { - if (m_simulator) - m_simulator->deleteLater(); - + if (m_simulator) { m_simulator->deleteLater(); } m_simulator = nullptr; } @@ -199,6 +227,29 @@ namespace BlackCore } } + void CContextSimulator::settingsChanged(uint type) + { + Q_ASSERT(this->getIContextSettings()); + if (!this->getIContextSettings()) return; + IContextSettings::SettingsType settingsType = static_cast(type); + if (settingsType == IContextSettings::SettingsSimulator) + { + CSimulatorInfo driver = this->getIContextSettings()->getSimulatorSettings().getSelectedDriver(); + if (this->loadSimulatorPlugin(driver)) + { + QString m = QString("Driver loaded: '%1'").arg(driver.toQString(true)); + if (this->getIContextApplication()) + this->getIContextApplication()->sendStatusMessage(CStatusMessage::getInfoMessage(m, CStatusMessage::TypeSimulator)); + } + else + { + QString m = QString("Cannot load driver: '%1'").arg(driver.toQString(true)); + if (this->getIContextApplication()) + this->getIContextApplication()->sendStatusMessage(CStatusMessage::getErrorMessage(m, CStatusMessage::TypeSimulator)); + } + } + } + void CContextSimulator::findSimulatorPlugins() { m_pluginsDir = QDir(qApp->applicationDirPath().append("/plugins/simulator")); diff --git a/src/blackcore/context_simulator_impl.h b/src/blackcore/context_simulator_impl.h index f161c9cd0..7930eb147 100644 --- a/src/blackcore/context_simulator_impl.h +++ b/src/blackcore/context_simulator_impl.h @@ -62,9 +62,15 @@ namespace BlackCore //! \copydoc IContextSimulator::loadSimulatorPlugin() virtual bool loadSimulatorPlugin(const BlackSim::CSimulatorInfo &simulatorInfo) override; + //! \copydoc IContextSimulator::loadSimulatorPluginFromSettings() + virtual bool loadSimulatorPluginFromSettings(); + //! \copydoc IContextSimulator::unloadSimulatorPlugin() virtual void unloadSimulatorPlugin() override; + //! \copydoc IContextSimulator::settingsChanged + virtual void settingsChanged(uint type) override; + protected: //! \brief Constructor CContextSimulator(CRuntimeConfig::ContextMode, CRuntime *runtime); diff --git a/src/blackcore/context_simulator_proxy.cpp b/src/blackcore/context_simulator_proxy.cpp index e3f0cb463..902afaaf4 100644 --- a/src/blackcore/context_simulator_proxy.cpp +++ b/src/blackcore/context_simulator_proxy.cpp @@ -73,9 +73,19 @@ namespace BlackCore return m_dBusInterface->callDBusRet(QLatin1Literal("loadSimulatorPlugin"), simulatorInfo); } + bool CContextSimulatorProxy::loadSimulatorPluginFromSettings() + { + return m_dBusInterface->callDBusRet(QLatin1Literal("loadSimulatorPluginFromSettings")); + } + void CContextSimulatorProxy::unloadSimulatorPlugin() { m_dBusInterface->callDBus(QLatin1Literal("unloadSimulatorPlugin")); } + void CContextSimulatorProxy::settingsChanged(uint type) + { + m_dBusInterface->callDBus(QLatin1Literal("settingsChanged"), type); + } + } // namespace BlackCore diff --git a/src/blackcore/context_simulator_proxy.h b/src/blackcore/context_simulator_proxy.h index be632a2cc..ec986d78e 100644 --- a/src/blackcore/context_simulator_proxy.h +++ b/src/blackcore/context_simulator_proxy.h @@ -63,12 +63,14 @@ namespace BlackCore //! \copydoc IContextSimulator::loadSimulatorPlugin virtual bool loadSimulatorPlugin(const BlackSim::CSimulatorInfo &simulatorInfo) override; + //! \copydoc IContextSimulator::loadSimulatorPluginFromSettings() + virtual bool loadSimulatorPluginFromSettings(); + //! \copydoc IContextSimulator::unloadSimulatorPlugin() virtual void unloadSimulatorPlugin() override; - - - + //! \copydoc IContextSimulator::settingsChanged + virtual void settingsChanged(uint type) override; }; } // namespace BlackCore