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
|
#include "steamutils.h"
#include <QDir>
#include <QFile>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QTextStream>
static QStringList findSteamPaths()
{
QStringList paths;
QString home = QDir::homePath();
QStringList candidates = {
home + "/.local/share/Steam",
home + "/.steam/debian-installation",
home + "/.steam/steam",
home + "/.var/app/com.valvesoftware.Steam/data/Steam",
home + "/.var/app/com.valvesoftware.Steam/.local/share/Steam",
home + "/snap/steam/common/.local/share/Steam",
};
// When Steam launches an app (e.g. in game mode) it sets
// STEAM_COMPAT_CLIENT_INSTALL_PATH to its own root. Add that as a
// candidate so we find the right installation even if $HOME resolves
// differently inside the gamescope session.
const QString steamEnvPath =
QString::fromLocal8Bit(qgetenv("STEAM_COMPAT_CLIENT_INSTALL_PATH"));
if (!steamEnvPath.isEmpty()) {
candidates.prepend(steamEnvPath);
}
for (const auto& candidate : candidates) {
QDir dir(candidate);
if (dir.exists("steamapps") || QFile::exists(candidate + "/steam.pid")) {
// Resolve symlinks to avoid duplicates
QString canonical = QFileInfo(candidate).canonicalFilePath();
if (!canonical.isEmpty() && !paths.contains(canonical)) {
paths.append(canonical);
}
}
}
return paths;
}
static QStringList parseLibraryFolders(const QString& vdfPath)
{
QStringList folders;
QFile file(vdfPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return folders;
QTextStream in(&file);
QString content = in.readAll();
// Match "path" entries in VDF format: "path" "/path/to/library"
static QRegularExpression re(R"--("path"\s+"([^"]+)")--");
auto it = re.globalMatch(content);
while (it.hasNext()) {
auto match = it.next();
QString path = match.captured(1);
if (QDir(path).exists()) {
folders.append(path);
}
}
return folders;
}
static QStringList getAllLibraryFolders(const QString& steamPath)
{
QStringList folders;
folders.append(steamPath);
// Parse libraryfolders.vdf
for (const auto& vdfName :
{"steamapps/libraryfolders.vdf", "config/libraryfolders.vdf"}) {
QString vdfPath = steamPath + "/" + vdfName;
for (const auto& path : parseLibraryFolders(vdfPath)) {
// Canonicalize before dedup so /home/ and /var/home/ paths compare equal
QString canonical = QFileInfo(path).canonicalFilePath();
if (canonical.isEmpty())
canonical = path;
if (!folders.contains(canonical)) {
folders.append(canonical);
}
}
}
return folders;
}
struct AppManifest {
int appId = 0;
QString name;
QString installDir;
int stateFlags = 0;
bool isInstalled() const { return (stateFlags & 4) != 0; }
};
static AppManifest parseAppManifest(const QString& path)
{
AppManifest manifest;
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return manifest;
QTextStream in(&file);
QString content = in.readAll();
static QRegularExpression reAppId(R"--("appid"\s+"(\d+)")--");
static QRegularExpression reName(R"--("name"\s+"([^"]+)")--");
static QRegularExpression reInstallDir(R"--("installdir"\s+"([^"]+)")--");
static QRegularExpression reState(R"--("StateFlags"\s+"(\d+)")--");
auto m = reAppId.match(content);
if (m.hasMatch())
manifest.appId = m.captured(1).toInt();
m = reName.match(content);
if (m.hasMatch())
manifest.name = m.captured(1);
m = reInstallDir.match(content);
if (m.hasMatch())
manifest.installDir = m.captured(1);
m = reState.match(content);
if (m.hasMatch())
manifest.stateFlags = m.captured(1).toInt();
return manifest;
}
// Cache: built once per process, cleared on explicit refresh.
static QHash<int, QString> s_steamGamesCache;
static bool s_steamGamesCacheValid = false;
QHash<int, QString> findSteamGames()
{
if (s_steamGamesCacheValid)
return s_steamGamesCache;
QHash<int, QString> games;
for (const auto& steamPath : findSteamPaths()) {
for (const auto& libraryPath : getAllLibraryFolders(steamPath)) {
QDir steamapps(libraryPath + "/steamapps");
if (!steamapps.exists())
continue;
QStringList manifests =
steamapps.entryList({"appmanifest_*.acf"}, QDir::Files);
for (const auto& manifestFile : manifests) {
auto manifest = parseAppManifest(steamapps.filePath(manifestFile));
if (manifest.appId > 0 && manifest.isInstalled() &&
!manifest.installDir.isEmpty()) {
QString installPath =
steamapps.filePath("common/" + manifest.installDir);
if (QDir(installPath).exists()) {
games.insert(manifest.appId, installPath);
}
}
}
}
}
s_steamGamesCache = games;
s_steamGamesCacheValid = true;
return games;
}
QString findSteamGamePath(int appId)
{
return findSteamGames().value(appId);
}
|