|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | + |
| 4 | +namespace Oxide.IO.Windows |
| 5 | +{ |
| 6 | + internal class WindowsFileSystemWatcher : CachedFileSystemWatcher |
| 7 | + { |
| 8 | + private readonly FileSystemWatcher watcher; |
| 9 | + private int currentSub = -1; |
| 10 | + |
| 11 | + public WindowsFileSystemWatcher(IFileSystem fs, string directory, bool subDirs, NotifyMask filter, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase) : base(directory, subDirs, filter, fs, stringComparison) |
| 12 | + { |
| 13 | + watcher = new FileSystemWatcher(Directory, "*") |
| 14 | + { |
| 15 | + EnableRaisingEvents = false |
| 16 | + }; |
| 17 | + |
| 18 | + watcher.IncludeSubdirectories = IncludeSubDirectories; |
| 19 | + watcher.NotifyFilter = NotifyFilters.LastAccess |
| 20 | + | NotifyFilters.LastWrite |
| 21 | + | NotifyFilters.FileName |
| 22 | + | NotifyFilters.DirectoryName; |
| 23 | + |
| 24 | + watcher.Deleted += OnDeleted; |
| 25 | + watcher.Changed += OnChanged; |
| 26 | + watcher.Created += OnCreated; |
| 27 | + watcher.Renamed += OnRenamed; |
| 28 | + watcher.Error += OnError; |
| 29 | + } |
| 30 | + |
| 31 | + public override void EndInit() |
| 32 | + { |
| 33 | + watcher.EnableRaisingEvents = true; |
| 34 | + base.EndInit(); |
| 35 | + } |
| 36 | + |
| 37 | + protected override int SubscribeTo(string directory) |
| 38 | + { |
| 39 | + return currentSub++; |
| 40 | + } |
| 41 | + |
| 42 | + protected override bool UnsubscribeFrom(int id) |
| 43 | + { |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + protected override void OnDispose() |
| 48 | + { |
| 49 | + watcher.Dispose(); |
| 50 | + } |
| 51 | + |
| 52 | + private void OnRenamed(object sender, RenamedEventArgs args) |
| 53 | + { |
| 54 | + bool isDirectory = System.IO.Directory.Exists(args.FullPath); |
| 55 | + CleanName(args.OldFullPath, out string directory, out string name); |
| 56 | + OnFileSystemEvent(directory, name, isDirectory ? NotifyMask.OnMovedFrom | NotifyMask.DirectoryOnly : NotifyMask.OnMovedFrom); |
| 57 | + CleanName(args.FullPath, out directory, out name); |
| 58 | + OnFileSystemEvent(directory, name, isDirectory ? NotifyMask.OnMovedTo | NotifyMask.DirectoryOnly : NotifyMask.OnMovedTo); |
| 59 | + } |
| 60 | + |
| 61 | + private void OnDeleted(object sender, FileSystemEventArgs args) |
| 62 | + { |
| 63 | + bool isDirectory = args.FullPath.EndsWith(Path.DirectorySeparatorChar.ToString()) || !Path.HasExtension(args.FullPath); |
| 64 | + CleanName(args.FullPath, out string directory, out string name); |
| 65 | + OnFileSystemEvent(directory, name, isDirectory ? NotifyMask.OnDeleted | NotifyMask.DirectoryOnly : NotifyMask.OnDeleted); |
| 66 | + } |
| 67 | + |
| 68 | + private void OnError(object sender, ErrorEventArgs args) => OnFileSystemError(args.GetException()); |
| 69 | + |
| 70 | + private void OnCreated(object sender, FileSystemEventArgs args) |
| 71 | + { |
| 72 | + bool isDirectory = System.IO.Directory.Exists(args.FullPath); |
| 73 | + CleanName(args.FullPath, out string directory, out string name); |
| 74 | + OnFileSystemEvent(directory, name, isDirectory ? NotifyMask.OnCreated | NotifyMask.DirectoryOnly : NotifyMask.OnCreated); |
| 75 | + } |
| 76 | + |
| 77 | + private void OnChanged(object sender, FileSystemEventArgs args) |
| 78 | + { |
| 79 | + bool isDirectory = System.IO.Directory.Exists(args.FullPath); |
| 80 | + CleanName(args.FullPath, out string directory, out string name); |
| 81 | + OnFileSystemEvent(directory, name, isDirectory ? NotifyMask.OnModified | NotifyMask.DirectoryOnly : NotifyMask.OnModified); |
| 82 | + } |
| 83 | + |
| 84 | + protected override void OnFileSystemEvent(string directory, string name, NotifyMask mask) |
| 85 | + { |
| 86 | + directory = FileSystem.ResolvePath(directory); |
| 87 | + string fullPath = Path.Combine(directory, name); |
| 88 | + CleanName(fullPath, out directory, out name); |
| 89 | + base.OnFileSystemEvent(directory, name, mask); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments