-
Notifications
You must be signed in to change notification settings - Fork 8
/
irsdk_diskclient.cpp
396 lines (343 loc) · 8.39 KB
/
irsdk_diskclient.cpp
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
/*
Copyright (c) 2013, iRacing.com Motorsport Simulations, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of iRacing.com Motorsport Simulations nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "irsdk_defines.h"
#include "yaml_parser.h"
#include "irsdk_diskclient.h"
#pragma warning(disable:4996)
bool irsdkDiskClient::openFile(const char *path)
{
closeFile();
m_ibtFile = fopen(path, "rb");
if(m_ibtFile)
{
if(fread(&m_header, 1, sizeof(m_header), m_ibtFile) == sizeof(m_header))
{
if(fread(&m_diskSubHeader, 1, sizeof(m_diskSubHeader), m_ibtFile) == sizeof(m_diskSubHeader))
{
m_sessionInfoString = new char[m_header.sessionInfoLen];
if(m_sessionInfoString)
{
fseek(m_ibtFile, m_header.sessionInfoOffset, SEEK_SET);
if(fread(m_sessionInfoString, 1, m_header.sessionInfoLen, m_ibtFile) == (size_t)m_header.sessionInfoLen)
{
m_sessionInfoString[m_header.sessionInfoLen-1] = '\0';
m_varHeaders = new irsdk_varHeader[m_header.numVars];
if(m_varHeaders)
{
fseek(m_ibtFile, m_header.varHeaderOffset, SEEK_SET);
size_t len = m_header.numVars * sizeof(irsdk_varHeader);
if(fread(m_varHeaders, 1, len, m_ibtFile) == len)
{
m_varBuf = new char[m_header.bufLen];
if(m_varBuf)
{
fseek(m_ibtFile, m_header.varBuf[0].bufOffset, SEEK_SET);
return true;
//delete [] m_varBuf;
//m_varBuf = NULL;
}
}
delete [] m_varHeaders;
m_varHeaders = NULL;
}
}
delete [] m_sessionInfoString;
m_sessionInfoString = NULL;
}
}
}
fclose(m_ibtFile);
m_ibtFile = NULL;
}
return false;
}
void irsdkDiskClient::closeFile()
{
if(m_varBuf)
delete [] m_varBuf;
m_varBuf = NULL;
if(m_varHeaders)
delete [] m_varHeaders;
m_varHeaders = NULL;
if(m_sessionInfoString)
delete [] m_sessionInfoString;
m_sessionInfoString = NULL;
if(m_ibtFile)
fclose(m_ibtFile);
m_ibtFile = NULL;
}
bool irsdkDiskClient::getNextData()
{
if(m_ibtFile)
return fread(m_varBuf, 1, m_header.bufLen, m_ibtFile) == (size_t)m_header.bufLen;
return false;
}
int irsdkDiskClient::getVarIdx(const char *name)
{
if(m_ibtFile && name)
{
for(int idx=0; idx<m_header.numVars; idx++)
{
if(0 == strncmp(name, m_varHeaders[idx].name, IRSDK_MAX_STRING))
{
return idx;
}
}
}
return -1;
}
irsdk_VarType irsdkDiskClient::getVarType(int idx)
{
if(m_ibtFile)
{
if(idx >= 0 && idx < m_header.numVars)
{
return (irsdk_VarType)m_varHeaders[idx].type;
}
//invalid variable index
assert(false);
}
return irsdk_char;
}
int irsdkDiskClient::getVarCount(int idx)
{
if(m_ibtFile)
{
if(idx >= 0 && idx < m_header.numVars)
{
return m_varHeaders[idx].count;
}
//invalid variable index
assert(false);
}
return 0;
}
bool irsdkDiskClient::getVarBool(int idx, int entry)
{
if(m_ibtFile)
{
if(idx >= 0 && idx < m_header.numVars)
{
if(entry >= 0 && entry < m_varHeaders[idx].count)
{
const char * data = m_varBuf + m_varHeaders[idx].offset;
switch(m_varHeaders[idx].type)
{
// 1 byte
case irsdk_char:
case irsdk_bool:
return (((const char*)data)[entry]) != 0;
break;
// 4 bytes
case irsdk_int:
case irsdk_bitField:
return (((const int*)data)[entry]) != 0;
break;
// test float/double for greater than 1.0 so that
// we have a chance of this being usefull
// technically there is no right conversion...
case irsdk_float:
return (((const float*)data)[entry]) >= 1.0f;
break;
// 8 bytes
case irsdk_double:
return (((const double*)data)[entry]) >= 1.0;
break;
}
}
else
{
// invalid offset
assert(false);
}
}
else
{
//invalid variable index
assert(false);
}
}
return false;
}
int irsdkDiskClient::getVarInt(int idx, int entry)
{
if(m_ibtFile)
{
if(idx >= 0 && idx < m_header.numVars)
{
if(entry >= 0 && entry < m_varHeaders[idx].count)
{
const char * data = m_varBuf + m_varHeaders[idx].offset;
switch(m_varHeaders[idx].type)
{
// 1 byte
case irsdk_char:
case irsdk_bool:
return (int)(((const char*)data)[entry]);
break;
// 4 bytes
case irsdk_int:
case irsdk_bitField:
return (int)(((const int*)data)[entry]);
break;
case irsdk_float:
return (int)(((const float*)data)[entry]);
break;
// 8 bytes
case irsdk_double:
return (int)(((const double*)data)[entry]);
break;
}
}
else
{
// invalid offset
assert(false);
}
}
else
{
//invalid variable index
assert(false);
}
}
return 0;
}
float irsdkDiskClient::getVarFloat(int idx, int entry)
{
if(m_ibtFile)
{
if(idx >= 0 && idx < m_header.numVars)
{
if(entry >= 0 && entry < m_varHeaders[idx].count)
{
const char * data = m_varBuf + m_varHeaders[idx].offset;
switch(m_varHeaders[idx].type)
{
// 1 byte
case irsdk_char:
case irsdk_bool:
return (float)(((const char*)data)[entry]);
break;
// 4 bytes
case irsdk_int:
case irsdk_bitField:
return (float)(((const int*)data)[entry]);
break;
case irsdk_float:
return (float)(((const float*)data)[entry]);
break;
// 8 bytes
case irsdk_double:
return (float)(((const double*)data)[entry]);
break;
}
}
else
{
// invalid offset
assert(false);
}
}
else
{
//invalid variable index
assert(false);
}
}
return 0.0f;
}
double irsdkDiskClient::getVarDouble(int idx, int entry)
{
if(m_ibtFile)
{
if(idx >= 0 && idx < m_header.numVars)
{
if(entry >= 0 && entry < m_varHeaders[idx].count)
{
const char * data = m_varBuf + m_varHeaders[idx].offset;
switch(m_varHeaders[idx].type)
{
// 1 byte
case irsdk_char:
case irsdk_bool:
return (double)(((const char*)data)[entry]);
break;
// 4 bytes
case irsdk_int:
case irsdk_bitField:
return (double)(((const int*)data)[entry]);
break;
case irsdk_float:
return (double)(((const float*)data)[entry]);
break;
// 8 bytes
case irsdk_double:
return (double)(((const double*)data)[entry]);
break;
}
}
else
{
// invalid offset
assert(false);
}
}
else
{
//invalid variable index
assert(false);
}
}
return 0.0;
}
//path is in the form of "DriverInfo:Drivers:CarIdx:{%d}UserName:"
int irsdkDiskClient::getSessionStrVal(const char *path, char *val, int valLen)
{
if(m_ibtFile && path && val && valLen > 0)
{
const char *tVal = NULL;
int tValLen = 0;
if(parseYaml(m_sessionInfoString, path, &tVal, &tValLen))
{
// dont overflow out buffer
int len = tValLen;
if(len > valLen)
len = valLen;
// copy what we can, even if buffer too small
memcpy(val, tVal, len);
val[len] = '\0'; // origional string has no null termination...
// if buffer was big enough, return success
if(valLen >= tValLen)
return 1;
else // return size of buffer needed
return -tValLen;
}
}
return 0;
}