Plugins: Add PluginOptionFilePath.AllowedExtensions

This commit is contained in:
Katy Coe
2020-12-29 18:44:42 +01:00
parent aecd41846d
commit e09002a060
2 changed files with 26 additions and 4 deletions

View File

@@ -161,8 +161,8 @@ namespace Il2CppInspector.PluginAPI.V100
/// </summary> /// </summary>
public class PluginOptionFilePath : PluginOption<string> public class PluginOptionFilePath : PluginOption<string>
{ {
// Don't asllow required path name to be empty
protected sealed override void InternalValidate(string path) { protected sealed override void InternalValidate(string path) {
// Don't allow required path name to be empty
if (Required && string.IsNullOrWhiteSpace(path)) if (Required && string.IsNullOrWhiteSpace(path))
throw new ArgumentException("Path name is required"); throw new ArgumentException("Path name is required");
@@ -182,6 +182,16 @@ namespace Il2CppInspector.PluginAPI.V100
if (MustNotExist && File.Exists(fullPath)) if (MustNotExist && File.Exists(fullPath))
throw new ArgumentException($"File {fileName} already exists"); 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");
}
} }
/// <summary> /// <summary>
@@ -198,6 +208,14 @@ namespace Il2CppInspector.PluginAPI.V100
/// Set this to true if the specified file or folder must not exist /// Set this to true if the specified file or folder must not exist
/// </summary> /// </summary>
public bool MustNotExist = false; public bool MustNotExist = false;
/// <summary>
/// List of file extensions to allow with descriptions for GUI, when selecting a file
/// Specify * to allow all extensions
/// </summary>
public Dictionary<string, string> AllowedExtensions = new Dictionary<string, string> {
["*"] = "All files"
};
} }
/// <summary> /// <summary>

View File

@@ -161,6 +161,8 @@ namespace Il2CppInspectorGUI
var option = (PluginOptionFilePath) ((Button) sender).DataContext; 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; string path = null;
if (option.IsFolder && option.MustExist) { if (option.IsFolder && option.MustExist) {
@@ -175,7 +177,7 @@ namespace Il2CppInspectorGUI
} else if (option.MustExist) { } else if (option.MustExist) {
var openFileDialog = new OpenFileDialog { var openFileDialog = new OpenFileDialog {
Title = option.Description, Title = option.Description,
Filter = "All files (*.*)|*.*", Filter = filter,
FileName = option.Value, FileName = option.Value,
CheckFileExists = true, CheckFileExists = true,
CheckPathExists = true CheckPathExists = true
@@ -186,7 +188,7 @@ namespace Il2CppInspectorGUI
} else { } else {
var saveFileDialog = new SaveFileDialog { var saveFileDialog = new SaveFileDialog {
Title = option.Description, Title = option.Description,
Filter = "All files (*.*)|*.*", Filter = filter,
FileName = option.Value, FileName = option.Value,
CheckFileExists = false, CheckFileExists = false,
OverwritePrompt = false OverwritePrompt = false
@@ -270,7 +272,9 @@ namespace Il2CppInspectorGUI
// Revert changes // Revert changes
foreach (var option in OriginalOptions) foreach (var option in OriginalOptions)
try {
ManagedPlugin[option.Key] = option.Value; ManagedPlugin[option.Key] = option.Value;
} catch { }
// Replace options in ListBox // Replace options in ListBox
lstOptions.ItemsSource = Plugin.Options; lstOptions.ItemsSource = Plugin.Options;