Skip to content

Commit d665e1b

Browse files
committed
Plugins: Add IPluginOption.SetFromString()
1 parent 8079cc6 commit d665e1b

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Il2CppInspector.CLI/PluginOptions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ public static bool ParsePluginOptions(IEnumerable<string> pluginOptions, Type[]
197197
var targetProp = plugin.Options.First(x => x.Name == prop.Name);
198198
var value = prop.GetValue(optionsObject);
199199

200+
// TODO: Use IPluginOption.SetFromString() instead
201+
200202
// Validate hex strings
201203
if (targetProp is IPluginOptionNumber n && n.Style == PluginOptionNumberStyle.Hex) {
202204
try {

Il2CppInspector.Common/Plugins/API/V100/IPluginOption.cs

+27
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public interface IPluginOption
4343
/// based on the settings of other options or any other desired criteria
4444
/// </summary>
4545
public Func<bool> If { get; set; }
46+
47+
/// <summary>
48+
/// Set an option from a string value
49+
/// </summary>
50+
public void SetFromString(string value);
4651
}
4752

4853
/// <summary>
@@ -112,6 +117,11 @@ public T Value {
112117
}
113118
}
114119

120+
/// <summary>
121+
/// Set an option from a string value
122+
/// </summary>
123+
public abstract void SetFromString(string value);
124+
115125
/// <summary>
116126
/// This can be set to a predicate that determines whether the option is enabled in the GUI
117127
/// By default, enable all options unless overridden
@@ -154,6 +164,8 @@ protected sealed override void InternalValidate(string text) {
154164
if (Required && string.IsNullOrWhiteSpace(text))
155165
throw new ArgumentException("Text cannot be empty");
156166
}
167+
168+
public override void SetFromString(string value) => Value = value;
157169
}
158170

159171
/// <summary>
@@ -220,6 +232,8 @@ protected sealed override void InternalValidate(string path) {
220232
public Dictionary<string, string> AllowedExtensions = new Dictionary<string, string> {
221233
["*"] = "All files"
222234
};
235+
236+
public override void SetFromString(string value) => Value = value;
223237
}
224238

225239
/// <summary>
@@ -228,6 +242,8 @@ protected sealed override void InternalValidate(string path) {
228242
public class PluginOptionBoolean : PluginOption<bool>
229243
{
230244
protected sealed override void InternalValidate(bool value) { }
245+
246+
public override void SetFromString(string value) => Value = bool.Parse(value);
231247
}
232248

233249
/// <summary>
@@ -248,6 +264,10 @@ public class PluginOptionNumber<T> : PluginOption<T>, IPluginOptionNumber where
248264
object IPluginOptionNumber.Value { get => Value; set => Value = (T) value; }
249265

250266
protected sealed override void InternalValidate(T value) { }
267+
268+
public override void SetFromString(string value) {
269+
Value = (T) Convert.ChangeType(Convert.ToInt64(value, Style == PluginOptionNumberStyle.Hex? 16 : 10), typeof(T));
270+
}
251271
}
252272

253273
/// <summary>
@@ -273,5 +293,12 @@ protected sealed override void InternalValidate(T value) {
273293
if (!Choices?.Keys.Contains(value) ?? false)
274294
throw new ArgumentException("Specified choice is not one of the available choices");
275295
}
296+
297+
public override void SetFromString(string value) {
298+
if (typeof(T).IsEnum)
299+
Value = (T) Enum.Parse(typeof(T), value);
300+
else
301+
Value = (T) Convert.ChangeType(value, typeof(T));
302+
}
276303
}
277304
}

Il2CppInspector.GUI/App.xaml.cs

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private class PluginOptionState : IPluginOption
7878
public object Value { get; set; }
7979
[JsonIgnore]
8080
public Func<bool> If { get; set; }
81+
public void SetFromString(string value) { }
8182
}
8283

8384
// Application startup
@@ -149,6 +150,8 @@ internal void LoadOptions() {
149150
managedPlugin.Enabled = savedState.Enabled;
150151

151152
// Set options
153+
// TODO: Use IPluginOption.SetFromString() instead
154+
152155
if (savedState.Plugin.Options != null) {
153156
var options = new Dictionary<string, object>();
154157

0 commit comments

Comments
 (0)