fixes, windows pgmptr fix

This commit is contained in:
jusax23 2023-11-06 20:13:11 +01:00
parent 686a2ab92c
commit 600900468d
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
7 changed files with 60 additions and 15 deletions

View file

@ -6,12 +6,13 @@ cmake_minimum_required(VERSION 3.18)
project(flutter_stockfish_plugin VERSION 0.0.1 LANGUAGES CXX) project(flutter_stockfish_plugin VERSION 0.0.1 LANGUAGES CXX)
file(GLOB_RECURSE cppPaths "Stockfish/src/*.cpp") file(GLOB_RECURSE cppPaths "Stockfish/src/*.cpp")
set(CMAKE_CXX_STANDARD 17) 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) set(NNUE_NAME nn-5af11540bbfe.nnue)
add_library(flutter_stockfish_plugin SHARED add_library(flutter_stockfish_plugin SHARED
"stockfish.cpp" "stockfish.cpp"
"stream_fix.cpp" "stream_fix.cpp"
"small_fixes.cpp"
${cppPaths} ${cppPaths}
) )
@ -21,9 +22,9 @@ set_target_properties(flutter_stockfish_plugin PROPERTIES
) )
if(MSVC) if(MSVC)
add_definitions(/FI"stream_fix.h") add_definitions(/FI"fixes.h")
else() else()
add_definitions(-include stream_fix.h) add_definitions(-include fixes.h)
endif() endif()
target_compile_definitions(flutter_stockfish_plugin PUBLIC DART_SHARED_LIB) target_compile_definitions(flutter_stockfish_plugin PUBLIC DART_SHARED_LIB)

View file

@ -75,8 +75,8 @@ namespace {
/// Version number or dev. /// Version number or dev.
constexpr string_view version = "16"; constexpr string_view version = "16";
/// Our fancy logging facility. The trick here is to replace cin.rdbuf() and /// Our fancy logging facility. The trick here is to replace fakein.rdbuf() and
/// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We /// 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 /// 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! /// usual I/O functionality, all without changing a single line of code!
/// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81 /// 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 { 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(""); } ~Logger() { start(""); }
ofstream file; ofstream file;
@ -118,8 +118,8 @@ public:
if (l.file.is_open()) if (l.file.is_open())
{ {
cout.rdbuf(l.out.buf); fakeout.rdbuf(l.out.buf);
cin.rdbuf(l.in.buf); fakein.rdbuf(l.in.buf);
l.file.close(); l.file.close();
} }
@ -133,8 +133,8 @@ public:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
cin.rdbuf(&l.in); fakein.rdbuf(&l.in);
cout.rdbuf(&l.out); fakeout.rdbuf(&l.out);
} }
} }
}; };
@ -751,10 +751,10 @@ void init([[maybe_unused]] int argc, char* argv[]) {
#ifdef _WIN32 #ifdef _WIN32
pathSeparator = "\\"; pathSeparator = "\\";
#ifdef _MSC_VER #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. // issues in some windows 10 versions, so check returned values carefully.
char* pgmptr = nullptr; char* pgmptr = nullptr;
if (!_get_pgmptr(&pgmptr) && pgmptr != nullptr && *pgmptr) if (!fake_get_pgmptr(&pgmptr) && pgmptr != nullptr && *pgmptr)
argv0 = pgmptr; argv0 = pgmptr;
#endif #endif
#else #else

View file

@ -12,6 +12,9 @@ find ./Stockfish/ -type f -exec sed -i \
-e 's/std::cin/fakein/g' \ -e 's/std::cin/fakein/g' \
-e 's/std::endl/fakeendl/g' \ -e 's/std::endl/fakeendl/g' \
-e 's/getline(cin, cmd)/getline(fakein, cmd)/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 \ nnue_name=$(grep EvalFileDefaultName Stockfish/src/evaluate.h \

2
src/fixes.h Normal file
View file

@ -0,0 +1,2 @@
#include "stream_fix.h"
#include "small_fixes.h"

26
src/small_fixes.cpp Normal file
View file

@ -0,0 +1,26 @@
#include "small_fixes.h"
#include <cstring>
#include <iostream>
#include <string>
#ifdef _WIN32
#include <windows.h>
#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

14
src/small_fixes.h Normal file
View file

@ -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

View file

@ -3,9 +3,6 @@
#ifdef _WIN32 #ifdef _WIN32
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#ifdef _WIN64 #ifdef _WIN64
#define ssize_t __int64 #define ssize_t __int64
#else #else
@ -19,6 +16,8 @@
#include "stockfish.h" #include "stockfish.h"
#include "fixes.h"
const char *QUITOK = "quitok\n"; const char *QUITOK = "quitok\n";
int main(int, char **); int main(int, char **);