improve zura performance

This commit is contained in:
rootdarkarchon
2023-04-11 02:24:45 +02:00
parent 170db28158
commit a1d5f18215

View File

@@ -387,14 +387,14 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase
Task.Run(async () => Task.Run(async () =>
{ {
List<FileReplacementData> toDownloadReplacements;
Dictionary<string, string> moddedPaths = new(StringComparer.Ordinal); Dictionary<string, string> moddedPaths = new(StringComparer.Ordinal);
if (updateModdedPaths) if (updateModdedPaths)
{ {
int attempts = 0; int attempts = 0;
while ((toDownloadReplacements = TryCalculateModdedDictionary(charaData, out moddedPaths)).Count > 0 && attempts++ <= 10) List<FileReplacementData> toDownloadReplacements = TryCalculateModdedDictionary(charaData, out moddedPaths, downloadToken);
while ((toDownloadReplacements.Count > 0 && attempts++ <= 10 && !downloadToken.IsCancellationRequested))
{ {
_downloadManager.CancelDownload(); _downloadManager.CancelDownload();
Logger.LogDebug("Downloading missing files for player {name}, {kind}", PlayerName, updatedData); Logger.LogDebug("Downloading missing files for player {name}, {kind}", PlayerName, updatedData);
@@ -411,7 +411,9 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase
return; return;
} }
if (TryCalculateModdedDictionary(charaData, out moddedPaths).All(c => _downloadManager.ForbiddenTransfers.Any(f => string.Equals(f.Hash, c.Hash, StringComparison.Ordinal)))) toDownloadReplacements = TryCalculateModdedDictionary(charaData, out moddedPaths, downloadToken);
if (toDownloadReplacements.All(c => _downloadManager.ForbiddenTransfers.Any(f => string.Equals(f.Hash, c.Hash, StringComparison.Ordinal))))
{ {
break; break;
} }
@@ -573,26 +575,28 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase
} }
} }
private List<FileReplacementData> TryCalculateModdedDictionary(CharacterData charaData, out Dictionary<string, string> moddedDictionary) private List<FileReplacementData> TryCalculateModdedDictionary(CharacterData charaData, out Dictionary<string, string> moddedDictionary, CancellationToken token)
{ {
List<FileReplacementData> missingFiles = new(); List<FileReplacementData> missingFiles = new();
moddedDictionary = new Dictionary<string, string>(StringComparer.Ordinal); moddedDictionary = new Dictionary<string, string>(StringComparer.Ordinal);
try try
{ {
foreach (var item in charaData.FileReplacements.SelectMany(k => k.Value.Where(v => string.IsNullOrEmpty(v.FileSwapPath))).ToList()) var replacementList = charaData.FileReplacements.SelectMany(k => k.Value.Where(v => string.IsNullOrEmpty(v.FileSwapPath))).DistinctBy(p => p.Hash).ToList();
foreach (var item in replacementList)
{ {
foreach (var gamePath in item.GamePaths) token.ThrowIfCancellationRequested();
var fileCache = _fileDbManager.GetFileCacheByHash(item.Hash);
if (fileCache != null)
{ {
var fileCache = _fileDbManager.GetFileCacheByHash(item.Hash); foreach (var gamePath in item.GamePaths)
if (fileCache != null)
{ {
moddedDictionary[gamePath] = fileCache.ResolvedFilepath; moddedDictionary[gamePath] = fileCache.ResolvedFilepath;
} }
else }
{ else
Logger.LogTrace("Missing file: {hash}", item.Hash); {
missingFiles.Add(item); Logger.LogTrace("Missing file: {hash}", item.Hash);
} missingFiles.Add(item);
} }
} }