浏览代码

Added ability to store a reference to a method in a script

Kenric Nugteren 1 年之前
父节点
当前提交
76466fffa2
共有 1 个文件被更改,包括 50 次插入27 次删除
  1. 50 27
      inabox.scripting/ScriptDocument.cs

+ 50 - 27
inabox.scripting/ScriptDocument.cs

@@ -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);
-                        }
                 }
             }