Skip to content

Commit

Permalink
Use Outcome rather than OneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Al12rs committed Oct 21, 2024
1 parent 5573c29 commit 3fe4c84
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
using NexusMods.Abstractions.Diagnostics.References;
using NexusMods.Abstractions.Diagnostics.Values;
using NexusMods.Abstractions.Loadouts;
using NexusMods.Abstractions.Loadouts.Extensions;
using NexusMods.Abstractions.Resources;
using NexusMods.Abstractions.Telemetry;
using NexusMods.Games.Larian.BaldursGate3.Utils.LsxXmlParsing;
using NexusMods.Hashing.xxHash64;
using OneOf.Types;
using Polly;

namespace NexusMods.Games.Larian.BaldursGate3.Emitters;

public class DependencyDiagnosticEmitter : ILoadoutDiagnosticEmitter
{
private readonly ILogger _logger;
private readonly IResourceLoader<Hash, OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>> _metadataPipeline;
private readonly IResourceLoader<Hash, Outcome<LsxXmlFormat.MetaFileData>> _metadataPipeline;

public DependencyDiagnosticEmitter(IServiceProvider serviceProvider, ILogger<DependencyDiagnosticEmitter> logger)
{
Expand Down Expand Up @@ -54,7 +53,7 @@ private async Task<IEnumerable<Diagnostic>> DiagnosePakModulesAsync(Loadout.Read
var loadoutItemGroup = mod.AsLoadoutItemWithTargetPath().AsLoadoutItem().Parent;

// error case
if (metadataOrError.IsT1)
if (metadataOrError.Exception is not null)
{
diagnostics.Add(Diagnostics.CreateInvalidPakFile(
ModName: loadoutItemGroup.ToReference(loadout),
Expand All @@ -65,7 +64,7 @@ private async Task<IEnumerable<Diagnostic>> DiagnosePakModulesAsync(Loadout.Read
}

// non error case
var metadata = metadataOrError.AsT0;
var metadata = metadataOrError.Result;
var dependencies = metadata.Dependencies;

foreach (var dependency in dependencies)
Expand All @@ -76,8 +75,8 @@ private async Task<IEnumerable<Diagnostic>> DiagnosePakModulesAsync(Loadout.Read

var matchingDeps = metaFileTuples.Where(
x =>
x.Item2.IsT0 &&
x.Item2.AsT0.ModuleShortDesc.Uuid == dependencyUuid
x.Item2.Exception is null &&
x.Item2.Result.ModuleShortDesc.Uuid == dependencyUuid
)
.ToArray();

Expand All @@ -100,9 +99,9 @@ private async Task<IEnumerable<Diagnostic>> DiagnosePakModulesAsync(Loadout.Read
continue;

var highestInstalledMatch = matchingDeps.MaxBy(
x => x.Item2.AsT0.ModuleShortDesc.SemanticVersion
x => x.Item2.Result.ModuleShortDesc.SemanticVersion
);
var installedMatchModule = highestInstalledMatch.Item2.AsT0.ModuleShortDesc;
var installedMatchModule = highestInstalledMatch.Item2.Result.ModuleShortDesc;
var matchLoadoutItemGroup = highestInstalledMatch.Item1.AsLoadoutItemWithTargetPath().AsLoadoutItem().Parent;

// Check if found dependency is outdated
Expand All @@ -129,15 +128,15 @@ private async Task<IEnumerable<Diagnostic>> DiagnosePakModulesAsync(Loadout.Read

#region Helpers

private static async IAsyncEnumerable<ValueTuple<LoadoutFile.ReadOnly, OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>>> GetAllPakMetadata(
private static async IAsyncEnumerable<ValueTuple<LoadoutFile.ReadOnly, Outcome<LsxXmlFormat.MetaFileData>>> GetAllPakMetadata(
LoadoutFile.ReadOnly[] pakLoadoutFiles,
IResourceLoader<Hash, OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>> metadataPipeline,
IResourceLoader<Hash, Outcome<LsxXmlFormat.MetaFileData>> metadataPipeline,
ILogger logger,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
foreach (var pakLoadoutFile in pakLoadoutFiles)
{
Resource<OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>> resource;
Resource<Outcome<LsxXmlFormat.MetaFileData>> resource;
try
{
resource = await metadataPipeline.LoadResourceAsync(pakLoadoutFile.Hash, cancellationToken);
Expand All @@ -149,9 +148,9 @@ private async Task<IEnumerable<Diagnostic>> DiagnosePakModulesAsync(Loadout.Read
}

// Log the InvalidDataException case, but still return the resource
if (resource.Data.IsT1)
if (resource.Data.Exception is not null)
{
logger.LogWarning(resource.Data.AsT1.Value, "Detected invalid BG3 Pak file: `{Name}`", pakLoadoutFile.AsLoadoutItemWithTargetPath().TargetPath.Item3);
logger.LogWarning(resource.Data.Exception, "Detected invalid BG3 Pak file: `{Name}`", pakLoadoutFile.AsLoadoutItemWithTargetPath().TargetPath.Item3);
}

yield return (pakLoadoutFile, resource.Data);
Expand Down
16 changes: 8 additions & 8 deletions src/Games/NexusMods.Games.Larian/BaldursGate3/Pipelines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using NexusMods.Games.Larian.BaldursGate3.Utils.LsxXmlParsing;
using NexusMods.Games.Larian.BaldursGate3.Utils.PakParsing;
using NexusMods.Hashing.xxHash64;
using OneOf.Types;
using Polly;

namespace NexusMods.Games.Larian.BaldursGate3;

Expand All @@ -18,35 +18,35 @@ public static class Pipelines

public static IServiceCollection AddPipelines(this IServiceCollection serviceCollection)
{
return serviceCollection.AddKeyedSingleton<IResourceLoader<Hash, OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>>>(
return serviceCollection.AddKeyedSingleton<IResourceLoader<Hash, Outcome<LsxXmlFormat.MetaFileData>>>(
serviceKey: MetadataPipelineKey,
implementationFactory: static (serviceProvider, _) => CreateMetadataPipeline(
fileStore: serviceProvider.GetRequiredService<IFileStore>()
)
);
}

public static IResourceLoader<Hash, OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>> GetMetadataPipeline(IServiceProvider serviceProvider)
public static IResourceLoader<Hash, Outcome<LsxXmlFormat.MetaFileData>> GetMetadataPipeline(IServiceProvider serviceProvider)
{
return serviceProvider.GetRequiredKeyedService<IResourceLoader<Hash,
OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>>>(serviceKey: MetadataPipelineKey);
Outcome<LsxXmlFormat.MetaFileData>>>(serviceKey: MetadataPipelineKey);
}

private static IResourceLoader<Hash, OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>> CreateMetadataPipeline(IFileStore fileStore)
private static IResourceLoader<Hash, Outcome<LsxXmlFormat.MetaFileData>> CreateMetadataPipeline(IFileStore fileStore)
{
// TODO: change pipeline to return C# 9 type unions instead of OneOf
var pipeline = new FileStoreStreamLoader(fileStore)
.ThenDo<Hash, OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>, Stream, Unit>(Unit.Default,
.ThenDo(Unit.Default,
static (_, _, resource, _) =>
{
try
{
var metaFileData = PakFileParser.ParsePakMeta(resource.Data);
return ValueTask.FromResult(resource.WithData(OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>.FromT0(metaFileData)));
return ValueTask.FromResult(resource.WithData(Outcome.FromResult(metaFileData)));
}
catch (InvalidDataException e)
{
return ValueTask.FromResult(resource.WithData(OneOf.OneOf<LsxXmlFormat.MetaFileData, Error<InvalidDataException>>.FromT1(new Error<InvalidDataException>(e))));
return ValueTask.FromResult(resource.WithData(Outcome.FromException<LsxXmlFormat.MetaFileData>(e)));
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<ItemGroup>
<PackageReference Include="K4os.Compression.LZ4" />
<PackageReference Include="Polly.Core" />
<PackageReference Include="ZstdSharp.Port" />
</ItemGroup>

Expand Down

0 comments on commit 3fe4c84

Please sign in to comment.