Read non-Steam games using shortcuts.vdf

This commit is contained in:
Patrick Nicolas
2026-01-20 14:05:06 +01:00
parent 514c44b0a1
commit cc773294e3
3 changed files with 200 additions and 15 deletions
+29 -15
View File
@@ -138,6 +138,11 @@ void read_steam_vr_apps(std::unordered_map<std::string, application> & 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<std::string, application> & 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<std::string, application> & 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)
+160
View File
@@ -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 <typename T>
T read();
template <typename T>
requires std::is_integral_v<T>
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 <typename T>
void read_dict(binary_reader & in, T & visitor, const std::vector<std::string> * string_table = nullptr)
{
while (true)
{
auto type = in.read<uint8_t>();
if (type == 8)
{
visitor.end_dict();
return;
}
// Key
const std::string & key = string_table ? string_table->at(in.read<uint32_t>()) : in.read<std::string>();
// 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<std::string>());
break;
case 2: // uint32_t
visitor.on_uint32_t(key, in.read<uint32_t>());
break;
// case 3: // float
default:
throw std::runtime_error{"Unknown object type " + std::to_string(type)};
}
}
}
template <typename T>
void read_vdf(binary_reader & in, T & visitor, int version)
{
if (version >= 41)
{
std::vector<std::string> string_table;
auto count = in.read<uint32_t>();
string_table.reserve(count);
for (size_t i = 0; i < count; ++i)
string_table.emplace_back(in.read<std::string>());
read_dict(in, visitor, &string_table);
}
else
{
read_dict(in, visitor);
}
}
} // namespace
std::vector<wivrn::steam_shortcut> wivrn::read_steam_shortcuts(std::filesystem::path path)
{
binary_reader in{std::ifstream{path}};
auto type = in.read<uint8_t>();
auto name = in.read<std::string>();
if (type != 0 or name != "shortcuts")
throw std::runtime_error{"Invalid type for shortcuts file"};
std::vector<wivrn::steam_shortcut> res;
struct V
{
std::vector<wivrn::steam_shortcut> & 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;
}
+11
View File
@@ -19,6 +19,7 @@
#pragma once
#include <filesystem>
#include <optional>
#include <unordered_map>
#include <variant>
#include <vector>
@@ -48,4 +49,14 @@ public:
return app_data.at(appid);
}
};
struct steam_shortcut
{
uint32_t appid;
std::string name;
std::optional<std::filesystem::path> icon;
};
std::vector<steam_shortcut> read_steam_shortcuts(std::filesystem::path path);
} // namespace wivrn