From 45d5d6dd7aabf9d95be4ae2af9399eb9c51c4d79 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sat, 9 Aug 2025 13:34:21 +0200 Subject: [PATCH] Read Linux steam icons --- CMakeLists.txt | 1 + common/CMakeLists.txt | 2 +- common/application.cpp | 6 +-- common/utils/load_icon.cpp | 65 ++++++++++++++++++++++++++++++++- common/utils/steam_app_info.cpp | 3 ++ 5 files changed, 72 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a3be38d8..540fefd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -185,6 +185,7 @@ endif() if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER) pkg_check_modules(librsvg-2.0 REQUIRED IMPORTED_TARGET librsvg-2.0) + find_package(LibArchive) find_package(PNG) endif() diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index c580a4fd..be306e28 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -50,7 +50,7 @@ if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER) utils/ini.cpp application.cpp ) - target_link_libraries(wivrn-common-server PRIVATE nlohmann_json::nlohmann_json PNG::PNG PkgConfig::librsvg-2.0 PUBLIC wivrn-common-base) + target_link_libraries(wivrn-common-server PRIVATE nlohmann_json::nlohmann_json PNG::PNG PkgConfig::librsvg-2.0 LibArchive::LibArchive PUBLIC wivrn-common-base) if (WIVRN_BUILD_TEST) add_executable(list-apps diff --git a/common/application.cpp b/common/application.cpp index 0e8f6e8c..4992fced 100644 --- a/common/application.cpp +++ b/common/application.cpp @@ -84,7 +84,7 @@ steam_app_info read_steam_app_info(const std::filesystem::path & root) return {}; } -std::filesystem::path 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 steam_app_info & info) { const auto & app_info = info.get(app_id); @@ -102,7 +102,7 @@ std::filesystem::path find_steam_icon(const std::filesystem::path & root, int ap try { - std::string icon{std::get(app_info.at("common.LinuxClientIcon"))}; + std::string icon{std::get(app_info.at("common.linuxclienticon"))}; auto icon_path = root / "steam/games" / (icon + ".zip"); @@ -113,7 +113,7 @@ std::filesystem::path find_steam_icon(const std::filesystem::path & root, int ap { } - return {}; + return std::nullopt; } void read_steam_vr_apps(std::unordered_map & res) diff --git a/common/utils/load_icon.cpp b/common/utils/load_icon.cpp index d973855e..5a3aaffa 100644 --- a/common/utils/load_icon.cpp +++ b/common/utils/load_icon.cpp @@ -18,6 +18,8 @@ #include "load_icon.h" +#include +#include #include #include #include @@ -537,6 +539,67 @@ std::vector load_ico(const std::filesystem::path & filename, int size throw std::runtime_error{"Wrong magic number in " + filename.native()}; } +std::vector load_zip(const std::filesystem::path & filename, int size) +{ + std::unique_ptr zip{archive_read_new(), &archive_read_free}; + + archive_read_support_filter_all(zip.get()); + archive_read_support_format_all(zip.get()); + + if (auto rc = archive_read_open_filename(zip.get(), filename.c_str(), 1'000'000); rc != ARCHIVE_OK) + throw std::runtime_error{"Cannot open " + filename.native() + ": " + archive_error_string(zip.get())}; + + archive_entry * entry; + int width = 0; + int height = 0; + std::vector png; + + while (archive_read_next_header(zip.get(), &entry) == ARCHIVE_OK) + { + printf("%s\n", archive_entry_pathname(entry)); + + std::vector buffer; + buffer.resize(archive_entry_size(entry)); + + if (auto status = archive_read_data(zip.get(), buffer.data(), buffer.size()); status < 0) + continue; + + // Try reading the PNG + std::vector pixels; + png_image png_ref{ + .version = PNG_IMAGE_VERSION, + .format = PNG_FORMAT_BGRA, + }; + + if (not png_image_begin_read_from_memory(&png_ref, buffer.data(), buffer.size())) + { + png_image_free(&png_ref); + continue; + } + + // Keep the largest icon only + if (png_ref.width * png_ref.height < width * height) + continue; + + pixels.resize(PNG_IMAGE_SIZE(png_ref)); + + if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr)) + { + png_image_free(&png_ref); + continue; + } + + // PNG read successfully: keep it until we find a better one + width = png_ref.width; + height = png_ref.height; + png = std::move(buffer); + + png_image_free(&png_ref); + } + + return png; +} + std::unordered_map> icon_cache; } // namespace @@ -561,7 +624,7 @@ const std::vector & wivrn::load_icon(const std::filesystem::path & fi else if (filename.extension() == ".zip") { // Steam icon (Linux) - // TODO + icon = load_zip(filename, size); } else { diff --git a/common/utils/steam_app_info.cpp b/common/utils/steam_app_info.cpp index 9e8e69dd..168240a1 100644 --- a/common/utils/steam_app_info.cpp +++ b/common/utils/steam_app_info.cpp @@ -18,6 +18,7 @@ #include "steam_app_info.h" #include +#include #include #include #include @@ -89,6 +90,8 @@ void read_vdf(wivrn::steam_app_info::info & info, const std::string & prefix, st 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)