Plugins: Add MustExist/MustNotExist/IsFolder to PluginOptionFilePath

This commit is contained in:
Katy Coe
2020-12-28 00:01:04 +01:00
parent a32a68b02f
commit 40a1785dff
3 changed files with 71 additions and 11 deletions

View File

@@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Il2CppInspector.PluginAPI.V100
@@ -148,8 +149,37 @@ namespace Il2CppInspector.PluginAPI.V100
throw new ArgumentException("Path name is required");
// Throws an exception if the path is invalid (file or folder may or may not exist)
System.IO.Path.GetFullPath(path ?? "");
var fullPath = Path.GetFullPath(path ?? "");
var fileName = Path.GetFileName(fullPath);
if (IsFolder) {
if (MustExist && !Directory.Exists(fullPath))
throw new ArgumentException($"Directory {fileName} does not exist");
if (MustNotExist && Directory.Exists(fullPath))
throw new ArgumentException($"Directory {fileName} already exists");
}
else {
if (MustExist && !File.Exists(fullPath))
throw new ArgumentException($"File {fileName} does not exist");
if (MustNotExist && File.Exists(fullPath))
throw new ArgumentException($"File {fileName} already exists");
}
}
/// <summary>
/// Set this to true if you want the user to specify a folder rather than a file
/// </summary>
public bool IsFolder = false;
/// <summary>
/// Set this to true if the specified file or folder must exist
/// </summary>
public bool MustExist = false;
/// <summary>
/// Set this to true if the specified file or folder must not exist
/// </summary>
public bool MustNotExist = false;
}
/// <summary>

View File

@@ -9,12 +9,14 @@
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<UseWPF>true</UseWPF>
<AssemblyName>Il2CppInspector</AssemblyName>
<Version>2020.2.1</Version>
<Version>2021.1</Version>
<Authors>Katy Coe</Authors>
<Company>Noisy Cow Studios</Company>
<Product>Il2CppInspector Windows Edition</Product>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>Il2CppInspector.ico</ApplicationIcon>
<AssemblyVersion>2021.1.0.0</AssemblyVersion>
<FileVersion>2021.1.0.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View File

@@ -25,6 +25,7 @@ using System.Linq;
using Il2CppInspector.PluginAPI.V100;
using Il2CppInspector.Reflection;
using Il2CppInspector;
using Ookii.Dialogs.Wpf;
namespace Il2CppInspectorGUI
{
@@ -134,22 +135,49 @@ namespace Il2CppInspectorGUI
var option = (PluginOptionFilePath) ((Button) sender).DataContext;
string path = null;
if (option.IsFolder && option.MustExist) {
var openFolderDialog = new VistaFolderBrowserDialog {
SelectedPath = option.Value,
Description = option.Description,
UseDescriptionForTitle = true
};
if (openFolderDialog.ShowDialog() == true) {
path = openFolderDialog.SelectedPath;
}
} else if (option.MustExist) {
var openFileDialog = new OpenFileDialog {
Title = option.Description,
Filter = "All files (*.*)|*.*",
FileName = option.Value,
CheckFileExists = false,
CheckPathExists = false
CheckFileExists = true,
CheckPathExists = true
};
if (openFileDialog.ShowDialog() == true) {
path = openFileDialog.FileName;
}
} else {
var saveFileDialog = new SaveFileDialog {
Title = option.Description,
Filter = "All files (*.*)|*.*",
FileName = option.Value,
CheckFileExists = false,
OverwritePrompt = false
};
if (saveFileDialog.ShowDialog() == true) {
path = saveFileDialog.FileName;
}
}
if (path != null) {
// This spaghetti saves us from implementing INotifyPropertyChanged on Plugin.Options
// (we don't want to expose WPF stuff in our SDK)
// Will break if we change the format of the FilePathDataTemplate too much
var tb = ((DockPanel) ((Button) sender).Parent).Children.OfType<StackPanel>().First().Children.OfType<TextBox>().First(n => n.Name == "valueControl");
try {
tb.Clear();
tb.AppendText(openFileDialog.FileName);
tb.AppendText(path);
tb.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
catch { }