feat:让0.8.1兼容0.8.0插件

This commit is contained in:
SengokuCola
2025-07-01 16:07:32 +08:00
parent 2446285804
commit 6b6f99659d

View File

@@ -17,9 +17,28 @@ logger = get_logger("manifest_utils")
class VersionComparator: 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 @staticmethod
def normalize_version(version: str) -> str: def normalize_version(version: str) -> str:
"""标准化版本号移除snapshot标识 """标准化版本号移除snapshot标识
@@ -88,9 +107,31 @@ class VersionComparator:
else: else:
return 0 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 @staticmethod
def is_version_in_range(version: str, min_version: str = "", max_version: str = "") -> Tuple[bool, str]: def is_version_in_range(version: str, min_version: str = "", max_version: str = "") -> Tuple[bool, str]:
"""检查版本是否在指定范围内 """检查版本是否在指定范围内,支持兼容性检查
Args: Args:
version: 要检查的版本号 version: 要检查的版本号
@@ -98,7 +139,7 @@ class VersionComparator:
max_version: 最大版本号(可选) max_version: 最大版本号(可选)
Returns: Returns:
Tuple[bool, str]: (是否兼容, 错误信息) Tuple[bool, str]: (是否兼容, 错误信息或兼容信息)
""" """
if not min_version and not max_version: if not min_version and not max_version:
return True, "" return True, ""
@@ -114,8 +155,19 @@ class VersionComparator:
# 检查最大版本 # 检查最大版本
if max_version: if max_version:
max_normalized = VersionComparator.normalize_version(max_version) max_normalized = VersionComparator.normalize_version(max_version)
if VersionComparator.compare_versions(version_normalized, max_normalized) > 0: comparison = VersionComparator.compare_versions(version_normalized, max_normalized)
return False, f"版本 {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, "" return True, ""
@@ -128,6 +180,29 @@ class VersionComparator:
""" """
return VersionComparator.normalize_version(MMC_VERSION) 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: class ManifestValidator:
"""Manifest文件验证器""" """Manifest文件验证器"""