Update to v0.1.2-alpha
- Fix glitches in some themes. - Add multi-platform library deduplication and comparison logic - Enhance mod and library merging logic with platform and loader preferences (prepare for forge/neoforge support) - Fix certain runtime detect/inspect issues. CoreLib update to 0.1.5
This commit is contained in:
1 parent
c55f559989
commit
ef3667ae5e
16 files changed
+693
-111
No files matched your search
+117
-13
@@ -1,12 +1,14 @@
|
||||
"""
|
||||
This module provide some advanced function for library parse/download process use.
|
||||
This module provides some advanced function for library parse/download process use.
|
||||
"""
|
||||
import logging
|
||||
import re
|
||||
from copy import deepcopy
|
||||
from enum import StrEnum
|
||||
|
||||
from .library import (convert_maven_version, parse_maven_coordinate, generate_relative_path_by_maven_coordinate,
|
||||
is_complete_maven_url, generate_repo_url)
|
||||
from .library import (convert_maven_version, parse_maven_coordinate, is_complete_maven_url,
|
||||
generate_repo_url, generate_filename_from_maven_coordinate,
|
||||
generate_repo_path)
|
||||
from .version_info import MOJANG_LIBRARIES_ENDPOINT
|
||||
from ..common.exception import VersionDataKeyNotFoundException, DependencyException
|
||||
|
||||
@@ -46,16 +48,18 @@ def generate_unique_library_key(group: str, artifact: str, classifier: str | Non
|
||||
return key
|
||||
|
||||
|
||||
def normalize_library_to_common_struct(library: dict, default_maven_url: str = MOJANG_LIBRARIES_ENDPOINT) -> dict:
|
||||
def normalize_library_to_common_struct(library: dict, default_maven_url: str | None = MOJANG_LIBRARIES_ENDPOINT,
|
||||
default_extension: str = "jar") -> dict:
|
||||
result = deepcopy(library)
|
||||
name: str = library.get("name")
|
||||
name: str | None = library.get("name")
|
||||
|
||||
if name is None:
|
||||
raise VersionDataKeyNotFoundException(
|
||||
"Provided mod loader version manifest library does not contain key \"name\"."
|
||||
)
|
||||
|
||||
group, artifact, version, classifier, extension = parse_maven_coordinate(name)
|
||||
group, artifact, version, classifier, extension = parse_maven_coordinate(name,
|
||||
default_extension=default_extension)
|
||||
|
||||
downloads_raw = result.setdefault("downloads", {})
|
||||
|
||||
@@ -68,15 +72,21 @@ def normalize_library_to_common_struct(library: dict, default_maven_url: str = M
|
||||
|
||||
# Generate new relative path and put back
|
||||
if not artifact_raw.get("path"):
|
||||
path = generate_relative_path_by_maven_coordinate(name)
|
||||
if not path:
|
||||
raise DependencyException(
|
||||
f"Unable to generate artifact path for {name}"
|
||||
)
|
||||
artifact_raw["path"] = path
|
||||
filename = generate_filename_from_maven_coordinate(
|
||||
artifact,
|
||||
version,
|
||||
classifier=classifier,
|
||||
extension=extension,
|
||||
)
|
||||
artifact_raw["path"] = generate_repo_path(
|
||||
group,
|
||||
artifact,
|
||||
version,
|
||||
filename,
|
||||
)
|
||||
|
||||
if not artifact_raw.get("url"):
|
||||
top_level_url = result.get("url")
|
||||
top_level_url: str | None = result.get("url")
|
||||
|
||||
if top_level_url:
|
||||
if is_complete_maven_url(
|
||||
@@ -100,6 +110,11 @@ def normalize_library_to_common_struct(library: dict, default_maven_url: str = M
|
||||
extension,
|
||||
)
|
||||
else:
|
||||
if not default_maven_url:
|
||||
raise DependencyException(
|
||||
f"No download source for library {name!r}."
|
||||
)
|
||||
|
||||
# Generate missing url
|
||||
artifact_raw["url"] = generate_repo_url(
|
||||
default_maven_url,
|
||||
@@ -128,3 +143,92 @@ def normalize_library_to_common_struct(library: dict, default_maven_url: str = M
|
||||
result["downloads"] = downloads
|
||||
|
||||
return result
|
||||
|
||||
class LibraryPreference(StrEnum):
|
||||
HIGHER_VERSION = "higher_version"
|
||||
LOWER_VERSION = "lower_version"
|
||||
MOD_LIBRARY = "mod_library"
|
||||
INHERITS_LIBRARY = "inherits_library"
|
||||
|
||||
|
||||
PREFER_BY_HIGHER_VERSION = LibraryPreference.HIGHER_VERSION
|
||||
PREFER_BY_LOWER_VERSION = LibraryPreference.LOWER_VERSION
|
||||
PREFER_MOD_LIBRARY = LibraryPreference.MOD_LIBRARY
|
||||
PREFER_INHERITS_LIBRARY = LibraryPreference.INHERITS_LIBRARY
|
||||
|
||||
LIBRARY_PREFER_OPTIONS = (PREFER_BY_HIGHER_VERSION, PREFER_BY_LOWER_VERSION, PREFER_MOD_LIBRARY, PREFER_INHERITS_LIBRARY,)
|
||||
|
||||
|
||||
def compare_library_versions(left: str, right: str) -> int:
|
||||
"""
|
||||
Compare two library version string.
|
||||
:param left:
|
||||
:param right:
|
||||
:return:
|
||||
result: -1 if left < right, 0 if left == right, 1 if left > right. If parse failed, throw DependencyException.
|
||||
"""
|
||||
if left == right:
|
||||
return 0
|
||||
|
||||
numeric_version = r"[0-9]+(?:\.[0-9]+)*"
|
||||
|
||||
if not (re.fullmatch(numeric_version, left) and re.fullmatch(numeric_version, right)):
|
||||
raise DependencyException(f"Cannot compare library versions {left!r} and {right!r}."
|
||||
f" Use PREFER_MOD_LIBRARY or PREFER_INHERITS_LIBRARY may solve this issue.")
|
||||
|
||||
left_parts = tuple(map(int, left.split(".")))
|
||||
right_parts = tuple(map(int, right.split(".")))
|
||||
|
||||
length = max(len(left_parts), len(right_parts))
|
||||
left_parts += (0,) * (length - len(left_parts))
|
||||
right_parts += (0,) * (length - len(right_parts))
|
||||
|
||||
return (left_parts > right_parts) - (left_parts < right_parts)
|
||||
|
||||
def deduplicate_libraries(libraries: list[dict], preference: LibraryPreference,
|
||||
default_extension: str = "jar") -> list[dict]:
|
||||
indexed = {}
|
||||
|
||||
preference = LibraryPreference(preference)
|
||||
|
||||
for library in libraries:
|
||||
name: str | None = library.get("name")
|
||||
if not name:
|
||||
raise DependencyException("Library is missing its name.")
|
||||
|
||||
group, artifact, version, classifier, extension = parse_maven_coordinate(name,
|
||||
default_extension=default_extension)
|
||||
key = generate_unique_library_key(
|
||||
group=group,
|
||||
artifact=artifact,
|
||||
classifier=classifier,
|
||||
extension=extension,
|
||||
)
|
||||
|
||||
previous = indexed.get(key)
|
||||
|
||||
if previous is not None:
|
||||
logger.warning(
|
||||
"Duplicate library: %s / %s",
|
||||
previous["name"],
|
||||
name,
|
||||
)
|
||||
|
||||
# Skip/Replace library depend by the library preference
|
||||
if preference in (PREFER_BY_HIGHER_VERSION, PREFER_BY_LOWER_VERSION,):
|
||||
previous_version = parse_maven_coordinate(previous["name"], default_extension=default_extension)[2]
|
||||
|
||||
comparison = compare_library_versions(previous_version, version,)
|
||||
|
||||
keep_previous = (
|
||||
comparison > 0
|
||||
if preference == PREFER_BY_HIGHER_VERSION
|
||||
else comparison < 0
|
||||
)
|
||||
|
||||
if keep_previous:
|
||||
continue
|
||||
|
||||
indexed[key] = library
|
||||
|
||||
return list(indexed.values())
|
||||
Reference in new issue
Block a user