Skip to content

Commit

Permalink
Add macOS steam
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Apr 30, 2024
1 parent 57efb4c commit 57ef0db
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 63 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# find_steam_game

[![CMake](https://github.com/cxong/find_steam_game/actions/workflows/cmake.yml/badge.svg)](https://github.com/cxong/find_steam_game/actions/workflows/cmake.yml)

Cross platform single-header C library for finding games installed from platforms like steam
Expand All @@ -13,14 +14,14 @@ fsg_get_steam_game_path(buf, "Wolfenstein 3D");
printf("Steam: %s found at %s\n", "Wolfenstein 3D", buf);

// Prints: Steam: Wolfenstein 3D found at c:/program files (x86)/steam/SteamApps/common/Wolfenstein 3D
```
```
## Support
| Store | Windows | Linux | macOS |
| ----------- | ----------- |----------- |----------- |
| Steam | ✔ (folder name) | ✔ | ❌ |
| GOG | ✔ (game id) | ❌ | ✔ (app name) |
| Store | Windows | Linux | macOS |
| ----- | ----------- | ----- | ------------ |
| Steam | ✔ | ✔ | ✔ |
| GOG | ✔ (game id) | ❌ | ✔ (app name) |
- macOS GOG doesn't support custom installation locations; it only looks at `/Applications`
Expand Down
129 changes: 71 additions & 58 deletions find_steam_game.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021 Cong
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
Expand Down Expand Up @@ -187,62 +187,75 @@ extern "C"
out[0] = '\0';
}
#else
// Look at $HOME/.local/share/Steam/steamapps/common/
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
sprintf(out, "%s/.local/share/Steam/steamapps/common/%s", homedir, name);
if (_fsg_dir_exists(out))
{
return;
}
// 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];
const int ret = snprintf(
buf, _FSG_PATH_MAX,
"%s/.local/share/Steam/steamapps/libraryfolders.vdf", homedir);
if (ret >= 0)
// 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)
{
FILE *f = fopen(buf, "r");
if (f)
char line_buf[256];
while (fgets(line_buf, 256, 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)
{
// 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;
}
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);
}
fclose(f);
}
}
#endif

out[0] = '\0';
Expand All @@ -257,15 +270,15 @@ extern "C"
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;
}
sprintf(buf, "/Applications/%s.app/Contents/Resources", app_id);
if (_fsg_dir_exists(buf))
{
strcpy(out, buf);
return;
}
#else
(void)app_id;
(void)buf;
(void)app_id;
(void)buf;
#endif
}

Expand Down

0 comments on commit 57ef0db

Please sign in to comment.