diff --git a/common/application.cpp b/common/application.cpp index 5b507bf4..6edd8854 100644 --- a/common/application.cpp +++ b/common/application.cpp @@ -75,11 +75,11 @@ std::string read_vr_manifest(const std::filesystem::path & root) return read_file(root / "config/steamapps.vrmanifest"); } -steam_app_info read_steam_app_info(const std::filesystem::path & root) +std::unordered_map safe_read_steam_icons(const std::filesystem::path & root) { try { - return steam_app_info{root / "appcache/appinfo.vdf"}; + return read_steam_icons(root / "appcache/appinfo.vdf"); } catch (...) {} @@ -87,14 +87,15 @@ steam_app_info read_steam_app_info(const std::filesystem::path & root) return {}; } -std::optional find_steam_icon(const std::filesystem::path & root, int app_id, const steam_app_info & info) +std::optional find_steam_icon(const std::filesystem::path & root, int app_id, const std::unordered_map & info) { - const auto & app_info = info.get(app_id); + auto icon = info.find(app_id); + if (icon == info.end()) + return std::nullopt; try { - std::string icon{std::get(app_info.at("common.clienticon"))}; - auto icon_path = root / "steam/games" / (icon + ".ico"); + auto icon_path = root / "steam/games" / (icon->second.clienticon + ".ico"); if (std::filesystem::exists(icon_path)) return icon_path; @@ -105,10 +106,7 @@ std::optional find_steam_icon(const std::filesystem::path try { - std::string icon{std::get(app_info.at("common.linuxclienticon"))}; - - auto icon_path = root / "steam/games" / (icon + ".zip"); - + auto icon_path = root / "steam/games" / (icon->second.linuxclienticon + ".zip"); if (std::filesystem::exists(icon_path)) return icon_path; } @@ -126,7 +124,7 @@ void read_steam_vr_apps(std::unordered_map & res) return; auto manifest = read_vr_manifest(*root); - auto info = read_steam_app_info(*root); + auto info = safe_read_steam_icons(*root); if (manifest.empty()) return; diff --git a/common/utils/steam_app_info.cpp b/common/utils/steam_app_info.cpp index 7933a553..dad88c98 100644 --- a/common/utils/steam_app_info.cpp +++ b/common/utils/steam_app_info.cpp @@ -18,172 +18,9 @@ #include "steam_app_info.h" #include -#include #include #include -#include -#include - -namespace -{ -template -T read(std::span & buffer); - -template - requires std::is_integral_v -T read(std::span & buffer) -{ - if (sizeof(T) > buffer.size()) - throw std::runtime_error{"File truncated"}; - - T value; - memcpy(&value, buffer.data(), sizeof(T)); - buffer = buffer.subspan(sizeof(T)); - - 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(value); - else - return value; -} - -template <> -std::string_view read(std::span & buffer) -{ - size_t size = strnlen((char *)buffer.data(), buffer.size()); - if (size == buffer.size()) - throw std::runtime_error{"File truncated"}; - - char * str = buffer.data(); - buffer = buffer.subspan(size + 1); - return {str, size}; -} - -std::span read(std::span & buffer, size_t size) -{ - if (size > buffer.size()) - throw std::runtime_error{"File truncated"}; - - std::span value{buffer.data(), size}; - buffer = buffer.subspan(size); - return value; -} - -std::vector read_string_table(std::span buffer) -{ - std::vector string_table; - - uint32_t count = read(buffer); - string_table.reserve(count); - - for (size_t i = 0; i < count; i++) - string_table.emplace_back(read(buffer)); - - return string_table; -} - -void read_vdf(wivrn::steam_app_info::info & info, const std::string & prefix, std::span & bindata, const std::vector & string_table) -{ - int type = read(bindata); - - while (type != 8) - { - std::string name{string_table.at(read(bindata))}; - for (char & c: name) - c = std::tolower(c); - - // https://github.com/ValveResourceFormat/ValveKeyValue/blob/master/ValveKeyValue/ValveKeyValue/KeyValues1/KV1BinaryNodeType.cs - switch (type) - { - case 0: // Dictionary - read_vdf(info, prefix + name + ".", bindata, string_table); - break; - - case 1: // UTF-8 string - info.emplace(prefix + name, read(bindata)); - break; - - case 2: // uint32_t - info.emplace(prefix + name, read(bindata)); - break; - - // case 3: // float - // break; - - default: - throw std::runtime_error{"Unknown object type " + std::to_string(type)}; - } - - type = read(bindata); - } -} -} // namespace - -wivrn::steam_app_info::steam_app_info(std::filesystem::path path) -{ - std::vector string_table; - - { - std::ifstream f{path}; - data = std::vector{std::istreambuf_iterator{f}, {}}; - } - std::span buffer = data; - - // See https://github.com/SteamDatabase/SteamAppInfo/blob/master/README.md#file-header - uint32_t magic = read(buffer); - if ((magic & 0xff'ff'ff'00) != 0x07'56'44'00) - throw std::runtime_error{"Wrong magic number"}; - int version = magic & 0xff; - - if (version < 41) - throw std::runtime_error{"Unsupported version"}; - - int universe = read(buffer); - if (version >= 41) - { - size_t string_offset = read(buffer); - string_table = read_string_table(std::span{data}.subspan(string_offset)); - } - else - throw std::runtime_error{"Unsupported version"}; - - while (true) - { - // See https://github.com/SteamDatabase/SteamAppInfo/blob/master/README.md#app-entry-repeated - - auto app_id = read(buffer); - if (app_id == 0) - break; - - auto size_data = read(buffer); - char * current_offset = buffer.data(); - - read(buffer); // info_state - read(buffer); // last_updated - read(buffer); // pics_token - read(buffer, 20); // SHA1 of the app info - read(buffer); // change number - // if (version < 38) - // read(buffer); // section type - read(buffer, 20); // SHA1 of the bin data - auto bindata = read(buffer, size_data - (buffer.data() - current_offset)); - - try - { - bindata = bindata.subspan(4 + 1); // Skip the checksum + type (must be 0) - - info app_info; - read_vdf(app_info, "", bindata, string_table); - - app_data.emplace(app_id, app_info); - } - catch (...) - { - // Ignore errors - } - } -} +#include namespace { @@ -195,6 +32,11 @@ class binary_reader public: binary_reader(std::ifstream && in) : in{std::move(in)} {} + void discard(size_t bytes) + { + in.seekg(bytes, std::ios_base::cur); + } + template T read(); @@ -214,6 +56,17 @@ public: else return res; } + + std::vector string_table(); + + auto tellg() + { + return in.tellg(); + } + void seekg(size_t pos) + { + in.seekg(pos); + } }; template <> @@ -226,6 +79,21 @@ std::string binary_reader::read() return res; } +std::vector binary_reader::string_table() +{ + std::vector res; + size_t string_offset = read(); + auto pos = in.tellg(); + + in.seekg(string_offset); + auto count = read(); + res.reserve(count); + for (size_t i = 0; i < count; ++i) + res.emplace_back(read()); + in.seekg(pos); + return res; +} + template void read_dict(binary_reader & in, T & visitor, const std::vector * string_table = nullptr) { @@ -260,27 +128,87 @@ void read_dict(binary_reader & in, T & visitor, const std::vector * } } } +} // namespace -template -void read_vdf(binary_reader & in, T & visitor, int version) +std::unordered_map wivrn::read_steam_icons(std::filesystem::path path) { + binary_reader in{std::ifstream{path}}; + + uint32_t magic = in.read(); + if ((magic & 0xff'ff'ff'00) != 0x07'56'44'00) + throw std::runtime_error{"Wrong magic number"}; + + auto universe = in.read(); + + int version = magic & 0xff; + + std::vector string_table; + std::vector * string_table_ptr = nullptr; 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); + string_table = in.string_table(); + string_table_ptr = &string_table; } - else + + std::unordered_map res; + + struct V { - read_dict(in, visitor); - } -} + wivrn::steam_icon current{}; -} // namespace + void begin_dict(const std::string & key) + { + } + void end_dict() + { + } + void on_string(const std::string & key, std::string && val) + { + if (key == "clienticon") + current.clienticon = std::move(val); + else if (key == "linuxclienticon") + current.linuxclienticon = std::move(val); + } + void on_uint32_t(const std::string & key, uint32_t val) + { + } + }; + + while (true) + { + // See https://github.com/SteamDatabase/SteamAppInfo/blob/master/README.md#app-entry-repeated + auto app_id = in.read(); + if (app_id == 0) + break; + + size_t size_data = in.read(); + size_t pos = in.tellg(); + in.discard( + 4 // info_state + + 4 // last_updated + + 8 // pics_token + + 20 // SHA1 of the app info + + 4 // change number + + 20 // SHA1 of the bin data + + 4 // checksum + + 1 // type + ); + try + { + 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)); + } + catch (...) + { + // Ignore errors + } + in.seekg(pos + size_data); + } + + return res; +} std::vector wivrn::read_steam_shortcuts(std::filesystem::path path) { @@ -340,7 +268,7 @@ std::vector wivrn::read_steam_shortcuts(std::filesystem:: } } v{res}; - read_vdf(in, v, 0); + read_dict(in, v); return res; } diff --git a/common/utils/steam_app_info.h b/common/utils/steam_app_info.h index 65f6be27..640dcc93 100644 --- a/common/utils/steam_app_info.h +++ b/common/utils/steam_app_info.h @@ -21,33 +21,15 @@ #include #include #include -#include #include namespace wivrn { -class steam_app_info + +struct steam_icon { -public: - using info = std::unordered_map>; - -private: - std::vector data; - - std::unordered_map app_data; - -public: - steam_app_info(std::filesystem::path path); - steam_app_info() = default; - steam_app_info(const steam_app_info &) = delete; - steam_app_info(steam_app_info &&) = default; - steam_app_info & operator=(const steam_app_info &) = delete; - steam_app_info & operator=(steam_app_info &&) = default; - - const auto & get(int appid) const - { - return app_data.at(appid); - } + std::string clienticon; + std::string linuxclienticon; }; struct steam_shortcut @@ -57,6 +39,7 @@ struct steam_shortcut std::optional icon; }; +std::unordered_map read_steam_icons(std::filesystem::path path); std::vector read_steam_shortcuts(std::filesystem::path path); } // namespace wivrn