From d665e1b3c86ad37a21734c190ccbc91c5d7cd786 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Tue, 26 Jan 2021 11:27:56 +0100 Subject: [PATCH] Plugins: Add IPluginOption.SetFromString() --- Il2CppInspector.CLI/PluginOptions.cs | 2 ++ .../Plugins/API/V100/IPluginOption.cs | 27 +++++++++++++++++++ Il2CppInspector.GUI/App.xaml.cs | 3 +++ 3 files changed, 32 insertions(+) diff --git a/Il2CppInspector.CLI/PluginOptions.cs b/Il2CppInspector.CLI/PluginOptions.cs index 663b66d..f14ae46 100644 --- a/Il2CppInspector.CLI/PluginOptions.cs +++ b/Il2CppInspector.CLI/PluginOptions.cs @@ -197,6 +197,8 @@ namespace Il2CppInspector.CLI var targetProp = plugin.Options.First(x => x.Name == prop.Name); var value = prop.GetValue(optionsObject); + // TODO: Use IPluginOption.SetFromString() instead + // Validate hex strings if (targetProp is IPluginOptionNumber n && n.Style == PluginOptionNumberStyle.Hex) { try { diff --git a/Il2CppInspector.Common/Plugins/API/V100/IPluginOption.cs b/Il2CppInspector.Common/Plugins/API/V100/IPluginOption.cs index c9f307b..d1134a5 100644 --- a/Il2CppInspector.Common/Plugins/API/V100/IPluginOption.cs +++ b/Il2CppInspector.Common/Plugins/API/V100/IPluginOption.cs @@ -43,6 +43,11 @@ namespace Il2CppInspector.PluginAPI.V100 /// based on the settings of other options or any other desired criteria /// public Func If { get; set; } + + /// + /// Set an option from a string value + /// + public void SetFromString(string value); } /// @@ -112,6 +117,11 @@ namespace Il2CppInspector.PluginAPI.V100 } } + /// + /// Set an option from a string value + /// + public abstract void SetFromString(string value); + /// /// This can be set to a predicate that determines whether the option is enabled in the GUI /// By default, enable all options unless overridden @@ -154,6 +164,8 @@ namespace Il2CppInspector.PluginAPI.V100 if (Required && string.IsNullOrWhiteSpace(text)) throw new ArgumentException("Text cannot be empty"); } + + public override void SetFromString(string value) => Value = value; } /// @@ -220,6 +232,8 @@ namespace Il2CppInspector.PluginAPI.V100 public Dictionary AllowedExtensions = new Dictionary { ["*"] = "All files" }; + + public override void SetFromString(string value) => Value = value; } /// @@ -228,6 +242,8 @@ namespace Il2CppInspector.PluginAPI.V100 public class PluginOptionBoolean : PluginOption { protected sealed override void InternalValidate(bool value) { } + + public override void SetFromString(string value) => Value = bool.Parse(value); } /// @@ -248,6 +264,10 @@ namespace Il2CppInspector.PluginAPI.V100 object IPluginOptionNumber.Value { get => Value; set => Value = (T) value; } protected sealed override void InternalValidate(T value) { } + + public override void SetFromString(string value) { + Value = (T) Convert.ChangeType(Convert.ToInt64(value, Style == PluginOptionNumberStyle.Hex? 16 : 10), typeof(T)); + } } /// @@ -273,5 +293,12 @@ namespace Il2CppInspector.PluginAPI.V100 if (!Choices?.Keys.Contains(value) ?? false) throw new ArgumentException("Specified choice is not one of the available choices"); } + + public override void SetFromString(string value) { + if (typeof(T).IsEnum) + Value = (T) Enum.Parse(typeof(T), value); + else + Value = (T) Convert.ChangeType(value, typeof(T)); + } } } \ No newline at end of file diff --git a/Il2CppInspector.GUI/App.xaml.cs b/Il2CppInspector.GUI/App.xaml.cs index 4c131e3..dc7bfaf 100644 --- a/Il2CppInspector.GUI/App.xaml.cs +++ b/Il2CppInspector.GUI/App.xaml.cs @@ -78,6 +78,7 @@ namespace Il2CppInspectorGUI public object Value { get; set; } [JsonIgnore] public Func If { get; set; } + public void SetFromString(string value) { } } // Application startup @@ -149,6 +150,8 @@ namespace Il2CppInspectorGUI managedPlugin.Enabled = savedState.Enabled; // Set options + // TODO: Use IPluginOption.SetFromString() instead + if (savedState.Plugin.Options != null) { var options = new Dictionary();