2018-12-18 22:36:22 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
2021-07-10 20:50:53 +00:00
|
|
|
* Copyright (c) 2018-2021 FabricMC
|
2018-12-18 22:36:22 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
2021-07-25 21:54:22 +00:00
|
|
|
import java.util.HashSet;
|
2020-05-13 18:44:45 +00:00
|
|
|
import java.util.List;
|
2021-07-25 21:54:22 +00:00
|
|
|
import java.util.Objects;
|
2020-10-10 20:13:12 +00:00
|
|
|
import java.util.Set;
|
2021-07-25 21:54:22 +00:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.concurrent.CompletionException;
|
|
|
|
import java.util.concurrent.ConcurrentLinkedDeque;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
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;
|
2021-07-25 21:54:22 +00:00
|
|
|
import org.cadixdev.mercury.SourceProcessor;
|
2018-12-18 22:36:22 +00:00
|
|
|
import org.cadixdev.mercury.remapper.MercuryRemapper;
|
2019-11-02 20:23:27 +00:00
|
|
|
import org.gradle.api.Project;
|
2021-07-25 21:54: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;
|
2021-06-14 17:39:03 +00:00
|
|
|
import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
|
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;
|
2021-07-25 21:54:22 +00:00
|
|
|
private final List<RemapTask> remapTasks = new ArrayList<>();
|
|
|
|
private boolean hasStartedRemap = false;
|
2020-05-13 18:44:45 +00:00
|
|
|
|
|
|
|
public SourceRemapper(Project project, boolean toNamed) {
|
|
|
|
this.project = project;
|
|
|
|
this.toNamed = toNamed;
|
2018-12-28 21:08:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
public static void remapSources(Project project, File input, File output, boolean named, boolean reproducibleFileOrder, boolean preserveFileTimestamps) throws IOException {
|
2020-05-13 18:44:45 +00:00
|
|
|
SourceRemapper sourceRemapper = new SourceRemapper(project, named);
|
2021-03-15 23:31:18 +00:00
|
|
|
sourceRemapper.scheduleRemapSources(input, output, reproducibleFileOrder, preserveFileTimestamps);
|
2020-05-13 18:44:45 +00:00
|
|
|
sourceRemapper.remapAll();
|
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2020-12-30 20:34:34 +00:00
|
|
|
public void scheduleRemapSources(File source, File destination, boolean reproducibleFileOrder, boolean preserveFileTimestamps) {
|
2021-07-25 21:54:22 +00:00
|
|
|
this.scheduleRemapSources(new RemapTask(source, destination, reproducibleFileOrder, preserveFileTimestamps));
|
|
|
|
}
|
2020-12-30 20:34:34 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
public synchronized void scheduleRemapSources(RemapTask data) {
|
|
|
|
if (hasStartedRemap) {
|
|
|
|
throw new UnsupportedOperationException("Cannot add data after remapping has started");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.remapTasks.add(data);
|
2020-05-13 18:44:45 +00:00
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
public void remapAll() throws IOException {
|
|
|
|
hasStartedRemap = true;
|
|
|
|
|
2021-01-19 22:15:23 +00:00
|
|
|
if (remapTasks.isEmpty()) {
|
|
|
|
return;
|
2020-05-13 18:44:45 +00:00
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
int threads = Math.min(remapTasks.size(), Math.max(2, Runtime.getRuntime().availableProcessors() / 2));
|
|
|
|
ExecutorService ioExecutor = Executors.newFixedThreadPool(2);
|
|
|
|
ExecutorService remapExecutor = Executors.newFixedThreadPool(threads);
|
|
|
|
|
|
|
|
List<CompletableFuture<Void>> completableFutures = new ArrayList<>();
|
|
|
|
|
|
|
|
{
|
|
|
|
// We have to build the Mercury instances on the main thread as createMercuryRemapper resolves gradle stuff
|
|
|
|
// TODO refactor this a bit to not do that.
|
|
|
|
var mercuryQueue = new ConcurrentLinkedDeque<Mercury>();
|
2021-01-19 22:15:23 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
final var remapper = createMercuryRemapper();
|
|
|
|
final var inputClasspath = getInputClasspath(project);
|
2020-06-01 11:17:58 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
for (int i = 0; i < threads; i++) {
|
|
|
|
Mercury mercury = createMercuryWithClassPath(project, toNamed);
|
|
|
|
mercury.getProcessors().add(remapper);
|
|
|
|
mercury.getClassPath().addAll(inputClasspath);
|
|
|
|
|
|
|
|
mercuryQueue.add(mercury);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadLocal<Mercury> mercuryThreadLocal = ThreadLocal.withInitial(() -> Objects.requireNonNull(mercuryQueue.pop(), "No Mercury instances left"));
|
|
|
|
|
|
|
|
for (RemapTask remapTask : this.remapTasks) {
|
|
|
|
completableFutures.add(
|
|
|
|
CompletableFuture.supplyAsync(() ->
|
|
|
|
prepareForRemap(remapTask), ioExecutor)
|
|
|
|
.thenApplyAsync(remapData -> doRemap(mercuryThreadLocal.get(), remapData), remapExecutor)
|
|
|
|
.thenAcceptAsync(remapData -> completeRemap(remapData, remapTask), ioExecutor)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CompletableFuture<Void> future : completableFutures) {
|
|
|
|
try {
|
|
|
|
future.join();
|
|
|
|
} catch (CompletionException e) {
|
|
|
|
try {
|
|
|
|
throw e.getCause();
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
throw ioe;
|
|
|
|
} catch (Throwable unknown) {
|
|
|
|
throw new RuntimeException("An unknown exception occured when remapping", unknown);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 11:17:58 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
ioExecutor.shutdown();
|
|
|
|
remapExecutor.shutdown();
|
2020-06-01 11:17:58 +00:00
|
|
|
|
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
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
private RemapData prepareForRemap(RemapTask remapTask) {
|
|
|
|
try {
|
|
|
|
File source = remapTask.source();
|
|
|
|
final File destination = remapTask.destination();
|
2018-12-18 22:36:22 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
if (source.equals(destination)) {
|
|
|
|
if (source.isDirectory()) {
|
|
|
|
throw new RuntimeException("Directories must differ!");
|
|
|
|
}
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
source = new File(destination.getAbsolutePath().substring(0, destination.getAbsolutePath().lastIndexOf('.')) + "-dev.jar");
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2021-07-25 21:54:22 +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
|
|
|
}
|
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
Path srcPath = source.toPath();
|
|
|
|
boolean isSrcTmp = false;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
if (!source.isDirectory()) {
|
|
|
|
// create tmp directory
|
|
|
|
isSrcTmp = true;
|
|
|
|
srcPath = Files.createTempDirectory("fabric-loom-src");
|
|
|
|
ZipUtil.unpack(source, srcPath.toFile());
|
|
|
|
}
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
if (!destination.isDirectory() && destination.exists()) {
|
|
|
|
if (!destination.delete()) {
|
|
|
|
throw new RuntimeException("Could not delete " + destination.getName() + "!");
|
|
|
|
}
|
2018-12-22 14:29:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
StitchUtil.FileSystemDelegate dstFs = remapTask.destination().isDirectory() ? null : StitchUtil.getJarFileSystem(remapTask.destination(), true);
|
|
|
|
Path dstPath = dstFs != null ? dstFs.get().getPath("/") : remapTask.destination().toPath();
|
|
|
|
|
|
|
|
return new RemapData(Objects.requireNonNull(srcPath, "srcPath"), Objects.requireNonNull(dstPath, "dstPath"), dstFs, isSrcTmp);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new CompletionException("Failed to prepare for remap", e);
|
|
|
|
}
|
|
|
|
}
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
private RemapData doRemap(Mercury mercury, RemapData remapData) {
|
2018-12-22 14:29:46 +00:00
|
|
|
try {
|
2021-07-25 21:54:22 +00:00
|
|
|
mercury.rewrite(remapData.source(), remapData.destination());
|
2018-12-22 14:29:46 +00:00
|
|
|
} catch (Exception e) {
|
2021-07-25 21:54:22 +00:00
|
|
|
project.getLogger().warn("Could not remap " + remapData.source().toString() + " fully!", e);
|
|
|
|
// TODO do something more with this error? delete the dst file in complete remap?
|
2018-12-22 14:29:46 +00:00
|
|
|
}
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
return remapData;
|
|
|
|
}
|
2019-08-23 10:59:16 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
private void completeRemap(RemapData remapData, RemapTask remapTask) {
|
|
|
|
try {
|
|
|
|
copyNonJavaFiles(remapData.source(), remapData.destination(), project, remapTask.source());
|
2018-12-22 13:37:16 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
if (remapData.dstFs() != null) {
|
|
|
|
remapData.dstFs().close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remapData.isSrcTmp()) {
|
|
|
|
Files.walkFileTree(remapData.source(), new DeletingFileVisitor());
|
|
|
|
}
|
|
|
|
|
|
|
|
ZipReprocessorUtil.reprocessZip(remapTask.destination(), remapTask.reproducibleFileOrder(), remapTask.preserveFileTimestamps());
|
2019-08-23 10:59:16 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
// Set the remapped sources creation date to match the sources if we're likely succeeded in making it
|
|
|
|
remapTask.destination().setLastModified(remapTask.source().lastModified());
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new CompletionException("Failed to complete remap", e);
|
2020-05-13 18:44:45 +00:00
|
|
|
}
|
2021-07-25 21:54:22 +00:00
|
|
|
}
|
2020-05-13 18:44:45 +00:00
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
private SourceProcessor createMercuryRemapper() {
|
2021-07-13 23:03:21 +00:00
|
|
|
LoomGradleExtension extension = LoomGradleExtension.get(project);
|
2021-06-14 17:39:03 +00:00
|
|
|
MappingsProviderImpl mappingsProvider = extension.getMappingsProvider();
|
2020-05-13 18:44:45 +00:00
|
|
|
|
|
|
|
MappingSet mappings = extension.getOrCreateSrcMappingCache(toNamed ? 1 : 0, () -> {
|
|
|
|
try {
|
|
|
|
TinyTree m = mappingsProvider.getMappings();
|
2021-02-11 18:12:27 +00:00
|
|
|
project.getLogger().info(":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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
return MercuryRemapper.create(mappings);
|
2020-05-13 18:44:45 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-07-25 21:54:22 +00:00
|
|
|
var mercury = new Mercury();
|
|
|
|
mercury.setGracefulClasspathChecks(true);
|
|
|
|
|
|
|
|
var classpath = mercury.getClassPath();
|
|
|
|
classpath.addAll(getCompileClasspath(project, toNamed));
|
|
|
|
|
|
|
|
return mercury;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Set<Path> getCompileClasspath(Project project, boolean toNamed) {
|
|
|
|
var classpath = new HashSet<Path>();
|
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()) {
|
2021-07-25 21:54:22 +00:00
|
|
|
classpath.add(file.toPath());
|
2019-11-09 19:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!toNamed) {
|
2021-07-25 21:54:22 +00:00
|
|
|
for (File file : project.getConfigurations().getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME).getFiles()) {
|
|
|
|
classpath.add(file.toPath());
|
2019-11-09 19:00:36 +00:00
|
|
|
}
|
2020-06-01 11:25:10 +00:00
|
|
|
} else {
|
|
|
|
for (RemappedConfigurationEntry entry : Constants.MOD_COMPILE_ENTRIES) {
|
2021-05-13 21:06:34 +00:00
|
|
|
for (File inputFile : project.getConfigurations().getByName(entry.sourceConfiguration()).getFiles()) {
|
2021-07-25 21:54:22 +00:00
|
|
|
classpath.add(inputFile.toPath());
|
2020-06-01 11:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-09 19:00:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 21:54:22 +00:00
|
|
|
return classpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Set<Path> getInputClasspath(Project project) {
|
|
|
|
LoomGradleExtension extension = LoomGradleExtension.get(project);
|
|
|
|
|
|
|
|
var classpath = new HashSet<Path>();
|
|
|
|
|
|
|
|
for (File file : extension.getUnmappedModCollection()) {
|
|
|
|
Path path = file.toPath();
|
|
|
|
|
|
|
|
if (Files.isRegularFile(path)) {
|
|
|
|
classpath.add(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
classpath.add(extension.getMinecraftMappedProvider().getMappedJar().toPath());
|
|
|
|
classpath.add(extension.getMinecraftMappedProvider().getIntermediaryJar().toPath());
|
|
|
|
|
|
|
|
Set<File> files = project.getConfigurations()
|
|
|
|
.detachedConfiguration(project.getDependencies().create(Constants.Dependencies.JETBRAINS_ANNOTATIONS + Constants.Dependencies.Versions.JETBRAINS_ANNOTATIONS))
|
|
|
|
.resolve();
|
|
|
|
|
|
|
|
for (File file : files) {
|
|
|
|
classpath.add(file.toPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
return classpath;
|
2019-11-09 19:00:36 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2021-07-25 21:54:22 +00:00
|
|
|
|
|
|
|
public static record RemapTask(File source, File destination, boolean reproducibleFileOrder, boolean preserveFileTimestamps) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static record RemapData(Path source, Path destination, StitchUtil.FileSystemDelegate dstFs, boolean isSrcTmp) {
|
|
|
|
}
|
2018-12-18 22:36:22 +00:00
|
|
|
}
|