-
Notifications
You must be signed in to change notification settings - Fork 4
[WIP] Epic manifest parsing #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devedse
wants to merge
17
commits into
master
Choose a base branch
from
EpicManifestParsing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2e2db24
First WIP
devedse e5d268e
wip
devedse 14e43ab
A bit of cleanup
devedse 75c894d
WIP manifest parsing
devedse e72f329
Add dependencies
devedse 362e586
Fix build
devedse 307a564
wip
devedse 2812955
Undo migrations
devedse 63b3b04
Do migration
devedse 36b2465
Added some more stuff and fixes
devedse 7c43f15
Fix test
devedse b3b420f
Fix tests 2
devedse 26bf1d3
WIP adding epic manifest downloads
devedse a2a0f5d
WIP rewriting codebase a little
devedse 90c1fd1
Merge branch 'master' into EpicManifestParsing
devedse 4b36e14
Fix some warnings
devedse 0c6dee4
Merge remote-tracking branch 'origin/master' into EpicManifestParsing
devedse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...I_Backend.EpicManifestParser.Tests/DeveLanCacheUI_Backend.EpicManifestParser.Tests.csproj
This file contains hidden or 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,31 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" /> | ||
| <PackageReference Include="MSTest.TestAdapter" Version="3.8.3" /> | ||
| <PackageReference Include="MSTest.TestFramework" Version="3.8.3" /> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.4"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\DeveLanCacheUI_Backend.EpicManifestParser\DeveLanCacheUI_Backend.EpicManifestParser.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Update="TestFiles\1sE9O19OT_X-rOWTFEiMUGNBYu8I1A.manifest"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
23 changes: 23 additions & 0 deletions
23
DeveLanCacheUI_Backend.EpicManifestParser.Tests/EpicManifestParserTests.cs
This file contains hidden or 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,23 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace DeveLanCacheUI_Backend.EpicManifestParser.Tests | ||
| { | ||
| [TestClass] | ||
| public sealed class EpicManifestParserTests | ||
| { | ||
| [TestMethod] | ||
| public async Task DoesItWork() | ||
| { | ||
| // Arrange | ||
| var manifestBuffer = await File.ReadAllBytesAsync(Path.Combine("TestFiles", "1sE9O19OT_X-rOWTFEiMUGNBYu8I1A.manifest")); | ||
|
|
||
| // Act | ||
| var manifest = EpicManifestParser.Deserialize(manifestBuffer); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual("Super Space Club.exe", manifest.Meta.LaunchExe); | ||
| Assert.AreEqual("eccf14e21df84712a54d2b89b20d15f9", manifest.Meta.AppName); | ||
| Assert.AreEqual("9lEUV1B3tU-lv8teGLbqaQ", manifest.Meta.BuildId); | ||
| } | ||
| } | ||
| } |
Binary file added
BIN
+51.9 KB
...acheUI_Backend.EpicManifestParser.Tests/TestFiles/1sE9O19OT_X-rOWTFEiMUGNBYu8I1A.manifest
Binary file not shown.
305 changes: 305 additions & 0 deletions
305
DeveLanCacheUI_Backend.EpicManifestParser/Api/ManifestInfo.cs
This file contains hidden or 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,305 @@ | ||
| using DeveLanCacheUI_Backend.EpicManifestParser.UE; | ||
| using Flurl; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Net.Http.Json; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace DeveLanCacheUI_Backend.EpicManifestParser.Api; | ||
| // ReSharper disable UseSymbolAlias | ||
|
|
||
| /// <summary/> | ||
| public class ManifestInfo | ||
| { | ||
| /// <summary/> | ||
| public required List<ManifestInfoElement> Elements { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Parses the UTF-8 encoded text representing a single JSON value into a <see cref="ManifestInfo"/>. | ||
| /// </summary> | ||
| /// <returns>A <see cref="ManifestInfo"/> representation of the JSON value.</returns> | ||
| /// <param name="utf8Json">JSON text to parse.</param> | ||
| /// <exception cref="JsonException"> | ||
| /// The JSON is invalid, | ||
| /// <see cref="ManifestInfo"/> is not compatible with the JSON, | ||
| /// or when there is remaining data in the buffer. | ||
| /// </exception> | ||
| public static ManifestInfo? Deserialize(ReadOnlySpan<byte> utf8Json) | ||
| => JsonSerializer.Deserialize(utf8Json, EpicManifestParserJsonContext.Default.ManifestInfo); | ||
|
|
||
| /// <summary> | ||
| /// Reads the UTF-8 encoded text representing a single JSON value into a <see cref="ManifestInfo"/>. | ||
| /// The Stream will be read to completion. | ||
| /// </summary> | ||
| /// <returns>A <see cref="ManifestInfo"/> representation of the JSON value.</returns> | ||
| /// <param name="utf8Json">JSON data to parse.</param> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// <paramref name="utf8Json"/> is <see langword="null"/>. | ||
| /// </exception> | ||
| /// <exception cref="JsonException"> | ||
| /// The JSON is invalid, | ||
| /// <see cref="ManifestInfo"/> is not compatible with the JSON, | ||
| /// or when there is remaining data in the Stream. | ||
| /// </exception> | ||
| public static ManifestInfo? Deserialize(Stream utf8Json) | ||
| => JsonSerializer.Deserialize(utf8Json, EpicManifestParserJsonContext.Default.ManifestInfo); | ||
|
|
||
| /// <summary> | ||
| /// Reads the UTF-8 encoded text representing a single JSON value into a <see cref="ManifestInfo"/>. | ||
| /// The Stream will be read to completion. | ||
| /// </summary> | ||
| /// <returns>A <see cref="ManifestInfo"/> representation of the JSON value.</returns> | ||
| /// <param name="utf8Json">JSON data to parse.</param> | ||
| /// <param name="cancellationToken"> | ||
| /// The <see cref="CancellationToken"/> that can be used to cancel the read operation. | ||
| /// </param> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// <paramref name="utf8Json"/> is <see langword="null"/>. | ||
| /// </exception> | ||
| /// <exception cref="JsonException"> | ||
| /// The JSON is invalid, | ||
| /// <see cref="ManifestInfo"/> is not compatible with the JSON, | ||
| /// or when there is remaining data in the Stream. | ||
| /// </exception> | ||
| public static ValueTask<ManifestInfo?> DeserializeAsync(Stream utf8Json, CancellationToken cancellationToken = default) | ||
| => JsonSerializer.DeserializeAsync(utf8Json, EpicManifestParserJsonContext.Default.ManifestInfo, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Reads the UTF-8 encoded text representing a single JSON value into a <see cref="ManifestInfo"/>. | ||
| /// The Stream will be read to completion. | ||
| /// </summary> | ||
| /// <returns>A <see cref="ManifestInfo"/> representation of the JSON value.</returns> | ||
| /// <param name="path">JSON file to parse.</param> | ||
| /// <exception cref="JsonException"> | ||
| /// The JSON is invalid, | ||
| /// <see cref="ManifestInfo"/> is not compatible with the JSON, | ||
| /// or when there is remaining data in the Stream. | ||
| /// </exception> | ||
| /// <inheritdoc cref="File.OpenRead(string)"/> | ||
| public static ManifestInfo? DeserializeFile(string path) | ||
| { | ||
| using var fs = File.OpenRead(path); | ||
| return JsonSerializer.Deserialize(fs, EpicManifestParserJsonContext.Default.ManifestInfo); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reads the UTF-8 encoded text representing a single JSON value into a <see cref="ManifestInfo"/>. | ||
| /// The Stream will be read to completion. | ||
| /// </summary> | ||
| /// <returns>A <see cref="ManifestInfo"/> representation of the JSON value.</returns> | ||
| /// <param name="path">JSON file to parse.</param> | ||
| /// <param name="cancellationToken"> | ||
| /// The <see cref="CancellationToken"/> that can be used to cancel the read operation. | ||
| /// </param> | ||
| /// <exception cref="JsonException"> | ||
| /// The JSON is invalid, | ||
| /// <see cref="ManifestInfo"/> is not compatible with the JSON, | ||
| /// or when there is remaining data in the Stream. | ||
| /// </exception> | ||
| /// <inheritdoc cref="File.OpenRead(string)"/> | ||
| public static ValueTask<ManifestInfo?> DeserializeFileAsync(string path, CancellationToken cancellationToken = default) | ||
| { | ||
| using var fs = File.OpenRead(path); | ||
| return JsonSerializer.DeserializeAsync(fs, EpicManifestParserJsonContext.Default.ManifestInfo, cancellationToken); | ||
| } | ||
|
|
||
| /// <param name="elementPredicate">Predicate to select the a single element in <see cref="Elements"/></param> | ||
| /// <param name="elementManifestPredicate">Predicate to select the a single manifest in <see cref="ManifestInfoElement.Manifests"/></param> | ||
| /// <param name="cancellationToken"> | ||
| /// The <see cref="CancellationToken"/> that can be used to cancel the read operation. | ||
| /// </param> | ||
| /// <param name="optionsBuilder">Builder for options for parsing and/or caching the manifest</param> | ||
| /// <inheritdoc cref="DownloadAndParseAsync(ManifestParseOptions, Predicate<ManifestInfoElement>?, Predicate<ManifestInfoElementManifest>?, CancellationToken)"/> | ||
| public Task<(FBuildPatchAppManifest Manifest, ManifestInfoElement InfoElement)> DownloadAndParseAsync( | ||
| Predicate<ManifestInfoElement>? elementPredicate = null, Predicate<ManifestInfoElementManifest>? elementManifestPredicate = null, | ||
| CancellationToken cancellationToken = default, Action<ManifestParseOptions>? optionsBuilder = null) | ||
| { | ||
| var options = new ManifestParseOptions(); | ||
| optionsBuilder?.Invoke(options); | ||
| return DownloadAndParseAsync(options, elementPredicate, elementManifestPredicate, cancellationToken); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Downloads and parses the manifest. | ||
| /// </summary> | ||
| /// <param name="options">Options for parsing and/or caching the manifest</param> | ||
| /// <param name="elementPredicate">Predicate to select the a single element in <see cref="Elements"/></param> | ||
| /// <param name="elementManifestPredicate">Predicate to select the a single manifest in <see cref="ManifestInfoElement.Manifests"/></param> | ||
| /// <param name="cancellationToken"> | ||
| /// The <see cref="CancellationToken"/> that can be used to cancel the read operation. | ||
| /// </param> | ||
| /// <returns> | ||
| /// The parsed <see cref="FBuildPatchAppManifest"/> manifest and the selected <see cref="ManifestInfoElement"/> info element in a <see cref="ValueTuple"/> | ||
| /// </returns> | ||
| /// <exception cref="InvalidOperationException">When a predicate fails.</exception> | ||
| /// <exception cref="HttpRequestException">When the manifest data fails to download.</exception> | ||
| public async Task<(FBuildPatchAppManifest Manifest, ManifestInfoElement InfoElement)> DownloadAndParseAsync( | ||
| ManifestParseOptions options, Predicate<ManifestInfoElement>? elementPredicate = null, | ||
| Predicate<ManifestInfoElementManifest>? elementManifestPredicate = null, CancellationToken cancellationToken = default) | ||
| { | ||
| ManifestInfoElement element; | ||
| if (elementPredicate is null) | ||
| element = Elements[0]; | ||
| else | ||
| element = Elements.Find(elementPredicate) ?? throw new InvalidOperationException("Could not find ManifestInfoElement based on predicate"); | ||
|
|
||
| ManifestInfoElementManifest elementManifest; | ||
| if (elementManifestPredicate is null) | ||
| elementManifest = element.Manifests[0]; | ||
| else | ||
| elementManifest = element.Manifests.Find(elementManifestPredicate) ?? throw new InvalidOperationException("Could not find ManifestInfoElement based on predicate"); | ||
|
|
||
| string? cachePath = null; | ||
|
|
||
| if (options.ManifestCacheDirectory is not null) | ||
| { | ||
| cachePath = Path.Join(options.ManifestCacheDirectory.AsSpan(), GetFileName(elementManifest.Uri)); | ||
| if (File.Exists(cachePath)) | ||
| { | ||
| var manifestBuffer = await File.ReadAllBytesAsync(cachePath, cancellationToken).ConfigureAwait(false); | ||
| var manifest = FBuildPatchAppManifest.Deserialize(manifestBuffer, options); | ||
| return (manifest, element); | ||
| } | ||
|
|
||
| static ReadOnlySpan<char> GetFileName(Uri uri) | ||
| { | ||
| var span = uri.OriginalString.AsSpan(); | ||
| return span[(span.LastIndexOf('/') + 1)..]; | ||
| } | ||
| } | ||
|
|
||
| { | ||
| Uri manifestUri; | ||
|
|
||
| if (elementManifest.QueryParams is { Count: not 0 }) | ||
| { | ||
| var url = new Url(elementManifest.Uri); | ||
| foreach (var queryParam in elementManifest.QueryParams) | ||
| { | ||
| url.AppendQueryParam(queryParam.Name, queryParam.Value, true, NullValueHandling.NameOnly); | ||
| } | ||
| manifestUri = url.ToUri(); | ||
| } | ||
| else | ||
| { | ||
| manifestUri = elementManifest.Uri; | ||
| } | ||
|
|
||
| options.CreateDefaultClient(); | ||
| byte[] manifestBuffer; | ||
|
|
||
| try | ||
| { | ||
| manifestBuffer = await options.Client!.GetByteArrayAsync(manifestUri, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| catch (HttpRequestException httpEx) | ||
| { | ||
| httpEx.Data.Add("ManifestUri", manifestUri); | ||
| httpEx.Data.Add("ElementManifest", elementManifest); | ||
| httpEx.Data.Add("Element", element); | ||
| throw; | ||
| } | ||
|
|
||
| var manifest = FBuildPatchAppManifest.Deserialize(manifestBuffer, options); | ||
|
|
||
| if (cachePath is not null) | ||
| { | ||
| await File.WriteAllBytesAsync(cachePath, manifestBuffer, cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| return (manifest, element); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary/> | ||
| public class ManifestInfoElement | ||
| { | ||
| /// <summary/> | ||
| public required string AppName { get; set; } | ||
| /// <summary/> | ||
| public required string LabelName { get; set; } | ||
| /// <summary/> | ||
| public required string BuildVersion { get; set; } | ||
| /// <summary/> | ||
| public FSHAHash Hash { get; set; } | ||
| /// <summary/> | ||
| public bool UseSignedUrl { get; set; } | ||
| /// <summary/> | ||
| public Dictionary<string, object>? Metadata { get; set; } | ||
| /// <summary/> | ||
| public required List<ManifestInfoElementManifest> Manifests { get; set; } | ||
|
|
||
| /// <inheritdoc cref="ManifestExtensions.TryParseVersionAndCL"/> | ||
| public bool TryParseVersionAndCL([NotNullWhen(true)] out Version? version, out int cl) => | ||
| ManifestExtensions.TryParseVersionAndCL(BuildVersion, out version, out cl); | ||
| } | ||
|
|
||
| /// <summary/> | ||
| public class ManifestInfoElementManifest | ||
| { | ||
| /// <summary/> | ||
| public required Uri Uri { get; set; } | ||
| /// <summary/> | ||
| public List<ManifestInfoElementManifestQueryParams>? QueryParams { get; set; } | ||
| } | ||
|
|
||
| /// <summary/> | ||
| public class ManifestInfoElementManifestQueryParams | ||
| { | ||
| /// <summary/> | ||
| public required string Name { get; set; } | ||
| /// <summary/> | ||
| public required string Value { get; set; } | ||
| } | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Source generated JSON parsers for <see cref="ManifestInfo"/> | ||
| /// </summary> | ||
| [JsonSourceGenerationOptions(JsonSerializerDefaults.Web, Converters = [typeof(FSHAHashConverter)])] | ||
| [JsonSerializable(typeof(ManifestInfo))] | ||
| public partial class EpicManifestParserJsonContext : JsonSerializerContext; | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Extension methods for manifest related things | ||
| /// </summary> | ||
| public static partial class ManifestExtensions | ||
| { | ||
| [GeneratedRegex(@"(\d+(?:\.\d+)+)-CL-(\d+)", RegexOptions.Singleline | RegexOptions.IgnoreCase)] | ||
| internal static partial Regex VersionAndClRegex(); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to parse <paramref name="version"/> and <paramref name="cl"/> from the <paramref name="buildVersion"/>. | ||
| /// </summary> | ||
| /// <param name="buildVersion"></param> | ||
| /// <param name="version"></param> | ||
| /// <param name="cl"></param> | ||
| /// <returns><see langword="true"/> if <paramref name="buildVersion" /> was successfully parsed; otherwise, <see langword="false"/>.</returns> | ||
| public static bool TryParseVersionAndCL(string buildVersion, [NotNullWhen(true)] out Version? version, out int cl) | ||
| { | ||
| version = null; | ||
| cl = -1; | ||
| if (string.IsNullOrEmpty(buildVersion)) | ||
| return false; | ||
|
|
||
| var match = VersionAndClRegex().Match(buildVersion); | ||
| if (!match.Success) | ||
| return false; | ||
|
|
||
| version = Version.Parse(match.Groups[1].ValueSpan); | ||
| cl = int.Parse(match.Groups[2].ValueSpan); | ||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reads the HTTP content and returns the value that results from deserializing the content as JSON in an asynchronous operation. | ||
| /// </summary> | ||
| /// <param name="content">The content to read from.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> | ||
| /// <returns>The task object representing the asynchronous operation.</returns> | ||
| public static Task<ManifestInfo?> ReadManifestInfoAsync(this HttpContent content, CancellationToken cancellationToken = default) | ||
| => content.ReadFromJsonAsync(EpicManifestParserJsonContext.Default.ManifestInfo, cancellationToken); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider handling the case where the URI ends with a slash to avoid returning an empty file name, which could lead to incorrect cache path resolution.