Plugins: Add IPluginOption.SetFromString()

This commit is contained in:
Katy Coe
2021-01-26 11:27:56 +01:00
parent 8079cc6f43
commit d665e1b3c8
3 changed files with 32 additions and 0 deletions

View File

@@ -197,6 +197,8 @@ namespace Il2CppInspector.CLI
var targetProp = plugin.Options.First(x => x.Name == prop.Name); var targetProp = plugin.Options.First(x => x.Name == prop.Name);
var value = prop.GetValue(optionsObject); var value = prop.GetValue(optionsObject);
// TODO: Use IPluginOption.SetFromString() instead
// Validate hex strings // Validate hex strings
if (targetProp is IPluginOptionNumber n && n.Style == PluginOptionNumberStyle.Hex) { if (targetProp is IPluginOptionNumber n && n.Style == PluginOptionNumberStyle.Hex) {
try { try {

View File

@@ -43,6 +43,11 @@ namespace Il2CppInspector.PluginAPI.V100
/// based on the settings of other options or any other desired criteria /// based on the settings of other options or any other desired criteria
/// </summary> /// </summary>
public Func<bool> If { get; set; } public Func<bool> If { get; set; }
/// <summary>
/// Set an option from a string value
/// </summary>
public void SetFromString(string value);
} }
/// <summary> /// <summary>
@@ -112,6 +117,11 @@ namespace Il2CppInspector.PluginAPI.V100
} }
} }
/// <summary>
/// Set an option from a string value
/// </summary>
public abstract void SetFromString(string value);
/// <summary> /// <summary>
/// This can be set to a predicate that determines whether the option is enabled in the GUI /// This can be set to a predicate that determines whether the option is enabled in the GUI
/// By default, enable all options unless overridden /// By default, enable all options unless overridden
@@ -154,6 +164,8 @@ namespace Il2CppInspector.PluginAPI.V100
if (Required && string.IsNullOrWhiteSpace(text)) if (Required && string.IsNullOrWhiteSpace(text))
throw new ArgumentException("Text cannot be empty"); throw new ArgumentException("Text cannot be empty");
} }
public override void SetFromString(string value) => Value = value;
} }
/// <summary> /// <summary>
@@ -220,6 +232,8 @@ namespace Il2CppInspector.PluginAPI.V100
public Dictionary<string, string> AllowedExtensions = new Dictionary<string, string> { public Dictionary<string, string> AllowedExtensions = new Dictionary<string, string> {
["*"] = "All files" ["*"] = "All files"
}; };
public override void SetFromString(string value) => Value = value;
} }
/// <summary> /// <summary>
@@ -228,6 +242,8 @@ namespace Il2CppInspector.PluginAPI.V100
public class PluginOptionBoolean : PluginOption<bool> public class PluginOptionBoolean : PluginOption<bool>
{ {
protected sealed override void InternalValidate(bool value) { } protected sealed override void InternalValidate(bool value) { }
public override void SetFromString(string value) => Value = bool.Parse(value);
} }
/// <summary> /// <summary>
@@ -248,6 +264,10 @@ namespace Il2CppInspector.PluginAPI.V100
object IPluginOptionNumber.Value { get => Value; set => Value = (T) value; } object IPluginOptionNumber.Value { get => Value; set => Value = (T) value; }
protected sealed override void InternalValidate(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));
}
} }
/// <summary> /// <summary>
@@ -273,5 +293,12 @@ namespace Il2CppInspector.PluginAPI.V100
if (!Choices?.Keys.Contains(value) ?? false) if (!Choices?.Keys.Contains(value) ?? false)
throw new ArgumentException("Specified choice is not one of the available choices"); 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));
}
} }
} }

View File

@@ -78,6 +78,7 @@ namespace Il2CppInspectorGUI
public object Value { get; set; } public object Value { get; set; }
[JsonIgnore] [JsonIgnore]
public Func<bool> If { get; set; } public Func<bool> If { get; set; }
public void SetFromString(string value) { }
} }
// Application startup // Application startup
@@ -149,6 +150,8 @@ namespace Il2CppInspectorGUI
managedPlugin.Enabled = savedState.Enabled; managedPlugin.Enabled = savedState.Enabled;
// Set options // Set options
// TODO: Use IPluginOption.SetFromString() instead
if (savedState.Plugin.Options != null) { if (savedState.Plugin.Options != null) {
var options = new Dictionary<string, object>(); var options = new Dictionary<string, object>();