| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#pragma once |
| 23 |
|
| 24 |
#ifdef __cplusplus |
| 25 |
extern "C" |
| 26 |
{ |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <stdbool.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <string.h> |
| 32 |
#include <sys/stat.h> |
| 33 |
#include <sys/types.h> |
| 34 |
|
| 35 |
#if defined(_WIN64) || defined(_WIN32) |
| 36 |
#define _FSG_FUNC static __inline |
| 37 |
#elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L |
| 38 |
#define _FSG_FUNC static __inline__ |
| 39 |
#elif defined(__cplusplus) |
| 40 |
#define _FSG_FUNC static inline |
| 41 |
#else |
| 42 |
#define _FSG_FUNC static |
| 43 |
#endif |
| 44 |
|
| 45 |
#if defined(_WIN64) || defined(_WIN32) |
| 46 |
#define WIN32_LEAN_AND_MEAN |
| 47 |
#define NOMINMAX |
| 48 |
#include <windows.h> |
| 49 |
#include <winerror.h> |
| 50 |
#include <winreg.h> |
| 51 |
|
| 52 |
_FSG_FUNC |
| 53 |
void _fsg_query_reg_key( |
| 54 |
char *out, const HKEY key, const char *keypath, const char *valname) |
| 55 |
{ |
| 56 |
HKEY pathkey; |
| 57 |
DWORD pathtype; |
| 58 |
DWORD pathlen; |
| 59 |
LONG res; |
| 60 |
|
| 61 |
if (ERROR_SUCCESS == |
| 62 |
RegOpenKeyEx(key, keypath, 0, KEY_QUERY_VALUE, &pathkey)) |
| 63 |
{ |
| 64 |
if (ERROR_SUCCESS == |
| 65 |
RegQueryValueEx( |
| 66 |
pathkey, valname, 0, &pathtype, NULL, &pathlen) && |
| 67 |
pathtype == REG_SZ && pathlen != 0) |
| 68 |
{ |
| 69 |
res = RegQueryValueEx( |
| 70 |
pathkey, valname, 0, NULL, (LPBYTE)out, &pathlen); |
| 71 |
if (res != ERROR_SUCCESS) |
| 72 |
{ |
| 73 |
out[0] = '\0'; |
| 74 |
} |
| 75 |
} |
| 76 |
RegCloseKey(pathkey); |
| 77 |
} |
| 78 |
} |
| 79 |
#else |
| 80 |
|
| 81 |
#include <pwd.h> |
| 82 |
#include <unistd.h> |
| 83 |
|
| 84 |
#endif |
| 85 |
|
| 86 |
#if defined(_WIN64) || defined(_WIN32) |
| 87 |
#define _FSG_PATH_MAX MAX_PATH |
| 88 |
#elif defined __linux__ |
| 89 |
#include <limits.h> |
| 90 |
#ifdef PATH_MAX |
| 91 |
#define _FSG_PATH_MAX PATH_MAX |
| 92 |
#endif |
| 93 |
#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 94 |
#include <sys/param.h> |
| 95 |
#if defined(BSD) |
| 96 |
#include <limits.h> |
| 97 |
#ifdef PATH_MAX |
| 98 |
#define _FSG_PATH_MAX PATH_MAX |
| 99 |
#endif |
| 100 |
#endif |
| 101 |
#endif |
| 102 |
|
| 103 |
#ifndef _FSG_PATH_MAX |
| 104 |
#define _FSG_PATH_MAX 4096 |
| 105 |
#endif |
| 106 |
|
| 107 |
_FSG_FUNC |
| 108 |
bool _fsg_dir_exists(const char *dir) |
| 109 |
{ |
| 110 |
struct stat info; |
| 111 |
return stat(dir, &info) == 0 && (info.st_mode & S_IFDIR); |
| 112 |
} |
| 113 |
|
| 114 |
_FSG_FUNC |
| 115 |
void fsg_get_steam_game_path(char *out, const char *name) |
| 116 |
{ |
| 117 |
out[0] = '\0'; |
| 118 |
#if defined(_WIN64) || defined(_WIN32) |
| 119 |
char steam_path[_FSG_PATH_MAX]; |
| 120 |
char buf[_FSG_PATH_MAX]; |
| 121 |
_fsg_query_reg_key( |
| 122 |
steam_path, HKEY_CURRENT_USER, "Software\\Valve\\Steam", |
| 123 |
"SteamPath"); |
| 124 |
if (strlen(steam_path) == 0) |
| 125 |
{ |
| 126 |
_fsg_query_reg_key( |
| 127 |
steam_path, HKEY_LOCAL_MACHINE, "Software\\Valve\\Steam", |
| 128 |
"InstallPath"); |
| 129 |
if (strlen(steam_path) == 0) |
| 130 |
return; |
| 131 |
} |
| 132 |
strcat(steam_path, "\\steamapps\\"); |
| 133 |
|
| 134 |
|
| 135 |
sprintf(out, "%scommon\\%s", steam_path, name); |
| 136 |
|
| 137 |
if (_fsg_dir_exists(out)) |
| 138 |
{ |
| 139 |
return; |
| 140 |
} |
| 141 |
out[0] = '\0'; |
| 142 |
|
| 143 |
|
| 144 |
sprintf(buf, "%s\\libraryfolders.vdf", steam_path); |
| 145 |
FILE *f = fopen(buf, "r"); |
| 146 |
if (f) |
| 147 |
{ |
| 148 |
char line_buf[256]; |
| 149 |
while (fgets(line_buf, 256, f)) |
| 150 |
{ |
| 151 |
|
| 152 |
const char *path_p = strstr(line_buf, "\"path\""); |
| 153 |
if (path_p == NULL) |
| 154 |
{ |
| 155 |
continue; |
| 156 |
} |
| 157 |
const char *value_start = |
| 158 |
strchr(path_p + strlen("\"path\"") + 1, '"'); |
| 159 |
if (value_start == NULL) |
| 160 |
{ |
| 161 |
continue; |
| 162 |
} |
| 163 |
value_start++; |
| 164 |
const char *value_end = strchr(value_start, '"'); |
| 165 |
const char *value_p = value_start; |
| 166 |
char *out_p = out; |
| 167 |
while (value_p < value_end) |
| 168 |
{ |
| 169 |
|
| 170 |
*out_p++ = *value_p++; |
| 171 |
if (*value_p == '\\' && value_p > value_start && |
| 172 |
*(value_p - 1) == '\\') |
| 173 |
{ |
| 174 |
out_p--; |
| 175 |
} |
| 176 |
} |
| 177 |
*out_p = '\0'; |
| 178 |
strcat(out, "\\steamapps\\common\\"); |
| 179 |
strcat(out, name); |
| 180 |
if (_fsg_dir_exists(out)) |
| 181 |
{ |
| 182 |
fclose(f); |
| 183 |
return; |
| 184 |
} |
| 185 |
} |
| 186 |
fclose(f); |
| 187 |
out[0] = '\0'; |
| 188 |
} |
| 189 |
#else |
| 190 |
|
| 191 |
struct passwd *pw = getpwuid(getuid()); |
| 192 |
const char *homedir = pw->pw_dir; |
| 193 |
#if defined(__APPLE__) |
| 194 |
sprintf( |
| 195 |
out, "%s/Library/Application Support/Steam/steamapps/common/%s", |
| 196 |
homedir, name); |
| 197 |
#else |
| 198 |
sprintf(out, "%s/.local/share/Steam/steamapps/common/%s", homedir, name); |
| 199 |
#endif |
| 200 |
if (_fsg_dir_exists(out)) |
| 201 |
{ |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
char buf[_FSG_PATH_MAX]; |
| 208 |
#if defined(__APPLE__) |
| 209 |
const int ret = snprintf( |
| 210 |
buf, _FSG_PATH_MAX, |
| 211 |
"%s/Library/Application Support/Steam/steamapps/libraryfolders.vdf", |
| 212 |
homedir); |
| 213 |
#else |
| 214 |
const int ret = snprintf( |
| 215 |
buf, _FSG_PATH_MAX, |
| 216 |
"%s/.local/share/Steam/steamapps/libraryfolders.vdf", homedir); |
| 217 |
#endif |
| 218 |
if (ret >= 0) |
| 219 |
{ |
| 220 |
FILE *f = fopen(buf, "r"); |
| 221 |
if (f) |
| 222 |
{ |
| 223 |
char line_buf[256]; |
| 224 |
while (fgets(line_buf, 256, f)) |
| 225 |
{ |
| 226 |
|
| 227 |
const char *path_p = strstr(line_buf, "\"path\""); |
| 228 |
if (path_p == NULL) |
| 229 |
{ |
| 230 |
continue; |
| 231 |
} |
| 232 |
const char *value_start = |
| 233 |
strchr(path_p + strlen("\"path\"") + 1, '"'); |
| 234 |
if (value_start == NULL) |
| 235 |
{ |
| 236 |
continue; |
| 237 |
} |
| 238 |
value_start++; |
| 239 |
const char *value_end = strchr(value_start, '"'); |
| 240 |
const char *value_p = value_start; |
| 241 |
char *out_p = out; |
| 242 |
while (value_p < value_end) |
| 243 |
{ |
| 244 |
|
| 245 |
*out_p++ = *value_p++; |
| 246 |
} |
| 247 |
*out_p = '\0'; |
| 248 |
strcat(out, "/steamapps/common/"); |
| 249 |
strcat(out, name); |
| 250 |
if (_fsg_dir_exists(out)) |
| 251 |
{ |
| 252 |
fclose(f); |
| 253 |
return; |
| 254 |
} |
| 255 |
} |
| 256 |
fclose(f); |
| 257 |
} |
| 258 |
} |
| 259 |
#endif |
| 260 |
|
| 261 |
out[0] = '\0'; |
| 262 |
} |
| 263 |
|
| 264 |
_FSG_FUNC |
| 265 |
void fsg_get_gog_game_path(char *out, const char *app_id) |
| 266 |
{ |
| 267 |
out[0] = '\0'; |
| 268 |
char buf[_FSG_PATH_MAX]; |
| 269 |
#if defined(_WIN64) || defined(_WIN32) |
| 270 |
sprintf(buf, "Software\\Wow6432Node\\GOG.com\\Games\\%s", app_id); |
| 271 |
_fsg_query_reg_key(out, HKEY_LOCAL_MACHINE, buf, "Path"); |
| 272 |
#elif __APPLE__ |
| 273 |
sprintf(buf, "/Applications/%s.app/Contents/Resources", app_id); |
| 274 |
if (_fsg_dir_exists(buf)) |
| 275 |
{ |
| 276 |
strcpy(out, buf); |
| 277 |
return; |
| 278 |
} |
| 279 |
#else |
| 280 |
(void)app_id; |
| 281 |
(void)buf; |
| 282 |
#endif |
| 283 |
} |
| 284 |
|
| 285 |
#ifdef __cplusplus |
| 286 |
} |
| 287 |
#endif |