Exemple #1
0
        private static void WriteAt(string str, int x, int y, bool controlCursor = true)
        {
            int buff_top  = Console.CursorTop;
            int buff_left = Console.CursorLeft;

            if (controlCursor)
            {
                Console.CursorVisible = false;
            }
            Console.SetCursorPosition(x, y);
            //颜色代码至少占2个字符,所以长度不满3的情况下可以直接用Console.Write提升一点点效率(大概可以?)
            if (str.Length > 2)
            {
                ColorfullyConsole.Write(str);
            }
            else
            {
                Console.Write(str);
            }
            Console.SetCursorPosition(buff_left, buff_top);
            if (controlCursor)
            {
                Console.CursorVisible = true;
            }
        }
        bool IConsoleGuide.OpenGuide()
        {
            string InputPrompt_Host = "服务器地址:";
            Random egg = new Random();

            while (string.IsNullOrWhiteSpace(this.ServerHost))
            {
                if (egg.Next(0, 233) == 23)
                {
                    ColorfullyConsole.WriteRainbow(InputPrompt_Host);
                }
                else
                {
                    ColorfullyConsole.Write(InputPrompt_Host, ConsoleColor.White);
                }
                string UserInput = Console.ReadLine();
                //我的解析写的有问题,可能有一些地址是可以用的但是我这边就是匹配不了,所以这边暂时加了一个强制使用符.
                if (UserInput.Length > 0 && UserInput[UserInput.Length - 1] == '!')
                {
                    this.ServerHost = UserInput;
                }
                else
                {
                    var BaseInfo = Minecraft.ServerAddressResolve(UserInput);
                    if (BaseInfo.HasValue)
                    {
                        this.ServerHost = BaseInfo.Value.Host;
                        this.ServerPort = BaseInfo.Value.Port;
                    }
                    else if (InputPrompt_Host[0] != '你')
                    {
                        InputPrompt_Host = "你输入的不是一个有效的服务器地址,请重新输入:";
                    }
                }
            }
            return(true);
        }
        string GetColorCode(string arg)
        {
            if (string.IsNullOrWhiteSpace(arg))
            {
                ColorfullyConsole.Write($"&c错误: \r\n &r选项 \"&e--highlight-color&r\" 没有值");
                Program.Exit(false, -1); return("");
            }

            try
            {
                int ColorCode = Convert.ToInt32(arg, 16);
                if (ColorCode >= 0 && ColorCode <= 0xf)
                {
                    return(ColorfullyConsole.DefaultColorCodeMark + ColorCode.ToString("x"));
                }
            }
            catch (FormatException)
            {
                string ColorText = arg.ToLowerInvariant();
                foreach (var name in typeof(ConsoleColor).GetEnumNames())
                {
                    if (name.ToLowerInvariant() == ColorText && Enum.TryParse(name, out ConsoleColor color))
                    {
                        return(ColorfullyConsole.DefaultColorCodeMark + ((int)color).ToString("x"));
                    }
                }
            }

            ColorfullyConsole.WriteLine($"&c错误&r: 颜色 \"{arg}\" 不存在,可用的颜色:");
            foreach (var name in typeof(ConsoleColor).GetEnumNames())
            {
                ConsoleColor CurrentColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), name);
                ColorfullyConsole.WriteLine($"0x{((int)CurrentColor):x}: {name}", CurrentColor);
            }
            Program.Exit(false, -1); return("");
        }
        protected override void LoadByConsoleOptions(ReadOnlySpan <string> args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            for (int i = 0; i < args.Length; i++)
            {
                try
                {
                    //这边我有点想改成大小写敏感..
                    switch (args[i].ToLowerInvariant())
                    {
                    case "-h":
                    case "-help":
                    case "--help":
                        (this as IConsoleHelp).Show(); break;

                    case "-i":
                    case "-ip":
                    case "-host":
                        this.ServerHost = args[++i]; break;

                    case "-p":
                    case "-port":
                        this.ServerPort = ushort.Parse(args[++i]); break;

                    case "-s":
                    case "-sleep":
                        this.SleepTime = int.Parse(args[++i]); break;

                    case "-b":
                    case "-blood":
                        this.Blood = int.Parse(args[++i]); break;

                    case "--highlight":
                        this.HighlightList.AddRange(args[++i].Replace(',', ',').Split(',')); break;

                    case "--highlight-color":
                        this.HighlightColor = GetColorCode(args[++i]); break;

                    case "--color-minecraft":
                        this.SwitchColorScheme(new ConsolePlus.ColorSchemes.MinecraftColorScheme()); break;

                    case "--watchcat":
                        Watchcat.Instance.Start(1000 * 26, 8, 100.0 / (Environment.ProcessorCount + 0.3)); break;

                    case "--script-logged":
                        this.RunCommandForPlayerJoin = args.Length >= i + 1 ? args[++i] : throw new Exception($"option {args[i]} it value is empty"); break;

                    case "--script-loggedout":
                        this.RunCommandForPlayerDisconnected = args.Length >= i + 1 ? args[++i] : throw new Exception($"option {args[i]} it value is empty"); break;

                    default:
                        ColorfullyConsole.WriteLine($"&c错误:\r\n &r未知命令行选项:{args[i]}\r\n");
                        Program.Exit(false, -1);
                        break;
                    }
                }
                catch (IndexOutOfRangeException)
                {
                    //这边坐标可能有问题
                    if (args.Length <= i + 1)
                    {
                        ColorfullyConsole.WriteLine($"&c错误:\r\n &r命令行选项 \"&e{args[i-1]}&r\" 需要一个参数.\r\n");
                        Program.Exit(false, -1);
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (FormatException)
                {
                    ColorfullyConsole.Write($"&c错误:\r\n &r命令行选项 \"&e{args[i-1]}&r\" 的值无法被转换,");
                    switch (args[i - 1].ToLowerInvariant())
                    {
                    case "-p":
                    case "-port":
                        Console.WriteLine("请输入一个有效的端口号(1-65535)");
                        break;

                    case "-s":
                    case "-b":
                    case "-sleep":
                    case "-blood":
                        Console.WriteLine("它不是一个有效的32位带符号整数。");
                        break;

                    default:
                        Console.WriteLine("超出范围。");
                        break;
                    }
                    Console.WriteLine();
                    Program.Exit(false, -1);
                }
            }
            //这里我当初是怎么想的???
            //if (!string.IsNullOrWhiteSpace(this.ServerHost))
            //    this.ServerPort = Minecraft.DefaultPortOfServer;
        }