From 8bc9a5535e430d5ccd9b9d20bf8ffded3a765edf Mon Sep 17 00:00:00 2001 From: Marlon Date: Wed, 4 Dec 2024 02:21:24 +0100 Subject: [PATCH] refactor --- .../ApiEndpoints/ValorantApiEndpoint.cs | 12 +++---- FModel/ViewModels/CUE4ParseViewModel.cs | 36 ++++++++----------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/FModel/ViewModels/ApiEndpoints/ValorantApiEndpoint.cs b/FModel/ViewModels/ApiEndpoints/ValorantApiEndpoint.cs index bb626a55..434de7e2 100644 --- a/FModel/ViewModels/ApiEndpoints/ValorantApiEndpoint.cs +++ b/FModel/ViewModels/ApiEndpoints/ValorantApiEndpoint.cs @@ -116,8 +116,6 @@ public class VManifest return chunkBytes; } - - public VPakStream GetPakStream(int index) => new VPakStream(this, index); } public readonly struct VHeader @@ -168,6 +166,7 @@ public readonly struct VPak } public string GetFullName() => $"ValorantLive/ShooterGame/Content/Paks/{Name}"; + public VPakStream GetStream(VManifest manifest) => new(manifest, this); } [StructLayout(LayoutKind.Sequential, Pack = 1)] @@ -182,16 +181,15 @@ public readonly struct VChunk public class VPakStream : RandomAccessStream, ICloneable { private readonly VManifest _manifest; - private readonly int _pakIndex; + private readonly VPak _pak; private readonly VChunk[] _chunks; - public VPakStream(VManifest manifest, int pakIndex, long position = 0L) + public VPakStream(VManifest manifest, in VPak pak, long position = 0L) { _manifest = manifest; - _pakIndex = pakIndex; + _pak = pak; _position = position; - var pak = manifest.Paks[pakIndex]; _chunks = new VChunk[pak.ChunkIndices.Length]; for (var i = 0; i < _chunks.Length; i++) { @@ -201,7 +199,7 @@ public class VPakStream : RandomAccessStream, ICloneable Length = pak.Size; } - public object Clone() => new VPakStream(_manifest, _pakIndex, _position); + public object Clone() => new VPakStream(_manifest, _pak, _position); public override int Read(byte[] buffer, int offset, int count) => ReadAsync(buffer, offset, count, CancellationToken.None).GetAwaiter().GetResult(); diff --git a/FModel/ViewModels/CUE4ParseViewModel.cs b/FModel/ViewModels/CUE4ParseViewModel.cs index b20c3095..bd106f8f 100644 --- a/FModel/ViewModels/CUE4ParseViewModel.cs +++ b/FModel/ViewModels/CUE4ParseViewModel.cs @@ -84,7 +84,7 @@ public class CUE4ParseViewModel : ViewModel { private ThreadWorkerViewModel _threadWorkerView => ApplicationService.ThreadWorkerView; private ApiEndpointViewModel _apiEndpointView => ApplicationService.ApiEndpointView; - private readonly Regex _fnLive = new(@"^FortniteGame[/\\]Content[/\\]Paks[/\\]", + private readonly Regex _fnLiveRegex = new(@"^FortniteGame[/\\]Content[/\\]Paks[/\\]", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); private string _internalGameName; @@ -251,44 +251,38 @@ public class CUE4ParseViewModel : ViewModel cancellationToken: cancellationToken, elementManifestPredicate: x => x.Uri.Host is ("epicgames-download1.akamaized.net" or "download.epicgames.com") ).GetAwaiter().GetResult(); - var parseTime = Stopwatch.GetElapsedTime(startTs); - Parallel.ForEach(manifest.Files, fileManifest => + if (manifest.TryFindFile("Cloud/IoStoreOnDemand.ini", out var ioStoreOnDemandFile)) { - if (fileManifest.FileName.Equals("Cloud/IoStoreOnDemand.ini", StringComparison.OrdinalIgnoreCase)) - { - IoStoreOnDemand.Read(new StreamReader(fileManifest.GetStream())); - return; - } + IoStoreOnDemand.Read(new StreamReader(ioStoreOnDemandFile.GetStream())); + } - if (!_fnLive.IsMatch(fileManifest.FileName)) - { - return; - } - - p.RegisterVfs(fileManifest.FileName, [fileManifest.GetStream()] - , it => new FRandomAccessStreamArchive(it, manifest.Files.First(x => x.FileName.Equals(it)).GetStream(), p.Versions)); + Parallel.ForEach(manifest.Files.Where(x => _fnLiveRegex.IsMatch(x.FileName)), fileManifest => + { + p.RegisterVfs(fileManifest.FileName, [fileManifest.GetStream()], + it => new FRandomAccessStreamArchive(it, manifest.FindFile(it)!.GetStream(), p.Versions)); }); + var elapsedTime = Stopwatch.GetElapsedTime(startTs); FLogger.Append(ELog.Information, () => - FLogger.Text($"Fortnite [LIVE] has been loaded successfully in {parseTime.TotalMilliseconds}ms", Constants.WHITE, true)); + FLogger.Text($"Fortnite [LIVE] has been loaded successfully in {elapsedTime.TotalMilliseconds:F1}ms", Constants.WHITE, true)); break; } case "ValorantLive": { - var manifestInfo = _apiEndpointView.ValorantApi.GetManifest(cancellationToken); - if (manifestInfo == null) + var manifest = _apiEndpointView.ValorantApi.GetManifest(cancellationToken); + if (manifest == null) { throw new Exception("Could not load latest Valorant manifest, you may have to switch to your local installation."); } - Parallel.For(0, manifestInfo.Paks.Length, i => + Parallel.ForEach(manifest.Paks, pak => { - p.RegisterVfs(manifestInfo.Paks[i].GetFullName(), [manifestInfo.GetPakStream(i)]); + p.RegisterVfs(pak.GetFullName(), [pak.GetStream(manifest)]); }); FLogger.Append(ELog.Information, () => - FLogger.Text($"Valorant '{manifestInfo.Header.GameVersion}' has been loaded successfully", Constants.WHITE, true)); + FLogger.Text($"Valorant '{manifest.Header.GameVersion}' has been loaded successfully", Constants.WHITE, true)); break; } }