Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selective array writing #80

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
16 changes: 14 additions & 2 deletions extplane-plugin/xplaneplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,25 @@ void XPlanePlugin::changeDataRef(DataRef *ref)
case extplaneRefTypeFloatArray:
{
FloatArrayDataRef *faRef = qobject_cast<FloatArrayDataRef*>(ref);
XPLMSetDatavf(ref->ref(), faRef->valueArray(), 0, faRef->value().size());
while(!faRef->changedIndices->empty()) {
XPLMSetDatavf(ref->ref(), faRef->valueArray() + faRef->changedIndices->front().lower,
faRef->changedIndices->front().lower,
faRef->changedIndices->front().upper - faRef->changedIndices->front().lower + 1
);
faRef->changedIndices->pop_front();
}
break;
}
case extplaneRefTypeIntArray:
{
IntArrayDataRef *iaRef = qobject_cast<IntArrayDataRef*>(ref);
XPLMSetDatavi(ref->ref(), iaRef->valueArray(), 0, iaRef->value().size());
while(!iaRef->changedIndices.empty()) {
XPLMSetDatavi(ref->ref(), iaRef->valueArray() + iaRef->changedIndices.front().lower,
iaRef->changedIndices.front().lower,
iaRef->changedIndices.front().upper - iaRef->changedIndices.front().lower + 1
);
iaRef->changedIndices.pop_front();
}
break;
}
case extplaneRefTypeInt:
Expand Down
4 changes: 4 additions & 0 deletions extplane-server/datarefs/dataref.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ enum {
extplaneRefTypeData = 32
};

typedef struct {
int lower;
int upper;
} indexPair;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could just use a pair here:

using indexPair = std::pair<int>;

Then just use first and second instead of lower and upper.


/**
* Base class for DataRefs.
Expand Down
16 changes: 16 additions & 0 deletions extplane-server/datarefs/floatarraydataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ FloatArrayDataRef::FloatArrayDataRef(QObject *parent, QString name, void *ref) :
_type = extplaneRefTypeFloatArray;
_length = 0;
_valueArray = nullptr;
changedIndices = new std::list<indexPair>;
}

FloatArrayDataRef::~FloatArrayDataRef() {
if(_valueArray) delete [] _valueArray;
if(changedIndices) delete changedIndices;
}

std::vector<float> &FloatArrayDataRef::value() {
Expand Down Expand Up @@ -53,12 +55,18 @@ QString FloatArrayDataRef::valueString() {
}

void FloatArrayDataRef::setValue(QString &newValue) {
indexPair indPair;

indPair.lower = 0;
indPair.upper = 0;
// Check that value starts with [ and ends with ]
if(!newValue.startsWith('[') || !newValue.endsWith(']')) {
INFO << "Invalid array value" << newValue;
return;
}

changedIndices->clear();

// Remove [] and split values
QString arrayString = newValue.mid(1, newValue.length() - 2);
QStringList values = arrayString.split(',');
Expand All @@ -72,6 +80,14 @@ void FloatArrayDataRef::setValue(QString &newValue) {
float value = values[i].toFloat(&ok);
if(ok) {
_valueArray[i] = value;
if(changedIndices->empty() || changedIndices->back().upper != (i-1))
{
indPair.lower = i;
indPair.upper = i;
changedIndices->push_back(indPair);
} else {
changedIndices->back().upper = i;
}
} else if(!values.at(i).isEmpty()) {
INFO << "Invalid value " << values.at(i) << "in array";
}
Expand Down
2 changes: 2 additions & 0 deletions extplane-server/datarefs/floatarraydataref.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "dataref.h"
#include <vector>
#include <list>

class FloatArrayDataRef : public DataRef {
Q_OBJECT
Expand All @@ -19,6 +20,7 @@ class FloatArrayDataRef : public DataRef {
FloatArrayDataRef(QObject *parent, QString name, void* ref);
~FloatArrayDataRef();
std::vector<float> &value();
std::list<indexPair>* changedIndices;
virtual void updateValue();
virtual QString valueString();
virtual void setValue(QString &newValue);
Expand Down
17 changes: 17 additions & 0 deletions extplane-server/datarefs/intarraydataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ IntArrayDataRef::IntArrayDataRef(QObject *parent, QString name, void *ref) : Dat
_type = extplaneRefTypeIntArray;
_length = 0;
_valueArray = nullptr;
changedIndices = new std::list<indexPair>;
}

IntArrayDataRef::~IntArrayDataRef() {
if(_valueArray) delete [] _valueArray;
if(changedIndices) delete changedIndices;
}

std::vector<int> & IntArrayDataRef::value() {
Expand Down Expand Up @@ -43,12 +45,19 @@ QString IntArrayDataRef::valueString() {
}

void IntArrayDataRef::setValue(QString &newValue) {
indexPair indPair;

indPair.lower = 0;
indPair.upper = 0;

// Check that value starts with [ and ends with ]
if(!newValue.startsWith('[') || !newValue.endsWith(']')) {
INFO << "Invalid array value" << newValue;
return;
}

changedIndices->clear();

// Remove [] and split values
QString arrayString = newValue.mid(1, newValue.length() - 2);
QStringList values = arrayString.split(',');
Expand All @@ -62,6 +71,14 @@ void IntArrayDataRef::setValue(QString &newValue) {
int value = values[i].toInt(&ok);
if(ok) {
_valueArray[i] = value;
if(changedIndices->empty() || changedIndices->back().upper != (i-1))
{
indPair.lower = i;
indPair.upper = i;
changedIndices->push_back(indPair);
} else {
changedIndices->back().upper = i;
}
} else if(!values.at(i).isEmpty()) {
INFO << "Invalid value " << values.at(i) << "in array";
}
Expand Down
2 changes: 2 additions & 0 deletions extplane-server/datarefs/intarraydataref.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "dataref.h"
#include <QObject>
#include <vector>
#include <list>

class IntArrayDataRef : public DataRef {
Q_OBJECT
Expand All @@ -12,6 +13,7 @@ class IntArrayDataRef : public DataRef {
IntArrayDataRef(QObject *parent, QString name, void* ref);
~IntArrayDataRef();
std::vector<int> &value();
std::list<indexPair>* changedIndices;
virtual void updateValue();
virtual QString valueString();
virtual void setValue(QString &newValue);
Expand Down
2 changes: 1 addition & 1 deletion protocoldefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Network protocol, currently always 1
#define EXTPLANE_PROTOCOL 1
// Feature revision, every time we add a new feature or bug fix, this should be incremented so that clients can know how old the plugin is
#define EXTPLANE_VERSION 1010
#define EXTPLANE_VERSION 1011

// Base of udp ports. Bind to this + client_id
#define UDP_OUT_PORT_BASE 52000