forked from ziloxaoki/iDash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFactorConnector.cs
222 lines (192 loc) · 7.56 KB
/
RFactorConnector.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
using iDash.rFactor1Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace iDash
{
class RFactorConnector : ISimConnector
{
public Boolean running = false;
private Boolean mapped = false;
private RFactorDataReader gameDataReader;
private RF1SharedMemoryReader.RF1StructWrapper wrapper;
private bool isConnected = false;
private bool isGameRunning = false;
private bool disposed = false;
private Logger logger = new Logger();
protected override void start()
{
gameDataReader = new RF1SharedMemoryReader();
Object rawGameData;
NotifyStatusMessage("Waiting for Automobilista(RFactor)...");
while (!CancellationPending)
{
if (isGameRunning)
{
if (!mapped)
{
mapped = gameDataReader.Initialise();
}
else
{
try {
rawGameData = gameDataReader.ReadGameData();
wrapper = (RF1SharedMemoryReader.RF1StructWrapper)rawGameData;
if (wrapper.data.numVehicles > 0)
{
float lastRpm = wrapper.data.engineMaxRPM;
float firstRpm = FIRST_RPM * lastRpm;
//calibrate shift gear light rpm
lastRpm *= MainForm.maxRPM;
float currentRpm = wrapper.data.engineRPM;
int flag = wrapper.data.vehicle[0].inPits > 0 ? (int)Constants.FLAG_TYPE.IN_PIT_FLAG : 0;
sendRPMShiftMsg(currentRpm, firstRpm, lastRpm, flag);
foreach (SerialManager serialManager in sm)
{
if (serialManager.deviceContains7Segments())
{
send7SegmentMsg();
}
}
if (!isConnected)
{
string s = "Connected to Automobilista(RFactor).";
logger.LogMessageToFile(s, true);
NotifyStatusMessage(s);
isConnected = true;
}
}
}
catch(Exception e)
{
logger.LogExceptionToFile(e);
isGameRunning = false;
isConnected = false;
}
}
}
else
{
isGameRunning = Utils.IsGameRunning(GameDefinition.automobilista.processName);
if (isConnected)
{
string s = "Automobilista(RFactor) closed.";
logger.LogMessageToFile(s, true);
NotifyStatusMessage(s);
isConnected = false;
}
}
if(!isConnected)
{
foreach (SerialManager serialManager in sm)
{
serialManager.enqueueCommand(Utils.getDisconnectedMsgCmd(), false);
serialManager.enqueueCommand(Utils.getBlackRPMCmd(), false);
}
}
Thread.Sleep(Constants.SharedMemoryReadRate);
}
NotifyStatusMessage("Automobilista(RFactor) thread stopped.");
}
private string getValue(string name, string type, object clazz)
{
string result = "";
Type pType = clazz.GetType();
System.Reflection.FieldInfo field = pType.GetField(name);
if (field != null)
{
try
{
switch (type)
{
case "time":
float seconds = 0;
if (field.FieldType.Name.Equals("Single"))
{
seconds = (float)field.GetValue(clazz);
}
TimeSpan interval = TimeSpan.FromSeconds(seconds);
result = interval.ToString(@"mm\.ss\.fff");
break;
case "kmh":
if (field.FieldType.Name.Equals("Single"))
{
result = ((int)Math.Floor((Single)field.GetValue(clazz) * 3.6)).ToString();
}
break;
default:
if (name.Equals("gear"))
{
int gear = (int)field.GetValue(clazz) - 1;
if (gear < 0)
{
return "R";
}
result = (gear + 1).ToString();
}
else
{
result = field.GetValue(clazz).ToString();
}
break;
}
}
catch (Exception e)
{
logger.LogExceptionToFile(e);
}
}
return result;
}
protected override string getTelemetryValue(string name, string type, string clazz)
{
string result = "";
if (!String.IsNullOrEmpty(name) && !String.IsNullOrEmpty(clazz))
{
switch (clazz)
{
case "shared":
result = getValue(name, type, wrapper.data);
break;
case "vehicle":
result = getValue(name, type, getCurrentPlayer(wrapper.data.vehicle));
break;
}
}
return result;
}
private rfVehicleInfo getCurrentPlayer(rfVehicleInfo[] vehicle)
{
foreach (rfVehicleInfo player in vehicle) {
if (player.isPlayer == 1)
return player;
}
return vehicle[0];
}
public override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected override void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (gameDataReader != null)
{
gameDataReader.Dispose();
}
}
// Release unmanaged resources.
disposed = true;
}
}
~RFactorConnector() { Dispose(false); }
}
}