Skip to content

Latest commit

 

History

History
670 lines (566 loc) · 19.9 KB

steamshim_parent.cpp

File metadata and controls

670 lines (566 loc) · 19.9 KB
 
Jul 25, 2013
Jul 25, 2013
1
#define GAME_LAUNCH_NAME "testapp"
Jul 25, 2013
Jul 25, 2013
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
#ifndef GAME_LAUNCH_NAME
#error Please define your game exe name.
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
typedef PROCESS_INFORMATION ProcessType;
typedef HANDLE PipeType;
#define NULLPIPE NULL
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <signal.h>
typedef pid_t ProcessType;
typedef int PipeType;
#define NULLPIPE -1
#endif
#include "steam/steam_api.h"
Jul 25, 2013
Jul 25, 2013
27
28
29
30
31
32
33
#define DEBUGPIPE 1
#if DEBUGPIPE
#define dbgpipe printf
#else
static inline void dbgpipe(const char *fmt, ...) {}
#endif
Jul 25, 2013
Jul 25, 2013
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
/* platform-specific mainline calls this. */
static int mainline(void);
/* Windows and Unix implementations of this stuff below. */
static void fail(const char *err);
static bool writePipe(PipeType fd, const void *buf, const unsigned int _len);
static int readPipe(PipeType fd, void *buf, const unsigned int _len);
static bool createPipes(PipeType *pPipeParentRead, PipeType *pPipeParentWrite,
PipeType *pPipeChildRead, PipeType *pPipeChildWrite);
static void closePipe(PipeType fd);
static bool setEnvVar(const char *key, const char *val);
static bool launchChild(ProcessType *pid);
static int closeProcess(ProcessType *pid);
#ifdef _WIN32
static void fail(const char *err)
{
MessageBoxA(NULL, err, "ERROR", MB_ICONERROR | MB_OK);
ExitProcess(1);
} // fail
static bool writePipe(PipeType fd, const void *buf, const unsigned int _len)
{
const DWORD len = (DWORD) _len;
DWORD bw = 0;
return ((WriteFile(fd, buf, len, &bw, NULL) != 0) && (bw == len));
} // writePipe
static int readPipe(PipeType fd, void *buf, const unsigned int _len)
{
const DWORD len = (DWORD) _len;
DWORD br = 0;
return ReadFile(fd, buf, len, &br, NULL) ? (int) br : -1;
} // readPipe
static bool createPipes(PipeType *pPipeParentRead, PipeType *pPipeParentWrite,
PipeType *pPipeChildRead, PipeType *pPipeChildWrite)
{
SECURITY_ATTRIBUTES pipeAttr;
pipeAttr.nLength = sizeof (pipeAttr);
pipeAttr.lpSecurityDescriptor = NULL;
pipeAttr.bInheritHandle = TRUE;
if (!CreatePipe(pPipeParentRead, pPipeChildWrite, &pipeAttr, 0))
return 0;
pipeAttr.nLength = sizeof (pipeAttr);
pipeAttr.lpSecurityDescriptor = NULL;
pipeAttr.bInheritHandle = TRUE;
if (!CreatePipe(pPipeChildRead, pPipeParentWrite, &pipeAttr, 0))
{
CloseHandle(*pPipeParentRead);
CloseHandle(*pPipeChildWrite);
return 0;
} // if
return 1;
} // createPipes
static void closePipe(PipeType fd)
{
CloseHandle(fd);
} // closePipe
static bool setEnvVar(const char *key, const char *val)
{
return (SetEnvironmentVariableA(key, val) != 0);
} // setEnvVar
static bool launchChild(ProcessType *pid);
{
return (CreateProcessW(TEXT(".\\") TEXT(GAME_LAUNCH_NAME) TEXT(".exe"),
GetCommandLineW(), NULL, NULL, TRUE, 0, NULL,
NULL, NULL, pid) != 0);
} // launchChild
static int closeProcess(ProcessType *pid)
{
CloseHandle(pid->hProcess);
CloseHandle(pid->hThread);
return 0;
} // closeProcess
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
mainline();
ExitProcess(0);
return 0; // just in case.
} // WinMain
#else // everyone else that isn't Windows.
static void fail(const char *err)
{
// !!! FIXME: zenity or something.
fprintf(stderr, "%s\n", err);
_exit(1);
} // fail
static bool writePipe(PipeType fd, const void *buf, const unsigned int _len)
{
const ssize_t len = (ssize_t) _len;
ssize_t bw;
while (((bw = write(fd, buf, len)) == -1) && (errno == EINTR)) { /*spin*/ }
return (bw == len);
} // writePipe
static int readPipe(PipeType fd, void *buf, const unsigned int _len)
{
const ssize_t len = (ssize_t) _len;
ssize_t br;
while (((br = read(fd, buf, len)) == -1) && (errno == EINTR)) { /*spin*/ }
return (int) br;
} // readPipe
static bool createPipes(PipeType *pPipeParentRead, PipeType *pPipeParentWrite,
PipeType *pPipeChildRead, PipeType *pPipeChildWrite)
{
int fds[2];
if (pipe(fds) == -1)
return 0;
*pPipeParentRead = fds[0];
*pPipeChildWrite = fds[1];
if (pipe(fds) == -1)
{
close(*pPipeParentRead);
close(*pPipeChildWrite);
return 0;
} // if
*pPipeChildRead = fds[0];
*pPipeParentWrite = fds[1];
return 1;
} // createPipes
static void closePipe(PipeType fd)
{
close(fd);
} // closePipe
static bool setEnvVar(const char *key, const char *val)
{
return (setenv(key, val, 1) != -1);
} // setEnvVar
static int GArgc = 0;
static char **GArgv = NULL;
static bool launchChild(ProcessType *pid)
{
*pid = fork();
if (*pid == -1) // failed
return false;
else if (*pid != 0) // we're the parent
return true; // we'll let the pipe fail if this didn't work.
// we're the child.
GArgv[0] = strdup("./" GAME_LAUNCH_NAME);
execvp(GArgv[0], GArgv);
// still here? It failed! Terminate, closing child's ends of the pipes.
_exit(1);
} // launchChild
static int closeProcess(ProcessType *pid)
{
Jul 25, 2013
Jul 25, 2013
203
int rc = 0;
Jul 25, 2013
Jul 25, 2013
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
while ((waitpid(*pid, &rc, 0) == -1) && (errno == EINTR)) { /*spin*/ }
if (!WIFEXITED(rc))
return 1; // oh well.
return WEXITSTATUS(rc);
} // closeProcess
int main(int argc, char **argv)
{
signal(SIGPIPE, SIG_IGN);
GArgc = argc;
GArgv = argv;
return mainline();
} // main
#endif
// THE ACTUAL PROGRAM.
Jul 25, 2013
Jul 25, 2013
223
class SteamBridge;
Jul 25, 2013
Jul 25, 2013
224
Jul 25, 2013
Jul 25, 2013
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
static ISteamUserStats *GSteamStats = NULL;
static ISteamUtils *GSteamUtils = NULL;
static ISteamUser *GSteamUser = NULL;
static AppId_t GAppID = 0;
static uint64 GUserID = 0;
static SteamBridge *GSteamBridge = NULL;
class SteamBridge
{
public:
SteamBridge(PipeType _fd);
STEAM_CALLBACK(SteamBridge, OnUserStatsReceived, UserStatsReceived_t, m_CallbackUserStatsReceived);
STEAM_CALLBACK(SteamBridge, OnUserStatsStored, UserStatsStored_t, m_CallbackUserStatsStored);
private:
PipeType fd;
};
typedef enum ShimCmd
Jul 25, 2013
Jul 25, 2013
244
{
Jul 25, 2013
Jul 25, 2013
245
246
247
248
SHIMCMD_BYE,
SHIMCMD_PUMP,
SHIMCMD_REQUESTSTATS,
SHIMCMD_STORESTATS,
Jul 25, 2013
Jul 25, 2013
249
250
SHIMCMD_SETACHIEVEMENT,
SHIMCMD_GETACHIEVEMENT,
Jul 25, 2013
Jul 25, 2013
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
SHIMCMD_RESETSTATS,
SHIMCMD_SETSTATI,
SHIMCMD_GETSTATI,
SHIMCMD_SETSTATF,
SHIMCMD_GETSTATF,
} ShimCmd;
typedef enum ShimEvent
{
SHIMEVENT_BYE,
SHIMEVENT_STATSRECEIVED,
SHIMEVENT_STATSSTORED,
SHIMEVENT_SETACHIEVEMENT,
SHIMEVENT_GETACHIEVEMENT,
SHIMEVENT_RESETSTATS,
SHIMEVENT_SETSTATI,
SHIMEVENT_GETSTATI,
SHIMEVENT_SETSTATF,
SHIMEVENT_GETSTATF,
} ShimEvent;
static bool write1ByteCmd(PipeType fd, const uint8 b1)
{
const uint8 buf[] = { 1, b1 };
return writePipe(fd, buf, sizeof (buf));
} // write1ByteCmd
static bool write2ByteCmd(PipeType fd, const uint8 b1, const uint8 b2)
{
const uint8 buf[] = { 2, b1, b2 };
return writePipe(fd, buf, sizeof (buf));
} // write2ByteCmd
static bool write3ByteCmd(PipeType fd, const uint8 b1, const uint8 b2, const uint8 b3)
{
const uint8 buf[] = { 3, b1, b2, b3 };
return writePipe(fd, buf, sizeof (buf));
} // write3ByteCmd
Jul 25, 2013
Jul 25, 2013
290
Jul 25, 2013
Jul 25, 2013
291
292
static inline bool writeBye(PipeType fd)
{
Jul 25, 2013
Jul 25, 2013
293
dbgpipe("Parent sending SHIMEVENT_BYE().\n");
Jul 25, 2013
Jul 25, 2013
294
295
296
297
298
return write1ByteCmd(fd, SHIMEVENT_BYE);
} // writeBye
static inline bool writeStatsReceived(PipeType fd, const bool okay)
{
Jul 25, 2013
Jul 25, 2013
299
dbgpipe("Parent sending SHIMEVENT_STATSRECEIVED(%sokay).\n", okay ? "" : "!");
Jul 25, 2013
Jul 25, 2013
300
301
302
303
304
return write2ByteCmd(fd, SHIMEVENT_STATSRECEIVED, okay ? 1 : 0);
} // writeStatsReceived
static inline bool writeStatsStored(PipeType fd, const bool okay)
{
Jul 25, 2013
Jul 25, 2013
305
dbgpipe("Parent sending SHIMEVENT_STATSSTORED(%sokay).\n", okay ? "" : "!");
Jul 25, 2013
Jul 25, 2013
306
307
308
309
310
311
312
return write2ByteCmd(fd, SHIMEVENT_STATSSTORED, okay ? 1 : 0);
} // writeStatsStored
static bool writeAchievementSet(PipeType fd, const char *name, const bool enable, const bool okay)
{
uint8 buf[256];
uint8 *ptr = buf+1;
Jul 25, 2013
Jul 25, 2013
313
dbgpipe("Parent sending SHIMEVENT_SETACHIEVEMENT('%s', %senable, %sokay).\n", name, enable ? "" : "!", okay ? "" : "!");
Jul 25, 2013
Jul 25, 2013
314
315
316
317
318
319
320
321
322
323
324
325
326
*(ptr++) = (uint8) SHIMEVENT_SETACHIEVEMENT;
*(ptr++) = enable ? 1 : 0;
*(ptr++) = okay ? 1 : 0;
strcpy((char *) ptr, name);
ptr += strlen(name) + 1;
buf[0] = (uint8) ((ptr-1) - buf);
return writePipe(fd, buf, buf[0] + 1);
} // writeAchievementSet
static bool writeAchievementGet(PipeType fd, const char *name, const int status, const uint64 time)
{
uint8 buf[256];
uint8 *ptr = buf+1;
Jul 25, 2013
Jul 25, 2013
327
dbgpipe("Parent sending SHIMEVENT_GETACHIEVEMENT('%s', status %d, time %llu).\n", name, status, (unsigned long long) time);
Jul 25, 2013
Jul 25, 2013
328
329
330
331
332
333
334
335
336
337
338
339
*(ptr++) = (uint8) SHIMEVENT_GETACHIEVEMENT;
*(ptr++) = (uint8) status;
memcpy(ptr, &time, sizeof (time));
ptr += sizeof (time);
strcpy((char *) ptr, name);
ptr += strlen(name) + 1;
buf[0] = (uint8) ((ptr-1) - buf);
return writePipe(fd, buf, buf[0] + 1);
} // writeAchievementGet
static inline bool writeResetStats(PipeType fd, const bool alsoAch, const bool okay)
{
Jul 25, 2013
Jul 25, 2013
340
dbgpipe("Parent sending SHIMEVENT_RESETSTATS(%salsoAchievements, %sokay).\n", alsoAch ? "" : "!", okay ? "" : "!");
Jul 25, 2013
Jul 25, 2013
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
return write3ByteCmd(fd, SHIMEVENT_RESETSTATS, alsoAch ? 1 : 0, okay ? 1 : 0);
} // writeResetStats
static bool writeStatThing(PipeType fd, const ShimEvent ev, const char *name, const void *val, const size_t vallen, const bool okay)
{
uint8 buf[256];
uint8 *ptr = buf+1;
*(ptr++) = (uint8) ev;
*(ptr++) = okay ? 1 : 0;
memcpy(ptr, val, vallen);
ptr += vallen;
strcpy((char *) ptr, name);
ptr += strlen(name) + 1;
buf[0] = (uint8) ((ptr-1) - buf);
return writePipe(fd, buf, buf[0] + 1);
} // writeStatThing
static inline bool writeSetStatI(PipeType fd, const char *name, const int32 val, const bool okay)
{
Jul 25, 2013
Jul 25, 2013
360
dbgpipe("Parent sending SHIMEVENT_SETSTATI('%s', val %d, %sokay).\n", name, (int) val, okay ? "" : "!");
Jul 25, 2013
Jul 25, 2013
361
362
return writeStatThing(fd, SHIMEVENT_SETSTATI, name, &val, sizeof (val), okay);
} // writeSetStatI
Jul 25, 2013
Jul 25, 2013
363
Jul 25, 2013
Jul 25, 2013
364
static inline bool writeSetStatF(PipeType fd, const char *name, const float val, const bool okay)
Jul 25, 2013
Jul 25, 2013
365
{
Jul 25, 2013
Jul 25, 2013
366
dbgpipe("Parent sending SHIMEVENT_SETSTATF('%s', val %f, %sokay).\n", name, val, okay ? "" : "!");
Jul 25, 2013
Jul 25, 2013
367
368
return writeStatThing(fd, SHIMEVENT_SETSTATF, name, &val, sizeof (val), okay);
} // writeSetStatF
Jul 25, 2013
Jul 25, 2013
369
Jul 25, 2013
Jul 25, 2013
370
371
static inline bool writeGetStatI(PipeType fd, const char *name, const int32 val, const bool okay)
{
Jul 25, 2013
Jul 25, 2013
372
dbgpipe("Parent sending SHIMEVENT_GETSTATI('%s', val %d, %sokay).\n", name, (int) val, okay ? "" : "!");
Jul 25, 2013
Jul 25, 2013
373
374
375
376
377
return writeStatThing(fd, SHIMEVENT_GETSTATI, name, &val, sizeof (val), okay);
} // writeGetStatI
static inline bool writeGetStatF(PipeType fd, const char *name, const float val, const bool okay)
{
Jul 25, 2013
Jul 25, 2013
378
dbgpipe("Parent sending SHIMEVENT_GETSTATF('%s', val %f, %sokay).\n", name, val, okay ? "" : "!");
Jul 25, 2013
Jul 25, 2013
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
return writeStatThing(fd, SHIMEVENT_GETSTATF, name, &val, sizeof (val), okay);
} // writeGetStatF
SteamBridge::SteamBridge(PipeType _fd)
: m_CallbackUserStatsReceived( this, &SteamBridge::OnUserStatsReceived )
, m_CallbackUserStatsStored( this, &SteamBridge::OnUserStatsStored )
, fd(_fd)
{
} // SteamBridge::SteamBridge
void SteamBridge::OnUserStatsReceived(UserStatsReceived_t *pCallback)
{
if (GAppID != pCallback->m_nGameID) return;
if (GUserID != pCallback->m_steamIDUser.ConvertToUint64()) return;
writeStatsReceived(fd, pCallback->m_eResult == k_EResultOK);
} // SteamBridge::OnUserStatsReceived
void SteamBridge::OnUserStatsStored(UserStatsStored_t *pCallback)
{
if (GAppID != pCallback->m_nGameID) return;
writeStatsStored(fd, pCallback->m_eResult == k_EResultOK);
} // SteamBridge::OnUserStatsStored
static bool processCommand(const uint8 *buf, unsigned int buflen, PipeType fd)
{
Jul 25, 2013
Jul 25, 2013
407
if (buflen == 0)
Jul 25, 2013
Jul 25, 2013
408
409
410
411
return true;
const ShimCmd cmd = (ShimCmd) *(buf++);
buflen--;
Jul 25, 2013
Jul 25, 2013
412
Jul 25, 2013
Jul 25, 2013
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#if DEBUGPIPE
if (false) {}
#define PRINTGOTCMD(x) else if (cmd == x) printf("Parent got " #x ".\n")
PRINTGOTCMD(SHIMCMD_BYE);
PRINTGOTCMD(SHIMCMD_PUMP);
PRINTGOTCMD(SHIMCMD_REQUESTSTATS);
PRINTGOTCMD(SHIMCMD_STORESTATS);
PRINTGOTCMD(SHIMCMD_SETACHIEVEMENT);
PRINTGOTCMD(SHIMCMD_GETACHIEVEMENT);
PRINTGOTCMD(SHIMCMD_RESETSTATS);
PRINTGOTCMD(SHIMCMD_SETSTATI);
PRINTGOTCMD(SHIMCMD_GETSTATI);
PRINTGOTCMD(SHIMCMD_SETSTATF);
PRINTGOTCMD(SHIMCMD_GETSTATF);
#undef PRINTGOTCMD
else printf("Parent got unknown shimcmd %d.\n", (int) cmd);
#endif
Jul 25, 2013
Jul 25, 2013
431
432
switch (cmd)
{
Jul 25, 2013
Jul 25, 2013
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
case SHIMCMD_PUMP:
SteamAPI_RunCallbacks();
break;
case SHIMCMD_BYE:
writeBye(fd);
return false;
case SHIMCMD_REQUESTSTATS:
if ((!GSteamStats) || (!GSteamStats->RequestCurrentStats()))
writeStatsReceived(fd, false);
// callback later.
break;
case SHIMCMD_STORESTATS:
if ((!GSteamStats) || (!GSteamStats->StoreStats()))
writeStatsStored(fd, false);
// callback later.
break;
case SHIMCMD_SETACHIEVEMENT:
if (buflen >= 2)
{
const bool enable = (*(buf++) != 0);
const char *name = (const char *) buf; // !!! FIXME: buffer overflow possible.
if (!GSteamStats)
writeAchievementSet(fd, name, enable, false);
else if (enable && !GSteamStats->SetAchievement(name))
writeAchievementSet(fd, name, enable, false);
else if (!enable && !GSteamStats->ClearAchievement(name))
writeAchievementSet(fd, name, enable, false);
else
writeAchievementSet(fd, name, enable, true);
} // if
break;
case SHIMCMD_GETACHIEVEMENT:
if (buflen)
{
const char *name = (const char *) buf; // !!! FIXME: buffer overflow possible.
bool ach = false;
uint32 t = 0;
if ((GSteamStats) && (GSteamStats->GetAchievementAndUnlockTime(name, &ach, &t)))
writeAchievementGet(fd, name, ach ? 1 : 0, t);
else
Jul 25, 2013
Jul 25, 2013
478
writeAchievementGet(fd, name, 2, 0);
Jul 25, 2013
Jul 25, 2013
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
} // if
break;
case SHIMCMD_RESETSTATS:
if (buflen)
{
const bool alsoAch = (*(buf++) != 0);
writeResetStats(fd, alsoAch, (GSteamStats) && (GSteamStats->ResetAllStats(alsoAch)));
} // if
break;
case SHIMCMD_SETSTATI:
if (buflen >= 5)
{
const int32 val = *((int32 *) buf);
buf += sizeof (int32);
const char *name = (const char *) buf; // !!! FIXME: buffer overflow possible.
writeSetStatI(fd, name, val, (GSteamStats) && (GSteamStats->SetStat(name, val)));
} // if
break;
case SHIMCMD_GETSTATI:
if (buflen)
{
const char *name = (const char *) buf; // !!! FIXME: buffer overflow possible.
int32 val = 0;
if ((GSteamStats) && (GSteamStats->GetStat(name, &val)))
Jul 25, 2013
Jul 25, 2013
506
writeGetStatI(fd, name, val, true);
Jul 25, 2013
Jul 25, 2013
507
else
Jul 25, 2013
Jul 25, 2013
508
writeGetStatI(fd, name, 0, false);
Jul 25, 2013
Jul 25, 2013
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
} // if
break;
case SHIMCMD_SETSTATF:
if (buflen >= 5)
{
const float val = *((float *) buf);
buf += sizeof (float);
const char *name = (const char *) buf; // !!! FIXME: buffer overflow possible.
writeSetStatF(fd, name, val, (GSteamStats) && (GSteamStats->SetStat(name, val)));
} // if
break;
case SHIMCMD_GETSTATF:
if (buflen)
{
const char *name = (const char *) buf; // !!! FIXME: buffer overflow possible.
float val = 0;
if ((GSteamStats) && (GSteamStats->GetStat(name, &val)))
writeGetStatF(fd, name, val, true);
else
writeGetStatF(fd, name, 0.0f, false);
} // if
break;
Jul 25, 2013
Jul 25, 2013
533
} // switch
Jul 25, 2013
Jul 25, 2013
534
535
return true; // keep going.
Jul 25, 2013
Jul 25, 2013
536
537
538
539
} // processCommand
static void processCommands(PipeType pipeParentRead, PipeType pipeParentWrite)
{
Jul 25, 2013
Jul 25, 2013
540
541
542
bool quit = false;
uint8 buf[256];
int br;
Jul 25, 2013
Jul 25, 2013
543
Jul 25, 2013
Jul 25, 2013
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// this read blocks.
while (!quit && ((br = readPipe(pipeParentRead, buf, sizeof (buf))) > 0))
{
while (br > 0)
{
const int cmdlen = (int) buf[0];
if ((br-1) >= cmdlen)
{
if (!processCommand(buf+1, cmdlen, pipeParentWrite))
{
quit = true;
break;
} // if
br -= cmdlen + 1;
Jul 25, 2013
Jul 25, 2013
559
560
if (br > 0)
memmove(buf, buf+cmdlen+1, br);
Jul 25, 2013
Jul 25, 2013
561
} // if
Jul 25, 2013
Jul 25, 2013
562
else // get more data.
Jul 25, 2013
Jul 25, 2013
563
{
Jul 25, 2013
Jul 25, 2013
564
const int morebr = readPipe(pipeParentRead, buf+br, sizeof (buf) - br);
Jul 25, 2013
Jul 25, 2013
565
566
567
568
569
570
571
572
573
if (morebr <= 0)
{
quit = true; // uhoh.
break;
} // if
br += morebr;
} // else
} // while
} // while
Jul 25, 2013
Jul 25, 2013
574
575
576
577
578
} // processCommands
static bool setEnvironmentVars(PipeType pipeChildRead, PipeType pipeChildWrite)
{
char buf[64];
Jul 25, 2013
Jul 25, 2013
579
snprintf(buf, sizeof (buf), "%llu", (unsigned long long) pipeChildRead);
Jul 25, 2013
Jul 25, 2013
580
581
582
if (!setEnvVar("STEAMSHIM_READHANDLE", buf))
return false;
Jul 25, 2013
Jul 25, 2013
583
snprintf(buf, sizeof (buf), "%llu", (unsigned long long) pipeChildWrite);
Jul 25, 2013
Jul 25, 2013
584
585
586
587
588
589
if (!setEnvVar("STEAMSHIM_WRITEHANDLE", buf))
return false;
return true;
} // setEnvironmentVars
Jul 25, 2013
Jul 25, 2013
590
static bool initSteamworks(PipeType fd)
Jul 25, 2013
Jul 25, 2013
591
{
Sep 16, 2015
Sep 16, 2015
592
593
594
595
// this can fail for many reasons:
// - you forgot a steam_appid.txt in the current working directory.
// - you don't have Steam running
// - you don't own the game listed in steam_appid.txt
Jul 25, 2013
Jul 25, 2013
596
597
598
if (!SteamAPI_Init())
return 0;
Jul 25, 2013
Jul 25, 2013
599
600
601
602
603
604
605
GSteamStats = SteamUserStats();
GSteamUtils = SteamUtils();
GSteamUser = SteamUser();
GAppID = GSteamUtils ? GSteamUtils->GetAppID() : 0;
GUserID = GSteamUser ? GSteamUser->GetSteamID().ConvertToUint64() : 0;
GSteamBridge = new SteamBridge(fd);
Jul 25, 2013
Jul 25, 2013
606
607
608
609
return 1;
} // initSteamworks
Jul 25, 2013
Jul 25, 2013
610
611
612
613
614
615
616
617
618
619
static void deinitSteamworks(void)
{
SteamAPI_Shutdown();
delete GSteamBridge;
GSteamBridge = NULL;
GSteamStats = NULL;
GSteamUtils= NULL;
GSteamUser = NULL;
} // deinitSteamworks
Jul 25, 2013
Jul 25, 2013
620
621
622
623
624
625
626
627
static int mainline(void)
{
PipeType pipeParentRead = NULLPIPE;
PipeType pipeParentWrite = NULLPIPE;
PipeType pipeChildRead = NULLPIPE;
PipeType pipeChildWrite = NULLPIPE;
ProcessType childPid;
Jul 25, 2013
Jul 25, 2013
628
629
dbgpipe("Parent starting mainline.\n");
Jul 25, 2013
Jul 25, 2013
630
if (!createPipes(&pipeParentRead, &pipeParentWrite, &pipeChildRead, &pipeChildWrite))
Jul 25, 2013
Jul 25, 2013
631
fail("Failed to create application pipes");
Jul 25, 2013
Jul 25, 2013
632
633
else if (!initSteamworks(pipeParentWrite))
fail("Failed to initialize Steamworks");
Jul 25, 2013
Jul 25, 2013
634
635
636
637
638
639
640
641
642
643
else if (!setEnvironmentVars(pipeChildRead, pipeChildWrite))
fail("Failed to set environment variables");
else if (!launchChild(&childPid))
fail("Failed to launch application");
// Close the ends of the pipes that the child will use; we don't need them.
closePipe(pipeChildRead);
closePipe(pipeChildWrite);
pipeChildRead = pipeChildWrite = NULLPIPE;
Jul 25, 2013
Jul 25, 2013
644
645
dbgpipe("Parent in command processing loop.\n");
Jul 25, 2013
Jul 25, 2013
646
647
648
649
// Now, we block for instructions until the pipe fails (child closed it or
// terminated/crashed).
processCommands(pipeParentRead, pipeParentWrite);
Jul 25, 2013
Jul 25, 2013
650
651
dbgpipe("Parent shutting down.\n");
Jul 25, 2013
Jul 25, 2013
652
// Close our ends of the pipes.
Jul 25, 2013
Jul 25, 2013
653
writeBye(pipeParentWrite);
Jul 25, 2013
Jul 25, 2013
654
655
656
closePipe(pipeParentRead);
closePipe(pipeParentWrite);
Jul 25, 2013
Jul 25, 2013
657
658
deinitSteamworks();
Jul 25, 2013
Jul 25, 2013
659
660
dbgpipe("Parent waiting on child process.\n");
Jul 25, 2013
Jul 25, 2013
661
// Wait for the child to terminate, close the child process handles.
Jul 25, 2013
Jul 25, 2013
662
663
664
665
666
const int retval = closeProcess(&childPid);
dbgpipe("Parent exiting mainline (child exit code %d).\n", retval);
return retval;
Jul 25, 2013
Jul 25, 2013
667
668
} // mainline
Jul 25, 2013
Jul 25, 2013
669
// end of steamshim_parent.cpp ...