XPMPCreatePlaneWithModelName adds plane with a defined model name

It bypasses the internal model matching of libxplanemp and lets
external applications do the model matching themselves.
This commit is contained in:
Roland Winklmeier
2015-06-18 23:13:49 +02:00
committed by Mathew Sutcliffe
parent 3de2779f80
commit fdf149610c
3 changed files with 55 additions and 3 deletions

View File

@@ -379,6 +379,14 @@ void XPMPGetModelInfo(int inIndex, const char **outModelName, const char **outIc
XPMPPlaneData_f inDataFunc,
void * inRefcon);
XPMPPlaneID XPMPCreatePlaneWithModelName(
const char * inModelName,
const char * inICAOCode,
const char * inAirline,
const char * inLivery,
XPMPPlaneData_f inDataFunc,
void * inRefcon);
/*
* XPMPDestroyPlane
*

View File

@@ -27,6 +27,7 @@
#include "XPMPMultiplayerCSL.h"
#include <algorithm>
#include <cctype>
#include <vector>
#include <string>
#include <string.h>
@@ -345,7 +346,50 @@ XPMPPlaneID XPMPCreatePlane(
iter->first.first(plane, xpmp_PlaneNotification_Created, iter->first.second);
}
return plane;
}
}
bool CompareCaseInsensitive(string strFirst, string strSecond)
{
// Convert both strings to upper case by transfrom() before compare.
transform(strFirst.begin(), strFirst.end(), strFirst.begin(), static_cast<int (*)(int)>(std::toupper));
transform(strSecond.begin(), strSecond.end(), strSecond.begin(), static_cast<int (*)(int)>(std::toupper));
return strFirst == strSecond;
}
XPMPPlaneID XPMPCreatePlaneWithModelName(const char *inModelName, const char *inICAOCode, const char *inAirline, const char *inLivery, XPMPPlaneData_f inDataFunc, void *inRefcon)
{
XPMPPlanePtr plane = new XPMPPlane_t;
plane->icao = inICAOCode;
plane->livery = inLivery;
plane->airline = inAirline;
plane->dataFunc = inDataFunc;
plane->ref = inRefcon;
// Find the model
for (auto &package : gPackages)
{
auto cslPlane = std::find_if(package.planes.begin(), package.planes.end(), [inModelName](CSLPlane_t p) { return CompareCaseInsensitive(p.modelName, inModelName); });
if (cslPlane != package.planes.end())
{
plane->model = &(*cslPlane);
}
}
if (!plane->model) return nullptr;
plane->pos.size = sizeof(plane->pos);
plane->surface.size = sizeof(plane->surface);
plane->radar.size = sizeof(plane->radar);
plane->posAge = plane->radarAge = plane->surfaceAge = -1;
gPlanes.push_back(plane);
for (XPMPPlaneNotifierVector::iterator iter = gObservers.begin(); iter !=
gObservers.end(); ++iter)
{
iter->first.first(plane, xpmp_PlaneNotification_Created, iter->first.second);
}
return plane;
}
void XPMPDestroyPlane(XPMPPlaneID inID)
{

View File

@@ -154,12 +154,12 @@ struct XPMPPlane_t {
string icao;
string airline;
string livery;
CSLPlane_t * model; // May be null if no good match
CSLPlane_t * model = nullptr; // May be null if no good match
bool good_livery; // is our paint correctly matched?
// This callback is used to pull data from the client for posiitons, etc.
XPMPPlaneData_f dataFunc;
void * ref;
void * ref = nullptr;
// This is last known data we got for the plane, with timestamps.
int posAge;