forked from SteamGridDB/SGDBoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgdboop.c
677 lines (566 loc) · 16.8 KB
/
sgdboop.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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <stdint.h>
#include <inttypes.h>
#include "string-helpers.h"
#include "curl-helper.h"
#include "include/iup.h"
#include "crc.h"
#ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */
#define OS_Windows 0
#define MAX_PATH 600
#define FALSE 0
#define TRUE 1
int GetConsoleWindow();
void MoveWindow(int, int, int, int, int, int);
void GetModuleFileName(char*, char*, int);
FILE* _popen(char*, char*);
void _pclose(FILE*);
#include <unistd.h>
#elif defined(_WIN32) || defined(WIN32) /* _Win32 is usually defined by compilers targeting 32 or 64 bit Windows systems */
#define OS_Windows 1
#include <windows.h>
int symlink(char* a, char* b) {
return CreateHardLink(b, a, NULL);
}
#endif
#define API_VERSION "1"
typedef struct nonSteamApp
{
int index;
char name[300];
char appid[128];
char appid_old[128];
} nonSteamApp;
int _nonSteamAppsCount = 0;
// Call the BOOP API
char** callAPI(char* grid_type, char* grid_id, char* mode)
{
char authHeader[] = "Authorization: Bearer 62696720-6f69-6c79-2070-65656e75733f";
char apiVersionHeader[20] = "X-BOOP-API-VER: ";
strcat(apiVersionHeader, API_VERSION);
char* url = malloc(512);
char** valuesArray = malloc(1024);
strcpy(url, "https://www.steamgriddb.com/api/sgdboop/");
strcat(url, grid_type);
strcat(url, "/");
strcat(url, grid_id);
if (strcmp(mode, "nonsteam") == 0) {
strcat(url, "?nonsteam=1");
}
CURL* curl;
CURLcode res;
struct curl_slist* headers = NULL;
curl = curl_easy_init();
if (curl)
{
struct string s;
init_string(&s);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, TRUE);
headers = curl_slist_append(headers, authHeader);
headers = curl_slist_append(headers, apiVersionHeader);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK)
{
return NULL;
}
valuesArray[0] = s.ptr;
valuesArray[1] = strstr(s.ptr, ",");
valuesArray[1][0] = '\0';
valuesArray[1] += 1;
valuesArray[2] = strstr(valuesArray[1], ",");
valuesArray[2][0] = '\0';
valuesArray[2] += 1;
free(url);
return valuesArray;
}
free(url);
free(valuesArray);
return NULL;
}
// Download an asset file
char* downloadAssetFile(char* app_id, char* url, char* type, char* orientation, char* destinationDir)
{
// Try creating folder
if (access(destinationDir, 0) != 0) {
mkdir(destinationDir, 0700);
}
CURL* curl;
FILE* fp;
CURLcode res;
char* outfilename = malloc(1024);
strcpy(outfilename, destinationDir);
strcat(outfilename, app_id);
if (strcmp(type, "hero") == 0) {
// Hero
strcat(outfilename, "_hero.jpg");
}
else if (strcmp(type, "logo") == 0) {
// Logo
strcat(outfilename, "_logo.jpg");
}
else if (strcmp(type, "grid") == 0 && strcmp(orientation, "p") == 0) {
// Vertical grid
strcat(outfilename, "p.jpg");
}
else if (strcmp(type, "grid") == 0) {
// Horizontal grid
strcat(outfilename, ".jpg");
}
else if (strcmp(type, "icon") == 0) {
// Icon
strcat(outfilename, "_icon.jpg");
}
curl = curl_easy_init();
if (curl && outfilename != 0) {
fp = fopen(outfilename, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, TRUE);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
if (res != 0) {
remove(outfilename);
free(outfilename);
return NULL;
}
return outfilename;
}
free(outfilename);
return NULL;
}
// Create the SGDB URI protocol
int createURIprotocol() {
if (OS_Windows) {
char cwd[MAX_PATH];
GetModuleFileName(NULL, cwd, MAX_PATH);
char* regeditCommand = malloc(2028);
strcpy(regeditCommand, "REG ADD HKCR\\sgdb\\Shell\\Open\\Command /t REG_SZ /d \"\\\"");
strcat(regeditCommand, cwd);
strcat(regeditCommand, "\\\" \\\"%1\\\" -new_console:z\" /f");
int ret_val = system("REG ADD HKCR\\sgdb /t REG_SZ /d \"URL:sgdb protocol\" /f");
if (ret_val != 0) {
system("cls");
printf("Please run this program as Administrator!\n");
system("pause");
free(regeditCommand);
return 1;
}
system(regeditCommand);
strcpy(regeditCommand, "REG ADD HKCR\\sgdb\\DefaultIcon /t REG_SZ /d \"");
strcat(regeditCommand, cwd);
strcat(regeditCommand, "\" /f");
system(regeditCommand);
system("REG ADD HKCR\\sgdb /v \"URL Protocol\" /t REG_SZ /d \"\" /f");
system("cls");
printf("Program registered successfully!\n");
system("pause");
free(regeditCommand);
return 0;
}
else {
// Do nothing on linux
printf("A SGDB URL argument is required.\nExample: SGDBoop sgdb://boop/[ASSET_TYPE]/[ASSET_ID]\n");
return 1;
}
}
// Delete the SGDB URI protocol
int deleteURIprotocol() {
if (OS_Windows) {
int ret_val = system("REG DELETE HKCR\\sgdb /f");
if (ret_val != 0) {
system("cls");
printf("Please run this program as Administrator!\n");
system("pause");
return 1;
}
system("cls");
printf("Program unregistered successfully!\n");
system("pause");
return 0;
}
else {
// Do nothing on linux
printf("A SGDB URL argument is required.\nExample: SGDBoop sgdb://boop/[ASSET_TYPE]/[ASSET_ID]\n");
return 1;
}
}
// Get Steam's base directory
char* getSteamBaseDir() {
char* steamBaseDir = malloc(MAX_PATH);
int foundValue = 0;
if (OS_Windows) {
FILE* terminal = _popen("reg query HKCU\\Software\\Valve\\Steam /v SteamPath", "r");
char buf[256];
while (fgets(buf, sizeof(buf), terminal) != 0) {
if (strstr(buf, "SteamPath")) {
char* extractedValue = strstr(buf, "REG_SZ") + 10;
strcpy(steamBaseDir, extractedValue);
foundValue = 1;
}
}
_pclose(terminal);
if (!foundValue) {
free(steamBaseDir);
return NULL;
}
int steamDirLength = strlen(steamBaseDir);
for (int i = 0; i < steamDirLength; i++) {
if (steamBaseDir[i] == '\n') {
steamBaseDir[i] = '\0';
}
}
}
else {
foundValue = 1;
strcpy(steamBaseDir, getenv("HOME"));
char steamFlatpakDir[MAX_PATH];
strcpy(steamFlatpakDir, getenv("HOME"));
strcat(steamFlatpakDir, "/.var/app/com.valvesoftware.Steam/data/Steam");
// If flatpaked Steam is installed
if (access(steamFlatpakDir, 0) == 0) {
strcat(steamBaseDir, "/.var/app/com.valvesoftware.Steam/data/Steam");
}
else {
// Steam installed on host
strcat(steamBaseDir, "/.steam/steam");
}
}
if (foundValue < 1) {
free(steamBaseDir);
return NULL;
}
return steamBaseDir;
}
// Find the most recently logged-in user
char* getMostRecentUser(char* steamBaseDir) {
char* steamid = malloc(512);
char* steamConfigFile = malloc(MAX_PATH);
strcpy(steamConfigFile, steamBaseDir);
strcat(steamConfigFile, "/config/loginusers.vdf");
FILE* fp;
char* line = NULL;
size_t len = 0;
size_t read;
fp = fopen(steamConfigFile, "r");
if (fp == NULL) {
free(steamid);
free(steamConfigFile);
exit(95);
}
while ((read = readLine(&line, &len, fp)) != -1) {
if (strstr(line, "7656119") && !strstr(line, "PersonaName")) {
// Found line with id
strcpy(steamid, strstr(line, "7656119"));
char* stringEnd = strstr(steamid, "\"");
stringEnd[0] = '\0';
}
else if ((strstr(line, "mostrecent") || strstr(line, "MostRecent")) && strstr(line, "\"1\"")) {
// Found line mostrecent
unsigned long long steamidLongLong = atoll(steamid);
steamidLongLong -= 76561197960265728;
sprintf(steamid, "%llu", steamidLongLong);
break;
}
}
fclose(fp);
if (line)
free(line);
return steamid;
}
// Get Steam's destination directory based on artwork type
char* getSteamDestinationDir(char* type) {
char* steamBaseDir = getSteamBaseDir();
if (steamBaseDir == NULL) {
return NULL;
}
if (strcmp(type, "icon") == 0) {
// If it's an icon
strcat(steamBaseDir, "/appcache/librarycache/");
}
else {
// If it's not an icon, read the loginusers.vdf to find the most recent user
char* steamid = getMostRecentUser(steamBaseDir);
strcat(steamBaseDir, "/userdata/");
strcat(steamBaseDir, steamid);
strcat(steamBaseDir, "/config/grid/");
}
return steamBaseDir;
}
// Parse shortcuts file and return a pointer to a list of structs containing the app data
struct nonSteamApp* getNonSteamApps(char* type, char* orientation) {
char* shortcutsVdfPath = getSteamBaseDir();
char* steamid = getMostRecentUser(shortcutsVdfPath);
// Get the shortcuts.vdf file
strcat(shortcutsVdfPath, "/userdata/");
strcat(shortcutsVdfPath, steamid);
strcat(shortcutsVdfPath, "/config/shortcuts.vdf");
// Parse the file
FILE* fp;
unsigned char buf[2] = { 0 };
size_t bytes = 0;
size_t read = sizeof buf;
fp = fopen(shortcutsVdfPath, "rb");
if (fp == NULL) {
free(shortcutsVdfPath);
IupMessage("SGDBoop Error", "Could not find any non-Steam apps.");
exit(90);
}
fseek(fp, 0L, SEEK_END);
size_t filesize = ftell(fp) + 2;
fseek(fp, 0, SEEK_SET);
unsigned char* fileContent = malloc(filesize + 1);
unsigned int currentFileByte = 0;
// Load the vdf in memory and fix string-related issues
while ((bytes = fread(buf, sizeof * buf, read, fp)) == read) {
for (int i = 0; i < sizeof buf; i++) {
if (buf[i] == 0x00) {
fileContent[currentFileByte] = 0x03;
}
else {
fileContent[currentFileByte] = buf[i];
}
currentFileByte++;
}
}
fileContent[filesize - 2] = '\x08';
fileContent[filesize - 1] = '\x03';
fileContent[filesize] = '\0';
fclose(fp);
struct nonSteamApp* apps = malloc(sizeof(nonSteamApp) * 500000);
unsigned char* parsingChar = fileContent;
unsigned char parsingAppid[512];
uint64_t intBytes[4];
crcInit();
int old_id_required = 0;
if (strcmp(type, "grid") == 0 && strcmp(orientation, "l") == 0) {
old_id_required = 1;
}
// Parse the vdf content
while (strstr_i(parsingChar, "appname") > 0) {
uint64_t appid_old = 0;
uint64_t appid = 0;
// Find app name
unsigned char* nameStartChar = strstr_i(parsingChar, "appname") + 8;
unsigned char* nameEndChar = strstr(nameStartChar, "\x03");
// Find exe path
unsigned char* exeStartChar = strstr_i(parsingChar, "exe") + 4;
unsigned char* exeEndChar = strstr(exeStartChar, "\x03");
unsigned char* appidPtr = strstr_i(parsingChar, "appid");
unsigned char* appBlockEndPtr = strstr(parsingChar, "\x08") + 1; // gcc fucks with optimization on strstr for 2 consecutive hex values. DON'T EDIT THIS.
while (*appBlockEndPtr != 0x03 && *appBlockEndPtr != 0x00) {
appBlockEndPtr = strstr(appBlockEndPtr, "\x08") + 1;
}
// If appid was found in this app block
if (appidPtr > 0 && appidPtr < appBlockEndPtr) {
unsigned char* hexBytes = appidPtr + 6;
intBytes[0] = *(hexBytes + 3);
intBytes[1] = *(hexBytes + 2);
intBytes[2] = *(hexBytes + 1);
intBytes[3] = *(hexBytes + 0);
appid =
((uint64_t)intBytes[0] << 24) |
((uint64_t)intBytes[1] << 16) |
((uint64_t)intBytes[2] << 8) |
((uint64_t)intBytes[3] << 0);
}
// If an old appid is required, calculate it
if (appid == 0 || old_id_required) {
*nameEndChar = '\0';
*exeEndChar = '\0';
strcpy(parsingAppid, exeStartChar);
strcat(parsingAppid, nameStartChar);
appid_old = crcFast(parsingAppid, strlen(parsingAppid));
if (appid == 0) {
appid = appid_old;
}
*exeEndChar = '\x03';
}
*nameEndChar = '\0'; // Close name string
// Do math magic. Valve pls fix
appid = (((appid | 0x80000000) << 32) | 0x02000000) >> 32;
if (old_id_required) {
appid_old = (((appid_old | 0x80000000) << 32) | 0x02000000);
sprintf(apps[_nonSteamAppsCount].appid_old, "%" PRIu64, appid_old);
}
else {
strcpy(apps[_nonSteamAppsCount].appid_old, "none");
}
// Add the values to struct
apps[_nonSteamAppsCount].index = _nonSteamAppsCount;
strcpy(apps[_nonSteamAppsCount].name, nameStartChar);
sprintf(apps[_nonSteamAppsCount].appid, "%lu", (unsigned long)appid);
_nonSteamAppsCount++;
// Move parser to end of app data
*nameEndChar = 0x03; // Revert name string to prevent string-related problems
parsingChar = appBlockEndPtr + 2;
}
// Exit with an error if no non-steam apps were found
if (_nonSteamAppsCount < 1) {
IupMessage("SGDBoop Error", "Could not find any non-Steam apps.");
free(fileContent);
free(apps);
exit(91);
}
free(fileContent);
return apps;
}
// Select a non-steam app from a dropdown list and return its ID
struct nonSteamApp* selectNonSteamApp(char* sgdbName, struct nonSteamApp* apps) {
char temp[512];
sprintf(temp, "%d", _nonSteamAppsCount);
char* appid = malloc(128);
char** values = malloc(sizeof(char*) * _nonSteamAppsCount);
for (int i = 0; i < _nonSteamAppsCount; i++) {
values[i] = malloc(strlen(apps[i].name) + 1);
strcpy(values[i], apps[i].name);
}
struct nonSteamApp* appData = malloc(sizeof(nonSteamApp));
char* title = malloc(40 + strlen(sgdbName));
strcpy(title, "SGDBoop: Pick a game for '");
strcat(title, sgdbName);
strcat(title, "'");
qsort(values, _nonSteamAppsCount, sizeof(const char*), compareStrings);
int selection = 0;
for (int i = 0; i < _nonSteamAppsCount; i++) {
if (strcmp(values[i], sgdbName) == 0) {
selection = i + 1;
break;
}
}
int retval = IupListDialog(1, title, _nonSteamAppsCount, (const char**)values, selection, strlen(title) - 12, 14, NULL);
// Exit when use clicks cancel
if (retval < 0) {
free(apps);
free(values);
free(title);
exit(0);
}
// Find match
for (int i = 0; i < _nonSteamAppsCount; i++) {
if (strcmp(apps[i].name, values[retval]) == 0) {
strcpy(appData->appid, apps[i].appid);
strcpy(appData->name, apps[i].name);
appData->index = apps[i].index;
if (strcmp(apps[i].appid_old, "none") != 0) {
strcpy(appData->appid_old, apps[i].appid_old);
}
break;
}
}
free(apps);
free(values);
free(title);
return appData;
}
// Create a symlink for a file that has the old nonsteam appid format
void createOldIdSymlink(struct nonSteamApp* appData, char* steamDestDir) {
char linkPath[MAX_PATH];
char targetPath[MAX_PATH];
strcpy(linkPath, steamDestDir);
strcat(linkPath, appData->appid_old);
strcat(linkPath, ".jpg");
strcpy(targetPath, steamDestDir);
strcat(targetPath, appData->appid);
strcat(targetPath, ".jpg");
int result = symlink(targetPath, linkPath);
}
// Add an icon to IUP windows
void loadIupIcon() {
unsigned char image_data[256] = { 0 };
Ihandle* sgdboop_image = IupImage(16, 16, image_data);
IupSetAttribute(sgdboop_image, "0", "BGCOLOR");
IupSetHandle("SGDBOOPIMAGE", sgdboop_image);
IupSetGlobal("ICON", "SGDBOOPIMAGE");
}
int main(int argc, char** argv)
{
// If no arguments were given, register the program
if (argc == 0 || (argc == 1 && !startsWith(argv[0], "sgdb://"))) {
// Create the sgdb URI protocol
if (createURIprotocol() == 1) {
return 80;
}
}
else {
if (OS_Windows) {
MoveWindow(GetConsoleWindow(), -3000, -3000, 0, 0, FALSE);
}
// If argument is unregister, unregister and exit
if (strcmp(argv[1], "unregister") == 0) {
if (deleteURIprotocol() == 1) {
return 85;
}
return 0;
}
// If sgdb:// arguments were passed, run program normally
// If the arguments aren't of the SGDB URI, return with an error
if (!startsWith(argv[1], "sgdb://boop")) {
return 81;
}
// Get the params from the string
char* type = strstr(argv[1], "sgdb://boop/") + strlen("sgdb://boop/");
char* grid_id = strstr(type, "/");
grid_id[0] = '\0'; // End app_id string
grid_id += 1; // Move 1 place
char* mode = strstr(grid_id, "/"); // If there's a method string, use it
if (mode > 0) {
*mode = '\0';
mode++;
}
else {
mode = malloc(sizeof("default") + 1);
strcpy(mode, "default");
}
// Get asset URL
char** apiValues = callAPI(type, grid_id, mode);
if (apiValues == NULL) {
return 82;
}
char* app_id = apiValues[0];
char* orientation = apiValues[1];
char* assetUrl = apiValues[2];
struct nonSteamApp* nonSteamAppData = NULL;
// If the game is a non-steam app, select an imported app
if (startsWith(app_id, "nonsteam-")) {
if (strcmp(type, "icon") == 0) {
return 92;
}
// Enable IUP GUI
IupOpen(&argc, &argv);
loadIupIcon();
// Get non-steam apps
struct nonSteamApp* apps = getNonSteamApps(type, orientation);
// Show selection screen and return the appid
nonSteamAppData = selectNonSteamApp(strstr(app_id, "-") + 1, apps);
app_id = nonSteamAppData->appid;
}
// Get Steam base dir
char* steamDestDir = getSteamDestinationDir(type);
if (steamDestDir == NULL) {
return 83;
}
// Download asset file
char* outfilename = downloadAssetFile(app_id, assetUrl, type, orientation, steamDestDir);
if (outfilename == NULL) {
return 84;
}
// If the asset is a horizontal grid, create a symlink (for back. compat.)
if (nonSteamAppData && strcmp(type, "grid") == 0 && strcmp(orientation, "l") == 0) {
createOldIdSymlink(nonSteamAppData, steamDestDir);
}
if (nonSteamAppData) {
free(nonSteamAppData);
}
}
return 0;
}