Now the *launcher* can parse natives in version manifest.
(Never let me did it again...it's a bullsh*t)
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
from __future__ import annotations
|
||||
import hashlib
|
||||
import logging
|
||||
import platform
|
||||
import zipfile
|
||||
from collections.abc import Callable
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from pathlib import Path
|
||||
@@ -7,7 +10,7 @@ from typing import List
|
||||
|
||||
import requests
|
||||
|
||||
from core_lib.common.exception import HashMismatchException
|
||||
from .exception import HashMismatchException
|
||||
|
||||
DEFAULT_HASH_CHUNK_SIZE = 4096
|
||||
DEFAULT_DOWNLOAD_CHUNK_SIZE = 4096
|
||||
@@ -134,11 +137,12 @@ class FileObject:
|
||||
return self.__path.exists()
|
||||
|
||||
|
||||
def download_file(file: FileObject, overwrite=True) -> FileObject | Exception:
|
||||
def download_file(file: FileObject, overwrite=True, progress_callback: Callable[int]=None) -> FileObject | Exception:
|
||||
"""
|
||||
Download file from url
|
||||
:param file: File (Objected)
|
||||
:param overwrite: Overwrite existing file
|
||||
:param progress_callback: Callback function called with progress information
|
||||
:return:
|
||||
"""
|
||||
|
||||
@@ -153,6 +157,8 @@ def download_file(file: FileObject, overwrite=True) -> FileObject | Exception:
|
||||
logger.debug("Downloading file {} to {}".format(file.url, file))
|
||||
|
||||
with requests.get(file.url, stream=True) as r:
|
||||
current_chunk = 0
|
||||
total_bytes = r.headers.get("content-length", None)
|
||||
logger.debug("URL: {}".format(file.url))
|
||||
logger.debug("Response code: {}".format(r.status_code))
|
||||
|
||||
@@ -162,6 +168,11 @@ def download_file(file: FileObject, overwrite=True) -> FileObject | Exception:
|
||||
break
|
||||
|
||||
f.write(chunk)
|
||||
current_chunk += 1
|
||||
|
||||
if callable(progress_callback) and total_bytes is not None:
|
||||
current_progress = ((current_chunk * DEFAULT_DOWNLOAD_CHUNK_SIZE) / int(total_bytes) ) * 100
|
||||
progress_callback(file.name, current_progress)
|
||||
|
||||
if file.expected_sha256 and not file.verify_sha256():
|
||||
raise HashMismatchException("sha256", file.sha256, file.expected_sha256, file.posix_path)
|
||||
@@ -178,18 +189,20 @@ def download_file(file: FileObject, overwrite=True) -> FileObject | Exception:
|
||||
return file
|
||||
|
||||
def download_multiple_files(files: List[FileObject], download_callback: Callable[str]=None,
|
||||
max_workers=THREADED_DOWNLOAD_MAX_WORKERS) \
|
||||
max_workers=THREADED_DOWNLOAD_MAX_WORKERS, progress_callback: Callable[str]=None) \
|
||||
-> List[FileObject]:
|
||||
"""
|
||||
Download multiple files
|
||||
:param download_callback:
|
||||
:param files:
|
||||
:param max_workers:
|
||||
:param progress_callback:
|
||||
:return: List of file objects that download failed.
|
||||
"""
|
||||
|
||||
fails = []
|
||||
|
||||
files_count = 0
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||
future_to_file = {executor.submit(download_file, file): file for file in files}
|
||||
|
||||
@@ -197,12 +210,48 @@ def download_multiple_files(files: List[FileObject], download_callback: Callable
|
||||
file = future_to_file[future]
|
||||
|
||||
try:
|
||||
files_count += 1
|
||||
future.result()
|
||||
if callable(download_callback):
|
||||
download_callback(file.posix_path)
|
||||
|
||||
if callable(progress_callback):
|
||||
progress_callback(f"{len(files)} Files", files_count / len(files) * 100)
|
||||
except Exception as exc:
|
||||
logger.error("Unable to download file {}: {}".format(file.posix_path, exc))
|
||||
fails.append(file)
|
||||
|
||||
|
||||
return fails
|
||||
|
||||
def unzip(file: FileObject, destination: Path, target_extract_dir: Path | None=None, exclude: list[str] | None = None):
|
||||
with zipfile.ZipFile(file.posix_path, "r") as archive:
|
||||
|
||||
if target_extract_dir is None and exclude is None:
|
||||
archive.extractall(path=destination)
|
||||
else:
|
||||
for member in archive.namelist():
|
||||
# Ignore exclude file.
|
||||
if any(member.startswith(pattern) for pattern in exclude):
|
||||
continue
|
||||
|
||||
if member.startswith(target_extract_dir.as_posix()):
|
||||
archive.extract(member, destination)
|
||||
|
||||
def get_platform_info():
|
||||
"""
|
||||
Get information about the current platform.
|
||||
:return:
|
||||
platform_name: str, architecture: str, os_version: str
|
||||
"""
|
||||
platform_name = platform.system().lower()
|
||||
arch = platform.uname().machine.lower()
|
||||
os_version = platform.version()
|
||||
|
||||
if platform_name == "darwin":
|
||||
os_version = platform.mac_ver()[0]
|
||||
elif platform_name == "linux":
|
||||
os_version = platform.release()
|
||||
|
||||
return platform_name, arch, os_version
|
||||
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
|
||||
from ..common.common import get_platform_info
|
||||
from .version_info import (
|
||||
MOJANG_LIBRARIES_ENDPOINT,
|
||||
NATIVE_OS_KEY_MAP,
|
||||
NATIVE_OS_RULE_MAP,
|
||||
NATIVE_ARCH_KEY_MAP,
|
||||
NATIVE_NEW_TO_OLD_ARCH,
|
||||
NATIVE_OLD_TO_NEW_ARCH,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("Launcher.CoreLib")
|
||||
|
||||
def generate_formatted_artifact_with_name(lib_name, lib_artifact, libraries_endpoint=MOJANG_LIBRARIES_ENDPOINT):
|
||||
"""
|
||||
Generates a formatted artifact
|
||||
:param lib_name: Library name
|
||||
:param lib_artifact: Type should be a dict, and it must contain the key "path" (relative path)
|
||||
:param libraries_endpoint: The function will use it and relative path to generate a new URL if artifact does not contain it.
|
||||
Default value is MOJANG_LIBRARIES_ENDPOINT
|
||||
:return:
|
||||
"""
|
||||
artifact_new = dict(lib_artifact)
|
||||
artifact_new["name"] = lib_name
|
||||
|
||||
if not artifact_new.get("url") and artifact_new.get("path"):
|
||||
artifact_new["url"] = (
|
||||
libraries_endpoint.rstrip("/")
|
||||
+ "/"
|
||||
+ artifact_new["path"].lstrip("/")
|
||||
)
|
||||
elif not artifact_new.get("url") and not artifact_new.get("path"):
|
||||
logger.warning(f"Artifact {lib_name} missing relative path. This may cause issues.")
|
||||
return None
|
||||
|
||||
return artifact_new
|
||||
|
||||
|
||||
def is_library_allowed(rules, platform_rule_name, platform_arch_type, platform_version):
|
||||
"""
|
||||
Checks if the given rule is allowed for the given platform to use it.
|
||||
:param rules: Rules from library data
|
||||
The following parameters can get from get_natives_platform_info
|
||||
:param platform_rule_name: Platform rule name
|
||||
:param platform_arch_type: Platform arch type
|
||||
:param platform_version: Platform version
|
||||
:return:
|
||||
"""
|
||||
allowed = False
|
||||
|
||||
if not rules:
|
||||
return True
|
||||
|
||||
for rule in rules:
|
||||
rule_action = rule.get("action", None)
|
||||
os_section = rule.get("os", {})
|
||||
rule_os = os_section.get("name", None)
|
||||
rule_arch = os_section.get("arch") # Not sure if is available in official source
|
||||
rule_version = os_section.get("version", None)
|
||||
|
||||
if rule_os is not None and platform_rule_name != rule_os:
|
||||
continue
|
||||
|
||||
if rule_arch is not None and platform_arch_type != rule_arch:
|
||||
continue
|
||||
|
||||
if rule_version is not None:
|
||||
try:
|
||||
if re.search(rule_version, platform_version) is None:
|
||||
continue
|
||||
except re.error as exc:
|
||||
logger.warning(
|
||||
f"Unsupported OS version regex {rule_version!r}: {exc}"
|
||||
)
|
||||
continue
|
||||
|
||||
if rule_action == "allow":
|
||||
allowed = True
|
||||
elif rule_action == "disallow":
|
||||
allowed = False
|
||||
else:
|
||||
logger.warning(f"Unknown action {rule_action}\n")
|
||||
|
||||
return allowed
|
||||
|
||||
|
||||
def get_natives_platform_info():
|
||||
"""
|
||||
Get platform information that converted to natives supported formula type
|
||||
:return:
|
||||
platform_key_name: str, platform_rule_name: str, platform_arch_type: str
|
||||
"""
|
||||
platform_name, arch, os_version = get_platform_info()
|
||||
|
||||
platform_key_name = NATIVE_OS_KEY_MAP.get(platform_name, None)
|
||||
platform_rule_name = NATIVE_OS_RULE_MAP.get(platform_name, None)
|
||||
platform_arch_type = NATIVE_ARCH_KEY_MAP.get(arch, None)
|
||||
|
||||
return platform_key_name, platform_rule_name, platform_arch_type
|
||||
|
||||
def is_native_allowed(lib_name, platform_key_name, platform_arch_type, unspecified_arch_policy: str="x86_64") -> bool:
|
||||
# Parse native key
|
||||
match = re.search(r":(natives-[^:]+)$", lib_name)
|
||||
|
||||
if match:
|
||||
native_key = match.group(1)
|
||||
logger.debug(f"Found native key {native_key} in {lib_name}")
|
||||
|
||||
try:
|
||||
parts = native_key.split("-")
|
||||
|
||||
if len(parts) == 2: # Only os name exists
|
||||
native_os_name = parts[1]
|
||||
if unspecified_arch_policy == "universal":
|
||||
arch_type = platform_arch_type
|
||||
logger.debug(f"This native may for platform \"{native_os_name}\" (\"May\" for all architecture type)\n")
|
||||
else:
|
||||
arch_type = unspecified_arch_policy
|
||||
logger.debug(f"This native may for platform \"{native_os_name}\" and specific architecture type {unspecified_arch_policy}\n")
|
||||
|
||||
elif len(parts) == 3: # With architecture type
|
||||
native_os_name, arch_type = parts[1], parts[2]
|
||||
logger.debug(f"This native may for platform \"{native_os_name}\" and architecture type \"{arch_type}\"\n")
|
||||
else:
|
||||
logger.warning("Unsupported native key '{}'".format(native_key))
|
||||
return False
|
||||
|
||||
arch_type_converted = NATIVE_ARCH_KEY_MAP.get(arch_type, arch_type)
|
||||
|
||||
if platform_key_name == native_os_name and arch_type_converted == platform_arch_type:
|
||||
return True
|
||||
|
||||
except ValueError:
|
||||
logger.warning(
|
||||
"Unsupported native key '{}'".format(native_key)
|
||||
)
|
||||
|
||||
return False
|
||||
|
||||
def is_legacy_native_allowed(natives, classifiers, platform_rule_name, platform_arch_type, unspecified_arch_policy: str="x86_64") -> tuple[bool, dict | None]:
|
||||
classifier = natives.get(platform_rule_name)
|
||||
|
||||
if classifier:
|
||||
if "${arch}" in classifier:
|
||||
legacy_arch = NATIVE_NEW_TO_OLD_ARCH.get(platform_arch_type)
|
||||
|
||||
if legacy_arch is None:
|
||||
logger.warning(
|
||||
f"No legacy architecture mapping for {platform_arch_type}"
|
||||
)
|
||||
classifier = None
|
||||
else:
|
||||
classifier = classifier.replace("${arch}", legacy_arch)
|
||||
|
||||
if classifier:
|
||||
native_artifact = classifiers.get(classifier)
|
||||
|
||||
if native_artifact:
|
||||
return True, native_artifact
|
||||
|
||||
# classifiers-rule use NATIVE_OS_RULE_MAP
|
||||
if classifiers:
|
||||
logger.debug(
|
||||
f"\nClassifiers: {json.dumps(classifiers, indent=4)}\n"
|
||||
)
|
||||
|
||||
for native in classifiers.keys():
|
||||
data_raw = classifiers[native]
|
||||
data = json.dumps(classifiers[native], indent=4)
|
||||
logger.debug(f"Contains native: {data}\n")
|
||||
|
||||
parts = native.split("-")
|
||||
|
||||
universal = False
|
||||
|
||||
if len(parts) == 2:
|
||||
native_os_name = parts[1].lower()
|
||||
if unspecified_arch_policy == "universal":
|
||||
universal = True
|
||||
arch_type = platform_arch_type
|
||||
logger.debug(f"This native may for platform \"{native_os_name}\" (\"May\" for all architecture type)\n")
|
||||
else:
|
||||
arch_type = unspecified_arch_policy
|
||||
logger.debug(f"This native may for platform \"{native_os_name}\" and specific architecture type {unspecified_arch_policy}\n")
|
||||
elif len(parts) == 3:
|
||||
native_os_name, arch_type = parts[1].lower(), parts[2].lower()
|
||||
logger.debug(f"This native may for platform \"{native_os_name}\" and architecture type \"{arch_type}\"\n")
|
||||
else:
|
||||
logger.warning("Unsupported legacy native key '{}'".format(native))
|
||||
continue
|
||||
|
||||
arch_type_converted = NATIVE_OLD_TO_NEW_ARCH.get(str(arch_type), None) if not universal else None
|
||||
if not universal and arch_type_converted != platform_arch_type:
|
||||
logger.warning("Unsupported legacy native architecture '{}'".format(native))
|
||||
continue
|
||||
|
||||
if native_os_name == platform_rule_name and (arch_type_converted == platform_arch_type or universal):
|
||||
return True, data_raw
|
||||
|
||||
return False, None
|
||||
@@ -3,6 +3,56 @@ import logging
|
||||
import requests
|
||||
|
||||
MOJANG_VERSION_MANIFEST_V2 = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json"
|
||||
MOJANG_LIBRARIES_ENDPOINT = "https://libraries.minecraft.net/"
|
||||
|
||||
NATIVE_OS_KEY_MAP = {
|
||||
"darwin": "macos",
|
||||
"macos": "macos",
|
||||
"osx": "macos",
|
||||
"linux": "linux",
|
||||
"windows": "windows",
|
||||
}
|
||||
|
||||
NATIVE_OS_RULE_MAP = {
|
||||
"darwin": "osx",
|
||||
"macos": "osx",
|
||||
"osx": "osx",
|
||||
"linux": "linux",
|
||||
"windows": "windows",
|
||||
}
|
||||
|
||||
NATIVE_ARCH_KEY_MAP = {
|
||||
"x86": "x86",
|
||||
"i386": "x86",
|
||||
"i486": "x86",
|
||||
"i586": "x86",
|
||||
"i686": "x86",
|
||||
|
||||
"x86_64": "x86_64",
|
||||
"amd64": "x86_64",
|
||||
|
||||
"aarch64": "arm64",
|
||||
"arm64": "arm64",
|
||||
"aarch_64": "arm64",
|
||||
}
|
||||
|
||||
NATIVE_OLD_TO_NEW_ARCH = {
|
||||
"32": "x86",
|
||||
"64": "x86_64",
|
||||
|
||||
"x86": "x86",
|
||||
"x86_64": "x86_64",
|
||||
"amd64": "x86_64",
|
||||
|
||||
"aarch64": "arm64",
|
||||
"arm64": "arm64",
|
||||
}
|
||||
|
||||
NATIVE_NEW_TO_OLD_ARCH = {
|
||||
"x86": "32",
|
||||
"x86_64": "64",
|
||||
}
|
||||
|
||||
|
||||
logger = logging.getLogger("Launcher.CoreLib")
|
||||
|
||||
@@ -43,18 +93,60 @@ def get_latest_version(version_manifest: dict, is_snapshot=False) -> None | str:
|
||||
|
||||
return latest_ver
|
||||
|
||||
def get_all_versions(version_manifest: dict, is_snapshot=False,
|
||||
include_all_type=False, specific_type=None) -> list:
|
||||
all_vers = version_manifest.get("versions", [])
|
||||
|
||||
if len(all_vers) == 0:
|
||||
logger.debug("No version section available in version manifest.")
|
||||
return []
|
||||
|
||||
if not specific_type:
|
||||
target_type = "release" if not is_snapshot else "snapshot"
|
||||
else:
|
||||
target_type = specific_type
|
||||
|
||||
filtered = []
|
||||
|
||||
for ver in all_vers:
|
||||
ver_id = ver.get("id", None)
|
||||
ver_type = ver.get("type")
|
||||
|
||||
logger.debug("Ver: {}, Type: {}".format(ver_id, ver_type))
|
||||
|
||||
if ver_type == target_type or include_all_type:
|
||||
filtered.append(ver)
|
||||
|
||||
if len(filtered) == 0:
|
||||
logger.debug("No version matches specified type: {}".format(target_type))
|
||||
|
||||
return filtered
|
||||
|
||||
def get_available_version_types(version_manifest: dict) -> list:
|
||||
all_vers = version_manifest.get("versions", [])
|
||||
|
||||
if len(all_vers) == 0:
|
||||
logger.debug("No version section available in version manifest.")
|
||||
return []
|
||||
|
||||
types = []
|
||||
|
||||
for ver in all_vers:
|
||||
ver_type = ver.get("type")
|
||||
|
||||
if ver_type not in types:
|
||||
types.append(ver_type)
|
||||
|
||||
return types
|
||||
|
||||
def get_specific_version_manifest_url(version_manifest: dict,
|
||||
version_id: str, is_snapshot=False) -> str | None:
|
||||
version_id: str) -> str | None:
|
||||
versions = version_manifest.get("versions", [])
|
||||
|
||||
if len(versions) == 0:
|
||||
logger.debug("No version section available in version manifest.")
|
||||
return None
|
||||
|
||||
ver_type = "release" if not is_snapshot else "snapshot"
|
||||
versions = [version for version in versions if version.get("type") == ver_type]
|
||||
|
||||
for version in versions:
|
||||
if version.get("id") == version_id:
|
||||
specific_ver_manifest_url = version.get("url", None)
|
||||
@@ -69,6 +161,30 @@ def get_specific_version_manifest_url(version_manifest: dict,
|
||||
|
||||
return None
|
||||
|
||||
def get_specific_version_manifest_url_and_hash(version_manifest: dict,
|
||||
version_id: str) -> tuple[str,str] | tuple[None, None]:
|
||||
versions = version_manifest.get("versions", [])
|
||||
|
||||
if len(versions) == 0:
|
||||
logger.debug("No version section available in version manifest.")
|
||||
return None, None
|
||||
|
||||
for version in versions:
|
||||
if version.get("id") == version_id:
|
||||
specific_ver_manifest_url = version.get("url", None)
|
||||
specific_ver_manifest_hash = version.get("hash", None)
|
||||
|
||||
if specific_ver_manifest_url is None:
|
||||
logger.debug("Specific version manifest URL not available in version manifest.")
|
||||
|
||||
if specific_ver_manifest_hash is None:
|
||||
logger.debug("Specific version manifest hash not available in version manifest.")
|
||||
|
||||
return specific_ver_manifest_url, specific_ver_manifest_hash
|
||||
|
||||
logger.debug("No version section available in version manifest.")
|
||||
|
||||
return None, None
|
||||
|
||||
def fetch_specific_version_manifest(version_manifest: dict, spec_version: str) -> dict | None:
|
||||
ver_url = get_specific_version_manifest_url(version_manifest, spec_version)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
from PySide6.QtWidgets import QMessageBox, QMainWindow, QWidget
|
||||
|
||||
def create_messagebox(parent: QWidget, title: str, message: str,
|
||||
icon: QMessageBox.Icon = QMessageBox.Icon.Information,
|
||||
button: QMessageBox.StandardButton = QMessageBox.StandardButton.Ok) -> QMessageBox:
|
||||
msg = QMessageBox(parent)
|
||||
msg.setWindowTitle(title)
|
||||
msg.setText(message)
|
||||
msg.setIcon(icon)
|
||||
msg.setStandardButtons(button)
|
||||
return msg
|
||||
|
||||
|
||||
def error(parent: QWidget, title: str, message: str,
|
||||
button: QMessageBox.StandardButton = QMessageBox.StandardButton.Ok) -> None:
|
||||
create_messagebox(
|
||||
parent,
|
||||
title=title,
|
||||
message=message,
|
||||
icon=QMessageBox.Icon.Critical,
|
||||
button=button
|
||||
).exec()
|
||||
|
||||
|
||||
def info(parent: QWidget, title: str, message: str,
|
||||
button: QMessageBox.StandardButton = QMessageBox.StandardButton.Ok) -> None:
|
||||
create_messagebox(
|
||||
parent,
|
||||
title=title,
|
||||
message=message,
|
||||
icon=QMessageBox.Icon.Information,
|
||||
button=button
|
||||
).exec()
|
||||
|
||||
|
||||
def warning(parent: QWidget, title: str, message: str,
|
||||
button: QMessageBox.StandardButton = QMessageBox.StandardButton.Ok) -> None:
|
||||
create_messagebox(
|
||||
parent,
|
||||
title=title,
|
||||
message=message,
|
||||
icon=QMessageBox.Icon.Warning,
|
||||
button=button
|
||||
).exec()
|
||||
Reference in New Issue
Block a user