Misc optimization

This commit is contained in:
Łukasz Domeradzki 2024-07-11 01:34:49 +02:00
parent f71b0bc945
commit dc57860f0d
No known key found for this signature in database
GPG key ID: 6B138B4C64555AEA

View file

@ -125,24 +125,31 @@ public interface IGitHubPluginUpdates : IPluginUpdates {
throw new ArgumentNullException(nameof(releaseAssets));
}
Dictionary<string, ReleaseAsset> assetsByName = releaseAssets.ToDictionary(static asset => asset.Name, StringComparer.OrdinalIgnoreCase);
// Since we only find assets from available .zip files, we can filter out other assets right away
Dictionary<string, ReleaseAsset> assetsByName = releaseAssets.Where(static asset => asset.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)).ToDictionary(static asset => asset.Name, StringComparer.OrdinalIgnoreCase);
foreach (string possibleMatch in GetPossibleMatches(asfVersion)) {
if (assetsByName.TryGetValue(possibleMatch, out ReleaseAsset? targetAsset)) {
return targetAsset;
}
switch (assetsByName.Count) {
case 0:
// Release does not have a single zip file, so the matching has failed
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateNoAssetFound, Name, Version, newPluginVersion));
return null;
case 1:
// If exactly one match is found, we can return it right away, as it'd be used as a fallback regardless of our matching
return assetsByName.Values.First();
default:
// Otherwise, we have 2+ zip files, so either we match one of those, or fail
foreach (string possibleMatch in GetPossibleMatches(asfVersion)) {
if (assetsByName.TryGetValue(possibleMatch, out ReleaseAsset? targetAsset)) {
return targetAsset;
}
}
// We can't possibly determine which zip file to use, the plugin author should either fix the release, or provide custom GetTargetReleaseAsset() implementation
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateConflictingAssetsFound, Name, Version, newPluginVersion));
return null;
}
// The very last fallback in case user uses different naming scheme
HashSet<ReleaseAsset> zipAssets = releaseAssets.Where(static asset => asset.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)).ToHashSet();
if (zipAssets.Count == 1) {
return zipAssets.First();
}
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateConflictingAssetsFound, Name, Version, newPluginVersion));
return null;
}
protected async Task<Uri?> GetTargetReleaseURL(Version asfVersion, string asfVariant, bool asfUpdate, bool stable, bool forced) {