Exemplo n.º 1
0
 /// <summary>
 /// Executes python commands from the console.
 /// </summary>
 /// <param name="input"></param>
 /// <returns>Returns the execution results or error messages.</returns>
 public void Execute(string input)
 {
     try
     {
         if ((input != "") && ((input[input.Length - 1].ToString() == ":") || (multi != ""))) //multiline block incomplete, ask for more
         {
             multi += input + "\n";
             Console.Prompt(Execute);
         }
         else if (multi != "" && input == "") //execute the multiline code after block is finished
         {
             string temp = multi;             // make sure that multi is cleared, even if it returns an error
             multi = "";
             PythonEngine.Execute(temp);
             Console.WriteLine(getOutput());
             Console.Prompt(Execute);
         }
         else // if (multi == "" && input != "") execute single line expressions or statements
         {
             PythonEngine.Execute(input);
             Console.WriteLine(Console.Chomp(getOutput()));
             Console.Prompt(Execute);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error: " + ex.Message + ".");
         Console.Prompt(Execute);
     }
 }
        /// <summary>
        /// Creates a new PythonInterpreter
        /// </summary>
        public PythonInterpreter(Game game, SpriteFont font)
            : base((Game)game)
        {
            this.PythonEngine = new PythonEngine();
            this.PythonOutput = new MemoryStream();
            this.PythonEngine.SetStandardOutput(PythonOutput);
            this.ASCIIEncoder = new ASCIIEncoding();

            ClrModule clr = this.PythonEngine.Import("clr") as ClrModule;
            clr.AddReference("Microsoft.Xna.Framework");
            clr.AddReference("Microsoft.Xna.Framework.Game");

            this.PythonEngine.Execute("from Microsoft.Xna.Framework import *");
            this.PythonEngine.Execute("from Microsoft.Xna.Framework.Graphics import *");
            this.PythonEngine.Execute("from Microsoft.Xna.Framework.Content import *");
            multi = "";

            Console = new XnaConsoleComponent(game, font);
            game.Components.Add(Console);
            Console.Prompt(Execute);
            AddGlobal("Console", Console);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new PythonInterpreter
        /// </summary>
        public PythonInterpreter(Game game, SpriteFont font) : base((Game)game)
        {
            this.PythonEngine = new PythonEngine();
            this.PythonOutput = new MemoryStream();
            this.PythonEngine.SetStandardOutput(PythonOutput);
            this.ASCIIEncoder = new ASCIIEncoding();


            ClrModule clr = this.PythonEngine.Import("clr") as ClrModule;

            clr.AddReference("Microsoft.Xna.Framework");
            clr.AddReference("Microsoft.Xna.Framework.Game");

            this.PythonEngine.Execute("from Microsoft.Xna.Framework import *");
            this.PythonEngine.Execute("from Microsoft.Xna.Framework.Graphics import *");
            this.PythonEngine.Execute("from Microsoft.Xna.Framework.Content import *");
            multi = "";

            Console = new XnaConsoleComponent(game, font);
            game.Components.Add(Console);
            Console.Prompt(Execute);
            AddGlobal("Console", Console);
        }