-
-
Notifications
You must be signed in to change notification settings - Fork 449
Hand SlimeLauncher the extracted metadata zip, cache extraction #1084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: FG_7.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,60 +6,70 @@ | |
|
|
||
| import org.gradle.api.DefaultTask; | ||
| import org.gradle.api.Project; | ||
| import org.gradle.api.file.ArchiveOperations; | ||
| import org.gradle.api.file.ConfigurableFileCollection; | ||
| import org.gradle.api.file.DirectoryProperty; | ||
| import org.gradle.api.file.FileSystemOperations; | ||
| import org.gradle.api.file.RegularFileProperty; | ||
| import org.gradle.api.tasks.CacheableTask; | ||
| import org.gradle.api.tasks.InputFiles; | ||
| import org.gradle.api.tasks.OutputDirectory; | ||
| import org.gradle.api.tasks.OutputFile; | ||
| import org.gradle.api.tasks.PathSensitive; | ||
| import org.gradle.api.tasks.PathSensitivity; | ||
| import org.gradle.api.tasks.TaskAction; | ||
| import org.gradle.api.tasks.TaskProvider; | ||
|
|
||
| import javax.inject.Inject; | ||
| import java.io.FileInputStream; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipInputStream; | ||
|
|
||
| @CacheableTask | ||
| abstract class SlimeLauncherMetadata extends DefaultTask implements ForgeGradleTask { | ||
| static TaskProvider<SlimeLauncherMetadata> register(Project project, MinecraftDependencyInternal mcdep) { | ||
| var taskName = "slimeLauncherMetadataFor" + Util.dependencyToCamelCase(mcdep.getModule()); | ||
| return project.getTasks().register(taskName, SlimeLauncherMetadata.class, task -> { | ||
| task.setDescription("Extracts the Slime Launcher metadata for '%s'.".formatted(mcdep.toString())); | ||
| task.getMetadata().setFrom(mcdep.getMetadataDependency()); | ||
| task.getMetadataZip().setFrom(mcdep.getMetadataDependency()); | ||
| }); | ||
| } | ||
|
|
||
| protected abstract @InputFiles ConfigurableFileCollection getMetadata(); | ||
| @PathSensitive(PathSensitivity.NONE) | ||
| protected abstract @InputFiles ConfigurableFileCollection getMetadataZip(); | ||
|
|
||
| protected abstract @OutputDirectory DirectoryProperty getOutputDirectory(); | ||
|
|
||
| protected abstract @OutputFile RegularFileProperty getRunsJson(); | ||
|
|
||
| protected abstract @Inject ArchiveOperations getArchiveOperations(); | ||
|
|
||
| protected abstract @Inject FileSystemOperations getFileSystemOperations(); | ||
|
|
||
| @Inject | ||
| public SlimeLauncherMetadata() { | ||
| this.getRunsJson().convention(this.getDefaultOutputDirectory().map(d -> d.file("runs.json"))); | ||
| this.getOutputDirectory().convention(this.getDefaultOutputDirectory()); | ||
| this.getRunsJson().convention( | ||
| this.getOutputDirectory().map(d -> d.dir("launcher").file("runs.json")) | ||
| ); | ||
| } | ||
|
|
||
| @TaskAction | ||
| protected void exec() throws IOException { | ||
| var archive = this.getMetadata().getSingleFile(); | ||
| var json = this.getRunsJson().getAsFile().get().toPath(); | ||
| var archive = this.getMetadataZip().getSingleFile(); | ||
| var outputDir = this.getOutputDirectory().get(); | ||
|
|
||
| boolean foundRuns = false; | ||
| try (var zin = new ZipInputStream(new FileInputStream(archive))) { | ||
| for (ZipEntry entry; ((entry = zin.getNextEntry()) != null); ) { | ||
| if (!entry.getName().startsWith("launcher/")) | ||
| continue; | ||
| if (entry.getName().equals("launcher/runs.json")) { | ||
| Files.copy(zin, json, StandardCopyOption.REPLACE_EXISTING); | ||
| foundRuns = true; | ||
| } | ||
| } | ||
| } | ||
| this.getFileSystemOperations().sync(spec -> { | ||
| spec.from(this.getArchiveOperations().zipTree(archive)); | ||
| spec.into(outputDir); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for note, this technically causes the entire zip archive to be extracted to disc twice due to how zipTree is implemented in gradle. Once in their zipTree cache and then to the output directory.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there another way that would be preferred over this?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old code extracts just the files needed, do file locking. Its a trade off of which part you care about. It would probably be worth doing a |
||
| }); | ||
|
|
||
| // If we don't find a metadata file, write an empty runs | ||
| // This happens when using a 'vanilla' minecraft dependency | ||
| if (!foundRuns) | ||
| // Write an empty runs.json if it doesn't exist | ||
| // This happens when using a 'vanilla' Minecraft dependency | ||
| var json = this.getRunsJson().getAsFile().get().toPath(); | ||
| if (!Files.exists(json)) { | ||
| Files.createDirectories(json.getParent()); | ||
| Files.writeString(json, "{}", StandardCharsets.UTF_8); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking DSL change, i'd keep the property named
getMetadataso that we don't have to worry about that.