Fix option names for UCI dialects
authorFabian Fichter <ianfab@users.noreply.github.com>
Mon, 29 Nov 2021 22:26:11 +0000 (23:26 +0100)
committerFabian Fichter <ianfab@users.noreply.github.com>
Tue, 30 Nov 2021 09:10:17 +0000 (10:10 +0100)
Closes #412.

src/uci.cpp
src/uci.h
src/ucioption.cpp

index 28e1a57..1903731 100644 (file)
@@ -116,8 +116,7 @@ namespace {
     if (Options.count(name))
         Options[name] = value;
     // UCI dialects do not allow spaces
-    else if (   (Options["Protocol"] == "ucci" || Options["Protocol"] == "usi")
-             && (std::replace(name.begin(), name.end(), '_', ' '), Options.count(name)))
+    else if (is_valid_option(Options, name))
         Options[name] = value;
     else
         sync_cout << "No such option: " << name << sync_endl;
@@ -550,4 +549,32 @@ Move UCI::to_move(const Position& pos, string& str) {
   return MOVE_NONE;
 }
 
+std::string UCI::option_name(std::string name, std::string protocol) {
+  if (protocol == "ucci" && name == "Hash")
+      return "hashsize";
+  if (protocol == "usi")
+  {
+      if (name == "Hash" || name == "Ponder" || name == "MultiPV")
+          return "USI_" + name;
+      if (name.substr(0, 4) == "UCI_")
+          name = "USI_" + name.substr(4);
+  }
+  if (protocol == "ucci" || protocol == "usi")
+      std::replace(name.begin(), name.end(), ' ', '_');
+  return name;
+}
+
+bool UCI::is_valid_option(UCI::OptionsMap& options, std::string& name) {
+  std::string protocol = options["Protocol"];
+  for (const auto& it : options)
+  {
+      if (options.key_comp()(option_name(it.first, protocol), name))
+      {
+          name = it.first;
+          return true;
+      }
+  }
+  return false;
+}
+
 } // namespace Stockfish
index 004cd69..ca901c8 100644 (file)
--- a/src/uci.h
+++ b/src/uci.h
@@ -93,6 +93,9 @@ std::string pv(const Position& pos, Depth depth, Value alpha, Value beta);
 std::string wdl(Value v, int ply);
 Move to_move(const Position& pos, std::string& str);
 
+std::string option_name(std::string name, std::string protocol);
+bool is_valid_option(UCI::OptionsMap& options, std::string& name);
+
 } // namespace UCI
 
 extern UCI::OptionsMap Options;
index 8666eac..52b6b7c 100644 (file)
@@ -256,8 +256,7 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
               // UCI dialects do not allow spaces
               if (Options["Protocol"] == "ucci" || Options["Protocol"] == "usi")
               {
-                  string name = it.first;
-                  std::replace(name.begin(), name.end(), ' ', '_');
+                  string name = option_name(it.first, Options["Protocol"]);
                   // UCCI skips "name"
                   os << "\noption " << (Options["Protocol"] == "ucci" ? "" : "name ") << name << " type " << o.type;
               }