Skip to content

Commit

Permalink
replace VersionDownloadInfo with VersionParser
Browse files Browse the repository at this point in the history
  • Loading branch information
supersaiyansubtlety committed Oct 28, 2024
1 parent de9df65 commit 6c57c44
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,15 +27,14 @@
* <li> {@link TaskContainer#register registers} {@value MergeJarsTask#MERGE_JARS_TASK_NAME}
* which merges the client and server jars
* <li> {@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}
* </ul>
*/
public abstract class MinecraftJarsPlugin extends DefaultTaskedMappingsProjectPlugin<MinecraftJarsPlugin.Tasks> {

@Override
protected Tasks applyImpl(@NotNull Project project) {
final Provider<Directory> minecraftDir = this.getMinecraftDir();
Expand Down Expand Up @@ -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> versionDownloadInfo =
this.getObjects().property(VersionDownloadInfo.class).convention(
downloadWantedVersionManifest.flatMap(DownloadWantedVersionManifestTask::provideVersionDownloadInfo)
);
final Property<VersionParser> 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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> targetVersion = this.getObjects().property(String.class).convention(
final Property<String> targetVersion = this.getObjects().property(String.class);
targetVersion.set(
downloadTargetMetaFile.flatMap(DownloadTargetMetaFileTask::provideTargetVersion)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<VersionDownloadInfo> getVersionDownloadInfo();
Property<VersionParser> getVersionParser();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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.
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}.
Expand Down Expand Up @@ -65,7 +67,9 @@ public DownloadMinecraftLibrariesTask() {
// put this in a property to cache it
final Provider<Map<NamedUrl, RegularFile>> 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);
Expand All @@ -88,8 +92,17 @@ public void download() {
);
}

private static Map<NamedUrl, RegularFile> getArtifactsByNamedUrl(VersionDownloadInfo info, Directory destDir) {
return info.getLibraryArtifactUrlsByName().entrySet().stream()
private static Map<NamedUrl, RegularFile> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -27,13 +27,13 @@ public abstract class DownloadWantedVersionManifestTask extends SimpleDownloadTa
public abstract RegularFileProperty getDest();

/**
* @see VersionDownloadInfoConsumingTask
* @see VersionParserConsumingTask
* @see MinecraftJarsPlugin
*/
public Provider<VersionDownloadInfo> provideVersionDownloadInfo() {
public Provider<VersionParser> provideVersionParser() {
return this.getDest()
.map(RegularFile::getAsFile)
.map(VersionDownloadInfo::of);
.map(VersionParser::new);
}

public DownloadWantedVersionManifestTask() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<N> 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);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package quilt.internal.util;
package quilt.internal.util.serializable;

import org.quiltmc.launchermeta.version.v1.DownloadableFile;

Expand Down
Loading

1 comment on commit 6c57c44

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No difference between head and target.

Please sign in to comment.