From 3e7df2664fcd258de405330ce62f79e562391663 Mon Sep 17 00:00:00 2001 From: Geoff Bourne Date: Mon, 15 May 2023 17:56:49 -0500 Subject: [PATCH] modrinth: unified version ID, name, number selection --- .../InstallModrinthModpackCommand.java | 17 +++-- .../helpers/modrinth/ModrinthApiClient.java | 5 +- .../me/itzg/helpers/modrinth/ProjectRef.java | 72 ++++++++++++------- .../itzg/helpers/modrinth/model/Version.java | 2 +- 4 files changed, 63 insertions(+), 33 deletions(-) diff --git a/src/main/java/me/itzg/helpers/modrinth/InstallModrinthModpackCommand.java b/src/main/java/me/itzg/helpers/modrinth/InstallModrinthModpackCommand.java index 9a49b31b..5c0d0a4e 100644 --- a/src/main/java/me/itzg/helpers/modrinth/InstallModrinthModpackCommand.java +++ b/src/main/java/me/itzg/helpers/modrinth/InstallModrinthModpackCommand.java @@ -63,8 +63,11 @@ public class InstallModrinthModpackCommand implements Callable { ) String modpackProject; - @Option(names = "--version-id", description = "Version ID (not name) from the file's metadata") - String versionId; + @Option(names = {"--version-id", "--version"}, + description = "Version ID, name, or number from the file's metadata" + + "%nDefault chooses newest file based on game version, loader, and/or default version type" + ) + String version; @Option(names = "--game-version", description = "Applicable Minecraft version" + "%nDefault: (any)") @@ -109,12 +112,12 @@ public Integer call() throws Exception { final Matcher m = MODPACK_PAGE_URL.matcher(modpackProject); if (m.matches()) { final String versionName = m.group("versionName"); - if (versionName != null && versionId != null) { - throw new InvalidParameterException("Cannot provide both project file URL and version ID"); + if (versionName != null && version != null) { + throw new InvalidParameterException("Cannot provide both project file URL and version"); } - projectRef = new ProjectRef(m.group("slug"), versionId, versionName); + projectRef = new ProjectRef(m.group("slug"), versionName != null ? versionName : version); } else { - projectRef = new ProjectRef(modpackProject, versionId, null); + projectRef = new ProjectRef(modpackProject, version); } final ModrinthModpackManifest newManifest = @@ -150,7 +153,7 @@ private ModrinthModpackManifest processModpack(ProjectRef projectRef, ModrinthMo .filter(version -> needsInstall(prevManifest, project, version)) .flatMap(version -> { final VersionFile versionFile = pickVersionFile(version); - log.debug("Picked versionFile={} from version={}", versionFile, version); + log.info("Installing version {} of {}", version.getVersionNumber(), project.getTitle()); return apiClient.downloadMrPack(versionFile) .publishOn(Schedulers.boundedElastic()) .flatMap(zipPath -> diff --git a/src/main/java/me/itzg/helpers/modrinth/ModrinthApiClient.java b/src/main/java/me/itzg/helpers/modrinth/ModrinthApiClient.java index 70bc5793..f2011fd5 100644 --- a/src/main/java/me/itzg/helpers/modrinth/ModrinthApiClient.java +++ b/src/main/java/me/itzg/helpers/modrinth/ModrinthApiClient.java @@ -66,7 +66,10 @@ public Mono resolveProjectVersion(Project project, ProjectRef projectRe return getVersionsForProject(project.getId(), loader, gameVersion) .flatMap(versions -> Mono.justOrEmpty(versions.stream() - .filter(version -> version.getName().equals(projectRef.getVersionName())) + .filter(version -> + version.getVersionNumber().equals(projectRef.getVersionName()) + || version.getName().equals(projectRef.getVersionName()) + ) .findFirst() )); } diff --git a/src/main/java/me/itzg/helpers/modrinth/ProjectRef.java b/src/main/java/me/itzg/helpers/modrinth/ProjectRef.java index 57227ede..b922cf6b 100644 --- a/src/main/java/me/itzg/helpers/modrinth/ProjectRef.java +++ b/src/main/java/me/itzg/helpers/modrinth/ProjectRef.java @@ -4,11 +4,18 @@ import lombok.ToString; import me.itzg.helpers.modrinth.model.VersionType; +import java.util.regex.Pattern; + @ToString public class ProjectRef { + private static final Pattern VERSIONS = Pattern.compile("[a-zA-z0-9]{8}"); + @Getter final String idOrSlug; - final String versionIdOrType; + @Getter + final VersionType versionType; + @Getter + final String versionId; @Getter final String versionName; @@ -16,43 +23,60 @@ public static ProjectRef parse(String projectRef) { final String[] projectRefParts = projectRef.split(":", 2); return new ProjectRef(projectRefParts[0], - projectRefParts.length > 1 ? projectRefParts[1] : null, - null + projectRefParts.length > 1 ? projectRefParts[1] : null ); } - public ProjectRef(String projectSlug, String versionIdOrType, String versionName) { + /** + * + * @param version can be a {@link VersionType}, ID, or name/number + */ + public ProjectRef(String projectSlug, String version) { this.idOrSlug = projectSlug; - this.versionIdOrType = versionIdOrType; - this.versionName = versionName; + this.versionType = parseVersionType(version); + if (this.versionType == null) { + if (isVersionId(version)) { + this.versionId = version; + this.versionName = null; + } + else { + this.versionId = null; + this.versionName = version; + } + } + else { + this.versionId = null; + this.versionName = null; + } } - public boolean hasVersionName() { - return versionName != null; + private boolean isVersionId(String version) { + if (version == null) { + return false; + } + return VERSIONS.matcher(version).matches(); } - public boolean hasVersionType() { - if (versionIdOrType != null) { - try { - VersionType.valueOf(versionIdOrType); - return true; - } catch (IllegalArgumentException e) { - return false; - } + private VersionType parseVersionType(String version) { + if (version == null) { + return null; + } + try { + return VersionType.valueOf(version); + } catch (IllegalArgumentException e) { + return null; } - return false; } - public VersionType getVersionType() { - return VersionType.valueOf(versionIdOrType); + public boolean hasVersionName() { + return versionName != null; } - public boolean hasVersionId() { - return versionIdOrType != null && - !hasVersionType(); + public boolean hasVersionType() { + return versionType != null; } - public String getVersionId() { - return versionIdOrType; + public boolean hasVersionId() { + return versionId != null; } } diff --git a/src/main/java/me/itzg/helpers/modrinth/model/Version.java b/src/main/java/me/itzg/helpers/modrinth/model/Version.java index ba3b052a..d8c51178 100644 --- a/src/main/java/me/itzg/helpers/modrinth/model/Version.java +++ b/src/main/java/me/itzg/helpers/modrinth/model/Version.java @@ -18,7 +18,7 @@ public class Version { Instant datePublished; - String version; + String versionNumber; VersionType versionType;