1
0
mirror of synced 2024-11-05 02:59:16 +00:00

Added FSX worker to marshal SimConnect data to Lua structures - untested

This commit is contained in:
Mark van Renswoude 2017-05-29 22:56:07 +02:00
parent 54e7e36ee9
commit f92d3e612b
5 changed files with 343 additions and 146 deletions

View File

@ -208,6 +208,58 @@ type
end; end;
TCustomLuaParameters = class(TInterfacedObject, ILuaVariable, ILuaReadParameters)
protected
function GetDefaultVariable: ILuaVariable;
public
{ ILuaVariable }
function GetVariableType: TLuaVariableType;
function GetDataType: TLuaDataType;
function GetAsBoolean: Boolean;
function GetAsInteger: Integer;
function GetAsNumber: Double;
function GetAsUserData: Pointer;
function GetAsString: string;
function GetAsTable: ILuaTable;
function GetAsFunction: ILuaFunction;
procedure SetAsBoolean(ABoolean: Boolean);
procedure SetAsInteger(AInteger: Integer);
procedure SetAsNumber(ANumber: Double);
procedure SetAsUserData(AUserData: Pointer);
procedure SetAsString(AString: string);
procedure SetAsTable(ATable: ILuaTable);
{ ILuaReadParameters }
function GetCount: Integer; virtual; abstract;
function GetItem(Index: Integer): ILuaVariable; virtual; abstract;
function GetEnumerator: ILuaParametersEnumerator;
function ToString: string; override;
end;
TLuaParameters = class(TCustomLuaParameters, ILuaWriteParameters)
private
FParameters: TList<ILuaVariable>;
public
constructor Create;
destructor Destroy; override;
function GetCount: Integer; override;
function GetItem(Index: Integer): ILuaVariable; override;
{ ILuaWriteParameters }
procedure Push(ABoolean: Boolean); overload;
procedure Push(AInteger: Integer); overload;
procedure Push(ANumber: Double); overload;
procedure Push(AUserData: Pointer); overload;
procedure Push(const AString: string); overload;
procedure Push(ATable: ILuaTable); overload;
end;
ILuaFunction = interface ILuaFunction = interface
['{1BE5E470-0318-410E-8D5B-94BFE04A3DBE}'] ['{1BE5E470-0318-410E-8D5B-94BFE04A3DBE}']
function Call(): ILuaReadParameters; overload; function Call(): ILuaReadParameters; overload;
@ -413,38 +465,6 @@ type
end; end;
TCustomLuaParameters = class(TInterfacedObject, ILuaVariable, ILuaReadParameters)
protected
function GetDefaultVariable: ILuaVariable;
public
{ ILuaVariable }
function GetVariableType: TLuaVariableType;
function GetDataType: TLuaDataType;
function GetAsBoolean: Boolean;
function GetAsInteger: Integer;
function GetAsNumber: Double;
function GetAsUserData: Pointer;
function GetAsString: string;
function GetAsTable: ILuaTable;
function GetAsFunction: ILuaFunction;
procedure SetAsBoolean(ABoolean: Boolean);
procedure SetAsInteger(AInteger: Integer);
procedure SetAsNumber(ANumber: Double);
procedure SetAsUserData(AUserData: Pointer);
procedure SetAsString(AString: string);
procedure SetAsTable(ATable: ILuaTable);
{ ILuaReadParameters }
function GetCount: Integer; virtual; abstract;
function GetItem(Index: Integer): ILuaVariable; virtual; abstract;
function GetEnumerator: ILuaParametersEnumerator;
function ToString: string; override;
end;
TLuaStackParameters = class(TCustomLuaParameters) TLuaStackParameters = class(TCustomLuaParameters)
private private
FState: lua_State; FState: lua_State;
@ -568,26 +588,6 @@ type
end; end;
TLuaReadWriteParameters = class(TCustomLuaParameters, ILuaWriteParameters)
private
FParameters: TList<ILuaVariable>;
public
constructor Create;
destructor Destroy; override;
function GetCount: Integer; override;
function GetItem(Index: Integer): ILuaVariable; override;
{ ILuaWriteParameters }
procedure Push(ABoolean: Boolean); overload;
procedure Push(AInteger: Integer); overload;
procedure Push(ANumber: Double); overload;
procedure Push(AUserData: Pointer); overload;
procedure Push(const AString: string); overload;
procedure Push(ATable: ILuaTable); overload;
end;
TLuaStackWriteParameters = class(TInterfacedObject, ILuaWriteParameters) TLuaStackWriteParameters = class(TInterfacedObject, ILuaWriteParameters)
private private
FState: lua_State; FState: lua_State;
@ -682,11 +682,11 @@ class function TLuaHelpers.CreateParameters(AParameters: array of const): ILuaRe
var var
parameterIndex: Integer; parameterIndex: Integer;
parameter: TVarRec; parameter: TVarRec;
resultParameters: TLuaReadWriteParameters; resultParameters: TLuaParameters;
table: ILuaTable; table: ILuaTable;
begin begin
resultParameters := TLuaReadWriteParameters.Create; resultParameters := TLuaParameters.Create;
for parameterIndex := Low(AParameters) to High(AParameters) do for parameterIndex := Low(AParameters) to High(AParameters) do
begin begin
@ -2263,8 +2263,8 @@ begin
end; end;
{ TLuaReadWriteParameters } { TLuaParameters }
constructor TLuaReadWriteParameters.Create; constructor TLuaParameters.Create;
begin begin
inherited Create; inherited Create;
@ -2272,7 +2272,7 @@ begin
end; end;
destructor TLuaReadWriteParameters.Destroy; destructor TLuaParameters.Destroy;
begin begin
FreeAndNil(FParameters); FreeAndNil(FParameters);
@ -2280,49 +2280,49 @@ begin
end; end;
function TLuaReadWriteParameters.GetCount: Integer; function TLuaParameters.GetCount: Integer;
begin begin
Result := FParameters.Count; Result := FParameters.Count;
end; end;
function TLuaReadWriteParameters.GetItem(Index: Integer): ILuaVariable; function TLuaParameters.GetItem(Index: Integer): ILuaVariable;
begin begin
Result := FParameters[Index]; Result := FParameters[Index];
end; end;
procedure TLuaReadWriteParameters.Push(ABoolean: Boolean); procedure TLuaParameters.Push(ABoolean: Boolean);
begin begin
FParameters.Add(TLuaVariable.Create(ABoolean)); FParameters.Add(TLuaVariable.Create(ABoolean));
end; end;
procedure TLuaReadWriteParameters.Push(AInteger: Integer); procedure TLuaParameters.Push(AInteger: Integer);
begin begin
FParameters.Add(TLuaVariable.Create(AInteger)); FParameters.Add(TLuaVariable.Create(AInteger));
end; end;
procedure TLuaReadWriteParameters.Push(ANumber: Double); procedure TLuaParameters.Push(ANumber: Double);
begin begin
FParameters.Add(TLuaVariable.Create(ANumber)); FParameters.Add(TLuaVariable.Create(ANumber));
end; end;
procedure TLuaReadWriteParameters.Push(AUserData: Pointer); procedure TLuaParameters.Push(AUserData: Pointer);
begin begin
FParameters.Add(TLuaVariable.Create(AUserData)); FParameters.Add(TLuaVariable.Create(AUserData));
end; end;
procedure TLuaReadWriteParameters.Push(const AString: string); procedure TLuaParameters.Push(const AString: string);
begin begin
FParameters.Add(TLuaVariable.Create(AString)); FParameters.Add(TLuaVariable.Create(AString));
end; end;
procedure TLuaReadWriteParameters.Push(ATable: ILuaTable); procedure TLuaParameters.Push(ATable: ILuaTable);
begin begin
FParameters.Add(TLuaVariable.Create(ATable)); FParameters.Add(TLuaVariable.Create(ATable));
end; end;

View File

@ -61,16 +61,18 @@ type
constructor Create(AProvider: TFSXLEDFunctionProvider; AInfo: ILuaTable; AOnSetup: ILuaFunction); constructor Create(AProvider: TFSXLEDFunctionProvider; AInfo: ILuaTable; AOnSetup: ILuaFunction);
end; end;
TFSXDefinition = record
ID: Cardinal;
DataHandler: IFSXSimConnectDataHandler;
end;
TFSXLEDFunctionWorker = class(TCustomLuaLEDFunctionWorker) TFSXLEDFunctionWorker = class(TCustomLuaLEDFunctionWorker)
private private
FDataHandler: IFSXSimConnectDataHandler; FDefinitions: TList<TFSXDefinition>;
FDefinitionID: TList<Cardinal>;
protected protected
property DataHandler: IFSXSimConnectDataHandler read FDataHandler; property Definitions: TList<TFSXDefinition> read FDefinitions;
property DefinitionID: TList<Cardinal> read FDefinitionID;
protected protected
procedure AddDefinition(ADefinition: IFSXSimConnectDefinition); procedure AddDefinition(ADefinition: IFSXSimConnectDefinition; ADataHandler: IFSXSimConnectDataHandler);
procedure HandleData(AData: Pointer); virtual; abstract; procedure HandleData(AData: Pointer); virtual; abstract;
public public
@ -94,16 +96,39 @@ uses
type type
TCustomFSXFunctionWorkerDataHandler = class(TInterfacedObject, IFSXSimConnectDataHandler) TLuaSimConnectDataType = (
// Native types
Float64, Float32, Int64, Int32, StringValue,
// Preprocessed types (for scripting convenience)
Bool,
// Structures
XYZ, LatLonAlt, Waypoint
);
TLuaSimConnectVariable = record
Name: string;
DataType: TLuaSimConnectDataType;
end;
TFSXFunctionWorkerDataHandler = class(TInterfacedObject, IFSXSimConnectDataHandler)
private private
FWorker: TFSXLEDFunctionWorker; FOnData: ILuaFunction;
FWorkerID: string;
FVariables: TList<TLuaSimConnectVariable>;
protected protected
{ IFSXSimConnectDataHandler } { IFSXSimConnectDataHandler }
procedure HandleData(AData: Pointer); procedure HandleData(AData: Pointer);
property Worker: TFSXLEDFunctionWorker read FWorker; property OnData: ILuaFunction read FOnData;
property Variables: TList<TLuaSimConnectVariable> read FVariables;
property WorkerID: string read FWorkerID;
public public
constructor Create(AWorker: TFSXLEDFunctionWorker); constructor Create(AVariables: TList<TLuaSimConnectVariable>; const AWorkerID: string; AOnData: ILuaFunction);
destructor Destroy; override;
end; end;
@ -119,34 +144,24 @@ type
end; end;
type
TLuaSimConnectType = record
TypeName: string;
Units: string;
DataType: SIMCONNECT_DATAType;
end;
const const
LuaSimConnectTypes: array[0..4] of TLuaSimConnectType = LuaSimConnectDataTypes: array[TLuaSimConnectDataType] of string =
( (
( TypeName: 'Bool'; Units: FSX_UNIT_BOOL; DataType: SIMCONNECT_DATAType_INT32 ), 'Float64', 'Float32', 'Int64', 'Int32', 'String',
( TypeName: 'Percent'; Units: FSX_UNIT_PERCENT; DataType: SIMCONNECT_DATAType_FLOAT64 ), 'Bool',
( TypeName: 'Integer'; Units: FSX_UNIT_NUMBER; DataType: SIMCONNECT_DATAType_INT32 ), 'XYZ', 'LatLonAlt', 'Waypoint'
( TypeName: 'Float'; Units: FSX_UNIT_NUMBER; DataType: SIMCONNECT_DATAType_FLOAT64 ), );
( TypeName: 'Mask'; Units: FSX_UNIT_MASK; DataType: SIMCONNECT_DATATYPE_INT32 )
);
function GetUnits(const AType: string; out AUnits: string; out ADataType: SIMCONNECT_DATAType): Boolean; function GetDataType(const ATypeName: string; out ADataType: TLuaSimConnectDataType): Boolean;
var var
typeIndex: Integer; dataType: TLuaSimConnectDataType;
begin begin
for typeIndex := Low(LuaSimConnectTypes) to High(LuaSimConnectTypes) do for dataType := Low(TLuaSimConnectDataType) to High(TLuaSimConnectDataType) do
if SameText(AType, LuaSimConnectTypes[typeIndex].TypeName) then if SameText(ATypeName, LuaSimConnectDataTypes[dataType]) then
begin begin
AUnits := LuaSimConnectTypes[typeIndex].Units; ADataType := dataType;
ADataType := LuaSimConnectTypes[typeIndex].DataType;
Exit(True); Exit(True);
end; end;
@ -251,7 +266,7 @@ end;
procedure TFSXLEDFunctionProvider.InitInterpreter; procedure TFSXLEDFunctionProvider.InitInterpreter;
var var
simConnectType: ILuaTable; simConnectType: ILuaTable;
typeIndex: Integer; dataType: TLuaSimConnectDataType;
begin begin
inherited InitInterpreter; inherited InitInterpreter;
@ -259,8 +274,8 @@ begin
Interpreter.RegisterFunctions(FScriptSimConnect, 'SimConnect'); Interpreter.RegisterFunctions(FScriptSimConnect, 'SimConnect');
simConnectType := TLuaTable.Create; simConnectType := TLuaTable.Create;
for typeIndex := Low(LuaSimConnectTypes) to High(LuaSimConnectTypes) do for dataType := Low(TLuaSimConnectDataType) to High(TLuaSimConnectDataType) do
simConnectType.SetValue(LuaSimConnectTypes[typeIndex].TypeName, LuaSimConnectTypes[typeIndex].TypeName); simConnectType.SetValue(LuaSimConnectDataTypes[dataType], LuaSimConnectDataTypes[dataType]);
Interpreter.SetGlobalVariable('SimConnectType', simConnectType); Interpreter.SetGlobalVariable('SimConnectType', simConnectType);
end; end;
@ -358,11 +373,7 @@ end;
{ TFSXLEDFunctionWorker } { TFSXLEDFunctionWorker }
constructor TFSXLEDFunctionWorker.Create(const AProviderUID, AFunctionUID: string; AStates: ILEDMultiStateFunction; ASettings: ILEDFunctionWorkerSettings; const APreviousState: string); constructor TFSXLEDFunctionWorker.Create(const AProviderUID, AFunctionUID: string; AStates: ILEDMultiStateFunction; ASettings: ILEDFunctionWorkerSettings; const APreviousState: string);
begin begin
{ We can't pass ourselves as the Data Handler, as it would keep a reference to FDefinitions := TList<TFSXDefinition>.Create;
this worker from the SimConnect interface. That'd mean the worker never
gets destroyed, and SimConnect never shuts down. Hence this proxy class. }
FDataHandler := TCustomFSXFunctionWorkerDataHandler.Create(Self);
FDefinitionID := TList<Cardinal>.Create;
inherited Create(AProviderUID, AFunctionUID, AStates, ASettings, APreviousState); inherited Create(AProviderUID, AFunctionUID, AStates, ASettings, APreviousState);
end; end;
@ -371,41 +382,174 @@ end;
destructor TFSXLEDFunctionWorker.Destroy; destructor TFSXLEDFunctionWorker.Destroy;
var var
simConnect: IFSXSimConnect; simConnect: IFSXSimConnect;
id: Cardinal; definition: TFSXDefinition;
begin begin
if Assigned(Provider) and (DefinitionID.Count > 0) then if Assigned(Provider) and (Definitions.Count > 0) then
begin begin
simConnect := (Provider as TFSXLEDFunctionProvider).GetSimConnect; simConnect := (Provider as TFSXLEDFunctionProvider).GetSimConnect;
for id in DefinitionID do for definition in Definitions do
simConnect.RemoveDefinition(id, DataHandler); simConnect.RemoveDefinition(definition.ID, definition.DataHandler);
end; end;
FreeAndNil(FDefinitionID); FreeAndNil(FDefinitions);
inherited Destroy; inherited Destroy;
end; end;
procedure TFSXLEDFunctionWorker.AddDefinition(ADefinition: IFSXSimConnectDefinition); procedure TFSXLEDFunctionWorker.AddDefinition(ADefinition: IFSXSimConnectDefinition; ADataHandler: IFSXSimConnectDataHandler);
var
definition: TFSXDefinition;
begin begin
DefinitionID.Add((Provider as TFSXLEDFunctionProvider).GetSimConnect.AddDefinition(ADefinition, DataHandler)); definition.DataHandler := ADataHandler;
definition.ID := (Provider as TFSXLEDFunctionProvider).GetSimConnect.AddDefinition(ADefinition, ADataHandler);
Definitions.Add(definition);
end; end;
{ TCustomFSXFunctionWorkerDataHandler } { TFSXFunctionWorkerDataHandler }
constructor TCustomFSXFunctionWorkerDataHandler.Create(AWorker: TFSXLEDFunctionWorker); constructor TFSXFunctionWorkerDataHandler.Create(AVariables: TList<TLuaSimConnectVariable>; const AWorkerID: string; AOnData: ILuaFunction);
begin begin
inherited Create; inherited Create;
FWorker := AWorker; FWorkerID := AWorkerID;
FOnData := AOnData;
FVariables := TList<TLuaSimConnectVariable>.Create;
FVariables.AddRange(AVariables);
end; end;
procedure TCustomFSXFunctionWorkerDataHandler.HandleData(AData: Pointer); destructor TFSXFunctionWorkerDataHandler.Destroy;
begin begin
Worker.HandleData(AData); FreeAndNil(FVariables);
inherited Destroy;
end;
procedure TFSXFunctionWorkerDataHandler.HandleData(AData: Pointer);
var
data: ILuaTable;
dataPointer: PByte;
variableIndex: Integer;
variable: TLuaSimConnectVariable;
value: string;
structure: ILuaTable;
flags: ILuaTable;
xyzData: ^SIMCONNECT_DATA_XYZ;
latLonAltData: ^SIMCONNECT_DATA_LATLONALT;
waypointData: ^SIMCONNECT_DATA_WAYPOINT;
begin
data := TLuaTable.Create;
dataPointer := AData;
for variableIndex := 0 to Pred(Variables.Count) do
begin
variable := Variables[variableIndex];
case variable.DataType of
Float64:
begin
data.SetValue(variable.Name, PDouble(dataPointer)^);
Inc(dataPointer, SizeOf(Double));
end;
Float32:
begin
data.SetValue(variable.Name, PSingle(dataPointer)^);
Inc(dataPointer, SizeOf(Single));
end;
Int64:
begin
data.SetValue(variable.Name, PInt64(dataPointer)^);
Inc(dataPointer, SizeOf(Int64));
end;
Int32:
begin
data.SetValue(variable.Name, PInteger(dataPointer)^);
Inc(dataPointer, SizeOf(Integer));
end;
StringValue:
begin
// TODO change to STRINGV
//SimConnect_RetrieveString()
SetString(value, PChar(dataPointer), 256);
data.SetValue(variable.Name, value);
Inc(dataPointer, 256);
end;
Bool:
begin
data.SetValue(variable.Name, (PInteger(dataPointer)^ <> 0));
Inc(dataPointer, SizeOf(Integer));
end;
XYZ:
begin
xyzData := AData;
structure := TLuaTable.Create;
structure.SetValue('X', xyzData^.x);
structure.SetValue('Y', xyzData^.y);
structure.SetValue('Z', xyzData^.z);
data.SetValue(variable.Name, structure);
Inc(dataPointer, SizeOf(SIMCONNECT_DATA_XYZ));
end;
LatLonAlt:
begin
latLonAltData := AData;
structure := TLuaTable.Create;
structure.SetValue('Latitude', latLonAltData^.Latitude);
structure.SetValue('Longitude', latLonAltData^.Longitude);
structure.SetValue('Altitude', latLonAltData^.Altitude);
data.SetValue(variable.Name, structure);
Inc(dataPointer, SizeOf(SIMCONNECT_DATA_LATLONALT));
end;
Waypoint:
begin
waypointData := AData;
structure := TLuaTable.Create;
structure.SetValue('Latitude', waypointData^.Latitude);
structure.SetValue('Longitude', waypointData^.Longitude);
structure.SetValue('Altitude', waypointData^.Altitude);
structure.SetValue('KtsSpeed', waypointData^.ktsSpeed);
structure.SetValue('PercentThrottle', waypointData^.percentThrottle);
flags := TLuaTable.Create;
flags.SetValue('SpeedRequested', (waypointData^.Flags and SIMCONNECT_WAYPOINT_SPEED_REQUESTED) <> 0);
flags.SetValue('ThrottleRequested', (waypointData^.Flags and SIMCONNECT_WAYPOINT_THROTTLE_REQUESTED) <> 0);
flags.SetValue('ComputeVerticalSpeed', (waypointData^.Flags and SIMCONNECT_WAYPOINT_COMPUTE_VERTICAL_SPEED) <> 0);
flags.SetValue('IsAGL', (waypointData^.Flags and SIMCONNECT_WAYPOINT_ALTITUDE_IS_AGL) <> 0);
flags.SetValue('OnGround', (waypointData^.Flags and SIMCONNECT_WAYPOINT_ON_GROUND) <> 0);
flags.SetValue('Reverse', (waypointData^.Flags and SIMCONNECT_WAYPOINT_REVERSE) <> 0);
flags.SetValue('WrapToFirst', (waypointData^.Flags and SIMCONNECT_WAYPOINT_WRAP_TO_FIRST) <> 0);
structure.SetValue('Flags', flags);
data.SetValue(variable.Name, structure);
Inc(dataPointer, SizeOf(SIMCONNECT_DATA_WAYPOINT));
end;
end;
end;
OnData.Call([WorkerID, data]);
end; end;
@ -427,8 +571,11 @@ var
definition: IFSXSimConnectDefinition; definition: IFSXSimConnectDefinition;
variable: TLuaKeyValuePair; variable: TLuaKeyValuePair;
info: ILuaTable; info: ILuaTable;
dataType: TLuaSimConnectDataType;
simConnectDataType: SIMCONNECT_DATAType;
units: string; units: string;
dataType: SIMCONNECT_DATAType; luaVariables: TList<TLuaSimConnectVariable>;
luaVariable: TLuaSimConnectVariable;
begin begin
if Context.Parameters.Count < 3 then if Context.Parameters.Count < 3 then
@ -453,21 +600,62 @@ begin
definition := Provider.GetSimConnect.CreateDefinition; definition := Provider.GetSimConnect.CreateDefinition;
for variable in variables do luaVariables := TList<TLuaSimConnectVariable>.Create;
begin try
if variable.Value.VariableType = VariableTable then for variable in variables do
begin begin
info := variable.Value.AsTable; if variable.Value.VariableType = VariableTable then
if info.HasValue('variable') and
info.HasValue('type') and
GetUnits(info.GetValue('type').AsString, units, dataType) then
begin begin
definition.AddVariable(info.GetValue('variable').AsString, units, dataType); info := variable.Value.AsTable;
if info.HasValue('variable') then
begin
luaVariable.Name := variable.Key.AsString;
units := '';
simConnectDataType := SIMCONNECT_DATAType_FLOAT64;
if info.HasValue('type') and GetDataType(info.GetValue('type').AsString, dataType) then
begin
luaVariable.DataType := dataType;
case dataType of
Float32: simConnectDataType := SIMCONNECT_DATAType_FLOAT32;
Int64: simConnectDataType := SIMCONNECT_DATAType_INT64;
Int32,
Bool:
begin
simConnectDataType := SIMCONNECT_DATAType_INT32;
units := 'bool';
end;
// TODO change to STRINGV
StringValue: simConnectDataType := SIMCONNECT_DATAType_STRING256;
XYZ: simConnectDataType := SIMCONNECT_DATAType_XYZ;
LatLonAlt: simConnectDataType := SIMCONNECT_DATAType_LATLONALT;
Waypoint: simConnectDataType := SIMCONNECT_DATAType_WAYPOINT;
end;
if info.HasValue('units') then
units := info.GetValue('units').AsString
else if not (dataType in [Bool, StringValue, XYZ, LatLonAlt, Waypoint]) then
raise ELuaScriptError.CreateFmt('Missing units for variable %s', [variable.Key.AsString]);
end else
begin
if not info.HasValue('units') then
raise ELuaScriptError.CreateFmt('Missing units or type for variable %s', [variable.Key.AsString]);
units := info.GetValue('units').AsString;
end;
luaVariables.Add(luaVariable);
definition.AddVariable(info.GetValue('variable').AsString, units, simConnectDataType);
end;
end; end;
end; end;
end;
(worker as TFSXLEDFunctionWorker).AddDefinition(definition); (worker as TFSXLEDFunctionWorker).AddDefinition(definition, TFSXFunctionWorkerDataHandler.Create(luaVariables, worker.UID, onData));
finally
FreeAndNil(luaVariables);
end;
end; end;
end. end.

View File

@ -88,12 +88,12 @@ const
type type
TFSXSimConnectDefinitionRef = class(TObject) TFSXSimConnectDefinitionRef = class(TObject)
private private
FDefinition: IFSXSimConnectDefinitionAccess; FDefinition: IFSXSimConnectDefinition;
FDataHandlers: TInterfaceList; FDataHandlers: TInterfaceList;
protected protected
property DataHandlers: TInterfaceList read FDataHandlers; property DataHandlers: TInterfaceList read FDataHandlers;
public public
constructor Create(ADefinition: IFSXSimConnectDefinitionAccess); constructor Create(ADefinition: IFSXSimConnectDefinition);
destructor Destroy; override; destructor Destroy; override;
function Attach(ADataHandler: IFSXSimConnectDataHandler): Integer; function Attach(ADataHandler: IFSXSimConnectDataHandler): Integer;
@ -101,7 +101,7 @@ type
procedure HandleData(AData: Pointer); procedure HandleData(AData: Pointer);
property Definition: IFSXSimConnectDefinitionAccess read FDefinition; property Definition: IFSXSimConnectDefinition read FDefinition;
end; end;
@ -145,11 +145,11 @@ type
procedure TrySimConnect(const ALibraryName: string); overload; procedure TrySimConnect(const ALibraryName: string); overload;
procedure RegisterDefinitions; procedure RegisterDefinitions;
procedure RegisterDefinition(ADefinitionID: Cardinal; ADefinition: IFSXSimConnectDefinitionAccess); procedure RegisterDefinition(ADefinitionID: Cardinal; ADefinition: IFSXSimConnectDefinition);
procedure UpdateDefinition(ADefinitionID: Cardinal); procedure UpdateDefinition(ADefinitionID: Cardinal);
procedure UnregisterDefinition(ADefinitionID: Cardinal); procedure UnregisterDefinition(ADefinitionID: Cardinal);
function SameDefinition(ADefinition1, ADefinition2: IFSXSimConnectDefinitionAccess): Boolean; function SameDefinition(ADefinition1, ADefinition2: IFSXSimConnectDefinition): Boolean;
procedure UpdateProfileMenu; procedure UpdateProfileMenu;
@ -187,7 +187,7 @@ type
TFSXSimConnectVariableList = TObjectList<TFSXSimConnectVariable>; TFSXSimConnectVariableList = TObjectList<TFSXSimConnectVariable>;
TFSXSimConnectDefinition = class(TInterfacedObject, IFSXSimConnectDefinition, IFSXSimConnectDefinitionAccess) TFSXSimConnectDefinition = class(TInterfacedObject, IFSXSimConnectDefinition)
private private
FSimConnect: IFSXSimConnect; FSimConnect: IFSXSimConnect;
FVariables: TFSXSimConnectVariableList; FVariables: TFSXSimConnectVariableList;
@ -198,7 +198,6 @@ type
{ IFSXSimConnectDefinition } { IFSXSimConnectDefinition }
procedure AddVariable(AVariableName, AUnitsName: string; ADataType: SIMCONNECT_DATAType; AEpsilon: Single = 0); procedure AddVariable(AVariableName, AUnitsName: string; ADataType: SIMCONNECT_DATAType; AEpsilon: Single = 0);
{ IFSXSimConnectDefinitionAccess }
function GetVariableCount: Integer; function GetVariableCount: Integer;
function GetVariable(AIndex: Integer): IFSXSimConnectVariable; function GetVariable(AIndex: Integer): IFSXSimConnectVariable;
public public
@ -518,6 +517,7 @@ var
simObjectData: PSimConnectRecvSimObjectData; simObjectData: PSimConnectRecvSimObjectData;
eventData: PSimConnectRecvEvent; eventData: PSimConnectRecvEvent;
definitionRef: TFSXSimConnectDefinitionRef; definitionRef: TFSXSimConnectDefinitionRef;
simObjectDataByType: PSimConnectRecvSimObjectDataByType;
begin begin
Log.Verbose('Handling messages'); Log.Verbose('Handling messages');
@ -538,6 +538,18 @@ begin
end; end;
end; end;
SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE:
begin
simObjectDataByType := PSimConnectRecvSimObjectDataByType(data);
Log.Verbose(Format('Received Sim Object Data By Type message (definition = %d)', [simObjectDataByType^.dwDefineID]));
if Definitions.ContainsKey(simObjectDataByType^.dwDefineID) then
begin
definitionRef := Definitions[simObjectDataByType^.dwDefineID];
definitionRef.HandleData(@simObjectDataByType^.dwData);
end;
end;
SIMCONNECT_RECV_ID_EVENT: SIMCONNECT_RECV_ID_EVENT:
begin begin
eventData := PSimConnectRecvEvent(data); eventData := PSimConnectRecvEvent(data);
@ -608,7 +620,7 @@ begin
end; end;
procedure TFSXSimConnectClient.RegisterDefinition(ADefinitionID: Cardinal; ADefinition: IFSXSimConnectDefinitionAccess); procedure TFSXSimConnectClient.RegisterDefinition(ADefinitionID: Cardinal; ADefinition: IFSXSimConnectDefinition);
var var
variableIndex: Integer; variableIndex: Integer;
variable: IFSXSimConnectVariable; variable: IFSXSimConnectVariable;
@ -658,7 +670,7 @@ begin
end; end;
function TFSXSimConnectClient.SameDefinition(ADefinition1, ADefinition2: IFSXSimConnectDefinitionAccess): Boolean; function TFSXSimConnectClient.SameDefinition(ADefinition1, ADefinition2: IFSXSimConnectDefinition): Boolean;
var var
variableIndex: Integer; variableIndex: Integer;
variable1: IFSXSimConnectVariable; variable1: IFSXSimConnectVariable;
@ -765,13 +777,13 @@ var
addDefinition: TAddDefinitionValue; addDefinition: TAddDefinitionValue;
definitionID: Cardinal; definitionID: Cardinal;
definitionRef: TFSXSimConnectDefinitionRef; definitionRef: TFSXSimConnectDefinitionRef;
definitionAccess: IFSXSimConnectDefinitionAccess; definitionAccess: IFSXSimConnectDefinition;
hasDefinition: Boolean; hasDefinition: Boolean;
refCount: Integer; refCount: Integer;
begin begin
addDefinition := Msg.MsgData; addDefinition := Msg.MsgData;
definitionAccess := (addDefinition.Definition as IFSXSimConnectDefinitionAccess); definitionAccess := (addDefinition.Definition as IFSXSimConnectDefinition);
hasDefinition := False; hasDefinition := False;
Log.Verbose('Received request to add a definition'); Log.Verbose('Received request to add a definition');
@ -887,7 +899,7 @@ end;
{ TFSXSimConnectDefinitionRef } { TFSXSimConnectDefinitionRef }
constructor TFSXSimConnectDefinitionRef.Create(ADefinition: IFSXSimConnectDefinitionAccess); constructor TFSXSimConnectDefinitionRef.Create(ADefinition: IFSXSimConnectDefinition);
begin begin
inherited Create; inherited Create;

View File

@ -39,12 +39,6 @@ type
end; end;
IFSXSimConnectDefinition = interface
['{F1EAB3B1-0A3D-4B06-A75F-823E15C313B8}']
procedure AddVariable(AVariableName, AUnitsName: string; ADataType: SIMCONNECT_DATAType; AEpsilon: Single = 0);
end;
IFSXSimConnectVariable = interface IFSXSimConnectVariable = interface
['{A41AD003-77C0-4E34-91E3-B0BAADD08FCE}'] ['{A41AD003-77C0-4E34-91E3-B0BAADD08FCE}']
function GetVariableName: string; function GetVariableName: string;
@ -54,8 +48,10 @@ type
end; end;
IFSXSimConnectDefinitionAccess = interface IFSXSimConnectDefinition = interface
['{2592534C-0344-4442-8A5F-1AB34B96E1B5}'] ['{F1EAB3B1-0A3D-4B06-A75F-823E15C313B8}']
procedure AddVariable(AVariableName, AUnitsName: string; ADataType: SIMCONNECT_DATAType; AEpsilon: Single = 0);
function GetVariableCount: Integer; function GetVariableCount: Integer;
function GetVariable(AIndex: Integer): IFSXSimConnectVariable; function GetVariable(AIndex: Integer): IFSXSimConnectVariable;
end; end;

View File

@ -171,6 +171,7 @@ begin
Interpreter.SetGlobalVariable('LEDColor', table); Interpreter.SetGlobalVariable('LEDColor', table);
// #ToDo1 -oMvR: 29-5-2017: Logging methods
// #ToDo1 -oMvR: 28-5-2017: SetState // #ToDo1 -oMvR: 28-5-2017: SetState
end; end;