重构 TerracottaMetadata (#4718)
This commit is contained in:
@@ -44,7 +44,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
|
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
|
||||||
@@ -66,7 +65,15 @@ public final class TerracottaMetadata {
|
|||||||
@SerializedName("downloads_CN") List<String> downloadsCN,
|
@SerializedName("downloads_CN") List<String> downloadsCN,
|
||||||
@SerializedName("links") List<Link> links
|
@SerializedName("links") List<Link> links
|
||||||
) {
|
) {
|
||||||
private TerracottaNative of(String classifier) {
|
private @Nullable TerracottaNative of(String classifier) {
|
||||||
|
String hash = this.classifiers.get(classifier);
|
||||||
|
if (hash == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (!hash.startsWith("sha256:"))
|
||||||
|
throw new IllegalArgumentException(String.format("Invalid hash value %s for classifier %s.", hash, classifier));
|
||||||
|
hash = hash.substring("sha256:".length());
|
||||||
|
|
||||||
List<URI> links = new ArrayList<>(this.downloads.size() + this.downloadsCN.size());
|
List<URI> links = new ArrayList<>(this.downloads.size() + this.downloadsCN.size());
|
||||||
for (String download : LocaleUtils.IS_CHINA_MAINLAND
|
for (String download : LocaleUtils.IS_CHINA_MAINLAND
|
||||||
? Lang.merge(this.downloadsCN, this.downloads)
|
? Lang.merge(this.downloadsCN, this.downloads)
|
||||||
@@ -74,12 +81,6 @@ public final class TerracottaMetadata {
|
|||||||
links.add(URI.create(download.replace("${version}", this.latest).replace("${classifier}", classifier)));
|
links.add(URI.create(download.replace("${version}", this.latest).replace("${classifier}", classifier)));
|
||||||
}
|
}
|
||||||
|
|
||||||
String hash = Objects.requireNonNull(this.classifiers.get(classifier), String.format("Classifier %s doesn't exist.", classifier));
|
|
||||||
if (!hash.startsWith("sha256:")) {
|
|
||||||
throw new IllegalArgumentException(String.format("Invalid hash value %s for classifier %s.", hash, classifier));
|
|
||||||
}
|
|
||||||
hash = hash.substring("sha256:".length());
|
|
||||||
|
|
||||||
return new TerracottaNative(
|
return new TerracottaNative(
|
||||||
Collections.unmodifiableList(links),
|
Collections.unmodifiableList(links),
|
||||||
Metadata.DEPENDENCIES_DIRECTORY.resolve(
|
Metadata.DEPENDENCIES_DIRECTORY.resolve(
|
||||||
@@ -143,36 +144,31 @@ public final class TerracottaMetadata {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static ProviderContext locateProvider(Config config) {
|
private static ProviderContext locateProvider(Config config) {
|
||||||
String architecture = switch (Architecture.SYSTEM_ARCH) {
|
String arch = Architecture.SYSTEM_ARCH.getCheckedName();
|
||||||
case X86_64 -> "x86_64";
|
|
||||||
case ARM64 -> "arm64";
|
|
||||||
default -> null;
|
|
||||||
};
|
|
||||||
if (architecture == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return switch (OperatingSystem.CURRENT_OS) {
|
return switch (OperatingSystem.CURRENT_OS) {
|
||||||
case WINDOWS -> {
|
case WINDOWS -> {
|
||||||
if (OperatingSystem.SYSTEM_VERSION.isAtLeast(OSVersion.WINDOWS_8_1)) {
|
if (!OperatingSystem.SYSTEM_VERSION.isAtLeast(OSVersion.WINDOWS_8_1))
|
||||||
yield new ProviderContext(
|
yield null;
|
||||||
new GeneralProvider(config.of(String.format("windows-%s.exe", architecture))),
|
|
||||||
"windows", architecture
|
TerracottaNative target = config.of("windows-%s.exe".formatted(arch));
|
||||||
);
|
yield target != null
|
||||||
}
|
? new ProviderContext(new GeneralProvider(target), "windows", arch)
|
||||||
yield null;
|
: null;
|
||||||
|
}
|
||||||
|
case LINUX -> {
|
||||||
|
TerracottaNative target = config.of("linux-%s".formatted(arch));
|
||||||
|
yield target != null
|
||||||
|
? new ProviderContext(new GeneralProvider(target), "linux", arch)
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
case MACOS -> {
|
||||||
|
TerracottaNative installer = config.of("macos-%s.pkg".formatted(arch));
|
||||||
|
TerracottaNative binary = config.of("macos-%s".formatted(arch));
|
||||||
|
|
||||||
|
yield installer != null && binary != null
|
||||||
|
? new ProviderContext(new MacOSProvider(installer, binary), "macos", arch)
|
||||||
|
: null;
|
||||||
}
|
}
|
||||||
case LINUX -> new ProviderContext(
|
|
||||||
new GeneralProvider(config.of(String.format("linux-%s", architecture))),
|
|
||||||
"linux", architecture
|
|
||||||
);
|
|
||||||
case MACOS -> new ProviderContext(
|
|
||||||
new MacOSProvider(
|
|
||||||
config.of(String.format("macos-%s.pkg", architecture)),
|
|
||||||
config.of(String.format("macos-%s", architecture))
|
|
||||||
),
|
|
||||||
"macos", architecture
|
|
||||||
);
|
|
||||||
default -> null;
|
default -> null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user