more fixes

This commit is contained in:
rootdarkarchon
2024-02-15 12:07:56 +01:00
committed by Loporrit
parent f9ca55db43
commit c1d8599b9c
6 changed files with 38 additions and 14 deletions

View File

@@ -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)

View File

@@ -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<string, FileCacheEntity?> 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;
}

View File

@@ -94,6 +94,8 @@ public class MarePlugin : MediatorSubscriberBase, IHostedService
Mediator.Subscribe<DalamudLoginMessage>(this, (_) => DalamudUtilOnLogIn());
Mediator.Subscribe<DalamudLogoutMessage>(this, (_) => DalamudUtilOnLogOut());
Mediator.StartQueueProcessing();
return Task.CompletedTask;
}

View File

@@ -146,13 +146,13 @@ public sealed class Plugin : IDalamudPlugin
s.GetRequiredService<ILogger<GameChatHooks>>(), gameInteropProvider, chatGui,
s.GetRequiredService<MareConfigService>(), s.GetRequiredService<ServerConfigurationManager>()));
collection.AddHostedService(p => p.GetRequiredService<MarePlugin>());
collection.AddHostedService(p => p.GetRequiredService<FileCacheManager>());
collection.AddHostedService(p => p.GetRequiredService<MareMediator>());
collection.AddHostedService(p => p.GetRequiredService<ConfigurationMigrator>());
collection.AddHostedService(p => p.GetRequiredService<DalamudUtilService>());
collection.AddHostedService(p => p.GetRequiredService<PerformanceCollectorService>());
collection.AddHostedService(p => p.GetRequiredService<DtrEntry>());
collection.AddHostedService(p => p.GetRequiredService<MareMediator>());
collection.AddHostedService(p => p.GetRequiredService<MarePlugin>());
})
.Build()
.RunAsync(_pluginCts.Token);

View File

@@ -14,6 +14,7 @@ public sealed class MareMediator : IHostedService
private readonly ConcurrentQueue<MessageBase> _messageQueue = new();
private readonly PerformanceCollectorService _performanceCollector;
private readonly Dictionary<Type, HashSet<SubscriberAction>> _subscriberDict = [];
private bool _processQueue = false;
public MareMediator(ILogger<MareMediator> 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<MessageBase> 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)

View File

@@ -51,7 +51,7 @@ public static class VariousExtensions
public static CancellationTokenSource CancelRecreate(this CancellationTokenSource? cts)
{
cts.CancelDispose();
cts?.CancelDispose();
return new CancellationTokenSource();
}