Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diversify Icon Detection (Icons for Steam, Local PC, and Microsoft Store Applications) #3189

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using UniGetUI.Core.Logging;
using UniGetUI.Core.Tools;
Expand Down Expand Up @@ -304,6 +305,46 @@ public void GetPackageDetails_UnSafe(IPackageDetails details)
{
details.ManifestUrl = new Uri("https://apps.microsoft.com/detail/" + details.Package.Id);
}
else if (details.Package.Source.Name == "Steam")
{
string AppId = details.Package.Id.Split(" ")[^1];
JsonNode? SteamManifest = JsonObject.Parse(WinGetPkgDetailsHelper.GetSteamManifest(details.Package) ?? "{}")?[AppId]?["data"];
if (SteamManifest?["developers"]?.AsArray() != null && SteamManifest?["developers"]?.AsArray().Count > 0)
{
details.Author = string.Join(", ", SteamManifest!["developers"]!.AsArray() ?? []);
}
if (SteamManifest?["short_description"] != null)
{
details.Description = SteamManifest["short_description"]!.ToString();
}
if (SteamManifest?["website"] != null)
{
details.HomepageUrl = new Uri(SteamManifest["website"]!.ToString());
}
details.ManifestUrl = new Uri($"https://store.steampowered.com/api/appdetails/?appids={AppId}");
if (SteamManifest?["publishers"]?.AsArray() != null && SteamManifest?["publishers"]?.AsArray().Count > 0)
{
details.Publisher = string.Join(", ", SteamManifest!["publishers"]!.AsArray() ?? []);
}
List<string> Tags = [];
if (SteamManifest?["categories"]?.AsArray() != null && SteamManifest?["categories"]?.AsArray().Count > 0)
{
foreach (JsonNode? category in SteamManifest!["categories"]!.AsArray())
{
if (category?.AsObject()?["description"] != null)
{
Tags.Add(category!.AsObject()!["description"]!.ToString());
}
}
}
if (Tags.Count > 0)
{
details.Tags = [.. Tags];
}
details.InstallerType = "Steam";
details.InstallerUrl = new Uri($"steam://install/{AppId}");
return;
}

// Get the output for the best matching locale
Process process = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Text.Json.Nodes;
using Microsoft.Management.Deployment;
using UniGetUI.Core.Classes;
using UniGetUI.Core.Logging;
Expand All @@ -10,6 +11,7 @@
using UniGetUI.PackageEngine.PackageClasses;
using UniGetUI.PackageEngine.Structs;
using WindowsPackageManager.Interop;
using WinRT;

namespace UniGetUI.PackageEngine.Managers.WingetManager;

Expand Down Expand Up @@ -318,6 +320,46 @@ public void GetPackageDetails_UnSafe(IPackageDetails details)
{
details.ManifestUrl = new Uri("https://apps.microsoft.com/detail/" + details.Package.Id);
}
else if (details.Package.Source.Name == "Steam")
{
string AppId = details.Package.Id.Split(" ")[^1];
JsonNode? SteamManifest = JsonObject.Parse(WinGetPkgDetailsHelper.GetSteamManifest(details.Package) ?? "{}")?[AppId]?["data"];
if (SteamManifest?["developers"]?.AsArray() != null && SteamManifest?["developers"]?.AsArray().Count > 0)
{
details.Author = string.Join(", ", SteamManifest!["developers"]!.AsArray() ?? []);
}
if (SteamManifest?["short_description"] != null)
{
details.Description = SteamManifest["short_description"]!.ToString();
}
if (SteamManifest?["website"] != null)
{
details.HomepageUrl = new Uri(SteamManifest["website"]!.ToString());
}
details.ManifestUrl = new Uri($"https://store.steampowered.com/api/appdetails/?appids={AppId}");
if (SteamManifest?["publishers"]?.AsArray() != null && SteamManifest?["publishers"]?.AsArray().Count > 0)
{
details.Publisher = string.Join(", ", SteamManifest!["publishers"]!.AsArray() ?? []);
}
List<string> Tags = [];
if (SteamManifest?["categories"]?.AsArray() != null && SteamManifest?["categories"]?.AsArray().Count > 0)
{
foreach (JsonNode? category in SteamManifest!["categories"]!.AsArray())
{
if (category?.AsObject()?["description"] != null)
{
Tags.Add(category!.AsObject()!["description"]!.ToString());
}
}
}
if (Tags.Count > 0)
{
details.Tags = [.. Tags];
}
details.InstallerType = "Steam";
details.InstallerUrl = new Uri($"steam://install/{AppId}");
return;
}

CatalogPackageMetadata? NativeDetails = NativePackageHandler.GetDetails(details.Package);
if (NativeDetails is null) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Globalization;
using System.Net;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using Microsoft.Management.Deployment;
using UniGetUI.Core.IconEngine;
Expand All @@ -12,6 +15,7 @@ namespace UniGetUI.PackageEngine.Managers.WingetManager
internal sealed class WinGetPkgDetailsHelper : BasePkgDetailsHelper
{
private static readonly Dictionary<string, string> __msstore_package_manifests = [];
private static readonly Dictionary<string, string> __steam_package_manifests = [];

public WinGetPkgDetailsHelper(WinGet manager) : base(manager) { }

Expand Down Expand Up @@ -39,6 +43,26 @@ protected override void GetDetails_UnSafe(IPackageDetails details)

protected override IEnumerable<Uri> GetScreenshots_UnSafe(IPackage package)
{
if (package.Source.Name == "Steam")
{
string? SteamResponseContent = GetSteamManifest(package);
if (SteamResponseContent is null)
{
return [];
}

string AppId = package.Id.Split(" ")[^1];
List<Uri> Screenshots = [];
foreach (JsonNode? Screenshot in JsonObject.Parse(SteamResponseContent)?[AppId]?["data"]?["screenshots"]?.AsArray() ?? []) {
string? ScreenshotUrl = Screenshot?.AsObject()["path_full"]?.ToString();
if (ScreenshotUrl != null)
{
Screenshots.Add(new Uri(ScreenshotUrl));
}
}
return Screenshots;
}

if (package.Source.Name != "msstore")
{
return [];
Expand Down Expand Up @@ -215,6 +239,45 @@ protected override IEnumerable<Uri> GetScreenshots_UnSafe(IPackage package)
return new CacheableIcon(new Uri(uri));
}

public static string? GetSteamManifest(IPackage package)
{
if (__steam_package_manifests.TryGetValue(package.Id, out var manifest))
{
return manifest;
}

string AppId = package.Id.Split(" ")[^1];
string url = $"https://store.steampowered.com/api/appdetails/?appids={AppId}";

#pragma warning disable SYSLIB0014
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
#pragma warning restore SYSLIB0014

httpRequest.Method = "GET";

HttpWebResponse? httpResponse = httpRequest.GetResponse() as HttpWebResponse;
if (httpResponse is null)
{
Logger.Warn($"Null Steam response for uri={url}");
return null;
}

string result;
using (StreamReader streamReader = new(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}

Logger.Debug("Steam API call status code: " + httpResponse.StatusCode);

if (result != "" && httpResponse.StatusCode == HttpStatusCode.OK)
{
__steam_package_manifests[package.Id] = result;
}

return result;
}

private static CacheableIcon? GetWinGetPackageIcon(IPackage package)
{
CatalogPackageMetadata? NativeDetails = NativePackageHandler.GetDetails(package);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public Package(
"Scoop" => id.ToLower().Replace(".app", ""),
"Chocolatey" => id.ToLower().Replace(".install", "").Replace(".portable", ""),
"vcpkg" => id.ToLower().Split(":")[0].Split("[")[0],
"Steam" => id.ToLower().Split("\\")[^1].Replace("Steam App", "steam:"),
"Local PC" => id.ToLower().Split("\\")[^1],
"Microsoft Store" => id.ToLower().Split(".")[1].Split("_")[0],
_ => id.ToLower()
};
}
Expand Down
12 changes: 11 additions & 1 deletion src/UniGetUI/Pages/DialogPages/PackageDetailsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ public async Task LoadInformation()
SetTextToItem(InstallerHash_Content, details.InstallerHash);
if (details.InstallerUrl is not null)
{
SetTextToItem(InstallerSize_Content, details.InstallerSize > 0 ? $" ({details.InstallerSize} MB)" : $" ({CoreTools.Translate("Unknown size")})");
if (details.Package.Source.Name != "Steam")
SetTextToItem(InstallerSize_Content, details.InstallerSize > 0 ? $" ({details.InstallerSize} MB)" : $" ({CoreTools.Translate("Unknown size")})");
SetTextToItem(DownloadInstaller_Button, CoreTools.Translate("Download installer"));
}
else
Expand Down Expand Up @@ -460,6 +461,15 @@ public async void DownloadInstallerButton_Click(object sender, RoutedEventArgs e
{
return;
}
if (Package.Details.InstallerUrl.ToString().StartsWith("steam://"))
{
Process.Start(new ProcessStartInfo
{
FileName = Package.Details.InstallerUrl.ToString(),
UseShellExecute = true
});
return;
}

FileSavePicker savePicker = new();
MainWindow window = MainApp.Instance.MainWindow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ protected void ClearSourceSelectionButton_Click(object sender, RoutedEventArgs e

protected void ShowDetailsForPackage(IPackage? package)
{
if (package is null || package.Source.IsVirtualManager || package is InvalidImportedPackage)
if (package is null || (package.Source.IsVirtualManager && package.Source.Name != "Steam") || package is InvalidImportedPackage)
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/UniGetUI/Pages/SoftwarePages/InstalledPackagesPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ protected override async void WhenShowingContextMenu(IPackage package)
MenuUninstallThenReinstall.IsEnabled = !IS_LOCAL;
MenuIgnoreUpdates.IsEnabled = false; //!IS_LOCAL;
MenuSharePackage.IsEnabled = !IS_LOCAL;
MenuPackageDetails.IsEnabled = !IS_LOCAL;
MenuPackageDetails.IsEnabled = !IS_LOCAL || package.Source.Name == "Steam";

MenuOpenInstallLocation.IsEnabled = package.Manager.DetailsHelper.GetInstallLocation(package) is not null;
if (!IS_LOCAL)
Expand Down
Loading