From 4920c19b80c44f76cf268e9e06a019c711499b2d Mon Sep 17 00:00:00 2001 From: latorc <65785354+latorc@users.noreply.github.com> Date: Mon, 15 Apr 2024 21:19:10 +0800 Subject: [PATCH] libriichi3p --- .gitignore | 3 ++- bot/bot_local.py | 18 ++++++++++++++---- libriichi3p/__init__.py | 5 +++-- scripts/generate_exe.bat | 2 +- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index db5ddbb..2b8c0c2 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ test/ /dev_docs /_*.py /settings*.json - +libriichi3p/*.pyd +libriichi3p/*.so diff --git a/bot/bot_local.py b/bot/bot_local.py index cd03611..0f5fce4 100644 --- a/bot/bot_local.py +++ b/bot/bot_local.py @@ -4,7 +4,6 @@ import threading import json from mjai.engine import get_engine -from mjai.engine3p import get_engine as get_engine_3p from common.utils import ModelFileException from common.mj_helper import MJAI_TYPE from common.log_helper import LOGGER @@ -12,7 +11,6 @@ import libriichi except: # pylint: disable=bare-except import riichi as libriichi -import libriichi3p from .bot import Bot, BotType, GameMode @@ -30,6 +28,13 @@ def __init__(self, model_files:dict[GameMode, str]) -> None: if not Path(v).exists() or not Path(v).is_file(): LOGGER.warning("Cannot find model file for mode %s:%s", k,v) self._supported_modes.remove(k) + if k == GameMode.MJ3P: + # test import libraries for 3p + try: + import libriichi3p + except Exception as e: # pylint: disable=bare-except + LOGGER.warning("Cannot import libriichi3p: %s", e) + self._supported_modes.remove(k) if not self._supported_modes: raise ModelFileException("No valid model files found") @@ -49,8 +54,13 @@ def _init_bot_impl(self, mode:GameMode=GameMode.MJ4P): engine = get_engine(self.model_files[mode]) self.mjai_bot = libriichi.mjai.Bot(engine, self.seat) elif mode == GameMode.MJ3P: - engine = get_engine_3p(self.model_files[mode]) - self.mjai_bot = libriichi3p.mjai.Bot(engine, self.seat) + try: + import libriichi3p + from mjai.engine3p import get_engine as get_engine_3p + engine = get_engine_3p(self.model_files[mode]) + self.mjai_bot = libriichi3p.mjai.Bot(engine, self.seat) + except Exception as e: + raise e else: raise NotImplementedError(f"Mode {mode} not supported") self.str_input_history.clear() diff --git a/libriichi3p/__init__.py b/libriichi3p/__init__.py index 484d9ca..a4d9249 100644 --- a/libriichi3p/__init__.py +++ b/libriichi3p/__init__.py @@ -3,6 +3,7 @@ import os import platform import importlib.util +from common.utils import sub_file assert sys.version_info >= (3, 10), "Python version must be 3.10 or higher" assert sys.version_info <= (3, 12), "Python version must be 3.12 or lower" @@ -26,9 +27,8 @@ def load_module(): raise EnvironmentError(f"Unsupported OS: {platform.system()}") # Adjust the path to the directory where the .pyd file is stored - dir_path = os.path.dirname(__file__) # Get the directory of the current file filename = f"libriichi3p-{python_version}-{proc_str}-{os_ext_str}" - file_path = os.path.join(dir_path, filename) # Combine with the filename + file_path = sub_file("libriichi3p", filename) if not os.path.exists(file_path): raise ImportError(f"Could not find file: {file_path}") @@ -42,3 +42,4 @@ def load_module(): raise ImportError(f"Could not import: {file_path}") libriichi3p = load_module() + diff --git a/scripts/generate_exe.bat b/scripts/generate_exe.bat index 7ae944f..47f41b6 100644 --- a/scripts/generate_exe.bat +++ b/scripts/generate_exe.bat @@ -14,6 +14,6 @@ REM robocopy . .\dist\MahjongCopilot settings.json REM robocopy models dist\MahjongCopilot\models /E robocopy resources dist\MahjongCopilot\resources /E robocopy liqi_proto dist\MahjongCopilot\liqi_proto /E -robocopy .\libriichi3p\ dist\MahjongCopilot\_internal\libriichi3p\ *.pyd *.so /E +REM robocopy .\libriichi3p\ dist\MahjongCopilot\_internal\libriichi3p\ *.pyd *.so /E robocopy .venv\Lib\site-packages\playwright\driver\package\.local-browsers dist\MahjongCopilot\_internal\playwright\driver\package\.local-browsers /E explorer.exe dist \ No newline at end of file