Refactor Steam libraries

This commit is contained in:
Patrick Nicolas
2026-02-13 14:37:03 +01:00
parent 74946403f0
commit 5d1ba50e92
7 changed files with 519 additions and 232 deletions
+13 -172
View File
@@ -26,7 +26,6 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
namespace wivrn
@@ -42,175 +41,6 @@ std::string read_file(const std::filesystem::path & path)
return {begin, end};
}
std::filesystem::path home()
{
auto home = std::getenv("HOME");
return home ? home : "";
}
std::pair<std::string, std::optional<std::filesystem::path>> find_steam()
{
auto h = home();
// Flatpak Steam
if (std::filesystem::exists(h / ".var/app/com.valvesoftware.Steam/.steam/steam"))
return {"flatpak run com.valvesoftware.Steam", h / ".var/app/com.valvesoftware.Steam/.steam/steam"};
// Debian Steam (accessed from flatpak)
if (std::filesystem::exists(h / ".steam/debian-installation"))
return {"steam", h / ".steam/debian-installation"};
// system Steam
if (std::filesystem::exists(xdg_data_home() / "Steam"))
return {"steam", xdg_data_home() / "Steam"};
// system Steam (accessed from flatpak)
if (std::filesystem::exists(h / ".local/share/Steam"))
return {"steam", h / ".local/share/Steam"};
return {};
}
std::string read_vr_manifest(const std::filesystem::path & root)
{
return read_file(root / "config/steamapps.vrmanifest");
}
std::unordered_map<uint32_t, wivrn::steam_icon> safe_read_steam_icons(const std::filesystem::path & root)
{
try
{
return read_steam_icons(root / "appcache/appinfo.vdf");
}
catch (...)
{}
return {};
}
std::optional<std::filesystem::path> find_steam_icon(const std::filesystem::path & root, int app_id, const std::unordered_map<uint32_t, wivrn::steam_icon> & info)
{
auto icon = info.find(app_id);
if (icon == info.end())
return std::nullopt;
try
{
auto icon_path = root / "steam/games" / (icon->second.clienticon + ".ico");
if (std::filesystem::exists(icon_path))
return icon_path;
}
catch (...)
{
}
try
{
auto icon_path = root / "steam/games" / (icon->second.linuxclienticon + ".zip");
if (std::filesystem::exists(icon_path))
return icon_path;
}
catch (...)
{
}
return std::nullopt;
}
void read_steam_vr_apps(std::unordered_map<std::string, application> & res)
{
auto [command, root] = find_steam();
if (not root)
return;
auto manifest = read_vr_manifest(*root);
auto info = safe_read_steam_icons(*root);
if (manifest.empty())
return;
nlohmann::json json = nlohmann::json::parse(manifest);
for (auto & i: json["applications"])
{
try
{
application app;
if (i["launch_type"] == "url")
app.exec = command + " " + (std::string)i["url"];
else
continue;
std::string app_key = i["app_key"];
for (auto [locale, items]: i["strings"].items())
{
if (auto it = items.find("name"); it != items.end())
app.name[locale] = *it;
}
if (not app.name.contains(""))
{
auto it = app.name.find("en_us");
if (it == app.name.end())
it = app.name.begin();
if (it != app.name.end())
app.name[""] = it->second;
}
try
{
const char prefix[] = "steam.app.";
if (app_key.starts_with(prefix))
{
uint32_t app_id = stoll(app_key.substr(strlen(prefix)));
app.icon_path = find_steam_icon(*root, app_id, info);
}
}
catch (...)
{
}
if (not app.exec.empty())
res[app_key] = std::move(app);
}
catch (std::exception & e)
{
std::cerr << "Failed to parse Steam VR manifest: " << e.what() << std::endl;
}
}
auto userid = guess_steam_userid(*root);
auto do_shortcuts = [&](const std::filesystem::path & shortcuts_vdf) {
if (std::filesystem::exists(shortcuts_vdf))
{
try
{
for (auto && item: read_steam_shortcuts(shortcuts_vdf))
{
res[std::to_string(item.appid)] = {
.name = {{"", std::move(item.name)}},
// ¯\_(ツ)_/¯
.exec = command + " steam://rungameid/" + std::to_string((uint64_t(item.appid) << 32) + 0x2000000),
.icon_path = std::move(item.icon),
};
}
}
catch (std::exception & e)
{
std::cerr << "Failed to parse Steam shortcuts file " << shortcuts_vdf << ": " << e.what() << std::endl;
}
}
};
if (userid and std::filesystem::exists(*root / "userdata" / std::to_string(*userid)))
do_shortcuts(*root / "userdata" / std::to_string(*userid) / "config/shortcuts.vdf");
else
{
for (auto const & entry: std::filesystem::directory_iterator{*root / "userdata"})
do_shortcuts(entry.path() / "config/shortcuts.vdf");
}
}
std::string unescape(std::string_view in)
{
std::string out;
@@ -398,8 +228,19 @@ std::unordered_map<std::string, application> list_applications()
{
std::unordered_map<std::string, application> res;
read_steam_vr_apps(res);
for (auto & steam: steam::find_installations())
{
auto cmd = steam.get_steam_command() + " ";
for (auto && app: steam.list_applications())
{
res.emplace(std::to_string(app.appid),
application{
.name = std::move(app.name),
.exec = cmd + app.url,
.icon_path = steam.get_icon(app.appid),
});
}
}
do_data_dir(xdg_data_home(), res);
for (auto && dir: xdg_data_dirs())