diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/meta/ForgeNewModMetadata.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/meta/ForgeNewModMetadata.java index 6b7ff0085b4..df64cc32772 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/meta/ForgeNewModMetadata.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/addon/meta/ForgeNewModMetadata.java @@ -56,101 +56,20 @@ import static org.jackhuang.hmcl.util.logging.Logger.LOG; @Immutable -public final class ForgeNewModMetadata { - private final String modLoader; - - private final String loaderVersion; - - private final String logoFile; - - private final String license; - - private final List mods; - - public ForgeNewModMetadata(String modLoader, String loaderVersion, String logoFile, String license, List mods) { - this.modLoader = modLoader; - this.loaderVersion = loaderVersion; - this.logoFile = logoFile; - this.license = license; - this.mods = mods; - } - - public String getModLoader() { - return modLoader; - } - - public String getLoaderVersion() { - return loaderVersion; - } - - public String getLogoFile() { - return logoFile; - } - - public String getLicense() { - return license; - } - - public List getMods() { - return mods; - } - - public static class Mod { - private final String modId; - private final String version; - private final String displayName; - private final String side; - private final String displayURL; - @JsonAdapter(AuthorDeserializer.class) - private final String authors; - private final String description; - private final String logoFile; - - public Mod() { - this("", "", "", "", "", "", "", ""); - } - - public Mod(String modId, String version, String displayName, String side, String displayURL, String authors, String description, String logoFile) { - this.modId = modId; - this.version = version; - this.displayName = displayName; - this.side = side; - this.displayURL = displayURL; - this.authors = authors; - this.description = description; - this.logoFile = logoFile; - } - - public String getModId() { - return modId; - } - - public String getVersion() { - return version; - } - - public String getDisplayName() { - return displayName; - } - - public String getSide() { - return side; - } - - public String getDisplayURL() { - return displayURL; - } - - public String getAuthors() { - return authors; - } - - public String getDescription() { - return description; - } - - public String getLogoFile() { - return logoFile; +public record ForgeNewModMetadata(String modLoader, String loaderVersion, String logoFile, String license, List mods) { + + public record Mod(String modId, String version, String displayName, String side, String displayURL, + @JsonAdapter(AuthorDeserializer.class) String authors, String description, String logoFile) { + + public Mod { + if (modId == null) modId = ""; + if (version == null) version = ""; + if (displayName == null) displayName = ""; + if (side == null) side = ""; + if (displayURL == null) displayURL = ""; + if (authors == null) authors = ""; + if (description == null) description = ""; + if (logoFile == null) logoFile = ""; } static final class AuthorDeserializer implements JsonDeserializer { @@ -226,9 +145,9 @@ private static LocalModFile fromFile0( throw ioException; } ForgeNewModMetadata metadata = JsonUtils.GSON.fromJson(tomlParseResult.toJson(), ForgeNewModMetadata.class); - if (metadata == null || metadata.getMods().isEmpty()) + if (metadata == null || metadata.mods().isEmpty()) throw new IOException("Mod " + modFile + " `%s` is malformed..".formatted(modToml.getName())); - Mod mod = metadata.getMods().get(0); + Mod mod = metadata.mods().get(0); ZipArchiveEntry manifestMF = tree.getEntry("META-INF/MANIFEST.MF"); String jarVersion = ""; if (manifestMF != null) { @@ -240,13 +159,13 @@ private static LocalModFile fromFile0( } } - ModLoaderType type = analyzeLoader(tomlParseResult, mod.getModId(), modLoaderType); + ModLoaderType type = analyzeLoader(tomlParseResult, mod.modId(), modLoaderType); - String logoPath = StringUtils.isNotBlank(mod.getLogoFile()) ? mod.getLogoFile() : metadata.getLogoFile(); + String logoPath = StringUtils.isNotBlank(mod.logoFile()) ? mod.logoFile() : metadata.logoFile(); - return new LocalModFile(modManager, modManager.getLocalMod(mod.getModId(), type), modFile, mod.getDisplayName(), new LocalAddonFile.Description(mod.getDescription()), - mod.getAuthors(), jarVersion == null ? mod.getVersion() : mod.getVersion().replace("${file.jarVersion}", jarVersion), "", - mod.getDisplayURL(), + return new LocalModFile(modManager, modManager.getLocalMod(mod.modId(), type), modFile, mod.displayName(), new LocalAddonFile.Description(mod.description()), + mod.authors(), jarVersion == null ? mod.version() : mod.version().replace("${file.jarVersion}", jarVersion), "", + mod.displayURL(), logoPath); } @@ -316,7 +235,7 @@ private static ModLoaderType analyzeLoader(TomlParseResult toml, String modID, M try { TomlArray tomlArray = toml.getArray("dependencies." + modID); if (tomlArray != null) { - dependencies = tomlArray.toList().stream().map( o -> ((TomlTable) o).toMap()).toList(); + dependencies = tomlArray.toList().stream().map(o -> ((TomlTable) o).toMap()).toList(); } } catch (ClassCastException ignored) { // https://github.com/HMCL-dev/HMCL/issues/5068 } @@ -325,7 +244,7 @@ private static ModLoaderType analyzeLoader(TomlParseResult toml, String modID, M try { TomlArray tomlArray = toml.getArray("dependencies"); // ??? I have no idea why some of the Forge mods use [[dependencies]] if (tomlArray != null) { - dependencies = tomlArray.toList().stream().map( o -> ((TomlTable) o).toMap()).toList(); + dependencies = tomlArray.toList().stream().map(o -> ((TomlTable) o).toMap()).toList(); } } catch (ClassCastException e) { try { @@ -335,7 +254,7 @@ private static ModLoaderType analyzeLoader(TomlParseResult toml, String modID, M TomlArray tomlArray = table.getArray(modID); if (tomlArray != null) { - dependencies = tomlArray.toList().stream().map( o -> ((TomlTable) o).toMap()).toList(); + dependencies = tomlArray.toList().stream().map(o -> ((TomlTable) o).toMap()).toList(); } } catch (Throwable ignored) { } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeBMCLVersionList.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeBMCLVersionList.java index 14dbcaae3d8..629e7cf6247 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeBMCLVersionList.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeBMCLVersionList.java @@ -91,20 +91,20 @@ public Task refreshAsync(String gameVersion) { continue; List urls = new ArrayList<>(); for (ForgeVersion.File file : version.getFiles()) - if ("installer".equals(file.getCategory()) && "jar".equals(file.getFormat())) { + if ("installer".equals(file.category()) && "jar".equals(file.format())) { String branch = toLookupBranch(gameVersion, version.getBranch()); String classifier = lookupVersion + "-" + version.getVersion() + (branch.isEmpty() ? "" : '-' + branch); - String fileName1 = "forge-" + classifier + "-" + file.getCategory() + "." + file.getFormat(); - String fileName2 = "forge-" + classifier + "-" + lookupVersion + "-" + file.getCategory() + "." + file.getFormat(); + String fileName1 = "forge-" + classifier + "-" + file.category() + "." + file.format(); + String fileName2 = "forge-" + classifier + "-" + lookupVersion + "-" + file.category() + "." + file.format(); urls.add("https://files.minecraftforge.net/maven/net/minecraftforge/forge/" + classifier + "/" + fileName1); urls.add("https://files.minecraftforge.net/maven/net/minecraftforge/forge/" + classifier + "-" + lookupVersion + "/" + fileName2); urls.add(NetworkUtils.withQuery("https://bmclapi2.bangbang93.com/forge/download", mapOf( pair("mcversion", version.getGameVersion()), pair("version", version.getVersion()), pair("branch", branch), - pair("category", file.getCategory()), - pair("format", file.getFormat()) + pair("category", file.category()), + pair("format", file.format()) ))); } @@ -202,31 +202,12 @@ public void validate() throws JsonParseException { } @Immutable - public static final class File { - private final String format; - private final String category; - private final String hash; + public record File(String format, String category, String hash) { - public File() { - this("", "", ""); - } - - public File(String format, String category, String hash) { - this.format = format; - this.category = category; - this.hash = hash; - } - - public String getFormat() { - return format; - } - - public String getCategory() { - return category; - } - - public String getHash() { - return hash; + public File { + if (format == null) format = ""; + if (category == null) category = ""; + if (hash == null) hash = ""; } } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstall.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstall.java index a7a40607998..ffbcd7658dc 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstall.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstall.java @@ -25,68 +25,6 @@ * @author huangyuhui */ @Immutable -public final class ForgeInstall { - - private final String profileName; - private final String target; - private final Artifact path; - private final String version; - private final String filePath; - private final String welcome; - private final String minecraft; - private final String mirrorList; - private final String logo; - - public ForgeInstall() { - this(null, null, null, null, null, null, null, null, null); - } - - public ForgeInstall(String profileName, String target, Artifact path, String version, String filePath, String welcome, String minecraft, String mirrorList, String logo) { - this.profileName = profileName; - this.target = target; - this.path = path; - this.version = version; - this.filePath = filePath; - this.welcome = welcome; - this.minecraft = minecraft; - this.mirrorList = mirrorList; - this.logo = logo; - } - - public String getProfileName() { - return profileName; - } - - public String getTarget() { - return target; - } - - public Artifact getPath() { - return path; - } - - public String getVersion() { - return version; - } - - public String getFilePath() { - return filePath; - } - - public String getWelcome() { - return welcome; - } - - public String getMinecraft() { - return minecraft; - } - - public String getMirrorList() { - return mirrorList; - } - - public String getLogo() { - return logo; - } - +public record ForgeInstall(String profileName, String target, Artifact path, String version, String filePath, + String welcome, String minecraft, String mirrorList, String logo) { } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstallProfile.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstallProfile.java index 2053837ddc5..f93921da2dd 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstallProfile.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstallProfile.java @@ -28,26 +28,8 @@ * @author huangyuhui */ @Immutable -public final class ForgeInstallProfile implements Validation { - - @SerializedName("install") - private final ForgeInstall install; - - @SerializedName("versionInfo") - private final Version versionInfo; - - public ForgeInstallProfile(ForgeInstall install, Version versionInfo) { - this.install = install; - this.versionInfo = versionInfo; - } - - public ForgeInstall getInstall() { - return install; - } - - public Version getVersionInfo() { - return versionInfo; - } +public record ForgeInstallProfile(@SerializedName("install") ForgeInstall install, + @SerializedName("versionInfo") Version versionInfo) implements Validation { @Override public void validate() throws JsonParseException { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstallTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstallTask.java index 922fb75f2aa..e61407e8fd7 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstallTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeInstallTask.java @@ -134,8 +134,8 @@ public static boolean detectForgeInstallerType(DependencyManager dependencyManag return true; } else if (installProfile.containsKey("install") && installProfile.containsKey("versionInfo")) { ForgeInstallProfile profile = JsonUtils.fromNonNullJson(installProfileText, ForgeInstallProfile.class); - if (!gameVersion.get().equals(profile.getInstall().getMinecraft())) - throw new VersionMismatchException(profile.getInstall().getMinecraft(), gameVersion.get()); + if (!gameVersion.get().equals(profile.install().minecraft())) + throw new VersionMismatchException(profile.install().minecraft(), gameVersion.get()); return false; } else { throw new IOException(); @@ -167,9 +167,9 @@ public static Task install(DefaultDependencyManager dependencyManager, return new ForgeNewInstallTask(dependencyManager, version, modifyVersion(gameVersion.get(), profile.getVersion()), installer); } else if (installProfile.containsKey("install") && installProfile.containsKey("versionInfo")) { ForgeInstallProfile profile = JsonUtils.fromNonNullJson(installProfileText, ForgeInstallProfile.class); - if (!gameVersion.get().equals(profile.getInstall().getMinecraft())) - throw new VersionMismatchException(profile.getInstall().getMinecraft(), gameVersion.get()); - return new ForgeOldInstallTask(dependencyManager, version, modifyVersion(gameVersion.get(), profile.getInstall().getPath().getVersion().replaceAll("(?i)forge", "")), installer); + if (!gameVersion.get().equals(profile.install().minecraft())) + throw new VersionMismatchException(profile.install().minecraft(), gameVersion.get()); + return new ForgeOldInstallTask(dependencyManager, version, modifyVersion(gameVersion.get(), profile.install().path().getVersion().replaceAll("(?i)forge", "")), installer); } else { throw new IOException(); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeNewInstallProfile.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeNewInstallProfile.java index d388403cd68..3209f42717b 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeNewInstallProfile.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeNewInstallProfile.java @@ -114,7 +114,7 @@ public Map getData() { if (data == null) return new HashMap<>(); - return data.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getClient())); + return data.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().client())); } @Override @@ -196,22 +196,12 @@ public void validate() throws JsonParseException, TolerableValidationException { } } - public static class Datum { - private final String client; - - public Datum(String client) { - this.client = client; - } - - /** - * Can be in the following formats: - * [value]: An artifact path. - * 'value': A string literal. - * value: A file in the installer package, to be extracted to a temp folder, and then have the absolute path in replacements. - * @return Value to use for the client install - */ - public String getClient() { - return client; - } + /// @param client Value to use for the client install. + /// + /// Can be in the following formats: + /// - \[value]: An artifact path. + /// - 'value': A string literal. + /// - value: A file in the installer package, to be extracted to a temp folder, and then have the absolute path in replacements. + public record Datum(String client) { } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeOldInstallTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeOldInstallTask.java index 61aa3ea3350..9ee03909746 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeOldInstallTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/forge/ForgeOldInstallTask.java @@ -71,21 +71,21 @@ public void execute() throws Exception { ForgeInstallProfile installProfile = JsonUtils.fromNonNullJsonFully(stream, ForgeInstallProfile.class); // unpack the universal jar in the installer file. - Library forgeLibrary = new Library(installProfile.getInstall().getPath()); + Library forgeLibrary = new Library(installProfile.install().path()); Path forgeFile = dependencyManager.getGameRepository().getLibraryFile(version, forgeLibrary); Files.createDirectories(forgeFile.getParent()); - ZipEntry forgeEntry = zipFile.getEntry(installProfile.getInstall().getFilePath()); + ZipEntry forgeEntry = zipFile.getEntry(installProfile.install().filePath()); try (InputStream is = zipFile.getInputStream(forgeEntry); OutputStream os = Files.newOutputStream(forgeFile, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) { is.transferTo(os); } - setResult(installProfile.getVersionInfo() + setResult(installProfile.versionInfo() .setPriority(Version.PRIORITY_LOADER) .setId(LibraryAnalyzer.LibraryType.FORGE.getPatchId()) .setVersion(selfVersion)); - dependencies.add(dependencyManager.checkLibraryCompletionAsync(installProfile.getVersionInfo(), true)); + dependencies.add(dependencyManager.checkLibraryCompletionAsync(installProfile.versionInfo(), true)); } catch (ZipException ex) { throw new ArtifactMalformedException("Malformed forge installer file", ex); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/neoforge/NeoForgeBMCLVersionList.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/neoforge/NeoForgeBMCLVersionList.java index d0466fd5208..9fbf8d6b0ee 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/neoforge/NeoForgeBMCLVersionList.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/neoforge/NeoForgeBMCLVersionList.java @@ -80,19 +80,8 @@ public Task refreshAsync(String gameVersion) { } @Immutable - private static final class NeoForgeVersion implements Validation { - private final String rawVersion; - - private final String version; - - @SerializedName("mcversion") - private final String mcVersion; - - public NeoForgeVersion(String rawVersion, String version, String mcVersion) { - this.rawVersion = rawVersion; - this.version = version; - this.mcVersion = mcVersion; - } + private record NeoForgeVersion(String rawVersion, String version, + @SerializedName("mcversion") String mcVersion) implements Validation { @Override public void validate() throws JsonParseException { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineBMCLVersionList.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineBMCLVersionList.java index 608a1ca0470..6f0cc5cee0b 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineBMCLVersionList.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineBMCLVersionList.java @@ -79,17 +79,17 @@ public Task refreshAsync() { versions.clear(); Set duplicates = new HashSet<>(); for (OptiFineVersion element : root) { - String version = element.getType() + "_" + element.getPatch(); - String mirror = apiRoot + "/optifine/" + toLookupVersion(element.getGameVersion()) + "/" + element.getType() + "/" + element.getPatch(); + String version = element.type() + "_" + element.patch(); + String mirror = apiRoot + "/optifine/" + toLookupVersion(element.gameVersion()) + "/" + element.type() + "/" + element.patch(); if (!duplicates.add(mirror)) continue; - boolean isPre = element.getPatch() != null && (element.getPatch().startsWith("pre") || element.getPatch().startsWith("alpha")); + boolean isPre = element.patch() != null && (element.patch().startsWith("pre") || element.patch().startsWith("alpha")); - if (StringUtils.isBlank(element.getGameVersion())) + if (StringUtils.isBlank(element.gameVersion())) continue; - String gameVersion = VersionNumber.normalize(fromLookupVersion(element.getGameVersion())); + String gameVersion = VersionNumber.normalize(fromLookupVersion(element.gameVersion())); versions.put(gameVersion, new OptiFineRemoteVersion(gameVersion, version, Collections.singletonList(mirror), isPre)); } } finally { @@ -101,69 +101,9 @@ public Task refreshAsync() { /** * @author huangyuhui */ - private static final class OptiFineVersion { - - @SerializedName("dl") - private final String downloadLink; - - @SerializedName("ver") - private final String version; - - @SerializedName("date") - private final String date; - - @SerializedName("type") - private final String type; - - @SerializedName("patch") - private final String patch; - - @SerializedName("mirror") - private final String mirror; - - @SerializedName("mcversion") - private final String gameVersion; - - public OptiFineVersion() { - this(null, null, null, null, null, null, null); - } - - public OptiFineVersion(String downloadLink, String version, String date, String type, String patch, String mirror, String gameVersion) { - this.downloadLink = downloadLink; - this.version = version; - this.date = date; - this.type = type; - this.patch = patch; - this.mirror = mirror; - this.gameVersion = gameVersion; - } - - public String getDownloadLink() { - return downloadLink; - } - - public String getVersion() { - return version; - } - - public String getDate() { - return date; - } - - public String getType() { - return type; - } - - public String getPatch() { - return patch; - } - - public String getMirror() { - return mirror; - } - - public String getGameVersion() { - return gameVersion; - } + private record OptiFineVersion(@SerializedName("dl") String downloadLink, @SerializedName("ver") String version, + @SerializedName("date") String date, @SerializedName("type") String type, + @SerializedName("patch") String patch, @SerializedName("mirror") String mirror, + @SerializedName("mcversion") String gameVersion) { } }