Store `LuaFile` `FileSystemWatcher` in dictionary
Avoid path string comparisons, `FileSystemWatcher` events may format relative paths differently
This commit is contained in:
parent
6aa7c48402
commit
bd53807b0f
|
@ -31,7 +31,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
private static readonly FilesystemFilterSet SessionsFSFilterSet = new FilesystemFilterSet(new FilesystemFilter("Lua Session Files", new[] { "luases" }));
|
private static readonly FilesystemFilterSet SessionsFSFilterSet = new FilesystemFilterSet(new FilesystemFilter("Lua Session Files", new[] { "luases" }));
|
||||||
|
|
||||||
private readonly LuaAutocompleteInstaller _luaAutoInstaller = new LuaAutocompleteInstaller();
|
private readonly LuaAutocompleteInstaller _luaAutoInstaller = new LuaAutocompleteInstaller();
|
||||||
private readonly List<FileSystemWatcher> _watches = new List<FileSystemWatcher>();
|
private readonly Dictionary<LuaFile, FileSystemWatcher> _watches = new();
|
||||||
|
|
||||||
private readonly int _defaultSplitDistance;
|
private readonly int _defaultSplitDistance;
|
||||||
|
|
||||||
|
@ -261,24 +261,24 @@ namespace BizHawk.Client.EmuHawk
|
||||||
ClearFileWatches();
|
ClearFileWatches();
|
||||||
foreach (var item in LuaImp.ScriptList.Where(s => !s.IsSeparator))
|
foreach (var item in LuaImp.ScriptList.Where(s => !s.IsSeparator))
|
||||||
{
|
{
|
||||||
var processedPath = Config.PathEntries.TryMakeRelative(item.Path);
|
CreateFileWatcher(item);
|
||||||
string pathToLoad = ProcessPath(processedPath);
|
|
||||||
|
|
||||||
CreateFileWatcher(pathToLoad);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ClearFileWatches()
|
private void ClearFileWatches()
|
||||||
{
|
{
|
||||||
foreach (var watch in _watches)
|
foreach (var watch in _watches.Values)
|
||||||
watch.Dispose();
|
watch.Dispose();
|
||||||
_watches.Clear();
|
_watches.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateFileWatcher(string path)
|
private void CreateFileWatcher(LuaFile item)
|
||||||
{
|
{
|
||||||
var (dir, file) = path.SplitPathToDirAndFile();
|
if (_watches.ContainsKey(item))
|
||||||
|
return;
|
||||||
|
|
||||||
|
var (dir, file) = item.Path.MakeProgramRelativePath().SplitPathToDirAndFile();
|
||||||
var watcher = new FileSystemWatcher
|
var watcher = new FileSystemWatcher
|
||||||
{
|
{
|
||||||
Path = dir,
|
Path = dir,
|
||||||
|
@ -289,28 +289,25 @@ namespace BizHawk.Client.EmuHawk
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO, Deleted and Renamed events
|
// TODO, Deleted and Renamed events
|
||||||
watcher.Changed += OnChanged;
|
watcher.Changed += (_, _) => OnLuaFileChanged(item);
|
||||||
|
|
||||||
_watches.Add(watcher);
|
_watches.Add(item, watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RemoveFileWatcher(string path)
|
private void RemoveFileWatcher(LuaFile item)
|
||||||
{
|
{
|
||||||
var (dir, file) = path.SplitPathToDirAndFile();
|
if (_watches.TryGetValue(item, out var watcher))
|
||||||
var watcher = _watches.Find(watcher => watcher.Path == dir && watcher.Filter == file);
|
|
||||||
if (watcher != null)
|
|
||||||
{
|
{
|
||||||
_watches.Remove(watcher);
|
_watches.Remove(item);
|
||||||
watcher.Dispose();
|
watcher.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnChanged(object source, FileSystemEventArgs e)
|
private void OnLuaFileChanged(LuaFile item)
|
||||||
{
|
{
|
||||||
var script = LuaImp.ScriptList.FirstOrDefault(s => s.Path == e.FullPath && s.Enabled);
|
if (item.Enabled && LuaImp?.ScriptList.Contains(item) == true)
|
||||||
if (script is not null)
|
|
||||||
{
|
{
|
||||||
RefreshLuaScript(script);
|
RefreshLuaScript(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,7 +345,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
|
|
||||||
if (Settings.ReloadOnScriptFileChange)
|
if (Settings.ReloadOnScriptFileChange)
|
||||||
{
|
{
|
||||||
CreateFileWatcher(processedPath);
|
CreateFileWatcher(luaFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,7 +371,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
if (!item.IsSeparator)
|
if (!item.IsSeparator)
|
||||||
{
|
{
|
||||||
LuaImp.RegisteredFunctions.RemoveForFile(item, Emulator);
|
LuaImp.RegisteredFunctions.RemoveForFile(item, Emulator);
|
||||||
RemoveFileWatcher(item.Path);
|
RemoveFileWatcher(item);
|
||||||
}
|
}
|
||||||
LuaImp.ScriptList.Remove(item);
|
LuaImp.ScriptList.Remove(item);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue