Difficult-Rocket/Difficult_Rocket/__init__.py

130 lines
3.5 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
2022-06-27 16:51:14 +08:00
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
2021-09-08 23:38:34 +08:00
# All rights reserved
# -------------------------------
2021-04-02 23:31:54 +08:00
"""
2021-01-24 18:26:56 +08:00
writen by shenjackyuanjie
2021-09-02 22:47:10 +08:00
mail: 3695888@qq.com
2021-08-13 12:25:29 +08:00
github: @shenjackyuanjie
2021-09-02 22:47:10 +08:00
gitee: @shenjackyuanjie
2021-04-02 23:31:54 +08:00
"""
2021-09-02 22:47:10 +08:00
2022-08-21 14:59:35 +08:00
from typing import Any, Dict, Callable, Union, Tuple, List, Type
2022-08-03 20:22:36 +08:00
2022-04-08 23:07:41 +08:00
from libs.MCDR.version import Version
2022-02-16 13:36:26 +08:00
2022-08-23 22:51:01 +08:00
game_version = Version("0.6.3")
2022-02-08 17:15:09 +08:00
__version__ = game_version
2021-01-24 18:26:56 +08:00
2021-11-06 19:07:32 +08:00
2022-08-12 21:07:36 +08:00
class Options:
"""
Difficult Rocket 的游戏配置的存储基类
"""
_options: Dict[str, Union[Callable, object]] = {}
2021-08-13 12:25:29 +08:00
2022-08-21 14:59:35 +08:00
def option(self) -> Dict[str, Any]:
2022-08-12 21:07:36 +08:00
values = {}
2022-08-21 14:59:35 +08:00
for ann in self.__annotations__: # 获取类型注释
values[ann] = getattr(self, ann, None) if getattr(self, ann, None) else self.__annotations__[ann]
for option, a_fun in self._options.items(): # 获取额外内容
values[option] = a_fun
for option, a_fun in values.items(): # 检查是否为 property
if a_fun is bool and getattr(self, option, None) is not None:
values[option] = False
if isinstance(a_fun, property):
values[option] = getattr(self, option)
return values
def option_with_len(self) -> List[Union[List[Tuple[str, Any, Any]], int, Any]]:
2022-08-21 14:59:35 +08:00
options = self.option()
max_len_key = 1
max_len_value = 1
max_len_value_t = 1
option_list = []
for key, value in options.items():
value_t = type(value) if not isinstance(type(value), type(value)) else value
# value_t = type(value) if type(value) is not type(type(value)) else value
max_len_key = max(max_len_key, len(key))
max_len_value = max(max_len_value, len(str(value)))
max_len_value_t = max(max_len_value_t, len(str(value_t)))
option_list.append((key, value, value_t))
return [option_list, max_len_key, max_len_value, max_len_value_t]
2022-08-03 20:22:36 +08:00
2022-08-12 21:07:36 +08:00
@classmethod
def add_option(cls, name, value: Union[Callable, object]) -> Dict:
cls._options[name] = value
return cls._options
2022-08-03 20:22:36 +08:00
2022-08-12 21:07:36 +08:00
class _DR_option(Options):
"""
DR 的整体配置存储类
"""
# runtime options
2022-08-21 14:59:35 +08:00
InputBox_use_TextEntry: bool = False
use_local_logging: bool = False
record_threads: bool = True
use_cProfile: bool = False
2022-08-12 21:07:36 +08:00
# tests
2022-08-21 14:59:35 +08:00
playing: bool = False
debugging: bool = False
crash_report_test: bool = True
2022-08-12 21:07:36 +08:00
class _DR_runtime(Options):
"""
DR 的运行时配置
"""
2022-08-21 14:59:35 +08:00
# game statue
DR_version: Version = game_version
2022-08-12 21:07:36 +08:00
# run status
start_time_ns: int = None
client_setup_time_ns: int = None
server_setup_time_ns: int = None
# game options
_language = 'zh-CN'
2022-08-21 14:59:35 +08:00
def __init__(self):
self._options = {'language': self.language}
2022-08-12 21:07:36 +08:00
@property
def language(self):
return self._language
@language.setter
def language(self, value: str):
if value == self._language:
return
assert isinstance(value, str), "DR language MUST be string"
self._language = value
from Difficult_Rocket.utils import translate
translate.tr._update_lang()
2022-08-21 14:59:35 +08:00
# _DR_runtime.add_option('language', _DR_runtime.language)
2022-08-12 21:07:36 +08:00
DR_option = _DR_option()
DR_runtime = _DR_runtime()
if DR_option.playing:
2022-08-03 20:22:36 +08:00
from .utils import new_thread
2021-08-13 12:25:29 +08:00
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