Make cache calculation thread safe(r)

This commit is contained in:
Ottermandias 2023-05-02 18:02:32 +02:00
parent fb84b43d69
commit 58bd223a80

View file

@ -27,7 +27,8 @@ public class CollectionCacheManager : IDisposable
internal readonly MetaFileManager MetaFileManager; internal readonly MetaFileManager MetaFileManager;
public int Count { get; private set; } public int Count { get; private set; }
public bool Calculating { get; private set; }
public IEnumerable<ModCollection> Active public IEnumerable<ModCollection> Active
=> _storage.Where(c => c.HasCache); => _storage.Where(c => c.HasCache);
@ -116,28 +117,36 @@ public class CollectionCacheManager : IDisposable
private void FullRecalculation(ModCollection collection) private void FullRecalculation(ModCollection collection)
{ {
var cache = collection._cache; var cache = collection._cache;
if (cache == null) if (cache == null || Calculating)
return; return;
cache.ResolvedFiles.Clear(); Calculating = true;
cache.Meta.Reset(); try
cache._conflicts.Clear(); {
cache.ResolvedFiles.Clear();
cache.Meta.Reset();
cache._conflicts.Clear();
// Add all forced redirects. // Add all forced redirects.
foreach (var tempMod in _tempMods.ModsForAllCollections foreach (var tempMod in _tempMods.ModsForAllCollections
.Concat(_tempMods.Mods.TryGetValue(collection, out var list) .Concat(_tempMods.Mods.TryGetValue(collection, out var list)
? list ? list
: Array.Empty<TemporaryMod>())) : Array.Empty<TemporaryMod>()))
cache.AddMod(tempMod, false); cache.AddMod(tempMod, false);
foreach (var mod in _modStorage) foreach (var mod in _modStorage)
cache.AddMod(mod, false); cache.AddMod(mod, false);
cache.AddMetaFiles(); cache.AddMetaFiles();
++collection.ChangeCounter; ++collection.ChangeCounter;
MetaFileManager.ApplyDefaultFiles(collection); MetaFileManager.ApplyDefaultFiles(collection);
}
finally
{
Calculating = false;
}
} }
private void OnCollectionChange(CollectionType type, ModCollection? old, ModCollection? newCollection, string displayName) private void OnCollectionChange(CollectionType type, ModCollection? old, ModCollection? newCollection, string displayName)