Difficult-Rocket/Difficult_Rocket/__init__.py

131 lines
4.2 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
2023-01-20 14:08:12 +08:00
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
2021-09-08 23:38:34 +08:00
# All rights reserved
# -------------------------------
2023-01-21 23:05:34 +08:00
import sys
2023-04-22 15:18:35 +08:00
import importlib
2023-01-22 17:21:21 +08:00
import traceback
2023-04-09 03:21:11 +08:00
import contextlib
2023-04-22 20:52:10 +08:00
import importlib.util
from pathlib import Path
2023-04-22 15:18:35 +08:00
from typing import Optional, List, Tuple
2022-11-08 20:18:01 +08:00
2023-06-10 18:08:40 +08:00
from Difficult_Rocket.api.types import Options, Version
2022-02-16 13:36:26 +08:00
2023-06-11 18:20:20 +08:00
game_version = Version("0.8.2.0") # 游戏版本
2023-06-11 17:42:24 +08:00
build_version = Version("2.1.0.0") # 编译文件版本(与游戏本体无关)
Api_version = Version("0.1.1.0") # API 版本
2022-02-08 17:15:09 +08:00
__version__ = game_version
2021-01-24 18:26:56 +08:00
class _DR_status(Options):
2022-08-12 21:07:36 +08:00
"""
DR 的特性开关 / 基本状态
2022-08-12 21:07:36 +08:00
"""
2022-11-20 17:46:02 +08:00
name = 'DR Option'
# run status
client_running: bool = False
server_running: bool = False
# feature switch
2023-01-24 23:59:07 +08:00
InputBox_use_TextEntry: bool = True
record_threads: bool = True
report_translate_not_found: bool = True
use_multiprocess: bool = False
use_cProfile: bool = False
use_local_logging: bool = False
2023-04-22 17:13:45 +08:00
2022-08-12 21:07:36 +08:00
# tests
2023-01-21 12:20:50 +08:00
playing: bool = False
debugging: bool = False
2023-04-30 00:48:42 +08:00
crash_report_test: bool = False
2022-08-12 21:07:36 +08:00
# game version status
DR_version: Version = game_version # DR SDK 版本
Build_version: Version = build_version # DR 构建 版本
API_version: Version = Api_version # DR SDK API 版本
# game options
language: str = 'zh-CN'
mod_path: str = './mods'
default_language: str = 'zh-CN'
2022-12-22 10:54:46 +08:00
# window option
2023-01-19 22:29:43 +08:00
gui_scale: float = 1.0 # default 1.0 2.0 -> 2x 3 -> 3x
@property
def std_font_size(self) -> int:
2023-01-20 20:20:35 +08:00
return round(12 * self.gui_scale)
2022-12-22 10:54:46 +08:00
2022-08-12 21:07:36 +08:00
class _DR_runtime(Options):
"""
2023-01-21 12:20:50 +08:00
DR 的运行时配置/状态
2022-08-12 21:07:36 +08:00
"""
2022-11-20 17:46:02 +08:00
name = 'DR Runtime'
2023-04-22 15:18:35 +08:00
2023-04-23 00:15:36 +08:00
DR_Mod_List: List[Tuple[str, Version]] = [] # DR Mod 列表 (name, version)
2023-04-22 17:13:45 +08:00
2022-08-12 21:07:36 +08:00
# run status
2023-04-22 17:13:45 +08:00
start_time_ns: Optional[int] = None
client_setup_cause_ns: Optional[int] = None
server_setup_cause_ns: Optional[int] = None
2022-08-12 21:07:36 +08:00
2023-04-22 15:18:35 +08:00
def load_file(self) -> bool:
2023-04-05 13:04:57 +08:00
with contextlib.suppress(FileNotFoundError):
with open('./configs/main.toml', 'r', encoding='utf-8') as f:
import rtoml
2023-04-22 17:28:17 +08:00
config_file = rtoml.load(f)
self.language = config_file['runtime']['language']
self.mod_path = config_file['game']['mods']['path']
2023-04-22 15:18:35 +08:00
return True
return False
def find_mods(self) -> List[str]:
2023-04-22 20:52:10 +08:00
mods = []
2023-05-02 15:45:52 +08:00
mod_path = Path(self.mod_path)
if not mod_path.exists():
mod_path.mkdir()
return []
paths = mod_path.iterdir()
2023-04-22 20:52:10 +08:00
sys.path.append(self.mod_path)
2023-04-24 23:35:00 +08:00
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()
2023-04-22 20:52:10 +08:00
return mods
2023-01-24 12:25:23 +08:00
2022-08-12 21:07:36 +08:00
DR_status = _DR_status()
2022-08-12 21:07:36 +08:00
DR_runtime = _DR_runtime()
if DR_status.playing:
2023-05-28 01:03:46 +08:00
from Difficult_Rocket.utils.thread import new_thread
def think_it(something):
return something
2021-10-31 23:26:32 +08:00
@new_thread('think')
def think(some_thing_to_think):
gotcha = think_it(some_thing_to_think)
2021-10-31 23:26:32 +08:00
return gotcha