2018-12-18 22:36:22 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016, 2017, 2018 FabricMC
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.fabricmc.loom.util;
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2020-05-13 18:44:45 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2020-10-10 20:13:12 +00:00
|
|
|
import java.util.Set;
|
2020-06-01 11:17:58 +00:00
|
|
|
import java.util.function.Consumer;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-12-18 22:36:22 +00:00
|
|
|
import org.cadixdev.lorenz.MappingSet;
|
|
|
|
import org.cadixdev.mercury.Mercury;
|
|
|
|
import org.cadixdev.mercury.remapper.MercuryRemapper;
|
2019-11-02 20:23:27 +00:00
|
|
|
import org.gradle.api.Project;
|
2020-10-10 20:13:12 +00:00
|
|
|
import org.gradle.api.artifacts.Dependency;
|
2021-01-17 18:34:22 +00:00
|
|
|
import org.gradle.api.plugins.JavaPlugin;
|
2019-11-09 19:00:36 +00:00
|
|
|
import org.zeroturnaround.zip.ZipUtil;
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
2020-12-24 20:58:30 +00:00
|
|
|
import net.fabricmc.loom.configuration.RemappedConfigurationEntry;
|
|
|
|
import net.fabricmc.loom.configuration.providers.LaunchProvider;
|
|
|
|
import net.fabricmc.loom.configuration.providers.mappings.MappingsProvider;
|
|
|
|
import net.fabricmc.loom.util.gradle.ProgressLogger;
|
2020-05-27 15:41:43 +00:00
|
|
|
import net.fabricmc.lorenztiny.TinyMappingsReader;
|
2019-11-09 19:00:36 +00:00
|
|
|
import net.fabricmc.mapping.tree.TinyTree;
|
2019-11-02 20:23:27 +00:00
|
|
|
import net.fabricmc.stitch.util.StitchUtil;
|
2018-12-18 22:36:22 +00:00
|
|
|
|
|
|
|
public class SourceRemapper {
|
2020-05-13 18:44:45 +00:00
|
|
|
private final Project project;
|
|
|
|
private final boolean toNamed;
|
2020-06-01 11:17:58 +00:00
|
|
|
private final List<Consumer<ProgressLogger>> remapTasks = new ArrayList<>();
|
2020-05-13 18:44:45 +00:00
|
|
|
|
|
|
|
private Mercury mercury;
|
|
|
|
|
|
|
|
public SourceRemapper(Project project, boolean toNamed) {
|
|
|
|
this.project = project;
|
|
|
|
this.toNamed = toNamed;
|
2018-12-28 21:08:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
public static void remapSources(Project project, File input, File output, boolean named) throws Exception {
|
|
|
|
SourceRemapper sourceRemapper = new SourceRemapper(project, named);
|
2020-12-21 19:47:08 +00:00
|
|
|
sourceRemapper.scheduleRemapSources(input, output, false, true);
|
2020-05-13 18:44:45 +00:00
|
|
|
sourceRemapper.remapAll();
|
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2020-12-21 19:47:08 +00:00
|
|
|
@Deprecated
|
2020-05-13 18:44:45 +00:00
|
|
|
public void scheduleRemapSources(File source, File destination) throws Exception {
|
2020-12-21 19:47:08 +00:00
|
|
|
scheduleRemapSources(source, destination, false, true); // Not reproducable by default, old behavior
|
|
|
|
}
|
|
|
|
|
2020-12-30 20:34:34 +00:00
|
|
|
public void scheduleRemapSources(File source, File destination, boolean reproducibleFileOrder, boolean preserveFileTimestamps) {
|
2020-06-01 11:17:58 +00:00
|
|
|
remapTasks.add((logger) -> {
|
2019-04-24 17:32:35 +00:00
|
|
|
try {
|
2020-06-01 11:17:58 +00:00
|
|
|
logger.progress("remapping sources - " + source.getName());
|
2020-05-13 18:44:45 +00:00
|
|
|
remapSourcesInner(source, destination);
|
2020-12-21 19:47:08 +00:00
|
|
|
ZipReprocessorUtil.reprocessZip(destination, reproducibleFileOrder, preserveFileTimestamps);
|
2020-12-30 20:34:34 +00:00
|
|
|
|
|
|
|
// Set the remapped sources creation date to match the sources if we're likely succeeded in making it
|
|
|
|
destination.setLastModified(source.lastModified());
|
2018-12-22 13:37:16 +00:00
|
|
|
} catch (Exception e) {
|
2020-12-30 20:34:34 +00:00
|
|
|
// Failed to remap, lets clean up to ensure we try again next time
|
|
|
|
destination.delete();
|
2020-05-13 18:44:45 +00:00
|
|
|
throw new RuntimeException("Failed to remap sources for " + source, e);
|
2018-12-22 13:37:16 +00:00
|
|
|
}
|
|
|
|
});
|
2020-05-13 18:44:45 +00:00
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
public void remapAll() {
|
|
|
|
if (!remapTasks.isEmpty()) {
|
|
|
|
project.getLogger().lifecycle(":remapping sources");
|
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2020-06-01 11:17:58 +00:00
|
|
|
ProgressLogger progressLogger = ProgressLogger.getProgressFactory(project, SourceRemapper.class.getName());
|
|
|
|
progressLogger.start("Remapping dependency sources", "sources");
|
|
|
|
|
|
|
|
remapTasks.forEach(consumer -> consumer.accept(progressLogger));
|
|
|
|
|
|
|
|
progressLogger.completed();
|
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
// TODO: FIXME - WORKAROUND https://github.com/FabricMC/fabric-loom/issues/45
|
|
|
|
System.gc();
|
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
private void remapSourcesInner(File source, File destination) throws Exception {
|
|
|
|
project.getLogger().info(":remapping source jar");
|
|
|
|
Mercury mercury = getMercuryInstance();
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
if (source.equals(destination)) {
|
|
|
|
if (source.isDirectory()) {
|
|
|
|
throw new RuntimeException("Directories must differ!");
|
|
|
|
}
|
|
|
|
|
|
|
|
source = new File(destination.getAbsolutePath().substring(0, destination.getAbsolutePath().lastIndexOf('.')) + "-dev.jar");
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-12-23 18:18:40 +00:00
|
|
|
try {
|
|
|
|
com.google.common.io.Files.move(destination, source);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Could not rename " + destination.getName() + "!", e);
|
2018-12-22 13:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Path srcPath = source.toPath();
|
|
|
|
boolean isSrcTmp = false;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
if (!source.isDirectory()) {
|
|
|
|
// create tmp directory
|
|
|
|
isSrcTmp = true;
|
|
|
|
srcPath = Files.createTempDirectory("fabric-loom-src");
|
|
|
|
ZipUtil.unpack(source, srcPath.toFile());
|
|
|
|
}
|
|
|
|
|
2018-12-22 14:29:46 +00:00
|
|
|
if (!destination.isDirectory() && destination.exists()) {
|
|
|
|
if (!destination.delete()) {
|
|
|
|
throw new RuntimeException("Could not delete " + destination.getName() + "!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
StitchUtil.FileSystemDelegate dstFs = destination.isDirectory() ? null : StitchUtil.getJarFileSystem(destination, true);
|
|
|
|
Path dstPath = dstFs != null ? dstFs.get().getPath("/") : destination.toPath();
|
|
|
|
|
2018-12-22 14:29:46 +00:00
|
|
|
try {
|
|
|
|
mercury.rewrite(srcPath, dstPath);
|
|
|
|
} catch (Exception e) {
|
|
|
|
project.getLogger().warn("Could not remap " + source.getName() + " fully!", e);
|
|
|
|
}
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2019-08-23 10:59:16 +00:00
|
|
|
copyNonJavaFiles(srcPath, dstPath, project, source);
|
|
|
|
|
2018-12-22 13:37:16 +00:00
|
|
|
if (dstFs != null) {
|
|
|
|
dstFs.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSrcTmp) {
|
2018-12-22 14:35:36 +00:00
|
|
|
Files.walkFileTree(srcPath, new DeletingFileVisitor());
|
2018-12-22 13:37:16 +00:00
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
}
|
2019-08-23 10:59:16 +00:00
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
private Mercury getMercuryInstance() {
|
|
|
|
if (this.mercury != null) {
|
|
|
|
return this.mercury;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
|
|
|
MappingsProvider mappingsProvider = extension.getMappingsProvider();
|
|
|
|
|
|
|
|
MappingSet mappings = extension.getOrCreateSrcMappingCache(toNamed ? 1 : 0, () -> {
|
|
|
|
try {
|
|
|
|
TinyTree m = mappingsProvider.getMappings();
|
|
|
|
project.getLogger().lifecycle(":loading " + (toNamed ? "intermediary -> named" : "named -> intermediary") + " source mappings");
|
2020-05-27 15:41:43 +00:00
|
|
|
return new TinyMappingsReader(m, toNamed ? "intermediary" : "named", toNamed ? "named" : "intermediary").read();
|
2020-05-13 18:44:45 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Mercury mercury = extension.getOrCreateSrcMercuryCache(toNamed ? 1 : 0, () -> {
|
|
|
|
Mercury m = createMercuryWithClassPath(project, toNamed);
|
|
|
|
|
2020-06-29 14:25:05 +00:00
|
|
|
for (File file : extension.getUnmappedModCollection()) {
|
|
|
|
Path path = file.toPath();
|
|
|
|
|
|
|
|
if (Files.isRegularFile(path)) {
|
|
|
|
m.getClassPath().add(path);
|
2020-05-13 18:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.getClassPath().add(extension.getMinecraftMappedProvider().getMappedJar().toPath());
|
|
|
|
m.getClassPath().add(extension.getMinecraftMappedProvider().getIntermediaryJar().toPath());
|
|
|
|
|
2020-10-10 20:13:12 +00:00
|
|
|
Dependency annotationDependency = extension.getDependencyManager().getProvider(LaunchProvider.class).annotationDependency;
|
2021-01-17 18:34:22 +00:00
|
|
|
Set<File> files = project.getConfigurations().getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME)
|
2020-10-10 20:13:12 +00:00
|
|
|
.files(annotationDependency);
|
|
|
|
|
|
|
|
for (File file : files) {
|
|
|
|
m.getClassPath().add(file.toPath());
|
|
|
|
}
|
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
m.getProcessors().add(MercuryRemapper.create(mappings));
|
|
|
|
|
|
|
|
return m;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.mercury = mercury;
|
|
|
|
return mercury;
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
private static void copyNonJavaFiles(Path from, Path to, Project project, File source) throws IOException {
|
|
|
|
Files.walk(from).forEach(path -> {
|
|
|
|
Path targetPath = to.resolve(from.relativize(path).toString());
|
|
|
|
|
|
|
|
if (!isJavaFile(path) && !Files.exists(targetPath)) {
|
|
|
|
try {
|
|
|
|
Files.copy(path, targetPath);
|
|
|
|
} catch (IOException e) {
|
|
|
|
project.getLogger().warn("Could not copy non-java sources '" + source.getName() + "' fully!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-12-18 22:36:22 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 19:00:36 +00:00
|
|
|
public static Mercury createMercuryWithClassPath(Project project, boolean toNamed) {
|
|
|
|
Mercury m = new Mercury();
|
2020-11-15 16:03:16 +00:00
|
|
|
m.setGracefulClasspathChecks(true);
|
2019-11-09 19:00:36 +00:00
|
|
|
|
2020-12-21 19:34:00 +00:00
|
|
|
for (File file : project.getConfigurations().getByName(Constants.Configurations.LOADER_DEPENDENCIES).getFiles()) {
|
2019-11-09 19:00:36 +00:00
|
|
|
m.getClassPath().add(file.toPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!toNamed) {
|
|
|
|
for (File file : project.getConfigurations().getByName("compileClasspath").getFiles()) {
|
|
|
|
m.getClassPath().add(file.toPath());
|
|
|
|
}
|
2020-06-01 11:25:10 +00:00
|
|
|
} else {
|
|
|
|
for (RemappedConfigurationEntry entry : Constants.MOD_COMPILE_ENTRIES) {
|
|
|
|
for (File inputFile : project.getConfigurations().getByName(entry.getSourceConfiguration()).getFiles()) {
|
|
|
|
m.getClassPath().add(inputFile.toPath());
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 19:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
private static boolean isJavaFile(Path path) {
|
|
|
|
String name = path.getFileName().toString();
|
|
|
|
// ".java" is not a valid java file
|
|
|
|
return name.endsWith(".java") && name.length() != 5;
|
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
}
|