2023-06-17 00:47:00 +08:00
|
|
|
# -------------------------------
|
|
|
|
# Difficult Rocket
|
|
|
|
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
|
|
|
# All rights reserved
|
|
|
|
# -------------------------------
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import importlib
|
|
|
|
import traceback
|
|
|
|
import contextlib
|
|
|
|
import importlib.util
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Optional, List, Tuple
|
|
|
|
|
|
|
|
from Difficult_Rocket.api.types import Options, Version
|
|
|
|
|
2023-12-03 16:54:07 +08:00
|
|
|
__all__ = ["DR_runtime"]
|
2023-06-17 00:47:00 +08:00
|
|
|
|
|
|
|
|
2023-12-16 22:24:44 +08:00
|
|
|
class _DRRuntime(Options):
|
2023-06-17 00:47:00 +08:00
|
|
|
"""
|
|
|
|
DR 的运行时配置 / 状态
|
|
|
|
"""
|
|
|
|
|
2023-12-03 16:54:07 +08:00
|
|
|
name = "DR Runtime"
|
|
|
|
|
2023-12-16 22:24:44 +08:00
|
|
|
running: bool = False
|
2023-12-03 16:54:07 +08:00
|
|
|
language: str = "zh-CN"
|
|
|
|
mod_path: str = "./mods"
|
2023-06-17 00:47:00 +08:00
|
|
|
DR_Mod_List: List[Tuple[str, Version]] = [] # DR Mod 列表 (name, version)
|
2023-12-16 22:24:44 +08:00
|
|
|
main_config: dict = {} # main.toml
|
2023-06-17 00:47:00 +08:00
|
|
|
|
|
|
|
# run status
|
|
|
|
start_time_ns: Optional[int] = None
|
|
|
|
client_setup_cause_ns: Optional[int] = None
|
|
|
|
server_setup_cause_ns: Optional[int] = None
|
|
|
|
|
|
|
|
def load_file(self) -> bool:
|
|
|
|
with contextlib.suppress(FileNotFoundError):
|
2023-12-03 16:54:07 +08:00
|
|
|
with open("./config/main.toml", "r", encoding="utf-8") as f:
|
2023-06-17 00:47:00 +08:00
|
|
|
import rtoml
|
2023-12-03 16:54:07 +08:00
|
|
|
|
2023-06-17 00:47:00 +08:00
|
|
|
config_file = rtoml.load(f)
|
2023-12-03 16:54:07 +08:00
|
|
|
self.language = config_file["runtime"]["language"]
|
|
|
|
self.mod_path = config_file["game"]["mods"]["path"]
|
2023-12-16 22:24:44 +08:00
|
|
|
self.main_config = config_file
|
2023-06-17 00:47:00 +08:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def find_mods(self) -> List[str]:
|
|
|
|
mods = []
|
|
|
|
mod_path = Path(self.mod_path)
|
|
|
|
if not mod_path.exists():
|
|
|
|
mod_path.mkdir()
|
|
|
|
return []
|
|
|
|
paths = mod_path.iterdir()
|
|
|
|
sys.path.append(self.mod_path)
|
|
|
|
for mod_path in paths:
|
|
|
|
try:
|
2023-12-03 16:54:07 +08:00
|
|
|
if mod_path.is_dir() and mod_path.name != "__pycache__": # 处理文件夹 mod
|
2023-06-17 00:47:00 +08:00
|
|
|
if importlib.util.find_spec(mod_path.name) is not None:
|
|
|
|
mods.append(mod_path.name)
|
|
|
|
else:
|
2023-12-03 16:54:07 +08:00
|
|
|
print(
|
|
|
|
f"can not import mod {mod_path} because importlib can not find spec"
|
|
|
|
)
|
|
|
|
elif mod_path.suffix in (".pyz", ".zip"): # 处理压缩包 mod
|
2023-06-17 00:47:00 +08:00
|
|
|
if importlib.util.find_spec(mod_path.name) is not None:
|
|
|
|
mods.append(mod_path.name)
|
2023-12-03 16:54:07 +08:00
|
|
|
elif mod_path.suffix == ".pyd": # pyd 扩展 mod
|
2023-06-17 00:47:00 +08:00
|
|
|
if importlib.util.find_spec(mod_path.name) is not None:
|
|
|
|
mods.append(mod_path.name)
|
2023-12-03 16:54:07 +08:00
|
|
|
elif mod_path.suffix == ".py": # 处理单文件 mod
|
|
|
|
print(f"importing mod {mod_path=} {mod_path.stem}")
|
2023-06-17 00:47:00 +08:00
|
|
|
if importlib.util.find_spec(mod_path.stem) is not None:
|
|
|
|
mods.append(mod_path.stem)
|
|
|
|
except ImportError:
|
2023-12-03 16:54:07 +08:00
|
|
|
print(f"ImportError when loading mod {mod_path}")
|
2023-06-17 00:47:00 +08:00
|
|
|
traceback.print_exc()
|
|
|
|
return mods
|
|
|
|
|
|
|
|
|
2023-12-16 22:24:44 +08:00
|
|
|
DR_runtime = _DRRuntime()
|