From 4e6c4a076a6a7667b8532bb35e7a67dc62ccaaf1 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Sat, 13 Aug 2016 13:38:11 +0200 Subject: [PATCH] Delay initial weather injection until simulator is connected Up to know, the initial weather injection was done in the constructor of ISimulator implementation. This was to early, since in the best case the injection was not doing anything and in the worst case, crashed the application because the plugin wasn't ready yet. This change also replaces ps_reloadSettings with a specific non-slot version to avoid code duplication. --- src/plugins/simulator/fs9/simulatorfs9.cpp | 25 +++++++++++-------- src/plugins/simulator/fs9/simulatorfs9.h | 8 +++--- src/plugins/simulator/fsx/simulatorfsx.cpp | 25 +++++++++++-------- src/plugins/simulator/fsx/simulatorfsx.h | 8 +++--- .../simulator/xplane/simulatorxplane.cpp | 25 +++++++++++-------- .../simulator/xplane/simulatorxplane.h | 5 ++-- 6 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/plugins/simulator/fs9/simulatorfs9.cpp b/src/plugins/simulator/fs9/simulatorfs9.cpp index 55b472f8a..d3c086b0a 100644 --- a/src/plugins/simulator/fs9/simulatorfs9.cpp +++ b/src/plugins/simulator/fs9/simulatorfs9.cpp @@ -113,7 +113,6 @@ namespace BlackSimPlugin "B737-400 default model", CAircraftIcaoCode("B734", "L2J") )); - ps_reloadSettings(); } bool CSimulatorFs9::isConnected() const @@ -133,6 +132,7 @@ namespace BlackSimPlugin { m_fsuipc->connect(); // connect FSUIPC too } + reloadWeatherSettings(); m_dispatchTimerId = startTimer(50); return true; } @@ -355,16 +355,6 @@ namespace BlackSimPlugin } } - void CSimulatorFs9::ps_reloadSettings() - { - auto selectedWeatherScenario = m_weatherScenarioSettings.get(); - if (!CWeatherScenario::isRealWeatherScenario(selectedWeatherScenario)) - { - m_lastWeatherPosition = {}; - injectWeatherGrid(CWeatherGrid::getByScenario(selectedWeatherScenario)); - } - } - void CSimulatorFs9::updateOwnAircraftFromSimulator(const CSimulatedAircraft &simDataOwnAircraft) { this->updateCockpit( @@ -388,6 +378,19 @@ namespace BlackSimPlugin m_fsuipc->write(weatherGrid); } + void CSimulatorFs9::reloadWeatherSettings() + { + if (m_fsuipc->isConnected()) + { + auto selectedWeatherScenario = m_weatherScenarioSettings.get(); + if (!CWeatherScenario::isRealWeatherScenario(selectedWeatherScenario)) + { + m_lastWeatherPosition = {}; + injectWeatherGrid(CWeatherGrid::getByScenario(selectedWeatherScenario)); + } + } + } + CSimulatorFs9Listener::CSimulatorFs9Listener(const CSimulatorPluginInfo &info, const QSharedPointer &fs9Host, const QSharedPointer &lobbyClient) : diff --git a/src/plugins/simulator/fs9/simulatorfs9.h b/src/plugins/simulator/fs9/simulatorfs9.h index 669e05451..3fe376e36 100644 --- a/src/plugins/simulator/fs9/simulatorfs9.h +++ b/src/plugins/simulator/fs9/simulatorfs9.h @@ -99,9 +99,6 @@ namespace BlackSimPlugin //! Process incoming FS9 message void ps_processFs9Message(const QByteArray &message); - //! Reload settings - void ps_reloadSettings(); - private: //! Called when data about our own aircraft are received @@ -112,6 +109,9 @@ namespace BlackSimPlugin //! Inject weather grid to simulator void injectWeatherGrid(const BlackMisc::Weather::CWeatherGrid &weatherGrid); + //! Reload Weather settings + void reloadWeatherSettings(); + QHash> m_hashFs9Clients; QMetaObject::Connection m_connectionHostMessages; int m_dispatchTimerId = -1; @@ -120,7 +120,7 @@ namespace BlackSimPlugin QSharedPointer m_lobbyClient; BlackMisc::Geo::CCoordinateGeodetic m_lastWeatherPosition; //!< Own aircraft position at which weather was fetched and injected last - BlackMisc::CSetting m_weatherScenarioSettings { this, &CSimulatorFs9::ps_reloadSettings }; + BlackMisc::CSetting m_weatherScenarioSettings { this, &CSimulatorFs9::reloadWeatherSettings }; }; //! Listener for FS9 diff --git a/src/plugins/simulator/fsx/simulatorfsx.cpp b/src/plugins/simulator/fsx/simulatorfsx.cpp index 9dc4cf5de..e0ddd264d 100644 --- a/src/plugins/simulator/fsx/simulatorfsx.cpp +++ b/src/plugins/simulator/fsx/simulatorfsx.cpp @@ -59,7 +59,6 @@ namespace BlackSimPlugin "B737-800 default model", CAircraftIcaoCode("B738", "L2J") )); - ps_reloadSettings(); } CSimulatorFsx::~CSimulatorFsx() @@ -95,6 +94,7 @@ namespace BlackSimPlugin initEvents(); initDataDefinitionsWhenConnected(); m_simconnectTimerId = startTimer(10); + reloadWeatherSettings(); return true; } @@ -518,16 +518,6 @@ namespace BlackSimPlugin } } - void CSimulatorFsx::ps_reloadSettings() - { - auto selectedWeatherScenario = m_weatherScenarioSettings.get(); - if (!CWeatherScenario::isRealWeatherScenario(selectedWeatherScenario)) - { - m_lastWeatherPosition = {}; - injectWeatherGrid(CWeatherGrid::getByScenario(selectedWeatherScenario)); - } - } - bool CSimulatorFsx::physicallyRemoveRemoteAircraft(const CCallsign &callsign) { // only remove from sim @@ -854,6 +844,19 @@ namespace BlackSimPlugin m_fsuipc->write(weatherGrid); } + void CSimulatorFsx::reloadWeatherSettings() + { + if (m_fsuipc->isConnected()) + { + auto selectedWeatherScenario = m_weatherScenarioSettings.get(); + if (!CWeatherScenario::isRealWeatherScenario(selectedWeatherScenario)) + { + m_lastWeatherPosition = {}; + injectWeatherGrid(CWeatherGrid::getByScenario(selectedWeatherScenario)); + } + } + } + CSimulatorFsxListener::CSimulatorFsxListener(const CSimulatorPluginInfo &info) : ISimulatorListener(info), m_timer(new QTimer(this)) diff --git a/src/plugins/simulator/fsx/simulatorfsx.h b/src/plugins/simulator/fsx/simulatorfsx.h index 4b8f761c7..e91eb1ee3 100644 --- a/src/plugins/simulator/fsx/simulatorfsx.h +++ b/src/plugins/simulator/fsx/simulatorfsx.h @@ -142,9 +142,6 @@ namespace BlackSimPlugin //! Dispatch SimConnect messages void ps_dispatch(); - //! Reload settings - void ps_reloadSettings(); - private: //! Call this method to declare the simulator connected void setSimConnected(); @@ -189,6 +186,9 @@ namespace BlackSimPlugin //! Inject weather grid to simulator void injectWeatherGrid(const BlackMisc::Weather::CWeatherGrid &weatherGrid); + //! Reload weather settings + void reloadWeatherSettings(); + static constexpr int SkipUpdateCyclesForCockpit = 10; //!< skip x cycles before updating cockpit again bool m_simConnected = false; //!< Is simulator connected? bool m_simSimulating = false; //!< Simulator running? @@ -209,7 +209,7 @@ namespace BlackSimPlugin int m_statsUpdateAircraftCount = 0; BlackMisc::Geo::CCoordinateGeodetic m_lastWeatherPosition; //!< Own aircraft position at which weather was fetched and injected last - BlackMisc::CSetting m_weatherScenarioSettings { this, &CSimulatorFsx::ps_reloadSettings }; + BlackMisc::CSetting m_weatherScenarioSettings { this, &CSimulatorFsx::reloadWeatherSettings }; }; //! Listener for FSX diff --git a/src/plugins/simulator/xplane/simulatorxplane.cpp b/src/plugins/simulator/xplane/simulatorxplane.cpp index 57b56eb77..57e26acc8 100644 --- a/src/plugins/simulator/xplane/simulatorxplane.cpp +++ b/src/plugins/simulator/xplane/simulatorxplane.cpp @@ -110,7 +110,6 @@ namespace BlackSimPlugin )); resetData(); - ps_reloadSettings(); } // convert xplane squawk mode to swift squawk mode @@ -231,16 +230,6 @@ namespace BlackSimPlugin } } - void CSimulatorXPlane::ps_reloadSettings() - { - auto selectedWeatherScenario = m_weatherScenarioSettings.get(); - if (!CWeatherScenario::isRealWeatherScenario(selectedWeatherScenario)) - { - m_lastWeatherPosition = {}; - injectWeatherGrid(CWeatherGrid::getByScenario(selectedWeatherScenario)); - } - } - bool CSimulatorXPlane::isConnected() const { return m_service && m_traffic && m_weather; @@ -263,6 +252,7 @@ namespace BlackSimPlugin m_service->updateAirportsInRange(); m_traffic->updateInstalledModels(); m_watcher->setConnection(m_conn); + reloadWeatherSettings(); emitSimulatorCombinedStatus(); return true; } @@ -646,6 +636,19 @@ namespace BlackSimPlugin m_weather->setThunderstormRatio(0.0); } + void CSimulatorXPlane::reloadWeatherSettings() + { + if (m_weather) + { + auto selectedWeatherScenario = m_weatherScenarioSettings.get(); + if (!CWeatherScenario::isRealWeatherScenario(selectedWeatherScenario)) + { + m_lastWeatherPosition = {}; + injectWeatherGrid(CWeatherGrid::getByScenario(selectedWeatherScenario)); + } + } + } + BlackCore::ISimulator *CSimulatorXPlaneFactory::create(const CSimulatorPluginInfo &info, IOwnAircraftProvider *ownAircraftProvider, IRemoteAircraftProvider *remoteAircraftProvider, diff --git a/src/plugins/simulator/xplane/simulatorxplane.h b/src/plugins/simulator/xplane/simulatorxplane.h index f7b595a45..00b0d7514 100644 --- a/src/plugins/simulator/xplane/simulatorxplane.h +++ b/src/plugins/simulator/xplane/simulatorxplane.h @@ -134,12 +134,11 @@ namespace BlackSimPlugin void ps_fastTimerTimeout(); void ps_slowTimerTimeout(); void ps_installedModelsUpdated(const QStringList &modelStrings, const QStringList &icaos, const QStringList &airlines, const QStringList &liveries); - void ps_reloadSettings(); - private: //! Inject weather grid to simulator void injectWeatherGrid(const BlackMisc::Weather::CWeatherGrid &weatherGrid); + void reloadWeatherSettings(); QDBusConnection m_conn { "default" }; QDBusServiceWatcher *m_watcher { nullptr }; @@ -152,7 +151,7 @@ namespace BlackSimPlugin BlackMisc::Simulation::CAircraftModelList m_installedModels; //!< \todo Do we still need this, as we now focus on model set BlackMisc::Geo::CCoordinateGeodetic m_lastWeatherPosition; //!< Own aircraft position at which weather was fetched and injected last - BlackMisc::CSetting m_weatherScenarioSettings { this, &CSimulatorXPlane::ps_reloadSettings }; + BlackMisc::CSetting m_weatherScenarioSettings { this, &CSimulatorXPlane::reloadWeatherSettings }; //! \todo Add units to members? pitchDeg?, altitudeFt? struct // data is written by DBus async method callbacks