From e09002a0607076c9e4bcd96ce4ae4ab6c3aa6d61 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Tue, 29 Dec 2020 18:44:42 +0100 Subject: [PATCH] Plugins: Add PluginOptionFilePath.AllowedExtensions --- .../Plugins/API/V100/IPluginOption.cs | 20 ++++++++++++++++++- .../PluginConfigurationDialog.xaml.cs | 10 +++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Il2CppInspector.Common/Plugins/API/V100/IPluginOption.cs b/Il2CppInspector.Common/Plugins/API/V100/IPluginOption.cs index ef4d6ce..297d754 100644 --- a/Il2CppInspector.Common/Plugins/API/V100/IPluginOption.cs +++ b/Il2CppInspector.Common/Plugins/API/V100/IPluginOption.cs @@ -161,8 +161,8 @@ namespace Il2CppInspector.PluginAPI.V100 /// public class PluginOptionFilePath : PluginOption { - // Don't asllow required path name to be empty protected sealed override void InternalValidate(string path) { + // Don't allow required path name to be empty if (Required && string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Path name is required"); @@ -182,6 +182,16 @@ namespace Il2CppInspector.PluginAPI.V100 if (MustNotExist && File.Exists(fullPath)) throw new ArgumentException($"File {fileName} already exists"); } + + // Check selected file has a valid extension + if (!IsFolder && !AllowedExtensions.ContainsKey("*")) { + var ext = Path.GetExtension(fullPath).ToLower(); + if (ext.StartsWith('.')) + ext = ext.Substring(1); + + if (!AllowedExtensions.ContainsKey(ext)) + throw new ArgumentException($"File {fileName} has an invalid filename extension"); + } } /// @@ -198,6 +208,14 @@ namespace Il2CppInspector.PluginAPI.V100 /// Set this to true if the specified file or folder must not exist /// public bool MustNotExist = false; + + /// + /// List of file extensions to allow with descriptions for GUI, when selecting a file + /// Specify * to allow all extensions + /// + public Dictionary AllowedExtensions = new Dictionary { + ["*"] = "All files" + }; } /// diff --git a/Il2CppInspector.GUI/PluginConfigurationDialog.xaml.cs b/Il2CppInspector.GUI/PluginConfigurationDialog.xaml.cs index 6f0e0b3..d494006 100644 --- a/Il2CppInspector.GUI/PluginConfigurationDialog.xaml.cs +++ b/Il2CppInspector.GUI/PluginConfigurationDialog.xaml.cs @@ -161,6 +161,8 @@ namespace Il2CppInspectorGUI var option = (PluginOptionFilePath) ((Button) sender).DataContext; + var filter = string.Join('|', option.AllowedExtensions.Select(e => $"{e.Value} (*.{e.Key.ToLower()})|*.{e.Key.ToLower()}")); + string path = null; if (option.IsFolder && option.MustExist) { @@ -175,7 +177,7 @@ namespace Il2CppInspectorGUI } else if (option.MustExist) { var openFileDialog = new OpenFileDialog { Title = option.Description, - Filter = "All files (*.*)|*.*", + Filter = filter, FileName = option.Value, CheckFileExists = true, CheckPathExists = true @@ -186,7 +188,7 @@ namespace Il2CppInspectorGUI } else { var saveFileDialog = new SaveFileDialog { Title = option.Description, - Filter = "All files (*.*)|*.*", + Filter = filter, FileName = option.Value, CheckFileExists = false, OverwritePrompt = false @@ -270,7 +272,9 @@ namespace Il2CppInspectorGUI // Revert changes foreach (var option in OriginalOptions) - ManagedPlugin[option.Key] = option.Value; + try { + ManagedPlugin[option.Key] = option.Value; + } catch { } // Replace options in ListBox lstOptions.ItemsSource = Plugin.Options;