|
| 1 | +using System.Collections.Concurrent; |
| 2 | +using Microsoft.Management.Deployment; |
| 3 | +using UniGetUI.Core.Logging; |
| 4 | +using UniGetUI.PackageEngine.Enums; |
| 5 | +using UniGetUI.PackageEngine.Interfaces; |
| 6 | + |
| 7 | +namespace UniGetUI.PackageEngine.Managers.WingetManager; |
| 8 | + |
| 9 | +public static class NativePackageHandler |
| 10 | +{ |
| 11 | + private static ConcurrentDictionary<long, CatalogPackage> __nativePackages = new(); |
| 12 | + private static ConcurrentDictionary<long, CatalogPackageMetadata> __nativeDetails = new(); |
| 13 | + private static ConcurrentDictionary<long, PackageInstallerInfo> __nativeInstallers_Install = new(); |
| 14 | + private static ConcurrentDictionary<long, PackageInstallerInfo> __nativeInstallers_Uninstall = new(); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Get (cache or load) the native package for the given package, if any; |
| 18 | + /// </summary> |
| 19 | + /// <returns></returns> |
| 20 | + public static CatalogPackage? GetPackage(IPackage package) |
| 21 | + { |
| 22 | + if (NativeWinGetHelper.ExternalFactory is null || NativeWinGetHelper.ExternalManager is null) |
| 23 | + return null; |
| 24 | + |
| 25 | + __nativePackages.TryGetValue(package.GetHash(), out CatalogPackage? catalogPackage); |
| 26 | + if (catalogPackage is not null) |
| 27 | + return catalogPackage; |
| 28 | + |
| 29 | + PackageCatalogReference Catalog = NativeWinGetHelper.ExternalManager.GetPackageCatalogByName(package.Source.Name); |
| 30 | + if (Catalog is null) |
| 31 | + { |
| 32 | + Logger.Error("Failed to get catalog " + package.Source.Name + ". Is the package local?"); |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + // Connect to catalog |
| 37 | + Catalog.AcceptSourceAgreements = true; |
| 38 | + ConnectResult ConnectResult = Catalog.Connect(); |
| 39 | + if (ConnectResult.Status != ConnectResultStatus.Ok) |
| 40 | + { |
| 41 | + Logger.Error("Failed to connect to catalog " + package.Source.Name); |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + // Match only the exact same Id |
| 46 | + FindPackagesOptions packageMatchFilter = NativeWinGetHelper.ExternalFactory.CreateFindPackagesOptions(); |
| 47 | + PackageMatchFilter filters = NativeWinGetHelper.ExternalFactory.CreatePackageMatchFilter(); |
| 48 | + filters.Field = PackageMatchField.Id; |
| 49 | + filters.Value = package.Id; |
| 50 | + filters.Option = PackageFieldMatchOption.Equals; |
| 51 | + packageMatchFilter.Filters.Add(filters); |
| 52 | + packageMatchFilter.ResultLimit = 1; |
| 53 | + var SearchResult = Task.Run(() => ConnectResult.PackageCatalog.FindPackages(packageMatchFilter)); |
| 54 | + |
| 55 | + if (SearchResult?.Result?.Matches is null || |
| 56 | + SearchResult.Result.Matches.Count == 0) |
| 57 | + { |
| 58 | + Logger.Error("Failed to find package " + package.Id + " in catalog " + package.Source.Name); |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + // Get the Native Package |
| 63 | + catalogPackage = SearchResult.Result.Matches.First().CatalogPackage; |
| 64 | + AddPackage(package, catalogPackage); |
| 65 | + |
| 66 | + return catalogPackage; |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Adds an external CatalogPackage to the internal database |
| 71 | + /// </summary> |
| 72 | + public static void AddPackage(IPackage package, CatalogPackage catalogPackage) |
| 73 | + { |
| 74 | + __nativePackages[package.GetHash()] = catalogPackage; |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Get (cached or load) the native package details for the given package, if any; |
| 79 | + /// </summary> |
| 80 | + public static CatalogPackageMetadata? GetDetails(IPackage package) |
| 81 | + { |
| 82 | + if (__nativeDetails.TryGetValue(package.GetHash(), out CatalogPackageMetadata? metadata)) |
| 83 | + return metadata; |
| 84 | + |
| 85 | + CatalogPackage? catalogPackage = GetPackage(package); |
| 86 | + metadata = catalogPackage?.DefaultInstallVersion?.GetCatalogPackageMetadata(); |
| 87 | + metadata ??= catalogPackage?.InstalledVersion?.GetCatalogPackageMetadata(); |
| 88 | + |
| 89 | + if (metadata is not null) |
| 90 | + __nativeDetails[package.GetHash()] = metadata; |
| 91 | + |
| 92 | + return metadata; |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Get (cached or load) the native installer for the given package, if any. The operation type determines wether |
| 97 | + /// </summary> |
| 98 | + public static PackageInstallerInfo? GetInstallationOptions(IPackage package, OperationType operation) |
| 99 | + { |
| 100 | + if (NativeWinGetHelper.ExternalFactory is null) |
| 101 | + return null; |
| 102 | + |
| 103 | + PackageInstallerInfo? installerInfo = null; |
| 104 | + if (operation is OperationType.Uninstall) |
| 105 | + installerInfo = _getInstallationOptionsOnDict(package, ref __nativeInstallers_Uninstall, true); |
| 106 | + else |
| 107 | + installerInfo = _getInstallationOptionsOnDict(package, ref __nativeInstallers_Uninstall, false); |
| 108 | + |
| 109 | + return installerInfo; |
| 110 | + } |
| 111 | + |
| 112 | + public static void Clear() |
| 113 | + { |
| 114 | + __nativePackages.Clear();; |
| 115 | + __nativeDetails.Clear();; |
| 116 | + __nativeInstallers_Install.Clear();; |
| 117 | + __nativeInstallers_Uninstall.Clear(); |
| 118 | + } |
| 119 | + |
| 120 | + private static PackageInstallerInfo? _getInstallationOptionsOnDict(IPackage package, ref ConcurrentDictionary<long, PackageInstallerInfo> source, bool installed) |
| 121 | + { |
| 122 | + if (source.TryGetValue(package.GetHash(), out PackageInstallerInfo? installerInfo)) |
| 123 | + return installerInfo; |
| 124 | + |
| 125 | + PackageVersionInfo? catalogPackage; |
| 126 | + if (installed) catalogPackage = GetPackage(package)?.InstalledVersion; |
| 127 | + else catalogPackage = GetPackage(package)?.DefaultInstallVersion; |
| 128 | + |
| 129 | + InstallOptions? options = NativeWinGetHelper.ExternalFactory?.CreateInstallOptions(); |
| 130 | + installerInfo = catalogPackage?.GetApplicableInstaller(options); |
| 131 | + |
| 132 | + if (installerInfo is not null) |
| 133 | + source[package.GetHash()] = installerInfo; |
| 134 | + |
| 135 | + return installerInfo; |
| 136 | + } |
| 137 | +} |
0 commit comments