From 82df6e52e6dffe751b8cb345e0726a827e87e661 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Mon, 24 Apr 2023 23:35:00 +0800 Subject: [PATCH] try mod loading --- Difficult_Rocket/__init__.py | 40 ++++++++++++++--------------- Difficult_Rocket/client/__init__.py | 4 +-- Difficult_Rocket/main.py | 2 +- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Difficult_Rocket/__init__.py b/Difficult_Rocket/__init__.py index 6c8aa99..6014ace 100644 --- a/Difficult_Rocket/__init__.py +++ b/Difficult_Rocket/__init__.py @@ -155,26 +155,26 @@ class _DR_runtime(Options): mods = [] paths = Path(self.mod_path).iterdir() sys.path.append(self.mod_path) - # for mod_path in paths: - # try: - # if mod_path.is_dir() and mod_path.name != '__pycache__': # 处理文件夹 mod - # if importlib.util.find_spec(mod_path.name) is not None: - # mods.append(mod_path.name) - # else: - # print(f'can not import mod {mod_path} because importlib can not find spec') - # elif mod_path.suffix in ('.pyz', '.zip'): # 处理压缩包 mod - # if importlib.util.find_spec(mod_path.name) is not None: - # mods.append(mod_path.name) - # elif mod_path.suffix == '.pyd': # pyd 扩展 mod - # if importlib.util.find_spec(mod_path.name) is not None: - # mods.append(mod_path.name) - # elif mod_path.suffix == '.py': # 处理单文件 mod - # print(f'importing mod {mod_path=} {mod_path.stem}') - # if importlib.util.find_spec(mod_path.stem) is not None: - # mods.append(mod_path.stem) - # except ImportError: - # print(f'ImportError when loading mod {mod_path}') - # traceback.print_exc() + for mod_path in paths: + try: + if mod_path.is_dir() and mod_path.name != '__pycache__': # 处理文件夹 mod + if importlib.util.find_spec(mod_path.name) is not None: + mods.append(mod_path.name) + else: + print(f'can not import mod {mod_path} because importlib can not find spec') + elif mod_path.suffix in ('.pyz', '.zip'): # 处理压缩包 mod + if importlib.util.find_spec(mod_path.name) is not None: + mods.append(mod_path.name) + elif mod_path.suffix == '.pyd': # pyd 扩展 mod + if importlib.util.find_spec(mod_path.name) is not None: + mods.append(mod_path.name) + elif mod_path.suffix == '.py': # 处理单文件 mod + print(f'importing mod {mod_path=} {mod_path.stem}') + if importlib.util.find_spec(mod_path.stem) is not None: + mods.append(mod_path.stem) + except ImportError: + print(f'ImportError when loading mod {mod_path}') + traceback.print_exc() return mods diff --git a/Difficult_Rocket/client/__init__.py b/Difficult_Rocket/client/__init__.py index c9a9ab0..a352836 100644 --- a/Difficult_Rocket/client/__init__.py +++ b/Difficult_Rocket/client/__init__.py @@ -195,8 +195,8 @@ class ClientWindow(Window): def setup(self): self.set_icon(pyglet.image.load('./textures/icon.png')) - # self.logger.info(f"=== finding mods from {DR_runtime.mod_path} ===") - # self.logger.info(f'find mods: {DR_runtime.find_mods()}') + self.logger.info(f"=== finding mods from {DR_runtime.mod_path} ===") + self.logger.info(f'find mods: {DR_runtime.find_mods()}') self.load_fonts() # TODO 读取配置文件,加载不同的屏幕,解耦 self.screen_list.append(DRDEBUGScreen(self)) diff --git a/Difficult_Rocket/main.py b/Difficult_Rocket/main.py index d1f3e5a..8d30e65 100644 --- a/Difficult_Rocket/main.py +++ b/Difficult_Rocket/main.py @@ -110,7 +110,7 @@ class Game: def setup(self) -> None: self.client = client.Client(net_mode='local') self.server = server.Server(net_mode='local') - # self.load_mods() + self.load_mods() def python_version_check(self) -> None: # best 3.8+ and write at 3.8.10 self.logger.info(f"{tr().main.version.now_on()} {self.on_python_v}")