diff --git a/src/core/application/applicationsettings.h b/src/core/application/applicationsettings.h index b2bfad00d..f655d53ec 100644 --- a/src/core/application/applicationsettings.h +++ b/src/core/application/applicationsettings.h @@ -42,38 +42,31 @@ namespace swift::core::application } }; - //! Selected simulator plugins - struct TEnabledSimulators : misc::TSettingTrait + //! Enabled simulator plugin identifier + struct TEnabledSimulator : misc::TSettingTrait { //! \copydoc swift::misc::TSettingTrait::key - static const char *key() { return "application/enabledsimulators"; } + static const char *key() { return "application/enabledsimulator"; } //! \copydoc swift::misc::TSettingTrait::humanReadable static const QString &humanReadable() { - static const QString name("Enabled simulators"); + static const QString name("Enabled simulator"); return name; } //! \copydoc swift::misc::TSettingTrait::defaultValue - static const QStringList &defaultValue() + static const QString &defaultValue() { - // All default simulators - static const QStringList enabledSimulators(misc::simulation::CSimulatorPluginInfo::guessDefaultPlugins()); - return enabledSimulators; + static QString defaultValue; + return defaultValue; } //! \copydoc swift::misc::TSettingTrait::isValid - static bool isValid(const QStringList &pluginIdentifiers, QString &) + static bool isValid(const QString &pluginIdentifier, QString &) { - for (const QString &pluginIdentifier : pluginIdentifiers) - { - if (!misc::simulation::CSimulatorPluginInfo::allIdentifiers().contains(pluginIdentifier)) - { - return false; - } - } - return true; + return pluginIdentifier.isEmpty() || + misc::simulation::CSimulatorPluginInfo::allIdentifiers().contains(pluginIdentifier); } }; diff --git a/src/core/context/contextsimulator.h b/src/core/context/contextsimulator.h index 7f41d3db6..cf19fb1f2 100644 --- a/src/core/context/contextsimulator.h +++ b/src/core/context/contextsimulator.h @@ -158,8 +158,9 @@ namespace swift::core::context //! Check all listeners enabled if simulator is connected virtual int checkListeners() = 0; - //! Load and start specific simulator plugin - virtual bool startSimulatorPlugin(const swift::misc::simulation::CSimulatorPluginInfo &simulatorInfo) = 0; + //! Set the plugin and start. This function has no effect if already called previously or using the stored + //! simulator from the settings + virtual bool setPlugin(const swift::misc::simulation::CSimulatorPluginInfo &simulatorInfo) = 0; //! Stop listener or unload the given plugin (if currently loaded) virtual void stopSimulatorPlugin(const swift::misc::simulation::CSimulatorPluginInfo &simulatorInfo) = 0; diff --git a/src/core/context/contextsimulatorempty.h b/src/core/context/contextsimulatorempty.h index 6fc79af78..621a690a3 100644 --- a/src/core/context/contextsimulatorempty.h +++ b/src/core/context/contextsimulatorempty.h @@ -55,8 +55,8 @@ namespace swift::core::context return false; } - //! \copydoc IContextSimulator::startSimulatorPlugin - virtual bool startSimulatorPlugin(const swift::misc::simulation::CSimulatorPluginInfo &simulatorInfo) override + //! \copydoc IContextSimulator::setPlugin + bool setPlugin(const swift::misc::simulation::CSimulatorPluginInfo &simulatorInfo) override { Q_UNUSED(simulatorInfo) logEmptyContextWarning(Q_FUNC_INFO); diff --git a/src/core/context/contextsimulatorimpl.cpp b/src/core/context/contextsimulatorimpl.cpp index b6e35edb1..3027c2a53 100644 --- a/src/core/context/contextsimulatorimpl.cpp +++ b/src/core/context/contextsimulatorimpl.cpp @@ -64,7 +64,17 @@ namespace swift::core::context CBuildConfig::isLocalDeveloperDebugBuild() ? MatchingLogAll : MatchingLogSimplified; m_logMatchingMessages = logMatchingMessages; m_plugins->collectPlugins(); - this->restoreSimulatorPlugins(); + + const QString enabledSimulator = m_enabledSimulator.get(); + if (!enabledSimulator.isEmpty()) + { + auto plugins = m_plugins->getAvailableSimulatorPlugins(); + auto it = std::find_if(plugins.begin(), plugins.end(), [&](const CSimulatorPluginInfo &plugin) { + return plugin.getIdentifier() == enabledSimulator; + }); + Q_ASSERT_X(it != plugins.end(), Q_FUNC_INFO, "Plugin not found"); + CContextSimulator::setPlugin(*it); + } connect(&m_aircraftMatcher, &CAircraftMatcher::setupChanged, this, &CContextSimulator::matchingSetupChanged); connect(&CCentralMultiSimulatorModelSetCachesProvider::modelCachesInstance(), @@ -122,7 +132,7 @@ namespace swift::core::context m_validator->deleteLater(); m_validator = nullptr; } - this->stopSimulatorListeners(); + this->stopSimulatorListener(); this->disconnect(); this->unloadSimulatorPlugin(); @@ -158,13 +168,25 @@ namespace swift::core::context return true; } - bool CContextSimulator::startSimulatorPlugin(const CSimulatorPluginInfo &simulatorInfo) + bool CContextSimulator::setPlugin(const CSimulatorPluginInfo &info) + { + if (!m_selectedSimulatorPlugin.isUnspecified()) { return false;} + m_selectedSimulatorPlugin = info; + startSimulatorListener(); + return true; + } + + + bool CContextSimulator::startSimulatorListener() { Q_ASSERT(this->getIContextApplication()); Q_ASSERT(this->getIContextApplication()->isUsingImplementingObject()); - Q_ASSERT(!simulatorInfo.isUnspecified()); + Q_ASSERT(!m_selectedSimulatorPlugin.isUnspecified()); Q_ASSERT(m_simulatorPlugin.first.isUnspecified()); + this->stopSimulatorListener(); + const CSimulatorPluginInfo simulatorInfo = m_selectedSimulatorPlugin; + if (!m_listenersThread.isRunning()) { m_listenersThread.setObjectName("CContextSimulator: Thread for listener " + simulatorInfo.getIdentifier()); @@ -738,7 +760,7 @@ namespace swift::core::context // we got disconnected, plugin no longer needed this->updateMarkAllAsNotRendered(); // without plugin nothing can be rendered this->unloadSimulatorPlugin(); - this->restoreSimulatorPlugins(); + this->startSimulatorListener(); if (m_wasSimulating) { emit this->vitalityLost(); } m_wasSimulating = false; @@ -881,33 +903,6 @@ namespace swift::core::context } } - void CContextSimulator::changeEnabledSimulators() - { - CSimulatorPluginInfo currentPluginInfo = m_simulatorPlugin.first; - const QStringList enabledSimulators = m_enabledSimulators.getThreadLocal(); - - // Unload the current plugin, if it is no longer enabled - if (!currentPluginInfo.isUnspecified() && !enabledSimulators.contains(currentPluginInfo.getIdentifier())) - { - unloadSimulatorPlugin(); - emit simulatorStatusChanged(ISimulator::Disconnected); - } - restoreSimulatorPlugins(); - } - - void CContextSimulator::restoreSimulatorPlugins() - { - if (!m_simulatorPlugin.first.isUnspecified()) { return; } - - this->stopSimulatorListeners(); - const QStringList enabledSimulators = m_enabledSimulators.getThreadLocal(); - const CSimulatorPluginInfoList allSimulators = m_plugins->getAvailableSimulatorPlugins(); - for (const CSimulatorPluginInfo &s : allSimulators) - { - if (enabledSimulators.contains(s.getIdentifier())) { startSimulatorPlugin(s); } - } - } - CStatusMessageList CContextSimulator::getMatchingMessages(const CCallsign &callsign) const { if (isDebugEnabled()) { CLogMessage(this, CLogCategories::contextSlot()).debug() << Q_FUNC_INFO << callsign; } @@ -1112,7 +1107,7 @@ namespace swift::core::context void CContextSimulator::onSimulatorStarted(const CSimulatorPluginInfo &info) { - this->stopSimulatorListeners(); + this->stopSimulatorListener(); this->loadSimulatorPlugin(info); // if we have enabled messages, we will disable if size getting too high @@ -1137,17 +1132,14 @@ namespace swift::core::context emit this->ownAircraftModelChanged(lookupModel); } - void CContextSimulator::stopSimulatorListeners() + void CContextSimulator::stopSimulatorListener() { - for (const CSimulatorPluginInfo &info : getAvailableSimulatorPlugins()) + ISimulatorListener *listener = m_plugins->getListener(m_selectedSimulatorPlugin.getIdentifier()); + if (listener) { - ISimulatorListener *listener = m_plugins->getListener(info.getIdentifier()); - if (listener) - { - const bool s = QMetaObject::invokeMethod(listener, &ISimulatorListener::stop); - Q_ASSERT_X(s, Q_FUNC_INFO, "Cannot invoke stop"); - Q_UNUSED(s) - } + const bool s = QMetaObject::invokeMethod(listener, &ISimulatorListener::stop); + Q_ASSERT_X(s, Q_FUNC_INFO, "Cannot invoke stop"); + Q_UNUSED(s) } } diff --git a/src/core/context/contextsimulatorimpl.h b/src/core/context/contextsimulatorimpl.h index 917051a83..4db2d549d 100644 --- a/src/core/context/contextsimulatorimpl.h +++ b/src/core/context/contextsimulatorimpl.h @@ -86,9 +86,11 @@ namespace swift::core virtual bool setSimulatorSettings(const swift::misc::simulation::settings::CSimulatorSettings &settings, const swift::misc::simulation::CSimulatorInfo &simulator) override; - //! \copydoc swift::core::context::IContextSimulator::startSimulatorPlugin - virtual bool - startSimulatorPlugin(const swift::misc::simulation::CSimulatorPluginInfo &simulatorInfo) override; + //! \copydoc swift::core::context::IContextSimulator::startSimulatorListener + bool startSimulatorListener(); + + //! \copydoc swift::core::context::IContextSimulator::setPlugin + bool setPlugin(const swift::misc::simulation::CSimulatorPluginInfo &simulatorInfo) override; //! \copydoc swift::core::context::IContextSimulator::stopSimulatorPlugin virtual void @@ -348,12 +350,6 @@ namespace swift::core //! Relay status message to simulator under consideration of settings void relayStatusMessageToSimulator(const swift::misc::CStatusMessage &message); - //! Handle a change in enabled simulators - void changeEnabledSimulators(); - - //! Reads list of enabled simulators, starts listeners - void restoreSimulatorPlugins(); - //! Load plugin and connect bool loadSimulatorPlugin(const swift::misc::simulation::CSimulatorPluginInfo &simulatorPluginInfo); @@ -361,7 +357,7 @@ namespace swift::core void unloadSimulatorPlugin(); //! Call stop() on all loaded listeners - void stopSimulatorListeners(); + void stopSimulatorListener(); //! Add to message list for matching void addMatchingMessages(const swift::misc::aviation::CCallsign &callsign, @@ -396,11 +392,10 @@ namespace swift::core QString m_networkSessionId; //!< Network session of CServer::getServerSessionId, if not connected empty (for //!< statistics, ..) swift::misc::simulation::CBackgroundValidation *m_validator = nullptr; + misc::simulation::CSimulatorPluginInfo m_selectedSimulatorPlugin; // settings - swift::misc::CSettingReadOnly m_enabledSimulators { - this, &CContextSimulator::changeEnabledSimulators - }; + swift::misc::CSettingReadOnly m_enabledSimulator { this }; swift::misc::CSetting m_matchingSettings { this }; //!< matching settings (all simulators) diff --git a/src/core/context/contextsimulatorproxy.cpp b/src/core/context/contextsimulatorproxy.cpp index 8c69aa3b3..2bf77f755 100644 --- a/src/core/context/contextsimulatorproxy.cpp +++ b/src/core/context/contextsimulatorproxy.cpp @@ -264,9 +264,9 @@ namespace swift::core::context m_dBusInterface->callDBus(QLatin1String("setInterpolationAndRenderingSetupGlobal"), setup); } - bool CContextSimulatorProxy::startSimulatorPlugin(const CSimulatorPluginInfo &simulatorInfo) + bool CContextSimulatorProxy::setPlugin(const CSimulatorPluginInfo &info) { - return m_dBusInterface->callDBusRet(QLatin1String("startSimulatorPlugin"), simulatorInfo); + return m_dBusInterface->callDBusRet(QLatin1String("setPlugin"), info); } void CContextSimulatorProxy::stopSimulatorPlugin(const CSimulatorPluginInfo &simulatorInfo) diff --git a/src/core/context/contextsimulatorproxy.h b/src/core/context/contextsimulatorproxy.h index d3b7f9ccc..b324967b2 100644 --- a/src/core/context/contextsimulatorproxy.h +++ b/src/core/context/contextsimulatorproxy.h @@ -69,9 +69,8 @@ namespace swift::core virtual bool setSimulatorSettings(const swift::misc::simulation::settings::CSimulatorSettings &settings, const swift::misc::simulation::CSimulatorInfo &simulatorInfo) override; - //! \copydoc swift::core::context::IContextSimulator::startSimulatorPlugin - virtual bool - startSimulatorPlugin(const swift::misc::simulation::CSimulatorPluginInfo &simulatorInfo) override; + //! \copydoc swift::core::context::IContextSimulator::setPlugin + virtual bool setPlugin(const swift::misc::simulation::CSimulatorPluginInfo &simulatorInfo) override; //! \copydoc swift::core::context::IContextSimulator::stopSimulatorPlugin virtual void diff --git a/src/gui/components/configsimulatorcomponent.h b/src/gui/components/configsimulatorcomponent.h index dfd4a9043..ffc9e6ff9 100644 --- a/src/gui/components/configsimulatorcomponent.h +++ b/src/gui/components/configsimulatorcomponent.h @@ -49,7 +49,7 @@ namespace swift::gui::components //! Get the plugin ids QStringList selectedSimsToPluginIds(); - swift::misc::CSetting m_enabledSimulators { this }; + swift::misc::CSetting m_enabledSimulators { this }; swift::misc::simulation::data::CModelSetCaches m_modelSets { true, this }; QScopedPointer ui; };