/// <summary>
        /// Starts the $PythonBehaviour
        /// </summary>
        /// <param name="classname">Name of the Script</param>
        public bool Awakening(String classname)
        {
            scope = engine.CreateScope();
            scope.SetVariable("this", this);
            scope.SetVariable("gameObject", gameObject);
            scope.SetVariable("transform", transform);
            scope.SetVariable("enabled", enabled);
            scope.SetVariable("useAPI", new Action(UseAPI));
            scope.SetVariable("disableAPI", new Action(DisableAPI));

            if (Settings.useAPI)
            {
                Besiege.SetUp();
                scope.SetVariable("besiege", Besiege._besiege);
            }
            spaar.ModLoader.Game.OnSimulationToggle += GameOnOnSimulationToggle;
            spaar.ModLoader.Game.OnLevelWon += GameOnOnLevelWon;

            foreach (string @ref in refs.Where(@ref => !String.IsNullOrEmpty(@ref)))
            {
                try
                {

                    #region OBSOLETE
                    /*
                    Assembly assembly = Assembly.Load(@ref);
                    var namespaces = assembly.GetTypes()
                        .Select(t => t.Namespace)
                        .Distinct();
                    String[] lines = Util.splitStringAtNewline(sauce);
                    for (int i = 0; i < lines.Length; i++)
                    {
                        if (!lines[i].Contains("import") && !String.IsNullOrEmpty(lines[i]))
                        {
                            foreach (string ns in namespaces)
                            {
                                if (!String.IsNullOrEmpty(ns))
                                {
                                    if (lines[i].Contains((ns + ".")))
                                    {
                                        lines[i] = Regex.Replace(lines[i], ns + ".", string.Empty);
                                    }
                                }
                            }
                        }
                        lines[i] += Util.getNewLine();
                    }
                    lines = lines.Where(x => !string.IsNullOrEmpty(x) && !x.Equals("\r\n") && !x.Equals("\r") && !x.Equals("\n") && !String.IsNullOrEmpty(x.Trim())).ToArray();
                    sauce = String.Concat(lines);
                    */
                    #endregion

                    engine.Runtime.LoadAssembly(Assembly.Load(@ref));
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                    return false;
                }
            }
            ScriptSource source = engine.CreateScriptSourceFromString(sauce);
            ErrorListener error = new ErrorSinkProxyListener(ErrorSink.Default);
            code = source.Compile(error);
            if (code == null)
            {
                Debug.LogError(error);
                return false;
            }
            code.Execute(scope);
            pythonClass = engine.Operations.Invoke(scope.GetVariable(classname));
            CallMethod("Awake");
            return true;
        }
Esempio n. 2
0
        public Guid RunScriptAsync(string code)
        {
            var scriptScope = _defaultEngine.CreateScope();
            scriptScope.SetVariable("logger", IRE.Instance);

            CompiledCode compiledCode = null;
            try
            {
                ScriptSource scriptSource = _defaultEngine.CreateScriptSourceFromString(code, SourceCodeKind.AutoDetect);

                var errListner = new ErrorSinkProxyListener(ErrorSink.Default);
                compiledCode = scriptSource.Compile(errListner);
            }
            catch (Exception ex)
            {
                WriteError(ex.Message);
                return Guid.Empty;
            }

            var action = new Action(() => compiledCode.Execute(scriptScope));
            var tokenSource = new CancellationTokenSource();
            var task = new Task(action, tokenSource.Token, TaskCreationOptions.LongRunning);
            var guid = Guid.NewGuid();
            AddTask(guid, new TaskRecord { TokenSource = tokenSource, Task = task });

            task.Start();
            task.ContinueWith((t) =>
                                  {
                                      // Actually we don't needed this due to "TaskContinuationOptions.OnlyOnFaulted" is set
                                      if (t.Exception == null)
                                          return;

                                      t.Exception.Flatten().Handle((ex) =>
                                                             {
                                                                 Instance.WriteError(t.Exception.InnerException.Message);
                                                                 return true;
                                                             });
                                  }, TaskContinuationOptions.OnlyOnFaulted);
            task.ContinueWith((t) => Instance.RemoveTask(guid));
            return guid;
        }