try mod loading

This commit is contained in:
shenjack 2023-04-24 23:35:00 +08:00
parent 9183e56a2d
commit 82df6e52e6
3 changed files with 23 additions and 23 deletions

View File

@ -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

View File

@ -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))

View File

@ -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}")