|
1 |
| -import copy |
2 | 1 | import json
|
3 |
| -from functools import reduce |
4 | 2 | from glob import glob
|
5 |
| -from operator import getitem |
6 |
| -from typing import Any, Final, Optional, Sequence |
7 | 3 |
|
| 4 | +from ..abstracts import * |
8 | 5 | from ..exception import EXCEPTION, os
|
9 | 6 |
|
10 | 7 | # 尝试导入yaml库
|
|
16 | 13 | except Exception:
|
17 | 14 | pass
|
18 | 15 |
|
19 |
| - |
20 |
| -# 根据keys查找值,最后返回一个复制的对象 |
21 |
| -def get_value_by_keys(_dict: dict, _keys: Sequence, _default: Optional[Any] = None) -> Any: |
22 |
| - try: |
23 |
| - return copy.deepcopy(reduce(getitem, _keys, _dict)) |
24 |
| - except KeyError: |
25 |
| - if _default is None: |
26 |
| - EXCEPTION.fatal('Getting "KeyError" while trying to get keys {} from dict!'.format(_keys)) |
27 |
| - return _default |
28 |
| - |
29 |
| - |
30 |
| -# 根据keys查找被设置对应对应对象为指定值 |
31 |
| -def set_value_by_keys(_dict: dict, _keys: Sequence, value: object, assumeKeyExists: bool = True) -> None: |
32 |
| - if len(_keys) < 1: |
33 |
| - EXCEPTION.fatal("Keys' length has to be greater than 0.") |
34 |
| - pointer: dict = _dict |
35 |
| - last_key_index: int = len(_keys) - 1 |
36 |
| - for index in range(last_key_index): |
37 |
| - _item: Optional[object] = pointer.get(_keys[index]) |
38 |
| - if isinstance(_item, dict): |
39 |
| - pointer = _item |
40 |
| - elif _item is None: |
41 |
| - if assumeKeyExists is True: |
42 |
| - EXCEPTION.fatal('Getting "KeyError" while trying to set keys {} to dict!'.format(_keys)) |
43 |
| - pointer[_keys[index]] = {} |
44 |
| - pointer = pointer[_keys[index]] |
45 |
| - else: |
46 |
| - EXCEPTION.fatal("Getting not dict object {0} while trying to set keys {1} to dict!".format(_item, _keys)) |
47 |
| - pointer[_keys[last_key_index]] = value |
48 |
| - |
49 |
| - |
50 | 16 | # 配置文件管理模块
|
51 | 17 | class Config:
|
52 | 18 |
|
@@ -93,7 +59,7 @@ def try_load_file_if_exists(cls, _path: str, _default: dict = {}) -> dict:
|
93 | 59 | # 加载配置文件,并根据key(s)返回对应的数据
|
94 | 60 | @classmethod
|
95 | 61 | def load(cls, path: str, *key: str) -> Any:
|
96 |
| - return get_value_by_keys(cls.__load_file(path), key) |
| 62 | + return TypeSafeGetter.get_by_keys(cls.__load_file(path), key) |
97 | 63 |
|
98 | 64 | # 加载内部配置文件
|
99 | 65 | @classmethod
|
@@ -147,16 +113,60 @@ def resolve_path_and_load_file(cls, file_location: str) -> dict:
|
147 | 113 |
|
148 | 114 |
|
149 | 115 | # 使用引擎的开发者可以自定义的参数
|
150 |
| -class Specification: |
| 116 | +class Specification(TypeSafeGetter): |
151 | 117 |
|
152 | 118 | __SPECIFICATIONS: Final[dict] = Config.load_internal_file("specifications.json")
|
153 | 119 | # 尝试加载项目自定义的参数
|
154 | 120 | __SPECIFICATIONS.update(Config.resolve_path_and_load_file(os.path.join("Data", "specifications")))
|
155 | 121 |
|
156 | 122 | @classmethod
|
157 |
| - def get(cls, *key: str) -> Any: |
158 |
| - return get_value_by_keys(cls.__SPECIFICATIONS, key) |
| 123 | + def _get_data(cls) -> dict: |
| 124 | + return cls.__SPECIFICATIONS |
159 | 125 |
|
160 | 126 | @classmethod
|
161 | 127 | def get_directory(cls, category: str, *_sub: str) -> str:
|
162 | 128 | return str(os.path.join(*cls.__SPECIFICATIONS["Directory"][category], *_sub))
|
| 129 | + |
| 130 | + |
| 131 | +# 数据库 |
| 132 | +class DataBase(TypeSafeGetter): |
| 133 | + |
| 134 | + # 用于存放数据库数据的字典 |
| 135 | + __DATA_BASE_DICT: Final[dict] = {"Tiles": {}, "Decorations": {}, "Npc": {}, "Filters": {}} |
| 136 | + |
| 137 | + @classmethod |
| 138 | + def _get_data(cls) -> dict: |
| 139 | + return cls.__DATA_BASE_DICT |
| 140 | + |
| 141 | + @classmethod |
| 142 | + def update(cls, _value: dict) -> None: |
| 143 | + for key, value in _value.items(): |
| 144 | + if key not in cls.__DATA_BASE_DICT: |
| 145 | + cls.__DATA_BASE_DICT[key] = value |
| 146 | + else: |
| 147 | + cls.__DATA_BASE_DICT[key].update(value) |
| 148 | + |
| 149 | + |
| 150 | +# 全局数据 |
| 151 | +class GlobalVariables(TypeSafeGetter, TypeSafeSetter): |
| 152 | + |
| 153 | + # 用于存放全局数据的字典 |
| 154 | + __GLOBAL_VARIABLES_DICT: Final[dict] = {} |
| 155 | + |
| 156 | + @classmethod |
| 157 | + def _get_data(cls) -> dict: |
| 158 | + return cls.__GLOBAL_VARIABLES_DICT |
| 159 | + |
| 160 | + # 删除特定的全局数据 |
| 161 | + @classmethod |
| 162 | + def remove(cls, _key: str) -> None: |
| 163 | + cls.__GLOBAL_VARIABLES_DICT.pop(_key, None) |
| 164 | + |
| 165 | + # 如果不是对应的值,则设置为对应的值,返回是否对应 |
| 166 | + @classmethod |
| 167 | + def if_get_set(cls, _key: str, valueToGet: object, valueToSet: object) -> bool: |
| 168 | + if cls.__GLOBAL_VARIABLES_DICT[_key] == valueToGet: |
| 169 | + cls.__GLOBAL_VARIABLES_DICT[_key] = valueToSet |
| 170 | + return True |
| 171 | + else: |
| 172 | + return False |
0 commit comments