Beispiel #1
0
        void Initialize()
        {
            logFactory = new LogFactory(type =>
            {
                return((level, message, exception) =>
                {
                    switch (level)
                    {
                    case LogLevel.Warning:
                    case LogLevel.Error:
                    case LogLevel.Critical:
                        Debug.LogWarning(message);
                        break;
                    }
                });
            });

            context = new ScriptContext(
                SourceText.From(string.Empty),
                Directory.GetCurrentDirectory(),
                Enumerable.Empty <string>(),
                scriptMode: ScriptMode.REPL
                );

            compiler = new ScriptCompiler(logFactory, useRestoreCache: false);

            @out = new CommandLineTextWriter(this);

            globals = new InteractiveScriptGlobals(@out, CSharpObjectFormatter.Instance);

            var compilation = compiler.CreateCompilationContext <object, InteractiveScriptGlobals>(context);

            compilation.Warnings.ToList().ForEach((x) => Debug.LogFormat(x.ToString()));
            compilation.Errors.ToList().ForEach((x) => Debug.LogFormat(x.ToString()));

            var task = compilation.Script.RunAsync(globals, ex => true);

            task.Wait();
            state   = task.Result;
            options = compilation.ScriptOptions;
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    if (assembly.IsDynamic)
                    {
                        continue;
                    }
                    Debug.LogFormat("Add reference: {0}", assembly.FullName);
                    options = options.AddReferences(assembly);
                }
                catch (Exception exception)
                {
                    Debug.Log(exception.ToString());
                }
            }
        }
Beispiel #2
0
        /// <inheritdoc />
        public bool RunAction(Package pack, string action)
        {
            bool success = true;

            try
            {
                var logger   = CreateScriptLogger(this);
                var src      = SourceText.From(pack.Meta.ScriptText);
                var ctx      = new ScriptContext(src, Environment.CurrentDirectory, Enumerable.Empty <string>());
                var compiler = new ScriptCompiler(logger, true);
                var compctx  = compiler.CreateCompilationContext <object, ScriptHost>(ctx);

                foreach (var ass in ReferencedAssemblies)
                {
                    compctx.Loader.RegisterDependency(ass);
                }

                var host = new ScriptHost(pack, Uppm.Implementation);

                var scriptResultT = compctx.Script.RunAsync(host, exception =>
                {
                    Log.Fatal(exception, "Script of {$PackRef} threw an exception:", pack.Meta.Self);
                    success = false;
                    return(true);
                });
                scriptResultT.Wait();
                var scriptResult = scriptResultT.Result;

                var result = scriptResult.ReturnValue;

                if (result != null)
                {
                    if (!(result.Get(action) is Action commandDelegate))
                    {
                        Log.Fatal("Script of {$PackRef} doesn't contain action {ScriptAction}", pack.Meta.Self, action);
                        success = false;
                    }
                    else
                    {
                        try
                        {
                            commandDelegate();
                        }
                        catch (Exception e)
                        {
                            Log.Error(e, "Script of {$PackRef} thrown an unhandled exception", pack.Meta.Self);
                            success = false;
                        }
                    }
                }
                else
                {
                    Log.Error("Script of {$PackRef} didn't return an object", pack.Meta.Self);
                    success = false;
                }
            }