#pragma warning disable SA1512,SA1124 // Single-line comments should not be followed by blank line using System; using System.Collections.Generic; using System.Threading.Tasks; using JetBrains.Annotations; using NugetForUnity.Helper; using NugetForUnity.PackageSource; using UnityEngine; #region No ReShaper // ReSharper disable All // needed because 'JetBrains.Annotations.NotNull' and 'System.Diagnostics.CodeAnalysis.NotNull' collide if this file is compiled with a never version of Unity / C# using SuppressMessageAttribute = System.Diagnostics.CodeAnalysis.SuppressMessageAttribute; // ReSharper restore All #endregion #pragma warning restore SA1512,SA1124 // Single-line comments should not be followed by blank line namespace NugetForUnity.Models { /// /// Represents a NuGet package that was downloaded from a NuGet server using NuGet API v3. /// [Serializable] internal sealed class NugetPackageV3 : NugetPackageIdentifier, INugetPackage, ISerializationCallbackReceiver { [ItemNotNull] [CanBeNull] [NonSerialized] private IReadOnlyList currentFrameworkDependencies; [SerializeField] private List dependencies = new List(); [SerializeField] private bool dependenciesFetched; [CanBeNull] [NonSerialized] private Task> dependenciesTask; [CanBeNull] [SerializeField] [SuppressMessage("Usage", "CA2235:Mark all non-serializable fields", Justification = "It is a Unity object that can be serialized.")] private Texture2D icon; [ItemCanBeNull] [CanBeNull] [NonSerialized] private Task iconTask; [CanBeNull] [SerializeField] private string iconUrl; [SerializeField] [NotNull] private NugetPackageSourceV3 packageSource; /// /// Initializes a new instance of the class. /// /// The id of the package. /// The version. /// The authors. /// The description. /// The total number of downloads. /// The license URL. /// The source used to receive the package. /// The URL of the project. /// The short summary. /// The human readable title. /// The URL where the icon can be downloaded. /// All available versions. public NugetPackageV3( [NotNull] string id, [NotNull] string version, [NotNull] List authors, [CanBeNull] string description, long totalDownloads, [CanBeNull] string licenseUrl, [NotNull] NugetPackageSourceV3 packageSource, [CanBeNull] string projectUrl, [CanBeNull] string summary, [CanBeNull] string title, [CanBeNull] string iconUrl, List versions) : base(id, version) { Authors = authors; Description = description; TotalDownloads = totalDownloads; LicenseUrl = licenseUrl; this.packageSource = packageSource; ProjectUrl = projectUrl; Summary = summary; Title = title; this.iconUrl = iconUrl; Versions = versions; } /// [field: SerializeField] public List Versions { get; private set; } /// [field: SerializeField] public List Authors { get; private set; } /// public List Dependencies { get { if (dependenciesFetched) { return dependencies; } return Task.Run(GetDependenciesCoreAsync).GetAwaiter().GetResult(); } set { dependencies = value; dependenciesFetched = true; } } /// [field: SerializeField] public string Description { get; private set; } /// [field: SerializeField] public long TotalDownloads { get; private set; } /// [field: SerializeField] public string LicenseUrl { get; private set; } /// public INugetPackageSource PackageSource => packageSource; /// [field: SerializeField] public string ProjectUrl { get; private set; } /// [field: SerializeField] public string Summary { get; private set; } /// [field: SerializeField] public string Title { get; private set; } /// public Task IconTask { get { if (iconTask != null) { return iconTask; } if (!string.IsNullOrEmpty(iconUrl)) { iconTask = NugetPackageTextureHelper.DownloadImageAsync(iconUrl); } return iconTask; } } /// public string ReleaseNotes => string.Empty; /// public RepositoryType RepositoryType => RepositoryType.NotSpecified; /// public string RepositoryUrl => string.Empty; /// public string RepositoryCommit => string.Empty; /// public IReadOnlyList CurrentFrameworkDependencies { get { if (currentFrameworkDependencies == null) { currentFrameworkDependencies = TargetFrameworkResolver.GetBestDependencyFrameworkGroupForCurrentSettings( Dependencies, InstalledPackagesManager.GetPackageConfigurationById(Id)?.TargetFramework) .Dependencies; } return currentFrameworkDependencies; } } /// /// Gets or sets the URL for the location of the actual (.nupkg) NuGet package. /// [CanBeNull] [field: SerializeField] public string DownloadUrl { get; set; } /// IReadOnlyList PluginAPI.Models.INugetPackage.CurrentFrameworkDependencies => CurrentFrameworkDependencies; /// public Task> GetDependenciesAsync() { if (dependenciesFetched) { return Task.FromResult(dependencies); } if (dependenciesTask != null) { return dependenciesTask; } dependenciesTask = GetDependenciesCoreAsync(); return dependenciesTask; } /// public void DownloadNupkgToFile(string outputFilePath) { packageSource.DownloadNupkgToFile(this, outputFilePath, DownloadUrl); } /// public void OnBeforeSerialize() { if (iconTask != null) { icon = iconTask.IsCompleted ? iconTask.Result : null; } } /// public void OnAfterDeserialize() { if (icon != null) { iconTask = Task.FromResult(icon); } } [NotNull] [ItemNotNull] private async Task> GetDependenciesCoreAsync() { NugetLogger.LogVerbose("Fetching dependencies for {0}", this); dependencies = await packageSource.GetPackageDetailsAsync(this); NugetLogger.LogVerbose("Fetched dependencies for {0}", this); dependenciesFetched = true; return dependencies; } } }