Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
game_library.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <algorithm>
8#include <cctype>
9#include <filesystem>
10#include <fstream>
11#include <map>
12#include <nlohmann/json.hpp>
13#include <optional>
14#include <set>
15#include <sstream>
16#include <string>
17#include <string_view>
18#include <vector>
19
20namespace platf::game_library {
21
22 struct game_t {
24 std::string source;
26 std::string id;
27 std::string name;
29 std::string cmd;
30 };
31
36 struct vdf_t {
37 std::map<std::string, std::string> values;
38 std::map<std::string, vdf_t> blocks;
39
40 const std::string *value(const std::string &key) const {
41 const auto it = values.find(key);
42 return it == values.end() ? nullptr : &it->second;
43 }
44
45 const vdf_t *block(const std::string &key) const {
46 const auto it = blocks.find(key);
47 return it == blocks.end() ? nullptr : &it->second;
48 }
49 };
50
51 namespace detail {
52 inline std::string lower(std::string text) {
53 std::ranges::transform(text, text.begin(), [](unsigned char c) {
54 return static_cast<char>(std::tolower(c));
55 });
56 return text;
57 }
58
59 class vdf_parser_t {
60 public:
61 explicit vdf_parser_t(std::string_view text):
62 text {text} {
63 }
64
65 vdf_t parse() {
66 vdf_t root;
67 block(root, 0);
68 return root;
69 }
70
71 private:
72 std::string_view text;
73 std::size_t at = 0;
74
75 enum class token_e {
76 string,
77 open,
78 close,
79 end
80 };
81
82 token_e next(std::string &out) {
83 for (;;) {
84 while (at < text.size() && std::isspace(static_cast<unsigned char>(text[at]))) {
85 ++at;
86 }
87 if (at + 1 < text.size() && text[at] == '/' && text[at + 1] == '/') {
88 while (at < text.size() && text[at] != '\n') {
89 ++at;
90 }
91 continue;
92 }
93 break;
94 }
95 if (at >= text.size()) {
96 return token_e::end;
97 }
98 const char c = text[at];
99 if (c == '{' || c == '}') {
100 ++at;
101 return c == '{' ? token_e::open : token_e::close;
102 }
103 out.clear();
104 if (c == '"') {
105 for (++at; at < text.size() && text[at] != '"'; ++at) {
106 if (text[at] == '\\' && at + 1 < text.size()) {
107 const char escaped = text[++at];
108 out += escaped == 'n' ? '\n' : escaped == 't' ? '\t' :
109 escaped;
110 } else {
111 out += text[at];
112 }
113 }
114 ++at;
115 return token_e::string;
116 }
117 // Unquoted tokens are allowed too; they end at whitespace or a brace.
118 while (at < text.size() && !std::isspace(static_cast<unsigned char>(text[at])) && text[at] != '{' && text[at] != '}') {
119 out += text[at++];
120 }
121 return token_e::string;
122 }
123
124 void block(vdf_t &into, int depth) {
125 std::string key;
126 std::string value;
127 for (;;) {
128 const auto token = next(key);
129 if (token != token_e::string) {
130 // A close ends this block; the end of the text ends them all.
131 return;
132 }
133 const auto after = next(value);
134 if (after == token_e::open) {
135 // Bounded, so a malformed file cannot exhaust the stack.
136 if (depth < 32) {
137 block(into.blocks[lower(key)], depth + 1);
138 }
139 } else if (after == token_e::string) {
140 into.values.emplace(lower(key), value);
141 } else {
142 return;
143 }
144 }
145 }
146 };
147
148 inline std::string read_file(const std::filesystem::path &file) {
149 std::ifstream stream {file, std::ios::binary};
150 std::ostringstream text;
151 text << stream.rdbuf();
152 return text.str();
153 }
154
157 inline bool is_tool(std::string_view appid, std::string_view name) {
158 static const std::set<std::string_view> tools {
159 "228980", // Steamworks Common Redistributables
160 "1070560", // Steam Linux Runtime 1.0 (scout)
161 "1391110", // Steam Linux Runtime 2.0 (soldier)
162 "1628350", // Steam Linux Runtime 3.0 (sniper)
163 };
164 return tools.contains(appid) || name.starts_with("Proton ") || name == "Proton Experimental" ||
165 name.starts_with("Proton Hotfix") || name.starts_with("Steam Linux Runtime") ||
166 name.starts_with("Steamworks Common");
167 }
168 } // namespace detail
169
170 inline vdf_t parse_vdf(std::string_view text) {
171 return detail::vdf_parser_t {text}.parse();
172 }
173
178 inline std::vector<std::filesystem::path> steam_libraries(const std::filesystem::path &root) {
179 std::vector<std::filesystem::path> libraries {root};
180 const auto file = parse_vdf(detail::read_file(root / "steamapps" / "libraryfolders.vdf"));
181 if (const auto *folders = file.block("libraryfolders")) {
182 for (const auto &[key, folder] : folders->blocks) {
183 if (const auto *path = folder.value("path")) {
184 libraries.emplace_back(*path);
185 }
186 }
187 // Before 2021 a library was a numbered value rather than a block.
188 for (const auto &[key, path] : folders->values) {
189 if (!key.empty() && std::ranges::all_of(key, [](unsigned char c) {
190 return std::isdigit(c);
191 })) {
192 libraries.emplace_back(path);
193 }
194 }
195 }
196
197 std::vector<std::filesystem::path> unique;
198 std::set<std::filesystem::path> seen;
199 for (const auto &library : libraries) {
200 std::error_code ec;
201 auto canonical = std::filesystem::weakly_canonical(library, ec);
202 if (ec) {
203 canonical = library;
204 }
205 if (seen.insert(canonical).second) {
206 unique.push_back(library);
207 }
208 }
209 return unique;
210 }
211
218 inline std::vector<game_t> steam_games(const std::filesystem::path &root, const std::string &launcher) {
219 std::vector<game_t> games;
220 for (const auto &library : steam_libraries(root)) {
221 std::error_code ec;
222 for (const auto &entry : std::filesystem::directory_iterator {library / "steamapps", ec}) {
223 const auto filename = entry.path().filename().string();
224 if (!filename.starts_with("appmanifest_") || !filename.ends_with(".acf")) {
225 continue;
226 }
227 const auto manifest = parse_vdf(detail::read_file(entry.path()));
228 const auto *state = manifest.block("appstate");
229 if (!state) {
230 continue;
231 }
232 const auto *appid = state->value("appid");
233 const auto *name = state->value("name");
234 const auto *flags = state->value("stateflags");
235 if (!appid || appid->empty() || !name || name->empty() || detail::is_tool(*appid, *name)) {
236 continue;
237 }
238 // StateFlags bit 4 is "fully installed"; a game mid-download or
239 // only partly uninstalled does not have it.
240 try {
241 if (!flags || !(std::stoul(*flags) & 4)) {
242 continue;
243 }
244 } catch (...) {
245 continue;
246 }
247 games.push_back({"steam", *appid, *name, "setsid " + launcher + " steam://rungameid/" + *appid});
248 }
249 }
250 return games;
251 }
252
262 inline std::vector<game_t> lutris_games(std::string_view output, const std::string &launcher) {
263 std::vector<game_t> games;
264 const auto start = output.find('[');
265 if (start == std::string_view::npos) {
266 return games;
267 }
268 const auto list = nlohmann::json::parse(output.substr(start), nullptr, false);
269 if (!list.is_array()) {
270 return games;
271 }
272 for (const auto &game : list) {
273 if (!game.is_object() || !game.contains("id") || !game.contains("name") || !game["name"].is_string()) {
274 continue;
275 }
276 if (game.value("runner", nlohmann::json {}).is_string() && game["runner"] == "steam") {
277 continue;
278 }
279 const auto &id = game["id"];
280 std::string game_id;
281 if (id.is_number_integer()) {
282 game_id = std::to_string(id.get<long long>());
283 } else if (id.is_string()) {
284 game_id = id.get<std::string>();
285 }
286 const auto name = game["name"].get<std::string>();
287 if (game_id.empty() || name.empty() || !std::ranges::all_of(game_id, [](unsigned char c) {
288 return std::isdigit(c);
289 })) {
290 continue;
291 }
292 games.push_back({"lutris", game_id, name, "setsid " + launcher + " lutris:rungameid/" + game_id});
293 }
294 return games;
295 }
296
298 inline std::vector<game_t> merge(std::vector<game_t> games) {
299 std::vector<game_t> unique;
300 std::set<std::pair<std::string, std::string>> seen;
301 for (auto &game : games) {
302 if (seen.emplace(game.source, game.id).second) {
303 unique.push_back(std::move(game));
304 }
305 }
306 std::ranges::stable_sort(unique, [](const game_t &a, const game_t &b) {
307 return detail::lower(a.name) < detail::lower(b.name);
308 });
309 return unique;
310 }
311
312} // namespace platf::game_library
std::vector< std::filesystem::path > steam_libraries(const std::filesystem::path &root)
Every Steam library of a Steam installation, itself first.
std::vector< game_t > merge(std::vector< game_t > games)
A game per source and ID, the first seen kept, sorted by name.
std::vector< game_t > steam_games(const std::filesystem::path &root, const std::string &launcher)
The games fully installed in a Steam installation's libraries.
std::vector< game_t > lutris_games(std::string_view output, const std::string &launcher)
Lutris' installed games, from lutris --list-games --installed --json.
bool is_tool(std::string_view appid, std::string_view name)
std::string source
"steam" or "lutris".
std::string id
Steam's app ID, or Lutris' game ID.
std::string cmd
Starts the game; an app runs it detached.
A Valve KeyValues (VDF/ACF) block: quoted keys with a quoted value or a nested block....