diff --git a/MareSynchronos/FileCache/CacheMonitor.cs b/MareSynchronos/FileCache/CacheMonitor.cs index f2a11ca..3c41353 100644 --- a/MareSynchronos/FileCache/CacheMonitor.cs +++ b/MareSynchronos/FileCache/CacheMonitor.cs @@ -57,6 +57,7 @@ public sealed class CacheMonitor : DisposableMediatorSubscriberBase if (configService.Current.HasValidSetup()) { StartMareWatcher(configService.Current.CacheFolder); + InvokeScan(); } var token = _periodicCalculationTokenSource.Token; @@ -350,11 +351,11 @@ public sealed class CacheMonitor : DisposableMediatorSubscriberBase { TotalFiles = 0; _currentFileProgress = 0; - _scanCancellationTokenSource?.Cancel(); - _scanCancellationTokenSource = new CancellationTokenSource(); + _scanCancellationTokenSource = _scanCancellationTokenSource?.CancelRecreate() ?? new CancellationTokenSource(); var token = _scanCancellationTokenSource.Token; _ = Task.Run(async () => { + Logger.LogDebug("Starting Full File Scan"); TotalFiles = 0; _currentFileProgress = 0; while (_dalamudUtil.IsOnFrameworkThread) diff --git a/MareSynchronos/FileCache/FileCacheManager.cs b/MareSynchronos/FileCache/FileCacheManager.cs index ccfb72e..d3602bf 100644 --- a/MareSynchronos/FileCache/FileCacheManager.cs +++ b/MareSynchronos/FileCache/FileCacheManager.cs @@ -38,9 +38,9 @@ public sealed class FileCacheManager : IHostedService public FileCacheEntity? CreateCacheEntry(string path) { - _logger.LogTrace("Creating cache entry for {path}", path); FileInfo fi = new(path); if (!fi.Exists) return null; + _logger.LogTrace("Creating cache entry for {path}", path); var fullName = fi.FullName.ToLowerInvariant(); if (!fullName.Contains(_configService.Current.CacheFolder.ToLowerInvariant(), StringComparison.Ordinal)) return null; string prefixedPath = fullName.Replace(_configService.Current.CacheFolder.ToLowerInvariant(), CachePrefix + "\\", StringComparison.Ordinal).Replace("\\\\", "\\", StringComparison.Ordinal); @@ -49,9 +49,9 @@ public sealed class FileCacheManager : IHostedService public FileCacheEntity? CreateFileEntry(string path) { - _logger.LogTrace("Creating file entry for {path}", path); FileInfo fi = new(path); if (!fi.Exists) return null; + _logger.LogTrace("Creating file entry for {path}", path); var fullName = fi.FullName.ToLowerInvariant(); if (!fullName.Contains(_ipcManager.PenumbraModDirectory!.ToLowerInvariant(), StringComparison.Ordinal)) return null; string prefixedPath = fullName.Replace(_ipcManager.PenumbraModDirectory!.ToLowerInvariant(), PenumbraPrefix + "\\", StringComparison.Ordinal).Replace("\\\\", "\\", StringComparison.Ordinal); @@ -159,7 +159,7 @@ public sealed class FileCacheManager : IHostedService return null; } - public FileCacheEntity? GetFileCacheByPath(string path) + private FileCacheEntity? GetFileCacheByPath(string path) { var cleanedPath = path.Replace("/", "\\", StringComparison.OrdinalIgnoreCase).ToLowerInvariant().Replace(_ipcManager.PenumbraModDirectory!.ToLowerInvariant(), "", StringComparison.OrdinalIgnoreCase); var entry = _fileCaches.SelectMany(v => v.Value).FirstOrDefault(f => f.ResolvedFilepath.EndsWith(cleanedPath, StringComparison.OrdinalIgnoreCase)); @@ -183,18 +183,25 @@ public sealed class FileCacheManager : IHostedService { var cleanedPaths = paths.Distinct(StringComparer.OrdinalIgnoreCase).ToDictionary(p => p, p => p.Replace("/", "\\", StringComparison.OrdinalIgnoreCase) - .Replace(_ipcManager.PenumbraModDirectory!, PenumbraPrefix, StringComparison.OrdinalIgnoreCase) - .Replace(_configService.Current.CacheFolder, CachePrefix, StringComparison.OrdinalIgnoreCase) + .Replace(_ipcManager.PenumbraModDirectory!, _ipcManager.PenumbraModDirectory!.EndsWith('\\') ? PenumbraPrefix + '\\' : PenumbraPrefix, StringComparison.OrdinalIgnoreCase) + .Replace(_configService.Current.CacheFolder, _configService.Current.CacheFolder.EndsWith('\\') ? CachePrefix + '\\' : CachePrefix, StringComparison.OrdinalIgnoreCase) .Replace("\\\\", "\\", StringComparison.Ordinal), StringComparer.OrdinalIgnoreCase); + foreach (var entry in cleanedPaths) + { + _logger.LogInformation("Getting FileCache by Cleaned Path: {path}", entry.Value); + } + Dictionary result = new(StringComparer.OrdinalIgnoreCase); var dict = _fileCaches.SelectMany(f => f.Value) - .ToDictionary(d => d.PrefixedFilePath, d => d, StringComparer.Ordinal); + .ToDictionary(d => d.PrefixedFilePath, d => d, StringComparer.OrdinalIgnoreCase); foreach (var entry in cleanedPaths) { + _logger.LogDebug("Validating {path}", entry.Value); + if (dict.TryGetValue(entry.Value, out var entity)) { var validatedCache = GetValidatedFileCache(entity); @@ -221,10 +228,12 @@ public sealed class FileCacheManager : IHostedService { if (_fileCaches.TryGetValue(hash, out var caches)) { - caches?.RemoveAll(c => string.Equals(c.PrefixedFilePath, prefixedFilePath, StringComparison.Ordinal)); + var removedCount = caches?.RemoveAll(c => string.Equals(c.PrefixedFilePath, prefixedFilePath, StringComparison.Ordinal)); + _logger.LogDebug("Removed {count} files with hash {hash} and file cache {path}", removedCount, hash, prefixedFilePath); + if (caches?.Count == 0) { - _fileCaches.Remove(hash, out _); + _fileCaches.Remove(hash, out var entity); } } } @@ -334,7 +343,7 @@ public sealed class FileCacheManager : IHostedService File.AppendAllLines(_csvPath, new[] { entity.CsvEntry }); } var result = GetFileCacheByPath(fileInfo.FullName); - _logger.LogDebug("Creating file cache for {name} success: {success}", fileInfo.FullName, (result != null)); + _logger.LogDebug("Creating cache entity for {name} success: {success}", fileInfo.FullName, (result != null)); return result; } diff --git a/MareSynchronos/MarePlugin.cs b/MareSynchronos/MarePlugin.cs index 90e8c0e..dc7bb0b 100644 --- a/MareSynchronos/MarePlugin.cs +++ b/MareSynchronos/MarePlugin.cs @@ -94,6 +94,8 @@ public class MarePlugin : MediatorSubscriberBase, IHostedService Mediator.Subscribe(this, (_) => DalamudUtilOnLogIn()); Mediator.Subscribe(this, (_) => DalamudUtilOnLogOut()); + Mediator.StartQueueProcessing(); + return Task.CompletedTask; } diff --git a/MareSynchronos/Plugin.cs b/MareSynchronos/Plugin.cs index e568b5f..9337715 100644 --- a/MareSynchronos/Plugin.cs +++ b/MareSynchronos/Plugin.cs @@ -146,13 +146,13 @@ public sealed class Plugin : IDalamudPlugin s.GetRequiredService>(), gameInteropProvider, chatGui, s.GetRequiredService(), s.GetRequiredService())); - collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); + collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); collection.AddHostedService(p => p.GetRequiredService()); - collection.AddHostedService(p => p.GetRequiredService()); + collection.AddHostedService(p => p.GetRequiredService()); }) .Build() .RunAsync(_pluginCts.Token); diff --git a/MareSynchronos/Services/Mediator/MareMediator.cs b/MareSynchronos/Services/Mediator/MareMediator.cs index e8f24c4..4433833 100644 --- a/MareSynchronos/Services/Mediator/MareMediator.cs +++ b/MareSynchronos/Services/Mediator/MareMediator.cs @@ -14,6 +14,7 @@ public sealed class MareMediator : IHostedService private readonly ConcurrentQueue _messageQueue = new(); private readonly PerformanceCollectorService _performanceCollector; private readonly Dictionary> _subscriberDict = []; + private bool _processQueue = false; public MareMediator(ILogger logger, PerformanceCollectorService performanceCollector) { @@ -60,6 +61,11 @@ public sealed class MareMediator : IHostedService { while (!_loopCts.Token.IsCancellationRequested) { + while (!_processQueue) + { + await Task.Delay(100, _loopCts.Token).ConfigureAwait(false); + } + await Task.Delay(100, _loopCts.Token).ConfigureAwait(false); HashSet processedMessages = []; @@ -167,6 +173,12 @@ public sealed class MareMediator : IHostedService } } + public void StartQueueProcessing() + { + _logger.LogInformation("Starting Message Queue Processing"); + _processQueue = true; + } + private sealed class SubscriberAction { public SubscriberAction(IMediatorSubscriber subscriber, object action) diff --git a/MareSynchronos/Utils/VariousExtensions.cs b/MareSynchronos/Utils/VariousExtensions.cs index 8be6717..e06d8f2 100644 --- a/MareSynchronos/Utils/VariousExtensions.cs +++ b/MareSynchronos/Utils/VariousExtensions.cs @@ -51,7 +51,7 @@ public static class VariousExtensions public static CancellationTokenSource CancelRecreate(this CancellationTokenSource? cts) { - cts.CancelDispose(); + cts?.CancelDispose(); return new CancellationTokenSource(); }