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
+2 -2
View File
@@ -97,9 +97,9 @@ include(VulkanVersion)
if (WIVRN_USE_SYSTEM_BOOST AND NEED_COMMON)
if (WIVRN_BUILD_DISSECTOR)
find_package(Boost 1.84.0 REQUIRED COMPONENTS locale url)
find_package(Boost 1.84.0 REQUIRED COMPONENTS locale url iostreams)
else()
find_package(Boost 1.75.0 REQUIRED COMPONENTS locale url)
find_package(Boost 1.75.0 REQUIRED COMPONENTS locale url iostreams)
endif()
endif()
+12 -5
View File
@@ -1,7 +1,7 @@
if (WIVRN_USE_SYSTEM_BOOST)
add_library(Boost::pfr ALIAS Boost::headers)
else()
set(BOOST_INCLUDE_LIBRARIES locale url pfr)
set(BOOST_INCLUDE_LIBRARIES locale url pfr iostreams)
FetchContent_MakeAvailable(boost)
endif()
@@ -40,14 +40,17 @@ add_library(wivrn-common STATIC EXCLUDE_FROM_ALL
if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER)
add_library(wivrn-common-server STATIC EXCLUDE_FROM_ALL
application.cpp
utils/flatpak.cpp
utils/xdg_icon_lookup.cpp
utils/steam_info.cpp
utils/load_icon.cpp
utils/ini.cpp
application.cpp
utils/steam_info.cpp
utils/vdf.cpp
utils/xdg_icon_lookup.cpp
)
target_link_libraries(wivrn-common-server PRIVATE nlohmann_json::nlohmann_json PNG::PNG PkgConfig::librsvg-2.0 LibArchive::LibArchive PUBLIC wivrn-common-base)
target_link_libraries(wivrn-common-server
PRIVATE nlohmann_json::nlohmann_json PNG::PNG PkgConfig::librsvg-2.0 LibArchive::LibArchive Boost::iostreams
PUBLIC wivrn-common-base)
if (WIVRN_BUILD_TEST)
add_executable(list-apps
@@ -55,6 +58,10 @@ if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER)
)
target_link_libraries(list-apps wivrn-common-server)
add_executable(vdf utils/vdf.cpp)
target_compile_definitions(vdf PRIVATE WIVRN_BUILD_TEST)
target_link_libraries(vdf Boost::iostreams)
endif()
endif()
+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())
+217 -45
View File
@@ -1,6 +1,7 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2026 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,10 +18,15 @@
*/
#include "steam_info.h"
#include "vdf.h"
#include "xdg_base_directory.h"
#include <bit>
#include <charconv>
#include <cstring>
#include <format>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
namespace
{
@@ -128,9 +134,49 @@ void read_dict(binary_reader & in, T & visitor, const std::vector<std::string> *
}
}
}
std::filesystem::path home()
{
auto home = std::getenv("HOME");
return home ? home : "";
}
std::optional<uint32_t> guess_steam_userid(const std::filesystem::path & root)
{
auto localconfig = root / "config/loginusers.vdf";
if (not std::filesystem::exists(localconfig))
return {};
try
{
wivrn::vdf::root loginusers(localconfig);
if (loginusers.key != "users")
return {};
std::optional<uint32_t> res;
for (const auto & [userid, entries]: std::get<std::vector<wivrn::vdf::keyvalue>>(loginusers.value))
{
uint64_t v;
if (std::from_chars(userid.data.begin(), userid.data.end(), v).ec != std::errc{})
continue;
res = v; // truncate to 32 bits
for (const auto & [key, value]: std::get<std::vector<wivrn::vdf::keyvalue>>(entries))
{
if (key == "MostRecent" and std::get<wivrn::vdf::string>(value) == "1")
return res;
}
}
return res;
}
catch (std::exception & e)
{
return {};
}
}
} // namespace
std::unordered_map<uint32_t, wivrn::steam_icon> wivrn::read_steam_icons(std::filesystem::path path)
void read_steam_icons(std::filesystem::path path, std::unordered_map<uint32_t, wivrn::steam::icon> & icons)
{
binary_reader in{std::ifstream{path}};
@@ -150,11 +196,9 @@ std::unordered_map<uint32_t, wivrn::steam_icon> wivrn::read_steam_icons(std::fil
string_table_ptr = &string_table;
}
std::unordered_map<uint32_t, wivrn::steam_icon> res;
struct V
{
wivrn::steam_icon current{};
wivrn::steam::icon current{};
void begin_dict(const std::string & key)
{
@@ -198,7 +242,7 @@ std::unordered_map<uint32_t, wivrn::steam_icon> wivrn::read_steam_icons(std::fil
V v;
read_dict(in, v, string_table_ptr);
if (not(v.current.clienticon.empty() and v.current.linuxclienticon.empty()))
res.emplace(app_id, std::move(v.current));
icons.emplace(app_id, std::move(v.current));
}
catch (...)
{
@@ -206,12 +250,15 @@ std::unordered_map<uint32_t, wivrn::steam_icon> wivrn::read_steam_icons(std::fil
}
in.seekg(pos + size_data);
}
return res;
}
std::vector<wivrn::steam_shortcut> wivrn::read_steam_shortcuts(std::filesystem::path path)
void read_steam_shortcuts(
const std::filesystem::path & path,
std::vector<wivrn::steam::application> & apps,
std::unordered_map<uint32_t, std::filesystem::path> & icons)
{
if (not std::filesystem::exists(path))
return;
binary_reader in{std::ifstream{path}};
auto type = in.read<uint8_t>();
auto name = in.read<std::string>();
@@ -222,10 +269,12 @@ std::vector<wivrn::steam_shortcut> wivrn::read_steam_shortcuts(std::filesystem::
std::vector<wivrn::steam_shortcut> res;
struct V
{
std::vector<wivrn::steam_shortcut> & items;
std::vector<wivrn::steam::application> & apps;
std::unordered_map<uint32_t, std::filesystem::path> & icons;
int depth = 0;
wivrn::steam_shortcut current{};
wivrn::steam::application current{};
std::filesystem::path icon;
bool VR = false;
void begin_dict(const std::string & key)
@@ -234,6 +283,7 @@ std::vector<wivrn::steam_shortcut> wivrn::read_steam_shortcuts(std::filesystem::
if (depth == 1)
{
current = decltype(current){};
icon.clear();
VR = false;
}
}
@@ -243,7 +293,14 @@ std::vector<wivrn::steam_shortcut> wivrn::read_steam_shortcuts(std::filesystem::
if (depth == 0)
{
if (VR)
items.push_back(std::move(current));
{
if (not icon.empty())
icons[current.appid] = std::move(icon);
// ¯\_(ツ)_/¯
current.appid = (current.appid << 32) | 0x02000000;
current.url = std::format("steam://rungameid/{}", current.appid);
apps.push_back(std::move(current));
}
}
}
void on_string(const std::string & key, std::string && val)
@@ -251,9 +308,9 @@ std::vector<wivrn::steam_shortcut> wivrn::read_steam_shortcuts(std::filesystem::
if (depth == 1)
{
if (strcasecmp(key.c_str(), "AppName") == 0)
current.name = std::move(val);
current.name[""] = std::move(val);
else if (strcasecmp(key.c_str(), "icon") == 0 and not val.empty())
current.icon = std::move(val);
icon = std::move(val);
}
}
void on_uint32_t(const std::string & key, uint32_t val)
@@ -266,54 +323,169 @@ std::vector<wivrn::steam_shortcut> wivrn::read_steam_shortcuts(std::filesystem::
VR = val;
}
}
} v{res};
} v{apps, icons};
read_dict(in, v);
}
wivrn::steam::steam(std::filesystem::path root, bool flatpak) :
root(std::move(root)),
flatpak(flatpak),
default_userid(guess_steam_userid(this->root))
{
}
std::vector<wivrn::steam> wivrn::steam::find_installations()
{
std::vector<wivrn::steam> res;
auto h = home();
auto try_construct = [&res](std::filesystem::path root, bool flatpak = false) {
try
{
if (std::filesystem::exists(root))
{
res.push_back(wivrn::steam(std::move(root), flatpak));
return true;
}
}
catch (std::exception &)
{
}
return false;
};
// Flatpak Steam
try_construct(h / ".var/app/com.valvesoftware.Steam/.steam/steam", true);
// Debian Steam (accessed from flatpak)
try_construct(h / ".steam/debian-installation")
// system Steam
or try_construct(xdg_data_home() / "Steam")
// system Steam (accessed from flatpak)
or try_construct(h / ".local/share/Steam");
return res;
}
std::optional<uint32_t> wivrn::guess_steam_userid(std::filesystem::path steam_root)
std::vector<wivrn::steam::application> wivrn::steam::list_applications()
{
auto localconfig = steam_root / "config/loginusers.vdf";
if (not std::filesystem::exists(localconfig))
return {};
try
std::vector<wivrn::steam::application> res;
// Steam games, from VR manifest
{
std::optional<uint32_t> res;
std::ifstream in{localconfig};
std::string line;
std::getline(in, line);
if (strcasecmp(line.c_str(), "\"users\""))
return {};
uint64_t current = 0;
std::vector<char> key;
std::vector<char> value;
while (in)
std::ifstream manifest(root / "config/steamapps.vrmanifest");
nlohmann::json json = nlohmann::json::parse(manifest);
for (auto & i: json["applications"])
{
std::getline(in, line);
key.resize(line.size());
value.resize(line.size());
if (sscanf(line.c_str(), "\t\"%ld\"", &current))
try
{
// Steam games have an url launch
// shortcuts may be in this file, or may not...
if (i["launch_type"] != "url")
continue;
std::string app_key = i["app_key"];
const char prefix[] = "steam.app.";
if (not app_key.starts_with(prefix))
continue;
application app{
.appid = stoul(app_key.substr(strlen(prefix))),
.url = i["url"],
};
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;
else
continue; // skip unnamed app
}
res.push_back(std::move(app));
}
else if (sscanf(line.c_str(), "\t\t%s\t%s", key.data(), value.data()))
catch (std::exception & e)
{
if (strncasecmp(key.data(), "\"MostRecent\"", key.size()) == 0 and strncasecmp(value.data(), "\"1\"", value.size()) == 0)
res = current; // truncate to 32 bits
std::cerr << "Failed to parse Steam VR manifest: " << e.what() << std::endl;
}
}
if (current and not res)
res = current;
return res;
}
catch (std::exception & e)
// Shortcuts
if (default_userid)
{
try
{
read_steam_shortcuts(root / "userdata" / std::to_string(*default_userid) / "config/shortcuts.vdf", res, shortcut_icons);
}
catch (std::exception & e)
{
}
}
else
{
// Is it a good idea to just iterate over all users?
for (auto const & entry: std::filesystem::directory_iterator{root / "userdata"})
try
{
read_steam_shortcuts(entry.path() / "config/shortcuts.vdf", res, shortcut_icons);
}
catch (std::exception & e)
{
}
}
return res;
}
std::optional<std::filesystem::path> wivrn::steam::get_icon(uint64_t appid)
{
if (appid & 0x02000000)
{
// Shortcut
auto it = shortcut_icons.find(appid >> 32);
if (it != shortcut_icons.end())
return it->second;
return {};
}
if (not icons)
{
icons.emplace();
try
{
read_steam_icons(root / "appcache/appinfo.vdf", *icons);
}
catch (...)
{}
}
auto it = icons->find(appid);
if (it == icons->end())
return {};
auto icon_path = root / "steam/games" / (it->second.clienticon + ".ico");
if (std::filesystem::exists(icon_path))
return icon_path;
icon_path = root / "steam/games" / (it->second.linuxclienticon + ".zip");
if (std::filesystem::exists(icon_path))
return icon_path;
return {};
}
std::string wivrn::steam::get_steam_command() const
{
if (flatpak)
return "flatpak run com.valvesoftware.Steam";
return "steam";
}
+30 -8
View File
@@ -26,10 +26,37 @@
namespace wivrn
{
struct steam_icon
class steam
{
std::string clienticon;
std::string linuxclienticon;
public:
struct application
{
uint64_t appid;
// localised names, with empty locale for default
std::unordered_map<std::string, std::string> name;
std::string url;
};
struct icon
{
std::string clienticon;
std::string linuxclienticon;
};
private:
std::filesystem::path root;
bool flatpak;
std::optional<uint32_t> default_userid;
std::optional<std::unordered_map<uint32_t, icon>> icons;
std::unordered_map<uint32_t, std::filesystem::path> shortcut_icons;
steam(std::filesystem::path root, bool flatpak);
public:
static std::vector<steam> find_installations();
std::vector<application> list_applications();
std::optional<std::filesystem::path> get_icon(uint64_t app_id);
std::string get_steam_command() const;
};
struct steam_shortcut
@@ -39,9 +66,4 @@ struct steam_shortcut
std::optional<std::filesystem::path> icon;
};
std::unordered_map<uint32_t, steam_icon> read_steam_icons(std::filesystem::path path);
std::vector<steam_shortcut> read_steam_shortcuts(std::filesystem::path path);
std::optional<uint32_t> guess_steam_userid(std::filesystem::path steam_root);
} // namespace wivrn
+191
View File
@@ -0,0 +1,191 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "vdf.h"
#include <boost/iostreams/device/mapped_file.hpp>
#include <iostream>
namespace
{
void consume_whitespace(const char *& pos, const char * end)
{
for (; pos != end; ++pos)
{
if (not std::isspace(*pos))
return;
}
throw std::runtime_error("malformed VDF file");
}
wivrn::vdf::string read_string(const char *& pos, const char * end)
{
consume_whitespace(pos, end);
const bool quoted = *pos == '"';
if (quoted)
++pos;
const char * begin = pos;
for (; pos != end; ++pos)
{
if (*pos == '\\')
{
++pos;
if (pos == end)
break;
continue;
}
if (quoted)
{
if (*pos == '"')
return {std::string_view(begin, pos++)};
}
else
{
if (std::isspace(*pos) or *pos == '{' or *pos == '}')
return {std::string_view(begin, pos)};
}
}
throw std::runtime_error("malformed string in VDF file");
}
std::variant<wivrn::vdf::string, std::vector<wivrn::vdf::keyvalue>> read_value(const char *& pos, const char * end);
wivrn::vdf::keyvalue read_key_value(const char *& pos, const char * end)
{
return wivrn::vdf::keyvalue{
.key = read_string(pos, end),
.value = read_value(pos, end),
};
}
std::vector<wivrn::vdf::keyvalue> read_dict(const char *& pos, const char * end)
{
consume_whitespace(pos, end);
if (*pos != '{')
throw std::runtime_error("malformed VDF file");
++pos;
std::vector<wivrn::vdf::keyvalue> res;
while (true)
{
consume_whitespace(pos, end);
if (*pos == '}')
{
++pos;
return res;
}
res.push_back(read_key_value(pos, end));
}
throw std::runtime_error("malformed dict in VDF file");
}
std::variant<wivrn::vdf::string, std::vector<wivrn::vdf::keyvalue>> read_value(const char *& pos, const char * end)
{
consume_whitespace(pos, end);
if (*pos == '{')
return read_dict(pos, end);
else
return read_string(pos, end);
}
} // namespace
bool wivrn::vdf::string::operator==(const char * b) const
{
const char * a = data.begin();
for (; a != data.end(); ++a, ++b)
{
if (*b == '\0')
return false;
if (*a == '\\')
{
++a;
// This should be taken care of by the parser
assert(a != data.end());
}
if (std::tolower(*b) != std::tolower(*a))
return false;
}
return *b == '\0';
}
wivrn::vdf::root::root(const std::filesystem::path & file)
{
boost::iostreams::mapped_file in{file};
opaque = in;
const char * pos = in.begin();
key = read_string(pos, in.end());
value = read_value(pos, in.end());
}
void dump(const wivrn::vdf::keyvalue & kv, int depth)
{
for (int i = 0; i < depth; ++i)
std::cout << '\t';
std::cout << '"' << kv.key.data << "\": ";
struct V
{
int depth;
void operator()(const wivrn::vdf::string & s)
{
std::cout << '"' << s.data << '"';
}
void operator()(const std::vector<wivrn::vdf::keyvalue> & kv)
{
std::cout << '{' << std::endl;
for (const auto & i: kv)
{
dump(i, depth + 1);
if (&i != &kv.back())
std::cout << ',';
std::cout << std::endl;
}
for (int i = 0; i < depth; ++i)
std::cout << '\t';
std::cout << "}";
}
};
std::visit(V{depth}, kv.value);
}
#ifdef WIVRN_BUILD_TEST
void dump(const wivrn::vdf::root & kv)
{
std::cout << '{';
dump(kv, 0);
std::cout << '}';
}
int main(int argc, char ** argv)
{
for (int i = 1; i < argc; ++i)
{
wivrn::vdf::root doc{argv[i]};
dump(doc);
}
}
#endif
+54
View File
@@ -0,0 +1,54 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <any>
#include <filesystem>
#include <string_view>
#include <variant>
#include <vector>
namespace wivrn
{
namespace vdf
{
struct string
{
std::string_view data;
bool operator==(const char *) const;
};
struct keyvalue
{
string key;
std::variant<string, std::vector<keyvalue>> value;
};
struct root : public keyvalue
{
root(const std::filesystem::path &);
private:
std::any opaque;
};
} // namespace vdf
} // namespace wivrn