2016-10-12 09:44:31 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
2018-10-27 14:14:05 +00:00
|
|
|
* Copyright (c) 2016, 2017, 2018 FabricMC
|
2016-10-12 09:44:31 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-05-10 22:53:50 +00:00
|
|
|
package net.fabricmc.loom.task;
|
2016-10-12 09:44:31 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
2020-06-27 19:18:32 +00:00
|
|
|
import java.io.IOException;
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2020-08-26 19:15:21 +00:00
|
|
|
import com.google.common.base.Preconditions;
|
2019-11-02 20:23:27 +00:00
|
|
|
import org.gradle.api.Project;
|
|
|
|
import org.gradle.api.file.RegularFileProperty;
|
|
|
|
import org.gradle.api.provider.Property;
|
|
|
|
import org.gradle.api.tasks.Input;
|
|
|
|
import org.gradle.api.tasks.InputFile;
|
|
|
|
import org.gradle.api.tasks.TaskAction;
|
|
|
|
import org.gradle.jvm.tasks.Jar;
|
2020-06-27 19:18:32 +00:00
|
|
|
import org.zeroturnaround.zip.ZipUtil;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2016-10-12 09:44:31 +00:00
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
2018-12-09 21:22:35 +00:00
|
|
|
import net.fabricmc.loom.providers.MappingsProvider;
|
2019-10-22 19:28:00 +00:00
|
|
|
import net.fabricmc.loom.util.GradleSupport;
|
2019-05-10 22:53:50 +00:00
|
|
|
import net.fabricmc.loom.util.MixinRefmapHelper;
|
|
|
|
import net.fabricmc.loom.util.NestedJars;
|
|
|
|
import net.fabricmc.loom.util.TinyRemapperMappingsHelper;
|
2020-12-21 19:47:08 +00:00
|
|
|
import net.fabricmc.loom.util.ZipReprocessorUtil;
|
2020-04-06 14:28:53 +00:00
|
|
|
import net.fabricmc.loom.util.accesswidener.AccessWidenerJarProcessor;
|
2020-06-27 19:18:32 +00:00
|
|
|
import net.fabricmc.loom.util.JarRemapper;
|
|
|
|
import net.fabricmc.stitch.util.Pair;
|
2018-10-31 13:20:50 +00:00
|
|
|
import net.fabricmc.tinyremapper.OutputConsumerPath;
|
|
|
|
import net.fabricmc.tinyremapper.TinyRemapper;
|
|
|
|
import net.fabricmc.tinyremapper.TinyUtils;
|
2016-10-12 09:44:31 +00:00
|
|
|
|
2019-06-11 02:20:57 +00:00
|
|
|
public class RemapJarTask extends Jar {
|
2020-07-26 20:32:10 +00:00
|
|
|
private final RegularFileProperty input;
|
|
|
|
private final Property<Boolean> addNestedDependencies;
|
|
|
|
private final Property<Boolean> remapAccessWidener;
|
2020-06-27 19:18:32 +00:00
|
|
|
public JarRemapper jarRemapper;
|
2019-06-11 02:20:57 +00:00
|
|
|
|
|
|
|
public RemapJarTask() {
|
|
|
|
super();
|
2019-10-22 19:28:00 +00:00
|
|
|
input = GradleSupport.getfileProperty(getProject());
|
2019-06-11 02:20:57 +00:00
|
|
|
addNestedDependencies = getProject().getObjects().property(Boolean.class);
|
2020-05-11 11:48:04 +00:00
|
|
|
remapAccessWidener = getProject().getObjects().property(Boolean.class);
|
2020-05-11 12:09:42 +00:00
|
|
|
// false by default, I have no idea why I have to do it for this property and not the other one
|
|
|
|
remapAccessWidener.set(false);
|
2019-06-11 02:20:57 +00:00
|
|
|
}
|
2016-10-12 09:44:31 +00:00
|
|
|
|
2019-05-10 22:53:50 +00:00
|
|
|
@TaskAction
|
|
|
|
public void doTask() throws Throwable {
|
2020-06-27 19:18:32 +00:00
|
|
|
if (jarRemapper == null) {
|
|
|
|
doSingleRemap();
|
|
|
|
} else {
|
|
|
|
scheduleRemap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void doSingleRemap() throws Throwable {
|
2019-05-10 22:53:50 +00:00
|
|
|
Project project = getProject();
|
2016-10-12 09:44:31 +00:00
|
|
|
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
2019-06-11 02:20:57 +00:00
|
|
|
Path input = this.getInput().getAsFile().get().toPath();
|
2019-07-24 20:34:27 +00:00
|
|
|
Path output = this.getArchivePath().toPath();
|
2018-11-05 20:33:57 +00:00
|
|
|
|
2019-05-10 22:53:50 +00:00
|
|
|
if (!Files.exists(input)) {
|
|
|
|
throw new FileNotFoundException(input.toString());
|
2016-10-12 09:44:31 +00:00
|
|
|
}
|
2018-11-01 21:15:51 +00:00
|
|
|
|
2018-12-09 21:22:35 +00:00
|
|
|
MappingsProvider mappingsProvider = extension.getMappingsProvider();
|
2018-11-04 21:28:46 +00:00
|
|
|
|
2018-11-02 16:19:57 +00:00
|
|
|
String fromM = "named";
|
2018-10-31 13:20:50 +00:00
|
|
|
String toM = "intermediary";
|
2016-10-12 09:44:31 +00:00
|
|
|
|
2019-06-11 02:20:57 +00:00
|
|
|
Set<File> classpathFiles = new LinkedHashSet<>(
|
2019-11-09 19:00:36 +00:00
|
|
|
project.getConfigurations().getByName("compileClasspath").getFiles()
|
2019-06-11 02:20:57 +00:00
|
|
|
);
|
2019-10-22 19:29:25 +00:00
|
|
|
Path[] classpath = classpathFiles.stream().map(File::toPath).filter((p) -> !input.equals(p) && Files.exists(p)).toArray(Path[]::new);
|
2018-12-12 06:10:30 +00:00
|
|
|
|
2018-11-01 21:15:51 +00:00
|
|
|
TinyRemapper.Builder remapperBuilder = TinyRemapper.newRemapper();
|
2019-04-24 17:32:35 +00:00
|
|
|
|
2019-11-09 19:00:36 +00:00
|
|
|
remapperBuilder = remapperBuilder.withMappings(TinyRemapperMappingsHelper.create(mappingsProvider.getMappings(), fromM, toM, false));
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2020-09-06 19:21:08 +00:00
|
|
|
for (File mixinMapFile : extension.getAllMixinMappings()) {
|
|
|
|
if (mixinMapFile.exists()) {
|
|
|
|
remapperBuilder = remapperBuilder.withMappings(TinyUtils.createTinyMappingProvider(mixinMapFile.toPath(), fromM, toM));
|
|
|
|
}
|
2016-10-12 09:44:31 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 22:53:50 +00:00
|
|
|
project.getLogger().lifecycle(":remapping " + input.getFileName());
|
2018-11-05 20:33:57 +00:00
|
|
|
|
2019-05-18 09:51:34 +00:00
|
|
|
StringBuilder rc = new StringBuilder("Remap classpath: ");
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-05-18 09:51:34 +00:00
|
|
|
for (Path p : classpath) {
|
|
|
|
rc.append("\n - ").append(p.toString());
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-05-18 09:51:34 +00:00
|
|
|
project.getLogger().debug(rc.toString());
|
|
|
|
|
2018-11-01 21:15:51 +00:00
|
|
|
TinyRemapper remapper = remapperBuilder.build();
|
2016-10-12 09:44:31 +00:00
|
|
|
|
2019-07-24 20:34:27 +00:00
|
|
|
try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) {
|
2019-05-10 22:53:50 +00:00
|
|
|
outputConsumer.addNonClassFiles(input);
|
2019-04-21 09:37:16 +00:00
|
|
|
remapper.readClassPath(classpath);
|
2019-05-10 22:53:50 +00:00
|
|
|
remapper.readInputs(input);
|
2019-04-21 09:37:16 +00:00
|
|
|
remapper.apply(outputConsumer);
|
2018-11-11 23:17:44 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
remapper.finish();
|
2020-05-13 18:44:45 +00:00
|
|
|
throw new RuntimeException("Failed to remap " + input + " to " + output, e);
|
|
|
|
}
|
|
|
|
|
2020-05-13 18:47:28 +00:00
|
|
|
if (getRemapAccessWidener().getOrElse(false) && extension.accessWidener != null) {
|
2020-05-13 18:44:45 +00:00
|
|
|
extension.getJarProcessorManager().getByType(AccessWidenerJarProcessor.class).remapAccessWidener(output, remapper.getRemapper());
|
2018-08-07 14:56:39 +00:00
|
|
|
}
|
2016-10-23 16:31:56 +00:00
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
remapper.finish();
|
|
|
|
|
2019-05-10 22:53:50 +00:00
|
|
|
if (!Files.exists(output)) {
|
|
|
|
throw new RuntimeException("Failed to remap " + input + " to " + output + " - file missing!");
|
2018-06-18 16:01:39 +00:00
|
|
|
}
|
2018-11-06 09:36:35 +00:00
|
|
|
|
2019-05-10 22:53:50 +00:00
|
|
|
if (MixinRefmapHelper.addRefmapName(extension.getRefmapName(), extension.getMixinJsonVersion(), output)) {
|
2018-12-30 11:04:53 +00:00
|
|
|
project.getLogger().debug("Transformed mixin reference maps in output JAR!");
|
2018-11-06 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:52:34 +00:00
|
|
|
if (getAddNestedDependencies().getOrElse(false)) {
|
2019-05-10 22:53:50 +00:00
|
|
|
if (NestedJars.addNestedJars(project, output)) {
|
2019-04-21 22:41:28 +00:00
|
|
|
project.getLogger().debug("Added nested jar paths to mod json");
|
|
|
|
}
|
2019-04-07 14:18:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 19:47:08 +00:00
|
|
|
if (isReproducibleFileOrder() || isPreserveFileTimestamps()) {
|
|
|
|
ZipReprocessorUtil.reprocessZip(output.toFile(), isReproducibleFileOrder(), isPreserveFileTimestamps());
|
|
|
|
}
|
2019-06-11 02:20:57 +00:00
|
|
|
}
|
2019-05-10 22:53:50 +00:00
|
|
|
|
2020-06-27 19:18:32 +00:00
|
|
|
public void scheduleRemap() throws Throwable {
|
|
|
|
Project project = getProject();
|
|
|
|
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
|
|
|
Path input = this.getInput().getAsFile().get().toPath();
|
|
|
|
Path output = this.getArchivePath().toPath();
|
|
|
|
|
|
|
|
if (!Files.exists(input)) {
|
|
|
|
throw new FileNotFoundException(input.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
MappingsProvider mappingsProvider = extension.getMappingsProvider();
|
|
|
|
|
|
|
|
String fromM = "named";
|
|
|
|
String toM = "intermediary";
|
|
|
|
|
|
|
|
if (extension.isRootProject()) {
|
|
|
|
Set<File> classpathFiles = new LinkedHashSet<>(
|
|
|
|
project.getConfigurations().getByName("compileClasspath").getFiles()
|
|
|
|
);
|
|
|
|
|
|
|
|
Path[] classpath = classpathFiles.stream()
|
|
|
|
.map(File::toPath)
|
|
|
|
.filter(Files::exists)
|
|
|
|
.toArray(Path[]::new);
|
|
|
|
|
|
|
|
jarRemapper.addToClasspath(classpath);
|
|
|
|
|
|
|
|
jarRemapper.addMappings(TinyRemapperMappingsHelper.create(mappingsProvider.getMappings(), fromM, toM, false));
|
|
|
|
}
|
|
|
|
|
2020-09-06 19:21:08 +00:00
|
|
|
for (File mixinMapFile : extension.getAllMixinMappings()) {
|
|
|
|
if (mixinMapFile.exists()) {
|
|
|
|
jarRemapper.addMappings(TinyUtils.createTinyMappingProvider(mixinMapFile.toPath(), fromM, toM));
|
|
|
|
}
|
2020-06-27 19:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jarRemapper.scheduleRemap(input, output)
|
|
|
|
.supplyAccessWidener((remapData, remapper) -> {
|
|
|
|
if (getRemapAccessWidener().getOrElse(false) && extension.accessWidener != null) {
|
|
|
|
AccessWidenerJarProcessor accessWidenerJarProcessor = extension.getJarProcessorManager().getByType(AccessWidenerJarProcessor.class);
|
|
|
|
byte[] data;
|
|
|
|
|
|
|
|
try {
|
|
|
|
data = accessWidenerJarProcessor.getRemappedAccessWidener(remapper);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Failed to remap access widener");
|
|
|
|
}
|
|
|
|
|
2020-08-26 19:15:21 +00:00
|
|
|
String awPath = accessWidenerJarProcessor.getAccessWidenerPath(remapData.input);
|
|
|
|
Preconditions.checkNotNull(awPath, "Failed to find accessWidener in fabric.mod.json: " + remapData.input);
|
|
|
|
|
|
|
|
return Pair.of(awPath, data);
|
2020-06-27 19:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.complete((data, accessWidener) -> {
|
|
|
|
if (!Files.exists(output)) {
|
|
|
|
throw new RuntimeException("Failed to remap " + input + " to " + output + " - file missing!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MixinRefmapHelper.addRefmapName(extension.getRefmapName(), extension.getMixinJsonVersion(), output)) {
|
|
|
|
project.getLogger().debug("Transformed mixin reference maps in output JAR!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getAddNestedDependencies().getOrElse(false)) {
|
|
|
|
if (NestedJars.addNestedJars(project, output)) {
|
|
|
|
project.getLogger().debug("Added nested jar paths to mod json");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accessWidener != null) {
|
2020-08-26 19:15:21 +00:00
|
|
|
boolean replaced = ZipUtil.replaceEntry(data.output.toFile(), accessWidener.getLeft(), accessWidener.getRight());
|
|
|
|
Preconditions.checkArgument(replaced, "Failed to remap access widener");
|
2020-06-27 19:18:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-11 02:20:57 +00:00
|
|
|
@InputFile
|
|
|
|
public RegularFileProperty getInput() {
|
|
|
|
return input;
|
2016-10-12 09:44:31 +00:00
|
|
|
}
|
2018-08-07 17:52:17 +00:00
|
|
|
|
2019-06-11 02:20:57 +00:00
|
|
|
@Input
|
|
|
|
public Property<Boolean> getAddNestedDependencies() {
|
|
|
|
return addNestedDependencies;
|
|
|
|
}
|
2020-05-11 11:48:04 +00:00
|
|
|
|
|
|
|
@Input
|
|
|
|
public Property<Boolean> getRemapAccessWidener() {
|
|
|
|
return remapAccessWidener;
|
|
|
|
}
|
2016-10-12 09:44:31 +00:00
|
|
|
}
|