-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEventSystem.EventTrack.cs
337 lines (271 loc) · 9.61 KB
/
EventSystem.EventTrack.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
namespace IRSDKSharper
{
public partial class EventSystem
{
public abstract class EventTrack
{
public List<Event> Events { get; private set; } = new List<Event>();
protected readonly string trackName;
protected readonly IRacingSdkEnum.VarType varType;
protected readonly IRacingSdkDatum datum;
protected readonly int datumIndex;
protected bool initialized = false;
protected int dataTick = 0; // dont record any more data until this tick
protected int dataRate = 0; // how often (in seconds) to record data (0 = not capped)
protected int updatesPerSecond = 0;
private int eventIndex = 0;
public static Dictionary<string, int> TrackNamesAndDataRates { get; private set; } = new Dictionary<string, int>
{
{ "AirDensity", 60 },
{ "AirPressure", 60 },
{ "AirTemp", 60 },
{ "Brake", 1 },
{ "BrakeRaw", 1 },
{ "CarIdxRPM", 1 },
{ "ChanAvgLatency", 15 },
{ "ChanClockSkew", 15 },
{ "ChanLatency", 15 },
{ "ChanPartnerQuality", 15 },
{ "ChanQuality", 15 },
{ "Clutch", 1 },
{ "ClutchRaw", 1 },
{ "CpuUsageBG", 60 },
{ "CpuUsageFG", 60 },
{ "dcBrakeBias", 1 },
{ "Engine0_RPM", 1 },
{ "FogLevel", 60 },
{ "FrameRate", 60 },
{ "FuelLevel", 1 },
{ "FuelLevelPct", 1 },
{ "FuelPress", 1 },
{ "FuelUsePerHour", 1 },
{ "GpuUsage", 60 },
{ "HandbrakeRaw", 1 },
{ "ManifoldPress", 1 },
{ "OilLevel", 1 },
{ "OilPress", 1 },
{ "OilTemp", 1 },
{ "PlayerCarTowTime", 1 },
{ "Precipitation", 60 },
{ "RelativeHumidity", 60 },
{ "RPM", 1 },
{ "SessionTimeOfDay", 60 },
{ "SessionTimeRemain", 1 },
{ "SolarAltitude", 60 },
{ "SolarAzimuth", 60 },
{ "Throttle", 1 },
{ "ThrottleRaw", 1 },
{ "TrackTemp", 60 },
{ "TrackTempCrew", 60 },
{ "Voltage", 1 },
{ "WaterLevel", 1 },
{ "WaterTemp", 1 },
{ "WindDir", 60 },
{ "WindVel", 60 },
};
public override string ToString()
{
return trackName;
}
protected EventTrack( string trackName, IRacingSdkEnum.VarType varType )
{
this.trackName = trackName;
this.varType = varType;
}
protected EventTrack( IRacingSdkDatum datum, int index )
{
trackName = ( datum.Count <= 1 ) ? datum.Name : $"{datum.Name}[{index}]";
varType = datum.VarType;
this.datum = datum;
this.datumIndex = index;
if ( TrackNamesAndDataRates.ContainsKey( this.datum.Name ) )
{
dataRate = TrackNamesAndDataRates[ this.datum.Name ];
}
}
protected EventTrack( string trackName, object valueAsObject )
{
this.trackName = trackName;
switch ( Type.GetTypeCode( valueAsObject.GetType() ) )
{
case TypeCode.String: varType = IRacingSdkEnum.VarType.Char; break;
case TypeCode.Int32: varType = IRacingSdkEnum.VarType.Int; break;
case TypeCode.Single: varType = IRacingSdkEnum.VarType.Float; break;
default: throw new Exception( $"Unexpected type ({valueAsObject.GetType().Name})!" );
};
}
public Event GetAt( int sessionNum, double sessionTime )
{
if ( Events.Count == 0 )
{
return null;
}
else if ( Events.Count == 1 )
{
return Events[ 0 ];
}
else
{
var _event = Events[ eventIndex ];
if ( ( _event.SessionNum > sessionNum ) || ( ( _event.SessionNum == sessionNum ) && ( _event.SessionTime > sessionTime ) ) )
{
eventIndex = 0;
_event = Events[ eventIndex ];
}
var maxEventIndex = Events.Count - 1;
while ( eventIndex < maxEventIndex )
{
var nextEventIndex = eventIndex + 1;
var nextEvent = Events[ nextEventIndex ];
if ( ( nextEvent.SessionNum > sessionNum ) || ( ( nextEvent.SessionNum == sessionNum ) && ( nextEvent.SessionTime > sessionTime ) ) )
{
break;
}
_event = nextEvent;
eventIndex = nextEventIndex;
}
return _event;
}
}
public abstract Event Record( EventSystem eventSystem, IRacingSdkData data );
public abstract Event Record( EventSystem eventSystem, object newValueAsObject );
public abstract void Load( int sessionNum, double sessionTime, BinaryReader binaryReader );
}
public class EventTrack<T> : EventTrack
{
private T value = default;
public EventTrack( string trackName, IRacingSdkEnum.VarType varType ) : base( trackName, varType )
{
value = (T) GetDefaultValueAsObject();
}
public EventTrack( IRacingSdkDatum datum, int index ) : base( datum, index )
{
value = (T) GetDefaultValueAsObject();
}
public EventTrack( string trackName, object valueAsObject ) : base( trackName, valueAsObject )
{
value = (T) GetDefaultValueAsObject();
}
private object GetDefaultValueAsObject()
{
object defaultValue;
switch ( varType )
{
case IRacingSdkEnum.VarType.Char: defaultValue = string.Empty; break;
case IRacingSdkEnum.VarType.Bool: defaultValue = false; break;
case IRacingSdkEnum.VarType.Int: defaultValue = 0; break;
case IRacingSdkEnum.VarType.BitField: defaultValue = (uint) 0; break;
case IRacingSdkEnum.VarType.Float: defaultValue = 0.0f; break;
case IRacingSdkEnum.VarType.Double: defaultValue = 0.0; break;
default: throw new Exception( $"Unexpected type ({varType})!" );
};
return defaultValue;
}
public override Event Record( EventSystem eventSystem, IRacingSdkData data )
{
Event _event = null;
if ( datum != null )
{
if ( !initialized || ( dataRate == 0 ) || ( eventSystem.sessionTick >= dataTick ) )
{
var newValueAsObject = data.GetValue( datum, datumIndex );
var newValueAsT = (T) newValueAsObject;
if ( !initialized || !value.Equals( newValueAsT ) )
{
eventSystem.RecordFrameHeader();
var trackNameId = eventSystem.GetTrackNameId( trackName, varType );
#pragma warning disable CS8602
eventSystem.binaryWriter.Write( trackNameId );
switch ( varType )
{
case IRacingSdkEnum.VarType.Char: eventSystem.binaryWriter.Write( (string) newValueAsObject ); break;
case IRacingSdkEnum.VarType.Bool: eventSystem.binaryWriter.Write( (bool) newValueAsObject ); break;
case IRacingSdkEnum.VarType.Int: eventSystem.binaryWriter.Write( (int) newValueAsObject ); break;
case IRacingSdkEnum.VarType.BitField: eventSystem.binaryWriter.Write( (uint) newValueAsObject ); break;
case IRacingSdkEnum.VarType.Float: eventSystem.binaryWriter.Write( (float) newValueAsObject ); break;
case IRacingSdkEnum.VarType.Double: eventSystem.binaryWriter.Write( (double) newValueAsObject ); break;
}
#pragma warning restore CS8602
_event = new Event<T>( eventSystem.sessionNum, eventSystem.sessionTime, newValueAsT, datum );
Events.Add( _event );
initialized = true;
value = newValueAsT;
if ( dataRate == 0 )
{
updatesPerSecond++;
}
else
{
dataTick = EventTrack<T>.GetNextDataTick( eventSystem.sessionTick, data.TickRate, dataRate );
}
}
}
if ( ( dataRate == 0 ) && ( updatesPerSecond > 0 ) )
{
if ( eventSystem.sessionTick >= dataTick )
{
updatesPerSecond--;
dataTick = eventSystem.sessionTick + data.TickRate / 5;
}
if ( updatesPerSecond >= 5 )
{
dataRate = 1;
dataTick = EventTrack<T>.GetNextDataTick( eventSystem.sessionTick, data.TickRate, dataRate );
updatesPerSecond = 0;
}
}
}
return _event;
}
public override Event Record( EventSystem eventSystem, object newValueAsObject )
{
Event _event = null;
var newValueAsT = (T) newValueAsObject;
if ( !initialized || !value.Equals( newValueAsT ) )
{
eventSystem.RecordFrameHeader();
var trackNameId = eventSystem.GetTrackNameId( trackName, varType );
#pragma warning disable CS8602
eventSystem.binaryWriter.Write( trackNameId );
switch ( varType )
{
case IRacingSdkEnum.VarType.Char: eventSystem.binaryWriter.Write( (string) newValueAsObject ); break;
case IRacingSdkEnum.VarType.Int: eventSystem.binaryWriter.Write( (int) newValueAsObject ); break;
case IRacingSdkEnum.VarType.Float: eventSystem.binaryWriter.Write( (float) newValueAsObject ); break;
}
#pragma warning restore CS8602
_event = new Event<T>( eventSystem.sessionNum, eventSystem.sessionTime, newValueAsT, datum );
Events.Add( _event );
initialized = true;
value = newValueAsT;
}
return _event;
}
public override void Load( int sessionNum, double sessionTime, BinaryReader binaryReader )
{
object newValueAsObject;
switch ( varType )
{
case IRacingSdkEnum.VarType.Char: newValueAsObject = binaryReader.ReadString(); break;
case IRacingSdkEnum.VarType.Bool: newValueAsObject = binaryReader.ReadBoolean(); break;
case IRacingSdkEnum.VarType.Int: newValueAsObject = binaryReader.ReadInt32(); break;
case IRacingSdkEnum.VarType.BitField: newValueAsObject = binaryReader.ReadUInt32(); break;
case IRacingSdkEnum.VarType.Float: newValueAsObject = binaryReader.ReadSingle(); break;
case IRacingSdkEnum.VarType.Double: newValueAsObject = binaryReader.ReadDouble(); break;
default: throw new Exception( $"Unexpected type ({varType})!" );
};
Events.Add( new Event<T>( sessionNum, sessionTime, (T) newValueAsObject, datum ) );
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
private static int GetNextDataTick( int sessionTick, int tickRate, int dataRate )
{
return ( sessionTick + ( dataRate * tickRate ) ) / dataRate * dataRate;
}
}
}
}