From 4128823b3fb5c8eddc2b66327ed2a561dfd64d27 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 17 Jun 2023 00:47:00 +0800 Subject: [PATCH 1/5] move DR_runtime [build skip] [build rs skip] --- DR.py | 3 +- Difficult_Rocket/__init__.py | 83 ++++++++---------------------------- Difficult_Rocket/runtime.py | 79 ++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 66 deletions(-) create mode 100644 Difficult_Rocket/runtime.py diff --git a/DR.py b/DR.py index 4a235cf..b5a9a24 100644 --- a/DR.py +++ b/DR.py @@ -58,7 +58,8 @@ def main() -> int: pyglet.resource.path = ['/textures/'] pyglet.resource.reindex() - from Difficult_Rocket import main, DR_runtime + from Difficult_Rocket import main + from Difficult_Rocket.runtime import DR_runtime DR_runtime.start_time_ns = start_time_ns # from pyglet.gl import glClearColor # 调整背景颜色 diff --git a/Difficult_Rocket/__init__.py b/Difficult_Rocket/__init__.py index ee92659..03887af 100644 --- a/Difficult_Rocket/__init__.py +++ b/Difficult_Rocket/__init__.py @@ -4,14 +4,6 @@ # 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 game_version = Version("0.8.2.0") # 游戏版本 @@ -20,6 +12,24 @@ Api_version = Version("0.1.1.0") # API 版本 __version__ = game_version +__all__ = [ + # __init__ + 'DR_status', + # folder + 'api', + 'client', + 'server', + 'command', + 'crash', + 'exception', + 'mod', + 'utils', + # file + 'main', + 'runtime', +] + + class _DR_status(Options): """ DR 的特性开关 / 基本状态 @@ -58,64 +68,7 @@ class _DR_status(Options): return round(12 * self.gui_scale) -class _DR_runtime(Options): - """ - DR 的运行时配置 / 状态 - """ - name = 'DR Runtime' - - language: str = 'zh-CN' - mod_path: str = './mods' - DR_Mod_List: List[Tuple[str, Version]] = [] # DR Mod 列表 (name, version) - - # 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): - with open('./configs/main.toml', 'r', encoding='utf-8') as f: - import rtoml - config_file = rtoml.load(f) - self.language = config_file['runtime']['language'] - self.mod_path = config_file['game']['mods']['path'] - 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: - 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 - - DR_status = _DR_status() -DR_runtime = _DR_runtime() if DR_status.playing: from Difficult_Rocket.utils.thread import new_thread diff --git a/Difficult_Rocket/runtime.py b/Difficult_Rocket/runtime.py new file mode 100644 index 0000000..9507ff1 --- /dev/null +++ b/Difficult_Rocket/runtime.py @@ -0,0 +1,79 @@ +# ------------------------------- +# 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 + + +__all__ = [ + 'DR_runtime' +] + + +class _DR_runtime(Options): + """ + DR 的运行时配置 / 状态 + """ + name = 'DR Runtime' + + language: str = 'zh-CN' + mod_path: str = './mods' + DR_Mod_List: List[Tuple[str, Version]] = [] # DR Mod 列表 (name, version) + + # 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): + with open('./configs/main.toml', 'r', encoding='utf-8') as f: + import rtoml + config_file = rtoml.load(f) + self.language = config_file['runtime']['language'] + self.mod_path = config_file['game']['mods']['path'] + 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: + 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 + + +DR_runtime = _DR_runtime() -- 2.45.2 From 645e7ae80d55d0e180d3b45c223c091e7078859c Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 17 Jun 2023 00:48:20 +0800 Subject: [PATCH 2/5] remove use less sys.path.append [build skip] [build rs skip] --- Difficult_Rocket/main.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Difficult_Rocket/main.py b/Difficult_Rocket/main.py index d888ea1..2b05854 100644 --- a/Difficult_Rocket/main.py +++ b/Difficult_Rocket/main.py @@ -24,10 +24,6 @@ from io import StringIO from pathlib import Path from typing import TYPE_CHECKING, List, Optional, Dict, TypeVar -if __name__ == '__main__': # been start will not run this - sys.path.append('/bin/libs') - sys.path.append('/bin') - if TYPE_CHECKING: from Difficult_Rocket.api.mod import ModInfo else: @@ -35,9 +31,10 @@ else: from Difficult_Rocket.utils import tools from Difficult_Rocket.api.types import Options from Difficult_Rocket.utils.translate import tr +from Difficult_Rocket.runtime import DR_runtime from Difficult_Rocket.utils.thread import new_thread from Difficult_Rocket.crash import write_info_to_cache -from Difficult_Rocket import client, server, DR_status, DR_runtime +from Difficult_Rocket import client, server, DR_status class Console(Options): -- 2.45.2 From 893912fae346cc98e5f94f4d0fefc1e5934fc389 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 17 Jun 2023 00:51:05 +0800 Subject: [PATCH 3/5] replace runtime import [build skip] [build rs skip] --- Difficult_Rocket/client/__init__.py | 6 +++--- Difficult_Rocket/crash/__init__.py | 3 ++- Difficult_Rocket/utils/translate.py | 3 ++- mods/dr_game/__init__.py | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Difficult_Rocket/client/__init__.py b/Difficult_Rocket/client/__init__.py index 7346b96..fe72e57 100644 --- a/Difficult_Rocket/client/__init__.py +++ b/Difficult_Rocket/client/__init__.py @@ -30,10 +30,10 @@ from pyglet.window import key, mouse if TYPE_CHECKING: from Difficult_Rocket.main import Game from Difficult_Rocket.utils import tools -from Difficult_Rocket.api.types import Options from Difficult_Rocket.command import line +from Difficult_Rocket.api.types import Options from Difficult_Rocket.utils.translate import tr -from Difficult_Rocket import DR_runtime +from Difficult_Rocket.runtime import DR_runtime from Difficult_Rocket.api.screen import BaseScreen from Difficult_Rocket.utils.thread import new_thread from Difficult_Rocket.client.fps.fps_log import FpsLogger @@ -51,7 +51,7 @@ class ClientOption(Options): resizeable: bool = True visible: bool = True gui_scale: float = 1.0 - caption: str = "Difficult Rocket v{DR_version}|DR_rs v{DR_Rust_get_version}" + caption: str = "Difficult Rocket v{DR_version}" def load_file(self) -> None: file: dict = tools.load_file('./configs/main.toml') diff --git a/Difficult_Rocket/crash/__init__.py b/Difficult_Rocket/crash/__init__.py index daad3ae..034c9cd 100644 --- a/Difficult_Rocket/crash/__init__.py +++ b/Difficult_Rocket/crash/__init__.py @@ -99,7 +99,8 @@ def write_cache(cache_stream, crash_info): def write_info_to_cache(cache_stream): # 运行状态信息 - from Difficult_Rocket import DR_status, DR_runtime + from Difficult_Rocket import DR_status + from Difficult_Rocket.runtime import DR_runtime cache_stream.write(Run_message) cache_stream.write(markdown_line_handler(f'DR Version: {Difficult_Rocket.game_version}', level=1)) cache_stream.write(markdown_line_handler(f'DR language: {DR_runtime.language}', level=1)) diff --git a/Difficult_Rocket/utils/translate.py b/Difficult_Rocket/utils/translate.py index e7ffaa6..e245a54 100644 --- a/Difficult_Rocket/utils/translate.py +++ b/Difficult_Rocket/utils/translate.py @@ -17,8 +17,9 @@ import inspect from dataclasses import dataclass from typing import Union, Tuple, Any, List, Dict, Hashable, Optional -from Difficult_Rocket import DR_runtime, DR_status +from Difficult_Rocket import DR_status from Difficult_Rocket.utils import tools +from Difficult_Rocket.runtime import DR_runtime from Difficult_Rocket.exception.language import (LanguageNotFound, TranslateKeyNotFound) diff --git a/mods/dr_game/__init__.py b/mods/dr_game/__init__.py index ebd731e..8b808a7 100644 --- a/mods/dr_game/__init__.py +++ b/mods/dr_game/__init__.py @@ -12,8 +12,8 @@ from typing import Optional from Difficult_Rocket import DR_status from Difficult_Rocket.main import Game from Difficult_Rocket.api.mod import ModInfo -from Difficult_Rocket.api.types import Options, Version from Difficult_Rocket.client import ClientWindow +from Difficult_Rocket.api.types import Options, Version DR_rust_version = Version("0.2.10.1") # DR_mod 的 Rust 编写部分的兼容版本 -- 2.45.2 From 021ad2d5de6919c4b48cbd80aca39478a8a42f5d Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 17 Jun 2023 00:54:29 +0800 Subject: [PATCH 4/5] add compat for workflow --- .github/workflows/get_info.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/get_info.py b/.github/workflows/get_info.py index 0f39923..120fd46 100644 --- a/.github/workflows/get_info.py +++ b/.github/workflows/get_info.py @@ -10,13 +10,10 @@ import rtoml sys.path.append(os.path.abspath(os.curdir)) -from Difficult_Rocket import DR_runtime +from Difficult_Rocket.runtime import DR_runtime args = ['-env', '-github-dev'] -# print(sys.argv) - - if sys.argv == [__file__]: # 没有输入参数,直接输出默认信息并输出 print(sys.version) -- 2.45.2 From 45531c431bf0c591882635d0de9cf8c41bc4face Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 17 Jun 2023 01:00:49 +0800 Subject: [PATCH 5/5] add event type for on_close --- Difficult_Rocket/client/__init__.py | 4 ++-- configs/lang/en-us.toml | 3 ++- configs/lang/zh-CN.toml | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Difficult_Rocket/client/__init__.py b/Difficult_Rocket/client/__init__.py index fe72e57..6f538be 100644 --- a/Difficult_Rocket/client/__init__.py +++ b/Difficult_Rocket/client/__init__.py @@ -223,7 +223,7 @@ class ClientWindow(Window): print("==========client stop. KeyboardInterrupt info==========") traceback.print_exc() print("==========client stop. KeyboardInterrupt info end==========") - self.dispatch_event("on_close") + self.dispatch_event("on_close", 'input') sys.exit(0) @new_thread('window save_info') @@ -388,7 +388,7 @@ class ClientWindow(Window): if symbol == key.ESCAPE and not (modifiers & ~(key.MOD_NUMLOCK | key.MOD_CAPSLOCK | key.MOD_SCROLLLOCK)): - self.dispatch_event('on_close') + self.dispatch_event('on_close', 'window') if symbol == key.SLASH: self.input_box._set_focus(True) self.logger.debug( diff --git a/configs/lang/en-us.toml b/configs/lang/en-us.toml index 0dc59a2..80ec430 100644 --- a/configs/lang/en-us.toml +++ b/configs/lang/en-us.toml @@ -29,6 +29,7 @@ mod.load.info = "mod id: {} version: {}" mod.load.faild.info = "Mod load failed: {} error info: {}" mod.load.faild.no_mod_class = "Can't find Mod class" mod.load.done = "All Mod loaded" +mod.event.error = "Mod evenet {} caught error: {} Mod: {}" [client] setup.start = "Client start loading" @@ -71,8 +72,8 @@ os.pid_is = "Server PID: {} PPID: {}" [game] input = "console" -command = "in game commands" window = "window" +command = "in game commands" require_DR_rs = "require DR_rs module" [client.sr1_render] diff --git a/configs/lang/zh-CN.toml b/configs/lang/zh-CN.toml index 99ebe75..8ebfa76 100644 --- a/configs/lang/zh-CN.toml +++ b/configs/lang/zh-CN.toml @@ -72,8 +72,8 @@ os.pid_is = "服务端 PID: {} PPID: {}" [game] input = "控制台" -command = "游戏内命令行" window = "窗口" +command = "游戏内命令行" require_DR_rs = "需要 DR_rs 模块" [client.sr1_render] -- 2.45.2