lukasmonk / lucaschessR2

Lucas Chess R Version 2
GNU General Public License v3.0
255 stars 36 forks source link

[feature] add uci engine as bat file #56

Closed Xyrio closed 1 year ago

Xyrio commented 1 year ago

when i try to ad a bat file that calls the engine, after a few seconds it freezes and crashes when i click on "One moment please..." ui.

not working:

i use the command option: c:\windows\system32\cmd.exe /C c:\stockfish.bat

content of stockfish.bat: <absolute path to stockfish 15.1 exe>

running bat from console works fine all paths are absolute

running lucasR2 as admin didnt help.

i also tried making an exe file from a py file then using that exe directly and also as command. the exe calls a bat file but same freeze and crash

i tried the Engine.py from #45 but no difference

working:

adding stockfish directly as exe works.

command using python.exe with a py script as parameter works

### spec: using windows 7 64bit
lukasmonk commented 1 year ago

This is not supported and will not be supported in the future.

Xyrio commented 1 year ago

is it a technical limitation? i might try to program it myself.

well here is an example of how i wrapped a bat calling an engine file from python passing stdin, stdout, stderr. just in case i dont find the time to do it:

bat file wrapper: runsubproc.py


import sys
from subprocess import Popen, PIPE
import time
import threading

#https://realpython.com/intro-to-python-threading/
#https://docs.python.org/3.8/library/threading.html
#https://docs.python.org/3.8/library/subprocess.html

#https://pypi.org/project/auto-py-to-exe/
#https://codeigo.com/python/convert-py-to-exe

debug = False

p = Popen( 'subproc.bat', stdout=PIPE, stdin=PIPE, stderr=PIPE, text=True)

def handle_exit():
    while True:
        time.sleep(1)
        rc = p.poll() #sets/returns returncode otherwise None
        #if debug: print("*** rc",rc)
        if rc != None:
            break #exit() not working when exe

def handle_stdin():
    while True:
        data = input()
        rc = p.poll() #sets/returns returncode otherwise None
        if rc != None:
            break
        if debug: print("*** stdin passing")
        p.stdin.write(data)
        p.stdin.write("\n")
        p.stdin.flush()

def handle_stdout():
    while True:
        data = p.stdout.readline()
        if len(data) == 0:
            break
        if debug: print("*** stdout passing")
        print(data, end="")

def handle_stderr():
    while True:
        data = p.stderr.readline()
        if len(data) == 0:
            break
        if debug: print("*** stderr passing")
        print(data, end="", file=sys.stderr)

tse = threading.Thread(target=handle_stderr, name="TSTDERR", daemon=True)
tse.start()

tso = threading.Thread(target=handle_stdout, name="TSTDOUT", daemon=True)
tso.start()

tsi = threading.Thread(target=handle_stdin, name="TSTDIN", daemon=True)
tsi.start()

handle_exit()

py to exe

pyinstaller --noconfirm --onefile --console "runsubproc.py"
copy /Y dist\runsubproc.exe runsubproc.exe
Xyrio commented 1 year ago
  File "...\bin\Code\Engines\EngineRunDirect.py", line 55, in __init__
    self.process = subprocess.Popen(xargs, stdout=subprocess.PIPE, stdin=subprocess.PIPE, startupinfo=startupinfo)
  File "D:\obj\windows-release\37win32_Release\msi_python\zip_win32\subprocess.py", line 753, in __init__
  File "D:\obj\windows-release\37win32_Release\msi_python\zip_win32\subprocess.py", line 1107, in _get_handles
  File "D:\obj\windows-release\37win32_Release\msi_python\zip_win32\subprocess.py", line 1119, in _make_inheritable
OSError: [WinError 6] The handle is invalid

no clue why it does not want to work with either cmd.exe (64bit) C:\Windows\System32\cmd.exe (32bit) C:\Windows\SysWOW64\cmd.exe (yea the folder with 32 in its name has 64bit programs and the one with 64 has 32bit programs. windows logic)

trying to use the non exe source but have not figured out how to get this FasterCode working:

Traceback (most recent call last):
  File "LucasR.py", line 17, in <module>
    import Code.Base.Init
  File "...\bin\Code\Base\Init.py", line 5, in <module>
    from Code import Procesador
  File "...\bin\Code\Procesador.py", line 10, in <module>
    from Code import ManagerEntPos
  File "...\.ve_3_7\lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "...\bin\Code\ManagerEntPos.py", line 5, in <module>
    from Code import FNSLine
  File "...\.ve_3_7\lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "...\bin\Code\FNSLine.py", line 1, in <module>
    from Code.Base import Game, Position
  File "...\.ve_3_7\lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "...\bin\Code\Base\Game.py", line 1, in <module>
    import FasterCode
  File "...\.ve_3_7\lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'FasterCode'
Xyrio commented 1 year ago

in 2.05 same freeze happens because the exception is not caught. so the message box with The file %1 does not correspond to a UCI engine type. from ...\bin\Code\Engines\WConfEngines.py does not appear

lukasmonk commented 1 year ago

FasterCode: .\bin_fastercode\ in source code.

Xyrio commented 1 year ago

i am not too familiar with python. read a bit about pyd stuff. as far as i read it is supposed to be like a DLL for python.

the FasterCode.cp37-win32.pyd already exists

i set the PYTHONPATH env var to the folder with FasterCode.cp37-win32.pyd but didnt do anything. still same error as above: ModuleNotFoundError: No module named 'FasterCode'

compiling c code myself is too much of a hassle.

used a copy renamed to FasterCode.pyd and the dlls from bin\OS\win32\DigitalBoards\ copied all to bin. but then it gives me this error:

(.ve_3_7) S:\CODE\Projects\_game\_chess\lucaschessR2_py\bin>python LucasR.py
Traceback (most recent call last):
  File "LucasR.py", line 17, in <module>
    import Code.Base.Init
  File "S:\CODE\Projects\_game\_chess\lucaschessR2_py\bin\Code\Base\Init.py", line 5, in <module>
    from Code import Procesador
  File "S:\CODE\Projects\_game\_chess\lucaschessR2_py\bin\Code\Procesador.py", line 10, in <module>
    from Code import ManagerEntPos
  File "S:\CODE\Projects\_game\_chess\lucaschessR2_py\.ve_3_7\lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "S:\CODE\Projects\_game\_chess\lucaschessR2_py\bin\Code\ManagerEntPos.py", line 5, in <module>
    from Code import FNSLine
  File "S:\CODE\Projects\_game\_chess\lucaschessR2_py\.ve_3_7\lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "S:\CODE\Projects\_game\_chess\lucaschessR2_py\bin\Code\FNSLine.py", line 1, in <module>
    from Code.Base import Game, Position
  File "S:\CODE\Projects\_game\_chess\lucaschessR2_py\.ve_3_7\lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "S:\CODE\Projects\_game\_chess\lucaschessR2_py\bin\Code\Base\Game.py", line 1, in <module>
    import FasterCode
  File "S:\CODE\Projects\_game\_chess\lucaschessR2_py\.ve_3_7\lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
ImportError: DLL load failed: %1 ist keine zulässige Win32-Anwendung.

also added all the dll/pyd stuff from 2.05 release to my source bin. but then it is this error.

(.ve_3_7) S:\CODE\Projects\_game\_chess\lucaschessR2_py\bin>python LucasR.py
Traceback (most recent call last):
  File "LucasR.py", line 17, in <module>
    import Code.Base.Init
  File "S:\CODE\Projects\_game\_chess\lucaschessR2_py\bin\Code\__init__.py", line 2, in <module>
    import ssl
  File "C:\Progz\_programming\python\WPy64-3760\python-3.7.6.amd64\lib\ssl.py", line 98, in <module>
    import _ssl             # if we can't import it, let the error propagate
ImportError: DLL load failed: %1 ist keine zulässige Win32-Anwendung.

if there no way to run in more pure python mode that doesnt require all the extras then i guess it is not ment to be run by me :)

ideally one has a script to setup stuff and then it should run. like pip install -r requirements.txt or some other script.

anyway i tested with 2.04 and replacing pyc files with the py equivalent. but running bat, cmd.exe or exe converted from py files always gives exception above: OSError: [WinError 6] The handle is invalid

might be a python thing i dont know.

so for now i give up.

using command with python.exe and a custom python script is close enough to what i need. (for running toy python chess engine)

since you dont intend to add this feature i close the issue.

Xyrio commented 1 year ago

you could fix the freezing of the gui, if you catch the OSError exception (see trace above)

then instead the message popup is shown from WConfEngines.py > WConfExternals ... QTUtil2.message_bold(self, X(("The file %1 does not correspond to a UCI engine type."), command))

although that might be even more misleading when one inputs something wrong (#45). but at least it doesnt not freeze/crash.

Xyrio commented 1 year ago

ImportError: DLL load failed: %1 is not a valid Win32 application

so it seems to be because of a mismatch of python 64bit with 32bit dlls i copied over. is there a manual somehwere how to get everything necessary to run?

Xyrio commented 1 year ago

ok confirmed 32bit necessary (setup because needs pip) https://www.python.org/downloads/release/python-379/ (just exe extracting) https://sourceforge.net/projects/winpython/files/WinPython_3.7/3.7.7.1/