12#include <nlohmann/json.hpp>
20namespace platf::game_library {
37 std::map<std::string, std::string> values;
38 std::map<std::string, vdf_t> blocks;
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;
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;
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));
61 explicit vdf_parser_t(std::string_view text):
72 std::string_view text;
82 token_e next(std::string &out) {
84 while (at < text.size() && std::isspace(
static_cast<unsigned char>(text[at]))) {
87 if (at + 1 < text.size() && text[at] ==
'/' && text[at + 1] ==
'/') {
88 while (at < text.size() && text[at] !=
'\n') {
95 if (at >= text.size()) {
98 const char c = text[at];
99 if (c ==
'{' || c ==
'}') {
101 return c ==
'{' ? token_e::open : token_e::close;
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' :
115 return token_e::string;
118 while (at < text.size() && !std::isspace(
static_cast<unsigned char>(text[at])) && text[at] !=
'{' && text[at] !=
'}') {
121 return token_e::string;
124 void block(
vdf_t &into,
int depth) {
128 const auto token = next(key);
129 if (token != token_e::string) {
133 const auto after = next(value);
134 if (after == token_e::open) {
137 block(into.blocks[lower(key)], depth + 1);
139 }
else if (after == token_e::string) {
140 into.values.emplace(lower(key), value);
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();
157 inline bool is_tool(std::string_view appid, std::string_view name) {
158 static const std::set<std::string_view> tools {
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");
170 inline vdf_t parse_vdf(std::string_view text) {
171 return detail::vdf_parser_t {text}.parse();
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);
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);
192 libraries.emplace_back(path);
197 std::vector<std::filesystem::path> unique;
198 std::set<std::filesystem::path> seen;
199 for (
const auto &library : libraries) {
201 auto canonical = std::filesystem::weakly_canonical(library, ec);
205 if (seen.insert(canonical).second) {
206 unique.push_back(library);
218 inline std::vector<game_t>
steam_games(
const std::filesystem::path &root,
const std::string &launcher) {
219 std::vector<game_t> games;
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")) {
227 const auto manifest = parse_vdf(detail::read_file(entry.path()));
228 const auto *state = manifest.block(
"appstate");
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)) {
241 if (!flags || !(std::stoul(*flags) & 4)) {
247 games.push_back({
"steam", *appid, *name,
"setsid " + launcher +
" steam://rungameid/" + *appid});
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) {
268 const auto list = nlohmann::json::parse(output.substr(start),
nullptr,
false);
269 if (!list.is_array()) {
272 for (
const auto &game : list) {
273 if (!game.is_object() || !game.contains(
"id") || !game.contains(
"name") || !game[
"name"].is_string()) {
276 if (game.value(
"runner", nlohmann::json {}).is_string() && game[
"runner"] ==
"steam") {
279 const auto &
id = 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>();
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);
292 games.push_back({
"lutris", game_id, name,
"setsid " + launcher +
" lutris:rungameid/" + game_id});
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));
306 std::ranges::stable_sort(unique, [](
const game_t &a,
const game_t &b) {
307 return detail::lower(a.name) < detail::lower(b.name);
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....