-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
ConfigurationWatcher.cs
57 lines (47 loc) · 1.42 KB
/
ConfigurationWatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
}
public void Dispose()
{
_watcher.Changed -= WatcherOnChanged;
_watcher.Dispose();
}
public void Register(string fileName, Action<string> fileUpdated)
{
_registeredActions.TryAdd(fileName, fileUpdated);
}
public void Unregister(string fileName)
{
_registeredActions.Remove(fileName);
}
public void Enable()
{
_watcher.EnableRaisingEvents = true;
}
private void WatcherOnChanged(object sender, FileSystemEventArgs eventArgs)
{
if (!_registeredActions.TryGetValue(eventArgs.FullPath, out var value) ||
eventArgs.ChangeType != WatcherChangeTypes.Changed ||
new FileInfo(eventArgs.FullPath).Length == 0)
{
return;
}
value.Invoke(eventArgs.FullPath);
}
}