|
| 1 | +/* |
| 2 | + * This file is part of fabric-loom, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) 2021 FabricMC |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package net.fabricmc.loom.configuration.decompile; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import org.gradle.api.Action; |
| 34 | +import org.gradle.api.Project; |
| 35 | +import org.gradle.api.Task; |
| 36 | +import org.gradle.api.tasks.TaskProvider; |
| 37 | + |
| 38 | +import net.fabricmc.loom.api.decompilers.DecompilerOptions; |
| 39 | +import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar; |
| 40 | +import net.fabricmc.loom.configuration.providers.minecraft.mapped.MappedMinecraftProvider; |
| 41 | +import net.fabricmc.loom.task.GenerateSourcesTask; |
| 42 | +import net.fabricmc.loom.util.Constants; |
| 43 | +import net.fabricmc.loom.util.Strings; |
| 44 | + |
| 45 | +public final class MultiJarDecompileConfiguration extends DecompileConfiguration<MappedMinecraftProvider> { |
| 46 | + public MultiJarDecompileConfiguration(Project project, MappedMinecraftProvider minecraftProvider) { |
| 47 | + super(project, minecraftProvider); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getTaskName(MinecraftJar.Type type) { |
| 52 | + return "gen%sSources".formatted(Strings.capitalize(type.toString())); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void afterEvaluation() { |
| 57 | + Map<String, List<TaskProvider<Task>>> decompTasks = new HashMap<>(); // decomp name -> tasks |
| 58 | + Map<MinecraftJar.Type, TaskProvider<Task>> decompOuterTasks = new HashMap<>(); // jar type -> task |
| 59 | + |
| 60 | + for (DecompilerOptions options : extension.getDecompilerOptions()) { |
| 61 | + decompTasks.put(options.getFormattedName(), new ArrayList<>()); |
| 62 | + } |
| 63 | + |
| 64 | + for (MinecraftJar minecraftJar : minecraftProvider.getMinecraftJars()) { |
| 65 | + if (decompOuterTasks.containsKey(minecraftJar.getType())) { |
| 66 | + throw new IllegalArgumentException("Duplicate jar type %s".formatted(minecraftJar.getType())); |
| 67 | + } |
| 68 | + |
| 69 | + TaskProvider<Task> taskProvider = createDecompileTasks(minecraftJar.getType(), decompTasks, task -> { |
| 70 | + task.getInputJarName().set(minecraftJar.getName()); |
| 71 | + task.getSourcesOutputJar().fileValue(GenerateSourcesTask.getJarFileWithSuffix("-sources.jar", minecraftJar.getPath())); |
| 72 | + }); |
| 73 | + decompOuterTasks.put(minecraftJar.getType(), taskProvider); |
| 74 | + } |
| 75 | + |
| 76 | + // chain tasks so only one can run at a time |
| 77 | + { |
| 78 | + List<TaskProvider<Task>> list = decompTasks.values().stream().flatMap(Collection::stream).toList(); |
| 79 | + |
| 80 | + for (int i = 1, listSize = list.size(); i < listSize; i++) { |
| 81 | + TaskProvider<Task> prev = list.get(i - 1); |
| 82 | + TaskProvider<Task> cur = list.get(i); |
| 83 | + cur.configure(task -> task.mustRunAfter(prev)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + for (Map.Entry<String, List<TaskProvider<Task>>> entry : decompTasks.entrySet()) { |
| 88 | + String decompilerName = entry.getKey(); |
| 89 | + List<TaskProvider<Task>> tasks = entry.getValue(); |
| 90 | + project.getTasks().register("genSourcesWith" + decompilerName, task -> { |
| 91 | + task.setDescription("Decompile minecraft using %s.".formatted(decompilerName)); |
| 92 | + task.setGroup(Constants.TaskGroup.FABRIC); |
| 93 | + |
| 94 | + tasks.forEach(task::dependsOn); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + project.getTasks().register("genSources", task -> { |
| 99 | + task.setDescription("Decompile minecraft using the default decompiler."); |
| 100 | + task.setGroup(Constants.TaskGroup.FABRIC); |
| 101 | + |
| 102 | + decompOuterTasks.values().forEach(task::dependsOn); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + private TaskProvider<Task> createDecompileTasks(MinecraftJar.Type type, Map<String, List<TaskProvider<Task>>> decompTasks, Action<GenerateSourcesTask> configureAction) { |
| 107 | + extension.getDecompilerOptions().forEach(options -> { |
| 108 | + final String decompilerName = options.getFormattedName(); |
| 109 | + final String taskName = "%sWith%s".formatted(getTaskName(type), decompilerName); |
| 110 | + |
| 111 | + TaskProvider<GenerateSourcesTask> taskProvider = project.getTasks().register(taskName, GenerateSourcesTask.class, options); |
| 112 | + taskProvider.configure(task -> { |
| 113 | + configureAction.execute(task); |
| 114 | + task.dependsOn(project.getTasks().named("validateAccessWidener")); |
| 115 | + task.setDescription("Decompile minecraft using %s.".formatted(decompilerName)); |
| 116 | + task.setGroup(Constants.TaskGroup.FABRIC); |
| 117 | + }); |
| 118 | + |
| 119 | + decompTasks.get(decompilerName).add((TaskProvider<Task>) (Object) taskProvider); |
| 120 | + }); |
| 121 | + |
| 122 | + return project.getTasks().register(getTaskName(type), task -> { |
| 123 | + task.setDescription("Decompile minecraft (%s) using the default decompiler.".formatted(type)); |
| 124 | + task.setGroup(Constants.TaskGroup.FABRIC); |
| 125 | + |
| 126 | + task.dependsOn(project.getTasks().named("%sWith%s".formatted(getTaskName(type), DecompileConfiguration.DEFAULT_DECOMPILER))); |
| 127 | + }); |
| 128 | + } |
| 129 | +} |
0 commit comments