-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_steam_game.h
287 lines (265 loc) · 6.6 KB
/
find_steam_game.h
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
/*
Copyright (c) 2021-2024 Cong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#if defined(_WIN64) || defined(_WIN32)
#define _FSG_FUNC static __inline
#elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
#define _FSG_FUNC static __inline__
#elif defined(__cplusplus)
#define _FSG_FUNC static inline
#else
#define _FSG_FUNC static
#endif
#if defined(_WIN64) || defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <winerror.h>
#include <winreg.h>
_FSG_FUNC
void _fsg_query_reg_key(
char *out, const HKEY key, const char *keypath, const char *valname)
{
HKEY pathkey;
DWORD pathtype;
DWORD pathlen;
LONG res;
if (ERROR_SUCCESS ==
RegOpenKeyEx(key, keypath, 0, KEY_QUERY_VALUE, &pathkey))
{
if (ERROR_SUCCESS ==
RegQueryValueEx(
pathkey, valname, 0, &pathtype, NULL, &pathlen) &&
pathtype == REG_SZ && pathlen != 0)
{
res = RegQueryValueEx(
pathkey, valname, 0, NULL, (LPBYTE)out, &pathlen);
if (res != ERROR_SUCCESS)
{
out[0] = '\0';
}
}
RegCloseKey(pathkey);
}
}
#else
#include <pwd.h>
#include <unistd.h>
#endif
#if defined(_WIN64) || defined(_WIN32)
#define _FSG_PATH_MAX MAX_PATH
#elif defined __linux__
#include <limits.h>
#ifdef PATH_MAX
#define _FSG_PATH_MAX PATH_MAX
#endif
#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/param.h>
#if defined(BSD)
#include <limits.h>
#ifdef PATH_MAX
#define _FSG_PATH_MAX PATH_MAX
#endif
#endif
#endif
#ifndef _FSG_PATH_MAX
#define _FSG_PATH_MAX 4096
#endif
_FSG_FUNC
bool _fsg_dir_exists(const char *dir)
{
struct stat info;
return stat(dir, &info) == 0 && (info.st_mode & S_IFDIR);
}
_FSG_FUNC
void fsg_get_steam_game_path(char *out, const char *name)
{
out[0] = '\0';
#if defined(_WIN64) || defined(_WIN32)
char steam_path[_FSG_PATH_MAX];
char buf[_FSG_PATH_MAX];
_fsg_query_reg_key(
steam_path, HKEY_CURRENT_USER, "Software\\Valve\\Steam",
"SteamPath");
if (strlen(steam_path) == 0)
{
_fsg_query_reg_key(
steam_path, HKEY_LOCAL_MACHINE, "Software\\Valve\\Steam",
"InstallPath");
if (strlen(steam_path) == 0)
return;
}
strcat(steam_path, "\\steamapps\\");
// Check for game in common
sprintf(out, "%scommon\\%s", steam_path, name);
if (_fsg_dir_exists(out))
{
return;
}
out[0] = '\0';
// Try reading library paths described by libraryfolders.vdf
sprintf(buf, "%s\\libraryfolders.vdf", steam_path);
FILE *f = fopen(buf, "r");
if (f)
{
char line_buf[256];
while (fgets(line_buf, 256, f))
{
// Look for a line with "path" "<library path>"
const char *path_p = strstr(line_buf, "\"path\"");
if (path_p == NULL)
{
continue;
}
const char *value_start =
strchr(path_p + strlen("\"path\"") + 1, '"');
if (value_start == NULL)
{
continue;
}
value_start++;
const char *value_end = strchr(value_start, '"');
const char *value_p = value_start;
char *out_p = out;
while (value_p < value_end)
{
// Copy value to output, skipping double backslashes
*out_p++ = *value_p++;
if (*value_p == '\\' && value_p > value_start &&
*(value_p - 1) == '\\')
{
out_p--;
}
}
*out_p = '\0';
strcat(out, "\\steamapps\\common\\");
strcat(out, name);
if (_fsg_dir_exists(out))
{
fclose(f);
return;
}
}
fclose(f);
out[0] = '\0';
}
#else
// Look directly at steam installation folder
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
#if defined(__APPLE__)
sprintf(
out, "%s/Library/Application Support/Steam/steamapps/common/%s",
homedir, name);
#else
sprintf(out, "%s/.local/share/Steam/steamapps/common/%s", homedir, name);
#endif
if (_fsg_dir_exists(out))
{
return;
}
// Try reading library paths described by libraryfolders.vdf
// TODO: steam installed at different location
char buf[_FSG_PATH_MAX];
#if defined(__APPLE__)
const int ret = snprintf(
buf, _FSG_PATH_MAX,
"%s/Library/Application Support/Steam/steamapps/libraryfolders.vdf",
homedir);
#else
const int ret = snprintf(
buf, _FSG_PATH_MAX,
"%s/.local/share/Steam/steamapps/libraryfolders.vdf", homedir);
#endif
if (ret >= 0)
{
FILE *f = fopen(buf, "r");
if (f)
{
char line_buf[256];
while (fgets(line_buf, 256, f))
{
// Look for a line with "path" "<library path>"
const char *path_p = strstr(line_buf, "\"path\"");
if (path_p == NULL)
{
continue;
}
const char *value_start =
strchr(path_p + strlen("\"path\"") + 1, '"');
if (value_start == NULL)
{
continue;
}
value_start++;
const char *value_end = strchr(value_start, '"');
const char *value_p = value_start;
char *out_p = out;
while (value_p < value_end)
{
// Copy value to output
*out_p++ = *value_p++;
}
*out_p = '\0';
strcat(out, "/steamapps/common/");
strcat(out, name);
if (_fsg_dir_exists(out))
{
fclose(f);
return;
}
}
fclose(f);
}
}
#endif
out[0] = '\0';
}
_FSG_FUNC
void fsg_get_gog_game_path(char *out, const char *app_id)
{
out[0] = '\0';
char buf[_FSG_PATH_MAX];
#if defined(_WIN64) || defined(_WIN32)
sprintf(buf, "Software\\Wow6432Node\\GOG.com\\Games\\%s", app_id);
_fsg_query_reg_key(out, HKEY_LOCAL_MACHINE, buf, "Path");
#elif __APPLE__
sprintf(buf, "/Applications/%s.app/Contents/Resources", app_id);
if (_fsg_dir_exists(buf))
{
strcpy(out, buf);
return;
}
#else
(void)app_id;
(void)buf;
#endif
}
#ifdef __cplusplus
}
#endif