Skip to content

Commit

Permalink
NXM Downloads as jobs (#1857)
Browse files Browse the repository at this point in the history
* Add NXM Downloader using Jobs

* Fix packaging

* Fix tests
  • Loading branch information
erri120 authored Aug 8, 2024
1 parent 0fd7266 commit 5b560e4
Show file tree
Hide file tree
Showing 44 changed files with 548 additions and 1,200 deletions.
1 change: 1 addition & 0 deletions scripts/validate-nupkgs.ps1
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#! /usr/bin/env pwsh
# This script extracts all "*.nupkg" files and parses the ".nuspec" file.
# It looks at the dependencies of that package and alerts if a dependency is
# a local project in the current solution that isn't being packaged and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace NexusMods.Abstractions.Downloads;
public interface IDownloadJob : IPersistedJob
{
/// <summary>
/// Gets the path where the file will be downloaded to.
/// Gets the destination path for the downloaded file.
/// </summary>
AbsolutePath DownloadPath { get; }
AbsolutePath Destination { get; }

/// <summary>
/// Adds metadata from the download to the library file
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using JetBrains.Annotations;
using NexusMods.Abstractions.Downloads;
using NexusMods.Abstractions.Library.Models;

namespace NexusMods.Abstractions.HttpDownloads;

Expand All @@ -12,5 +13,8 @@ public interface IHttpDownloadJob : IDownloadJob
/// <summary>
/// Gets the URI to download.
/// </summary>
Uri DownloadUri { get; }
Uri Uri { get; }

/// <inheritdoc cref="DownloadedFile.DownloadPageUri"/>
Uri DownloadPageUri { get; }
}
2 changes: 1 addition & 1 deletion src/Abstractions/NexusMods.Abstractions.Jobs/AJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static MutableProgress CreateGroupProgress()
throw new NotImplementedException();
}

internal void SetStatus(JobStatus value)
internal virtual void SetStatus(JobStatus value)
{
Status.AssertTransition(value);
Status = value;
Expand Down
25 changes: 21 additions & 4 deletions src/Abstractions/NexusMods.Abstractions.Jobs/APersistedJob.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
using JetBrains.Annotations;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.Attributes;
using NexusMods.MnemonicDB.Abstractions.IndexSegments;

namespace NexusMods.Abstractions.Jobs;

/// <summary>
/// Base class for persisted jobs.
/// </summary>
[PublicAPI]
public abstract class APersistedJob : AJob, IPersistedJob
{
private readonly IConnection _connection;

/// <inheritdoc />
protected APersistedJob(IConnection connection, PersistedJobStateId id, MutableProgress progress, IJobGroup? group = default, IJobWorker? worker = default, IJobMonitor? monitor = default) :
base(progress, group, worker, monitor)
protected APersistedJob(
IConnection connection,
PersistedJobState.ReadOnly state,
MutableProgress progress,
IJobGroup? group = default,
IJobWorker? worker = default,
IJobMonitor? monitor = default) : base(progress, group, worker, monitor)
{
_connection = connection;
PersistedJobStateId = id;
_state = PersistedJobState.Load(_connection.Db, PersistedJobStateId);

PersistedJobStateId = state;
_state = state;
}

/// <inheritdoc />
Expand Down Expand Up @@ -62,4 +73,10 @@ public async Task Set<THighLevel, TLowLevel>(ScalarAttribute<THighLevel, TLowLev
await tx.Commit();
_state = _state.Rebase();
}

internal override void SetStatus(JobStatus value)
{
base.SetStatus(value);
// TODO: Set(PersistedJobState.Status, value);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using JetBrains.Annotations;

namespace NexusMods.Abstractions.Jobs;

/// <summary>
/// Base class for persisted job workers.
/// </summary>
[PublicAPI]
public abstract class APersistedJobWorker<T> : AJobWorker<T>, IPersistedJobWorker
where T : AJob
{
private PersistedJobStateId _stateId;

/// <inheritdoc />
public abstract Guid Id { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using JetBrains.Annotations;
using NexusMods.Abstractions.Downloads;
using NexusMods.Abstractions.HttpDownloads;

namespace NexusMods.Abstractions.NexusModsLibrary;

/// <summary>
/// Represents an NXM download.
/// </summary>
[PublicAPI]
public interface INXMDownloadJob : IHttpDownloadJob;
public interface INXMDownloadJob : IDownloadJob
{
NexusModsFileMetadata.ReadOnly FileMetadata { get; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- NuGet Package Shared Details -->
<Import Project="$([MSBuild]::GetPathOfFileAbove('NuGet.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<ItemGroup>
<ProjectReference Include="..\NexusMods.Abstractions.Downloads\NexusMods.Abstractions.Downloads.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public partial class NexusModsModPageMetadata : IModelDefinition
/// Back-reference to all files from this page.
/// </summary>
public static readonly BackReferenceAttribute<NexusModsFileMetadata> Files = new(NexusModsFileMetadata.ModPage);

public partial struct ReadOnly
{
public Uri GetUri() => new($"https://nexusmods.com/{GameDomain}/mods/{ModId}");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;

namespace NexusMods.Extensions.BCL;

Expand All @@ -13,7 +14,8 @@ public static class EnumerableExtensions
/// in a try-get style. This is helpful for value types like structs that have a non-null default value.
/// </summary>
/// <returns></returns>
public static bool TryGetFirst<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate, out T? value)
public static bool TryGetFirst<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate, [NotNullWhen(true)] out T? value)
where T : notnull
{
foreach (var item in enumerable)
{
Expand All @@ -22,7 +24,7 @@ public static bool TryGetFirst<T>(this IEnumerable<T> enumerable, Func<T, bool>
return true;
}

value = default;
value = default(T);
return false;
}

Expand Down
55 changes: 0 additions & 55 deletions src/Networking/NexusMods.Networking.Downloaders/HttpDownloadJob.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,4 @@
<InternalsVisibleTo Include="NexusMods.Networking.Downloaders.Tests" />
<PackageReference Include="ReactiveUI.Fody"/>
</ItemGroup>

<ItemGroup>
<Compile Update="HttpDownloadJobWorker.cs">
<DependentUpon>HttpDownloadJob.cs</DependentUpon>
</Compile>
<Compile Update="HttpDownloadJobPersistedState.cs">
<DependentUpon>HttpDownloadJob.cs</DependentUpon>
</Compile>
</ItemGroup>
</Project>
2 changes: 0 additions & 2 deletions src/Networking/NexusMods.Networking.Downloaders/Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public static IServiceCollection AddDownloaders(this IServiceCollection services
.AddSingleton<IDownloadService>(sp=> sp.GetRequiredService<DownloadService>())
.AddTransient<NxmDownloadTask>()
.AddTransient<HttpDownloadTask>()
.AddWorker<HttpDownloadJobWorker>()
.AddHttpDownloadJobPersistedStateModel()
.AddAttributeCollection(typeof(DownloaderState))
.AddAttributeCollection(typeof(HttpDownloadState))
.AddAttributeCollection(typeof(NxmDownloadState))
Expand Down
Loading

0 comments on commit 5b560e4

Please sign in to comment.