From cc773294e3284f5282d0500d946ddfaa7a0a5065 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Tue, 20 Jan 2026 12:46:24 +0100 Subject: [PATCH] Read non-Steam games using shortcuts.vdf --- common/application.cpp | 44 ++++++--- common/utils/steam_app_info.cpp | 160 ++++++++++++++++++++++++++++++++ common/utils/steam_app_info.h | 11 +++ 3 files changed, 200 insertions(+), 15 deletions(-) diff --git a/common/application.cpp b/common/application.cpp index afdd08b8..5b507bf4 100644 --- a/common/application.cpp +++ b/common/application.cpp @@ -138,6 +138,11 @@ void read_steam_vr_apps(std::unordered_map & res) { 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()) { @@ -154,21 +159,6 @@ void read_steam_vr_apps(std::unordered_map & res) app.name[""] = it->second; } - if (i["launch_type"] == "url") - { - app.exec = command + " " + (std::string)i["url"]; - } - else if (i["launch_type"] == "binary") - { - const char prefix[] = "steam.app."; - if (app_key.starts_with(prefix)) - { - // ¯\_(ツ)_/¯ - uint64_t appkey = stoll(app_key.substr(strlen(prefix))); - app.exec = command + " steam://rungameid/" + std::to_string((appkey << 32) + 0x2000000); - } - } - try { const char prefix[] = "steam.app."; @@ -190,6 +180,30 @@ void read_steam_vr_apps(std::unordered_map & res) std::cerr << "Failed to parse Steam VR manifest: " << e.what() << std::endl; } } + + for (auto const & entry: std::filesystem::directory_iterator{*root / "userdata"}) + { + auto shortcuts_vdf = entry.path() / "config/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; + } + } + } } std::string unescape(std::string_view in) diff --git a/common/utils/steam_app_info.cpp b/common/utils/steam_app_info.cpp index 168240a1..7933a553 100644 --- a/common/utils/steam_app_info.cpp +++ b/common/utils/steam_app_info.cpp @@ -184,3 +184,163 @@ wivrn::steam_app_info::steam_app_info(std::filesystem::path path) } } } + +namespace +{ + +class binary_reader +{ + std::ifstream in; + +public: + binary_reader(std::ifstream && in) : in{std::move(in)} {} + + template + T read(); + + template + requires std::is_integral_v + T read() + { + T res; + in.read((char *)&res, sizeof(T)); + if (not in) + throw std::runtime_error{"File truncated"}; + + static_assert(std::endian::native == std::endian::big or std::endian::native == std::endian::little); + + if constexpr (std::endian::native == std::endian::big) + return std::byteswap(res); + else + return res; + } +}; + +template <> +std::string binary_reader::read() +{ + std::string res; + std::getline(in, res, '\0'); + if (not in) + throw std::runtime_error{"File truncated"}; + return res; +} + +template +void read_dict(binary_reader & in, T & visitor, const std::vector * string_table = nullptr) +{ + while (true) + { + auto type = in.read(); + if (type == 8) + { + visitor.end_dict(); + return; + } + + // Key + const std::string & key = string_table ? string_table->at(in.read()) : in.read(); + + // Value + switch (type) + { + case 0: // Dictionary + visitor.begin_dict(key); + read_dict(in, visitor, string_table); + break; + case 1: // UTF-8 string + visitor.on_string(key, in.read()); + break; + case 2: // uint32_t + visitor.on_uint32_t(key, in.read()); + break; + // case 3: // float + default: + throw std::runtime_error{"Unknown object type " + std::to_string(type)}; + } + } +} + +template +void read_vdf(binary_reader & in, T & visitor, int version) +{ + if (version >= 41) + { + std::vector string_table; + auto count = in.read(); + string_table.reserve(count); + for (size_t i = 0; i < count; ++i) + string_table.emplace_back(in.read()); + + read_dict(in, visitor, &string_table); + } + else + { + read_dict(in, visitor); + } +} + +} // namespace + +std::vector wivrn::read_steam_shortcuts(std::filesystem::path path) +{ + binary_reader in{std::ifstream{path}}; + auto type = in.read(); + auto name = in.read(); + + if (type != 0 or name != "shortcuts") + throw std::runtime_error{"Invalid type for shortcuts file"}; + + std::vector res; + struct V + { + std::vector & items; + + int depth = 0; + wivrn::steam_shortcut current{}; + bool VR = false; + + void begin_dict(const std::string & key) + { + ++depth; + if (depth == 1) + { + current = decltype(current){}; + VR = false; + } + } + void end_dict() + { + --depth; + if (depth == 0) + { + if (VR) + items.push_back(std::move(current)); + } + } + void on_string(const std::string & key, std::string && val) + { + if (depth == 1) + { + if (key == "AppName") + current.name = std::move(val); + else if (key == "icon") + current.icon = std::move(val); + } + } + void on_uint32_t(const std::string & key, uint32_t val) + { + if (depth == 1) + { + if (key == "appid") + current.appid = val; + if (key == "OpenVR") + VR = val; + } + } + } v{res}; + + read_vdf(in, v, 0); + + return res; +} diff --git a/common/utils/steam_app_info.h b/common/utils/steam_app_info.h index bc53788c..65f6be27 100644 --- a/common/utils/steam_app_info.h +++ b/common/utils/steam_app_info.h @@ -19,6 +19,7 @@ #pragma once #include +#include #include #include #include @@ -48,4 +49,14 @@ public: return app_data.at(appid); } }; + +struct steam_shortcut +{ + uint32_t appid; + std::string name; + std::optional icon; +}; + +std::vector read_steam_shortcuts(std::filesystem::path path); + } // namespace wivrn