Ejemplo n.º 1
0
        public override void Initialize()
        {
            DLRIntegrationAddIn addIn = CurrentSession.AddInManager.GetAddIn <DLRIntegrationAddIn>();

            _virtualConsole = new VirtualConsole(CurrentSession, Console);
            addIn.ScriptRuntime.IO.SetOutput(MemoryStream.Null, _virtualConsole.Output);
            addIn.ScriptRuntime.IO.SetErrorOutput(MemoryStream.Null, _virtualConsole.Output);
            _pythonCommandLine = new PythonCommandLine();//(CurrentServer, CurrentSession);
            PythonConsoleOptions consoleOptions = new PythonConsoleOptions();

            addIn.ScriptRuntime.Globals.SetVariable("Session", CurrentSession);
            addIn.ScriptRuntime.Globals.SetVariable("CurrentSession", CurrentSession);
            addIn.ScriptRuntime.Globals.SetVariable("Server", CurrentServer);
            addIn.ScriptRuntime.Globals.SetVariable("CurrentServer", CurrentServer);

            _consoleThread = new Thread(t =>
            {
                _pythonCommandLine.Run(addIn.ScriptRuntime.GetEngine("py"), _virtualConsole, consoleOptions);
            });
            _consoleThread.Start();
            Thread.Sleep(1000);
            _pythonCommandLine.ScriptScope.SetVariable("Session", CurrentSession);
            _pythonCommandLine.ScriptScope.SetVariable("CurrentSession", CurrentSession);
            _pythonCommandLine.ScriptScope.SetVariable("Server", CurrentServer);
            _pythonCommandLine.ScriptScope.SetVariable("CurrentServer", CurrentServer);


            base.Initialize();
        }
Ejemplo n.º 2
0
    protected override ConsoleOptions ParseOptions(string[] args, ScriptRuntimeSetup runtimeSetup, LanguageSetup languageSetup)
    {
        var options = base.ParseOptions(args, runtimeSetup, languageSetup);

        _pyoptions = (PythonConsoleOptions)options;
        return(options);
    }
Ejemplo n.º 3
0
        protected override IConsole CreateConsole(ScriptEngine engine, CommandLine commandLine, ConsoleOptions options)
        {
            PythonConsoleOptions options2 = (PythonConsoleOptions)options;

            if (!options2.BasicConsole)
            {
                return(new SuperConsole(commandLine, options.ColorfulConsole));
            }
            return(new BasicConsole(options.ColorfulConsole));
        }
Ejemplo n.º 4
0
    protected override IConsole CreateConsole(ScriptEngine engine, CommandLine commandLine, ConsoleOptions options)
    {
        PythonConsoleOptions pyoptions = (PythonConsoleOptions)options;

        return(pyoptions.BasicConsole ? new BasicConsole(options.ColorfulConsole) : new SuperConsole(commandLine, options.ColorfulConsole));
    }
Ejemplo n.º 5
0
        public static void Run(bool allocConsole, bool focusWindow, TimeSpan waitTime)
        {
            if (!_enable)
            {
                return;
            }

            System.Threading.Thread.Sleep(waitTime);

            try
            {
                if (allocConsole && IntPtr.Zero == GetConsoleWindow())
                {
                    var hForeground = GetForegroundWindow();
                    var hActiveHwnd = GetActiveWindow();
                    var hFocusHwnd  = GetFocus();

                    AllocConsole();
                    SetConsoleTitle("Unity Console");
                    SetConsoleCP(65001);
                    SetConsoleOutputCP(65001);

                    // reset focus
                    if (!focusWindow)
                    {
                        if (hForeground != IntPtr.Zero)
                        {
                            SetForegroundWindow(hForeground);
                        }
                        if (hActiveHwnd != IntPtr.Zero)
                        {
                            SetActiveWindow(hActiveHwnd);
                        }
                        if (hFocusHwnd != IntPtr.Zero)
                        {
                            SetFocus(hFocusHwnd);
                        }
                    }
                }
            }
            catch
            {
            }

            var oldInStream    = System.Console.In;
            var oldOutStream   = System.Console.Out;
            var oldErrorStream = System.Console.Error;

            try
            {
                if (Environment.GetEnvironmentVariable("TERM") == null)
                {
                    Environment.SetEnvironmentVariable("TERM", "dumb");
                }

                if (allocConsole)
                {
                    if (inStream == null)
                    {
                        inStream = new InternalStream(StandardHandles.STD_INPUT);
                    }
                    outStream = new InternalStream(StandardHandles.STD_OUTPUT);
                    errStream = new InternalStream(StandardHandles.STD_ERROR);

                    oldInStream    = System.Console.In;
                    oldOutStream   = System.Console.Out;
                    oldErrorStream = System.Console.Error;

                    System.Console.SetIn(new StreamReader(inStream));
                    System.Console.SetOut(new StreamWriter(outStream)
                    {
                        AutoFlush = true
                    });
                    System.Console.SetError(new StreamWriter(errStream)
                    {
                        AutoFlush = true
                    });
                }

                var stdwriter = new StreamWriter(new InternalStream(StandardHandles.STD_OUTPUT))
                {
                    AutoFlush = true
                };


                if (MainRuntime == null)
                {
                    var runtimeoptions = new Dictionary <string, object>
                    {
                        ["PrivateBinding"] = true,
                        ["Debug"]          = false,
                        //["Frames"] = false, ["Tracing"] = false,
                    };

                    MainRuntime = Python.CreateRuntime(runtimeoptions);
                    MainEngine  = MainRuntime.GetEngine("py");

                    var scriptfolders = new List <string>();
                    var sb            = new StringBuilder(4096);
                    sb.Length   = 0;
                    sb.Capacity = 4096;
                    if (0 < GetPrivateProfileString("Console", "ScriptsFolders", ".", sb, sb.Capacity, _iniPath))
                    {
                        foreach (var scname in sb.ToString()
                                 .Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                        {
                            var scpath = Path.GetFullPath(Path.IsPathRooted(scname)
                                ? scname
                                : Path.Combine(_rootPath, scname));
                            scriptfolders.Add(scpath);
                        }
                    }
                    if (scriptfolders.Count == 0)
                    {
                        scriptfolders.Add(Path.GetFullPath(_rootPath));
                    }
                    MainEngine.SetSearchPaths(scriptfolders.ToArray());

                    string[] lines;
                    if (GetPrivateProfileSection("Preload.Assemblies", _iniPath, out lines))
                    {
                        foreach (var line in lines)
                        {
                            var asmname = line.Trim();
                            if (string.IsNullOrEmpty(asmname) || asmname.StartsWith(";") || asmname.StartsWith("#"))
                            {
                                continue;
                            }
                            Assembly.Load(new AssemblyName(Path.GetFullPath(Path.Combine(_rootPath, asmname))));
                        }
                    }
                    if (GetPrivateProfileSection("Script.Assemblies", _iniPath, out lines))
                    {
                        foreach (var line in lines)
                        {
                            var asmname = line.Trim();
                            if (string.IsNullOrEmpty(asmname) || asmname.StartsWith(";") || asmname.StartsWith("#"))
                            {
                                continue;
                            }

                            Assembly asm;
                            if (!FindAssembly(asmname, out asm))
                            {
                                stdwriter.WriteLine("Error adding assembly: " + asmname);
                            }
                            else
                            {
                                MainRuntime.LoadAssembly(asm);
                            }
                        }
                    }

                    if (GetPrivateProfileSection("Startup.Script.Py", _iniPath, out lines) && lines != null &&
                        lines.Length > 0)
                    {
                        var str    = string.Join("\n", lines);
                        var source = MainEngine.CreateScriptSourceFromString(str, SourceCodeKind.File);
                        source.Compile().Execute();
                    }
                }
                if (allocConsole && MainEngine != null)
                {
                    var cmdline = new PythonCommandLine();
                    console = new UnityConsole(cmdline);
                    var options = new PythonConsoleOptions
                    {
                        PrintUsage                 = false,
                        PrintVersion               = false,
                        ColorfulConsole            = true,
                        IsMta                      = false,
                        Introspection              = false,
                        TabCompletion              = true,
                        AutoIndentSize             = 2,
                        AutoIndent                 = true,
                        HandleExceptions           = true,
                        IgnoreEnvironmentVariables = true,
                    };
                    cmdline.Run(MainEngine, console, options);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Exception: " + ex.ToString());
                System.Threading.Thread.Sleep(10000);
            }

            console = null;
            System.Console.SetIn(oldInStream);
            System.Console.SetOut(oldOutStream);
            System.Console.SetError(oldErrorStream);

            if (allocConsole)
            {
                Close();
            }

            _enable = false; // TODO: something goes wrong with console after shutdown and restart
        }