diff --git a/src/plugin_system/utils/manifest_utils.py b/src/plugin_system/utils/manifest_utils.py index 7db2321ae..4a858e20f 100644 --- a/src/plugin_system/utils/manifest_utils.py +++ b/src/plugin_system/utils/manifest_utils.py @@ -17,9 +17,28 @@ logger = get_logger("manifest_utils") class VersionComparator: """版本号比较器 - 支持语义化版本号比较,自动处理snapshot版本 + 支持语义化版本号比较,自动处理snapshot版本,并支持向前兼容性检查 """ + # 版本兼容性映射表(硬编码) + # 格式: {插件最大支持版本: [实际兼容的版本列表]} + COMPATIBILITY_MAP = { + # 0.8.x 系列向前兼容规则 + "0.8.0": ["0.8.1", "0.8.2", "0.8.3", "0.8.4", "0.8.5", "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10"], + "0.8.1": ["0.8.2", "0.8.3", "0.8.4", "0.8.5", "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10"], + "0.8.2": ["0.8.3", "0.8.4", "0.8.5", "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10"], + "0.8.3": ["0.8.4", "0.8.5", "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10"], + "0.8.4": ["0.8.5", "0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10"], + "0.8.5": ["0.8.6", "0.8.7", "0.8.8", "0.8.9", "0.8.10"], + "0.8.6": ["0.8.7", "0.8.8", "0.8.9", "0.8.10"], + "0.8.7": ["0.8.8", "0.8.9", "0.8.10"], + "0.8.8": ["0.8.9", "0.8.10"], + "0.8.9": ["0.8.10"], + + # 可以根据需要添加更多兼容映射 + # "0.9.0": ["0.9.1", "0.9.2", "0.9.3"], # 示例:0.9.x系列兼容 + } + @staticmethod def normalize_version(version: str) -> str: """标准化版本号,移除snapshot标识 @@ -88,9 +107,31 @@ class VersionComparator: else: return 0 + @staticmethod + def check_forward_compatibility(current_version: str, max_version: str) -> Tuple[bool, str]: + """检查向前兼容性(仅使用兼容性映射表) + + Args: + current_version: 当前版本 + max_version: 插件声明的最大支持版本 + + Returns: + Tuple[bool, str]: (是否兼容, 兼容信息) + """ + current_normalized = VersionComparator.normalize_version(current_version) + max_normalized = VersionComparator.normalize_version(max_version) + + # 检查兼容性映射表 + if max_normalized in VersionComparator.COMPATIBILITY_MAP: + compatible_versions = VersionComparator.COMPATIBILITY_MAP[max_normalized] + if current_normalized in compatible_versions: + return True, f"根据兼容性映射表,版本 {current_normalized} 与 {max_normalized} 兼容" + + return False, "" + @staticmethod def is_version_in_range(version: str, min_version: str = "", max_version: str = "") -> Tuple[bool, str]: - """检查版本是否在指定范围内 + """检查版本是否在指定范围内,支持兼容性检查 Args: version: 要检查的版本号 @@ -98,7 +139,7 @@ class VersionComparator: max_version: 最大版本号(可选) Returns: - Tuple[bool, str]: (是否兼容, 错误信息) + Tuple[bool, str]: (是否兼容, 错误信息或兼容信息) """ if not min_version and not max_version: return True, "" @@ -114,8 +155,19 @@ class VersionComparator: # 检查最大版本 if max_version: max_normalized = VersionComparator.normalize_version(max_version) - if VersionComparator.compare_versions(version_normalized, max_normalized) > 0: - return False, f"版本 {version_normalized} 高于最大支持版本 {max_normalized}" + comparison = VersionComparator.compare_versions(version_normalized, max_normalized) + + if comparison > 0: + # 严格版本检查失败,尝试兼容性检查 + is_compatible, compat_msg = VersionComparator.check_forward_compatibility( + version_normalized, max_normalized + ) + + if is_compatible: + logger.info(f"版本兼容性检查:{compat_msg}") + return True, compat_msg + else: + return False, f"版本 {version_normalized} 高于最大支持版本 {max_normalized},且无兼容性映射" return True, "" @@ -128,6 +180,29 @@ class VersionComparator: """ return VersionComparator.normalize_version(MMC_VERSION) + @staticmethod + def add_compatibility_mapping(base_version: str, compatible_versions: list) -> None: + """动态添加兼容性映射 + + Args: + base_version: 基础版本(插件声明的最大支持版本) + compatible_versions: 兼容的版本列表 + """ + base_normalized = VersionComparator.normalize_version(base_version) + VersionComparator.COMPATIBILITY_MAP[base_normalized] = [ + VersionComparator.normalize_version(v) for v in compatible_versions + ] + logger.info(f"添加兼容性映射:{base_normalized} -> {compatible_versions}") + + @staticmethod + def get_compatibility_info() -> Dict[str, list]: + """获取当前的兼容性映射表 + + Returns: + Dict[str, list]: 兼容性映射表的副本 + """ + return VersionComparator.COMPATIBILITY_MAP.copy() + class ManifestValidator: """Manifest文件验证器"""