Don't rely on steamapps.vrmanifest
The file seems to no longer be updated by Steam. Instead, read installed games from libraryfolders.vdf, owned games from user's localconfig.vdf and data from appinfo.vdf
This commit is contained in:
+133
-119
@@ -27,6 +27,7 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -174,11 +175,46 @@ std::optional<uint32_t> guess_steam_userid(const std::filesystem::path & root)
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void read_steam_icons(std::filesystem::path path, std::unordered_map<uint32_t, wivrn::steam::icon> & icons)
|
||||
std::unordered_set<uint64_t> installed_appids(std::filesystem::path root)
|
||||
{
|
||||
binary_reader in{std::ifstream{path}};
|
||||
wivrn::vdf::root vdf(root / "config/libraryfolders.vdf");
|
||||
using values_t = std::vector<wivrn::vdf::keyvalue>;
|
||||
if (vdf.key != "libraryfolders")
|
||||
return {};
|
||||
|
||||
std::unordered_set<uint64_t> res;
|
||||
for (const auto & kv: std::get<values_t>(vdf.value))
|
||||
{
|
||||
for (const auto & [appid, _]: std::get<values_t>(kv["apps"].value))
|
||||
{
|
||||
uint64_t val;
|
||||
if (std::from_chars(appid.data.begin(), appid.data.end(), val).ec == std::errc())
|
||||
res.emplace(val);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
std::unordered_set<uint64_t> owned_appids(std::filesystem::path localconfig)
|
||||
{
|
||||
wivrn::vdf::root localconfig_vdf(localconfig);
|
||||
using values_t = std::vector<wivrn::vdf::keyvalue>;
|
||||
if (localconfig_vdf.key != "UserLocalConfigStore")
|
||||
return {};
|
||||
|
||||
std::unordered_set<uint64_t> res;
|
||||
for (const auto & [k, entries]: std::get<values_t>(localconfig_vdf["Software"]["Valve"]["Steam"]["apps"].value))
|
||||
{
|
||||
uint64_t val;
|
||||
if (std::from_chars(k.data.begin(), k.data.end(), val).ec == std::errc())
|
||||
res.emplace(val);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
std::vector<wivrn::steam::application> read_appinfo(std::filesystem::path root, std::unordered_set<uint64_t> const & appids)
|
||||
{
|
||||
binary_reader in{std::ifstream{root / "appcache/appinfo.vdf"}};
|
||||
|
||||
uint32_t magic = in.read<uint32_t>();
|
||||
if ((magic & 0xff'ff'ff'00) != 0x07'56'44'00)
|
||||
@@ -195,28 +231,73 @@ void read_steam_icons(std::filesystem::path path, std::unordered_map<uint32_t, w
|
||||
string_table = in.string_table();
|
||||
string_table_ptr = &string_table;
|
||||
}
|
||||
std::vector<wivrn::steam::application> res;
|
||||
|
||||
enum section
|
||||
{
|
||||
common,
|
||||
other,
|
||||
};
|
||||
struct V
|
||||
{
|
||||
wivrn::steam::icon current{};
|
||||
std::vector<enum section> section;
|
||||
wivrn::steam::application current{};
|
||||
std::string clienticon;
|
||||
std::string linuxclienticon;
|
||||
bool VR = false;
|
||||
|
||||
void clear()
|
||||
{
|
||||
section.clear();
|
||||
current.icon.clear();
|
||||
current.name.clear();
|
||||
current.url.clear();
|
||||
clienticon.clear();
|
||||
linuxclienticon.clear();
|
||||
VR = false;
|
||||
section.push_back(section::other);
|
||||
}
|
||||
|
||||
void begin_dict(const std::string & key)
|
||||
{
|
||||
if (key == "common")
|
||||
section.push_back(section::common);
|
||||
else
|
||||
section.push_back(section::other);
|
||||
}
|
||||
void end_dict()
|
||||
{
|
||||
section.pop_back();
|
||||
}
|
||||
void on_string(const std::string & key, std::string && val)
|
||||
{
|
||||
if (strcasecmp(key.c_str(), "clienticon") == 0)
|
||||
current.clienticon = std::move(val);
|
||||
else if (strcasecmp(key.c_str(), "linuxclienticon") == 0)
|
||||
current.linuxclienticon = std::move(val);
|
||||
switch (section.back())
|
||||
{
|
||||
case common:
|
||||
if (strcasecmp(key.c_str(), "clienticon") == 0)
|
||||
clienticon = std::move(val);
|
||||
else if (strcasecmp(key.c_str(), "linuxclienticon") == 0)
|
||||
linuxclienticon = std::move(val);
|
||||
else if (strcasecmp(key.c_str(), "name") == 0)
|
||||
current.name[""] = std::move(val);
|
||||
break;
|
||||
case other:
|
||||
break;
|
||||
}
|
||||
}
|
||||
void on_uint32_t(const std::string & key, uint32_t val)
|
||||
{
|
||||
switch (section.back())
|
||||
{
|
||||
case common:
|
||||
if (key == "openvrsupport")
|
||||
VR = val;
|
||||
break;
|
||||
case other:
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
} v;
|
||||
|
||||
while (true)
|
||||
{
|
||||
@@ -237,25 +318,40 @@ void read_steam_icons(std::filesystem::path path, std::unordered_map<uint32_t, w
|
||||
+ 4 // checksum
|
||||
+ 1 // type
|
||||
);
|
||||
try
|
||||
if (appids.contains(app_id))
|
||||
{
|
||||
V v;
|
||||
read_dict(in, v, string_table_ptr);
|
||||
if (not(v.current.clienticon.empty() and v.current.linuxclienticon.empty()))
|
||||
icons.emplace(app_id, std::move(v.current));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// Ignore errors
|
||||
try
|
||||
{
|
||||
v.clear();
|
||||
read_dict(in, v, string_table_ptr);
|
||||
if (v.VR)
|
||||
{
|
||||
auto & item = res.emplace_back(std::move(v.current));
|
||||
item.appid = app_id;
|
||||
item.url = std::format("steam://launch/{}/VR", app_id);
|
||||
|
||||
if (auto icon_path = root / "steam/games" / (v.clienticon + ".ico"); std::filesystem::exists(icon_path))
|
||||
item.icon = icon_path;
|
||||
|
||||
else if (auto icon_path = root / "steam/games" / (v.linuxclienticon + ".zip"); std::filesystem::exists(icon_path))
|
||||
item.icon = icon_path;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// Ignore errors
|
||||
}
|
||||
}
|
||||
in.seekg(pos + size_data);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void read_steam_shortcuts(
|
||||
const std::filesystem::path & path,
|
||||
std::vector<wivrn::steam::application> & apps,
|
||||
std::unordered_map<uint32_t, std::filesystem::path> & icons)
|
||||
std::vector<wivrn::steam::application> & apps)
|
||||
{
|
||||
if (not std::filesystem::exists(path))
|
||||
return;
|
||||
@@ -270,11 +366,9 @@ void read_steam_shortcuts(
|
||||
struct V
|
||||
{
|
||||
std::vector<wivrn::steam::application> & apps;
|
||||
std::unordered_map<uint32_t, std::filesystem::path> & icons;
|
||||
|
||||
int depth = 0;
|
||||
wivrn::steam::application current{};
|
||||
std::filesystem::path icon;
|
||||
bool VR = false;
|
||||
|
||||
void begin_dict(const std::string & key)
|
||||
@@ -283,7 +377,6 @@ void read_steam_shortcuts(
|
||||
if (depth == 1)
|
||||
{
|
||||
current = decltype(current){};
|
||||
icon.clear();
|
||||
VR = false;
|
||||
}
|
||||
}
|
||||
@@ -294,8 +387,6 @@ void read_steam_shortcuts(
|
||||
{
|
||||
if (VR)
|
||||
{
|
||||
if (not icon.empty())
|
||||
icons[current.appid] = std::move(icon);
|
||||
// ¯\_(ツ)_/¯
|
||||
current.appid = (current.appid << 32) | 0x02000000;
|
||||
current.url = std::format("steam://rungameid/{}", current.appid);
|
||||
@@ -310,7 +401,7 @@ void read_steam_shortcuts(
|
||||
if (strcasecmp(key.c_str(), "AppName") == 0)
|
||||
current.name[""] = std::move(val);
|
||||
else if (strcasecmp(key.c_str(), "icon") == 0 and not val.empty())
|
||||
icon = std::move(val);
|
||||
current.icon = std::move(val);
|
||||
}
|
||||
}
|
||||
void on_uint32_t(const std::string & key, uint32_t val)
|
||||
@@ -323,7 +414,7 @@ void read_steam_shortcuts(
|
||||
VR = val;
|
||||
}
|
||||
}
|
||||
} v{apps, icons};
|
||||
} v{apps};
|
||||
|
||||
read_dict(in, v);
|
||||
}
|
||||
@@ -371,64 +462,17 @@ std::vector<wivrn::steam> wivrn::steam::find_installations()
|
||||
std::vector<wivrn::steam::application> wivrn::steam::list_applications()
|
||||
{
|
||||
std::vector<wivrn::steam::application> res;
|
||||
// Steam games, from VR manifest
|
||||
try
|
||||
{
|
||||
std::ifstream manifest(root / "config/steamapps.vrmanifest");
|
||||
nlohmann::json json = nlohmann::json::parse(manifest);
|
||||
for (auto & i: json["applications"])
|
||||
{
|
||||
try
|
||||
{
|
||||
// Steam games have an url launch
|
||||
// shortcuts may be in this file, or may not...
|
||||
if (i["launch_type"] != "url")
|
||||
continue;
|
||||
const auto installed = installed_appids(root);
|
||||
|
||||
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));
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cerr << "Failed to parse Steam VR manifest: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
auto const & config = root / "userdata" / std::to_string(*default_userid) / "config";
|
||||
auto ids = owned_appids(config / "localconfig.vdf");
|
||||
std::erase_if(ids, [&](auto id) { return not installed.contains(id); });
|
||||
res = read_appinfo(root, ids);
|
||||
read_steam_shortcuts(config / "shortcuts.vdf", res);
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
@@ -441,7 +485,14 @@ std::vector<wivrn::steam::application> wivrn::steam::list_applications()
|
||||
for (auto const & entry: std::filesystem::directory_iterator{root / "userdata", ec})
|
||||
try
|
||||
{
|
||||
read_steam_shortcuts(entry.path() / "config/shortcuts.vdf", res, shortcut_icons);
|
||||
auto const & config = entry.path() / "config";
|
||||
auto ids = owned_appids(config / "localconfig.vdf");
|
||||
std::erase_if(ids, [&](auto id) { return not installed.contains(id); });
|
||||
auto tmp = read_appinfo(root, ids);
|
||||
res.insert(res.end(),
|
||||
std::make_move_iterator(tmp.begin()),
|
||||
std::make_move_iterator(tmp.end()));
|
||||
read_steam_shortcuts(config / "shortcuts.vdf", res);
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
@@ -451,43 +502,6 @@ std::vector<wivrn::steam::application> wivrn::steam::list_applications()
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user