|
@@ -42,10 +42,9 @@ namespace InABox.Scripting
|
|
|
|
|
|
private string _text = "";
|
|
|
private bool? compiled;
|
|
|
- private MethodInfo method;
|
|
|
- private object obj;
|
|
|
+ private object? obj;
|
|
|
|
|
|
- private Type type;
|
|
|
+ private Type? type;
|
|
|
|
|
|
static ScriptDocument()
|
|
|
{
|
|
@@ -186,10 +185,8 @@ namespace InABox.Scripting
|
|
|
return prop != null ? prop.Value : defaultvalue;
|
|
|
}
|
|
|
|
|
|
- public bool Execute(string classname = "Module", string methodname = "Execute", object[] parameters = null, bool defaultResult = false)
|
|
|
+ private Type? GetClassType(string className = "Module")
|
|
|
{
|
|
|
- var result = defaultResult;
|
|
|
-
|
|
|
if (!compiled.HasValue)
|
|
|
{
|
|
|
compiled = false;
|
|
@@ -198,7 +195,7 @@ namespace InABox.Scripting
|
|
|
if (emitResult.Success)
|
|
|
{
|
|
|
var asm = Assembly.Load(stream.ToArray());
|
|
|
- type = asm.GetTypes().Where(x => x.Name.Equals(classname)).FirstOrDefault();
|
|
|
+ type = asm.GetTypes().Where(x => x.Name.Equals(className)).FirstOrDefault();
|
|
|
if (type != null)
|
|
|
{
|
|
|
obj = Activator.CreateInstance(type);
|
|
@@ -206,36 +203,62 @@ namespace InABox.Scripting
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ return type;
|
|
|
+ }
|
|
|
|
|
|
- if (compiled.Value)
|
|
|
+ public object? GetObject(string className = "Module")
|
|
|
+ {
|
|
|
+ GetClassType(className);
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ public MethodInfo? GetMethod(string className = "Module", string methodName = "Execute")
|
|
|
+ {
|
|
|
+ var type = GetClassType(className);
|
|
|
+ if (compiled == true && type != null)
|
|
|
+ {
|
|
|
+ return type.GetMethod(methodName);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool Execute(string classname = "Module", string methodname = "Execute", object[]? parameters = null, bool defaultResult = false)
|
|
|
+ {
|
|
|
+ var result = defaultResult;
|
|
|
+
|
|
|
+ var type = GetClassType(classname);
|
|
|
+ var obj = GetObject(classname);
|
|
|
+ var method = GetMethod(classname, methodname);
|
|
|
+
|
|
|
+ if (compiled == true && type != null && method != null)
|
|
|
{
|
|
|
foreach (var property in Properties)
|
|
|
{
|
|
|
var prop = type.GetProperty(property.Name);
|
|
|
- if (prop != null)
|
|
|
- prop.SetValue(obj, property.Value);
|
|
|
+ prop?.SetValue(obj, property.Value);
|
|
|
}
|
|
|
|
|
|
- method = type.GetMethod(methodname);
|
|
|
- if (method != null)
|
|
|
+ if (method.ReturnType == typeof(bool))
|
|
|
{
|
|
|
- if (method.ReturnType == typeof(bool))
|
|
|
- {
|
|
|
- result = (bool)(method.Invoke(obj, parameters ?? Array.Empty<object>()) ?? false);
|
|
|
- }
|
|
|
- else
|
|
|
+ result = (bool)(method.Invoke(obj, parameters ?? Array.Empty<object>()) ?? false);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ method.Invoke(obj, parameters ?? Array.Empty<object>());
|
|
|
+ result = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result)
|
|
|
+ {
|
|
|
+ foreach (var property in Properties)
|
|
|
{
|
|
|
- method.Invoke(obj, parameters ?? Array.Empty<object>());
|
|
|
- result = true;
|
|
|
+ var prop = type.GetProperty(property.Name);
|
|
|
+ if (prop != null)
|
|
|
+ property.Value = prop.GetValue(obj);
|
|
|
}
|
|
|
-
|
|
|
- if (result)
|
|
|
- foreach (var property in Properties)
|
|
|
- {
|
|
|
- var prop = type.GetProperty(property.Name);
|
|
|
- if (prop != null)
|
|
|
- property.Value = prop.GetValue(obj);
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|