@irsdk-node/native 4.0.2 → 4.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/README.md +46 -46
- package/binding.gyp +28 -19
- package/dist/INativeSDK.d.ts +28 -0
- package/dist/INativeSDK.js +2 -0
- package/dist/MockSdk.d.ts +43 -0
- package/dist/MockSdk.js +70 -0
- package/dist/mock-data/loader.d.ts +3 -0
- package/dist/mock-data/loader.js +41 -0
- package/dist/mock-data/session.json +4305 -0
- package/dist/mock-data/telemetry.json +4986 -0
- package/{src/index.d.ts → index.d.ts} +5 -35
- package/index.js +36 -0
- package/lib/irsdk_client.cpp +495 -495
- package/lib/irsdk_client.h +139 -139
- package/lib/irsdk_defines.h +528 -528
- package/{src → lib}/irsdk_node.cc +404 -375
- package/{src → lib}/irsdk_node.h +49 -50
- package/lib/irsdk_node_mocked.cc +226 -0
- package/lib/irsdk_utils.cpp +377 -377
- package/lib/yaml_parser.cpp +176 -176
- package/lib/yaml_parser.h +34 -34
- package/package.json +37 -29
- package/prebuilds/darwin-arm64/@irsdk-node+native.node +0 -0
- package/prebuilds/linux-x64/@irsdk-node+native.node +0 -0
- package/prebuilds/win32-x64/@irsdk-node+native.node +0 -0
- package/build/Debug/irsdk_node.exp +0 -0
- package/build/Debug/irsdk_node.lib +0 -0
- package/build/Debug/irsdk_node.node +0 -0
- package/build/Debug/irsdk_node.pdb +0 -0
- package/build/Debug/obj/irsdk_node/irsdk_node.node.recipe +0 -11
- package/build/Debug/obj/irsdk_node/irsdk_node.tlog/CL.command.1.tlog +0 -0
- package/build/Debug/obj/irsdk_node/irsdk_node.tlog/CL.read.1.tlog +0 -0
- package/build/Debug/obj/irsdk_node/irsdk_node.tlog/CL.write.1.tlog +0 -0
- package/build/Debug/obj/irsdk_node/irsdk_node.tlog/Cl.items.tlog +0 -4
- package/build/Debug/obj/irsdk_node/irsdk_node.tlog/irsdk_node.lastbuildstate +0 -2
- package/build/Debug/obj/irsdk_node/irsdk_node.tlog/link.command.1.tlog +0 -0
- package/build/Debug/obj/irsdk_node/irsdk_node.tlog/link.read.1.tlog +0 -0
- package/build/Debug/obj/irsdk_node/irsdk_node.tlog/link.secondary.1.tlog +0 -3
- package/build/Debug/obj/irsdk_node/irsdk_node.tlog/link.write.1.tlog +0 -0
- package/build/Debug/obj/irsdk_node/lib/irsdk_utils.obj +0 -0
- package/build/Debug/obj/irsdk_node/lib/yaml_parser.obj +0 -0
- package/build/Debug/obj/irsdk_node/src/irsdk_node.obj +0 -0
- package/build/Debug/obj/irsdk_node/vcpkg.applocal.log +0 -1
- package/build/Debug/obj/irsdk_node/win_delay_load_hook.obj +0 -0
- package/build/Release/irsdk_node.exp +0 -0
- package/build/Release/irsdk_node.iobj +0 -0
- package/build/Release/irsdk_node.ipdb +0 -0
- package/build/Release/irsdk_node.lib +0 -0
- package/build/Release/irsdk_node.node +0 -0
- package/build/Release/irsdk_node.pdb +0 -0
- package/build/Release/obj/irsdk_node/irsdk_node.node.recipe +0 -11
- package/build/Release/obj/irsdk_node/irsdk_node.tlog/CL.command.1.tlog +0 -0
- package/build/Release/obj/irsdk_node/irsdk_node.tlog/CL.read.1.tlog +0 -0
- package/build/Release/obj/irsdk_node/irsdk_node.tlog/CL.write.1.tlog +0 -0
- package/build/Release/obj/irsdk_node/irsdk_node.tlog/Cl.items.tlog +0 -4
- package/build/Release/obj/irsdk_node/irsdk_node.tlog/irsdk_node.lastbuildstate +0 -2
- package/build/Release/obj/irsdk_node/irsdk_node.tlog/link.command.1.tlog +0 -0
- package/build/Release/obj/irsdk_node/irsdk_node.tlog/link.read.1.tlog +0 -0
- package/build/Release/obj/irsdk_node/irsdk_node.tlog/link.secondary.1.tlog +0 -5
- package/build/Release/obj/irsdk_node/irsdk_node.tlog/link.write.1.tlog +0 -0
- package/build/Release/obj/irsdk_node/lib/irsdk_utils.obj +0 -0
- package/build/Release/obj/irsdk_node/lib/yaml_parser.obj +0 -0
- package/build/Release/obj/irsdk_node/src/irsdk_node.obj +0 -0
- package/build/Release/obj/irsdk_node/vcpkg.applocal.log +0 -1
- package/build/Release/obj/irsdk_node/win_delay_load_hook.obj +0 -0
- package/build/binding.sln +0 -19
- package/build/config.gypi +0 -426
- package/build/irsdk_node.vcxproj +0 -157
- package/build/irsdk_node.vcxproj.filters +0 -73
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -13
- package/src/index.js +0 -11
package/lib/irsdk_utils.cpp
CHANGED
|
@@ -1,377 +1,377 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (c) 2013, iRacing.com Motorsport Simulations, LLC.
|
|
3
|
-
All rights reserved.
|
|
4
|
-
|
|
5
|
-
Redistribution and use in source and binary forms, with or without
|
|
6
|
-
modification, are permitted provided that the following conditions are met:
|
|
7
|
-
* Redistributions of source code must retain the above copyright
|
|
8
|
-
notice, this list of conditions and the following disclaimer.
|
|
9
|
-
* Redistributions in binary form must reproduce the above copyright
|
|
10
|
-
notice, this list of conditions and the following disclaimer in the
|
|
11
|
-
documentation and/or other materials provided with the distribution.
|
|
12
|
-
* Neither the name of iRacing.com Motorsport Simulations nor the
|
|
13
|
-
names of its contributors may be used to endorse or promote products
|
|
14
|
-
derived from this software without specific prior written permission.
|
|
15
|
-
|
|
16
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
17
|
-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
18
|
-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
19
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
20
|
-
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
21
|
-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
22
|
-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
23
|
-
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
24
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
25
|
-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
#define MIN_WIN_VER 0x0501
|
|
29
|
-
|
|
30
|
-
#ifndef WINVER
|
|
31
|
-
# define WINVER MIN_WIN_VER
|
|
32
|
-
#endif
|
|
33
|
-
|
|
34
|
-
#ifndef _WIN32_WINNT
|
|
35
|
-
# define _WIN32_WINNT MIN_WIN_VER
|
|
36
|
-
#endif
|
|
37
|
-
|
|
38
|
-
#include <windows.h>
|
|
39
|
-
#include <stdio.h>
|
|
40
|
-
#include <time.h>
|
|
41
|
-
#include <limits.h>
|
|
42
|
-
|
|
43
|
-
#ifdef _MSC_VER
|
|
44
|
-
#include <crtdbg.h>
|
|
45
|
-
#endif
|
|
46
|
-
|
|
47
|
-
#include "irsdk_defines.h"
|
|
48
|
-
|
|
49
|
-
// for timeBeginPeriod()
|
|
50
|
-
#pragma comment(lib, "Winmm")
|
|
51
|
-
// for RegisterWindowMessage() and SendMessage()
|
|
52
|
-
#pragma comment(lib, "User32")
|
|
53
|
-
|
|
54
|
-
// Local memory
|
|
55
|
-
|
|
56
|
-
static HANDLE hDataValidEvent = NULL;
|
|
57
|
-
static HANDLE hMemMapFile = NULL;
|
|
58
|
-
|
|
59
|
-
static const char *pSharedMem = NULL;
|
|
60
|
-
static const irsdk_header *pHeader = NULL;
|
|
61
|
-
|
|
62
|
-
static int lastTickCount = INT_MAX;
|
|
63
|
-
static bool isInitialized = false;
|
|
64
|
-
|
|
65
|
-
static const double timeout = 30.0; // timeout after 30 seconds with no communication
|
|
66
|
-
static time_t lastValidTime = 0;
|
|
67
|
-
|
|
68
|
-
// Function Implementations
|
|
69
|
-
|
|
70
|
-
bool irsdk_startup()
|
|
71
|
-
{
|
|
72
|
-
if(!hMemMapFile)
|
|
73
|
-
{
|
|
74
|
-
hMemMapFile = OpenFileMapping( FILE_MAP_READ, FALSE, IRSDK_MEMMAPFILENAME);
|
|
75
|
-
lastTickCount = INT_MAX;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if(hMemMapFile)
|
|
79
|
-
{
|
|
80
|
-
if(!pSharedMem)
|
|
81
|
-
{
|
|
82
|
-
pSharedMem = (const char *)MapViewOfFile(hMemMapFile, FILE_MAP_READ, 0, 0, 0);
|
|
83
|
-
pHeader = (irsdk_header *)pSharedMem;
|
|
84
|
-
lastTickCount = INT_MAX;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if(pSharedMem)
|
|
88
|
-
{
|
|
89
|
-
if(!hDataValidEvent)
|
|
90
|
-
{
|
|
91
|
-
hDataValidEvent = OpenEvent(SYNCHRONIZE, false, IRSDK_DATAVALIDEVENTNAME);
|
|
92
|
-
lastTickCount = INT_MAX;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if(hDataValidEvent)
|
|
96
|
-
{
|
|
97
|
-
isInitialized = true;
|
|
98
|
-
return isInitialized;
|
|
99
|
-
}
|
|
100
|
-
//else printf("Error opening event: %d\n", GetLastError());
|
|
101
|
-
}
|
|
102
|
-
//else printf("Error mapping file: %d\n", GetLastError());
|
|
103
|
-
}
|
|
104
|
-
//else printf("Error opening file: %d\n", GetLastError());
|
|
105
|
-
|
|
106
|
-
isInitialized = false;
|
|
107
|
-
return isInitialized;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
void irsdk_shutdown()
|
|
111
|
-
{
|
|
112
|
-
if(hDataValidEvent)
|
|
113
|
-
CloseHandle(hDataValidEvent);
|
|
114
|
-
|
|
115
|
-
if(pSharedMem)
|
|
116
|
-
UnmapViewOfFile(pSharedMem);
|
|
117
|
-
|
|
118
|
-
if(hMemMapFile)
|
|
119
|
-
CloseHandle(hMemMapFile);
|
|
120
|
-
|
|
121
|
-
hDataValidEvent = NULL;
|
|
122
|
-
pSharedMem = NULL;
|
|
123
|
-
pHeader = NULL;
|
|
124
|
-
hMemMapFile = NULL;
|
|
125
|
-
|
|
126
|
-
isInitialized = false;
|
|
127
|
-
lastTickCount = INT_MAX;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
bool irsdk_getNewData(char *data)
|
|
131
|
-
{
|
|
132
|
-
if(isInitialized || irsdk_startup())
|
|
133
|
-
{
|
|
134
|
-
#ifdef _MSC_VER
|
|
135
|
-
_ASSERTE(NULL != pHeader);
|
|
136
|
-
#endif
|
|
137
|
-
|
|
138
|
-
// if sim is not active, then no new data
|
|
139
|
-
if(!(pHeader->status & irsdk_stConnected))
|
|
140
|
-
{
|
|
141
|
-
lastTickCount = INT_MAX;
|
|
142
|
-
return false;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
int latest = 0;
|
|
146
|
-
for(int i=1; i<pHeader->numBuf; i++)
|
|
147
|
-
if(pHeader->varBuf[latest].tickCount < pHeader->varBuf[i].tickCount)
|
|
148
|
-
latest = i;
|
|
149
|
-
|
|
150
|
-
// if newer than last recieved, than report new data
|
|
151
|
-
if(lastTickCount < pHeader->varBuf[latest].tickCount)
|
|
152
|
-
{
|
|
153
|
-
// if asked to retrieve the data
|
|
154
|
-
if(data)
|
|
155
|
-
{
|
|
156
|
-
// try twice to get the data out
|
|
157
|
-
for(int count = 0; count < 2; count++)
|
|
158
|
-
{
|
|
159
|
-
int curTickCount = pHeader->varBuf[latest].tickCount;
|
|
160
|
-
memcpy(data, pSharedMem + pHeader->varBuf[latest].bufOffset, pHeader->bufLen);
|
|
161
|
-
if(curTickCount == pHeader->varBuf[latest].tickCount)
|
|
162
|
-
{
|
|
163
|
-
lastTickCount = curTickCount;
|
|
164
|
-
lastValidTime = time(NULL);
|
|
165
|
-
return true;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
// if here, the data changed out from under us.
|
|
169
|
-
return false;
|
|
170
|
-
}
|
|
171
|
-
else
|
|
172
|
-
{
|
|
173
|
-
lastTickCount = pHeader->varBuf[latest].tickCount;
|
|
174
|
-
lastValidTime = time(NULL);
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
// if older than last recieved, than reset, we probably disconnected
|
|
179
|
-
else if(lastTickCount > pHeader->varBuf[latest].tickCount)
|
|
180
|
-
{
|
|
181
|
-
lastTickCount = pHeader->varBuf[latest].tickCount;
|
|
182
|
-
return false;
|
|
183
|
-
}
|
|
184
|
-
// else the same, and nothing changed this tick
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
bool irsdk_waitForDataReady(int timeOut, char *data)
|
|
192
|
-
{
|
|
193
|
-
#ifdef _MSC_VER
|
|
194
|
-
_ASSERTE(timeOut >= 0);
|
|
195
|
-
#endif
|
|
196
|
-
|
|
197
|
-
if(isInitialized || irsdk_startup())
|
|
198
|
-
{
|
|
199
|
-
// just to be sure, check before we sleep
|
|
200
|
-
if(irsdk_getNewData(data))
|
|
201
|
-
return true;
|
|
202
|
-
|
|
203
|
-
// sleep till signaled
|
|
204
|
-
WaitForSingleObject(hDataValidEvent, timeOut);
|
|
205
|
-
|
|
206
|
-
// we woke up, so check for data
|
|
207
|
-
if(irsdk_getNewData(data))
|
|
208
|
-
return true;
|
|
209
|
-
else
|
|
210
|
-
return false;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// sleep if error
|
|
214
|
-
if(timeOut > 0)
|
|
215
|
-
Sleep(timeOut);
|
|
216
|
-
|
|
217
|
-
return false;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
bool irsdk_isConnected()
|
|
221
|
-
{
|
|
222
|
-
if(isInitialized)
|
|
223
|
-
{
|
|
224
|
-
int elapsed = (int)difftime(time(NULL), lastValidTime);
|
|
225
|
-
return (pHeader->status & irsdk_stConnected) > 0 && elapsed < timeout;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return false;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const irsdk_header *irsdk_getHeader()
|
|
232
|
-
{
|
|
233
|
-
if(isInitialized)
|
|
234
|
-
{
|
|
235
|
-
return pHeader;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
return NULL;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// direct access to the data buffer
|
|
242
|
-
// Warnign! This buffer is volitile so read it out fast!
|
|
243
|
-
// Use the cached copy from irsdk_waitForDataReady() or irsdk_getNewData() instead
|
|
244
|
-
const char *irsdk_getData(int index)
|
|
245
|
-
{
|
|
246
|
-
if(isInitialized)
|
|
247
|
-
{
|
|
248
|
-
return pSharedMem + pHeader->varBuf[index].bufOffset;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return NULL;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const char *irsdk_getSessionInfoStr()
|
|
255
|
-
{
|
|
256
|
-
if(isInitialized)
|
|
257
|
-
{
|
|
258
|
-
return pSharedMem + pHeader->sessionInfoOffset;
|
|
259
|
-
}
|
|
260
|
-
return NULL;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
int irsdk_getSessionInfoStrUpdate()
|
|
264
|
-
{
|
|
265
|
-
if(isInitialized)
|
|
266
|
-
{
|
|
267
|
-
return pHeader->sessionInfoUpdate;
|
|
268
|
-
}
|
|
269
|
-
return -1;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const irsdk_varHeader *irsdk_getVarHeaderPtr()
|
|
273
|
-
{
|
|
274
|
-
if(isInitialized)
|
|
275
|
-
{
|
|
276
|
-
return ((irsdk_varHeader*)(pSharedMem + pHeader->varHeaderOffset));
|
|
277
|
-
}
|
|
278
|
-
return NULL;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const irsdk_varHeader *irsdk_getVarHeaderEntry(int index)
|
|
282
|
-
{
|
|
283
|
-
if(isInitialized)
|
|
284
|
-
{
|
|
285
|
-
if(index >= 0 && index < pHeader->numVars)
|
|
286
|
-
{
|
|
287
|
-
return &((irsdk_varHeader*)(pSharedMem + pHeader->varHeaderOffset))[index];
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
return NULL;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// Note: this is a linear search, so cache the results
|
|
294
|
-
int irsdk_varNameToIndex(const char *name)
|
|
295
|
-
{
|
|
296
|
-
const irsdk_varHeader *pVar;
|
|
297
|
-
|
|
298
|
-
if(name)
|
|
299
|
-
{
|
|
300
|
-
for(int index=0; index<pHeader->numVars; index++)
|
|
301
|
-
{
|
|
302
|
-
pVar = irsdk_getVarHeaderEntry(index);
|
|
303
|
-
if(pVar && 0 == strncmp(name, pVar->name, IRSDK_MAX_STRING))
|
|
304
|
-
{
|
|
305
|
-
return index;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return -1;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
int irsdk_varNameToOffset(const char *name)
|
|
314
|
-
{
|
|
315
|
-
const irsdk_varHeader *pVar;
|
|
316
|
-
|
|
317
|
-
if(name)
|
|
318
|
-
{
|
|
319
|
-
for(int index=0; index<pHeader->numVars; index++)
|
|
320
|
-
{
|
|
321
|
-
pVar = irsdk_getVarHeaderEntry(index);
|
|
322
|
-
if(pVar && 0 == strncmp(name, pVar->name, IRSDK_MAX_STRING))
|
|
323
|
-
{
|
|
324
|
-
return pVar->offset;
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return -1;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
unsigned int irsdk_getBroadcastMsgID()
|
|
333
|
-
{
|
|
334
|
-
static unsigned int msgId = RegisterWindowMessage(IRSDK_BROADCASTMSGNAME);
|
|
335
|
-
|
|
336
|
-
return msgId;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
void irsdk_broadcastMsg(irsdk_BroadcastMsg msg, int var1, int var2, int var3)
|
|
340
|
-
{
|
|
341
|
-
irsdk_broadcastMsg(msg, var1, static_cast<int>MAKELONG(var2, var3));
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
void irsdk_broadcastMsg(irsdk_BroadcastMsg msg, int var1, float var2)
|
|
345
|
-
{
|
|
346
|
-
// multiply by 2^16-1 to move fractional part to the integer part
|
|
347
|
-
int real = (int)(var2 * 65536.0f);
|
|
348
|
-
|
|
349
|
-
irsdk_broadcastMsg(msg, var1, real);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
void irsdk_broadcastMsg(irsdk_BroadcastMsg msg, int var1, int var2)
|
|
353
|
-
{
|
|
354
|
-
static unsigned int msgId = irsdk_getBroadcastMsgID();
|
|
355
|
-
|
|
356
|
-
if(msgId && msg >= 0 && msg < irsdk_BroadcastLast)
|
|
357
|
-
{
|
|
358
|
-
SendNotifyMessage(HWND_BROADCAST, msgId, MAKELONG(msg, var1), var2);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
int irsdk_padCarNum(int num, int zero)
|
|
363
|
-
{
|
|
364
|
-
int retVal = num;
|
|
365
|
-
int numPlace = 1;
|
|
366
|
-
if(num > 99)
|
|
367
|
-
numPlace = 3;
|
|
368
|
-
else if(num > 9)
|
|
369
|
-
numPlace = 2;
|
|
370
|
-
if(zero)
|
|
371
|
-
{
|
|
372
|
-
numPlace += zero;
|
|
373
|
-
retVal = num + 1000*numPlace;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
return retVal;
|
|
377
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2013, iRacing.com Motorsport Simulations, LLC.
|
|
3
|
+
All rights reserved.
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
* Redistributions of source code must retain the above copyright
|
|
8
|
+
notice, this list of conditions and the following disclaimer.
|
|
9
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
10
|
+
notice, this list of conditions and the following disclaimer in the
|
|
11
|
+
documentation and/or other materials provided with the distribution.
|
|
12
|
+
* Neither the name of iRacing.com Motorsport Simulations nor the
|
|
13
|
+
names of its contributors may be used to endorse or promote products
|
|
14
|
+
derived from this software without specific prior written permission.
|
|
15
|
+
|
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
17
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
18
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
20
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
21
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
22
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
23
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
24
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
25
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
#define MIN_WIN_VER 0x0501
|
|
29
|
+
|
|
30
|
+
#ifndef WINVER
|
|
31
|
+
# define WINVER MIN_WIN_VER
|
|
32
|
+
#endif
|
|
33
|
+
|
|
34
|
+
#ifndef _WIN32_WINNT
|
|
35
|
+
# define _WIN32_WINNT MIN_WIN_VER
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
#include <windows.h>
|
|
39
|
+
#include <stdio.h>
|
|
40
|
+
#include <time.h>
|
|
41
|
+
#include <limits.h>
|
|
42
|
+
|
|
43
|
+
#ifdef _MSC_VER
|
|
44
|
+
#include <crtdbg.h>
|
|
45
|
+
#endif
|
|
46
|
+
|
|
47
|
+
#include "irsdk_defines.h"
|
|
48
|
+
|
|
49
|
+
// for timeBeginPeriod()
|
|
50
|
+
#pragma comment(lib, "Winmm")
|
|
51
|
+
// for RegisterWindowMessage() and SendMessage()
|
|
52
|
+
#pragma comment(lib, "User32")
|
|
53
|
+
|
|
54
|
+
// Local memory
|
|
55
|
+
|
|
56
|
+
static HANDLE hDataValidEvent = NULL;
|
|
57
|
+
static HANDLE hMemMapFile = NULL;
|
|
58
|
+
|
|
59
|
+
static const char *pSharedMem = NULL;
|
|
60
|
+
static const irsdk_header *pHeader = NULL;
|
|
61
|
+
|
|
62
|
+
static int lastTickCount = INT_MAX;
|
|
63
|
+
static bool isInitialized = false;
|
|
64
|
+
|
|
65
|
+
static const double timeout = 30.0; // timeout after 30 seconds with no communication
|
|
66
|
+
static time_t lastValidTime = 0;
|
|
67
|
+
|
|
68
|
+
// Function Implementations
|
|
69
|
+
|
|
70
|
+
bool irsdk_startup()
|
|
71
|
+
{
|
|
72
|
+
if(!hMemMapFile)
|
|
73
|
+
{
|
|
74
|
+
hMemMapFile = OpenFileMapping( FILE_MAP_READ, FALSE, IRSDK_MEMMAPFILENAME);
|
|
75
|
+
lastTickCount = INT_MAX;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if(hMemMapFile)
|
|
79
|
+
{
|
|
80
|
+
if(!pSharedMem)
|
|
81
|
+
{
|
|
82
|
+
pSharedMem = (const char *)MapViewOfFile(hMemMapFile, FILE_MAP_READ, 0, 0, 0);
|
|
83
|
+
pHeader = (irsdk_header *)pSharedMem;
|
|
84
|
+
lastTickCount = INT_MAX;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if(pSharedMem)
|
|
88
|
+
{
|
|
89
|
+
if(!hDataValidEvent)
|
|
90
|
+
{
|
|
91
|
+
hDataValidEvent = OpenEvent(SYNCHRONIZE, false, IRSDK_DATAVALIDEVENTNAME);
|
|
92
|
+
lastTickCount = INT_MAX;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if(hDataValidEvent)
|
|
96
|
+
{
|
|
97
|
+
isInitialized = true;
|
|
98
|
+
return isInitialized;
|
|
99
|
+
}
|
|
100
|
+
//else printf("Error opening event: %d\n", GetLastError());
|
|
101
|
+
}
|
|
102
|
+
//else printf("Error mapping file: %d\n", GetLastError());
|
|
103
|
+
}
|
|
104
|
+
//else printf("Error opening file: %d\n", GetLastError());
|
|
105
|
+
|
|
106
|
+
isInitialized = false;
|
|
107
|
+
return isInitialized;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
void irsdk_shutdown()
|
|
111
|
+
{
|
|
112
|
+
if(hDataValidEvent)
|
|
113
|
+
CloseHandle(hDataValidEvent);
|
|
114
|
+
|
|
115
|
+
if(pSharedMem)
|
|
116
|
+
UnmapViewOfFile(pSharedMem);
|
|
117
|
+
|
|
118
|
+
if(hMemMapFile)
|
|
119
|
+
CloseHandle(hMemMapFile);
|
|
120
|
+
|
|
121
|
+
hDataValidEvent = NULL;
|
|
122
|
+
pSharedMem = NULL;
|
|
123
|
+
pHeader = NULL;
|
|
124
|
+
hMemMapFile = NULL;
|
|
125
|
+
|
|
126
|
+
isInitialized = false;
|
|
127
|
+
lastTickCount = INT_MAX;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
bool irsdk_getNewData(char *data)
|
|
131
|
+
{
|
|
132
|
+
if(isInitialized || irsdk_startup())
|
|
133
|
+
{
|
|
134
|
+
#ifdef _MSC_VER
|
|
135
|
+
_ASSERTE(NULL != pHeader);
|
|
136
|
+
#endif
|
|
137
|
+
|
|
138
|
+
// if sim is not active, then no new data
|
|
139
|
+
if(!(pHeader->status & irsdk_stConnected))
|
|
140
|
+
{
|
|
141
|
+
lastTickCount = INT_MAX;
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
int latest = 0;
|
|
146
|
+
for(int i=1; i<pHeader->numBuf; i++)
|
|
147
|
+
if(pHeader->varBuf[latest].tickCount < pHeader->varBuf[i].tickCount)
|
|
148
|
+
latest = i;
|
|
149
|
+
|
|
150
|
+
// if newer than last recieved, than report new data
|
|
151
|
+
if(lastTickCount < pHeader->varBuf[latest].tickCount)
|
|
152
|
+
{
|
|
153
|
+
// if asked to retrieve the data
|
|
154
|
+
if(data)
|
|
155
|
+
{
|
|
156
|
+
// try twice to get the data out
|
|
157
|
+
for(int count = 0; count < 2; count++)
|
|
158
|
+
{
|
|
159
|
+
int curTickCount = pHeader->varBuf[latest].tickCount;
|
|
160
|
+
memcpy(data, pSharedMem + pHeader->varBuf[latest].bufOffset, pHeader->bufLen);
|
|
161
|
+
if(curTickCount == pHeader->varBuf[latest].tickCount)
|
|
162
|
+
{
|
|
163
|
+
lastTickCount = curTickCount;
|
|
164
|
+
lastValidTime = time(NULL);
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// if here, the data changed out from under us.
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
else
|
|
172
|
+
{
|
|
173
|
+
lastTickCount = pHeader->varBuf[latest].tickCount;
|
|
174
|
+
lastValidTime = time(NULL);
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// if older than last recieved, than reset, we probably disconnected
|
|
179
|
+
else if(lastTickCount > pHeader->varBuf[latest].tickCount)
|
|
180
|
+
{
|
|
181
|
+
lastTickCount = pHeader->varBuf[latest].tickCount;
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
// else the same, and nothing changed this tick
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
bool irsdk_waitForDataReady(int timeOut, char *data)
|
|
192
|
+
{
|
|
193
|
+
#ifdef _MSC_VER
|
|
194
|
+
_ASSERTE(timeOut >= 0);
|
|
195
|
+
#endif
|
|
196
|
+
|
|
197
|
+
if(isInitialized || irsdk_startup())
|
|
198
|
+
{
|
|
199
|
+
// just to be sure, check before we sleep
|
|
200
|
+
if(irsdk_getNewData(data))
|
|
201
|
+
return true;
|
|
202
|
+
|
|
203
|
+
// sleep till signaled
|
|
204
|
+
WaitForSingleObject(hDataValidEvent, timeOut);
|
|
205
|
+
|
|
206
|
+
// we woke up, so check for data
|
|
207
|
+
if(irsdk_getNewData(data))
|
|
208
|
+
return true;
|
|
209
|
+
else
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// sleep if error
|
|
214
|
+
if(timeOut > 0)
|
|
215
|
+
Sleep(timeOut);
|
|
216
|
+
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
bool irsdk_isConnected()
|
|
221
|
+
{
|
|
222
|
+
if(isInitialized)
|
|
223
|
+
{
|
|
224
|
+
int elapsed = (int)difftime(time(NULL), lastValidTime);
|
|
225
|
+
return (pHeader->status & irsdk_stConnected) > 0 && elapsed < timeout;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const irsdk_header *irsdk_getHeader()
|
|
232
|
+
{
|
|
233
|
+
if(isInitialized)
|
|
234
|
+
{
|
|
235
|
+
return pHeader;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return NULL;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// direct access to the data buffer
|
|
242
|
+
// Warnign! This buffer is volitile so read it out fast!
|
|
243
|
+
// Use the cached copy from irsdk_waitForDataReady() or irsdk_getNewData() instead
|
|
244
|
+
const char *irsdk_getData(int index)
|
|
245
|
+
{
|
|
246
|
+
if(isInitialized)
|
|
247
|
+
{
|
|
248
|
+
return pSharedMem + pHeader->varBuf[index].bufOffset;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return NULL;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const char *irsdk_getSessionInfoStr()
|
|
255
|
+
{
|
|
256
|
+
if(isInitialized)
|
|
257
|
+
{
|
|
258
|
+
return pSharedMem + pHeader->sessionInfoOffset;
|
|
259
|
+
}
|
|
260
|
+
return NULL;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
int irsdk_getSessionInfoStrUpdate()
|
|
264
|
+
{
|
|
265
|
+
if(isInitialized)
|
|
266
|
+
{
|
|
267
|
+
return pHeader->sessionInfoUpdate;
|
|
268
|
+
}
|
|
269
|
+
return -1;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const irsdk_varHeader *irsdk_getVarHeaderPtr()
|
|
273
|
+
{
|
|
274
|
+
if(isInitialized)
|
|
275
|
+
{
|
|
276
|
+
return ((irsdk_varHeader*)(pSharedMem + pHeader->varHeaderOffset));
|
|
277
|
+
}
|
|
278
|
+
return NULL;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const irsdk_varHeader *irsdk_getVarHeaderEntry(int index)
|
|
282
|
+
{
|
|
283
|
+
if(isInitialized)
|
|
284
|
+
{
|
|
285
|
+
if(index >= 0 && index < pHeader->numVars)
|
|
286
|
+
{
|
|
287
|
+
return &((irsdk_varHeader*)(pSharedMem + pHeader->varHeaderOffset))[index];
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return NULL;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Note: this is a linear search, so cache the results
|
|
294
|
+
int irsdk_varNameToIndex(const char *name)
|
|
295
|
+
{
|
|
296
|
+
const irsdk_varHeader *pVar;
|
|
297
|
+
|
|
298
|
+
if(name)
|
|
299
|
+
{
|
|
300
|
+
for(int index=0; index<pHeader->numVars; index++)
|
|
301
|
+
{
|
|
302
|
+
pVar = irsdk_getVarHeaderEntry(index);
|
|
303
|
+
if(pVar && 0 == strncmp(name, pVar->name, IRSDK_MAX_STRING))
|
|
304
|
+
{
|
|
305
|
+
return index;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return -1;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
int irsdk_varNameToOffset(const char *name)
|
|
314
|
+
{
|
|
315
|
+
const irsdk_varHeader *pVar;
|
|
316
|
+
|
|
317
|
+
if(name)
|
|
318
|
+
{
|
|
319
|
+
for(int index=0; index<pHeader->numVars; index++)
|
|
320
|
+
{
|
|
321
|
+
pVar = irsdk_getVarHeaderEntry(index);
|
|
322
|
+
if(pVar && 0 == strncmp(name, pVar->name, IRSDK_MAX_STRING))
|
|
323
|
+
{
|
|
324
|
+
return pVar->offset;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return -1;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
unsigned int irsdk_getBroadcastMsgID()
|
|
333
|
+
{
|
|
334
|
+
static unsigned int msgId = RegisterWindowMessage(IRSDK_BROADCASTMSGNAME);
|
|
335
|
+
|
|
336
|
+
return msgId;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
void irsdk_broadcastMsg(irsdk_BroadcastMsg msg, int var1, int var2, int var3)
|
|
340
|
+
{
|
|
341
|
+
irsdk_broadcastMsg(msg, var1, static_cast<int>MAKELONG(var2, var3));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
void irsdk_broadcastMsg(irsdk_BroadcastMsg msg, int var1, float var2)
|
|
345
|
+
{
|
|
346
|
+
// multiply by 2^16-1 to move fractional part to the integer part
|
|
347
|
+
int real = (int)(var2 * 65536.0f);
|
|
348
|
+
|
|
349
|
+
irsdk_broadcastMsg(msg, var1, real);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
void irsdk_broadcastMsg(irsdk_BroadcastMsg msg, int var1, int var2)
|
|
353
|
+
{
|
|
354
|
+
static unsigned int msgId = irsdk_getBroadcastMsgID();
|
|
355
|
+
|
|
356
|
+
if(msgId && msg >= 0 && msg < irsdk_BroadcastLast)
|
|
357
|
+
{
|
|
358
|
+
SendNotifyMessage(HWND_BROADCAST, msgId, MAKELONG(msg, var1), var2);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
int irsdk_padCarNum(int num, int zero)
|
|
363
|
+
{
|
|
364
|
+
int retVal = num;
|
|
365
|
+
int numPlace = 1;
|
|
366
|
+
if(num > 99)
|
|
367
|
+
numPlace = 3;
|
|
368
|
+
else if(num > 9)
|
|
369
|
+
numPlace = 2;
|
|
370
|
+
if(zero)
|
|
371
|
+
{
|
|
372
|
+
numPlace += zero;
|
|
373
|
+
retVal = num + 1000*numPlace;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return retVal;
|
|
377
|
+
}
|