delay start scan to ingame login, show what is halting scans in settings ui and allow resetting it

This commit is contained in:
Stanley Dimant
2022-09-28 23:10:29 +02:00
parent ff4079a16d
commit e71bd0d80f
5 changed files with 63 additions and 32 deletions

View File

@@ -17,9 +17,9 @@ public class PeriodicFileScanner : IDisposable
private readonly FileCacheManager _fileDbManager;
private readonly ApiController _apiController;
private readonly DalamudUtil _dalamudUtil;
private int haltScanRequests = 0;
private CancellationTokenSource? _scanCancellationTokenSource;
private Task? _fileScannerTask = null;
public ConcurrentDictionary<string, int> haltScanLocks = new();
public PeriodicFileScanner(IpcManager ipcManager, Configuration pluginConfiguration, FileCacheManager fileDbManager, ApiController apiController, DalamudUtil dalamudUtil)
{
Logger.Verbose("Creating " + nameof(PeriodicFileScanner));
@@ -30,32 +30,57 @@ public class PeriodicFileScanner : IDisposable
_apiController = apiController;
_dalamudUtil = dalamudUtil;
_ipcManager.PenumbraInitialized += StartScan;
if (!string.IsNullOrEmpty(_ipcManager.PenumbraModDirectory()))
{
StartScan();
}
_apiController.DownloadStarted += HaltScan;
_apiController.DownloadFinished += ResumeScan;
_dalamudUtil.ZoneSwitchStart += HaltScan;
_dalamudUtil.ZoneSwitchEnd += ResumeScan;
_apiController.DownloadStarted += ApiHaltScan;
_apiController.DownloadFinished += ApiResumeScan;
_dalamudUtil.ZoneSwitchStart += ZoneSwitchHaltScan;
_dalamudUtil.ZoneSwitchEnd += ZoneSwitchResumeScan;
}
public void ResumeScan()
private void ApiHaltScan()
{
Interlocked.Decrement(ref haltScanRequests);
HaltScan("Download");
}
if (fileScanWasRunning && haltScanRequests == 0)
private void ApiResumeScan()
{
ResumeScan("Download");
}
private void ZoneSwitchHaltScan()
{
HaltScan("Zoning/Gpose");
}
private void ZoneSwitchResumeScan()
{
ResumeScan("Zoning/Gpose");
}
public void ResetLocks()
{
haltScanLocks.Clear();
}
public void ResumeScan(string source)
{
if (!haltScanLocks.ContainsKey(source)) haltScanLocks[source] = 0;
haltScanLocks[source]--;
if (haltScanLocks[source] < 0) haltScanLocks[source] = 0;
if (fileScanWasRunning && haltScanLocks.All(f => f.Value == 0))
{
fileScanWasRunning = false;
InvokeScan(true);
}
}
public void HaltScan()
public void HaltScan(string source)
{
Interlocked.Increment(ref haltScanRequests);
if (!haltScanLocks.ContainsKey(source)) haltScanLocks[source] = 0;
haltScanLocks[source]++;
if (IsScanRunning && haltScanRequests >= 0)
if (IsScanRunning && haltScanLocks.Any(f => f.Value > 0))
{
_scanCancellationTokenSource?.Cancel();
fileScanWasRunning = true;
@@ -81,10 +106,10 @@ public class PeriodicFileScanner : IDisposable
Logger.Verbose("Disposing " + nameof(PeriodicFileScanner));
_ipcManager.PenumbraInitialized -= StartScan;
_apiController.DownloadStarted -= HaltScan;
_apiController.DownloadFinished -= ResumeScan;
_dalamudUtil.ZoneSwitchStart -= HaltScan;
_dalamudUtil.ZoneSwitchEnd -= ResumeScan;
_apiController.DownloadStarted -= ApiHaltScan;
_apiController.DownloadFinished -= ApiResumeScan;
_dalamudUtil.ZoneSwitchStart -= ZoneSwitchHaltScan;
_dalamudUtil.ZoneSwitchEnd -= ZoneSwitchResumeScan;
_scanCancellationTokenSource?.Cancel();
}
@@ -100,7 +125,7 @@ public class PeriodicFileScanner : IDisposable
{
while (!token.IsCancellationRequested)
{
while (haltScanRequests > 0)
while (haltScanLocks.Any(f => f.Value > 0))
{
await Task.Delay(TimeSpan.FromSeconds(1));
}
@@ -125,11 +150,6 @@ public class PeriodicFileScanner : IDisposable
}, token);
}
internal void StartWatchers()
{
InvokeScan();
}
public bool RecalculateFileCacheSize()
{
FileCacheSize = Directory.EnumerateFiles(_pluginConfiguration.CacheFolder).Sum(f =>
@@ -306,10 +326,10 @@ public class PeriodicFileScanner : IDisposable
}
}
private void StartScan()
public void StartScan()
{
if (!_ipcManager.Initialized || !_pluginConfiguration.HasValidSetup()) return;
Logger.Verbose("Penumbra is active, configuration is valid, starting watchers and scan");
Logger.Verbose("Penumbra is active, configuration is valid, scan");
InvokeScan(true);
}
}

View File

@@ -235,14 +235,14 @@ namespace MareSynchronos.Managers
Task.Run(async () =>
{
_periodicFileScanner.HaltScan();
_periodicFileScanner.HaltScan("Character creation");
foreach (var item in unprocessedObjects)
{
_dalamudUtil.WaitWhileCharacterIsDrawing("self " + item.ObjectKind.ToString(), item.Address, 10000, token);
}
CharacterCacheDto? cacheDto = (await CreateFullCharacterCacheDto(token));
_periodicFileScanner.ResumeScan();
_periodicFileScanner.ResumeScan("Character creation");
if (cacheDto == null || token.IsCancellationRequested) return;
#if DEBUG

View File

@@ -3,7 +3,7 @@
<PropertyGroup>
<Authors></Authors>
<Company></Company>
<Version>0.4.20</Version>
<Version>0.4.21</Version>
<Description></Description>
<Copyright></Copyright>
<PackageProjectUrl>https://github.com/Penumbra-Sync/client</PackageProjectUrl>

View File

@@ -81,7 +81,7 @@ namespace MareSynchronos
{
_introUi.IsOpen = false;
_compactUi.IsOpen = true;
_periodicFileScanner.StartWatchers();
_periodicFileScanner.StartScan();
ReLaunchCharacterManager();
};
_compactUi.OpenSettingsUi += () =>
@@ -146,6 +146,7 @@ namespace MareSynchronos
return;
}
_periodicFileScanner.StartScan();
ReLaunchCharacterManager();
}

View File

@@ -3,6 +3,7 @@ using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Dalamud.Interface;
@@ -165,6 +166,15 @@ namespace MareSynchronos.UI
_cacheScanner.InvokeScan(true);
}
}
else if (_cacheScanner.haltScanLocks.Any(f => f.Value > 0))
{
ImGui.Text("Halted (" + string.Join(", ", _cacheScanner.haltScanLocks.Where(f => f.Value > 0).Select(locker => locker.Key + ": " + locker.Value + " halt requests")) + ")");
ImGui.SameLine();
if (ImGui.Button("Reset halt requests##clearlocks"))
{
_cacheScanner.ResetLocks();
}
}
else
{
ImGui.Text("Next scan in " + _cacheScanner.TimeUntilNextScan);
@@ -455,7 +465,7 @@ namespace MareSynchronos.UI
{
_pluginConfiguration.CacheFolder = path;
_pluginConfiguration.Save();
_cacheScanner.StartWatchers();
_cacheScanner.StartScan();
}
});
}