add configuration watcher implementation
This commit is contained in:
parent
c14042a109
commit
51fae05a73
60
Application/IO/ConfigurationWatcher.cs
Normal file
60
Application/IO/ConfigurationWatcher.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -29,6 +29,7 @@ using Data.Helpers;
|
|||||||
using Integrations.Source.Extensions;
|
using Integrations.Source.Extensions;
|
||||||
using IW4MAdmin.Application.Alerts;
|
using IW4MAdmin.Application.Alerts;
|
||||||
using IW4MAdmin.Application.Extensions;
|
using IW4MAdmin.Application.Extensions;
|
||||||
|
using IW4MAdmin.Application.IO;
|
||||||
using IW4MAdmin.Application.Localization;
|
using IW4MAdmin.Application.Localization;
|
||||||
using IW4MAdmin.Application.Plugin;
|
using IW4MAdmin.Application.Plugin;
|
||||||
using IW4MAdmin.Application.Plugin.Script;
|
using IW4MAdmin.Application.Plugin.Script;
|
||||||
|
Loading…
Reference in New Issue
Block a user