-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimshmbridge.c
340 lines (281 loc) · 8.73 KB
/
simshmbridge.c
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
#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
//#define DEBUG
int pids[4];
#ifdef ASSETTOCORSA
#include "simapi/simapi/ac.h"
#include "simapi/include/acdata.h"
LPCSTR filefind = "acpmf_*";
// define a single file to use as a test to see if the game has initialized the shared memory
LPCSTR file1 = AC_PHYSICS_FILE;
int numfiles = 4;
const char* files[] =
{
AC_PHYSICS_FILE,
AC_STATIC_FILE,
AC_GRAPHIC_FILE,
AC_CREWCHIEF_FILE
};
typedef struct SPageFilePhysics SharedMemory1;
size_t getmemfilesize(const char* filename)
{
if(strcmp(filename, AC_PHYSICS_FILE) == 0)
{
return sizeof(struct SPageFilePhysics);
}
if(strcmp(filename, AC_STATIC_FILE) == 0)
{
return sizeof(struct SPageFileStatic);
}
if(strcmp(filename, AC_GRAPHIC_FILE) == 0)
{
return sizeof(struct SPageFileGraphic);
}
if(strcmp(filename, AC_CREWCHIEF_FILE) == 0)
{
return sizeof(struct SPageFileCrewChief);
}
if(strcmp(filename, "acpmf_secondMonitor") == 0)
{
return sizeof(struct SPageFileCrewChief);
}
return 4096;
}
#endif
#ifdef RFACTOR2
#include "simapi/simapi/rf2.h"
#include "simapi/include/rf2data.h"
LPCSTR filefind = "$rFactor2*";
typedef struct rF2Scoring SharedMemory1;
size_t getmemfilesize(const char* filename)
{
if(strcmp(filename, RF2_TELEMETRY_FILE) == 0)
{
return sizeof(struct rF2Telemetry);
}
if(strcmp(filename, RF2_SCORING_FILE) == 0)
{
return sizeof(struct rF2Scoring);
}
return 4096;
}
#endif
#ifdef PROJECTCARS2
#include "simapi/simapi/pcars2.h"
#include "simapi/include/pcars2data.h"
typedef struct pcars2APIStruct SharedMemory1;
LPCSTR filefind = PCARS2_FILE;
LPCSTR file1 = PCARS2_FILE;
int numfiles = 1;
const char* files[] =
{
PCARS2_FILE,
};
size_t getmemfilesize(const char* filename)
{
if(strcmp(filename, PCARS2_FILE) == 0)
{
return sizeof(struct pcars2APIStruct);
}
return 10000;
}
#endif
void hexDump(char *desc, void *addr, int len)
{
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char*)addr;
// Output description if given.
if (desc != NULL)
printf ("%s:\n", desc);
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf(" %s\n", buff);
// Output the offset.
printf(" %04x ", i);
}
// Now the hex code for the specific character.
printf(" %02x", pc[i]);
// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e)) {
buff[i % 16] = '.';
} else {
buff[i % 16] = pc[i];
}
buff[(i % 16) + 1] = '\0';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf(" ");
i++;
}
// And print the final ASCII bit.
printf(" %s\n", buff);
}
#ifdef HELPERPROCESSFIRST
int main(int argc, char** argv)
{
WIN32_FIND_DATA fdata;
HANDLE ffhandle;
if (chdir("/dev/shm") != 0)
{
printf("Could not change directory to /dev/shm: %s\n", strerror(errno));
return 1;
}
// TODO: change this to a loop until it's up and running
if ((ffhandle = FindFirstFile(filefind, &fdata)) == INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
if (err == 2) {
printf("Could not find any OVR SHM objects: is service running?\n");
} else {
printf("FindFirstFile error: %s\n", strerror(err));
}
return 1;
}
void* pMem;
void* pMem2;
HANDLE maph;
HANDLE mapha;
SharedMemory1* a;
SharedMemory1* b;
do
{
HANDLE fd = CreateFile(fdata.cFileName, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fd == INVALID_HANDLE_VALUE)
{
printf("warning: could not open %s: %s\n", fdata.cFileName, strerror(GetLastError()));
continue;
}
int filesize = getmemfilesize(fdata.cFileName);
maph = CreateFileMapping(fd, NULL, PAGE_EXECUTE_READWRITE, 0, filesize, fdata.cFileName);
if (maph == NULL)
{
printf("warning: could not create mapping for %s: %s\n", fdata.cFileName, strerror(GetLastError()));
CloseHandle(fd);
continue;
}
printf("Bridged /dev/shm/%s of %i size to Win32 named mapping \"%s\"\n", fdata.cFileName, filesize, fdata.cFileName);
#ifdef DEBUG
pMem = (unsigned char*)MapViewOfFile(maph, FILE_MAP_READ, 0, 0, filesize);
#endif
}
while (FindNextFile(ffhandle, &fdata));
printf("Done! Sleeping forever to keep objects alive, press Shift-E to stop\n");
#ifndef DEBUG
int key = 0;
while(1)
{
if (_kbhit())
{
key =_getch();
if (key == 'E')
break;
}
#endif
#ifdef DEBUG
for (;;)
{
a = malloc(sizeof(SharedMemory1));
hexDump( NULL, a, sizeof(SharedMemory1));
#endif
}
return 0;
}
#endif
#ifdef HELPERPROCESSSECOND
int main(int argc, char** argv) {
char *access_mode;
DWORD access = 0;
STARTUPINFO si;
PROCESS_INFORMATION pi[numfiles];
HANDLE shmhandles[numfiles];
HANDLE maph = NULL;
fprintf(stderr, "Waiting to map first shared memory file.");
while(maph == NULL) {
maph = OpenFileMapping(FILE_MAP_READ, FALSE, file1);
Sleep(3000);
}
CloseHandle(maph);
//maph = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, FILE_MAP_READ, 0, sizeof(SharedMemory1), file1);
//fprintf(stderr, "Mapped first shared memory file, mapping any necessary remaining shared memory files.\n");
#ifdef DEBUG
unsigned char* mapfb;
mapfb = (unsigned char*)MapViewOfFile(maph, FILE_MAP_READ, 0, 0, sizeof(SharedMemory1));
SharedMemory1* b = malloc(sizeof(SharedMemory1));
b = (SharedMemory1*)mapfb;
//fprintf(stderr, "buf contains: %d\n", b->mNumVehicles);
//fprintf(stderr, "buf contains: %d\n", b->mVehicles[0].mLapNumber);
//fprintf(stderr, "buf contains: %lf\n", b->mVehicles[0].mLocalVel.x);
//fprintf(stderr, "buf contains: %lf\n", b->mVehicles[0].mLocalVel.y);
//fprintf(stderr, "buf contains: %lf\n", b->mVehicles[0].mLocalVel.z);
//fprintf(stderr, "buf contains: %d\n", b->mVehicles[0].mGear);
//fprintf(stderr, "buf contains: %lf\n", b->mVehicles[0].mEngineRPM);
fprintf(stderr, "buf contains: %i\n", b->mVersion);
fprintf(stderr, "buf contains: %i\n", b->mBuildVersionNumber);
fprintf(stderr, "buf contains: %i\n", b->mGameState);
fprintf(stderr, "buf contains: %i\n", b->mSessionState);
fprintf(stderr, "buf contains: %i\n", b->mRaceState);
FILE *outfile;
//outfile = fopen ("/home/racerx/telemetry.dat", "w");
//if (outfile == NULL)
//{
// fprintf(stderr, "\nError opened file\n");
// exit (1);
//}
//fwrite (b, sizeof(SharedMemory1), 1, outfile);
//if(fwrite != 0)
// printf("contents to file written successfully !\n");
//else
// printf("error writing file !\n");
//fclose (outfile);
#endif
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.hStdInput = maph;
si.dwFlags |= STARTF_USESTDHANDLES;
for(int i = 0; i < numfiles; i++)
{
maph = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, FILE_MAP_READ, 0, getmemfilesize(files[i]), files[i]);
ZeroMemory(&pi[i], sizeof(PROCESS_INFORMATION));
size_t size = snprintf(NULL, 0, "%s %s", argv[1], files[i]);
char* ProcessString = malloc(size + 1);
sprintf(ProcessString, "%s %s", argv[1], files[i]);
SetStdHandle(STD_INPUT_HANDLE, maph);
si.hStdInput = maph;
shmhandles[i] = maph;
CreateProcess(NULL, ProcessString, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi[i]);
fprintf(stderr, "started process %s at pid %li\n", ProcessString, pi[i].dwProcessId);
free(ProcessString);
}
SetStdHandle(STD_INPUT_HANDLE, "CONIN$");
fprintf(stderr, "Mapped and sleeping forever Press Shift-E to stop ( may need to press more than once until it stops )\n");
int key = 0;
while(1)
{
if (_kbhit())
{
key =_getch();
if (key == 'E')
break;
}
}
for(int i = 0; i < numfiles; i++)
{
CloseHandle(shmhandles[i]);
TerminateProcess(pi[i].hProcess, 0);
CloseHandle(pi[i].hProcess);
CloseHandle(pi[i].hThread);
}
return 0;
}
#endif