Exemplo n.º 1
0
        /// <summary>
        /// Run a CSharpScriptAsset and return the output.
        /// </summary>
        /// <param name="scriptAsset">The asset to run.</param>
        /// <param name="args">Arguments to pass to it.</param>
        /// <returns>The return value.</returns>
        public static Task <object> RunScript(CSharpScriptAsset scriptAsset, params object[] args)
        {
            // No loaded assembly to run.
            if (scriptAsset.Assembly == null)
            {
                return(Task.FromResult <object>(null));
            }

            // Find the entry point.
            MethodInfo entry       = scriptAsset.Assembly.EntryPoint;
            MethodInfo customEntry = entry?.DeclaringType?.GetMethod("ScriptMain");

            if (customEntry != null)
            {
                entry = customEntry;
            }

            // Run the code.
            if (entry != null)
            {
                return(Task.Run(() => entry.Invoke(null, entry.GetParameters().Length == 0 ? null : args)));
            }
            Engine.Log.Warning("Script has no entry point.", MessageSource.ScriptingEngine);
            return(Task.FromResult <object>(null));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Run a string as CSharp code and return the output.
        /// </summary>
        /// <param name="script">The script to run.</param>
        /// <param name="args">Arguments to pass to it.</param>
        /// <returns>The return value.</returns>
        public static Task <object> RunScript(string script, params object[] args)
        {
            var runtimeAsset = new CSharpScriptAsset(script);

            return(RunScript(runtimeAsset, args).ContinueWith(r =>
            {
                runtimeAsset.Dispose();
                return r.Result;
            }));
        }