diff --git a/common/utils/steam_info.cpp b/common/utils/steam_info.cpp index 5d9e6c54..204cb7ba 100644 --- a/common/utils/steam_info.cpp +++ b/common/utils/steam_info.cpp @@ -65,6 +65,7 @@ public: } std::vector string_table(); + std::vector string_table(uint32_t count); auto tellg() { @@ -101,6 +102,15 @@ std::vector binary_reader::string_table() return res; } +std::vector binary_reader::string_table(uint32_t count) +{ + std::vector res; + res.reserve(count); + for (size_t i = 0; i < count; ++i) + res.emplace_back(read()); + return res; +} + template void read_dict(binary_reader & in, T & visitor, const std::vector * string_table = nullptr) { @@ -217,15 +227,113 @@ std::unordered_set owned_appids(std::filesystem::path localconfig) return res; } -std::vector read_appinfo(std::filesystem::path root, std::unordered_set const & appids) +struct app_data +{ + enum section + { + common, + other, + }; + + std::vector 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) + { + 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; + } + } +}; + +bool is_appid_magic(uint32_t magic) +{ + return (magic & 0xff'ff'ff'00) == 0x07'56'44'00; +} + +void try_parse_application(std::vector & out, std::filesystem::path root, uint32_t app_id, binary_reader & in, app_data & v, const std::vector * string_table) +{ + try + { + v.clear(); + read_dict(in, v, string_table); + if (v.VR) + { + auto & item = out.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 + } +} + +// TODO: Remove when v42 appinfo is used in stable Steam Client +std::vector read_appinfo_pre_v42(std::filesystem::path root, std::unordered_set const & appids) { binary_reader in{std::ifstream{root / "appcache/appinfo.vdf"}}; uint32_t magic = in.read(); - if ((magic & 0xff'ff'ff'00) != 0x07'56'44'00) + if (!is_appid_magic(magic)) throw std::runtime_error{"Wrong magic number"}; - auto universe = in.read(); + in.discard(4); // universe int version = magic & 0xff; @@ -238,71 +346,7 @@ std::vector read_appinfo(std::filesystem::path root, } std::vector res; - enum section - { - common, - other, - }; - struct V - { - std::vector 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) - { - 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; + app_data v; while (true) { @@ -325,33 +369,105 @@ std::vector read_appinfo(std::filesystem::path root, ); if (appids.contains(app_id)) { - 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 - } + try_parse_application(res, root, app_id, in, v, string_table_ptr); } in.seekg(pos + size_data); } return res; } +std::vector read_appinfo_v42(std::filesystem::path root, std::unordered_set const & appids) +{ + std::vector res; + for (auto const & entry: std::filesystem::directory_iterator(root / "appcache/appinfo")) + { + try + { + if (auto extension = entry.path().extension(); extension != ".vdf") + throw std::runtime_error{"Improper file extension"}; + + binary_reader in{std::ifstream{entry.path()}}; + + // https://github.com/xPaw/SteamAppInfo/blob/master/README.md#shard-header + uint32_t magic = in.read(); + if (!is_appid_magic(magic)) + throw std::runtime_error{"Wrong magic number"}; + + int version = magic & 0xff; + if (version < 42) + throw std::runtime_error{std::format("Invalid version {}", version)}; + + in.discard(4 // universe + + 4 // shard_index + + 4 // shard_count + ); + + auto index_count = in.read(); + auto index_size = in.read(); + auto string_table_count = in.read(); + auto string_table_size = in.read(); + + in.discard(4 // data_size + + 4 // index_crc + + 4 // header_crc + ); + + size_t index_data_pos = in.tellg(); + size_t string_table_pos = index_data_pos + index_size; + size_t data_pos = string_table_pos + string_table_size; + + in.seekg(string_table_pos); + std::vector string_table = in.string_table(string_table_count); + + // https://github.com/xPaw/SteamAppInfo/blob/master/README.md#index-entry-repeated + in.seekg(index_data_pos); + app_data v; + for (uint32_t i = 0; i < index_count; i++) + { + auto app_id = in.read(); + if (app_id == 0) + break; + + auto data_offset = in.read(); + in.discard(4); // data_size + auto remaining = in.read(); + auto next = (size_t)in.tellg() + remaining; + + if (appids.contains(app_id)) + { + in.seekg(data_pos + data_offset); + in.discard(20); // SHA-1 of the bin data + + try_parse_application(res, root, app_id, in, v, &string_table); + } + + in.seekg(next); + } + } + catch (std::exception & e) + { + std::cerr << "Failed to read appinfo shard " << entry.path().filename() << ": " << e.what() << std::endl; + } + } + + return res; +} + +std::vector read_appinfo(std::filesystem::path root, std::unordered_set const & appids) +{ + std::error_code ec; + if (std::filesystem::exists(root / "appcache/appinfo.vdf", ec)) + { + return read_appinfo_pre_v42(root, appids); + } + else if (std::filesystem::is_directory(root / "appcache/appinfo", ec)) + { + return read_appinfo_v42(root, appids); + } + + throw std::runtime_error("No appinfos found in appcache"); +} + } // namespace void read_steam_shortcuts(