From d0ac5c06cf049c8e8f736c31134f952f0e2d96a1 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 17 Jun 2023 00:06:39 +0800 Subject: [PATCH] Add log api --- Difficult_Rocket/api/__init__.py | 13 ++++++++++- Difficult_Rocket/api/log.py | 16 +++++++++++++ Difficult_Rocket/mod/api/__init__.py | 10 ++++++-- Difficult_Rocket/utils/log.py | 34 ++++++++++++++++++++++++++++ docs/src/change_log/dr_sdk.md | 10 ++++++++ mods/dr_game/__init__.py | 3 ++- mods/dr_game/sr1_ship.py | 10 ++++---- 7 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 Difficult_Rocket/api/log.py create mode 100644 Difficult_Rocket/utils/log.py diff --git a/Difficult_Rocket/api/__init__.py b/Difficult_Rocket/api/__init__.py index ae9f186..9101f1c 100644 --- a/Difficult_Rocket/api/__init__.py +++ b/Difficult_Rocket/api/__init__.py @@ -14,4 +14,15 @@ gitee: @shenjackyuanjie # from Difficult_Rocket.api import screen, mod, exception -__all__ = ['screen', 'mod', 'exception'] +__all__ = [ + 'exception', + # 错误类定义 + 'screen', + # screen api + 'types', + # 类型定义 + 'mod', + # mod api + 'log' + # log api +] diff --git a/Difficult_Rocket/api/log.py b/Difficult_Rocket/api/log.py new file mode 100644 index 0000000..78af126 --- /dev/null +++ b/Difficult_Rocket/api/log.py @@ -0,0 +1,16 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com +# All rights reserved +# ------------------------------- + +from Difficult_Rocket.utils.log import (get_named_client_logger, + get_named_server_logger, + get_named_main_logger) + + +__all__ = [ + 'get_named_client_logger', + 'get_named_server_logger', + 'get_named_main_logger', +] \ No newline at end of file diff --git a/Difficult_Rocket/mod/api/__init__.py b/Difficult_Rocket/mod/api/__init__.py index 02192ee..5d068a7 100644 --- a/Difficult_Rocket/mod/api/__init__.py +++ b/Difficult_Rocket/mod/api/__init__.py @@ -12,9 +12,9 @@ gitee: @shenjackyuanjie """ # system function +import warnings from typing import Tuple, List, Optional, TypeVar, TYPE_CHECKING - # from DR if TYPE_CHECKING: from Difficult_Rocket.main import Game @@ -59,9 +59,15 @@ class ModInfo(Options): config: Options = Options() # mod 配置存储 old_mod: Optional["ModInfo"] = None # 旧的mod实例 + def __init__(self, **kwargs): + if not self.DR_version[0] <= DR_status.DR_version <= self.DR_version[1]: + warnings.warn(f"mod {self.mod_id} version {self.version} is not support by DR {DR_status.DR_version}\nDR {self.DR_version} is required") + if not self.DR_Api_version[0] <= DR_status.API_version <= self.DR_Api_version[1]: + warnings.warn(f"mod {self.mod_id} version {self.version} is not support by DR {DR_status.API_version}\nDR {self.DR_Api_version} is required") + super().__init__(**kwargs) + def on_load(self, game: Game, old_self: Optional["ModInfo"] = None) -> bool: """ 加载时调用 """ - print(f'Mod {self.mod_id} loaded') return True def on_client_start(self, game: Game, client: ClientWindow): diff --git a/Difficult_Rocket/utils/log.py b/Difficult_Rocket/utils/log.py new file mode 100644 index 0000000..0b94859 --- /dev/null +++ b/Difficult_Rocket/utils/log.py @@ -0,0 +1,34 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com +# All rights reserved +# ------------------------------- + +import logging + +from typing import Callable + + +__all__ = [ + 'get_named_client_logger', + 'get_named_server_logger', + 'get_named_main_logger', +] + + +def _gen_get_named_logger(from_name: str) -> Callable[[str], logging.Logger]: + + def get_named_logger(name: str) -> logging.Logger: + logger = logging.getLogger(from_name) + logger.name = f'{from_name}.{name}' + return logger + + return get_named_logger + + +get_named_client_logger = _gen_get_named_logger('client') +# 用于获取一个基于 client 配置的 logger +get_named_server_logger = _gen_get_named_logger('server') +# 用于获取一个基于 server 配置的 logger +get_named_main_logger = _gen_get_named_logger('main') +# 用于获取一个基于 main 配置的 logger diff --git a/docs/src/change_log/dr_sdk.md b/docs/src/change_log/dr_sdk.md index 0f9a752..d6f0880 100644 --- a/docs/src/change_log/dr_sdk.md +++ b/docs/src/change_log/dr_sdk.md @@ -17,6 +17,16 @@ - `server_running` - 服务器是否在运行 - Is server running +- Mod loader + - 添加了对支持版本号的 warnings + - Added warnings for supporting version numbers +- API + - `Difficult_Rocket.api.log` + - `get_named_client_logger` + - `get_named_server_logger` + - `get_named_main_logger` + - 分别用于获取 基于 对应名称配置的 logger + - Get the logger for the corresponding name configuration ### 移动 diff --git a/mods/dr_game/__init__.py b/mods/dr_game/__init__.py index 3fd777a..ebd731e 100644 --- a/mods/dr_game/__init__.py +++ b/mods/dr_game/__init__.py @@ -9,6 +9,7 @@ import traceback 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 @@ -58,7 +59,7 @@ class DR_mod(ModInfo): config = DR_mod_runtime - # DR_version = # DR SDK 兼容版本 + DR_version = (DR_status.DR_version, DR_status.DR_version) # DR SDK 兼容版本 # 反正是内置 mod 跟着最新版本的 DR 走就行了 # DR_Api_version = # DR Api版本 # 同理 不管 API 版本 这东西要是不兼容了才是大问题 diff --git a/mods/dr_game/sr1_ship.py b/mods/dr_game/sr1_ship.py index 2019883..46af75d 100644 --- a/mods/dr_game/sr1_ship.py +++ b/mods/dr_game/sr1_ship.py @@ -4,12 +4,11 @@ # All rights reserved # ------------------------------- -import math +# import math import time import random -import logging import traceback -from xml.etree import ElementTree +# from xml.etree import ElementTree from xml.etree.ElementTree import Element from typing import List, TYPE_CHECKING, Union, Dict, Optional, Generator @@ -21,7 +20,7 @@ from pyglet.math import Vec4 from pyglet.text import Label from pyglet.shapes import Line, Rectangle from pyglet.sprite import Sprite -from pyglet.image import Texture +# from pyglet.image import Texture from pyglet.graphics import Batch, Group from . import DR_mod_runtime @@ -32,6 +31,7 @@ from Difficult_Rocket.utils.translate import tr from Difficult_Rocket.api.types import Fonts, Options from Difficult_Rocket.command.line import CommandText from Difficult_Rocket.client.screen import BaseScreen +from Difficult_Rocket.api.log import get_named_client_logger from .types import SR1Textures, SR1PartTexture, SR1PartData, SR1Rotation, xml_bool if TYPE_CHECKING: @@ -40,7 +40,7 @@ if TYPE_CHECKING: if DR_mod_runtime.use_DR_rust: from .Difficult_Rocket_rs import CenterCamera_rs, SR1PartList_rs, SR1Ship_rs -logger = logging.getLogger('client') +logger = get_named_client_logger('dr_game_sr1_ship') def get_sr1_part(part_xml: Element) -> Optional[SR1PartData]: