From 7f09ac752b4fe4d9c1e8b5cb883703a59d96f418 Mon Sep 17 00:00:00 2001 From: biwa <6475593+biwa@users.noreply.github.com> Date: Sat, 17 Sep 2022 13:53:02 +0200 Subject: [PATCH] UDBScript: fixed a crash when the UDBScript folder was missing or empty. Fixes #789 --- Source/Plugins/UDBScript/BuilderPlug.cs | 31 +++++++++++++------ .../UDBScript/Controls/ScriptDockerControl.cs | 3 ++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Source/Plugins/UDBScript/BuilderPlug.cs b/Source/Plugins/UDBScript/BuilderPlug.cs index 56eeaab6..74126c05 100644 --- a/Source/Plugins/UDBScript/BuilderPlug.cs +++ b/Source/Plugins/UDBScript/BuilderPlug.cs @@ -132,13 +132,18 @@ namespace CodeImp.DoomBuilder.UDBScript General.Actions.BindMethods(this); - watcher = new FileSystemWatcher(Path.Combine(General.AppPath, SCRIPT_FOLDER, "scripts")); - watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size; - watcher.IncludeSubdirectories = true; - watcher.Changed += OnWatcherEvent; - watcher.Created += OnWatcherEvent; - watcher.Deleted += OnWatcherEvent; - watcher.Renamed += OnWatcherEvent; + string scriptspath = Path.Combine(General.AppPath, SCRIPT_FOLDER, "scripts"); + + if (Directory.Exists(scriptspath)) + { + watcher = new FileSystemWatcher(Path.Combine(General.AppPath, SCRIPT_FOLDER, "scripts")); + watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size; + watcher.IncludeSubdirectories = true; + watcher.Changed += OnWatcherEvent; + watcher.Created += OnWatcherEvent; + watcher.Deleted += OnWatcherEvent; + watcher.Renamed += OnWatcherEvent; + } editorexepath = General.Settings.ReadPluginSetting("externaleditor", string.Empty); @@ -154,7 +159,8 @@ namespace CodeImp.DoomBuilder.UDBScript // Methods called by LoadScripts might sleep for some time, so call LoadScripts asynchronously new Task(LoadScripts).Start(); - watcher.EnableRaisingEvents = true; + if (watcher != null) + watcher.EnableRaisingEvents = true; } public override void OnMapOpenEnd() @@ -164,12 +170,14 @@ namespace CodeImp.DoomBuilder.UDBScript // Methods called by LoadScripts might sleep for some time, so call LoadScripts asynchronously new Task(LoadScripts).Start(); - watcher.EnableRaisingEvents = true; + if (watcher != null) + watcher.EnableRaisingEvents = true; } public override void OnMapCloseBegin() { - watcher.EnableRaisingEvents = false; + if (watcher != null) + watcher.EnableRaisingEvents = false; SaveScriptSlotsAndOptions(); SaveScriptDirectoryExpansionStatus(scriptdirectorystructure); @@ -230,6 +238,9 @@ namespace CodeImp.DoomBuilder.UDBScript internal void SaveScriptDirectoryExpansionStatus(ScriptDirectoryStructure root) { + if (root == null) + return; + if(root.Expanded) { General.Settings.DeletePluginSetting("directoryexpand." + root.Hash); diff --git a/Source/Plugins/UDBScript/Controls/ScriptDockerControl.cs b/Source/Plugins/UDBScript/Controls/ScriptDockerControl.cs index 73dd7ceb..75776356 100644 --- a/Source/Plugins/UDBScript/Controls/ScriptDockerControl.cs +++ b/Source/Plugins/UDBScript/Controls/ScriptDockerControl.cs @@ -284,6 +284,9 @@ namespace CodeImp.DoomBuilder.UDBScript { List newnodes = new List(); + if (sds == null) + return newnodes.ToArray(); + // Go through folders and add files (and other folders) recusrively foreach (ScriptDirectoryStructure subsds in sds.Directories.OrderBy(s => s.Name)) {