Ejemplo n.º 1
0
 /// <summary>
 /// Executes Python code and updates output value.
 /// </summary>
 protected override void Update()
 {
     if (!PythonEnvironment.Loaded)
     {
         return;
     }
     if (!Running && initialised)
     {   // Stops running if not initialised
         initialised = false;
         Running     = false;
     }
     if (!Running)
     {
         return;
     }
     if (initialised)
     {
         try
         {   // Attempts to run the update code.
             var result = update.Invoke();
             if (result == null)
             {
                 Error   = "Update code does not return a value.";
                 Running = false;
             }
             else if (result is float || result is int || result is double)
             {
                 OutputValue = Mathf.Clamp(Convert.ToSingle(result), -1f, 1f);
             }
             else
             {
                 Error   = "Update code returns " + result.GetType() + "\ninstead of number.";
                 Running = false;
             }
         }
         catch (Exception e)
         {   // On raised exception, it displays it and stops execution.
             if (e.InnerException != null)
             {
                 e = e.InnerException;
             }
             Error       = PythonEnvironment.FormatException(e);
             Running     = false;
             initialised = false;
         }
     }
     else
     {
         // Initializes axis when starting through Running toggle.
         Initialise();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes Python environment and compiles code.
        /// Sets Running to true if successfull.
        /// </summary>
        protected override void Initialise()
        {
            if (!PythonEnvironment.Loaded)
            {
                return;
            }

            init   = null;
            update = null;

            initialised = false;
            if (!Running && !Game.IsSimulating)
            {
                return;
            }
            Error   = null;
            Running = false;
            if (GlobalScope)
            {
                if (PythonEnvironment.ScripterEnvironment == null)
                {
                    InitGlobalScope();
                }
                python = PythonEnvironment.ScripterEnvironment;
            }
            else
            {
                python = new PythonEnvironment();
            }
            try
            {
                // Attempts to compile initialisation and update code.
                init   = python.Compile(InitialisationCode);
                update = python.Compile(UpdateCode);

                // Executes initialisation code and checks it's scope for linked axes.
                init.Invoke();
                LinkAxes();
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    e = e.InnerException;
                }
                Error = PythonEnvironment.FormatException(e);
                return;
            }
            Running     = true;
            initialised = true;
        }
Ejemplo n.º 3
0
 public static void Error(Exception e)
 {
     _component.enabled = false;
     Debug.Log($"<b><color=#FF0000>Python error: {e.Message}</color></b>\n{PythonEnvironment.FormatException(e)}");
     OnError?.Invoke();
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Called on python console command.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="namedArgs"></param>
        /// <returns></returns>
        internal string PythonCommand(string[] args, IDictionary <string, string> namedArgs)
        {
            if (args.Length == 0)
            {
                return("Executes a Python expression.");
            }

            string expression = "";

            for (int i = 0; i < args.Length; i++)
            {
                expression += args[i] + " ";
            }

            try
            {
                var result = PythonEnvironment.ScripterEnvironment.Execute(expression);
                return(result != null?result.ToString() : "");
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    e = e.InnerException;
                }
                Debug.Log("<b><color=#FF0000>Python error: " + e.Message + "</color></b>\n" + PythonEnvironment.FormatException(e));
                return("");
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Calls Python functions at a fixed rate.
        /// </summary>
        private void FixedUpdate()
        {
            if (!Game.IsSimulating)
            {
                return;
            }

            // Call script update.
            try
            {
                if (!runtime_error)
                {
                    PythonEnvironment.ScripterEnvironment?.CallFixedUpdate();
                }
            }
            catch (Exception e)
            {
                runtime_error = true;
                if (e.InnerException != null)
                {
                    e = e.InnerException;
                }
                ScriptOptions.Instance.ErrorMessage = "Runtime error.\nSee console (Ctrl+K) for more info.";
                Debug.Log("<b><color=#FF0000>Python error: " + e.Message + "</color></b>\n" + PythonEnvironment.FormatException(e));
            }
        }
Ejemplo n.º 6
0
 private void LoadScript()
 {
     try
     {
         if (scriptFile != null)
         {
             PythonEnvironment.ScripterEnvironment.LoadScript(scriptFile);
         }
         else if (scriptCode != null)
         {
             PythonEnvironment.ScripterEnvironment.LoadCode(scriptCode);
         }
         ScriptOptions.Instance.SuccessMessage = "Successfully compiled code.";
     }
     catch (Exception e)
     {
         if (e.InnerException != null)
         {
             e = e.InnerException;
         }
         ScriptOptions.Instance.ErrorMessage = "Error while compiling code.\nSee console (Ctrl+K) for more info.";
         Debug.Log("<b><color=#FF0000>Python error: " + e.Message + "</color></b>\n" + PythonEnvironment.FormatException(e));
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Mod functionality.
        /// Calls Python functions.
        /// </summary>
        private void Update()
        {
            // Initialize block handlers
            if (Game.IsSimulating && !BlockHandlerController.Initialised)
            {
                BlockHandlerController.InitializeBlockHandlers();
            }

            // Initialize block identifiers
            if (!Game.IsSimulating && rebuildIDs)
            {
                rebuildIDs = false;
                BlockHandlerController.InitializeBuildingBlockIDs();
            }

            // Execute code on first call
            if (Game.IsSimulating && PythonEnvironment.Loaded && enableScript && (scriptFile != null || scriptCode != null))
            {
                LoadScript();
                scriptFile = null;
                scriptCode = null;
            }

            // Toggle watchlist visibility
            if (PythonEnvironment.Loaded && Keybindings.Get("Watchlist").Pressed())
            {
                Watchlist.Instance.Visible = !Watchlist.Instance.Visible;
            }

            // Toggle options visibility
            if (PythonEnvironment.Loaded && Keybindings.Get("Script Options").Pressed())
            {
                ScriptOptions.Instance.Visible = !ScriptOptions.Instance.Visible;
            }

            if (!Game.IsSimulating)
            {
                // Show block identifiers
                if (PythonEnvironment.Loaded && Keybindings.Get("Show Block ID").IsDown())
                {
                    ShowBlockIdentifiers();
                }
            }

            if (!Game.IsSimulating)
            {
                return;
            }

            // Call script update.
            try
            {
                if (!runtime_error)
                {
                    PythonEnvironment.ScripterEnvironment?.CallUpdate();
                }
            }
            catch (Exception e)
            {
                runtime_error = true;
                if (e.InnerException != null)
                {
                    e = e.InnerException;
                }
                ScriptOptions.Instance.ErrorMessage = "Runtime error.\nSee console (Ctrl+K) for more info.";
                Debug.Log("<b><color=#FF0000>Python error: " + e.Message + "</color></b>\n" + PythonEnvironment.FormatException(e));
            }
        }