示例#1
0
 /// <summary>
 /// The main entry point for the application.
 /// </summary>
 static void Main(string[] args)
 {
     using (EMSim game = new EMSim())
     {
         game.Run();
     }
 }
示例#2
0
        public EMConsole(GraphicsDevice d, ContentManager content, EMSim sim)
        {
            device      = d;
            this.sim    = sim;
            font        = content.Load <SpriteFont>("ConsoleFont");
            spriteBatch = new SpriteBatch(device);

            EventInput.EventInput.CharEntered += CharEnteredHandler;
            EventInput.EventInput.KeyDown     += KeyDownHandler;

            int fullWidth  = sim.fullscreenWidth;
            int fullHeight = sim.fullscreenHeight;

            overlay = new RectangleOverlay(new Rectangle(10, device.Viewport.Height - 200, device.Viewport.Width - 20, 200), new Rectangle(10, fullHeight - 200, fullWidth - 20, 200), new Color(0.1f, 0.1f, 0.1f, 0.6f), device);
            overlay.LoadContent();


            // Setup python interepreter
            Commands.console = this;
            Commands.sim     = this.sim;
            //Assembly asm = Assembly.LoadFrom("Content\\Microsoft.Scripting.dll");
            Assembly asm        = Assembly.LoadFrom("Microsoft.Scripting.dll");
            Type     engineType = asm.GetType("Microsoft.Scripting.Hosting.ScriptEngine");
            Type     pythonType = typeof(Python);
            Type     scopeType  = asm.GetType("Microsoft.Scripting.Hosting.ScriptScope");

            Type[]       empty             = {};
            Type[]       stringParam       = { typeof(string) };
            Type[]       execParams        = { typeof(string), scopeType };
            Type[]       setPathsParams    = { typeof(ICollection <string>) };
            MethodInfo[] allMethods        = engineType.GetMethods();
            MethodInfo   createMethod      = pythonType.GetMethod("CreateEngine", empty);
            MethodInfo   getSearchMethod   = engineType.GetMethod("GetSearchPaths", empty);
            MethodInfo   setSearchMethod   = engineType.GetMethod("SetSearchPaths", setPathsParams);
            MethodInfo   createScopeMethod = engineType.GetMethod("CreateScope", empty);

            foreach (MethodInfo m in allMethods)
            {
                if (m.Name == "Execute" && m.GetParameters().Length == 2)
                {
                    execMethod = m;
                    break;
                }
            }

            /*try
             * {
             *  execMethod = engineType.GetMethod("Execute", execParams);
             * }
             * catch (AmbiguousMatchException err)
             * {
             *  Console.WriteLine(err.Data.ToString());
             * }*/
            execFileMethod = engineType.GetMethod("ExecuteFile", execParams);
            //return;

            pythonEngine = createMethod.Invoke(null, null);
            //pythonEngine = (object)Python.CreateEngine();
            dynamic paths = getSearchMethod.Invoke(pythonEngine, null);

            //paths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib");
            paths.Add(@"Content\Lib");
            dynamic[] pathsParam = { paths };
            setSearchMethod.Invoke(pythonEngine, pathsParam);

            pythonScope = createScopeMethod.Invoke(pythonEngine, null);

            /*ScriptEngine e = Python.CreateEngine();
             * ScriptScope s = e.CreateScope();
             * e.SetSearchPaths(*/

            invokeCode(@"from __future__ import print_function
def print(*args, **kwargs):
    logf(args, kwargs)

");

            invokeCode(@"def help(command = None):
    if(command is None):
        helpbackup()
    else:
        helpcommand(command)

");

            pythonScope.logf           = new Action <dynamic, dynamic>(Logf);
            pythonScope.log            = new Action <dynamic>(Log);
            pythonScope.helpbackup     = new Action(Commands.Help);
            pythonScope.helpcommand    = new Action <string>(Commands.Help);
            pythonScope.quit           = new Action(Commands.Quit);
            pythonScope.clear          = new Action(Commands.Clear);
            pythonScope.addCharge      = new Action <float, float, float, float>(Commands.AddCharge);
            pythonScope.addSphere      = new Action <float, float, float, float, float>(Commands.AddSphere);
            pythonScope.modifyCharge   = new Action <string, float>(Commands.ModifyCharge);
            pythonScope.modifyChargeID = new Action <int, string, float>(Commands.ModifyChargeID);
            pythonScope.ls             = new Action(Commands.Ls);
            pythonScope.select         = new Action <int>(Commands.Select);
            pythonScope.deleteID       = new Action <int>(Commands.Delete);
            pythonScope.delete         = new Action(Commands.Delete);
            pythonScope.toggleVectors  = new Action(Commands.ToggleVectors);
            pythonScope.toggleLines    = new Action(Commands.ToggleLines);
            pythonScope.genVectors     = new Action(Commands.GenVectors);
            pythonScope.genLines       = new Action(Commands.GenLines);
            pythonScope.autogen        = new Action <bool>(Commands.Autogen);
            pythonScope.evalE          = new Action <float, float, float>(Commands.EvalE);
            pythonScope.lsScripts      = new Action(Commands.LsScripts);
            pythonScope.run            = new Action <string>(Commands.Run);
            pythonScope.openContentDir = new Action(Commands.OpenContentDir);
        }