Exemplo 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();
        }
Exemplo n.º 2
0
    public static async Task Main()
    {
        var eng = global::IronPython.Hosting.Python.CreateEngine();
        var pythonCommandLine = new PythonCommandLine();

        pythonCommandLine.Run(eng, new Console(), new PythonConsoleOptions()
        {
            SkipImportSite = true
        });
    }
Exemplo n.º 3
0
        private static void Main()
        {
            Events.Start();

            Logger.Debug("Loading...");

            ObjectManager <IRoom> .Instance.RegisterManager <RoomManager>();

            ObjectManager <IItem> .Instance.RegisterManager <ItemManager>();

            ObjectManager <IMessenger> .Instance.RegisterManager <MessengerManager>();

            Logger.Debug("Loading objects...");
            foreach (var assembly in Configuration.Config.GetSection("assemblies").GetChildren())
            {
                MainManager.Instance.LoadAssembly(assembly.Value);
            }

            foreach (var python in Configuration.Config.GetSection("python").GetChildren())
            {
                MainManager.Instance.LoadPython(Path.GetDirectoryName(python.Value), Path.GetFileName(python.Value));
            }

            MainManager.Instance.LoadAssembly(Assembly.GetExecutingAssembly());


            Logger.Info("Working!");

            // To allow long strings
            Console.SetIn(new StreamReader(Console.OpenStandardInput(),
                                           Console.InputEncoding,
                                           false,
                                           16384));

            var commandLine = new PythonCommandLine();
            var engine      = Python.CreateEngine();

            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                engine.Runtime.LoadAssembly(assembly);
            }

            commandLine.Run(engine, new SuperConsole(commandLine, true), new PythonConsoleOptions
            {
                AutoIndent      = true,
                ColorfulConsole = true,
                TabCompletion   = true
            });

            Logger.Info("Saving users...");
            UserManager.Instance.Flush();
            Logger.Debug("Done!");
            Thread.Sleep(500); // Finish logging
            Environment.Exit(0);
        }
Exemplo n.º 4
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
        }