mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-30 11:55:35 +08:00
refs #185 , utility class for FSX simconnect (e.g. writing simconnect.cfg , or error coded to QString)
This commit is contained in:
@@ -24,9 +24,13 @@ else: PRE_TARGETDEPS += ../../lib/libblackmisc.a
|
||||
|
||||
HEADERS += *.h
|
||||
SOURCES += *.cpp
|
||||
|
||||
HEADERS += $$PWD/fscommon/*.h
|
||||
SOURCES += $$PWD/fscommon/*.cpp
|
||||
|
||||
HEADERS += $$PWD/fsx/*.h
|
||||
SOURCES += $$PWD/fsx/*.cpp
|
||||
|
||||
DESTDIR = ../../lib
|
||||
|
||||
include (../../libraries.pri)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "blacksimfreefunctions.h"
|
||||
#include "fscommon/aircraftcfgentrieslist.h"
|
||||
#include "fscommon/aircraftmappinglist.h"
|
||||
#include "fsx/simconnectutilities.h"
|
||||
|
||||
namespace BlackSim
|
||||
{
|
||||
@@ -11,6 +12,7 @@ namespace BlackSim
|
||||
BlackSim::FsCommon::CAircraftMapping::registerMetadata();
|
||||
BlackSim::FsCommon::CAircraftCfgEntriesList::registerMetadata();
|
||||
BlackSim::FsCommon::CAircraftMappingList::registerMetadata();
|
||||
BlackSim::Fsx::CSimConnectUtilities::registerMetadata();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
12
src/blacksim/fsx/fsx.h
Normal file
12
src/blacksim/fsx/fsx.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef FSX_NAMESPACES_H
|
||||
#define FSX_NAMESPACES_H
|
||||
|
||||
// just a dummy header, namespace documentation will go here
|
||||
/*! \file */
|
||||
|
||||
/*!
|
||||
* \namespace FSX
|
||||
* \brief Utility classes for FSX
|
||||
*/
|
||||
|
||||
#endif // guard
|
||||
75
src/blacksim/fsx/simconnectutilities.cpp
Normal file
75
src/blacksim/fsx/simconnectutilities.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "simconnectutilities.h"
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QCoreApplication>
|
||||
|
||||
namespace BlackSim
|
||||
{
|
||||
namespace Fsx
|
||||
{
|
||||
CSimConnectUtilities::CSimConnectUtilities() { }
|
||||
|
||||
//
|
||||
// Filename / path of a "local" SimConnect.cfg file
|
||||
//
|
||||
QString CSimConnectUtilities::getLocalSimConnectCfgFilename()
|
||||
{
|
||||
return QCoreApplication::applicationDirPath() + "/SimConnect.cfg";
|
||||
}
|
||||
|
||||
//
|
||||
// Create a very simple SimConnect.cfg
|
||||
//
|
||||
bool CSimConnectUtilities::writeSimConnectCfg(const QString &fileName, const QString &ip, quint16 port)
|
||||
{
|
||||
QString sc = QString("[SimConnect]\nProtocol=Ipv4\nAddress=%1\nPort=%2\n"
|
||||
"MaxReceiveSize=4096\nDisableNagle=0").arg(ip).arg(port);
|
||||
QFile file(fileName);
|
||||
bool success = false;
|
||||
if ((success = file.open(QIODevice::WriteOnly | QIODevice::Text)))
|
||||
{
|
||||
QTextStream out(&file);
|
||||
out << sc;
|
||||
file.close();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
//
|
||||
// Resolve enum id to string
|
||||
// http://labs.qt.nokia.com/2008/10/09/coding-tip-pretty-printing-enum-values/
|
||||
//
|
||||
const QString CSimConnectUtilities::resolveEnumToString(const DWORD id, const char *enumName)
|
||||
{
|
||||
int i = CSimConnectUtilities::staticMetaObject.indexOfEnumerator(enumName);
|
||||
if (i < 0) return QString("No enumerator for %1").arg(enumName);
|
||||
QMetaEnum m = CSimConnectUtilities::staticMetaObject.enumerator(i);
|
||||
const char *k = m.valueToKey(id);
|
||||
return (k) ? QLatin1String(k) : QString("Id %1 not found for %2").arg(id).arg(enumName);
|
||||
}
|
||||
|
||||
//
|
||||
// Exception id to something human readible
|
||||
//
|
||||
const QString CSimConnectUtilities::simConnectExceptionToString(const DWORD id)
|
||||
{
|
||||
return CSimConnectUtilities::resolveEnumToString(id, "SIMCONNECT_EXCEPTION");
|
||||
}
|
||||
|
||||
//
|
||||
// The surface types
|
||||
//
|
||||
const QString CSimConnectUtilities::simConnectSurfaceTypeToString(const DWORD type, bool beautify)
|
||||
{
|
||||
QString sf = CSimConnectUtilities::resolveEnumToString(type, "SIMCONNECT_SURFACE");
|
||||
return beautify ? sf.replace('_', ' ') : sf;
|
||||
}
|
||||
|
||||
void CSimConnectUtilities::registerMetadata()
|
||||
{
|
||||
qRegisterMetaType<CSimConnectUtilities::SIMCONNECT_EXCEPTION>();
|
||||
qRegisterMetaType<CSimConnectUtilities::SIMCONNECT_SURFACE>();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
142
src/blacksim/fsx/simconnectutilities.h
Normal file
142
src/blacksim/fsx/simconnectutilities.h
Normal file
@@ -0,0 +1,142 @@
|
||||
#ifndef BLACKSIM_FSX_SIMCONNECTUTILITIES_H
|
||||
#define BLACKSIM_FSX_SIMCONNECTUTILITIES_H
|
||||
|
||||
#include <QString>
|
||||
#include <QMetaEnum>
|
||||
#include <windows.h>
|
||||
|
||||
namespace BlackSim
|
||||
{
|
||||
namespace Fsx
|
||||
{
|
||||
//! \brief Utilities for SimConnect
|
||||
class CSimConnectUtilities : QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_ENUMS(SIMCONNECT_EXCEPTION)
|
||||
Q_ENUMS(SIMCONNECT_SURFACE)
|
||||
|
||||
private:
|
||||
//! \brief CSimConnect
|
||||
CSimConnectUtilities();
|
||||
|
||||
public:
|
||||
//! Path to local config file
|
||||
static QString getLocalSimConnectCfgFilename();
|
||||
|
||||
/*!
|
||||
* \brief Create a FSX simconnect.cfg file
|
||||
* \param fileName and path
|
||||
* \param ip IP address of FSX
|
||||
* \param port Port of FSX (e.g. 500)
|
||||
* \return success
|
||||
*/
|
||||
static bool writeSimConnectCfg(const QString &fileName, const QString &ip, quint16 port);
|
||||
|
||||
/*!
|
||||
* \brief Resolve SimConnect exception (based on Qt metadata).
|
||||
* \param id enum element
|
||||
* \return enum element's name
|
||||
*/
|
||||
static const QString simConnectExceptionToString(const DWORD id);
|
||||
|
||||
/*!
|
||||
* \brief Resolve SimConnect surface (based on Qt metadata).
|
||||
* \param type enum element
|
||||
* \return enum element's name
|
||||
*/
|
||||
static const QString simConnectSurfaceTypeToString(const DWORD type, bool beautify = true);
|
||||
|
||||
//! SimConnect surfaces.
|
||||
// http://msdn.microsoft.com/en-us/library/cc526981.aspx#AircraftFlightInstrumentationData
|
||||
enum SIMCONNECT_SURFACE
|
||||
{
|
||||
Concrete,
|
||||
Grass,
|
||||
Water,
|
||||
Grass_bumpy,
|
||||
Asphalt,
|
||||
Short_grass,
|
||||
Long_grass,
|
||||
Hard_turf,
|
||||
Snow,
|
||||
Ice,
|
||||
Urban,
|
||||
Forest,
|
||||
Dirt,
|
||||
Coral,
|
||||
Gravel,
|
||||
Oil_treated,
|
||||
Steel_mats,
|
||||
Bituminus,
|
||||
Brick,
|
||||
Macadam,
|
||||
Planks,
|
||||
Sand,
|
||||
Shale,
|
||||
Tarmac,
|
||||
Wright_flyer_track
|
||||
};
|
||||
|
||||
//! SimConnect exceptions.
|
||||
enum SIMCONNECT_EXCEPTION
|
||||
{
|
||||
SIMCONNECT_EXCEPTION_NONE,
|
||||
SIMCONNECT_EXCEPTION_ERROR,
|
||||
SIMCONNECT_EXCEPTION_SIZE_MISMATCH,
|
||||
SIMCONNECT_EXCEPTION_UNRECOGNIZED_ID,
|
||||
SIMCONNECT_EXCEPTION_UNOPENED,
|
||||
SIMCONNECT_EXCEPTION_VERSION_MISMATCH,
|
||||
SIMCONNECT_EXCEPTION_TOO_MANY_GROUPS,
|
||||
SIMCONNECT_EXCEPTION_NAME_UNRECOGNIZED,
|
||||
SIMCONNECT_EXCEPTION_TOO_MANY_EVENT_NAMES,
|
||||
SIMCONNECT_EXCEPTION_EVENT_ID_DUPLICATE,
|
||||
SIMCONNECT_EXCEPTION_TOO_MANY_MAPS,
|
||||
SIMCONNECT_EXCEPTION_TOO_MANY_OBJECTS,
|
||||
SIMCONNECT_EXCEPTION_TOO_MANY_REQUESTS,
|
||||
SIMCONNECT_EXCEPTION_WEATHER_INVALID_PORT,
|
||||
SIMCONNECT_EXCEPTION_WEATHER_INVALID_METAR,
|
||||
SIMCONNECT_EXCEPTION_WEATHER_UNABLE_TO_GET_OBSERVATION,
|
||||
SIMCONNECT_EXCEPTION_WEATHER_UNABLE_TO_CREATE_STATION,
|
||||
SIMCONNECT_EXCEPTION_WEATHER_UNABLE_TO_REMOVE_STATION,
|
||||
SIMCONNECT_EXCEPTION_INVALID_DATA_TYPE,
|
||||
SIMCONNECT_EXCEPTION_INVALID_DATA_SIZE,
|
||||
SIMCONNECT_EXCEPTION_DATA_ERROR,
|
||||
SIMCONNECT_EXCEPTION_INVALID_ARRAY,
|
||||
SIMCONNECT_EXCEPTION_CREATE_OBJECT_FAILED,
|
||||
SIMCONNECT_EXCEPTION_LOAD_FLIGHTPLAN_FAILED,
|
||||
SIMCONNECT_EXCEPTION_OPERATION_INVALID_FOR_OBJECT_TYPE,
|
||||
SIMCONNECT_EXCEPTION_ILLEGAL_OPERATION,
|
||||
SIMCONNECT_EXCEPTION_ALREADY_SUBSCRIBED,
|
||||
SIMCONNECT_EXCEPTION_INVALID_ENUM,
|
||||
SIMCONNECT_EXCEPTION_DEFINITION_ERROR,
|
||||
SIMCONNECT_EXCEPTION_DUPLICATE_ID,
|
||||
SIMCONNECT_EXCEPTION_DATUM_ID,
|
||||
SIMCONNECT_EXCEPTION_OUT_OF_BOUNDS,
|
||||
SIMCONNECT_EXCEPTION_ALREADY_CREATED,
|
||||
SIMCONNECT_EXCEPTION_OBJECT_OUTSIDE_REALITY_BUBBLE,
|
||||
SIMCONNECT_EXCEPTION_OBJECT_CONTAINER,
|
||||
SIMCONNECT_EXCEPTION_OBJECT_AI,
|
||||
SIMCONNECT_EXCEPTION_OBJECT_ATC,
|
||||
SIMCONNECT_EXCEPTION_OBJECT_SCHEDULE
|
||||
};
|
||||
|
||||
//! \brief Register metadata
|
||||
static void registerMetadata();
|
||||
|
||||
private:
|
||||
/*!
|
||||
* \brief Resolve enum value to its cleartext (based on Qt metadata).
|
||||
* \param id enum element
|
||||
* \param enumName name of the resolved enum
|
||||
* \return enum element's name
|
||||
*/
|
||||
static const QString resolveEnumToString(const DWORD id, const char *enumName);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Q_DECLARE_METATYPE(BlackSim::Fsx::CSimConnectUtilities::SIMCONNECT_EXCEPTION)
|
||||
Q_DECLARE_METATYPE(BlackSim::Fsx::CSimConnectUtilities::SIMCONNECT_SURFACE)
|
||||
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user