-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRacingWorker.cs
More file actions
300 lines (260 loc) · 9.3 KB
/
Copy pathRacingWorker.cs
File metadata and controls
300 lines (260 loc) · 9.3 KB
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
using DualSenseSharp;
using RacingDualSense.Config;
using RacingDualSense.DSX;
using RacingDualSense.GameParsers;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
namespace RacingDualSense
{
public interface IDataSender
{
void Send(Packet data);
void Connect();
void Stop();
}
public enum InstructionTriggerMode : sbyte
{
NONE,
RESISTANCE,
VIBRATION
}
public sealed class RacingWorker
{
public struct RacingReportStruct
{
public enum ReportType : ushort
{
VERBOSEMESSAGE = 0,
NORACE = 1,
RACING = 2,
HEARTBEAT = 3,
}
public enum RacingReportType : ushort
{
// 0 = Throttle vibration message
THROTTLE_VIBRATION = 0,
// 1 = Throttle message
THROTTLE,
// 2 = Brake vibration message
BRAKE_VIBRATION,
// 3 = Brake message
BRAKE
}
public RacingReportStruct(VerboseLevel level, ReportType type, RacingReportType racingType, string msg)
{
this.verboseLevel = level;
this.type = type;
this.racingType = racingType;
this.message = msg;
}
public RacingReportStruct(ReportType type, RacingReportType racingType, string msg)
{
this.type = type;
this.racingType = racingType;
this.message = msg;
}
public RacingReportStruct(VerboseLevel level, ReportType type, string msg)
{
this.verboseLevel = level;
this.type = type;
this.message = msg;
}
public RacingReportStruct(ReportType type, string msg)
{
this.type = type;
this.message = msg;
}
public RacingReportStruct(VerboseLevel level, string msg)
{
this.verboseLevel = level;
this.type = ReportType.VERBOSEMESSAGE;
this.message = msg;
}
public RacingReportStruct(string msg)
{
this.type = ReportType.VERBOSEMESSAGE;
this.message = msg;
}
public ReportType type = 0;
public RacingReportType racingType = 0;
public string message = string.Empty;
public VerboseLevel verboseLevel = VerboseLevel.Limited;
}
internal Config.Config settings;
internal IProgress<RacingReportStruct> progressReporter;
private Parser parser;
private Func<DualSense> supplierController;
private IDataSender dataSender;
// JSON serialization options
public RacingWorker(Config.Config currentSettings, IProgress<RacingReportStruct> progressReporter, Func<DualSense> getController)
{
settings = currentSettings;
this.progressReporter = progressReporter;
supplierController = getController;
}
public void SetSettings(Config.Config currentSettings)
{
lock (this)
{
settings = currentSettings;
}
}
void SendData()
{
CsvData csvRecord = new CsvData();
Profile activeProfile = settings.ActiveProfile;
List<Instruction> instructionsList = new List<Instruction>();
ReportableInstruction reportableInstruction;
// No race = normal triggers
if (!parser.IsRaceOn())
{
reportableInstruction = parser.GetPreRaceInstructions();
reportableInstruction.RacingReportStructs.ForEach(x =>
{
if (x.verboseLevel <= settings.VerboseLevel && progressReporter != null)
{
progressReporter.Report(x);
}
});
//Send the commands to DSX
instructionsList.AddRange(reportableInstruction.Instructions);
}
else
{
reportableInstruction = parser.GetInRaceRightTriggerInstruction();
instructionsList.AddRange(reportableInstruction.Instructions);
reportableInstruction = parser.GetInRaceLeftTriggerInstruction();
instructionsList.AddRange(reportableInstruction.Instructions);
reportableInstruction = parser.GetInRaceLightbarInstruction();
instructionsList.AddRange(reportableInstruction.Instructions);
}
Packet p = new Packet
{
Instructions = [.. instructionsList]
};
dataSender.Send(p);
}
static IPEndPoint ipEndPoint = null;
static UdpClient client = null;
public struct UdpState
{
public UdpClient u;
public IPEndPoint e;
public UdpState(UdpClient u, IPEndPoint e)
{
this.u = u;
this.e = e;
}
}
private bool bRunning = false;
public void Run()
{
bRunning = true;
dataSender = (settings.DSXPort != null) ? new DsxSender(this) : new ControllerSender(this, supplierController);
try
{
dataSender.Connect();
if (settings.ActiveProfile == null)
{
if (progressReporter != null)
{
progressReporter.Report(new RacingReportStruct("No active profile selected. Exiting..."));
}
return;
}
switch (settings.ActiveProfile.GameType)
{
case GameTypes.Forza:
parser = new ForzaParser(settings);
break;
case GameTypes.Dirt:
parser = new DirtParser(settings);
break;
default:
parser = new NullParser(settings);
break;
}
ipEndPoint = new IPEndPoint(IPAddress.Loopback, settings.ActiveProfile.gameUDPPort);
client = new UdpClient(settings.ActiveProfile.gameUDPPort);
byte[] resultBuffer;
while (bRunning)
{
resultBuffer = client.Receive(ref ipEndPoint);
if (resultBuffer == null)
continue;
progressReporter.Report(new RacingReportStruct(VerboseLevel.Off, RacingReportStruct.ReportType.HEARTBEAT, 0, ""));
if (settings.VerboseLevel > VerboseLevel.Limited && progressReporter != null)
{
progressReporter.Report(new RacingReportStruct("received Message from Forza!"));
}
if (!AdjustToBufferType(resultBuffer.Length))
{
continue;
}
parser.ParsePacket(resultBuffer);
if (settings.VerboseLevel > VerboseLevel.Limited && progressReporter != null)
{
progressReporter.Report(new RacingReportStruct("Data Parsed"));
}
SendData();
}
}
catch (Exception e)
{
if (progressReporter != null)
{
progressReporter.Report(new RacingReportStruct("Application encountered an exception: " + e.Message));
}
}
finally
{
Stop();
}
}
public void Stop()
{
bRunning = false;
if (settings.VerboseLevel > VerboseLevel.Off && progressReporter != null)
{
progressReporter.Report(new RacingReportStruct($"Cleaning Up"));
}
if (client != null)
{
client.Close();
client.Dispose();
}
if(dataSender != null)
{
dataSender.Stop();
}
if (settings.VerboseLevel > VerboseLevel.Off)
{
progressReporter.Report(new RacingReportStruct($"Cleanup Finished. Exiting..."));
}
}
static bool AdjustToBufferType(int bufferLength)
{
switch (bufferLength)
{
case 232: // FM7 sled
return false;
case 264: // Dirt Rally 1
FMData.BufferOffset = 0;
return true;
case 311: // FM7 dash
FMData.BufferOffset = 0;
return true;
case 331: // FM8 dash
FMData.BufferOffset = 0;
return true;
case 324: // FH4
FMData.BufferOffset = 12;
return true;
default:
return false;
}
}
}
}