diff --git a/Difficult_Rocket/utils/tools.py b/Difficult_Rocket/utils/tools.py index 5609e81..e731e66 100644 --- a/Difficult_Rocket/utils/tools.py +++ b/Difficult_Rocket/utils/tools.py @@ -16,20 +16,19 @@ import sys import time import math import json +import rtoml import logging import configparser -from typing import Union +from pathlib import Path +from typing import Union, Optional from xml.etree import ElementTree - -import rtoml - from defusedxml.ElementTree import parse from Difficult_Rocket.exception.unsupport import NoMoreJson5 # logger -tools_logger = logging.getLogger('part-tools') +tools_logger = logging.getLogger('tools') """ file configs """ @@ -39,10 +38,12 @@ file_error = {FileNotFoundError: 'no {filetype} file was founded!:\n file name: Exception: 'get some {error_type} when read {filetype} file {filename}! \n file type: {} \n file name: {} \n stack: {stack}'} -def load_file(file_name: str, - stack: Union[str, list, dict, None] = None, - raise_error: bool = True, - encoding: str = 'utf-8') -> Union[dict, ElementTree.ElementTree]: +def load_file(file_name: Union[str, Path], + stack: Optional[Union[str, list, dict]] = None, + raise_error: Optional[bool] = True, + encoding: Optional[str] = 'utf-8') -> Union[dict, ElementTree.ElementTree]: + if isinstance(file_name, Path): + file_name = str(file_name) f_type = file_name[file_name.rfind('.') + 1:] # 从最后一个.到末尾 (截取文件格式) get_file = NotImplementedError('解析失败,请检查文件类型/文件内容/文件是否存在!') try: diff --git a/Difficult_Rocket/utils/translate.py b/Difficult_Rocket/utils/translate.py index e245a54..971584f 100644 --- a/Difficult_Rocket/utils/translate.py +++ b/Difficult_Rocket/utils/translate.py @@ -14,6 +14,7 @@ gitee: @shenjackyuanjie import os import inspect +from pathlib import Path from dataclasses import dataclass from typing import Union, Tuple, Any, List, Dict, Hashable, Optional @@ -153,15 +154,16 @@ class Tr: GOOD """ - def __init__(self, language: str = None, config: Optional[TranslateConfig] = None): + def __init__(self, language: str = None, config: Optional[TranslateConfig] = None, lang_path: Optional[Path] = None): """ 诶嘿,我抄的MCDR :param language: Tr 所使用的的语言 :param config: 配置 """ self.language_name = language if language is not None else DR_runtime.language - self.translates: Dict[str, Union[str, Dict]] = tools.load_file(f'configs/lang/{self.language_name}.toml') - self.default_translate: Dict = tools.load_file(f'configs/lang/{DR_status.default_language}.toml') + self.language_path = lang_path if lang_path is not None else Path('configs/lang') + self.translates: Dict[str, Union[str, Dict]] = tools.load_file(self.language_path / f'{self.language_name}.toml') + self.default_translate: Dict = tools.load_file(f'{self.language_path}/{DR_status.default_language}.toml') self.default_config = config.set('source', self) if config is not None else TranslateConfig(source=self) self.translates_cache = Translates(value=self.translates, config=self.default_config.copy()) @@ -185,11 +187,11 @@ class Tr: if lang == ' ' or lang == '': raise LanguageNotFound('Can not be empty') lang = lang or self.language_name - if not os.path.exists(f'./configs/lang/{lang}.toml'): - print(f"lang: {os.path.exists(f'./configs/lang/{lang}.toml')} language = {lang} {self.language_name=}") + if not os.path.exists(f'{self.language_path}/{lang}.toml'): + print(f"lang: {os.path.exists(f'{self.language_path}/{lang}.toml')} language = {lang} {self.language_name=}") raise LanguageNotFound(lang) - self.translates: Dict[str, Union[str, Dict]] = tools.load_file(f'configs/lang/{lang}.toml') - self.default_translate: Dict = tools.load_file(f'configs/lang/{DR_runtime.default_language}.toml') + self.translates: Dict[str, Union[str, Dict]] = tools.load_file(f'{self.language_path}/{lang}.toml') + self.default_translate: Dict = tools.load_file(f'{self.language_path}/{DR_runtime.default_language}.toml') self.translates_cache = Translates(value=self.translates, config=self.default_config.copy()) self.language_name = lang DR_runtime.language = self.language_name diff --git a/configs/lang/en-us.toml b/configs/lang/en-us.toml index 80ec430..11bfe76 100644 --- a/configs/lang/en-us.toml +++ b/configs/lang/en-us.toml @@ -75,14 +75,3 @@ input = "console" window = "window" command = "in game commands" require_DR_rs = "require DR_rs module" - -[client.sr1_render] -setup.start = "SR1 Renderer start loading" -setup.use_time = "SR1 Renderer loading has used: {} second" -xml.loading = "Loading XML file: {}" -xml.load_done = "XML file loaded" -xml.load_time = "XML file loading has used: {} second" -ship.load = "Loading ship: {}" -ship.load_time = "Ship loading has used: {} second" -ship.info = "Ship info:\n- Parts: {}\n- Weight: {}" -ship.render.done = "Ship render done" diff --git a/configs/lang/zh-CN.toml b/configs/lang/zh-CN.toml index 8ebfa76..b348ff3 100644 --- a/configs/lang/zh-CN.toml +++ b/configs/lang/zh-CN.toml @@ -75,15 +75,3 @@ input = "控制台" window = "窗口" command = "游戏内命令行" require_DR_rs = "需要 DR_rs 模块" - -[client.sr1_render] -setup.start = "SR1 渲染器开始载入" -setup.use_time = "SR1 渲染器载入消耗时间: {} 秒" -xml.loading = "正在加载XML文件: {}" -xml.load_done = "XML 文件加载完成" -xml.load_time = "XML 文件加载消耗时间: {} 秒" -ship.load = "正在加载飞船: {}" -ship.load_time = "飞船加载消耗时间: {} 秒" -#ship.info = "飞船信息:\n- 部件数量: {}\n- 部件重量: {}\n- 文件大小: {}" -ship.info = "飞船信息:\n- 部件数量: {}\n- 部件重量: {}" -ship.render.done = "飞船渲染完成" diff --git a/mods/dr_game/lang/en-us.toml b/mods/dr_game/lang/en-us.toml new file mode 100644 index 0000000..8c8a105 --- /dev/null +++ b/mods/dr_game/lang/en-us.toml @@ -0,0 +1,12 @@ +[mod.info] +setup.start = "SR1 Renderer start loading" +setup.use_time = "SR1 Renderer loading has used: {} second" + +[sr1.ship] +xml.loading = "Loading XML file: {}" +xml.load_done = "XML file loaded" +xml.load_time = "XML file loading has used: {} second" +ship.load = "Loading ship: {}" +ship.load_time = "Ship loading has used: {} second" +ship.info = "Ship info:\n- Parts: {}\n- Weight: {}" +ship.render.done = "Ship render done" \ No newline at end of file diff --git a/mods/dr_game/lang/zh-CN.toml b/mods/dr_game/lang/zh-CN.toml index e69de29..a1baf37 100644 --- a/mods/dr_game/lang/zh-CN.toml +++ b/mods/dr_game/lang/zh-CN.toml @@ -0,0 +1,13 @@ +[mod.info] +setup.start = "SR1 渲染器开始载入" +setup.use_time = "SR1 渲染器载入消耗时间: {} 秒" + +[sr1.ship] +xml.loading = "正在加载XML文件: {}" +xml.load_done = "XML 文件加载完成" +xml.load_time = "XML 文件加载消耗时间: {} 秒" +ship.load = "正在加载飞船: {}" +ship.load_time = "飞船加载消耗时间: {} 秒" +#ship.info = "飞船信息:\n- 部件数量: {}\n- 部件重量: {}\n- 文件大小: {}" +ship.info = "飞船信息:\n- 部件数量: {}\n- 部件重量: {}" +ship.render.done = "飞船渲染完成" diff --git a/mods/dr_game/sr1_ship.py b/mods/dr_game/sr1_ship.py index 6402379..ac6bdb3 100644 --- a/mods/dr_game/sr1_ship.py +++ b/mods/dr_game/sr1_ship.py @@ -9,14 +9,12 @@ import time import random import logging import traceback -# from xml.etree import ElementTree + +from pathlib import Path from xml.etree.ElementTree import Element from typing import List, TYPE_CHECKING, Union, Dict, Optional, Generator - -# third party package from defusedxml.ElementTree import parse -# pyglet from pyglet.math import Vec4 from pyglet.text import Label from pyglet.sprite import Sprite @@ -28,7 +26,7 @@ from . import DR_mod_runtime # Difficult Rocket from Difficult_Rocket import DR_status -from Difficult_Rocket.utils.translate import tr +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 @@ -41,6 +39,7 @@ if DR_mod_runtime.use_DR_rust: from .Difficult_Rocket_rs import CenterCamera_rs, SR1PartList_rs, SR1Ship_rs logger = logging.getLogger('client.dr_game_sr1_ship') +sr_tr = Tr(lang_path=Path('./mods/dr_game/lang')) def get_sr1_part(part_xml: Element) -> Optional[SR1PartData]: @@ -88,7 +87,7 @@ class SR1ShipRender(BaseScreen): def __init__(self, main_window: "ClientWindow"): super().__init__(main_window) - logger.info(tr().client.sr1_render.setup.start()) + logger.info(sr_tr().mod.info.setup.start()) load_start_time = time.time_ns() self.rendered = False self.focus = True @@ -133,7 +132,7 @@ class SR1ShipRender(BaseScreen): self.part_box_dict: Dict[int, Rectangle] = {} self.part_line_box: Dict[int, List[Line]] = {} load_end_time = time.time_ns() - logger.info(tr().client.sr1_render.setup.use_time().format( + logger.info(sr_tr().mod.info.setup.use_time().format( (load_end_time - load_start_time) / 1000000000)) if DR_mod_runtime.use_DR_rust: self.camera_rs = CenterCamera_rs(main_window, @@ -144,7 +143,7 @@ class SR1ShipRender(BaseScreen): def load_xml(self, file_path: str) -> bool: try: start_time = time.time_ns() - logger.info(tr().client.sr1_render.xml.loading().format(file_path)) + logger.info(sr_tr().sr1.ship.xml.loading().format(file_path)) cache_doc = parse(file_path) self.xml_doc = cache_doc self.xml_root = self.xml_doc.getroot() @@ -156,8 +155,8 @@ class SR1ShipRender(BaseScreen): print(self.rust_ship.img_pos) except Exception: traceback.print_exc() - logger.info(tr().client.sr1_render.xml.load_done()) - logger.info(tr().client.sr1_render.xml.load_time().format( + logger.info(sr_tr().sr1.ship.xml.load_done()) + logger.info(sr_tr().sr1.ship.xml.load_time().format( (time.time_ns() - start_time) / 1000000000)) return True except Exception as e: @@ -224,7 +223,7 @@ class SR1ShipRender(BaseScreen): def render_ship(self): if self.textures is None: self.load_textures() - logger.info(tr().client.sr1_render.ship.load().format(self.xml_name)) + logger.info(sr_tr().sr1.ship.ship.load().format(self.xml_name)) start_time = time.perf_counter_ns() self.part_data: Dict[int, SR1PartData] = {} self.parts_sprite: Dict[int, Sprite] = {} @@ -252,10 +251,10 @@ class SR1ShipRender(BaseScreen): if DR_mod_runtime.use_DR_rust: for part in self.part_data: full_mass += self.part_list_rs.get_part_type(self.part_data[part].p_type).mass * 500 - logger.info(tr().client.sr1_render.ship.load_time().format( + logger.info(sr_tr().sr1.ship.ship.load_time().format( (time.perf_counter_ns() - start_time) / 1000000000)) - logger.info(tr().client.sr1_render.ship.info().format( - len(self.part_data), f'{full_mass}kg' if DR_mod_runtime.use_DR_rust else tr().game.require_DR_rs())) + logger.info(sr_tr().sr1.ship.ship.info().format( + len(self.part_data), f'{full_mass}kg' if DR_mod_runtime.use_DR_rust else sr_tr().game.require_DR_rs())) self.rendered = True def update_parts(self) -> bool: @@ -278,7 +277,7 @@ class SR1ShipRender(BaseScreen): next(self.gen_draw) except GeneratorExit: self.drawing = False - logger.info(tr().client.sr1_render.ship.render.done()) + logger.info(sr_tr().sr1.ship.ship.render.done()) if self.need_update_parts: self.update_parts()