Skip to content

Commit

Permalink
Fix NaN on counting progress percentage on Hi3 cache
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Jan 5, 2025
1 parent 6c60c73 commit 5336976
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
77 changes: 48 additions & 29 deletions CollapseLauncher/Classes/CachesManagement/Honkai/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Hi3Helper.Http;
using Hi3Helper.UABT;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -17,6 +18,7 @@
using static Hi3Helper.Data.ConverterTool;
using static Hi3Helper.Locale;
using static Hi3Helper.Logger;
// ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault

namespace CollapseLauncher
{
Expand All @@ -41,34 +43,39 @@ private async Task<List<CacheAsset>> Fetch(CancellationToken token)
await BuildGameRepoURL(downloadClient, token);

// Iterate type and do fetch
foreach (CacheAssetType type in Enum.GetValues<CacheAssetType>())
{
// Skip for unused type
switch (type)
await Parallel.ForEachAsync(
Enum.GetValues<CacheAssetType>(),
new ParallelOptions
{
case CacheAssetType.Unused:
case CacheAssetType.Dispatcher:
case CacheAssetType.Gateway:
case CacheAssetType.General:
case CacheAssetType.IFix:
case CacheAssetType.DesignData:
case CacheAssetType.Lua:
continue;
}

// uint = Count of the assets available
// long = Total size of the assets available
(int, long) count = await FetchByType(type, downloadClient, returnAsset, token);

// Write a log about the metadata
LogWriteLine($"Cache Metadata [T: {type}]:", LogType.Default, true);
LogWriteLine($" Cache Count = {count.Item1}", LogType.NoTag, true);
LogWriteLine($" Cache Size = {SummarizeSizeSimple(count.Item2)}", LogType.NoTag, true);

// Increment the Total Size and Count
_progressAllCountTotal += count.Item1;
_progressAllSizeTotal += count.Item2;
}
MaxDegreeOfParallelism = _threadCount,
CancellationToken = token
},
async (type, ctx) =>
{
switch (type)
{
case CacheAssetType.Data:
case CacheAssetType.Event:
case CacheAssetType.AI:
{
// uint = Count of the assets available
// long = Total size of the assets available
(int, long) count = await FetchByType(type, downloadClient, returnAsset, ctx);

// Write a log about the metadata
LogWriteLine($"Cache Metadata [T: {type}]:", LogType.Default, true);
LogWriteLine($" Cache Count = {count.Item1}", LogType.NoTag, true);
LogWriteLine($" Cache Size = {SummarizeSizeSimple(count.Item2)}", LogType.NoTag, true);

// Increment the Total Size and Count
Interlocked.Add(ref _progressAllCountTotal, count.Item1);
Interlocked.Add(ref _progressAllSizeTotal, count.Item2);
}
break;
default:
return;
}
});

// Return asset index
return returnAsset;
Expand Down Expand Up @@ -230,7 +237,7 @@ private IEnumerable<CacheAsset> EnumerateCacheTextAsset(CacheAssetType type, IEn
}
}

private async ValueTask<(int, long)> BuildAssetIndex(CacheAssetType type, string baseURL, Stream stream,
private async ValueTask<ValueTuple<int, long>> BuildAssetIndex(CacheAssetType type, string baseURL, Stream stream,
List<CacheAsset> assetIndex, CancellationToken token)
{
int count = 0;
Expand Down Expand Up @@ -258,6 +265,9 @@ private IEnumerable<CacheAsset> EnumerateCacheTextAsset(CacheAssetType type, IEn
.SetAllowedDecompression(DecompressionMethods.None)
.Create();

// Use ConcurrentQueue for adding assets in parallel.
ConcurrentQueue<CacheAsset> assetQueue = [];

// Iterate lines of the TextAsset in parallel
await Parallel.ForEachAsync(EnumerateCacheTextAsset(type, dataTextAsset.GetStringList(), baseURL),
new ParallelOptions
Expand All @@ -283,9 +293,18 @@ await Parallel.ForEachAsync(EnumerateCacheTextAsset(type, dataTextAsset.GetStrin
if (!urlStatus.IsSuccessStatusCode) return;
}

assetIndex.Add(content);
// Append the content to the queue
assetQueue.Enqueue(content);

// Increment the count and size
Interlocked.Increment(ref count);
Interlocked.Add(ref size, content.CS);
});

// Take out from the ConcurrentQueue
assetIndex.AddRange(assetQueue);
assetQueue.Clear();

// Return the count and the size
return (count, size);
}
Expand Down
2 changes: 1 addition & 1 deletion Hi3Helper.EncTool

0 comments on commit 5336976

Please sign in to comment.