Adds Forge Sources Remapping

Filter ':launcher' dependency (Could cause problems! Please test!)
Fix previous optimisations leaving signing info in
Add license header to various files

Signed-off-by: shedaniel <daniel@shedaniel.me>
This commit is contained in:
shedaniel
2021-05-05 18:29:42 +08:00
parent 8d96bc0b6f
commit 2625b90a17
10 changed files with 395 additions and 34 deletions

View File

@@ -1,3 +1,27 @@
/*
* 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.build.nesting;
import java.io.File;

View File

@@ -140,7 +140,7 @@ public class LoomDependencyManager {
String platformSuffix = extension.isForge() ? "_forge" : "";
String mappingsKey = mappingsProvider.getMappingsKey() + platformSuffix;
if (extension.getInstallerJson() == null) {
if (extension.getInstallerJson() == null && !extension.isForge()) {
//If we've not found the installer JSON we've probably skipped remapping Fabric loader, let's go looking
project.getLogger().info("Searching through modCompileClasspath for installer JSON");
final Configuration configuration = project.getConfigurations().getByName(Constants.Configurations.MOD_COMPILE_CLASSPATH);
@@ -159,10 +159,10 @@ public class LoomDependencyManager {
handleInstallerJson(extension.getInstallerJson(), project);
}
}
}
if (extension.getInstallerJson() == null && !extension.isForge()) {
project.getLogger().warn("fabric-installer.json not found in classpath!");
if (extension.getInstallerJson() == null) {
project.getLogger().warn("fabric-installer.json not found in classpath!");
}
}
ModCompileRemapper.remapDependencies(project, mappingsKey, extension, sourceRemapper);

View File

@@ -86,6 +86,7 @@ public class ForgeUserdevProvider extends DependencyProvider {
}
}
if (lib.getAsString().endsWith(":launcher")) continue;
addDependency(lib.getAsString(), Constants.Configurations.FORGE_DEPENDENCIES);
}

View File

@@ -47,6 +47,8 @@ import java.util.jar.Manifest;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import dev.architectury.tinyremapper.IMappingProvider;
import dev.architectury.tinyremapper.NonClassCopyMode;
import dev.architectury.tinyremapper.OutputConsumerPath;
import dev.architectury.tinyremapper.TinyRemapper;
import org.gradle.api.Project;
import org.jetbrains.annotations.Nullable;
@@ -55,6 +57,7 @@ import net.fabricmc.loom.configuration.DependencyProvider;
import net.fabricmc.loom.configuration.providers.MinecraftProvider;
import net.fabricmc.loom.configuration.providers.mappings.MappingsProvider;
import net.fabricmc.loom.configuration.providers.minecraft.tr.OutputRemappingHandler;
import net.fabricmc.loom.configuration.sources.ForgeSourcesRemapper;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.DownloadUtil;
import net.fabricmc.loom.util.FileSystemUtil;
@@ -66,7 +69,7 @@ import net.fabricmc.loom.util.srg.InnerClassRemapper;
import net.fabricmc.mapping.tree.TinyTree;
public class MinecraftMappedProvider extends DependencyProvider {
private static final Map<String, String> JSR_TO_JETBRAINS = new ImmutableMap.Builder<String, String>()
public static final Map<String, String> JSR_TO_JETBRAINS = new ImmutableMap.Builder<String, String>()
.put("javax/annotation/Nullable", "org/jetbrains/annotations/Nullable")
.put("javax/annotation/Nonnull", "org/jetbrains/annotations/NotNull")
.put("javax/annotation/concurrent/Immutable", "org/jetbrains/annotations/Unmodifiable")
@@ -132,6 +135,16 @@ public class MinecraftMappedProvider extends DependencyProvider {
}
addDependencies(dependency, postPopulationScheduler);
getProject().afterEvaluate(project -> {
if (getExtension().isForge()) {
try {
ForgeSourcesRemapper.addBaseForgeSources(project);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void mapMinecraftJar() throws Exception {
@@ -158,32 +171,28 @@ public class MinecraftMappedProvider extends DependencyProvider {
try (FileSystemUtil.FileSystemDelegate inputFs = FileSystemUtil.getJarFileSystem(input, false)) {
ThreadingUtils.TaskCompleter taskCompleter = ThreadingUtils.taskCompleter();
try (FileSystemUtil.FileSystemDelegate assetsFs = FileSystemUtil.getJarFileSystem(tmpAssets, true)) {
for (Path path : (Iterable<? extends Path>) Files.walk(inputFs.get().getPath("/"))::iterator) {
if (Files.isRegularFile(path)) {
if (path.getFileName().toString().endsWith(".class")) {
taskCompleter.add(() -> {
byte[] bytes = Files.readAllBytes(path);
for (Path path : (Iterable<? extends Path>) Files.walk(inputFs.get().getPath("/"))::iterator) {
if (Files.isRegularFile(path)) {
if (path.getFileName().toString().endsWith(".class")) {
taskCompleter.add(() -> {
byte[] bytes = Files.readAllBytes(path);
synchronized (inputByteList) {
inputByteList.add(bytes);
}
});
} else {
Path p = assetsFs.get().getPath(path.toString());
if (p.getParent() != null) {
Files.createDirectories(p.getParent());
synchronized (inputByteList) {
inputByteList.add(bytes);
}
taskCompleter.add(() -> {
Files.copy(path, p);
});
}
});
}
}
}
taskCompleter.complete();
taskCompleter.complete();
}
try (OutputConsumerPath tmpAssetsPath = new OutputConsumerPath.Builder(tmpAssets).assumeArchive(true).build()) {
if (getExtension().isForge()) {
tmpAssetsPath.addNonClassFiles(input, NonClassCopyMode.FIX_META_INF, null);
} else {
tmpAssetsPath.addNonClassFiles(input);
}
}

View File

@@ -1,3 +1,27 @@
/*
* 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.configuration.providers.minecraft.tr;
import org.cadixdev.mercury.Mercury;

View File

@@ -1,3 +1,27 @@
/*
* 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.configuration.providers.minecraft.tr;
import java.io.IOException;

View File

@@ -0,0 +1,239 @@
/*
* 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.configuration.sources;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.mercury.Mercury;
import org.cadixdev.mercury.remapper.MercuryRemapper;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.plugins.JavaPlugin;
import org.zeroturnaround.zip.ZipUtil;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.build.ModCompileRemapper;
import net.fabricmc.loom.configuration.providers.LaunchProvider;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftMappedProvider;
import net.fabricmc.loom.task.GenerateSourcesTask;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.DeletingFileVisitor;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.SourceRemapper;
import net.fabricmc.loom.util.ThreadingUtils;
import net.fabricmc.lorenztiny.TinyMappingsReader;
public class ForgeSourcesRemapper {
public static void addBaseForgeSources(Project project) throws IOException {
Path sourcesJar = GenerateSourcesTask.getMappedJarFileWithSuffix(project, "-sources.jar").toPath();
if (!Files.exists(sourcesJar)) {
addForgeSources(project, sourcesJar);
}
}
public static void addForgeSources(Project project, Path sourcesJar) throws IOException {
try (FileSystemUtil.FileSystemDelegate delegate = FileSystemUtil.getJarFileSystem(sourcesJar, true)) {
ThreadingUtils.TaskCompleter taskCompleter = ThreadingUtils.taskCompleter();
provideForgeSources(project, (path, bytes) -> {
Path fsPath = delegate.get().getPath(path);
if (fsPath.getParent() != null) {
try {
Files.createDirectories(fsPath.getParent());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
taskCompleter.add(() -> {
Files.write(fsPath, bytes, StandardOpenOption.CREATE);
});
});
taskCompleter.complete();
}
}
public static void provideForgeSources(Project project, BiConsumer<String, byte[]> consumer) throws IOException {
List<Path> forgeInstallerSources = new ArrayList<>();
for (ResolvedArtifact artifact : project.getConfigurations().getByName(Constants.Configurations.FORGE_INSTALLER).getResolvedConfiguration().getResolvedArtifacts()) {
File forgeInstallerSource = ModCompileRemapper.findSources(project.getDependencies(), artifact);
if (forgeInstallerSource != null) {
forgeInstallerSources.add(forgeInstallerSource.toPath());
}
}
project.getLogger().lifecycle(":found {} forge source jars", forgeInstallerSources.size());
Map<String, byte[]> forgeSources = extractSources(forgeInstallerSources);
project.getLogger().lifecycle(":extracted {} forge source classes", forgeSources.size());
remapSources(project, forgeSources);
forgeSources.forEach(consumer);
}
private static void remapSources(Project project, Map<String, byte[]> sources) throws IOException {
File tmpInput = File.createTempFile("tmpInputForgeSources", null);
tmpInput.delete();
tmpInput.deleteOnExit();
File tmpOutput = File.createTempFile("tmpOutputForgeSources", null);
tmpOutput.delete();
tmpOutput.deleteOnExit();
try (FileSystemUtil.FileSystemDelegate delegate = FileSystemUtil.getJarFileSystem(tmpInput, true)) {
ThreadingUtils.TaskCompleter taskCompleter = ThreadingUtils.taskCompleter();
for (Map.Entry<String, byte[]> entry : sources.entrySet()) {
Path path = delegate.get().getPath(entry.getKey());
if (path.getParent() != null) {
Files.createDirectories(path.getParent());
}
taskCompleter.add(() -> {
Files.write(path, entry.getValue(), StandardOpenOption.CREATE);
});
}
taskCompleter.complete();
}
remapForgeSourcesInner(project, tmpInput.toPath(), tmpOutput.toPath());
tmpInput.delete();
int[] failedToRemap = {0};
try (FileSystemUtil.FileSystemDelegate delegate = FileSystemUtil.getJarFileSystem(tmpOutput, false)) {
ThreadingUtils.TaskCompleter taskCompleter = ThreadingUtils.taskCompleter();
for (Map.Entry<String, byte[]> entry : new HashSet<>(sources.entrySet())) {
taskCompleter.add(() -> {
Path path = delegate.get().getPath(entry.getKey());
if (Files.exists(path)) {
sources.put(entry.getKey(), Files.readAllBytes(path));
} else {
sources.remove(entry.getKey());
project.getLogger().error("forge source failed to remap " + entry.getKey());
failedToRemap[0]++;
}
});
}
taskCompleter.complete();
}
tmpOutput.delete();
if (failedToRemap[0] > 0) {
project.getLogger().error("{} forge sources failed to remap", failedToRemap[0]);
}
}
private static void remapForgeSourcesInner(Project project, Path tmpInput, Path tmpOutput) throws IOException {
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
Mercury mercury = SourceRemapper.createMercuryWithClassPath(project, false);
MappingSet mappings = new TinyMappingsReader(extension.getMappingsProvider().getMappingsWithSrg(), "srg", "named").read();
for (Map.Entry<String, String> entry : MinecraftMappedProvider.JSR_TO_JETBRAINS.entrySet()) {
mappings.getOrCreateClassMapping(entry.getKey()).setDeobfuscatedName(entry.getValue());
}
Dependency annotationDependency = extension.getDependencyManager().getProvider(LaunchProvider.class).annotationDependency;
Set<File> files = project.getConfigurations().getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME)
.files(annotationDependency);
for (File file : files) {
mercury.getClassPath().add(file.toPath());
}
// Distinct and add the srg jar at the top, so it gets prioritized
mercury.getClassPath().add(0, extension.getMinecraftMappedProvider().getSrgJar().toPath());
List<Path> newClassPath = mercury.getClassPath().stream()
.distinct()
.filter(Files::isRegularFile)
.collect(Collectors.toList());
mercury.getClassPath().clear();
mercury.getClassPath().addAll(newClassPath);
mercury.getProcessors().add(MercuryRemapper.create(mappings));
boolean isSrcTmp = false;
if (!Files.isDirectory(tmpInput)) {
Path tmpInput1 = tmpInput;
// create tmp directory
isSrcTmp = true;
tmpInput = Files.createTempDirectory("fabric-loom-src");
ZipUtil.unpack(tmpInput1.toFile(), tmpInput.toFile());
}
try (FileSystemUtil.FileSystemDelegate outputFs = FileSystemUtil.getJarFileSystem(tmpOutput, true)) {
Path outputFsRoot = outputFs.get().getPath("/");
mercury.rewrite(tmpInput, outputFsRoot);
} catch (Exception e) {
project.getLogger().warn("Could not remap " + tmpInput + " fully!", e);
}
if (isSrcTmp) {
Files.walkFileTree(tmpInput, new DeletingFileVisitor());
}
}
private static Map<String, byte[]> extractSources(List<Path> forgeInstallerSources) throws IOException {
Map<String, byte[]> sources = new ConcurrentHashMap<>();
ThreadingUtils.TaskCompleter taskCompleter = ThreadingUtils.taskCompleter();
for (Path path : forgeInstallerSources) {
FileSystemUtil.FileSystemDelegate system = FileSystemUtil.getJarFileSystem(path, false);
taskCompleter.onComplete(stopwatch -> system.close());
for (Path filePath : (Iterable<? extends Path>) Files.walk(system.get().getPath("/"))::iterator) {
if (Files.isRegularFile(filePath) && filePath.getFileName().toString().endsWith(".java")) {
taskCompleter.add(() -> sources.put(filePath.toString(), Files.readAllBytes(filePath)));
}
}
}
taskCompleter.complete();
return sources;
}
}

View File

@@ -35,6 +35,7 @@ import java.util.stream.Collectors;
import javax.inject.Inject;
import org.gradle.api.Project;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.TaskAction;
@@ -42,6 +43,7 @@ import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.api.decompilers.DecompilationMetadata;
import net.fabricmc.loom.api.decompilers.LoomDecompiler;
import net.fabricmc.loom.configuration.providers.mappings.MappingsProvider;
import net.fabricmc.loom.configuration.sources.ForgeSourcesRemapper;
import net.fabricmc.loom.decompilers.LineNumberRemapper;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.gradle.ProgressLogger;
@@ -81,6 +83,10 @@ public class GenerateSourcesTask extends AbstractLoomTask {
Files.copy(linemappedJarDestination, runtimeJar, StandardCopyOption.REPLACE_EXISTING);
Files.delete(linemappedJarDestination);
}
if (getExtension().isForge()) {
ForgeSourcesRemapper.addForgeSources(getProject(), sourcesDestination);
}
}
private void remapLineNumbers(Path oldCompiledJar, Path linemap, Path linemappedJarDestination) throws IOException {
@@ -100,7 +106,11 @@ public class GenerateSourcesTask extends AbstractLoomTask {
}
private File getMappedJarFileWithSuffix(String suffix) {
LoomGradleExtension extension = getProject().getExtensions().getByType(LoomGradleExtension.class);
return getMappedJarFileWithSuffix(getProject(), suffix);
}
public static File getMappedJarFileWithSuffix(Project project, String suffix) {
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
MappingsProvider mappingsProvider = extension.getMappingsProvider();
File mappedJar = mappingsProvider.mappedProvider.getMappedJar();
String path = mappedJar.getAbsolutePath();

View File

@@ -1,3 +1,27 @@
/*
* 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;
public enum ModPlatform {

View File

@@ -34,7 +34,6 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -134,7 +133,7 @@ public class ThreadingUtils {
Stopwatch stopwatch = Stopwatch.createUnstarted();
List<CompletableFuture<?>> tasks = new ArrayList<>();
ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
List<Consumer<Stopwatch>> completionListener = new ArrayList<>();
List<UnsafeConsumer<Stopwatch>> completionListener = new ArrayList<>();
public TaskCompleter add(UnsafeRunnable job) {
if (!stopwatch.isRunning()) {
@@ -152,7 +151,7 @@ public class ThreadingUtils {
return this;
}
public TaskCompleter onComplete(Consumer<Stopwatch> consumer) {
public TaskCompleter onComplete(UnsafeConsumer<Stopwatch> consumer) {
completionListener.add(consumer);
return this;
}
@@ -161,13 +160,20 @@ public class ThreadingUtils {
try {
CompletableFuture.allOf(tasks.toArray(new CompletableFuture[0])).exceptionally(this).get();
service.shutdownNow();
stopwatch.stop();
for (Consumer<Stopwatch> consumer : completionListener) {
consumer.accept(stopwatch);
if (stopwatch.isRunning()) {
stopwatch.stop();
}
} catch (InterruptedException | ExecutionException e) {
} catch (Throwable e) {
throw new RuntimeException(e);
} finally {
try {
for (UnsafeConsumer<Stopwatch> consumer : completionListener) {
consumer.accept(stopwatch);
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}