diff --git a/.gitignore b/.gitignore index 0551768..3d1d2ee 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ src/-lstdc++.res # Neural network for the NNUE evaluation **/*.nnue +/.vs \ No newline at end of file diff --git a/lib/stockfish.dart b/lib/stockfish.dart index eba6abb..8722abf 100644 --- a/lib/stockfish.dart +++ b/lib/stockfish.dart @@ -73,6 +73,7 @@ class Stockfish { } }, onError: (error) { + print('The init isolate encountered an error $error'); developer.log('The init isolate encountered an error $error', name: 'Stockfish'); _cleanUp(1); @@ -108,6 +109,8 @@ class Stockfish { throw StateError('Stockfish is not ready ($stateValue)'); } + print("sending: " + line); + final unicodePointer = '$line\n'.toNativeUtf8(); final pointer = unicodePointer.cast(); _bindings.stockfish_stdin_write(pointer); @@ -210,6 +213,7 @@ Future _spawnIsolates(List mainAndStdout) async { try { await Isolate.spawn(_isolateStdout, mainAndStdout[1]); } catch (error) { + print('Failed to spawn stdout isolate: $error'); developer.log('Failed to spawn stdout isolate: $error', name: 'Stockfish'); return false; } @@ -217,6 +221,7 @@ Future _spawnIsolates(List mainAndStdout) async { try { await Isolate.spawn(_isolateMain, mainAndStdout[0]); } catch (error) { + print('Failed to spawn stdout isolate: $error'); developer.log('Failed to spawn main isolate: $error', name: 'Stockfish'); return false; } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 13a498d..1b209be 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.18) project(flutter_stockfish_plugin VERSION 0.0.1 LANGUAGES CXX) file(GLOB_RECURSE cppPaths "Stockfish/src/*.cpp") set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -flto") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -O3 -flto") #-DNDEBUG -fno-exceptions -funroll-loops set(NNUE_NAME nn-5af11540bbfe.nnue) add_library(flutter_stockfish_plugin SHARED diff --git a/src/Stockfish/src/misc.cpp b/src/Stockfish/src/misc.cpp index 7073853..d31bb71 100644 --- a/src/Stockfish/src/misc.cpp +++ b/src/Stockfish/src/misc.cpp @@ -105,7 +105,7 @@ struct Tie: public streambuf { // MSVC requires split streambuf for cin and cout class Logger { - Logger() : in(cin.rdbuf(), file.rdbuf()), out(cout.rdbuf(), file.rdbuf()) {} + Logger() : in(fakein.rdbuf(), file.rdbuf()), out(fakeout.rdbuf(), file.rdbuf()) {} ~Logger() { start(""); } ofstream file; @@ -118,9 +118,9 @@ public: if (l.file.is_open()) { - cout.rdbuf(l.out.buf); - cin.rdbuf(l.in.buf); - l.file.close(); + fakeout.rdbuf(l.out.buf); + fakein.rdbuf(l.in.buf); + l.file.close(); } if (!fname.empty()) @@ -133,8 +133,8 @@ public: exit(EXIT_FAILURE); } - cin.rdbuf(&l.in); - cout.rdbuf(&l.out); + fakein.rdbuf(&l.in); + fakeout.rdbuf(&l.out); } } }; @@ -753,10 +753,14 @@ void init([[maybe_unused]] int argc, char* argv[]) { #ifdef _MSC_VER // Under windows argv[0] may not have the extension. Also _get_pgmptr() had // issues in some windows 10 versions, so check returned values carefully. - char* pgmptr = nullptr; - if (!_get_pgmptr(&pgmptr) && pgmptr != nullptr && *pgmptr) - argv0 = pgmptr; - #endif + wchar_t buffer[MAX_PATH]; + if (GetModuleFileNameW(nullptr, buffer, MAX_PATH) != 0) + { + char narrowBuffer[MAX_PATH]; + WideCharToMultiByte(CP_UTF8, 0, buffer, -1, narrowBuffer, MAX_PATH, NULL, NULL); + argv0 = narrowBuffer; + } +#endif #else pathSeparator = "/"; #endif diff --git a/src/Stockfish/src/misc.h b/src/Stockfish/src/misc.h index 2119be8..a73a587 100644 --- a/src/Stockfish/src/misc.h +++ b/src/Stockfish/src/misc.h @@ -70,7 +70,6 @@ std::ostream& operator<<(std::ostream&, SyncCout); #define sync_cout fakeout << IO_LOCK #define sync_endl fakeendl << IO_UNLOCK - // align_ptr_up() : get the first aligned element of an array. // ptr must point to an array of size at least `sizeof(T) * N + alignment` bytes, // where N is the number of elements in the array. diff --git a/src/stockfish.cpp b/src/stockfish.cpp index 44d9ea4..e35edbb 100644 --- a/src/stockfish.cpp +++ b/src/stockfish.cpp @@ -33,7 +33,7 @@ int stockfish_main() { char *argv[] = {(char *)""}; int exitCode = main(argc, argv); - fakeout << QUITOK << "\n"; + fakeout << exitCode << "\n"; #if _WIN32 Sleep(100); @@ -49,6 +49,7 @@ int stockfish_main() { ssize_t stockfish_stdin_write(char *data) { std::string val(data); + fakeout << val << fakeendl; fakein << val << fakeendl; return val.length(); } diff --git a/src/stream_fix.cpp b/src/stream_fix.cpp index f7a3806..baaf535 100644 --- a/src/stream_fix.cpp +++ b/src/stream_fix.cpp @@ -1,6 +1,7 @@ #include "stream_fix.h" -bool FakeStream::try_get_line(std::string& val) { +bool FakeStream::try_get_line(std::string &val) +{ std::unique_lock lock(mutex_guard); if (string_queue.empty() || closed) return false; val = string_queue.front(); @@ -26,6 +27,17 @@ bool std::getline(FakeStream& is, std::string& str) { return true; } +/*FakeStream &operator<<(FakeStream &os, SyncFakeout sc) { + static std::mutex m; + if (sc == SyncFakeout::IO_LOCK) + m.lock(); + + if (sc == SyncFakeout::IO_UNLOCK) + m.unlock(); + + return os; +}*/ + FakeStream fakeout; FakeStream fakein; std::string fakeendl("\n"); \ No newline at end of file diff --git a/src/stream_fix.h b/src/stream_fix.h index 8044f36..4db7ada 100644 --- a/src/stream_fix.h +++ b/src/stream_fix.h @@ -8,7 +8,7 @@ template inline std::string stringify(const T& input) { - std::ostringstream output; // from www .ja va 2s . com + std::ostringstream output; output << input; return output.str(); } @@ -55,6 +55,9 @@ namespace std { bool getline(FakeStream& is, std::string& str); } // namespace std +//enum SyncFakeout { IO_LOCK, IO_UNLOCK }; +//FakeStream &operator<<(FakeStream &os, SyncFakeout sc); + // #define endl fakeendl // #define cout fakeout // #define cin fakein