Exemplo n.º 1
0
 public void WriteCommand(string data)
 {
     engineProcess.StandardInput.WriteLine(data);
     engineProcess.StandardInput.Flush();
     if (CommandSendEvent != null)
     {
         CommandSendEvent.Invoke(CommandDirection.EngineInput, data);
     }
 }
Exemplo n.º 2
0
        private void ReadCommand(string data)
        {
            if (String.IsNullOrWhiteSpace(data))
            {
                return;
            }

            if (CommandSendEvent != null)
            {
                CommandSendEvent.Invoke(CommandDirection.EngineOutput, data);
            }

            var parts = data.Split(' ');

            if (parts.Length < 0)
            {
                return;
            }

            var command = UciUtils.GetEnum <UciToGui>(parts[0]);

            if (command == null)
            {
                return;
            }

            var cmd = command.Value;
            Dictionary <string, string> parameters;
            string name;

            switch (cmd)
            {
            case UciToGui.Id:
                parameters = CommandParser.GetElements(data, "name", "author");
                name       = parameters.GetValueOrNull("name");
                var author = parameters.GetValueOrNull("author");
                ID(name, author);
                break;

            case UciToGui.UciOk:
                UciOk();
                break;

            case UciToGui.ReadyOk:
                ReadyOk();
                break;

            case UciToGui.BestMove:
                parameters = CommandParser.GetElements(data, "bestmove", "ponder");
                var bestmove = parameters["bestmove"];
                var ponder   = parameters.GetValueOrNull("ponder");
                if (BestMoveCallback != null)
                {
                    BestMoveCallback(UciMove.FromString(bestmove), UciMove.FromString(ponder));
                }
                break;

            case UciToGui.CopyProtection:
                parameters = CommandParser.GetElements(data, "copyprotection");
                var protection = parameters.GetValueOrNull("copyprotection");
                protection = protection.ToLower();
                if (protection == "error")
                {
                    CopyProtection(false);
                }
                else if (protection == "ok")
                {
                    CopyProtection(true);
                }
                break;

            case UciToGui.Registration:
                parameters = CommandParser.GetElements(data, "registration");
                var register = parameters.GetValueOrNull("registration");
                register = register.ToLower();
                if (register == "error")
                {
                    Registration(false);
                    Register(true, null, null);
                }
                else if (register == "ok")
                {
                    Registration(true);
                }
                break;

            case UciToGui.Info:
                parameters = CommandParser.GetElements(data,
                                                       "depth", "seldepth", "time", "nodes", "pv", "multipv", "score", "currmove", "currmovenumber",
                                                       "hashfull", "nps", "tbhits", "sbhits", "cpuload", "string", "refutation", "currline");
                var infos = parameters
                            .Where(x => UciUtils.GetEnum <UciInfo>(x.Key) != null)
                            .ToDictionary(x => UciUtils.GetEnum <UciInfo>(x.Key).Value, x => x.Value);
                if (InfoCallback != null)
                {
                    InfoCallback(infos);
                }
                break;

            case UciToGui.Option:
                parameters = CommandParser.GetElements(data, "name", "type", "default", "min", "max", "var");
                var vars = CommandParser.GetMultipleElements(data, "var").Cast <object>().ToList();
                name = parameters["name"];
                var type = UciUtils.GetEnum <UciOptionType>(parameters["type"]).Value;
                var def  = parameters.GetValueOrNull("default");
                var min  = parameters.ContainsKey("min") ? Convert.ToInt32(parameters["min"]) : new int?();
                var max  = parameters.ContainsKey("max") ? Convert.ToInt32(parameters["max"]) : new int?();
                Option(name, type, def, min, max, vars);
                break;
            }
        }