diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 13a498d..b9b7ac9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,12 +6,13 @@ 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} -O3 -flto -DNDEBUG -fPIC") set(NNUE_NAME nn-5af11540bbfe.nnue) add_library(flutter_stockfish_plugin SHARED "stockfish.cpp" "stream_fix.cpp" + "small_fixes.cpp" ${cppPaths} ) @@ -21,9 +22,9 @@ set_target_properties(flutter_stockfish_plugin PROPERTIES ) if(MSVC) - add_definitions(/FI"stream_fix.h") + add_definitions(/FI"fixes.h") else() - add_definitions(-include stream_fix.h) + add_definitions(-include fixes.h) endif() target_compile_definitions(flutter_stockfish_plugin PUBLIC DART_SHARED_LIB) diff --git a/src/Stockfish/src/misc.cpp b/src/Stockfish/src/misc.cpp index 7073853..3584377 100644 --- a/src/Stockfish/src/misc.cpp +++ b/src/Stockfish/src/misc.cpp @@ -75,8 +75,8 @@ namespace { /// Version number or dev. constexpr string_view version = "16"; -/// Our fancy logging facility. The trick here is to replace cin.rdbuf() and -/// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We +/// Our fancy logging facility. The trick here is to replace fakein.rdbuf() and +/// fakeout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We /// can toggle the logging of fakeout and std:cin at runtime whilst preserving /// usual I/O functionality, all without changing a single line of code! /// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81 @@ -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,8 +118,8 @@ public: if (l.file.is_open()) { - cout.rdbuf(l.out.buf); - cin.rdbuf(l.in.buf); + fakeout.rdbuf(l.out.buf); + fakein.rdbuf(l.in.buf); l.file.close(); } @@ -133,8 +133,8 @@ public: exit(EXIT_FAILURE); } - cin.rdbuf(&l.in); - cout.rdbuf(&l.out); + fakein.rdbuf(&l.in); + fakeout.rdbuf(&l.out); } } }; @@ -751,10 +751,10 @@ void init([[maybe_unused]] int argc, char* argv[]) { #ifdef _WIN32 pathSeparator = "\\"; #ifdef _MSC_VER - // Under windows argv[0] may not have the extension. Also _get_pgmptr() had + // Under windows argv[0] may not have the extension. Also fake_get_pgmptr() had // issues in some windows 10 versions, so check returned values carefully. char* pgmptr = nullptr; - if (!_get_pgmptr(&pgmptr) && pgmptr != nullptr && *pgmptr) + if (!fake_get_pgmptr(&pgmptr) && pgmptr != nullptr && *pgmptr) argv0 = pgmptr; #endif #else diff --git a/src/create_stockfish.sh b/src/create_stockfish.sh index 3ccc0ad..a389377 100644 --- a/src/create_stockfish.sh +++ b/src/create_stockfish.sh @@ -12,6 +12,9 @@ find ./Stockfish/ -type f -exec sed -i \ -e 's/std::cin/fakein/g' \ -e 's/std::endl/fakeendl/g' \ -e 's/getline(cin, cmd)/getline(fakein, cmd)/g' \ + -e 's/cin.rdbuf/fakein.rdbuf/g' \ + -e 's/cout.rdbuf/fakeout.rdbuf/g' \ + -e 's/_get_pgmptr/fake_get_pgmptr/g' \ {} + nnue_name=$(grep EvalFileDefaultName Stockfish/src/evaluate.h \ diff --git a/src/fixes.h b/src/fixes.h new file mode 100644 index 0000000..4008e36 --- /dev/null +++ b/src/fixes.h @@ -0,0 +1,2 @@ +#include "stream_fix.h" +#include "small_fixes.h" \ No newline at end of file diff --git a/src/small_fixes.cpp b/src/small_fixes.cpp new file mode 100644 index 0000000..7a38a29 --- /dev/null +++ b/src/small_fixes.cpp @@ -0,0 +1,26 @@ +#include "small_fixes.h" + +#include +#include +#include + +#ifdef _WIN32 +#include +#endif + +#ifdef _WIN32 +#ifdef _MSC_VER + + +bool fake_get_pgmptr(char** ptr) { + wchar_t buffer[MAX_PATH]; + if (GetModuleFileNameW(nullptr, buffer, MAX_PATH) != 0) { + char* narrowBuffer = new char[MAX_PATH]; + WideCharToMultiByte(CP_UTF8, 0, buffer, -1, narrowBuffer, MAX_PATH, + NULL, NULL); + *ptr = narrowBuffer; + } + return false; +} +#endif +#endif \ No newline at end of file diff --git a/src/small_fixes.h b/src/small_fixes.h new file mode 100644 index 0000000..65c4d90 --- /dev/null +++ b/src/small_fixes.h @@ -0,0 +1,14 @@ +#ifndef _SMALL_FIXES_H_ +#define _SMALL_FIXES_H_ + +#ifdef _WIN32 +#ifdef _MSC_VER + +// Expects a pointer to a char pointer. +// Overwrites *ptr with a new allocated memory. +// Memory managment is left to the user of this function. +bool fake_get_pgmptr(char** ptr); +#endif +#endif + +#endif \ No newline at end of file diff --git a/src/stockfish.cpp b/src/stockfish.cpp index 44d9ea4..cdfda3f 100644 --- a/src/stockfish.cpp +++ b/src/stockfish.cpp @@ -3,9 +3,6 @@ #ifdef _WIN32 #include #include -#define STDIN_FILENO 0 -#define STDOUT_FILENO 1 -#define STDERR_FILENO 2 #ifdef _WIN64 #define ssize_t __int64 #else @@ -19,6 +16,8 @@ #include "stockfish.h" +#include "fixes.h" + const char *QUITOK = "quitok\n"; int main(int, char **);