-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathirsdk_node.cc
More file actions
586 lines (522 loc) · 23.8 KB
/
Copy pathirsdk_node.cc
File metadata and controls
586 lines (522 loc) · 23.8 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#include "./irsdk_node.h"
#include "./lib/yaml_parser.h"
/*
Nan::SetPrototypeMethod(tmpl, "getSessionData", GetSessionData);
Nan::SetPrototypeMethod(tmpl, "getSessionVersionNum", GetSessionVersionNum);
Nan::SetPrototypeMethod(tmpl, "getTelemetryData", GetTelemetryData);
Nan::SetPrototypeMethod(tmpl, "broadcast", BroadcastMessage);
Nan::SetPrototypeMethod(tmpl, "__getTelemetryTypes", __GetTelemetryTypes);
*/
// ---------------------------
// Constructors
// ---------------------------
Napi::Object iRacingSdkNode::Init(Napi::Env env, Napi::Object exports)
{
Napi::Function func = DefineClass(env, "iRacingSdkNode", {
// Properties
// Runtime-pointer overloads, not the templated forms which ICE on MSVC/VS 2026.
InstanceAccessor("currDataVersion", &iRacingSdkNode::GetCurrSessionDataVersion, nullptr),
InstanceAccessor("enableLogging", &iRacingSdkNode::GetEnableLogging, &iRacingSdkNode::SetEnableLogging),
// Methods
//Control
InstanceMethod("startSDK", &iRacingSdkNode::StartSdk),
InstanceMethod("stopSDK", &iRacingSdkNode::StopSdk),
InstanceMethod("waitForData", &iRacingSdkNode::WaitForData),
InstanceMethod("broadcast", &iRacingSdkNode::BroadcastMessage),
// Getters
InstanceMethod("isRunning", &iRacingSdkNode::IsRunning),
InstanceMethod("getSessionVersionNum", &iRacingSdkNode::GetSessionVersionNum),
InstanceMethod("getSessionData", &iRacingSdkNode::GetSessionData),
InstanceMethod("getTelemetryData", &iRacingSdkNode::GetTelemetryData),
InstanceMethod("getTelemetryVariable", &iRacingSdkNode::GetTelemetryVar),
// Helpers
InstanceMethod("__getTelemetryTypes", &iRacingSdkNode::__GetTelemetryTypes)
});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);
exports.Set("iRacingSdkNode", func);
return exports;
}
iRacingSdkNode::iRacingSdkNode(const Napi::CallbackInfo &info)
: Napi::ObjectWrap<iRacingSdkNode>(info)
, _loggingEnabled(false)
, _data(NULL)
, _bufLineLen(0)
, _sessionStatusID(0)
, _lastSessionCt(-1)
, _sessionData(NULL)
{
printf("Initializing cpp class instance...\n");
}
// ---------------------------
// Property implementations
// ---------------------------
Napi::Value iRacingSdkNode::GetCurrSessionDataVersion(const Napi::CallbackInfo &info)
{
int ver = this->_lastSessionCt;
return Napi::Number::New(info.Env(), ver);
}
Napi::Value iRacingSdkNode::GetEnableLogging(const Napi::CallbackInfo &info)
{
bool enabled = this->_loggingEnabled;
return Napi::Boolean::New(info.Env(), enabled);
}
void iRacingSdkNode::SetEnableLogging(const Napi::CallbackInfo &info, const Napi::Value &value)
{
Napi::Boolean enable;
if (info.Length() <= 0 || !info[0].IsBoolean()) {
enable = Napi::Boolean::New(info.Env(), false);
} else {
enable = info[0].As<Napi::Boolean>();
}
printf("Setting logging enabled: %i\n", enable.Value());
this->_loggingEnabled = enable;
}
// ---------------------------
// Instance implementations
// ---------------------------
// SDK Control
Napi::Value iRacingSdkNode::StartSdk(const Napi::CallbackInfo &info)
{
printf("Starting SDK...\n");
if (!irsdk_isConnected()) {
bool result = irsdk_startup();
printf("Connected at least! %i\n", result);
return Napi::Boolean::New(info.Env(), result);
}
return Napi::Boolean::New(info.Env(), true);
}
Napi::Value iRacingSdkNode::StopSdk(const Napi::CallbackInfo &info)
{
irsdk_shutdown();
return Napi::Boolean::New(info.Env(), true);
}
Napi::Value iRacingSdkNode::WaitForData(const Napi::CallbackInfo &info)
{
// Figure out the time to wait
// This will default to the timeout set on the class
Napi::Number timeout;
if (info.Length() <= 0 || !info[0].IsNumber()) {
timeout = Napi::Number::New(info.Env(), 16);
} else {
timeout = info[0].As<Napi::Number>();
}
if (!irsdk_isConnected() && !irsdk_startup()) {
return Napi::Boolean::New(info.Env(), false);
}
const irsdk_header* header = irsdk_getHeader();
// Allocate buffer before waiting (so waitForDataReady can populate it)
if (header && !this->_data) {
if (this->_loggingEnabled) printf("Initial buffer allocation\n");
this->_data = new char[header->bufLen];
this->_bufLineLen = header->bufLen;
}
// Wait for start of session or new data (buffer will be populated here)
bool dataReady = irsdk_waitForDataReady(timeout, this->_data);
if (dataReady && header)
{
if (this->_loggingEnabled) printf("Got data from iRacing SDK\n");
// Check if data changed length (need to reallocate)
if (this->_bufLineLen != header->bufLen)
{
if (this->_loggingEnabled) printf("Data changed length, reallocating\n");
// Reallocate buffer for new size
if (this->_data) delete[] this->_data;
this->_bufLineLen = header->bufLen;
this->_data = new char[this->_bufLineLen];
// Increment connection counter
this->_sessionStatusID++;
this->_lastSessionCt = -1;
// Fetch data into the newly allocated buffer
if (irsdk_getNewData(this->_data))
{
if (this->_loggingEnabled) printf("New data retrieved after reallocation\n");
return Napi::Boolean::New(info.Env(), true);
}
}
else if (this->_data)
{
if (this->_loggingEnabled) printf("Data ready for processing\n");
return Napi::Boolean::New(info.Env(), true);
}
}
else if (!irsdk_isConnected())
{
if (this->_loggingEnabled) printf("Session ended. Cleaning up.\n");
// Session ended
if (this->_data) delete[] this->_data;
this->_data = NULL;
// Reset session info string status
this->_lastSessionCt = -1;
}
if (this->_loggingEnabled) printf("No data available.\n");
return Napi::Boolean::New(info.Env(), false);
}
Napi::Value iRacingSdkNode::BroadcastMessage(const Napi::CallbackInfo &info)
{
auto env = info.Env();
// S2 hardening: validate all args up front. The previous guard
// accepted info.Length() == 3 but then read info[3] out of bounds via
// .As<Napi::Number>() on the absent slot, and never type-checked
// info[1] or info[2]. The minimum required is msg + arg1 + arg2
// (every switch case below uses at least three positions). arg3 is
// read defensively below — only the two camera-switch variants need
// it, and those reject when it's missing.
if (info.Length() < 3 ||
!info[0].IsNumber() ||
!info[1].IsNumber() ||
!info[2].IsNumber()) {
return Napi::Boolean::New(env, false);
}
int msgEnumIndex = info[0].As<Napi::Number>().Int32Value();
irsdk_BroadcastMsg msgType = static_cast<irsdk_BroadcastMsg>(msgEnumIndex);
// Args
int arg1 = info[1].As<Napi::Number>().Int32Value();
auto arg2 = info[2].As<Napi::Number>();
// arg3 is only required by the camera-switch variants. Read it
// defensively so the other variants (which currently pass 3 args from
// TS — see irsdk-node.ts) continue to work.
bool hasArg3 = info.Length() > 3 && info[3].IsNumber();
auto arg3 = hasArg3 ? info[3].As<Napi::Number>() : Napi::Number::New(env, -1);
// these defs are in irsdk_defines.cpp
switch (msgType)
{
// irsdk_BroadcastMsg msg, int arg1, int arg2, int var3
case irsdk_BroadcastCamSwitchPos: // @todo we need to use irsdk_padCarNum for arg1
case irsdk_BroadcastCamSwitchNum:
if (!hasArg3) return Napi::Boolean::New(env, false);
printf("BroadcastMessage(msgType: %d, arg1: %d, arg2: %d, arg3: %d)\n", msgType, arg1, arg2.Int32Value(), arg3.Int32Value());
irsdk_broadcastMsg(msgType, arg1, arg2, arg3);
break;
// irsdk_BroadcastMsg msg, int arg1, int unused, int unused
case irsdk_BroadcastReplaySearch: // arg1 == irsdk_RpySrchMode
case irsdk_BroadcastReplaySetState: // arg1 == irsdk_RpyStateMode
case irsdk_BroadcastCamSetState: // arg1 == irsdk_CameraState
case irsdk_BroadcastTelemCommand: // arg1 == irsdk_TelemCommandMode
case irsdk_BroadcastVideoCapture: // arg1 == irsdk_VideoCaptureMode
printf("BroadcastMessage(msgType: %d, arg1: %d, arg2: -1, arg3: -1)\n", msgType, arg1);
irsdk_broadcastMsg(msgType, arg1, -1, -1);
break;
// irsdk_BroadcastMsg msg, int arg1, int arg2, int unused
case irsdk_BroadcastReloadTextures: // arg1 == irsdk_ReloadTexturesMode
case irsdk_BroadcastChatComand: // arg1 == irsdk_ChatCommandMode
case irsdk_BroadcastReplaySetPlaySpeed:
printf("BroadcastMessage(msgType: %d, arg1: %d, arg2: %d, arg3: -1)\n", msgType, arg1, arg2.Int32Value());
irsdk_broadcastMsg(msgType, arg1, arg2, -1);
break;
// irsdk_BroadcastMsg msg, int arg1, float arg2
case irsdk_BroadcastPitCommand: // arg1 == irsdk_PitCommandMode
case irsdk_BroadcastFFBCommand: // arg1 == irsdk_FFBCommandMode
case irsdk_BroadcastReplaySearchSessionTime:
case irskd_BroadcastReplaySetPlayPosition:
printf("BroadcastMessage(msgType: %d, arg1: %d, arg2: %f)\n", msgType, arg1, (float)arg2.FloatValue());
irsdk_broadcastMsg(msgType, arg1, (float)arg2.FloatValue());
break;
default:
printf("Attempted to broadcast an unsupported message.");
return Napi::Boolean::New(env, false);
}
return Napi::Boolean::New(env, true);
}
// SDK State Getters
Napi::Value iRacingSdkNode::IsRunning(const Napi::CallbackInfo &info)
{
bool result = irsdk_isConnected();
return Napi::Boolean::New(info.Env(), result);
}
Napi::Value iRacingSdkNode::GetSessionVersionNum(const Napi::CallbackInfo &info)
{
int sessVer = irsdk_getSessionInfoStrUpdate();
return Napi::Number::New(info.Env(), sessVer);
}
// Helper function to convert Windows-1252 to UTF-8
std::string ConvertToUTF8(const char* input) {
if (!input) return "";
std::string result;
result.reserve(strlen(input) * 2); // Reserve space for potential UTF-8 expansion
for (const char* p = input; *p; ++p) {
unsigned char c = *p;
if (c < 0x80) {
// ASCII character
result += c;
} else {
// Windows-1252 to UTF-8 conversion
switch (c) {
case 0x80: result += "\xE2\x82\xAC"; break; // €
case 0x82: result += "\xE2\x80\x9A"; break; // ‚
case 0x83: result += "\xC6\x92"; break; // ƒ
case 0x84: result += "\xE2\x80\x9E"; break; // „
case 0x85: result += "\xE2\x80\xA6"; break; // …
case 0x86: result += "\xE2\x80\xA0"; break; // †
case 0x87: result += "\xE2\x80\xA1"; break; // ‡
case 0x88: result += "\xCB\x86"; break; // ˆ
case 0x89: result += "\xE2\x80\xB0"; break; // ‰
case 0x8A: result += "\xC5\xA0"; break; // Š
case 0x8B: result += "\xE2\x80\xB9"; break; // ‹
case 0x8C: result += "\xC5\x92"; break; // Œ
case 0x8E: result += "\xC5\xBD"; break; // Ž
case 0x91: result += "\xE2\x80\x98"; break; // '
case 0x92: result += "\xE2\x80\x99"; break; // '
case 0x93: result += "\xE2\x80\x9C"; break; // "
case 0x94: result += "\xE2\x80\x9D"; break; // "
case 0x95: result += "\xE2\x80\xA2"; break; // •
case 0x96: result += "\xE2\x80\x93"; break; // –
case 0x97: result += "\xE2\x80\x94"; break; // —
case 0x98: result += "\xCB\x9C"; break; // ˜
case 0x99: result += "\xE2\x84\xA2"; break; // ™
case 0x9A: result += "\xC5\xA1"; break; // š
case 0x9B: result += "\xE2\x80\xBA"; break; // ›
case 0x9C: result += "\xC5\x93"; break; // œ
case 0x9E: result += "\xC5\xBE"; break; // ž
case 0x9F: result += "\xC5\xB8"; break; // Ÿ
case 0xA0: result += "\xC2\xA0"; break; //
case 0xA1: result += "\xC2\xA1"; break; // ¡
case 0xA2: result += "\xC2\xA2"; break; // ¢
case 0xA3: result += "\xC2\xA3"; break; // £
case 0xA4: result += "\xC2\xA4"; break; // ¤
case 0xA5: result += "\xC2\xA5"; break; // ¥
case 0xA6: result += "\xC2\xA6"; break; // ¦
case 0xA7: result += "\xC2\xA7"; break; // §
case 0xA8: result += "\xC2\xA8"; break; // ¨
case 0xA9: result += "\xC2\xA9"; break; // ©
case 0xAA: result += "\xC2\xAA"; break; // ª
case 0xAB: result += "\xC2\xAB"; break; // «
case 0xAC: result += "\xC2\xAC"; break; // ¬
case 0xAD: result += "\xC2\xAD"; break; //
case 0xAE: result += "\xC2\xAE"; break; // ®
case 0xAF: result += "\xC2\xAF"; break; // ¯
case 0xB0: result += "\xC2\xB0"; break; // °
case 0xB1: result += "\xC2\xB1"; break; // ±
case 0xB2: result += "\xC2\xB2"; break; // ²
case 0xB3: result += "\xC2\xB3"; break; // ³
case 0xB4: result += "\xC2\xB4"; break; // ´
case 0xB5: result += "\xC2\xB5"; break; // µ
case 0xB6: result += "\xC2\xB6"; break; // ¶
case 0xB7: result += "\xC2\xB7"; break; // ·
case 0xB8: result += "\xC2\xB8"; break; // ¸
case 0xB9: result += "\xC2\xB9"; break; // ¹
case 0xBA: result += "\xC2\xBA"; break; // º
case 0xBB: result += "\xC2\xBB"; break; // »
case 0xBC: result += "\xC2\xBC"; break; // ¼
case 0xBD: result += "\xC2\xBD"; break; // ½
case 0xBE: result += "\xC2\xBE"; break; // ¾
case 0xBF: result += "\xC2\xBF"; break; // ¿
case 0xC0: result += "\xC3\x80"; break; // À
case 0xC1: result += "\xC3\x81"; break; // Á
case 0xC2: result += "\xC3\x82"; break; // Â
case 0xC3: result += "\xC3\x83"; break; // Ã
case 0xC4: result += "\xC3\x84"; break; // Ä
case 0xC5: result += "\xC3\x85"; break; // Å
case 0xC6: result += "\xC3\x86"; break; // Æ
case 0xC7: result += "\xC3\x87"; break; // Ç
case 0xC8: result += "\xC3\x88"; break; // È
case 0xC9: result += "\xC3\x89"; break; // É
case 0xCA: result += "\xC3\x8A"; break; // Ê
case 0xCB: result += "\xC3\x8B"; break; // Ë
case 0xCC: result += "\xC3\x8C"; break; // Ì
case 0xCD: result += "\xC3\x8D"; break; // Í
case 0xCE: result += "\xC3\x8E"; break; // Î
case 0xCF: result += "\xC3\x8F"; break; // Ï
case 0xD0: result += "\xC3\x90"; break; // Ð
case 0xD1: result += "\xC3\x91"; break; // Ñ
case 0xD2: result += "\xC3\x92"; break; // Ò
case 0xD3: result += "\xC3\x93"; break; // Ó
case 0xD4: result += "\xC3\x94"; break; // Ô
case 0xD5: result += "\xC3\x95"; break; // Õ
case 0xD6: result += "\xC3\x96"; break; // Ö
case 0xD7: result += "\xC3\x97"; break; // ×
case 0xD8: result += "\xC3\x98"; break; // Ø
case 0xD9: result += "\xC3\x99"; break; // Ù
case 0xDA: result += "\xC3\x9A"; break; // Ú
case 0xDB: result += "\xC3\x9B"; break; // Û
case 0xDC: result += "\xC3\x9C"; break; // Ü
case 0xDD: result += "\xC3\x9D"; break; // Ý
case 0xDE: result += "\xC3\x9E"; break; // Þ
case 0xDF: result += "\xC3\x9F"; break; // ß
case 0xE0: result += "\xC3\xA0"; break; // à
case 0xE1: result += "\xC3\xA1"; break; // á
case 0xE2: result += "\xC3\xA2"; break; // â
case 0xE3: result += "\xC3\xA3"; break; // ã
case 0xE4: result += "\xC3\xA4"; break; // ä
case 0xE5: result += "\xC3\xA5"; break; // å
case 0xE6: result += "\xC3\xA6"; break; // æ
case 0xE7: result += "\xC3\xA7"; break; // ç
case 0xE8: result += "\xC3\xA8"; break; // è
case 0xE9: result += "\xC3\xA9"; break; // é
case 0xEA: result += "\xC3\xAA"; break; // ê
case 0xEB: result += "\xC3\xAB"; break; // ë
case 0xEC: result += "\xC3\xAC"; break; // ì
case 0xED: result += "\xC3\xAD"; break; // í
case 0xEE: result += "\xC3\xAE"; break; // î
case 0xEF: result += "\xC3\xAF"; break; // ï
case 0xF0: result += "\xC3\xB0"; break; // ð
case 0xF1: result += "\xC3\xB1"; break; // ñ
case 0xF2: result += "\xC3\xB2"; break; // ò
case 0xF3: result += "\xC3\xB3"; break; // ó
case 0xF4: result += "\xC3\xB4"; break; // ô
case 0xF5: result += "\xC3\xB5"; break; // õ
case 0xF6: result += "\xC3\xB6"; break; // ö
case 0xF7: result += "\xC3\xB7"; break; // ÷
case 0xF8: result += "\xC3\xB8"; break; // ø
case 0xF9: result += "\xC3\xB9"; break; // ù
case 0xFA: result += "\xC3\xBA"; break; // ú
case 0xFB: result += "\xC3\xBB"; break; // û
case 0xFC: result += "\xC3\xBC"; break; // ü
case 0xFD: result += "\xC3\xBD"; break; // ý
case 0xFE: result += "\xC3\xBE"; break; // þ
case 0xFF: result += "\xC3\xBF"; break; // ÿ
default: result += c; break;
}
}
}
return result;
}
Napi::Value iRacingSdkNode::GetSessionData(const Napi::CallbackInfo &info)
{
int latestUpdate = irsdk_getSessionInfoStrUpdate();
if (this->_lastSessionCt != latestUpdate) {
printf("Session data has been updated (prev: %d, new: %d)\n", this->_lastSessionCt, latestUpdate);
this->_lastSessionCt = latestUpdate;
this->_sessionData = irsdk_getSessionInfoStr();
}
const char *session = this->_sessionData;
if (session == NULL) {
return Napi::String::New(info.Env(), "");
}
// Convert Windows-1252 to UTF-8
std::string utf8Session = ConvertToUTF8(session);
return Napi::String::New(info.Env(), utf8Session.c_str());
}
Napi::Value iRacingSdkNode::GetTelemetryVar(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
// S2 hardening: varIndex was previously uninitialised when the common
// numeric-arg path was taken, leaving a garbage value on the stack
// that flowed into GetTelemetryVarByIndex. Initialise to 0 and extract
// the actual value from info[0] when it's a valid number; out-of-range
// values are caught defensively by GetTelemetryVarByIndex below.
int varIndex = 0;
if (info.Length() > 0) {
if (info[0].IsString()) {
std::string nameStr = info[0].As<Napi::String>().Utf8Value();
return this->GetTelemetryVar(env, nameStr.c_str());
}
if (info[0].IsNumber()) {
varIndex = info[0].As<Napi::Number>().Int32Value();
}
// Any other type falls through with varIndex = 0.
}
return this->GetTelemetryVarByIndex(env, varIndex);
}
Napi::Value iRacingSdkNode::GetTelemetryData(const Napi::CallbackInfo &info)
{
auto env = info.Env();
auto telemVars = Napi::Object::New(env);
// S2 hardening: irsdk_getHeader() returns NULL when the SDK has not yet
// mapped the shared-memory header (i.e. iRacing isn't running). Reading
// header->numVars unguarded would crash the main process. Return an
// empty object instead — the JS caller already handles the empty case
// (no telemetry yet).
const irsdk_header* header = irsdk_getHeader();
if (header == nullptr) {
return telemVars;
}
int count = header->numVars;
for (int i = 0; i < count; i++) {
auto telemVariable = this->GetTelemetryVarByIndex(env, i);
if (telemVariable.IsObject() && telemVariable.Has("name")) {
telemVars.Set(telemVariable.Get("name"), telemVariable);
}
}
return telemVars;
}
// Helpers
Napi::Value iRacingSdkNode::__GetTelemetryTypes(const Napi::CallbackInfo &info)
{
auto env = info.Env();
auto result = Napi::Object::New(env);
const int count = irsdk_getHeader()->numVars;
const irsdk_varHeader *varHeader;
for (int i = 0; i < count; i++) {
varHeader = irsdk_getVarHeaderEntry(i);
result.Set(varHeader->name, Napi::Number::New(env, varHeader->type));
}
return result;
}
// ---------------------------
// Helper functions
// ---------------------------
bool iRacingSdkNode::GetTelemetryBool(int entry, int index)
{
const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
return *(reinterpret_cast<bool const *>(_data + headerVar->offset) + index);
}
int iRacingSdkNode::GetTelemetryInt(int entry, int index)
{
// Each int is 4 bytes
const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
return *(reinterpret_cast<int const *>(_data + headerVar->offset) + index * 4);
}
float iRacingSdkNode::GetTelemetryFloat(int entry, int index)
{
// Each float is 4 bytes
const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
return *(reinterpret_cast<float const *>(_data + headerVar->offset) + index * 4);
}
double iRacingSdkNode::GetTelemetryDouble(int entry, int index)
{
// Each double is 8 bytes
const irsdk_varHeader *headerVar = irsdk_getVarHeaderEntry(entry);
return *(reinterpret_cast<double const *>(_data + headerVar->offset) + index * 8);
}
Napi::Object iRacingSdkNode::GetTelemetryVarByIndex(const Napi::Env env, int index)
{
auto telemVar = Napi::Object::New(env);
// S2 hardening: irsdk_getVarHeaderEntry() returns NULL for indices
// outside [0, numVars) or when the SDK is not yet initialised. _data
// is NULL until the first successful telemetry read. Either condition
// would crash the process on the field reads or memcpy below — return
// an empty object instead. The JS-side TelemetryStore tolerates
// missing keys, so callers degrade gracefully.
auto headerVar = irsdk_getVarHeaderEntry(index);
if (headerVar == nullptr || this->_data == nullptr) {
return telemVar;
}
// Defensive bound on the type-byte-size lookup: headerVar->type is
// attacker-influenced via shared memory in principle. Drop the entry
// if the type is outside the known enum range.
if (headerVar->type < 0 || headerVar->type >= irsdk_ETCount) {
return telemVar;
}
// Create entry object
telemVar.Set("countAsTime", headerVar->countAsTime);
telemVar.Set("length", headerVar->count);
telemVar.Set("name", headerVar->name);
telemVar.Set("description", headerVar->desc);
telemVar.Set("unit", headerVar->unit);
telemVar.Set("varType", headerVar->type);
int dataSize = headerVar->count * irsdk_VarTypeBytes[headerVar->type];
auto entryVal = Napi::ArrayBuffer::New(env, dataSize);
memcpy(entryVal.Data(), this->_data + headerVar->offset, dataSize);
telemVar.Set("value", entryVal);
return telemVar;
}
Napi::Object iRacingSdkNode::GetTelemetryVar(const Napi::Env env, const char *varName)
{
int varIndex = irsdk_varNameToIndex(varName);
// S2 hardening: irsdk_varNameToIndex returns -1 when the name is
// unknown or the SDK is not yet initialised. Passing -1 into
// GetTelemetryVarByIndex would have crashed before its null-guard
// was added; the guard now catches it, but reject early for clarity.
if (varIndex < 0) {
return Napi::Object::New(env);
}
return this->GetTelemetryVarByIndex(env, varIndex);
}
Napi::Object InitAll(Napi::Env env, Napi::Object exports)
{
iRacingSdkNode::Init(env, exports);
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, InitAll);