mirror of
https://github.com/xivdev/Penumbra.git
synced 2025-12-12 10:17:22 +01:00
Improve file watcher.
This commit is contained in:
parent
5bf901d0c4
commit
912c183fc6
5 changed files with 165 additions and 133 deletions
|
|
@ -1 +1 @@
|
|||
Subproject commit 283d51f6f6c7721a810548d95ba83eef2484e17e
|
||||
Subproject commit d889f9ef918514a46049725052d378b441915b00
|
||||
|
|
@ -1,37 +1,69 @@
|
|||
using System.Threading.Channels;
|
||||
using OtterGui.Services;
|
||||
using OtterGui.Services;
|
||||
using Penumbra.Mods.Manager;
|
||||
|
||||
namespace Penumbra.Services;
|
||||
|
||||
public class FileWatcher : IDisposable, IService
|
||||
{
|
||||
private readonly FileSystemWatcher _fsw;
|
||||
private readonly Channel<string> _queue;
|
||||
private readonly CancellationTokenSource _cts = new();
|
||||
private readonly Task _consumer;
|
||||
// TODO: use ConcurrentSet when it supports comparers in Luna.
|
||||
private readonly ConcurrentDictionary<string, byte> _pending = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly ModImportManager _modImportManager;
|
||||
private readonly MessageService _messageService;
|
||||
private readonly Configuration _config;
|
||||
|
||||
private bool _pausedConsumer;
|
||||
private FileSystemWatcher? _fsw;
|
||||
private CancellationTokenSource? _cts = new();
|
||||
private Task? _consumer;
|
||||
|
||||
public FileWatcher(ModImportManager modImportManager, MessageService messageService, Configuration config)
|
||||
{
|
||||
_modImportManager = modImportManager;
|
||||
_messageService = messageService;
|
||||
_config = config;
|
||||
|
||||
if (!_config.EnableDirectoryWatch)
|
||||
if (_config.EnableDirectoryWatch)
|
||||
{
|
||||
SetupFileWatcher(_config.WatchDirectory);
|
||||
SetupConsumerTask();
|
||||
}
|
||||
}
|
||||
|
||||
public void Toggle(bool value)
|
||||
{
|
||||
if (_config.EnableDirectoryWatch == value)
|
||||
return;
|
||||
|
||||
_queue = Channel.CreateBounded<string>(new BoundedChannelOptions(256)
|
||||
_config.EnableDirectoryWatch = value;
|
||||
_config.Save();
|
||||
if (value)
|
||||
{
|
||||
SingleReader = true,
|
||||
SingleWriter = false,
|
||||
FullMode = BoundedChannelFullMode.DropOldest,
|
||||
});
|
||||
SetupFileWatcher(_config.WatchDirectory);
|
||||
SetupConsumerTask();
|
||||
}
|
||||
else
|
||||
{
|
||||
EndFileWatcher();
|
||||
EndConsumerTask();
|
||||
}
|
||||
}
|
||||
|
||||
_fsw = new FileSystemWatcher(_config.WatchDirectory)
|
||||
internal void PauseConsumer(bool pause)
|
||||
=> _pausedConsumer = pause;
|
||||
|
||||
private void EndFileWatcher()
|
||||
{
|
||||
if (_fsw is null)
|
||||
return;
|
||||
|
||||
_fsw.Dispose();
|
||||
_fsw = null;
|
||||
}
|
||||
|
||||
private void SetupFileWatcher(string directory)
|
||||
{
|
||||
EndFileWatcher();
|
||||
_fsw = new FileSystemWatcher
|
||||
{
|
||||
IncludeSubdirectories = false,
|
||||
NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime,
|
||||
|
|
@ -46,44 +78,77 @@ public class FileWatcher : IDisposable, IService
|
|||
|
||||
_fsw.Created += OnPath;
|
||||
_fsw.Renamed += OnPath;
|
||||
UpdateDirectory(directory);
|
||||
}
|
||||
|
||||
|
||||
private void EndConsumerTask()
|
||||
{
|
||||
if (_cts is not null)
|
||||
{
|
||||
_cts.Cancel();
|
||||
_cts = null;
|
||||
}
|
||||
_consumer = null;
|
||||
}
|
||||
|
||||
private void SetupConsumerTask()
|
||||
{
|
||||
EndConsumerTask();
|
||||
_cts = new CancellationTokenSource();
|
||||
_consumer = Task.Factory.StartNew(
|
||||
() => ConsumerLoopAsync(_cts.Token),
|
||||
_cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap();
|
||||
}
|
||||
|
||||
public void UpdateDirectory(string newPath)
|
||||
{
|
||||
if (_config.WatchDirectory != newPath)
|
||||
{
|
||||
_config.WatchDirectory = newPath;
|
||||
_config.Save();
|
||||
}
|
||||
|
||||
if (_fsw is null)
|
||||
return;
|
||||
|
||||
_fsw.EnableRaisingEvents = false;
|
||||
if (!Directory.Exists(newPath) || newPath.Length is 0)
|
||||
{
|
||||
_fsw.Path = string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
_fsw.Path = newPath;
|
||||
_fsw.EnableRaisingEvents = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPath(object? sender, FileSystemEventArgs e)
|
||||
{
|
||||
// Cheap de-dupe: only queue once per filename until processed
|
||||
if (!_config.EnableDirectoryWatch || !_pending.TryAdd(e.FullPath, 0))
|
||||
return;
|
||||
|
||||
_ = _queue.Writer.TryWrite(e.FullPath);
|
||||
}
|
||||
=> _pending.TryAdd(e.FullPath, 0);
|
||||
|
||||
private async Task ConsumerLoopAsync(CancellationToken token)
|
||||
{
|
||||
if (!_config.EnableDirectoryWatch)
|
||||
return;
|
||||
while (true)
|
||||
{
|
||||
var (path, _) = _pending.FirstOrDefault();
|
||||
if (path is null || _pausedConsumer)
|
||||
{
|
||||
await Task.Delay(500, token).ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
var reader = _queue.Reader;
|
||||
while (await reader.WaitToReadAsync(token).ConfigureAwait(false))
|
||||
{
|
||||
while (reader.TryRead(out var path))
|
||||
{
|
||||
try
|
||||
{
|
||||
await ProcessOneAsync(path, token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Penumbra.Log.Debug($"[FileWatcher] Canceled via Token.");
|
||||
Penumbra.Log.Debug("[FileWatcher] Canceled via Token.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Penumbra.Log.Debug($"[FileWatcher] Error during Processing: {ex}");
|
||||
Penumbra.Log.Warning($"[FileWatcher] Error during Processing: {ex}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
@ -91,7 +156,6 @@ public class FileWatcher : IDisposable, IService
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessOneAsync(string path, CancellationToken token)
|
||||
{
|
||||
|
|
@ -115,28 +179,10 @@ public class FileWatcher : IDisposable, IService
|
|||
if (len > 0 && len == lastLen)
|
||||
{
|
||||
if (_config.EnableAutomaticModImport)
|
||||
{
|
||||
_modImportManager.AddUnpack(path);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
var invoked = false;
|
||||
Action<bool> installRequest = args =>
|
||||
{
|
||||
if (invoked)
|
||||
_messageService.AddMessage(new InstallNotification(_modImportManager, path), false);
|
||||
return;
|
||||
|
||||
invoked = true;
|
||||
_modImportManager.AddUnpack(path);
|
||||
};
|
||||
|
||||
_messageService.PrintModFoundInfo(
|
||||
Path.GetFileNameWithoutExtension(path),
|
||||
installRequest);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
lastLen = len;
|
||||
|
|
@ -154,34 +200,10 @@ public class FileWatcher : IDisposable, IService
|
|||
}
|
||||
}
|
||||
|
||||
public void UpdateDirectory(string newPath)
|
||||
{
|
||||
if (!_config.EnableDirectoryWatch || _fsw is null || !Directory.Exists(newPath) || string.IsNullOrWhiteSpace(newPath))
|
||||
return;
|
||||
|
||||
_fsw.EnableRaisingEvents = false;
|
||||
_fsw.Path = newPath;
|
||||
_fsw.EnableRaisingEvents = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!_config.EnableDirectoryWatch)
|
||||
return;
|
||||
|
||||
_fsw.EnableRaisingEvents = false;
|
||||
_cts.Cancel();
|
||||
_fsw.Dispose();
|
||||
_queue.Writer.TryComplete();
|
||||
try
|
||||
{
|
||||
_consumer.Wait(TimeSpan.FromSeconds(5));
|
||||
}
|
||||
catch
|
||||
{
|
||||
/* swallow */
|
||||
}
|
||||
|
||||
_cts.Dispose();
|
||||
EndConsumerTask();
|
||||
EndFileWatcher();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
39
Penumbra/Services/InstallNotification.cs
Normal file
39
Penumbra/Services/InstallNotification.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using Dalamud.Bindings.ImGui;
|
||||
using Dalamud.Interface.ImGuiNotification;
|
||||
using Dalamud.Interface.ImGuiNotification.EventArgs;
|
||||
using OtterGui.Text;
|
||||
using Penumbra.Mods.Manager;
|
||||
|
||||
namespace Penumbra.Services;
|
||||
|
||||
public class InstallNotification(ModImportManager modImportManager, string filePath) : OtterGui.Classes.MessageService.IMessage
|
||||
{
|
||||
public string Message
|
||||
=> "A new mod has been found!";
|
||||
|
||||
public NotificationType NotificationType
|
||||
=> NotificationType.Info;
|
||||
|
||||
public uint NotificationDuration
|
||||
=> uint.MaxValue;
|
||||
|
||||
public string NotificationTitle { get; } = Path.GetFileNameWithoutExtension(filePath);
|
||||
|
||||
public string LogMessage
|
||||
=> $"A new mod has been found: {Path.GetFileName(filePath)}";
|
||||
|
||||
public void OnNotificationActions(INotificationDrawArgs args)
|
||||
{
|
||||
var region = ImGui.GetContentRegionAvail();
|
||||
var buttonSize = new Vector2((region.X - ImGui.GetStyle().ItemSpacing.X) / 2, 0);
|
||||
if (ImUtf8.ButtonEx("Install"u8, ""u8, buttonSize))
|
||||
{
|
||||
modImportManager.AddUnpack(filePath);
|
||||
args.Notification.DismissNow();
|
||||
}
|
||||
|
||||
ImGui.SameLine();
|
||||
if (ImUtf8.ButtonEx("Ignore"u8, ""u8, buttonSize))
|
||||
args.Notification.DismissNow();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,43 +1,19 @@
|
|||
using Dalamud.Bindings.ImGui;
|
||||
using Dalamud.Game.Text;
|
||||
using Dalamud.Game.Text.SeStringHandling;
|
||||
using Dalamud.Game.Text.SeStringHandling.Payloads;
|
||||
using Dalamud.Interface;
|
||||
using Dalamud.Interface.ImGuiNotification;
|
||||
using Dalamud.Interface.ImGuiNotification.EventArgs;
|
||||
using Dalamud.Plugin.Services;
|
||||
using Lumina.Excel.Sheets;
|
||||
using OtterGui.Log;
|
||||
using OtterGui.Services;
|
||||
using OtterGui.Text;
|
||||
using Penumbra.GameData.Data;
|
||||
using Penumbra.Mods.Manager;
|
||||
using Penumbra.String.Classes;
|
||||
using static OtterGui.Classes.MessageService;
|
||||
using Notification = OtterGui.Classes.Notification;
|
||||
|
||||
namespace Penumbra.Services;
|
||||
|
||||
public class InstallNotification(string message, Action<bool> installRequest) : IMessage
|
||||
{
|
||||
private bool _invoked = false;
|
||||
|
||||
public string Message { get; } = message;
|
||||
|
||||
public NotificationType NotificationType => NotificationType.Info;
|
||||
|
||||
public uint NotificationDuration => 10000;
|
||||
|
||||
public void OnNotificationActions(INotificationDrawArgs args)
|
||||
{
|
||||
if (ImUtf8.ButtonEx("Install"u8, "Install this mod."u8, disabled: _invoked))
|
||||
{
|
||||
installRequest(true);
|
||||
_invoked = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class MessageService(Logger log, IUiBuilder builder, IChatGui chat, INotificationManager notificationManager)
|
||||
: OtterGui.Classes.MessageService(log, builder, chat, notificationManager), IService
|
||||
{
|
||||
|
|
@ -79,11 +55,4 @@ public class MessageService(Logger log, IUiBuilder builder, IChatGui chat, INoti
|
|||
$"Cowardly refusing to load replacement for {originalGamePath.Filename().ToString().ToLowerInvariant()} by {mod.Name}{(messageComplement.Length > 0 ? ":\n" : ".")}{messageComplement}",
|
||||
NotificationType.Warning, 10000));
|
||||
}
|
||||
|
||||
public void PrintModFoundInfo(string fileName, Action<bool> installRequest)
|
||||
{
|
||||
AddMessage(
|
||||
new InstallNotification($"A new mod has been found: {fileName}", installRequest)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,8 @@ public class SettingsTab : ITab, IUiService
|
|||
|
||||
public SettingsTab(IDalamudPluginInterface pluginInterface, Configuration config, FontReloader fontReloader, TutorialService tutorial,
|
||||
Penumbra penumbra, FileDialogService fileDialog, ModManager modManager, ModFileSystemSelector selector,
|
||||
CharacterUtility characterUtility, ResidentResourceManager residentResources, ModExportManager modExportManager, FileWatcher fileWatcher, HttpApi httpApi,
|
||||
CharacterUtility characterUtility, ResidentResourceManager residentResources, ModExportManager modExportManager,
|
||||
FileWatcher fileWatcher, HttpApi httpApi,
|
||||
DalamudSubstitutionProvider dalamudSubstitutionProvider, FileCompactor compactor, DalamudConfigService dalamudConfig,
|
||||
IDataManager gameData, PredefinedTagManager predefinedTagConfig, CrashHandlerService crashService,
|
||||
MigrationSectionDrawer migrationDrawer, CollectionAutoSelector autoSelector, CleanupService cleanupService,
|
||||
|
|
@ -651,7 +652,7 @@ public class SettingsTab : ITab, IUiService
|
|||
DrawDefaultModExportPath();
|
||||
Checkbox("Enable Directory Watcher",
|
||||
"Enables a File Watcher that automatically listens for Mod files that enter a specified directory, causing Penumbra to open a popup to import these mods.",
|
||||
_config.EnableDirectoryWatch, v => _config.EnableDirectoryWatch = v);
|
||||
_config.EnableDirectoryWatch, _fileWatcher.Toggle);
|
||||
Checkbox("Enable Fully Automatic Import",
|
||||
"Uses the File Watcher in order to skip the query popup and automatically import any new mods.",
|
||||
_config.EnableAutomaticModImport, v => _config.EnableAutomaticModImport = v);
|
||||
|
|
@ -735,19 +736,24 @@ public class SettingsTab : ITab, IUiService
|
|||
+ "Keep this empty to use the root directory.");
|
||||
}
|
||||
|
||||
private string _tempWatchDirectory = string.Empty;
|
||||
private string? _tempWatchDirectory;
|
||||
|
||||
/// <summary> Draw input for the Automatic Mod import path. </summary>
|
||||
private void DrawFileWatcherPath()
|
||||
{
|
||||
var tmp = _config.WatchDirectory;
|
||||
var tmp = _tempWatchDirectory ?? _config.WatchDirectory;
|
||||
var spacing = new Vector2(UiHelpers.ScaleX3);
|
||||
using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, spacing);
|
||||
ImGui.SetNextItemWidth(UiHelpers.InputTextMinusButton3);
|
||||
if (ImGui.InputText("##fileWatchPath", ref tmp, 256))
|
||||
_tempWatchDirectory = tmp;
|
||||
|
||||
if (ImGui.IsItemDeactivated() && _tempWatchDirectory is not null)
|
||||
{
|
||||
if (ImGui.IsItemDeactivatedAfterEdit())
|
||||
_fileWatcher.UpdateDirectory(_tempWatchDirectory);
|
||||
_tempWatchDirectory = null;
|
||||
}
|
||||
|
||||
ImGui.SameLine();
|
||||
if (ImGuiUtil.DrawDisabledButton($"{FontAwesomeIcon.Folder.ToIconString()}##fileWatch", UiHelpers.IconButtonSize,
|
||||
|
|
@ -761,11 +767,7 @@ public class SettingsTab : ITab, IUiService
|
|||
_fileDialog.OpenFolderPicker("Choose Automatic Import Directory", (b, s) =>
|
||||
{
|
||||
if (b)
|
||||
{
|
||||
_fileWatcher.UpdateDirectory(s);
|
||||
_config.WatchDirectory = s;
|
||||
_config.Save();
|
||||
}
|
||||
}, startDir, false);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue