Exemplo n.º 1
0
        static void TryStart(bool allocConsole)
        {
            if (TryStarted)
            {
                return;
            }
            TryStarted = true;

            // Only attempt the kernel32 method if this is windows.
            if (allocConsole && Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                AllocConsole();
            }

            try
            {
                // Check if the console has a handle
                Console.Clear();
                // If we didn't crash their, the handle exists.
                ConsoleHandleExists = true;

                outCs = new ConsoleStream(new MemoryStream());

                sw           = new StreamWriter(outCs);
                sw.AutoFlush = true;

                outCs.OnConsoleWrite += outCs_OnConsoleWrite;

                OnMainScreen = true;

                #region Commands
                // Setup default commands
                AddCommand("help", "Displays basic help for all commands.",
                           delegate(string [] args)
                {
                    Console.WriteLine("Defined Commands ({0}):\n", commands.Count);

                    foreach (var pair in commands)
                    {
                        if (!pair.Value.hideInHelp)
                        {
                            Console.WriteLine(String.Format("{0}{1}", pair.Value.command.PadRight(20), pair.Value.help));
                        }
                    }

                    WriteImportant("\nYou can also use the argument --syntax or --? for each command, to view that commands syntax.\n");
                });

                AddCommand("screens", "Displays all registered screens.",
                           delegate(string [] args)
                {
                    Console.WriteLine("Defined Screens ({0}):\n", screens.Count);

                    foreach (var pair in screens)
                    {
                        Console.WriteLine(String.Format("{0}{1}", pair.Value.Name.PadRight(20), pair.Value.Description));
                    }

                    Console.WriteLine();
                });

                AddCommand("screen",
                           "Switches to a screen.", "screen [screen name] (when name not provided, it goes back to main screen)",
                           delegate(string [] args)
                {
                    string screenName = CombineArgs(args);
                    if (screens.ContainsKey(screenName))
                    {
                        OnMainScreen = false;
                        SwitchScreen(screens [screenName]);
                    }
                    else
                    {
                        WriteError(string.Format("Screen '{0}' is not defined.", screenName));
                    }
                });

                AddCommand("cls", "Clears the screen.",
                           delegate(string [] args)
                {
                    Console.Clear();
                    logLines.Clear();
                    top = 0;
                    WriteInputLine();
                });

                AddCommand("allvars", "Prints a list of all available CVars.", "allvars [page]",
                           delegate(string [] args)
                {
                    // Grab page number
                    int page = 0;
                    if (args.Length > 0)
                    {
                        int.TryParse(args [0], out page);
                    }

                    // Calculate number of pages
                    int maxLines     = (Console.BufferHeight - 2);
                    double _numpages = (double)CVars.Count / (double)maxLines;
                    int numpages     = (int)Math.Ceiling(_numpages) - 1;

                    // Check page number
                    if (page > numpages)
                    {
                        page = numpages;
                    }
                    if (page < 0)
                    {
                        page = 0;
                    }

                    // Start log
                    WriteImportant("-- -- CVars (Page: {0} of {1}) -- --", page, numpages);

                    if (CVars.Count != 0)
                    {
                        // Grab enumerator
                        var e = CVars.OrderBy(x => x.Key).GetEnumerator();
                        e.MoveNext();

                        // Skip pages if necessary
                        for (int i = 0; i < maxLines * page; i++)
                        {
                            e.MoveNext();
                        }

                        // Log current page
                        for (int i = maxLines * page; i < maxLines * (page + 1); i++)
                        {
                            KeyValuePair <string, CVar> var = e.Current;
                            WriteLine("{0}= {1}", var.Key.PadRight(20), var.Value.value);

                            if (!e.MoveNext())
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        WriteLine("There are no CVars defined!");
                    }

                    WriteLine("");
                });

                AddCommand("cmd-history", "Sets the length of the history.",
                           "cmd-history <length [current window height - 300]>",
                           delegate(string [] args)
                {
                    if (isLinux)
                    {
                        WriteError("Command not available in Linux terminals.");
                        return;
                    }

                    if (!AllowCMDHistory)
                    {
                        WriteError("Command not allowed.");
                        return;
                    }

                    if (args.Length < 1)
                    {
                        WriteError("Wrong number of arguments");
                        return;
                    }

                    int length = Convert.ToInt32(args [0]);

                    if (length <= Console.WindowHeight || length > 300)
                    {
                        WriteError("Invalid length: must be between the current window height and 300");
                        return;
                    }
                    else
                    {
                        Console.WindowTop    = 0;
                        Console.BufferHeight = length;
                        WriteInputLine();
                    }
                });

                AddCommand("cmd-exit", "Exits DashCMD.",
                           delegate(string [] args)
                {
                    if (!AllowCMDExit)
                    {
                        WriteError("Command not allowed.");
                        return;
                    }

                    StopListening();
                });
                #endregion

                // Mark all commands are core.
                List <string> keys = new List <string>(commands.Keys);
                foreach (string key in keys)
                {
                    Command rcmd = commands [key];
                    if (isLinux && key == "cmd-history")
                    {
                        rcmd.hideInHelp = true;
                    }
                    rcmd.core      = true;
                    commands [key] = rcmd;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }