Skip to content

Commit 0a66863

Browse files
authored
重构 TerracottaMetadata (#4718)
1 parent d90df22 commit 0a66863

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaMetadata.java

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.util.Collections;
4545
import java.util.List;
4646
import java.util.Map;
47-
import java.util.Objects;
4847
import java.util.regex.Pattern;
4948

5049
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
@@ -66,20 +65,22 @@ private record Config(
6665
@SerializedName("downloads_CN") List<String> downloadsCN,
6766
@SerializedName("links") List<Link> links
6867
) {
69-
private TerracottaNative of(String classifier) {
68+
private @Nullable TerracottaNative of(String classifier) {
69+
String hash = this.classifiers.get(classifier);
70+
if (hash == null)
71+
return null;
72+
73+
if (!hash.startsWith("sha256:"))
74+
throw new IllegalArgumentException(String.format("Invalid hash value %s for classifier %s.", hash, classifier));
75+
hash = hash.substring("sha256:".length());
76+
7077
List<URI> links = new ArrayList<>(this.downloads.size() + this.downloadsCN.size());
7178
for (String download : LocaleUtils.IS_CHINA_MAINLAND
7279
? Lang.merge(this.downloadsCN, this.downloads)
7380
: Lang.merge(this.downloads, this.downloadsCN)) {
7481
links.add(URI.create(download.replace("${version}", this.latest).replace("${classifier}", classifier)));
7582
}
7683

77-
String hash = Objects.requireNonNull(this.classifiers.get(classifier), String.format("Classifier %s doesn't exist.", classifier));
78-
if (!hash.startsWith("sha256:")) {
79-
throw new IllegalArgumentException(String.format("Invalid hash value %s for classifier %s.", hash, classifier));
80-
}
81-
hash = hash.substring("sha256:".length());
82-
8384
return new TerracottaNative(
8485
Collections.unmodifiableList(links),
8586
Metadata.DEPENDENCIES_DIRECTORY.resolve(
@@ -143,36 +144,31 @@ private record ProviderContext(ITerracottaProvider provider, String branch) {
143144

144145
@Nullable
145146
private static ProviderContext locateProvider(Config config) {
146-
String architecture = switch (Architecture.SYSTEM_ARCH) {
147-
case X86_64 -> "x86_64";
148-
case ARM64 -> "arm64";
149-
default -> null;
150-
};
151-
if (architecture == null) {
152-
return null;
153-
}
154-
147+
String arch = Architecture.SYSTEM_ARCH.getCheckedName();
155148
return switch (OperatingSystem.CURRENT_OS) {
156149
case WINDOWS -> {
157-
if (OperatingSystem.SYSTEM_VERSION.isAtLeast(OSVersion.WINDOWS_8_1)) {
158-
yield new ProviderContext(
159-
new GeneralProvider(config.of(String.format("windows-%s.exe", architecture))),
160-
"windows", architecture
161-
);
162-
}
163-
yield null;
150+
if (!OperatingSystem.SYSTEM_VERSION.isAtLeast(OSVersion.WINDOWS_8_1))
151+
yield null;
152+
153+
TerracottaNative target = config.of("windows-%s.exe".formatted(arch));
154+
yield target != null
155+
? new ProviderContext(new GeneralProvider(target), "windows", arch)
156+
: null;
157+
}
158+
case LINUX -> {
159+
TerracottaNative target = config.of("linux-%s".formatted(arch));
160+
yield target != null
161+
? new ProviderContext(new GeneralProvider(target), "linux", arch)
162+
: null;
163+
}
164+
case MACOS -> {
165+
TerracottaNative installer = config.of("macos-%s.pkg".formatted(arch));
166+
TerracottaNative binary = config.of("macos-%s".formatted(arch));
167+
168+
yield installer != null && binary != null
169+
? new ProviderContext(new MacOSProvider(installer, binary), "macos", arch)
170+
: null;
164171
}
165-
case LINUX -> new ProviderContext(
166-
new GeneralProvider(config.of(String.format("linux-%s", architecture))),
167-
"linux", architecture
168-
);
169-
case MACOS -> new ProviderContext(
170-
new MacOSProvider(
171-
config.of(String.format("macos-%s.pkg", architecture)),
172-
config.of(String.format("macos-%s", architecture))
173-
),
174-
"macos", architecture
175-
);
176172
default -> null;
177173
};
178174
}

0 commit comments

Comments
 (0)