mirror of
https://github.com/LmeSzinc/AzurLaneAutoScript.git
synced 2025-01-05 11:16:44 +08:00
Add: fake module PIL for webui (#4374)
* Add: fake module PIL for webui * Fix: Load runtime before module PIL * Upd: Error message of MAA loading failure * Upd: Adjust timing of import fake module
This commit is contained in:
parent
bc5e6ed86a
commit
bd680eb3a8
@ -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, '')
|
||||
|
@ -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 (
|
||||
|
15
module/webui/fake_pil_module.py
Normal file
15
module/webui/fake_pil_module.py
Normal file
@ -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)
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user