-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2116 from erri120/tech/stardew-valley-pipelines
Add manifest pipeline for Stardew Valley
- Loading branch information
Showing
6 changed files
with
94 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Reactive; | ||
using System.Text; | ||
using BitFaster.Caching.Lru; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NexusMods.Abstractions.IO; | ||
using NexusMods.Abstractions.Resources; | ||
using NexusMods.Abstractions.Resources.Caching; | ||
using NexusMods.Abstractions.Resources.IO; | ||
using NexusMods.Games.StardewValley.Models; | ||
using NexusMods.Hashing.xxHash64; | ||
using SMAPIManifest = StardewModdingAPI.Toolkit.Serialization.Models.Manifest; | ||
|
||
namespace NexusMods.Games.StardewValley; | ||
|
||
internal static class Pipelines | ||
{ | ||
public const string ManifestPipelineKey = nameof(ManifestPipelineKey); | ||
|
||
public static IServiceCollection AddPipelines(this IServiceCollection serviceCollection) | ||
{ | ||
return serviceCollection.AddKeyedSingleton<IResourceLoader<SMAPIModLoadoutItem.ReadOnly, SMAPIManifest>>( | ||
serviceKey: ManifestPipelineKey, | ||
implementationFactory: static (serviceProvider, _) => CreateManifestPipeline( | ||
fileStore: serviceProvider.GetRequiredService<IFileStore>() | ||
) | ||
); | ||
} | ||
|
||
public static IResourceLoader<SMAPIModLoadoutItem.ReadOnly, SMAPIManifest> GetManifestPipeline(IServiceProvider serviceProvider) | ||
{ | ||
return serviceProvider.GetRequiredKeyedService<IResourceLoader<SMAPIModLoadoutItem.ReadOnly, SMAPIManifest>>(serviceKey: ManifestPipelineKey); | ||
} | ||
|
||
private static IResourceLoader<SMAPIModLoadoutItem.ReadOnly, SMAPIManifest> CreateManifestPipeline(IFileStore fileStore) | ||
{ | ||
var pipeline = new FileStoreLoader(fileStore) | ||
.ThenDo(Unit.Default, static (_, _, resource, _) => | ||
{ | ||
var bytes = resource.Data; | ||
var json = Encoding.UTF8.GetString(bytes); | ||
|
||
var manifest = Interop.SMAPIJsonHelper.Deserialize<SMAPIManifest>(json); | ||
ArgumentNullException.ThrowIfNull(manifest); | ||
|
||
return ValueTask.FromResult(resource.WithData(manifest)); | ||
}) | ||
.UseCache( | ||
keyGenerator: static hash => hash, | ||
keyComparer: EqualityComparer<Hash>.Default, | ||
capacityPartition: new FavorWarmPartition(totalCapacity: 100) | ||
) | ||
.ChangeIdentifier<SMAPIModLoadoutItem.ReadOnly, Hash, SMAPIManifest>( | ||
static mod => mod.Manifest.AsLoadoutFile().Hash | ||
); | ||
|
||
return pipeline; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters