diff --git a/client.pro b/client.pro index abc530549..d18cdf00c 100644 --- a/client.pro +++ b/client.pro @@ -21,6 +21,10 @@ contains(BLACK_CONFIG, BlackGui) { contains(BLACK_CONFIG, BlackSim) { SUBDIRS += src/blacksim + + contains(BLACK_CONFIG, FSX) { + SUBDIRS += src/blacksim/fsx/plugin_fsx.pro + } } contains(BLACK_CONFIG, Samples) { diff --git a/libraries.pri b/libraries.pri index 47dc4d968..1f2489d3b 100644 --- a/libraries.pri +++ b/libraries.pri @@ -1,6 +1,6 @@ include (externals.pri) -LIBS *= -L../../lib +LIBS *= -L../../lib -L../../../lib blackgui { LIBS += -lblackgui diff --git a/src/blackcore/blackcore.pro b/src/blackcore/blackcore.pro index 223256cf8..c0efb418f 100644 --- a/src/blackcore/blackcore.pro +++ b/src/blackcore/blackcore.pro @@ -28,12 +28,6 @@ SOURCES += *.cpp win32 { HEADERS += $$PWD/win/*.h SOURCES += $$PWD/win/*.cpp - - contains(BLACK_CONFIG, FSX) { - DEFINES += BLACK_WITH_FSX - HEADERS += $$PWD/fsx/*.h - SOURCES += $$PWD/fsx/*.cpp - } } win32:!win32-g++*: PRE_TARGETDEPS += ../../lib/blackmisc.lib ../../lib/blacksound.lib diff --git a/src/blackcore/context_simulator_impl.cpp b/src/blackcore/context_simulator_impl.cpp index f8554b855..ab02a091e 100644 --- a/src/blackcore/context_simulator_impl.cpp +++ b/src/blackcore/context_simulator_impl.cpp @@ -5,10 +5,7 @@ #include "context_simulator_impl.h" #include "coreruntime.h" - -#ifdef BLACK_WITH_FSX - #include "fsx/simulator_fsx.h" -#endif +#include using namespace BlackMisc; using namespace BlackMisc::PhysicalQuantities; @@ -26,10 +23,6 @@ namespace BlackCore { m_updateTimer = new QTimer(this); -#ifdef BLACK_WITH_FSX - m_simulator = new BlackCore::FSX::CSimulatorFSX(this); - connect(m_simulator, &ISimulator::connectionChanged, this, &CContextSimulator::setConnectionStatus); -#endif connect(m_updateTimer, &QTimer::timeout, this, &CContextSimulator::updateOwnAircraft); } @@ -53,13 +46,16 @@ namespace BlackCore void CContextSimulator::init() { + loadPlugins(); + if (!m_contextNetwork) { m_contextNetwork = getRuntime()->getIContextNetwork(); } if (m_simulator) - connect(m_contextNetwork, &IContextNetwork::aircraftSituationUpdate, m_simulator, &ISimulator::addAircraftSituation); + connect(m_contextNetwork, SIGNAL(aircraftSituationUpdate(BlackMisc::Aviation::CCallsign,BlackMisc::Aviation::CAircraftSituation)), + m_simulator, SLOT(addAircraftSituation(BlackMisc::Aviation::CCallsign,BlackMisc::Aviation::CAircraftSituation))); } void CContextSimulator::updateOwnAircraft() @@ -84,4 +80,30 @@ namespace BlackCore emit connectionChanged(value); } + void CContextSimulator::loadPlugins() + { + m_pluginsDir = QDir(qApp->applicationDirPath()); + m_pluginsDir.cd("plugins"); + + foreach (QString fileName, m_pluginsDir.entryList(QDir::Files)) + { + QPluginLoader loader(m_pluginsDir.absoluteFilePath(fileName)); + QObject *plugin = loader.instance(); + if (plugin) + { + ISimulatorFactory *factory = qobject_cast(plugin); + if(plugin) + { + m_simulator = factory->create(this); + connect(m_simulator, SIGNAL(connectionChanged(bool)), this, SLOT(setConnectionStatus(bool))); + } + + } + else + { + qDebug() << loader.errorString(); + } + } + } + } // namespace BlackCore diff --git a/src/blackcore/context_simulator_impl.h b/src/blackcore/context_simulator_impl.h index a8de7ada0..38778e452 100644 --- a/src/blackcore/context_simulator_impl.h +++ b/src/blackcore/context_simulator_impl.h @@ -11,6 +11,7 @@ #include "blackcore/simulator.h" #include +#include namespace BlackCore { @@ -83,11 +84,21 @@ namespace BlackCore void setConnectionStatus(bool value); private: + + /*! + * \brief Load any kind of plugins + * \todo Currently it goes through the plugins folder and creates an instance for any plugin it may find + * In case an FSX and an X-Plane are in that folder, m_simulator will always point to X-Plane in the end. + */ + void loadPlugins(); + BlackMisc::Aviation::CAircraft m_ownAircraft; BlackCore::ISimulator *m_simulator; QTimer *m_updateTimer; BlackCore::IContextNetwork *m_contextNetwork; + + QDir m_pluginsDir; }; } // namespace BlackCore diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index 035e07115..3168503c3 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -40,6 +40,7 @@ namespace BlackCore //! \brief Are we connected to the simulator? virtual bool isConnected() const = 0; + public slots: /*! * \brief Return user aircraft object * \return @@ -78,6 +79,24 @@ namespace BlackCore void ownAircraftReceived(BlackMisc::Aviation::CAircraft aircraft); }; + //! \brief Factory pattern class to create instances of ISimulator + class ISimulatorFactory + { + public: + + //! \brief Virtual destructor + virtual ~ISimulatorFactory() {} + + /*! + * \brief Create a new instance + * \param parent + * \return + */ + virtual ISimulator* create(QObject *parent = nullptr) = 0; + }; + } // namespace BlackCore +Q_DECLARE_INTERFACE(BlackCore::ISimulatorFactory, "net.vatsim.PilotClient.BlackCore.SimulatorInterface") + #endif // BLACKCORE_SIMULATOR_H diff --git a/src/blacksim/fsx/plugin_fsx.pro b/src/blacksim/fsx/plugin_fsx.pro new file mode 100644 index 000000000..ddb9655af --- /dev/null +++ b/src/blacksim/fsx/plugin_fsx.pro @@ -0,0 +1,26 @@ +include (../../../config.pri) +include (../../../build.pri) + +QT += core dbus gui network + +TARGET = simulator_fsx +TEMPLATE = lib + +CONFIG += plugin shared +CONFIG += blackmisc blackcore + +LIBS += -lSimConnect + + +DEPENDPATH += . ../../../src +INCLUDEPATH += . ../../../src + +SOURCES += *.cpp +HEADERS += *.h + +win32:!win32-g++*: PRE_TARGETDEPS += ../../../lib/blackmisc.lib +win32:!win32-g++*: PRE_TARGETDEPS += ../../../lib/blackcore.lib + +DESTDIR = ../../../bin/plugins + +include (../../../libraries.pri) diff --git a/src/blackcore/fsx/simconnect_datadefinition.cpp b/src/blacksim/fsx/simconnect_datadefinition.cpp similarity index 99% rename from src/blackcore/fsx/simconnect_datadefinition.cpp rename to src/blacksim/fsx/simconnect_datadefinition.cpp index 108020359..a40b008a2 100644 --- a/src/blackcore/fsx/simconnect_datadefinition.cpp +++ b/src/blacksim/fsx/simconnect_datadefinition.cpp @@ -6,7 +6,7 @@ #include "simconnect_datadefinition.h" #include "simconnect/SimConnect.h" -namespace BlackCore +namespace BlackSimPlugin { namespace FSX { diff --git a/src/blackcore/fsx/simconnect_datadefinition.h b/src/blacksim/fsx/simconnect_datadefinition.h similarity index 94% rename from src/blackcore/fsx/simconnect_datadefinition.h rename to src/blacksim/fsx/simconnect_datadefinition.h index da9c57602..785d225af 100644 --- a/src/blackcore/fsx/simconnect_datadefinition.h +++ b/src/blacksim/fsx/simconnect_datadefinition.h @@ -3,12 +3,12 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BLACKCORE_FSX_SIMCONNECT_DATADEFINITION_H -#define BLACKCORE_FSX_SIMCONNECT_DATADEFINITION_H +#ifndef BLACKSIMPLUGIN_FSX_SIMCONNECT_DATADEFINITION_H +#define BLACKSIMPLUGIN_FSX_SIMCONNECT_DATADEFINITION_H #include -namespace BlackCore +namespace BlackSimPlugin { namespace FSX { @@ -102,4 +102,4 @@ namespace BlackCore } } -#endif // BLACKCORE__FSX_SIMCONNECT_DATADEFINITION_H +#endif // BLACKSIMPLUGIN_FSX_SIMCONNECT_DATADEFINITION_H diff --git a/src/blackcore/fsx/simconnect_exception.cpp b/src/blacksim/fsx/simconnect_exception.cpp similarity index 99% rename from src/blackcore/fsx/simconnect_exception.cpp rename to src/blacksim/fsx/simconnect_exception.cpp index 3ae259eed..c317b4d35 100644 --- a/src/blackcore/fsx/simconnect_exception.cpp +++ b/src/blacksim/fsx/simconnect_exception.cpp @@ -6,7 +6,7 @@ #include "simconnect_exception.h" #include -namespace BlackCore +namespace BlackSimPlugin { namespace FSX { diff --git a/src/blackcore/fsx/simconnect_exception.h b/src/blacksim/fsx/simconnect_exception.h similarity index 79% rename from src/blackcore/fsx/simconnect_exception.h rename to src/blacksim/fsx/simconnect_exception.h index 94413c5c4..5763ddfcc 100644 --- a/src/blackcore/fsx/simconnect_exception.h +++ b/src/blacksim/fsx/simconnect_exception.h @@ -3,12 +3,12 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BLACKCORE_FSX_SIMCONNECT_EXCEPTION_H -#define BLACKCORE_FSX_SIMCONNECT_EXCEPTION_H +#ifndef BLACKSIMPLUGIN_FSX_SIMCONNECT_EXCEPTION_H +#define BLACKSIMPLUGIN_FSX_SIMCONNECT_EXCEPTION_H #include "simconnect/SimConnect.h" -namespace BlackCore +namespace BlackSimPlugin { namespace FSX { @@ -27,4 +27,4 @@ namespace BlackCore } } -#endif // BLACKCORE_FSX_SIMCONNECT_EXCEPTION_H +#endif // BLACKSIMPLUGIN_FSX_SIMCONNECT_EXCEPTION_H diff --git a/src/blackcore/fsx/simulator_fsx.cpp b/src/blacksim/fsx/simulator_fsx.cpp similarity index 95% rename from src/blackcore/fsx/simulator_fsx.cpp rename to src/blacksim/fsx/simulator_fsx.cpp index 3e8cb7942..b5d4bbc53 100644 --- a/src/blackcore/fsx/simulator_fsx.cpp +++ b/src/blacksim/fsx/simulator_fsx.cpp @@ -6,16 +6,22 @@ #include "simulator_fsx.h" #include "simconnect_datadefinition.h" #include "simconnect_exception.h" + #include using namespace BlackMisc::Aviation; using namespace BlackMisc::PhysicalQuantities; using namespace BlackMisc::Geo; -namespace BlackCore +namespace BlackSimPlugin { namespace FSX { + BlackCore::ISimulator *CSimulatorFsxFactory::create(QObject *parent) + { + return new FSX::CSimulatorFSX(parent); + } + CSimulatorFSX::CSimulatorFSX(QObject *parent) : ISimulator(parent), m_isConnected(false), @@ -57,7 +63,7 @@ namespace BlackCore hr = SimConnect_AICreateNonATCAircraft(m_hSimConnect, "Boeing 737-800 Paint1", callsign.toQString().left(12).toLatin1().constData(), initialPosition, simObj.m_requestId); } - void CSimulatorFSX::addAircraftSituation(const CCallsign &callsign, const CAircraftSituation & situation) + void CSimulatorFSX::addAircraftSituation(const CCallsign &callsign, const CAircraftSituation &situation) { if (!m_simConnectObjects.contains(callsign)) { @@ -70,7 +76,7 @@ namespace BlackCore m_simConnectObjects.insert(callsign, simObj); } - void CSimulatorFSX::removeRemoteAircraft(const CCallsign &callsign) + void CSimulatorFSX::removeRemoteAircraft(const CCallsign &/*callsign*/) { // TODO } @@ -194,7 +200,7 @@ namespace BlackCore com2.setFrequencyActive(CFrequency(aircraft.com2ActiveMHz, CFrequencyUnit::MHz())); com2.setFrequencyStandby(CFrequency(aircraft.com2StandbyMHz, CFrequencyUnit::MHz())); - CTransponder transponder("Transponder", aircraft.transponderCode, CTransponder::ModeC); + CTransponder transponder("Transponder", aircraft.transponderCode, CTransponder::ModeS); m_ownAircraft.setSituation(aircraftSituation); m_ownAircraft.setCom1System(com1); @@ -218,7 +224,7 @@ namespace BlackCore configuration.gearRight = 100.0; configuration.gearTail = 100.0; configuration.gearAux = 100.0; - SimConnect_SetDataOnSimObject(m_hSimConnect, CSimConnectDataDefinition::DataAircraftConfiguration, simObj.m_objectId, SIMCONNECT_DATA_SET_FLAG_DEFAULT, 0, sizeof(DataDefinitionAircraftConfiguration), &configuration); + SimConnect_SetDataOnSimObject(m_hSimConnect, CSimConnectDataDefinition::DataAircraftConfiguration, objectID, SIMCONNECT_DATA_SET_FLAG_DEFAULT, 0, sizeof(DataDefinitionAircraftConfiguration), &configuration); SimConnectObject simObject; foreach (simObject, m_simConnectObjects) @@ -304,6 +310,8 @@ namespace BlackCore if (simObj.m_objectId != 0) { SimConnect_SetDataOnSimObject(m_hSimConnect, CSimConnectDataDefinition::DataAircraftPosition, simObj.m_objectId, SIMCONNECT_DATA_SET_FLAG_DEFAULT, 0, sizeof(DataDefinitionAircraftPosition), &position); + + // With the following SimConnect call all aircrafts loose their red tag. No idea why though. SimConnect_SetDataOnSimObject(m_hSimConnect, CSimConnectDataDefinition::DataAircraftConfiguration, simObj.m_objectId, SIMCONNECT_DATA_SET_FLAG_DEFAULT, 0, sizeof(DataDefinitionAircraftConfiguration), &configuration); } } diff --git a/src/blackcore/fsx/simulator_fsx.h b/src/blacksim/fsx/simulator_fsx.h similarity index 85% rename from src/blackcore/fsx/simulator_fsx.h rename to src/blacksim/fsx/simulator_fsx.h index 263122482..b565e892a 100644 --- a/src/blackcore/fsx/simulator_fsx.h +++ b/src/blacksim/fsx/simulator_fsx.h @@ -3,8 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BLACKCORE_SIMULATOR_FSX_H -#define BLACKCORE_SIMULATOR_FSX_H +#ifndef BLACKSIMPLUGIN_SIMULATOR_FSX_H +#define BLACKSIMPLUGIN_SIMULATOR_FSX_H #define NOMINMAX @@ -15,14 +15,26 @@ #include "blackmisc/avaircraft.h" #include #include +#include #include #include -namespace BlackCore +namespace BlackSimPlugin { namespace FSX { + //! \brief Factory implementation to create CSimulatorFSX instances + class Q_DECL_EXPORT CSimulatorFsxFactory : public QObject, public BlackCore::ISimulatorFactory + { + Q_OBJECT + Q_PLUGIN_METADATA(IID "net.vatsim.PilotClient.BlackCore.SimulatorInterface") + Q_INTERFACES(BlackCore::ISimulatorFactory) + public: + //! \copydoc BlackCore::ISimulatorFactory::create() + virtual BlackCore::ISimulator* create(QObject *parent) override; + }; + //! \brief SimConnect Event ID's enum EVENT_ID { EVENT_SIM_STATUS, @@ -36,7 +48,7 @@ namespace BlackCore }; //! \brief FSX Simulator Implementation - class CSimulatorFSX : public ISimulator + class CSimulatorFSX : public BlackCore::ISimulator { Q_OBJECT public: @@ -140,4 +152,4 @@ namespace BlackCore } // namespace BlackCore -#endif // BLACKCORE_SIMULATOR_FSX_H +#endif // BLACKSIMPLUGIN_SIMULATOR_FSX_H