Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 06b32b3

Browse files
committed
attempt to fix #84
Added a directory access check everytime a user adds a new scripting directory to a config, and added a logging action everytime a user changes config and the editor could not access one of its dirs to parse the includes, listing it in the log box.
1 parent 7ee2471 commit 06b32b3

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

SourcepawnCondenser/SourcepawnCondenser/SourcemodDefinition/SMDefinition.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ public void Sort()
6060
} //racing condition on save when the thread closes first or not..
6161
}
6262

63-
public void AppendFiles(IEnumerable<string> paths)
63+
public void AppendFiles(IEnumerable<string> paths, out List<string> rejectedPaths)
6464
{
65+
rejectedPaths = new();
6566
foreach (var path in paths)
6667
{
6768
if (Directory.Exists(path))
@@ -86,13 +87,11 @@ public void AppendFiles(IEnumerable<string> paths)
8687
}
8788
catch (UnauthorizedAccessException)
8889
{
89-
// ignored
90+
rejectedPaths.Add(path);
9091
}
9192
}
9293
}
9394

94-
// var editor = Program.MainWindow.GetCurrentEditorElement();
95-
9695
Sort();
9796
ProduceStringArrays();
9897
}

UI/MainWindow/MainWindowConfigHandler.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Windows;
1+
using System.Linq;
2+
using System.Text;
3+
using System.Windows;
24
using System.Windows.Controls;
35
using SPCode.Interop;
46
using SPCode.UI.Components;
@@ -41,7 +43,7 @@ private void Item_Click(object sender, RoutedEventArgs e)
4143
{
4244
var name = (string)((MenuItem)sender).Header;
4345
ChangeConfig(name);
44-
LoggingControl.LogAction($"Changed to config \"{name}\".");
46+
LoggingControl.LogAction($"Changed to config \"{name}\".", 2);
4547
}
4648

4749
/// <summary>
@@ -54,14 +56,30 @@ public void ChangeConfig(int index)
5456
{
5557
return;
5658
}
59+
5760
Program.Configs[index].LoadSMDef();
61+
62+
if (Program.Configs[index].RejectedPaths.Any())
63+
{
64+
var sb = new StringBuilder();
65+
sb.Append("SPCode was unauthorized to access the following directories to parse their includes: \n");
66+
foreach (var path in Program.Configs[index].RejectedPaths)
67+
{
68+
sb.Append($" - {path}\n");
69+
}
70+
71+
LoggingControl.LogAction(sb.ToString());
72+
}
73+
5874
var name = Program.Configs[index].Name;
5975
for (var i = 0; i < ConfigMenu.Items.Count - 2; ++i)
6076
{
6177
((MenuItem)ConfigMenu.Items[i]).IsChecked = name == (string)((MenuItem)ConfigMenu.Items[i]).Header;
6278
}
79+
6380
Program.SelectedConfig = index;
6481
Program.OptionsObject.Program_SelectedConfig = Program.Configs[Program.SelectedConfig].Name;
82+
6583
var editors = GetAllEditorElements();
6684
if (editors != null)
6785
{
@@ -72,6 +90,7 @@ public void ChangeConfig(int index)
7290
editor.InvalidateVisual();
7391
}
7492
}
93+
7594
OBDirList.ItemsSource = Program.Configs[index].SMDirectories;
7695
OBDirList.Items.Refresh();
7796
OBDirList.SelectedIndex = 0;

UI/Windows/ConfigWindow.xaml.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,27 @@ private void AddSMDirButton_Click(object sender, RoutedEventArgs e)
209209
{
210210
IsFolderPicker = true
211211
};
212+
212213
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
213214
{
214215
var c = Program.Configs[ConfigListBox.SelectedIndex];
216+
215217
if (c.SMDirectories.Contains(dialog.FileName))
216218
{
217219
return;
218220
}
221+
222+
try
223+
{
224+
Directory.GetAccessControl(dialog.FileName);
225+
}
226+
catch (UnauthorizedAccessException)
227+
{
228+
this.ShowMessageAsync("Access error",
229+
"The directory you just specified could not be accessed properly by SPCode. You might have trouble using the includes from this directory.",
230+
MessageDialogStyle.Affirmative, Program.MainWindow.MetroDialogOptions);
231+
}
232+
219233
c.SMDirectories.Add(dialog.FileName);
220234
C_SMDir.Items.Refresh();
221235
NeedsSMDefInvalidation = true;

Utils/Models/Config.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
35
using SourcepawnCondenser.SourcemodDefinition;
6+
using SPCode.Interop;
47

58
namespace SPCode.Utils
69
{
@@ -39,6 +42,7 @@ public class Config
3942
private SMDefinition SMDef;
4043

4144
public List<string> SMDirectories;
45+
public List<string> RejectedPaths = new();
4246

4347
public bool Standard;
4448
public int VerboseLevel = 1;
@@ -68,12 +72,18 @@ public void LoadSMDef()
6872
try
6973
{
7074
var def = new SMDefinition();
71-
def.AppendFiles(SMDirectories);
75+
def.AppendFiles(SMDirectories, out var rejectedPaths);
76+
77+
if (rejectedPaths.Any())
78+
{
79+
rejectedPaths.ForEach(x => RejectedPaths.Add(x));
80+
}
81+
7282
SMDef = def;
7383
}
7484
catch (Exception)
7585
{
76-
SMDef = new SMDefinition(); //this could be dangerous...
86+
SMDef = new SMDefinition();
7787
}
7888
}
7989
}

0 commit comments

Comments
 (0)