From b01c9abc6f161840caebc3791a7170b4c8bfcf56 Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Mon, 29 Nov 2021 23:26:11 +0100 Subject: [PATCH] Fix option names for UCI dialects Closes #412. --- src/uci.cpp | 31 +++++++++++++++++++++++++++++-- src/uci.h | 3 +++ src/ucioption.cpp | 3 +-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/uci.cpp b/src/uci.cpp index 28e1a57..1903731 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -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 diff --git a/src/uci.h b/src/uci.h index 004cd69..ca901c8 100644 --- 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; diff --git a/src/ucioption.cpp b/src/ucioption.cpp index 8666eac..52b6b7c 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -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; } -- 1.7.0.4