@irsdk-node/native 4.1.1 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/irsdk_node.cc CHANGED
@@ -1,36 +1,44 @@
1
- #include "./irsdk_node.h"
2
- #include "./yaml_parser.h"
3
1
  #include "./irsdk_defines.h"
4
- #include "./irsdk_client.h"
2
+ #include "./irsdk_node.h"
5
3
 
6
4
  // ---------------------------
7
5
  // Constructors
8
6
  // ---------------------------
9
7
  Napi::Object iRacingSdkNode::Init(Napi::Env env, Napi::Object exports)
10
8
  {
11
- Napi::Function func = DefineClass(env, "iRacingSdkNode", {// Properties
12
- InstanceAccessor<&iRacingSdkNode::GetCurrSessionDataVersion>("currDataVersion"), InstanceAccessor<&iRacingSdkNode::GetEnableLogging, &iRacingSdkNode::SetEnableLogging>("enableLogging"), InstanceAccessor<&iRacingSdkNode::GetIsMocked>("isMocked"),
13
-
14
- // Methods
15
- // Control
16
- InstanceMethod<&iRacingSdkNode::StartSdk>("startSDK"), InstanceMethod("stopSDK", &iRacingSdkNode::StopSdk), InstanceMethod("waitForData", &iRacingSdkNode::WaitForData), InstanceMethod("broadcast", &iRacingSdkNode::BroadcastMessage),
17
- // Getters
18
- InstanceMethod("isRunning", &iRacingSdkNode::IsRunning), InstanceMethod("getSessionVersionNum", &iRacingSdkNode::GetSessionVersionNum), InstanceMethod("getSessionData", &iRacingSdkNode::GetSessionData), InstanceMethod("getTelemetryData", &iRacingSdkNode::GetTelemetryData), InstanceMethod("getTelemetryVariable", &iRacingSdkNode::GetTelemetryVar),
19
- // Helpers
20
- InstanceMethod("__getTelemetryTypes", &iRacingSdkNode::__GetTelemetryTypes)});
21
-
22
- Napi::FunctionReference *constructor = new Napi::FunctionReference();
23
- *constructor = Napi::Persistent(func);
24
- env.SetInstanceData(constructor);
25
-
26
- exports.Set("iRacingSdkNode", func);
27
- return exports;
9
+ Napi::Function func = DefineClass(env, "iRacingSdkNode", {
10
+ // Properties
11
+ InstanceAccessor<&iRacingSdkNode::GetCurrSessionDataVersion>("currDataVersion"),
12
+ InstanceAccessor<&iRacingSdkNode::GetEnableLogging, &iRacingSdkNode::SetEnableLogging>("enableLogging"),
13
+ InstanceAccessor<&iRacingSdkNode::GetIsMocked>("isMocked"),
14
+
15
+ // Methods
16
+ // Control
17
+ InstanceMethod<&iRacingSdkNode::StartSdk>("startSDK"),
18
+ InstanceMethod("stopSDK", &iRacingSdkNode::StopSdk),
19
+ InstanceMethod("waitForData", &iRacingSdkNode::WaitForData),
20
+ InstanceMethod("broadcast", &iRacingSdkNode::BroadcastMessage),
21
+ // Getters
22
+ InstanceMethod("isRunning", &iRacingSdkNode::IsRunning),
23
+ InstanceMethod("getSessionVersionNum", &iRacingSdkNode::GetSessionVersionNum),
24
+ InstanceMethod("getSessionData", &iRacingSdkNode::GetSessionData),
25
+ InstanceMethod("getTelemetryData", &iRacingSdkNode::GetTelemetryData),
26
+ InstanceMethod("getTelemetryVariable", &iRacingSdkNode::GetTelemetryVar),
27
+ // Helpers
28
+ InstanceMethod("__getTelemetryTypes", &iRacingSdkNode::__GetTelemetryTypes)});
29
+
30
+ Napi::FunctionReference *constructor = new Napi::FunctionReference();
31
+ *constructor = Napi::Persistent(func);
32
+ env.SetInstanceData(constructor);
33
+
34
+ exports.Set("iRacingSdkNode", func);
35
+ return exports;
28
36
  }
29
37
 
30
38
  iRacingSdkNode::iRacingSdkNode(const Napi::CallbackInfo &info)
31
- : Napi::ObjectWrap<iRacingSdkNode>(info), _data(NULL), _bufLineLen(0), _sessionStatusID(0), _lastSessionCt(-1), _sessionData(NULL), _loggingEnabled(false)
39
+ : Napi::ObjectWrap<iRacingSdkNode>(info), _data(NULL), _bufLineLen(0), _sessionStatusID(0), _lastSessionCt(-1), _sessionData(NULL), _loggingEnabled(false)
32
40
  {
33
- printf("Initializing iRacingSdkNode\n");
41
+ printf("Initializing iRacingSdkNode\n");
34
42
  }
35
43
 
36
44
  // ---------------------------
@@ -38,35 +46,34 @@ iRacingSdkNode::iRacingSdkNode(const Napi::CallbackInfo &info)
38
46
  // ---------------------------
39
47
  Napi::Value iRacingSdkNode::GetCurrSessionDataVersion(const Napi::CallbackInfo &info)
40
48
  {
41
- int ver = this->_lastSessionCt;
42
- return Napi::Number::New(info.Env(), ver);
49
+ int ver = this->_lastSessionCt;
50
+ return Napi::Number::New(info.Env(), ver);
43
51
  }
44
52
 
45
53
  Napi::Value iRacingSdkNode::GetEnableLogging(const Napi::CallbackInfo &info)
46
54
  {
47
- bool enabled = this->_loggingEnabled;
48
- return Napi::Boolean::New(info.Env(), enabled);
55
+ bool enabled = this->_loggingEnabled;
56
+ return Napi::Boolean::New(info.Env(), enabled);
49
57
  }
50
58
 
51
59
  void iRacingSdkNode::SetEnableLogging(const Napi::CallbackInfo &info, const Napi::Value &value)
52
60
  {
53
- Napi::Boolean enable;
54
- if (info.Length() <= 0 || !info[0].IsBoolean())
55
- {
56
- enable = Napi::Boolean::New(info.Env(), false);
57
- }
58
- else
59
- {
60
- enable = info[0].As<Napi::Boolean>();
61
- }
62
- this->_loggingEnabled = enable;
63
- if (this->_loggingEnabled)
64
- printf("iRacingSDK Logging enabled\n");
61
+ Napi::Boolean enable;
62
+ if (info.Length() <= 0 || !info[0].IsBoolean())
63
+ {
64
+ enable = Napi::Boolean::New(info.Env(), false);
65
+ } else
66
+ {
67
+ enable = info[0].As<Napi::Boolean>();
68
+ }
69
+ this->_loggingEnabled = enable;
70
+ if (this->_loggingEnabled)
71
+ printf("iRacingSDK Logging enabled\n");
65
72
  }
66
73
 
67
74
  Napi::Value iRacingSdkNode::GetIsMocked(const Napi::CallbackInfo &info)
68
75
  {
69
- return Napi::Boolean::New(info.Env(), false);
76
+ return Napi::Boolean::New(info.Env(), false);
70
77
  }
71
78
 
72
79
  // ---------------------------
@@ -75,260 +82,306 @@ Napi::Value iRacingSdkNode::GetIsMocked(const Napi::CallbackInfo &info)
75
82
  // SDK Control
76
83
  Napi::Value iRacingSdkNode::StartSdk(const Napi::CallbackInfo &info)
77
84
  {
78
- if (this->_loggingEnabled)
79
- printf("Starting SDK...\n");
80
-
81
- if (!irsdk_isConnected())
82
- {
83
- bool result = irsdk_startup();
84
- if (this->_loggingEnabled)
85
- printf("Connected to iRacing SDK (%i)\n", result);
86
- return Napi::Boolean::New(info.Env(), result);
87
- }
88
- return Napi::Boolean::New(info.Env(), true);
85
+ if (this->_loggingEnabled)
86
+ printf("Starting SDK...\n");
87
+
88
+ if (!irsdk_isConnected())
89
+ {
90
+ bool result = irsdk_startup();
91
+ if (this->_loggingEnabled)
92
+ printf("Connected to iRacing SDK (%i)\n", result);
93
+ return Napi::Boolean::New(info.Env(), result);
94
+ }
95
+ return Napi::Boolean::New(info.Env(), true);
89
96
  }
90
97
 
91
98
  Napi::Value iRacingSdkNode::StopSdk(const Napi::CallbackInfo &info)
92
99
  {
93
- irsdk_shutdown();
94
- return Napi::Boolean::New(info.Env(), true);
100
+ irsdk_shutdown();
101
+ return Napi::Boolean::New(info.Env(), true);
95
102
  }
96
103
 
97
104
  Napi::Value iRacingSdkNode::WaitForData(const Napi::CallbackInfo &info)
98
105
  {
99
106
  // Figure out the time to wait
100
107
  // This will default to the timeout set on the class
101
- Napi::Number timeout;
102
- if (info.Length() <= 0 || !info[0].IsNumber())
103
- {
104
- timeout = Napi::Number::New(info.Env(), 16);
105
- }
106
- else
107
- {
108
- timeout = info[0].As<Napi::Number>();
109
- }
110
-
111
- if (!irsdk_isConnected() && !irsdk_startup())
112
- {
113
- return Napi::Boolean::New(info.Env(), false);
114
- }
115
-
116
- // @todo: try to do this async instead
117
- const irsdk_header *header = irsdk_getHeader();
118
-
119
- // @todo: This isn't the best way of doing this. Need to improve, but this works for now
120
- if (!this->_data)
121
- {
122
- this->_data = new char[header->bufLen];
123
- }
124
-
125
- // wait for start of sesh or new data
126
- bool dataReady = irsdk_waitForDataReady(timeout, this->_data);
127
- if (dataReady && header)
128
- {
129
- if (this->_loggingEnabled)
130
- ("Session started or we have new data.\n");
131
-
132
- // New connection or data changed length
133
- if (this->_bufLineLen != header->bufLen)
134
- {
135
- if (this->_loggingEnabled)
136
- printf("Connection started / data changed length.\n");
137
-
138
- this->_bufLineLen = header->bufLen;
139
-
140
- // Increment connection
141
- this->_sessionStatusID++;
142
-
143
- // Reset info str status
144
- this->_lastSessionCt = -1;
145
- return Napi::Boolean::New(info.Env(), true);
146
- }
147
- else if (this->_data)
148
- {
149
- if (this->_loggingEnabled)
150
- printf("Data initialized and ready to process.\n");
151
- // already initialized and ready to process
152
- return Napi::Boolean::New(info.Env(), true);
153
- }
154
- }
155
- else if (!(this->_data != NULL && irsdk_isConnected()))
156
- {
157
- if (this->_loggingEnabled)
158
- printf("Session ended. Cleaning up.\n");
159
- // Session ended
160
- if (this->_data)
161
- delete[] this->_data;
162
- this->_data = NULL;
163
-
164
- // Reset Info str
165
- this->_lastSessionCt = -1;
166
- }
167
- if (this->_loggingEnabled)
168
- printf("Session ended or something went wrong. Not successful.\n");
169
- return Napi::Boolean::New(info.Env(), false);
108
+ Napi::Number timeout;
109
+ if (info.Length() <= 0 || !info[0].IsNumber())
110
+ {
111
+ timeout = Napi::Number::New(info.Env(), 16);
112
+ } else
113
+ {
114
+ timeout = info[0].As<Napi::Number>();
115
+ }
116
+
117
+ if (!irsdk_isConnected() && !irsdk_startup())
118
+ {
119
+ return Napi::Boolean::New(info.Env(), false);
120
+ }
121
+
122
+ // @todo: try to do this async instead
123
+ const irsdk_header *header = irsdk_getHeader();
124
+
125
+ // @todo: This isn't the best way of doing this. Need to improve, but this works for now
126
+ if (!this->_data)
127
+ {
128
+ this->_data = new char[header->bufLen];
129
+ }
130
+
131
+ // wait for start of sesh or new data
132
+ bool dataReady = irsdk_waitForDataReady(timeout, this->_data);
133
+ if (dataReady && header)
134
+ {
135
+ if (this->_loggingEnabled)
136
+ ("Session started or we have new data.\n");
137
+
138
+ // New connection or data changed length
139
+ if (this->_bufLineLen != header->bufLen)
140
+ {
141
+ if (this->_loggingEnabled)
142
+ printf("Connection started / data changed length.\n");
143
+
144
+ this->_bufLineLen = header->bufLen;
145
+
146
+ // Increment connection
147
+ this->_sessionStatusID++;
148
+
149
+ // Reset info str status
150
+ this->_lastSessionCt = -1;
151
+ return Napi::Boolean::New(info.Env(), true);
152
+ } else if (this->_data)
153
+ {
154
+ if (this->_loggingEnabled)
155
+ printf("Data initialized and ready to process.\n");
156
+ // already initialized and ready to process
157
+ return Napi::Boolean::New(info.Env(), true);
158
+ }
159
+ } else if (!(this->_data != NULL && irsdk_isConnected()))
160
+ {
161
+ if (this->_loggingEnabled)
162
+ printf("Session ended. Cleaning up.\n");
163
+ // Session ended
164
+ if (this->_data)
165
+ delete[] this->_data;
166
+ this->_data = NULL;
167
+
168
+ // Reset Info str
169
+ this->_lastSessionCt = -1;
170
+ }
171
+ if (this->_loggingEnabled)
172
+ printf("Session ended or something went wrong. Not successful.\n");
173
+ return Napi::Boolean::New(info.Env(), false);
170
174
  }
171
175
 
172
176
  Napi::Value iRacingSdkNode::BroadcastMessage(const Napi::CallbackInfo &info)
173
177
  {
174
- auto env = info.Env();
175
-
176
- // Determine message type
177
- if (info.Length() <= 2 || !info[0].IsNumber())
178
- {
179
- return Napi::Boolean::New(env, false);
180
- }
181
-
182
- if (info.Length() == 4 && !info[2].IsNumber())
183
- {
184
- return Napi::Boolean::New(env, false);
185
- }
186
-
187
- int msgEnumIndex = info[0].As<Napi::Number>();
188
- irsdk_BroadcastMsg msgType = static_cast<irsdk_BroadcastMsg>(msgEnumIndex);
189
-
190
- // Args
191
- int arg1 = info[1].As<Napi::Number>();
192
- auto arg2 = info[2].As<Napi::Number>();
193
- auto arg3 = info[3].As<Napi::Number>();
194
-
195
- // these defs are in irsdk_defines.cpp
196
- switch (msgType)
197
- {
198
- // irsdk_BroadcastMsg msg, int arg1, int arg2, int var3
199
- case irsdk_BroadcastCamSwitchPos: // @todo we need to use irsdk_padCarNum for arg1
200
- case irsdk_BroadcastCamSwitchNum:
201
- if (this->_loggingEnabled)
202
- printf("BroadcastMessage(msgType: %d, arg1: %d, arg2: %d, arg3: %d)\n", msgType, arg1, arg2.Int32Value(), arg3.Int32Value());
203
- irsdk_broadcastMsg(msgType, arg1, arg2, arg3);
204
- break;
205
-
206
- // irsdk_BroadcastMsg msg, int arg1, int unused, int unused
207
- case irsdk_BroadcastReplaySearch: // arg1 == irsdk_RpySrchMode
208
- case irsdk_BroadcastReplaySetState: // arg1 == irsdk_RpyStateMode
209
- case irsdk_BroadcastCamSetState: // arg1 == irsdk_CameraState
210
- case irsdk_BroadcastTelemCommand: // arg1 == irsdk_TelemCommandMode
211
- case irsdk_BroadcastVideoCapture: // arg1 == irsdk_VideoCaptureMode
212
- if (this->_loggingEnabled)
213
- printf("BroadcastMessage(msgType: %d, arg1: %d, arg2: -1, arg3: -1)\n", msgType, arg1);
214
- irsdk_broadcastMsg(msgType, arg1, -1, -1);
215
- break;
216
-
217
- // irsdk_BroadcastMsg msg, int arg1, int arg2, int unused
218
- case irsdk_BroadcastReloadTextures: // arg1 == irsdk_ReloadTexturesMode
219
- case irsdk_BroadcastChatComand: // arg1 == irsdk_ChatCommandMode
220
- case irsdk_BroadcastReplaySetPlaySpeed:
221
- if (this->_loggingEnabled)
222
- printf("BroadcastMessage(msgType: %d, arg1: %d, arg2: %d, arg3: -1)\n", msgType, arg1, arg2.Int32Value());
223
- irsdk_broadcastMsg(msgType, arg1, arg2, -1);
224
- break;
225
-
226
- // irsdk_BroadcastMsg msg, int arg1, float arg2
227
- case irsdk_BroadcastPitCommand: // arg1 == irsdk_PitCommandMode
228
- case irsdk_BroadcastFFBCommand: // arg1 == irsdk_FFBCommandMode
229
- case irsdk_BroadcastReplaySearchSessionTime:
230
- case irskd_BroadcastReplaySetPlayPosition:
231
- if (this->_loggingEnabled)
232
- printf("BroadcastMessage(msgType: %d, arg1: %d, arg2: %f)\n", msgType, arg1, (float)arg2.FloatValue());
233
- irsdk_broadcastMsg(msgType, arg1, (float)arg2.FloatValue());
234
- break;
235
-
236
- default:
237
- if (this->_loggingEnabled)
238
- printf("Attempted to broadcast an unsupported message.\n");
239
- return Napi::Boolean::New(env, false);
240
- }
241
-
242
- return Napi::Boolean::New(env, true);
178
+ auto env = info.Env();
179
+
180
+ // Determine message type
181
+ if (info.Length() <= 2 || !info[0].IsNumber())
182
+ {
183
+ return Napi::Boolean::New(env, false);
184
+ }
185
+
186
+ if (info.Length() == 4 && !info[2].IsNumber())
187
+ {
188
+ return Napi::Boolean::New(env, false);
189
+ }
190
+
191
+ Napi::Number msgEnumIndex = info[0].As<Napi::Number>();
192
+ irsdk_BroadcastMsg msgType = static_cast<irsdk_BroadcastMsg>(msgEnumIndex.Int32Value());
193
+
194
+ // Args
195
+ Napi::Number arg1 = info[1].As<Napi::Number>();
196
+ Napi::Number arg2 = info[2].As<Napi::Number>();
197
+ Napi::Number arg3 = info[3].As<Napi::Number>();
198
+
199
+ irsdk_ChatCommandMode chatCommand = irsdk_ChatCommand_Cancel;
200
+
201
+
202
+ // Handle each message independently.
203
+ //
204
+ // This could be consolidated, but then it becomes way too difficult to easily
205
+ // grep what each messages expected API is, as some are not necessarily what
206
+ switch (msgType)
207
+ {
208
+ // BroadcastCamSwitchPos: car position, group, camera
209
+ // BroadcastCamSwitchNum: driver #, group, camera
210
+ case irsdk_BroadcastCamSwitchPos:
211
+ case irsdk_BroadcastCamSwitchNum:
212
+ // First arg can be irsdk_csMode enum.
213
+ // Any value above -1 equates to focusing on a specific driver.
214
+ irsdk_broadcastMsg(msgType, arg1.Int32Value(), arg2.Int32Value(), arg3.Int32Value());
215
+ break;
216
+
217
+ // irsdk_CameraState, unused, unused
218
+ case irsdk_BroadcastCamSetState:
219
+ irsdk_broadcastMsg(msgType, static_cast<irsdk_CameraState>(arg1.Int32Value()), 0);
220
+ break;
221
+
222
+ // speed, slowMotion, unused
223
+ case irsdk_BroadcastReplaySetPlaySpeed:
224
+ // Speed (arg1) should be multiples of 2 (0, 1, 2, 4, 8, 16), can be negative
225
+ // Slow mo (arg2) should be 1 | 0
226
+ irsdk_broadcastMsg(msgType, arg1.Int32Value(), arg2.ToBoolean().Value());
227
+ break;
228
+
229
+ // irsdk_RpyPosMode, Frame Number (high, low)
230
+ case irsdk_BroadcastReplaySetPlayPosition:
231
+ irsdk_broadcastMsg(msgType, static_cast<irsdk_RpyPosMode>(arg1.Int32Value()), arg2.Int32Value());
232
+ break;
233
+
234
+ // irsdk_RpySrchMode, unused, unused
235
+ case irsdk_BroadcastReplaySearch:
236
+ irsdk_broadcastMsg(msgType, static_cast<irsdk_RpySrchMode>(arg1.Int32Value()), 0, 0);
237
+ break;
238
+
239
+ // irsdk_RpyStateMode, unused, unused
240
+ case irsdk_BroadcastReplaySetState:
241
+ irsdk_broadcastMsg(msgType, static_cast<irsdk_RpyStateMode>(arg1.Int32Value()), 0);
242
+ break;
243
+
244
+ // irsdk_ReloadTexturesMode, carIdx, unused
245
+ case irsdk_BroadcastReloadTextures:
246
+ irsdk_broadcastMsg(msgType, static_cast<irsdk_ReloadTexturesMode>(arg1.Int32Value()), arg2.Int32Value(), 0);
247
+ break;
248
+
249
+ // irsdk_ChatCommandMode, subCommand, unused
250
+ case irsdk_BroadcastChatComand:
251
+ chatCommand = static_cast<irsdk_ChatCommandMode>(arg1.Int32Value());
252
+ if (chatCommand != irsdk_ChatCommand_Macro)
253
+ {
254
+ irsdk_broadcastMsg(msgType, chatCommand, 0);
255
+ break;
256
+
257
+ }
258
+
259
+ // If the chat command is to use a macro, parse the macro id (1 - 15) (2nd arg)
260
+ irsdk_broadcastMsg(msgType, chatCommand, arg2.Int32Value(), 0);
261
+ break;
262
+
263
+ // irsdk_PitCommandMode, parameter
264
+ case irsdk_BroadcastPitCommand:
265
+ irsdk_broadcastMsg(msgType, static_cast<irsdk_PitCommandMode>(arg1.Int32Value()), arg2.Int32Value());
266
+ break;
267
+
268
+ // irsdk_TelemCommandMode, unused, unused
269
+ case irsdk_BroadcastTelemCommand:
270
+ irsdk_broadcastMsg(msgType, static_cast<irsdk_TelemCommandMode>(arg1.Int32Value()), 0, 0);
271
+ break;
272
+
273
+ // irsdk_FFBCommandMode, value (float, high, low)
274
+ case irsdk_BroadcastFFBCommand:
275
+ irsdk_broadcastMsg(msgType, static_cast<irsdk_FFBCommandMode>(arg1.Int32Value()), arg2.FloatValue());
276
+ break;
277
+
278
+ // This does a search and not a direct jump, so it may take a while
279
+ // sessionNum, sessionTimeMS (high, low)
280
+ case irsdk_BroadcastReplaySearchSessionTime:
281
+ irsdk_broadcastMsg(msgType, arg1.Int32Value(), arg2.Int32Value());
282
+ break;
283
+
284
+ // irsdk_VideoCaptureMode, unused, unused
285
+ case irsdk_BroadcastVideoCapture:
286
+ irsdk_broadcastMsg(msgType, static_cast<irsdk_VideoCaptureMode>(arg1.Int32Value()), 0);
287
+ break;
288
+
289
+ // Unused + out-of-bounds
290
+ default:
291
+ if (this->_loggingEnabled)
292
+ printf("Attempted to broadcast an unsupported message.\n");
293
+ return Napi::Boolean::New(env, false);
294
+ }
295
+
296
+ return Napi::Boolean::New(env, true);
243
297
  }
244
298
 
245
299
  // SDK State Getters
246
300
  Napi::Value iRacingSdkNode::IsRunning(const Napi::CallbackInfo &info)
247
301
  {
248
- bool result = irsdk_isConnected();
249
- return Napi::Boolean::New(info.Env(), result);
302
+ bool result = irsdk_isConnected();
303
+ return Napi::Boolean::New(info.Env(), result);
250
304
  }
251
305
 
252
306
  Napi::Value iRacingSdkNode::GetSessionVersionNum(const Napi::CallbackInfo &info)
253
307
  {
254
- int sessVer = irsdk_getSessionInfoStrUpdate();
255
- return Napi::Number::New(info.Env(), sessVer);
308
+ int sessVer = irsdk_getSessionInfoStrUpdate();
309
+ return Napi::Number::New(info.Env(), sessVer);
256
310
  }
257
311
 
258
312
  Napi::Value iRacingSdkNode::GetSessionData(const Napi::CallbackInfo &info)
259
313
  {
260
- int latestUpdate = irsdk_getSessionInfoStrUpdate();
261
- if (this->_lastSessionCt != latestUpdate)
262
- {
263
- if (this->_loggingEnabled)
264
- printf("Session data has been updated (prev: %d, new: %d)\n", this->_lastSessionCt, latestUpdate);
265
- this->_lastSessionCt = latestUpdate;
266
- this->_sessionData = irsdk_getSessionInfoStr();
267
- }
268
- const char *session = this->_sessionData;
269
- if (session == NULL)
270
- {
271
- return Napi::String::New(info.Env(), "");
272
- }
273
- return Napi::String::New(info.Env(), session);
314
+ int latestUpdate = irsdk_getSessionInfoStrUpdate();
315
+ if (this->_lastSessionCt != latestUpdate)
316
+ {
317
+ if (this->_loggingEnabled)
318
+ printf("Session data has been updated (prev: %d, new: %d)\n", this->_lastSessionCt, latestUpdate);
319
+ this->_lastSessionCt = latestUpdate;
320
+ this->_sessionData = irsdk_getSessionInfoStr();
321
+ }
322
+ const char *session = this->_sessionData;
323
+ if (session == NULL)
324
+ {
325
+ return Napi::String::New(info.Env(), "");
326
+ }
327
+ return Napi::String::New(info.Env(), session);
274
328
  }
275
329
 
276
330
  Napi::Value iRacingSdkNode::GetTelemetryVar(const Napi::CallbackInfo &info)
277
331
  {
278
- Napi::Env env = info.Env();
279
-
280
- int varIndex;
281
- if (info.Length() <= 0)
282
- {
283
- varIndex = 0;
284
- }
285
- else if (!info[0].IsNumber())
286
- {
287
- if (info[0].IsString())
288
- {
289
- const char *name = info[0].As<Napi::String>().Utf8Value().c_str();
290
- return this->GetTelemetryVar(env, name);
291
- }
292
- varIndex = 0;
293
- }
294
-
295
- return this->GetTelemetryVarByIndex(env, varIndex);
332
+ Napi::Env env = info.Env();
333
+
334
+ int varIndex;
335
+ if (info.Length() <= 0)
336
+ {
337
+ varIndex = 0;
338
+ } else if (!info[0].IsNumber())
339
+ {
340
+ if (info[0].IsString())
341
+ {
342
+ const char *name = info[0].As<Napi::String>().Utf8Value().c_str();
343
+ return this->GetTelemetryVar(env, name);
344
+ }
345
+ varIndex = 0;
346
+ }
347
+
348
+ return this->GetTelemetryVarByIndex(env, varIndex);
296
349
  }
297
350
 
298
351
  Napi::Value iRacingSdkNode::GetTelemetryData(const Napi::CallbackInfo &info)
299
352
  {
300
- const irsdk_header *header = irsdk_getHeader();
301
- auto env = info.Env();
302
- auto telemVars = Napi::Object::New(env);
303
-
304
- int count = header->numVars;
305
- for (int i = 0; i < count; i++)
306
- {
307
- auto telemVariable = this->GetTelemetryVarByIndex(env, i);
308
- if (telemVariable.IsObject() && telemVariable.Has("name"))
309
- {
310
- telemVars.Set(telemVariable.Get("name"), telemVariable);
311
- }
312
- }
313
-
314
- return telemVars;
353
+ const irsdk_header *header = irsdk_getHeader();
354
+ auto env = info.Env();
355
+ auto telemVars = Napi::Object::New(env);
356
+
357
+ int count = header->numVars;
358
+ for (int i = 0; i < count; i++)
359
+ {
360
+ auto telemVariable = this->GetTelemetryVarByIndex(env, i);
361
+ if (telemVariable.IsObject() && telemVariable.Has("name"))
362
+ {
363
+ telemVars.Set(telemVariable.Get("name"), telemVariable);
364
+ }
365
+ }
366
+
367
+ return telemVars;
315
368
  }
316
369
 
317
370
  // Helpers
318
371
  Napi::Value iRacingSdkNode::__GetTelemetryTypes(const Napi::CallbackInfo &info)
319
372
  {
320
- auto env = info.Env();
321
- auto result = Napi::Object::New(env);
322
-
323
- const int count = irsdk_getHeader()->numVars;
324
- const irsdk_varHeader *varHeader;
325
- for (int i = 0; i < count; i++)
326
- {
327
- varHeader = irsdk_getVarHeaderEntry(i);
328
- result.Set(varHeader->name, Napi::Number::New(env, varHeader->type));
329
- }
330
-
331
- return result;
373
+ auto env = info.Env();
374
+ auto result = Napi::Object::New(env);
375
+
376
+ const int count = irsdk_getHeader()->numVars;
377
+ const irsdk_varHeader *varHeader;
378
+ for (int i = 0; i < count; i++)
379
+ {
380
+ varHeader = irsdk_getVarHeaderEntry(i);
381
+ result.Set(varHeader->name, Napi::Number::New(env, varHeader->type));
382
+ }
383
+
384
+ return result;
332
385
  }
333
386
 
334
387
  // ---------------------------
@@ -336,56 +389,56 @@ Napi::Value iRacingSdkNode::__GetTelemetryTypes(const Napi::CallbackInfo &info)
336
389
  // ---------------------------
337
390
  bool iRacingSdkNode::GetTelemetryBool(int entry, int index)
338
391
  {
339
- const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
340
- return *(reinterpret_cast<bool const *>(_data + headerVar->offset) + index);
392
+ const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
393
+ return *(reinterpret_cast<bool const *>(_data + headerVar->offset) + index);
341
394
  }
342
395
 
343
396
  int iRacingSdkNode::GetTelemetryInt(int entry, int index)
344
397
  {
345
398
  // Each int is 4 bytes
346
- const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
347
- return *(reinterpret_cast<int const *>(_data + headerVar->offset) + index * 4);
399
+ const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
400
+ return *(reinterpret_cast<int const *>(_data + headerVar->offset) + index * 4);
348
401
  }
349
402
 
350
403
  float iRacingSdkNode::GetTelemetryFloat(int entry, int index)
351
404
  {
352
405
  // Each float is 4 bytes
353
- const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
354
- return *(reinterpret_cast<float const *>(_data + headerVar->offset) + index * 4);
406
+ const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
407
+ return *(reinterpret_cast<float const *>(_data + headerVar->offset) + index * 4);
355
408
  }
356
409
 
357
410
  double iRacingSdkNode::GetTelemetryDouble(int entry, int index)
358
411
  {
359
412
  // Each double is 8 bytes
360
- const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
361
- return *(reinterpret_cast<double const *>(_data + headerVar->offset) + index * 8);
413
+ const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
414
+ return *(reinterpret_cast<double const *>(_data + headerVar->offset) + index * 8);
362
415
  }
363
416
 
364
417
  Napi::Object iRacingSdkNode::GetTelemetryVarByIndex(const Napi::Env env, int index)
365
418
  {
366
- auto headerVar = irsdk_getVarHeaderEntry(index);
367
- auto telemVar = Napi::Object::New(env);
368
-
369
- // Create entry object
370
- telemVar.Set("countAsTime", headerVar->countAsTime);
371
- telemVar.Set("length", headerVar->count);
372
- telemVar.Set("name", headerVar->name);
373
- telemVar.Set("description", headerVar->desc);
374
- telemVar.Set("unit", headerVar->unit);
375
- telemVar.Set("varType", headerVar->type);
376
-
377
- int dataSize = headerVar->count * irsdk_VarTypeBytes[headerVar->type];
378
- auto entryVal = Napi::ArrayBuffer::New(env, dataSize);
379
- memcpy(entryVal.Data(), this->_data + headerVar->offset, dataSize);
380
-
381
- telemVar.Set("value", entryVal);
382
- return telemVar;
419
+ auto headerVar = irsdk_getVarHeaderEntry(index);
420
+ auto telemVar = Napi::Object::New(env);
421
+
422
+ // Create entry object
423
+ telemVar.Set("countAsTime", headerVar->countAsTime);
424
+ telemVar.Set("length", headerVar->count);
425
+ telemVar.Set("name", headerVar->name);
426
+ telemVar.Set("description", headerVar->desc);
427
+ telemVar.Set("unit", headerVar->unit);
428
+ telemVar.Set("varType", headerVar->type);
429
+
430
+ int dataSize = headerVar->count * irsdk_VarTypeBytes[headerVar->type];
431
+ auto entryVal = Napi::ArrayBuffer::New(env, dataSize);
432
+ memcpy(entryVal.Data(), this->_data + headerVar->offset, dataSize);
433
+
434
+ telemVar.Set("value", entryVal);
435
+ return telemVar;
383
436
  }
384
437
 
385
438
  Napi::Object iRacingSdkNode::GetTelemetryVar(const Napi::Env env, const char *varName)
386
439
  {
387
- int varIndex = irsdk_varNameToIndex(varName);
388
- return this->GetTelemetryVarByIndex(env, varIndex);
440
+ int varIndex = irsdk_varNameToIndex(varName);
441
+ return this->GetTelemetryVarByIndex(env, varIndex);
389
442
  }
390
443
 
391
444
  // ---------------------------
@@ -394,11 +447,11 @@ Napi::Object iRacingSdkNode::GetTelemetryVar(const Napi::Env env, const char *va
394
447
  // ---------------------------
395
448
  Napi::Object InitAll(Napi::Env env, Napi::Object exports)
396
449
  {
397
- iRacingSdkNode::Init(env, exports);
450
+ iRacingSdkNode::Init(env, exports);
398
451
 
399
- exports.Set("mockedSdk", false);
452
+ exports.Set("mockedSdk", false);
400
453
 
401
- return exports;
454
+ return exports;
402
455
  }
403
456
 
404
457
  NODE_API_MODULE(NODE_GYP_MODULE_NAME, InitAll);