blob: 28fac2b7810498ff284f29f18b4d82122840a564 (
plain)
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
|
#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",
};
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)) {
if (!folders.contains(path)) {
folders.append(path);
}
}
}
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;
}
QHash<int, QString> findSteamGames()
{
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);
}
}
}
}
}
return games;
}
QString findSteamGamePath(int appId)
{
auto games = findSteamGames();
return games.value(appId);
}
|