diff --git a/module/submodule/submodule.py b/module/submodule/submodule.py index f24d4ed21..8e88160ab 100644 --- a/module/submodule/submodule.py +++ b/module/submodule/submodule.py @@ -1,6 +1,5 @@ import importlib -from module.config.config import AzurLaneConfig from module.logger import logger from module.submodule.utils import * @@ -15,6 +14,8 @@ def load_mod(name): def load_config(config_name): + from module.config.config import AzurLaneConfig + mod_name = get_config_mod(config_name) if mod_name == 'alas': return AzurLaneConfig(config_name, '') diff --git a/module/webui/app.py b/module/webui/app.py index 2795d05b4..2a7895a00 100644 --- a/module/webui/app.py +++ b/module/webui/app.py @@ -1,12 +1,18 @@ -import argparse +import sys import json -import queue -import threading import time +import queue +import argparse +import threading + from datetime import datetime from functools import partial from typing import Dict, List, Optional +# Import fake module before import pywebio to avoid importing unnecessary module PIL +from module.webui.fake_pil_module import import_fake_pil_module +import_fake_pil_module() + from pywebio import config as webconfig from pywebio.input import file_upload, input, input_group, select from pywebio.output import ( diff --git a/module/webui/fake_pil_module.py b/module/webui/fake_pil_module.py new file mode 100644 index 000000000..ac31239a0 --- /dev/null +++ b/module/webui/fake_pil_module.py @@ -0,0 +1,15 @@ +import sys +from types import ModuleType + + +def import_fake_pil_module(): + fake_pil_module = ModuleType('PIL') + fake_pil_module.Image = ModuleType('PIL.Image') + fake_pil_module.Image.Image = type('MockPILImage', (), dict(__init__=None)) + sys.modules['PIL'] = fake_pil_module + sys.modules['PIL.Image'] = fake_pil_module.Image + + +def remove_fake_pil_module(): + sys.modules.pop('PIL', None) + sys.modules.pop('PIL.Image', None) diff --git a/module/webui/process_manager.py b/module/webui/process_manager.py index 7256e1dea..d1963c2f8 100644 --- a/module/webui/process_manager.py +++ b/module/webui/process_manager.py @@ -1,6 +1,7 @@ -import argparse import os +import sys import queue +import argparse import threading from multiprocessing import Process from typing import Dict, List, Union @@ -9,6 +10,12 @@ import inflection from filelock import FileLock from rich.console import Console, ConsoleRenderable +# Since this file does not run under the same process or subprocess of app.py +# the following code needs to be repeated +# Import fake module before import pywebio to avoid importing unnecessary module PIL +from module.webui.fake_pil_module import * +import_fake_pil_module() + from module.config.utils import filepath_config from module.logger import logger, set_file_logger, set_func_logger from module.submodule.submodule import load_mod @@ -141,6 +148,9 @@ class ProcessManager: from module.config.config import AzurLaneConfig + # Remove fake PIL module, because subprocess will use it + remove_fake_pil_module() + AzurLaneConfig.stop_event = e try: # Run alas diff --git a/submodule/AlasMaaBridge/maa.py b/submodule/AlasMaaBridge/maa.py index b8df787d1..d633cf471 100644 --- a/submodule/AlasMaaBridge/maa.py +++ b/submodule/AlasMaaBridge/maa.py @@ -1,8 +1,29 @@ import os import json - +import ctypes from cached_property import cached_property +# In order for MAA submodule to exexute, +# it is necessary to load runtime before module PIL +if os.name == 'nt': + # This DLL is the dependency for next DLL + # Try loading to avoid mix different versions of runtime + try: + ctypes.WinDLL(os.path.join(os.environ['SystemRoot'], 'System32/vcruntime140_1.dll')) + except Exception as e: + print(e) + + # This DLL must be loaded due to conflict issues + ctypes.WinDLL(os.path.join(os.environ['SystemRoot'], 'System32/msvcp140.dll')) + + # These DLLS are other DLLS that MAA depends on + # Try loading to avoid mix different versions of runtime + try: + ctypes.WinDLL(os.path.join(os.environ['SystemRoot'], 'System32/msvcp140_1.dll')) + ctypes.WinDLL(os.path.join(os.environ['SystemRoot'], 'System32/concrt140.dll')) + except Exception as e: + print(e) + from alas import AzurLaneAutoScript from module.exception import RequestHumanTakeover from module.logger import logger diff --git a/submodule/AlasMaaBridge/module/asst/asst.py b/submodule/AlasMaaBridge/module/asst/asst.py index 69779d9f7..73cf47162 100644 --- a/submodule/AlasMaaBridge/module/asst/asst.py +++ b/submodule/AlasMaaBridge/module/asst/asst.py @@ -50,11 +50,6 @@ class Asst: platform_type = platform.system().lower() if platform_type == 'windows': lib_import_func = ctypes.WinDLL - try: - # Override by System32 dll - lib_import_func(os.path.join(os.environ['SystemRoot'], 'System32/msvcp140.dll')) - except Exception as e: - pass else: lib_import_func = ctypes.CDLL diff --git a/submodule/AlasMaaBridge/module/handler/handler.py b/submodule/AlasMaaBridge/module/handler/handler.py index d5d0f443c..bb48e7c3d 100644 --- a/submodule/AlasMaaBridge/module/handler/handler.py +++ b/submodule/AlasMaaBridge/module/handler/handler.py @@ -31,7 +31,12 @@ class AssistantHandler: AssistantHandler.Asst = asst.Asst AssistantHandler.Message = utils.Message AssistantHandler.InstanceOptionType = utils.InstanceOptionType - AssistantHandler.Asst.load(path, user_dir=path, incremental_path=incremental_path) + try: + AssistantHandler.Asst.load(path, user_dir=path, incremental_path=incremental_path) + except OSError as e: + logger.critical(e) + logger.critical("MAA加载失败,请检查MAA本体能否正常打开") + raise RequestHumanTakeover AssistantHandler.ASST_HANDLER = None