Merge branch 'dev' of https://github.com/MoFox-Studio/MoFox_Bot into dev
This commit is contained in:
@@ -49,42 +49,50 @@ class TTSVoicePlugin(BasePlugin):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.tts_service = None
|
self.tts_service = None
|
||||||
|
# 新增配置缓存
|
||||||
|
self._config_cache = None
|
||||||
|
|
||||||
def _get_config_wrapper(self, key: str, default: Any = None) -> Any:
|
def _get_config_wrapper(self, key: str, default: Any = None) -> Any:
|
||||||
"""
|
"""
|
||||||
配置获取的包装器,用于解决 get_config 无法直接获取动态表(如 tts_styles)和未在 schema 中定义的节的问题。
|
配置获取的包装器,用于解决 get_config 无法直接获取动态表(如 tts_styles)和未在 schema 中定义的节的问题。
|
||||||
由于插件系统的 schema 为空时不会加载未定义的键,这里手动读取配置文件以获取所需配置。
|
由于插件系统的 schema 为空时不会加载未定义的键,这里手动读取配置文件以获取所需配置。
|
||||||
"""
|
"""
|
||||||
# 需要手动加载的顶级配置节
|
# 需要手动加载的顶级配置节(移除未定义的 spatial_effects)
|
||||||
manual_load_keys = ["tts_styles", "spatial_effects", "tts_advanced", "tts"]
|
manual_load_keys = ["tts_styles", "tts_advanced", "tts"]
|
||||||
top_key = key.split('.')[0]
|
top_key = key.split('.')[0]
|
||||||
|
|
||||||
if top_key in manual_load_keys:
|
# 仅当需要手动加载且缓存为空时加载配置
|
||||||
|
if top_key in manual_load_keys and self._config_cache is None:
|
||||||
try:
|
try:
|
||||||
plugin_file = Path(__file__).resolve()
|
# 优化:使用标准插件路径
|
||||||
bot_root = plugin_file.parent.parent.parent.parent.parent
|
config_path = Path(__file__).parent / "config" / self.config_file_name
|
||||||
config_file = bot_root / "config" / "plugins" / self.plugin_name / self.config_file_name
|
|
||||||
|
|
||||||
if not config_file.is_file():
|
if not config_path.exists():
|
||||||
logger.error(f"TTS config file not found at robustly constructed path: {config_file}")
|
logger.error(f"TTS config file not found at {config_path}")
|
||||||
return default
|
self._config_cache = {}
|
||||||
|
else:
|
||||||
|
# 使用 toml.load 代替 toml.loads 以提高性能
|
||||||
|
self._config_cache = toml.load(config_path)
|
||||||
|
except toml.TomlDecodeError as e:
|
||||||
|
logger.error(f"Invalid TOML syntax in config: {e}")
|
||||||
|
self._config_cache = {}
|
||||||
|
except FileNotFoundError:
|
||||||
|
logger.error(f"Config file missing: {config_path}")
|
||||||
|
self._config_cache = {}
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Unexpected config error: {e}", exc_info=True)
|
||||||
|
self._config_cache = {}
|
||||||
|
|
||||||
full_config = toml.loads(config_file.read_text(encoding="utf-8"))
|
# 从缓存中获取配置
|
||||||
|
if top_key in manual_load_keys:
|
||||||
# 支持点状路径访问
|
value = self._config_cache
|
||||||
value = full_config
|
|
||||||
for k in key.split('.'):
|
for k in key.split('.'):
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
value = value.get(k)
|
value = value.get(k)
|
||||||
else:
|
else:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
return value if value is not None else default
|
return value if value is not None else default
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Failed to manually load '{key}' from config: {e}", exc_info=True)
|
|
||||||
return default
|
|
||||||
|
|
||||||
return self.get_config(key, default)
|
return self.get_config(key, default)
|
||||||
|
|
||||||
async def on_plugin_loaded(self):
|
async def on_plugin_loaded(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user