From 6c57c447a9e78e75ab70916d47ffa076757f7434 Mon Sep 17 00:00:00 2001 From: supersaiyansubtlety Date: Mon, 28 Oct 2024 00:45:05 -0700 Subject: [PATCH] replace VersionDownloadInfo with VersionParser --- .../internal/plugin/MinecraftJarsPlugin.java | 26 +++--- .../internal/plugin/TargetDiffPlugin.java | 3 +- ...k.java => VersionParserConsumingTask.java} | 8 +- .../task/setup/DownloadMinecraftJarsTask.java | 30 ++++--- .../setup/DownloadMinecraftLibrariesTask.java | 27 +++++-- .../setup/DownloadVersionsManifestTask.java | 2 +- .../DownloadWantedVersionManifestTask.java | 12 +-- .../internal/util/VersionDownloadInfo.java | 81 ------------------- .../NonSerializableObjectParser.java | 27 +++++++ .../SerializableDownloadableFile.java | 2 +- .../SerializableVersionEntry.java | 2 +- .../util/serializable/VersionParser.java | 24 ++++++ 12 files changed, 117 insertions(+), 127 deletions(-) rename buildSrc/src/main/java/quilt/internal/task/{VersionDownloadInfoConsumingTask.java => VersionParserConsumingTask.java} (50%) delete mode 100644 buildSrc/src/main/java/quilt/internal/util/VersionDownloadInfo.java create mode 100644 buildSrc/src/main/java/quilt/internal/util/serializable/NonSerializableObjectParser.java rename buildSrc/src/main/java/quilt/internal/util/{ => serializable}/SerializableDownloadableFile.java (88%) rename buildSrc/src/main/java/quilt/internal/util/{ => serializable}/SerializableVersionEntry.java (97%) create mode 100644 buildSrc/src/main/java/quilt/internal/util/serializable/VersionParser.java diff --git a/buildSrc/src/main/java/quilt/internal/plugin/MinecraftJarsPlugin.java b/buildSrc/src/main/java/quilt/internal/plugin/MinecraftJarsPlugin.java index 25d2f1a45e..79e4fe9bc9 100644 --- a/buildSrc/src/main/java/quilt/internal/plugin/MinecraftJarsPlugin.java +++ b/buildSrc/src/main/java/quilt/internal/plugin/MinecraftJarsPlugin.java @@ -2,20 +2,21 @@ import org.gradle.api.Project; import org.gradle.api.file.Directory; +import org.gradle.api.provider.Property; import org.gradle.api.provider.Provider; import org.gradle.api.tasks.TaskContainer; import org.gradle.api.tasks.TaskProvider; import org.jetbrains.annotations.NotNull; import quilt.internal.Constants; import quilt.internal.plugin.abstraction.DefaultTaskedMappingsProjectPlugin; -import quilt.internal.task.VersionDownloadInfoConsumingTask; +import quilt.internal.task.VersionParserConsumingTask; import quilt.internal.task.setup.DownloadMinecraftJarsTask; import quilt.internal.task.setup.DownloadMinecraftLibrariesTask; import quilt.internal.task.setup.DownloadVersionsManifestTask; import quilt.internal.task.setup.DownloadWantedVersionManifestTask; import quilt.internal.task.setup.ExtractServerJarTask; import quilt.internal.task.setup.MergeJarsTask; -import quilt.internal.util.VersionDownloadInfo; +import quilt.internal.util.serializable.VersionParser; /** * {@linkplain TaskContainer#register Registers} tasks that download and extract @@ -26,15 +27,14 @@ *
  • {@link TaskContainer#register registers} {@value MergeJarsTask#MERGE_JARS_TASK_NAME} * which merges the client and server jars *
  • {@linkplain org.gradle.api.tasks.TaskCollection#configureEach configures} the default value of - * {@link VersionDownloadInfoConsumingTask}s' - * {@link VersionDownloadInfoConsumingTask#getVersionDownloadInfo versionDownloadInfo} to + * {@link VersionParserConsumingTask}s' + * {@link VersionParserConsumingTask#getVersionParser versionParser} to * {@value DownloadWantedVersionManifestTask#DOWNLOAD_WANTED_VERSION_MANIFEST_TASK_NAME}'s - * {@linkplain DownloadWantedVersionManifestTask#provideVersionDownloadInfo provided} - * {@link VersionDownloadInfo} + * {@linkplain DownloadWantedVersionManifestTask#provideVersionParser provided} + * {@link VersionParser} * */ public abstract class MinecraftJarsPlugin extends DefaultTaskedMappingsProjectPlugin { - @Override protected Tasks applyImpl(@NotNull Project project) { final Provider minecraftDir = this.getMinecraftDir(); @@ -65,13 +65,13 @@ protected Tasks applyImpl(@NotNull Project project) { ); // put mapped provider in a property so all tasks use the same cached value - final Provider versionDownloadInfo = - this.getObjects().property(VersionDownloadInfo.class).convention( - downloadWantedVersionManifest.flatMap(DownloadWantedVersionManifestTask::provideVersionDownloadInfo) - ); + final Property versionParser = this.getObjects().property(VersionParser.class); + versionParser.set( + downloadWantedVersionManifest.flatMap(DownloadWantedVersionManifestTask::provideVersionParser) + ); - tasks.withType(VersionDownloadInfoConsumingTask.class).configureEach(task -> { - task.getVersionDownloadInfo().convention(versionDownloadInfo); + tasks.withType(VersionParserConsumingTask.class).configureEach(task -> { + task.getVersionParser().convention(versionParser); }); } diff --git a/buildSrc/src/main/java/quilt/internal/plugin/TargetDiffPlugin.java b/buildSrc/src/main/java/quilt/internal/plugin/TargetDiffPlugin.java index 1b8768050c..2d94429154 100644 --- a/buildSrc/src/main/java/quilt/internal/plugin/TargetDiffPlugin.java +++ b/buildSrc/src/main/java/quilt/internal/plugin/TargetDiffPlugin.java @@ -109,7 +109,8 @@ public void apply(@NotNull Project project) { ); // put mapped provider in a property so all tasks use the same cached value - final Provider targetVersion = this.getObjects().property(String.class).convention( + final Property targetVersion = this.getObjects().property(String.class); + targetVersion.set( downloadTargetMetaFile.flatMap(DownloadTargetMetaFileTask::provideTargetVersion) ); diff --git a/buildSrc/src/main/java/quilt/internal/task/VersionDownloadInfoConsumingTask.java b/buildSrc/src/main/java/quilt/internal/task/VersionParserConsumingTask.java similarity index 50% rename from buildSrc/src/main/java/quilt/internal/task/VersionDownloadInfoConsumingTask.java rename to buildSrc/src/main/java/quilt/internal/task/VersionParserConsumingTask.java index d927f5da93..0309a7d49f 100644 --- a/buildSrc/src/main/java/quilt/internal/task/VersionDownloadInfoConsumingTask.java +++ b/buildSrc/src/main/java/quilt/internal/task/VersionParserConsumingTask.java @@ -3,14 +3,14 @@ import org.gradle.api.provider.Property; import org.gradle.api.tasks.Input; import quilt.internal.plugin.MinecraftJarsPlugin; -import quilt.internal.util.VersionDownloadInfo; +import quilt.internal.util.serializable.VersionParser; /** - * A task that takes {@link VersionDownloadInfo} as input. + * A task that takes {@link VersionParser} as input. * * @see MinecraftJarsPlugin MinecraftJarsPlugin's configureEach */ -public interface VersionDownloadInfoConsumingTask extends MappingsTask { +public interface VersionParserConsumingTask extends MappingsTask { @Input - Property getVersionDownloadInfo(); + Property getVersionParser(); } diff --git a/buildSrc/src/main/java/quilt/internal/task/setup/DownloadMinecraftJarsTask.java b/buildSrc/src/main/java/quilt/internal/task/setup/DownloadMinecraftJarsTask.java index 5d587505fe..8923e7bf6e 100644 --- a/buildSrc/src/main/java/quilt/internal/task/setup/DownloadMinecraftJarsTask.java +++ b/buildSrc/src/main/java/quilt/internal/task/setup/DownloadMinecraftJarsTask.java @@ -6,23 +6,24 @@ import com.google.common.hash.HashCode; import com.google.common.hash.Hashing; import com.google.common.io.Files; +import org.gradle.api.GradleException; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.TaskAction; +import org.quiltmc.launchermeta.version.v1.DownloadableFile; +import org.quiltmc.launchermeta.version.v1.Downloads; import quilt.internal.Constants.Groups; import quilt.internal.plugin.MinecraftJarsPlugin; import quilt.internal.task.DefaultMappingsTask; -import quilt.internal.task.VersionDownloadInfoConsumingTask; +import quilt.internal.task.VersionParserConsumingTask; import quilt.internal.util.DownloadUtil; -import quilt.internal.util.VersionDownloadInfo; /** - * Downloads the Minecraft client and server jars for the passed {@linkplain #getVersionDownloadInfo version}. + * Downloads the Minecraft client and server jars for the passed {@linkplain #getVersionParser version}. * * @see MinecraftJarsPlugin MinecraftJarsPlugin's configureEach */ -public abstract class DownloadMinecraftJarsTask extends DefaultMappingsTask implements - VersionDownloadInfoConsumingTask { +public abstract class DownloadMinecraftJarsTask extends DefaultMappingsTask implements VersionParserConsumingTask { /** * {@linkplain org.gradle.api.tasks.TaskContainer#register Registered} by * {@link MinecraftJarsPlugin MinecraftJarsPlugin}. @@ -39,7 +40,7 @@ public DownloadMinecraftJarsTask() { super(Groups.SETUP); // TODO I'm not sure that this is necessary - // VersionDownloadInfoConsumingTasks indirectly depend on + // VersionParserConsumingTasks indirectly depend on // DownloadVersionManifestFileTask which has @DisableCachingByDefault. // I'm not sure if gradle considers output files to have been updated when they're overwritten with // the same content. @@ -49,11 +50,11 @@ public DownloadMinecraftJarsTask() { try { final File clientJar = this.getClientJar().get().getAsFile(); final File serverBootstrapJar = this.getServerBootstrapJar().get().getAsFile(); - final VersionDownloadInfo info = this.getVersionDownloadInfo().get(); + final Downloads downloads = this.getVersionParser().get().get().getDownloads(); return clientJar.exists() && serverBootstrapJar.exists() - && validateChecksum(clientJar, info.getClient().getSha1()) - && validateChecksum(serverBootstrapJar, info.getServer().getSha1()); + && validateChecksum(clientJar, downloads.getClient().getSha1()) + && validateChecksum(serverBootstrapJar, getServerOrThrow(downloads).getSha1()); } catch (Exception e) { return false; } @@ -64,23 +65,28 @@ && validateChecksum(clientJar, info.getClient().getSha1()) public void download() { this.getLogger().lifecycle(":downloading minecraft jars"); - final VersionDownloadInfo info = this.getVersionDownloadInfo().get(); + final Downloads downloads = this.getVersionParser().get().get().getDownloads(); DownloadUtil.download( - info.getClient().getUrl(), + downloads.getClient().getUrl(), this.getClientJar().get().getAsFile(), false, this.getLogger() ); DownloadUtil.download( - info.getServer().getUrl(), + getServerOrThrow(downloads).getUrl(), this.getServerBootstrapJar().get().getAsFile(), false, this.getLogger() ); } + private static DownloadableFile getServerOrThrow(Downloads downloads) { + return downloads.getServer() + .orElseThrow(() -> new GradleException("Version has no server download")); + } + @SuppressWarnings("deprecation") private static boolean validateChecksum(File file, String checksum) throws IOException { if (file != null) { diff --git a/buildSrc/src/main/java/quilt/internal/task/setup/DownloadMinecraftLibrariesTask.java b/buildSrc/src/main/java/quilt/internal/task/setup/DownloadMinecraftLibrariesTask.java index 0591283446..01519480d3 100644 --- a/buildSrc/src/main/java/quilt/internal/task/setup/DownloadMinecraftLibrariesTask.java +++ b/buildSrc/src/main/java/quilt/internal/task/setup/DownloadMinecraftLibrariesTask.java @@ -10,13 +10,15 @@ import org.gradle.api.tasks.OutputFiles; import org.gradle.api.tasks.TaskAction; import org.gradle.work.DisableCachingByDefault; +import org.quiltmc.launchermeta.version.v1.DownloadableFile; +import org.quiltmc.launchermeta.version.v1.Version; import quilt.internal.Constants.Groups; import quilt.internal.plugin.MapMinecraftJarsPlugin; import quilt.internal.plugin.MinecraftJarsPlugin; import quilt.internal.task.DefaultMappingsTask; -import quilt.internal.task.VersionDownloadInfoConsumingTask; +import quilt.internal.task.VersionParserConsumingTask; import quilt.internal.util.DownloadUtil; -import quilt.internal.util.VersionDownloadInfo; +import quilt.internal.util.serializable.VersionParser; import javax.inject.Inject; import java.io.File; @@ -25,14 +27,14 @@ import java.util.stream.Collectors; /** - * Downloads the Minecraft's libraries for the passed {@linkplain #getVersionDownloadInfo version}. + * Downloads the Minecraft's libraries for the passed {@linkplain #getVersionParser version}. * * @see MinecraftJarsPlugin MinecraftJarsPlugin's configureEach */ // TODO why? @DisableCachingByDefault(because = "unknown") public abstract class DownloadMinecraftLibrariesTask extends DefaultMappingsTask implements - VersionDownloadInfoConsumingTask { + VersionParserConsumingTask { /** * {@linkplain org.gradle.api.tasks.TaskContainer#register Registered} by * {@link MinecraftJarsPlugin MinecraftJarsPlugin}. @@ -65,7 +67,9 @@ public DownloadMinecraftLibrariesTask() { // put this in a property to cache it final Provider> artifactsByNamedUrl = this.getObjects().mapProperty(NamedUrl.class, RegularFile.class).convention( - this.getVersionDownloadInfo().map(info -> getArtifactsByNamedUrl(info, this.getLibrariesDir().get())) + this.getVersionParser() + .map(VersionParser::get) + .map(version -> getArtifactsByNamedUrl(version, this.getLibrariesDir().get())) ); this.getArtifactsByNamedUrl().convention(artifactsByNamedUrl); @@ -88,8 +92,17 @@ public void download() { ); } - private static Map getArtifactsByNamedUrl(VersionDownloadInfo info, Directory destDir) { - return info.getLibraryArtifactUrlsByName().entrySet().stream() + private static Map getArtifactsByNamedUrl(Version version, Directory destDir) { + return version.getLibraries().stream() + .flatMap(library -> + library.getDownloads().getArtifact() + .map(DownloadableFile.PathDownload::getUrl) + .map(artifact -> Map.entry( + library.getName(), + artifact + )) + .stream() + ) .collect(Collectors.toMap( entry -> new NamedUrl(entry.getKey(), entry.getValue()), entry -> artifactOf(entry.getValue(), destDir) diff --git a/buildSrc/src/main/java/quilt/internal/task/setup/DownloadVersionsManifestTask.java b/buildSrc/src/main/java/quilt/internal/task/setup/DownloadVersionsManifestTask.java index d9a4cf20b6..b0e4d0f27d 100644 --- a/buildSrc/src/main/java/quilt/internal/task/setup/DownloadVersionsManifestTask.java +++ b/buildSrc/src/main/java/quilt/internal/task/setup/DownloadVersionsManifestTask.java @@ -6,7 +6,7 @@ import quilt.internal.Constants.Groups; import quilt.internal.plugin.MinecraftJarsPlugin; import quilt.internal.task.SimpleDownloadTask; -import quilt.internal.util.SerializableVersionEntry; +import quilt.internal.util.serializable.SerializableVersionEntry; @DisableCachingByDefault(because = "Output depends on a remote source that may change.") public abstract class DownloadVersionsManifestTask extends SimpleDownloadTask { diff --git a/buildSrc/src/main/java/quilt/internal/task/setup/DownloadWantedVersionManifestTask.java b/buildSrc/src/main/java/quilt/internal/task/setup/DownloadWantedVersionManifestTask.java index dbe4128dda..added580a1 100644 --- a/buildSrc/src/main/java/quilt/internal/task/setup/DownloadWantedVersionManifestTask.java +++ b/buildSrc/src/main/java/quilt/internal/task/setup/DownloadWantedVersionManifestTask.java @@ -9,9 +9,9 @@ import quilt.internal.Constants.Groups; import quilt.internal.plugin.MinecraftJarsPlugin; import quilt.internal.task.SimpleDownloadTask; -import quilt.internal.task.VersionDownloadInfoConsumingTask; -import quilt.internal.util.SerializableVersionEntry; -import quilt.internal.util.VersionDownloadInfo; +import quilt.internal.task.VersionParserConsumingTask; +import quilt.internal.util.serializable.SerializableVersionEntry; +import quilt.internal.util.serializable.VersionParser; public abstract class DownloadWantedVersionManifestTask extends SimpleDownloadTask { /** @@ -27,13 +27,13 @@ public abstract class DownloadWantedVersionManifestTask extends SimpleDownloadTa public abstract RegularFileProperty getDest(); /** - * @see VersionDownloadInfoConsumingTask + * @see VersionParserConsumingTask * @see MinecraftJarsPlugin */ - public Provider provideVersionDownloadInfo() { + public Provider provideVersionParser() { return this.getDest() .map(RegularFile::getAsFile) - .map(VersionDownloadInfo::of); + .map(VersionParser::new); } public DownloadWantedVersionManifestTask() { diff --git a/buildSrc/src/main/java/quilt/internal/util/VersionDownloadInfo.java b/buildSrc/src/main/java/quilt/internal/util/VersionDownloadInfo.java deleted file mode 100644 index 7fe458942a..0000000000 --- a/buildSrc/src/main/java/quilt/internal/util/VersionDownloadInfo.java +++ /dev/null @@ -1,81 +0,0 @@ -package quilt.internal.util; - -import org.apache.commons.io.FileUtils; -import org.gradle.api.GradleException; -import org.quiltmc.launchermeta.version.v1.DownloadableFile; -import org.quiltmc.launchermeta.version.v1.Downloads; -import org.quiltmc.launchermeta.version.v1.Version; - -import java.io.File; -import java.io.IOException; -import java.io.Serializable; -import java.nio.charset.StandardCharsets; -import java.util.Map; -import java.util.stream.Collectors; - -/** - * Holds download information from a {@link org.quiltmc.launchermeta.version.v1.Version Version} - * so they can be cached and the {@link org.quiltmc.launchermeta.version.v1.Version Version} - * needn't be parsed by multiple {@link org.gradle.api.Task Task}s. - */ -// TODO consider replacing this with a NonSerializableObjectParser class -public final class VersionDownloadInfo implements Serializable { - public static VersionDownloadInfo of(File versionFile) { - final Version version; - try { - version = Version.fromString(FileUtils.readFileToString(versionFile, StandardCharsets.UTF_8)); - } catch (IOException e) { - throw new GradleException("Failed to read version file", e); - } - - final Downloads downloads = version.getDownloads(); - - final var client = new SerializableDownloadableFile(downloads.getClient()); - - final var server = new SerializableDownloadableFile(downloads.getServer().orElseThrow(() -> - new GradleException("Version has no server download") - )); - - final Map libraryArtifactUrlsByName = version.getLibraries().stream() - .flatMap(library -> - library.getDownloads().getArtifact() - .map(DownloadableFile.PathDownload::getUrl) - .map(artifact -> Map.entry( - library.getName(), - artifact - )) - .stream() - ) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); - - return new VersionDownloadInfo(client, server, libraryArtifactUrlsByName); - } - - private final SerializableDownloadableFile client; - - private final SerializableDownloadableFile server; - - private final Map libraryArtifactUrlsByName; - - public SerializableDownloadableFile getClient() { - return this.client; - } - - public SerializableDownloadableFile getServer() { - return this.server; - } - - public Map getLibraryArtifactUrlsByName() { - return this.libraryArtifactUrlsByName; - } - - private VersionDownloadInfo( - SerializableDownloadableFile client, - SerializableDownloadableFile server, - Map libraryArtifactUrlsByName - ) { - this.client = client; - this.server = server; - this.libraryArtifactUrlsByName = libraryArtifactUrlsByName; - } -} diff --git a/buildSrc/src/main/java/quilt/internal/util/serializable/NonSerializableObjectParser.java b/buildSrc/src/main/java/quilt/internal/util/serializable/NonSerializableObjectParser.java new file mode 100644 index 0000000000..0c12b1c059 --- /dev/null +++ b/buildSrc/src/main/java/quilt/internal/util/serializable/NonSerializableObjectParser.java @@ -0,0 +1,27 @@ +package quilt.internal.util.serializable; + +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.io.Serializable; + +public abstract class NonSerializableObjectParser implements Serializable { + private final File source; + + @Nullable + private transient N nonSerializable; + + public NonSerializableObjectParser(File source) { + this.source = source; + } + + public final N get() { + if (this.nonSerializable == null) { + this.nonSerializable = this.parse(this.source); + } + + return this.nonSerializable; + } + + protected abstract N parse(File source); +} diff --git a/buildSrc/src/main/java/quilt/internal/util/SerializableDownloadableFile.java b/buildSrc/src/main/java/quilt/internal/util/serializable/SerializableDownloadableFile.java similarity index 88% rename from buildSrc/src/main/java/quilt/internal/util/SerializableDownloadableFile.java rename to buildSrc/src/main/java/quilt/internal/util/serializable/SerializableDownloadableFile.java index 8d92b128e0..5b891ce4a8 100644 --- a/buildSrc/src/main/java/quilt/internal/util/SerializableDownloadableFile.java +++ b/buildSrc/src/main/java/quilt/internal/util/serializable/SerializableDownloadableFile.java @@ -1,4 +1,4 @@ -package quilt.internal.util; +package quilt.internal.util.serializable; import org.quiltmc.launchermeta.version.v1.DownloadableFile; diff --git a/buildSrc/src/main/java/quilt/internal/util/SerializableVersionEntry.java b/buildSrc/src/main/java/quilt/internal/util/serializable/SerializableVersionEntry.java similarity index 97% rename from buildSrc/src/main/java/quilt/internal/util/SerializableVersionEntry.java rename to buildSrc/src/main/java/quilt/internal/util/serializable/SerializableVersionEntry.java index 2055ec9335..73ac4dbb90 100644 --- a/buildSrc/src/main/java/quilt/internal/util/SerializableVersionEntry.java +++ b/buildSrc/src/main/java/quilt/internal/util/serializable/SerializableVersionEntry.java @@ -1,4 +1,4 @@ -package quilt.internal.util; +package quilt.internal.util.serializable; import org.gradle.api.GradleException; import org.gradle.api.file.RegularFileProperty; diff --git a/buildSrc/src/main/java/quilt/internal/util/serializable/VersionParser.java b/buildSrc/src/main/java/quilt/internal/util/serializable/VersionParser.java new file mode 100644 index 0000000000..6d0307c0db --- /dev/null +++ b/buildSrc/src/main/java/quilt/internal/util/serializable/VersionParser.java @@ -0,0 +1,24 @@ +package quilt.internal.util.serializable; + +import org.apache.commons.io.FileUtils; +import org.gradle.api.GradleException; +import org.quiltmc.launchermeta.version.v1.Version; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +public class VersionParser extends NonSerializableObjectParser { + public VersionParser(File versionSource) { + super(versionSource); + } + + @Override + protected Version parse(File versionSource) { + try { + return Version.fromString(FileUtils.readFileToString(versionSource, StandardCharsets.UTF_8)); + } catch (IOException e) { + throw new GradleException("Failed to read version file", e); + } + } +}