-
Notifications
You must be signed in to change notification settings - Fork 19
/
DataFeed.cs
234 lines (191 loc) · 8.64 KB
/
DataFeed.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// This file is part of iRacingSDK.
//
// Copyright 2014 Dean Netherton
// https://github.com/vipoo/iRacingSDK.Net
//
// iRacingSDK is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// iRacingSDK is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with iRacingSDK. If not, see <http://www.gnu.org/licenses/>.
using iRacingSDK.Support;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using YamlDotNet.Serialization;
namespace iRacingSDK
{
public class DataFeed
{
MemoryMappedViewAccessor accessor;
public DataFeed(MemoryMappedViewAccessor accessor)
{
this.accessor = accessor;
}
public unsafe DataSample GetNextDataSample(int requestedTickCount, bool logging)
{
var headers = accessor.AcquirePointer(ptr =>
{
var a = ReadHeader(ptr);
var b = ReadVariableHeaders(a, ptr);
return new { Header = a, VarHeaders = b };
});
if ((headers.Header.status & 1) == 0)
return DisconnectedSample();
var sessionData = ReadSessionInfo(headers.Header);
var variables = ReadVariables(headers.Header, headers.VarHeaders, requestedTickCount, logging);
var variableDescriptions = headers.VarHeaders.ToDictionary(vh => vh.name, vh => vh.desc);
if(variables != null)
variables.Descriptions = variableDescriptions;
if (sessionData == null)
return DisconnectedSample();
if (variables == null)
return null;
variables.SessionData = sessionData;
return new DataSample { Telemetry = variables, SessionData = sessionData, TelemetryDescription = variableDescriptions, IsConnected = true };
}
unsafe iRSDKHeader ReadHeader(byte* ptr)
{
return (iRSDKHeader)System.Runtime.InteropServices.Marshal.PtrToStructure(new IntPtr(ptr), typeof(iRSDKHeader));
}
static readonly int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(VarHeader));
unsafe VarHeader[] ReadVariableHeaders(iRSDKHeader header, byte* ptr)
{
var varHeaders = new VarHeader[header.numVars];
ptr += header.varHeaderOffset;
for (var i = 0; i < header.numVars; i++)
{
varHeaders[i] = (VarHeader)Marshal.PtrToStructure(new IntPtr(ptr), typeof(VarHeader));
ptr += size;
}
return varHeaders;
}
int sessionLastInfoUpdate = -2;
SessionData lastSessionInfo;
DataSample DisconnectedSample()
{
lastSessionInfo = null;
sessionLastInfoUpdate = -2;
return DataSample.YetToConnected;
}
SessionData ReadSessionInfo(iRSDKHeader header)
{
if (header.sessionInfoUpdate == sessionLastInfoUpdate)
return lastSessionInfo;
sessionLastInfoUpdate = header.sessionInfoUpdate;
Trace.WriteLine("New Session data retrieved from iRacing. {0}".F(sessionLastInfoUpdate), "DEBUG");
var t = Task.Factory.StartNew(() => {
try {
var sessionInfoData = new byte[header.sessionInfoLen];
accessor.ReadArray<byte>(header.sessionInfoOffset, sessionInfoData, 0, header.sessionInfoLen);
var sessionInfoString = System.Text.Encoding.Default.GetString(sessionInfoData);
var length = sessionInfoString.IndexOf('\0');
if (length == -1)
{
lastSessionInfo = null;
return;
}
sessionInfoString = sessionInfoString.Substring(0, sessionInfoString.IndexOf('\0'));
Trace.WriteLine(sessionInfoString, "DEBUG");
lastSessionInfo = DeserialiseSessionInfo(sessionInfoString, header.sessionInfoUpdate);
}
catch(Exception e)
{
TraceError.WriteLine(e.Message);
TraceError.WriteLine(e.StackTrace);
}
});
if (lastSessionInfo == null)
t.Wait();
return lastSessionInfo;
}
static SessionData DeserialiseSessionInfo(string sessionInfoString, int sessionInfoUpdate)
{
if (sessionInfoString.Length == 0)
return null;
try
{
sessionInfoString = sessionInfoString.Replace(": *", ": ");
var input = new StringReader(sessionInfoString);
var deserializer = new DeserializerBuilder().IgnoreUnmatchedProperties().Build();
var result = (SessionData)deserializer.Deserialize(input, typeof(SessionData));
result.Raw = sessionInfoString.Replace("\n", "\r\n");
result.InfoUpdate = sessionInfoUpdate;
return result;
}
catch (Exception e)
{
Trace.WriteLine(string.Format("Error decoding session yml data {0}", e.Message), "DEBUG");
return null;
}
}
unsafe Telemetry ReadVariables(iRSDKHeader header, VarHeader[] varHeaders, int requestedTickCount, bool logging)
{
var buf = header.FindLatestBuf(requestedTickCount);
if (buf.index == -1)
{
Trace.WriteLine("No data in iRacing buffers", "DEBUG");
return null;
}
var values = ReadAllValues(accessor, buf.bufOffset, varHeaders);
var latestHeader = accessor.AcquirePointer(ptr => ReadHeader(ptr));
if (latestHeader.HasChangedSinceReading(buf))
{
Trace.WriteLineIf(logging, "Failed to read data before iRacing overwrote new sample!", "DEBUG");
return null;
}
values.Add("TickCount", buf.tickCount);
return values;
}
static Telemetry ReadAllValues(MemoryMappedViewAccessor accessor, int buffOffset, VarHeader[] varHeaders)
{
var result = new Telemetry();
var maps = new Dictionary<VarType, Func<int, object>>() {
{ VarType.irInt, (offset) => accessor.ReadInt32(offset) },
{ VarType.irBitField, (offset) => accessor.ReadInt32(offset) },
{ VarType.irDouble, (offset) => accessor.ReadDouble(offset) },
{ VarType.irBool, (offset) => accessor.ReadBoolean(offset) },
{ VarType.irFloat, (offset) => accessor.ReadSingle(offset) }
};
var arryMaps = new Dictionary<VarType, Func<int, int, object>>() {
{ VarType.irInt, (size, offset) => GetArrayData<int>(accessor, size, offset) },
{ VarType.irBitField, (size, offset) => GetArrayData<int>(accessor, size, offset) },
{ VarType.irDouble, (size, offset) => GetArrayData<double>(accessor, size, offset) },
{ VarType.irFloat, (size, offset) => GetArrayData<float>(accessor, size, offset) },
{ VarType.irBool, (size, offset) => GetArrayData<bool>(accessor, size, offset) }
};
for (var i = 0; i < varHeaders.Length; i++)
{
var varHeader = varHeaders[i];
var offset = buffOffset + varHeader.offset;
if (varHeader.type == VarType.irChar)
throw new NotSupportedException();
object value;
if (varHeader.count != 1)
value = arryMaps[varHeader.type](varHeader.count, offset);
else
value = maps[varHeader.type](offset);
result.Add(varHeader.name, value);
}
return result;
}
static T[] GetArrayData<T>(MemoryMappedViewAccessor accessor, int size, int offset) where T : struct
{
var data = new T[size];
accessor.ReadArray<T>(offset, data, 0, size);
return data;
}
}
}