add configuration watcher implementation

This commit is contained in:
RaidMax 2023-04-04 21:44:08 -05:00
parent c14042a109
commit 51fae05a73
2 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.IO;
using SharedLibraryCore;
namespace IW4MAdmin.Application.IO;
public sealed class ConfigurationWatcher : IDisposable
{
private readonly FileSystemWatcher _watcher;
private readonly Dictionary<string, Action<string>> _registeredActions = new();
public ConfigurationWatcher()
{
_watcher = new FileSystemWatcher
{
Path = Path.Join(Utilities.OperatingDirectory, "Configuration"),
Filter = "*.json",
NotifyFilter = NotifyFilters.LastWrite
};
_watcher.Changed += WatcherOnChanged;
_watcher.EnableRaisingEvents = true;
}
public void Dispose()
{
_watcher.Changed -= WatcherOnChanged;
_watcher.Dispose();
}
public void Register(string fileName, Action<string> fileUpdated)
{
if (_registeredActions.ContainsKey(fileName))
{
return;
}
_registeredActions.Add(fileName, fileUpdated);
}
public void Unregister(string fileName)
{
if (_registeredActions.ContainsKey(fileName))
{
_registeredActions.Remove(fileName);
}
}
private void WatcherOnChanged(object sender, FileSystemEventArgs eventArgs)
{
if (!_registeredActions.ContainsKey(eventArgs.FullPath) || eventArgs.ChangeType != WatcherChangeTypes.Changed ||
new FileInfo(eventArgs.FullPath).Length == 0)
{
return;
}
_registeredActions[eventArgs.FullPath].Invoke(eventArgs.FullPath);
}
}

View File

@ -29,6 +29,7 @@ using Data.Helpers;
using Integrations.Source.Extensions;
using IW4MAdmin.Application.Alerts;
using IW4MAdmin.Application.Extensions;
using IW4MAdmin.Application.IO;
using IW4MAdmin.Application.Localization;
using IW4MAdmin.Application.Plugin;
using IW4MAdmin.Application.Plugin.Script;