From ae60cf560d2690be2335ed84f4bdc2aeb73eec1b Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Tue, 18 Sep 2018 19:23:13 +0200 Subject: [PATCH] Ref T361, better guards for plugin and sGui No need to create an assert if plugin is not avilable, though the plugin info should be "Unspecified" in that case --- src/blackcore/context/contextsimulator.cpp | 5 ++ src/blackcore/context/contextsimulator.h | 2 +- .../context/contextsimulatorimpl.cpp | 70 ++++++------------- .../components/settingssimulatorcomponent.cpp | 1 + 4 files changed, 28 insertions(+), 50 deletions(-) diff --git a/src/blackcore/context/contextsimulator.cpp b/src/blackcore/context/contextsimulator.cpp index ac0eea01b..30eee8b57 100644 --- a/src/blackcore/context/contextsimulator.cpp +++ b/src/blackcore/context/contextsimulator.cpp @@ -61,6 +61,11 @@ namespace BlackCore return static_cast(this->getSimulatorStatus()); } + bool IContextSimulator::isSimulatorAvailable() const + { + return BlackConfig::CBuildConfig::isCompiledWithFlightSimulatorSupport() && !getSimulatorPluginInfo().isUnspecified(); + } + bool IContextSimulator::isSimulatorSimulating() const { if (!isSimulatorAvailable() || !getSimulatorStatusEnum().testFlag(ISimulator::Simulating)) { return false; } diff --git a/src/blackcore/context/contextsimulator.h b/src/blackcore/context/contextsimulator.h index aff9781be..0b8fc5d36 100644 --- a/src/blackcore/context/contextsimulator.h +++ b/src/blackcore/context/contextsimulator.h @@ -216,7 +216,7 @@ namespace BlackCore virtual BlackMisc::PhysicalQuantities::CTime getTimeSynchronizationOffset() const = 0; //! Simulator avialable (driver available)? - bool isSimulatorAvailable() const { return BlackConfig::CBuildConfig::isCompiledWithFlightSimulatorSupport() && !getSimulatorPluginInfo().isUnspecified(); } + bool isSimulatorAvailable() const; //! Is available simulator simulating? Returns false if no simulator is available bool isSimulatorSimulating() const; diff --git a/src/blackcore/context/contextsimulatorimpl.cpp b/src/blackcore/context/contextsimulatorimpl.cpp index 2872a376a..d2f508e78 100644 --- a/src/blackcore/context/contextsimulatorimpl.cpp +++ b/src/blackcore/context/contextsimulatorimpl.cpp @@ -140,18 +140,15 @@ namespace BlackCore int CContextSimulator::getSimulatorStatus() const { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return 0; } - - Q_ASSERT_X(m_simulatorPlugin.second, Q_FUNC_INFO, "Missing simulator"); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return 0; } return m_simulatorPlugin.second->getSimulatorStatus(); } CSimulatorPluginInfo CContextSimulator::getSimulatorPluginInfo() const { + static const CSimulatorPluginInfo unspecified; if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return CSimulatorPluginInfo(); } - - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return unspecified; } if (m_simulatorPlugin.first.getSimulator().contains("emulated", Qt::CaseInsensitive)) { return m_simulatorPlugin.second->getSimulatorPluginInfo(); } return m_simulatorPlugin.first; } @@ -159,12 +156,7 @@ namespace BlackCore CSimulatorInternals CContextSimulator::getSimulatorInternals() const { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) - { - return CSimulatorInternals(); - } - - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return CSimulatorInternals(); } return m_simulatorPlugin.second->getSimulatorInternals(); } @@ -172,12 +164,7 @@ namespace BlackCore { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } // If no ISimulator object is available, return a dummy. - if (m_simulatorPlugin.first.isUnspecified()) - { - return CAirportList(); - } - - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return CAirportList(); } return m_simulatorPlugin.second->getAirportsInRange(recalculateDistance); } @@ -265,30 +252,23 @@ namespace BlackCore int CContextSimulator::getModelSetCount() const { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return 0; } - - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return 0; } return this->getModelSet().size(); } CAircraftModelList CContextSimulator::getModelSetModelsStartingWith(const QString &modelString) const { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO << modelString; } - if (m_simulatorPlugin.first.isUnspecified()) - { - return CAircraftModelList(); - } + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return CAircraftModelList(); } - Q_ASSERT(m_simulatorPlugin.second); return this->getModelSet().findModelsStartingWith(modelString); } bool CContextSimulator::setTimeSynchronization(bool enable, const CTime &offset) { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return false; } + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return false; } - Q_ASSERT(m_simulatorPlugin.second); const bool c = m_simulatorPlugin.second->setTimeSynchronization(enable, offset); if (!c) { return false; } @@ -299,41 +279,35 @@ namespace BlackCore bool CContextSimulator::isTimeSynchronized() const { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return false; } - - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return false; } return m_simulatorPlugin.second->isTimeSynchronized(); } CInterpolationAndRenderingSetupGlobal CContextSimulator::getInterpolationAndRenderingSetupGlobal() const { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return m_renderSettings.get(); } - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return m_renderSettings.get(); } return m_simulatorPlugin.second->getInterpolationSetupGlobal(); } CInterpolationSetupList CContextSimulator::getInterpolationAndRenderingSetupsPerCallsign() const { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return CInterpolationSetupList(); } - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return CInterpolationSetupList(); } return m_simulatorPlugin.second->getInterpolationSetupsPerCallsign(); } CInterpolationAndRenderingSetupPerCallsign CContextSimulator::getInterpolationAndRenderingSetupPerCallsignOrDefault(const CCallsign &callsign) const { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return CInterpolationAndRenderingSetupPerCallsign(); } - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return CInterpolationAndRenderingSetupPerCallsign(); } return m_simulatorPlugin.second->getInterpolationSetupPerCallsignOrDefault(callsign); } bool CContextSimulator::setInterpolationAndRenderingSetupsPerCallsign(const CInterpolationSetupList &setups, bool ignoreSameAsGlobal) { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return false; } - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return false; } return m_simulatorPlugin.second->setInterpolationSetupsPerCallsign(setups, ignoreSameAsGlobal); } @@ -346,16 +320,14 @@ namespace BlackCore CLogMessage::preformatted(m); // transfer to sim - if (m_simulatorPlugin.first.isUnspecified()) { return; } - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return; } m_simulatorPlugin.second->setInterpolationSetupGlobal(setup); } CTime CContextSimulator::getTimeSynchronizationOffset() const { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return CTime(0, CTimeUnit::hrmin()); } - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return CTime(0, CTimeUnit::hrmin()); } return m_simulatorPlugin.second->getTimeSynchronizationOffset(); } @@ -631,6 +603,7 @@ namespace BlackCore { if (!this->isSimulatorAvailable()) { return; } if (!this->getIContextOwnAircraft()) { return; } + const CSimulatorMessagesSettings settings = m_messageSettings.getThreadLocal(); const CSimulatedAircraft ownAircraft = this->getIContextOwnAircraft()->getOwnAircraft(); for (const auto &tm : textMessages) @@ -721,7 +694,7 @@ namespace BlackCore { if (!this->isSimulatorAvailable()) { return; } const CSimulatorMessagesSettings simMsg = m_messageSettings.getThreadLocal(); - if (simMsg.relayThisStatusMessage(message)) + if (simMsg.relayThisStatusMessage(message) && m_simulatorPlugin.second) { m_simulatorPlugin.second->displayStatusMessage(message); } @@ -759,8 +732,7 @@ namespace BlackCore CPixmap CContextSimulator::iconForModel(const QString &modelString) const { - if (m_simulatorPlugin.first.isUnspecified()) { return CPixmap(); } - Q_ASSERT_X(m_simulatorPlugin.second, Q_FUNC_INFO, "Missing simulator"); + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return CPixmap(); } // load from file CStatusMessage msg; @@ -890,14 +862,14 @@ namespace BlackCore void CContextSimulator::highlightAircraft(const CSimulatedAircraft &aircraftToHighlight, bool enableHighlight, const CTime &displayTime) { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO << aircraftToHighlight << enableHighlight << displayTime; } - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second) { return; } m_simulatorPlugin.second->highlightAircraft(aircraftToHighlight, enableHighlight, displayTime); } bool CContextSimulator::followAircraft(const CCallsign &callsign) { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO << callsign; } - Q_ASSERT(m_simulatorPlugin.second); + if (!m_simulatorPlugin.second) { return false; } return m_simulatorPlugin.second->followAircraft(callsign); } @@ -916,7 +888,7 @@ namespace BlackCore m_isWeatherActivated = activated; if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } - if (m_simulatorPlugin.first.isUnspecified()) { return; } + if (!m_simulatorPlugin.second || m_simulatorPlugin.first.isUnspecified()) { return; } m_simulatorPlugin.second->setWeatherActivated(activated); } diff --git a/src/blackgui/components/settingssimulatorcomponent.cpp b/src/blackgui/components/settingssimulatorcomponent.cpp index d6b90b1ad..18df3209a 100644 --- a/src/blackgui/components/settingssimulatorcomponent.cpp +++ b/src/blackgui/components/settingssimulatorcomponent.cpp @@ -103,6 +103,7 @@ namespace BlackGui void CSettingsSimulatorComponent::setGuiValues() { + if (!sGui || !sGui->getIContextSimulator() || sGui->isShuttingDown()) { return; } const CInterpolationAndRenderingSetupGlobal setup = sGui->getIContextSimulator()->getInterpolationAndRenderingSetupGlobal(); // time sync