Fix an issue that causes create symlink failed when installing java on unix-like platforms.

This commit is contained in:
wei committed 2026-10-18 21:42:54 +08:00
1 parent 8bde4ef753
commit 1754b01230
15 files changed
+600 -77

No files matched your search

+66 -29
View File
@@ -4,10 +4,18 @@ import re
import shlex
from core_lib.common.common import get_platform_info
from core_lib.common.exception import LauncherException
from core_lib.game.library import get_natives_platform_info
logger = logging.getLogger("Launcher.CoreLib")
def compare_versions(left: str, right: str) -> int:
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 is_argument_allowed(argument: dict | str, allow_features: dict | None = None) -> bool:
"""
@@ -44,6 +52,7 @@ def is_argument_allowed(argument: dict | str, allow_features: dict | None = None
feature_rule = rule.get("features") or {}
rule_arch = os_rule.get("arch")
rule_version = os_rule.get("version")
rule_version_range = os_rule.get("versionRange")
# Check platform
if rule_os is not None and platform_rule_name != rule_os:
@@ -69,6 +78,18 @@ def is_argument_allowed(argument: dict | str, allow_features: dict | None = None
)
continue
# Check version range
if rule_version_range is not None:
min_value = rule_version_range.get("min")
max_value = rule_version_range.get("max")
try:
if min_value is not None and compare_versions(platform_version, min_value) < 0:
continue
if max_value is not None and compare_versions(platform_version, max_value) > 0:
continue
except ValueError:
raise LauncherException("Unable to compare versions: SYS_VER: {} MIN: {} MAX: {}".format(platform_version, min_value, max_value))
# Check feature rule
if any(allow_features.get(key, False) != expected for key, expected in feature_rule.items()):
@@ -98,7 +119,8 @@ class ArgumentMappings:
auth_access_token="", clientid=None, auth_xuid=None,
user_type=None, user_properties=None, version_type=None, natives_directory=None,
launcher_name=None, launcher_version=None,
classpath=None, classpath_separator=None, library_directory=None):
classpath=None, classpath_separator=None, library_directory=None,
resolution: tuple[int, int] | None = None):
self.player_name = player_name
self.version_name = version_name
self.game_directory = game_directory
@@ -111,6 +133,7 @@ class ArgumentMappings:
self.auth_xuid = auth_xuid
self.user_type = user_type
self.user_properties = user_properties
self.resolution = resolution
if isinstance(user_properties, dict):
self.user_properties = json.dumps(
user_properties,
@@ -194,24 +217,8 @@ def find_game_and_jvm_args(arguments: dict | str) -> tuple[list, list]:
return game_args, jvm_args
REMAPPING_ARGUMENTS = {
# For some (I don't know) reason, in 26.2, Mojang include some
"-Djava.library.path=${natives_directory}/java": "-Djava.library.path=${natives_directory}"
}
def hard_remapping_special_arguments(argument: str) -> str:
for arg in REMAPPING_ARGUMENTS:
new_value = REMAPPING_ARGUMENTS[arg]
if arg == argument:
return new_value
return argument
def parse_and_generate_arguments(arguments: list | str, mappings: ArgumentMappings, features: dict | None = None) -> \
list[str]:
list[str]:
if features is None:
features = {}
@@ -228,13 +235,8 @@ list[str]:
if not is_argument_allowed(argument, allow_features=features):
continue
# Replace some special arguments
new_argument = hard_remapping_special_arguments(argument)
# Each argument (section) may contain multiple items, convert it to list before replace placeholder
converted = convert_argument_to_multiple_string(new_argument)
for item in converted:
for item in convert_argument_to_multiple_string(argument):
parsed = replace_placeholders(item, placeholders)
parsed_arguments.append(parsed)
@@ -263,24 +265,59 @@ def append_missing_necessary_jvm_arguments(arguments: list, arg_maps: ArgumentMa
return result
def add_feature_arguments(arguments: dict, arg_maps: ArgumentMappings, features: dict | None = None) -> list[str]:
result = []
if features is None:
return result
if features.get("has_custom_resolution"):
result.append(f"-Dminecraft.overrideResolution={features['resolution']}")
return result
def generate_launch_command(arg_maps: ArgumentMappings, main_class: str, java_path: str, full_args: dict | str
, features: dict | None = None) -> list[str]:
, features: dict | None = None, user_jvm_args: list[str] | None | str = None) -> list[str]:
cmd = [java_path]
game_args, jvm_args = find_game_and_jvm_args(full_args)
parsed_jvm_args = parse_and_generate_arguments(
if isinstance(user_jvm_args, str):
user_jvm_args = shlex.split(user_jvm_args)
if user_jvm_args is None:
# Use manifest recommended user jvm arguments
default_args = (
full_args.get("default-user-jvm", [])
if isinstance(full_args, dict)
else []
)
parsed_user_jvm_args = parse_and_generate_arguments(
default_args,
arg_maps,
features=features,
)
else:
# Use jvm arguments that provided by user
parsed_user_jvm_args = list(user_jvm_args)
parsed_manifest_jvm_args = parse_and_generate_arguments(
jvm_args,
arg_maps,
features=features
features=features,
)
parsed_game_args = parse_and_generate_arguments(
game_args,
arg_maps,
features=features
features=features,
)
# Legacy Minecraft don't have jvm arguments exist in the version library. We need to generate by ourselves
parsed_jvm_args = append_missing_necessary_jvm_arguments(parsed_jvm_args, arg_maps)
parsed_jvm_args = append_missing_necessary_jvm_arguments(
parsed_user_jvm_args + parsed_manifest_jvm_args, # Combine manifest and user jvm arguments together
arg_maps,
)
cmd.extend(parsed_jvm_args)
cmd.append(main_class)
+55 -7
View File
@@ -1,9 +1,10 @@
import logging
import re
import zipfile
from pathlib import Path
from pathlib import Path, PureWindowsPath
from typing import Callable
from .argument import convert_argument_to_multiple_string, find_game_and_jvm_args, is_argument_allowed
from .library import get_natives_platform_info, is_library_allowed, generate_formatted_artifact_with_name, \
is_native_allowed, is_legacy_native_allowed, remove_duplicate_artifacts
from .libraryx import normalize_library_to_common_struct
@@ -64,6 +65,47 @@ def collect_library_artifacts(specific_version_manifest):
return items
def locate_natives_extract_dir(jvm_args: list[str | dict], default_extract_dir: Path,
features: dict | None = None) -> Path:
property_prefix = "-Djava.library.path="
placeholder = "${natives_directory}"
path_value = None
for argument in jvm_args:
if not is_argument_allowed(argument, allow_features=features):
continue
for arg in convert_argument_to_multiple_string(argument):
if arg.startswith(property_prefix):
path_value = arg[len(property_prefix):]
if path_value is None or path_value == placeholder:
return default_extract_dir
if not path_value.startswith(placeholder + "/") and not path_value.startswith(placeholder + "\\"):
raise ValueError("Unsupported java.library.path extraction layout")
suffix = path_value[len(placeholder) + 1:].replace("\\", "/")
parts = suffix.split("/")
if (
any(part in ("", ".", "..") for part in parts)
or any(char in suffix for char in ":;${}")
or PureWindowsPath(suffix).drive
):
raise ValueError("Invalid native extraction subdirectory")
root = default_extract_dir.resolve()
destination = root.joinpath(*parts).resolve()
if not destination.is_relative_to(root):
raise ValueError("Native extraction directory escapes its root")
return destination
def locate_natives_extract_dir_by_manifest(manifest: dict, default_extract_dir: Path,
features: dict | None = None) -> Path:
_, jvm = find_game_and_jvm_args(manifest.get("arguments") or {})
return locate_natives_extract_dir(jvm, default_extract_dir, features=features)
def collect_native_artifacts(specific_version_manifest):
"""
@@ -344,7 +386,9 @@ def check_libraries_exists(manifest: dict, libraries_dir: Path):
return not_found
def check_natives_exists(manifest: dict, libraries_dir: Path, natives_dir: Path):
def check_natives_exists(manifest: dict, libraries_dir: Path, natives_dir: Path, *,
features: dict | None = None):
extract_dir = locate_natives_extract_dir_by_manifest(manifest, natives_dir, features=features)
filtered = []
# Convert current platform and architecture type to same formula
@@ -464,17 +508,19 @@ def check_natives_exists(manifest: dict, libraries_dir: Path, natives_dir: Path)
return False
for target in expected:
if not (natives_dir / target).exists():
if not (extract_dir / target).is_file():
return False
return True
def check_full_libraries_exists(manifest: dict, libraries_dir: Path, natives_dir: Path) -> bool:
def check_full_libraries_exists(manifest: dict, libraries_dir: Path, natives_dir: Path, *,
features: dict | None = None) -> bool:
natives_exist = check_natives_exists(
manifest,
libraries_dir,
natives_dir,
features=features,
)
missing_libraries = check_libraries_exists(
manifest,
@@ -555,10 +601,12 @@ def generate_classpath(manifest: dict, libraries_dir: Path, core_file_path: Path
return f"{separator}".join(classes)
def download_full_libraries(manifest: dict, libraries_dir: Path, natives_dir: Path,
def download_full_libraries(manifest: dict, libraries_dir: Path, default_natives_dir: Path,
progress_callback: Callable[[str, float], None] = None,
redownload_callback: Callable[
[list[FileObject]], tuple[bool, list[FileObject]]] = None) -> None:
[list[FileObject]], tuple[bool, list[FileObject]]] = None, *,
features: dict | None = None) -> None:
natives_dir = locate_natives_extract_dir_by_manifest(manifest, default_natives_dir, features=features)
# libraries
libraries = collect_library_artifacts(manifest)
@@ -593,4 +641,4 @@ def download_full_libraries(manifest: dict, libraries_dir: Path, natives_dir: Pa
else:
raise DependencyException(
"Certain libraries download failed. Unable to continue unzip process.\n"
)
)