Skip to content

Commit

Permalink
unload engines, cmake, messages; tenchou no igo wine example
Browse files Browse the repository at this point in the history
  • Loading branch information
popojan committed Mar 28, 2023
1 parent f8abc75 commit be87213
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 82 deletions.
11 changes: 4 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ function(build_libsgfcplusplus)
GIT_REPOSITORY https://github.com/popojan/libsgfcplusplus.git
GIT_TAG master
CMAKE_ARGS ${CL_ARGS}
BUILD_COMMAND
${CMAKE_COMMAND}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} ${CMAKE_SOURCE_DIR}/deps/libsgfcplusplus -DENABLE_TESTS=0
BUILD_COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} ${CMAKE_SOURCE_DIR}/deps/libsgfcplusplus -DENABLE_TESTS=0
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/deps/libsgfcplusplus --config ${CMAKE_BUILD_TYPE}
SOURCE_DIR ${CMAKE_SOURCE_DIR}/deps/libsgfcplusplus
BINARY_DIR ${CMAKE_BINARY_DIR}/deps/libsgfcplusplus
Expand All @@ -175,7 +173,6 @@ function(build_libsgfcplusplus)
#COMMAND git checkout sgfc
INSTALL_COMMAND echo Skipping install step for libsgfcplusplus
)

ExternalProject_Get_Property(project_libsgfcplusplus SOURCE_DIR)
ExternalProject_Get_Property(project_libsgfcplusplus BINARY_DIR)
set(libsgfcplusplus_inc_dir "${SOURCE_DIR}/include" "${BINARY_DIR}/src" PARENT_SCOPE)
Expand All @@ -200,7 +197,7 @@ function(fetch_json)
include(FetchContent)

FetchContent_Declare(json
GIT_REPOSITORY git://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent.git
GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent.git
GIT_TAG v3.7.3)

FetchContent_GetProperties(json)
Expand All @@ -224,7 +221,7 @@ endfunction()

function(fetch_spdlog)
FetchContent_Declare(spdlog
GIT_REPOSITORY git://github.com/gabime/spdlog.git
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_TAG v1.6.0)

FetchContent_GetProperties(spdlog)
Expand All @@ -236,7 +233,7 @@ endfunction()

function(fetch_clipp)
FetchContent_Declare(clipp
GIT_REPOSITORY git://github.com/muellan/clipp.git
GIT_REPOSITORY https://github.com/muellan/clipp.git
GIT_TAG 2c32b2f1f7cc530b1ec1f62c92f698643bb368db)

FetchContent_GetProperties(clipp)
Expand Down
25 changes: 22 additions & 3 deletions data/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
},
{
"name": "Pachi 12.60",
"path": "./engine/Pachi-12.60",
"command": "pachi.exe",
"path": "./engine/pachi",
"command": "pachi-12.60-amd64",
"parameters": "-r japanese --smart-pass",
"enabled": 0
},
{
"name": "Katago #kata1 b40",
"path": "./engine/katago",
"command": "katago",
"parameters": "gtp -model ./engine/katago/models/kata1-b40c256-s10359230464-d2525387336.bin.gz -config ./engine/katago/default_gtp.cfg",
"parameters": "gtp -model ./engine/katago/kata1-b40c256-s11101799168-d2715431527.bin.gz -config ./engine/katago/default_gtp.cfg",
"enabled": 0,
"kibitz": 0,
"messages": [
Expand All @@ -39,9 +39,28 @@
"output": " "
}
]
},
{
"name": "Zen 6",
"path": "./engine/zen",
"command": "wine",
"parameters": "./engine/zen/gtp4zen.exe -z 6 -t 2 -T 120 -s 2147483647 -r 25",
"enabled": 0,
"kibitz": 0,
"main": 0
},
{
"name": "Zen 7",
"path": "./engine/zen",
"command": "wine",
"parameters": "./engine/zen/gtp4zen.exe -z 7 -t 2 -T 120 -s 2147483647 -r 25",
"enabled": 0,
"kibitz": 0,
"main": 0
}
],
"controls" : [
{"key": 1, "command": "play once"},
{"key": 81, "command": "quit"},
{"key": 17, "command": "toggle_fullscreen"},
{"key": 35, "command": "fps"},
Expand Down
44 changes: 0 additions & 44 deletions engine/README.md

This file was deleted.

8 changes: 1 addition & 7 deletions src/GameThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,7 @@ void GameThread::loadEngines(const std::shared_ptr<Configuration> config) {
int role = Player::SPECTATOR;

if(!command.empty()) {
auto engine = new GtpEngine(command, parameters, path, name);
for(auto &&msg: messages) {
engine->addOutputFilter(
msg.value("regex", ""),
msg.value("output", ""),
msg.value("var", ""));
}
auto engine = new GtpEngine(command, parameters, path, name, messages);
std::size_t id = addEngine(engine);

if (main) {
Expand Down
8 changes: 7 additions & 1 deletion src/GameThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class GameThread
int getActivePlayer(int which);

Move getLocalMove(const Position& coord);
Move getLocalMove(const Move::Special move);
Move getLocalMove(Move::Special move);
void reset();

void addGameObserver(GameObserver* pobserver) {
Expand All @@ -74,6 +74,12 @@ class GameThread
std::vector<Engine*> getEngines() { return engines;}
std::vector<Player*> getPlayers() { return players;}

void unloadEngines() {
for(auto& engine: engines) {
dynamic_cast<GtpEngine*>(engine)->issueCommand("quit");
}
}

private:
std::vector<GameObserver*> gameObservers;
GobanModel& model;
Expand Down
1 change: 1 addition & 0 deletions src/GobanControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ bool GobanControl::command(const std::string& cmd) {
if(cmd == "quit") {
model.game.saveAs("");
exit = true;
engine.unloadEngines();
Shell::RequestExit();
}
else if (cmd == "toggle_fullscreen") {
Expand Down
50 changes: 33 additions & 17 deletions src/gtpclient.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "gtpclient.h"
#include <string>

GtpClient::GtpClient(const std::string& exe, const std::string& cmdline, const std::string& path)
GtpClient::GtpClient(const std::string& exe, const std::string& cmdline,
const std::string& path, const nlohmann::json& messages)
: exe(exe), vars({})
{
spdlog::info("Starting GTP client [{}/{}]", path, exe);

std::vector<boost::filesystem::path> where(::boost::this_process::path());
where.push_back(path);
where.emplace_back(path);

boost::filesystem::path file(boost::process::search_path(exe, where));
spdlog::info("About to run GTP engine [{}]", file.generic_path().string());
Expand All @@ -17,44 +18,59 @@ GtpClient::GtpClient(const std::string& exe, const std::string& cmdline, const s
std::istream_iterator<std::string>());

spdlog::info("running child [{} {}]", file.generic_path().string(), cmdline);
c = boost::process::child(file, params,
boost::process::std_out > pos,
boost::process::std_in < pis,
boost::process::std_err > pes);

reader = new InputThread<GtpClient, boost::process::ipstream>(pes);
reader->bind(*this);
initFilters(messages);

if(outputFilters.empty()) { // do not capture stderr if not needed
c = boost::process::child(file, params,
boost::process::std_out > pos,
boost::process::std_in < pis);
reader = nullptr;
} else {
c = boost::process::child(file, params,
boost::process::std_out > pos,
boost::process::std_in < pis,
boost::process::std_err > pes);
reader = new InputThread<GtpClient, boost::process::ipstream>(pes);
reader->bind(*this);
}
}

void GtpClient::initFilters(const nlohmann::json& messages) {
for(auto &&msg: messages) {
addOutputFilter(
msg.value("regex", ""),
msg.value("output", ""),
msg.value("var", ""));
}
}
void replaceAll(std::string& out, const std::string& what, const std::string& by) {
size_t index(0);
while (true) {
while (index != std::string::npos) {
spdlog::trace("replace [{}] by [{}] in [{}]", what, by, out);
index = out.find(what, index);
if (index == std::string::npos) break;
out.replace(index, what.size(), by);
index += what.size();
if (index != std::string::npos) {
out.replace(index, what.size(), by);
index += what.size();
}
}
}

std::string& ltrim(std::string & str)
{
std::string& ltrim(std::string & str) {
auto it2 = std::find_if(str.begin(), str.end(),
[](char ch){return !std::isspace<char>(ch , std::locale::classic());});
str.erase(str.begin(), it2);
return str;
}

std::string & rtrim(std::string & str)
{
std::string & rtrim(std::string & str) {
auto it1 = std::find_if(str.rbegin(), str.rend(),
[](char ch){ return !std::isspace<char>(ch , std::locale::classic());});
str.erase(it1.base(), str.end());
return str;
}

void GtpClient::operator()(const std::string& line) {
//TODO
spdlog::debug("gtp err = {}", line);
for (auto &re: outputFilters) {
std::smatch m;
Expand Down
5 changes: 4 additions & 1 deletion src/gtpclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ class GtpClient {

public:
typedef std::vector<std::string> CommandOutput;
GtpClient(const std::string& exe, const std::string& cmdline, const std::string& path);
GtpClient(const std::string& exe, const std::string& cmdline,
const std::string& path, const nlohmann::json& messages);

~GtpClient();

void interpolate(std::string& out);

void operator()(const std::string& line);

void initFilters(const nlohmann::json& messages);

void compileFilters();

void addOutputFilter(const std::string& msg, const std::string& format, const std::string& var);
Expand Down
4 changes: 2 additions & 2 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ class GtpEngine : public Engine, public GtpClient {
public:

GtpEngine(const std::string& exe, const std::string& cmdline, const std::string& path = "",
const std::string& nameExtra = "")
: Engine(nameExtra), GtpClient(exe, cmdline, path)
const std::string& nameExtra = "", const nlohmann::json& messages = {})
: Engine(nameExtra), GtpClient(exe, cmdline, path, messages)
{
//setEngineName(nameExtra);
}
Expand Down

0 comments on commit be87213

Please sign in to comment.