From f673579da80547699ea15e86745bea595e9ff85e Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Mon, 1 Jun 2020 12:17:58 +0100 Subject: [PATCH] Cleanup and fix dep remapping, also add better log output to source remapping. --- .../dependency/ModDependencyInfo.java | 7 +- .../loom/util/LoomDependencyManager.java | 9 +- .../loom/util/ModCompileRemapper.java | 94 +++++++++---------- .../net/fabricmc/loom/util/ModProcessor.java | 29 ++++-- .../fabricmc/loom/util/SourceRemapper.java | 15 ++- 5 files changed, 89 insertions(+), 65 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/processors/dependency/ModDependencyInfo.java b/src/main/java/net/fabricmc/loom/processors/dependency/ModDependencyInfo.java index 92e62b7..85f6b99 100644 --- a/src/main/java/net/fabricmc/loom/processors/dependency/ModDependencyInfo.java +++ b/src/main/java/net/fabricmc/loom/processors/dependency/ModDependencyInfo.java @@ -32,6 +32,7 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; import com.google.gson.JsonObject; +import org.gradle.api.artifacts.Configuration; import net.fabricmc.loom.util.ModProcessor; @@ -41,15 +42,19 @@ public class ModDependencyInfo { public final String version; public final String classifier; public final File inputFile; + public final File sourcesFile; + public final Configuration targetConfig; public final RemapData remapData; - public ModDependencyInfo(String group, String name, String version, String classifier, File inputFile, RemapData remapData) { + public ModDependencyInfo(String group, String name, String version, String classifier, File inputFile, File sourcesFile, Configuration targetConfig, RemapData remapData) { this.group = group; this.name = name; this.version = version; this.classifier = classifier; this.inputFile = inputFile; + this.sourcesFile = sourcesFile; + this.targetConfig = targetConfig; this.remapData = remapData; } diff --git a/src/main/java/net/fabricmc/loom/util/LoomDependencyManager.java b/src/main/java/net/fabricmc/loom/util/LoomDependencyManager.java index a2b0e8e..81cb42a 100644 --- a/src/main/java/net/fabricmc/loom/util/LoomDependencyManager.java +++ b/src/main/java/net/fabricmc/loom/util/LoomDependencyManager.java @@ -117,14 +117,9 @@ public class LoomDependencyManager { } SourceRemapper sourceRemapper = new SourceRemapper(project, true); + String mappingsKey = mappingsProvider.mappingsName + "." + mappingsProvider.minecraftVersion.replace(' ', '_').replace('.', '_').replace('-', '_') + "." + mappingsProvider.mappingsVersion; - { - String mappingsKey = mappingsProvider.mappingsName + "." + mappingsProvider.minecraftVersion.replace(' ', '_').replace('.', '_').replace('-', '_') + "." + mappingsProvider.mappingsVersion; - - for (RemappedConfigurationEntry entry : Constants.MOD_COMPILE_ENTRIES) { - ModCompileRemapper.remapDependencies(project, mappingsKey, extension, project.getConfigurations().getByName(entry.getSourceConfiguration()), project.getConfigurations().getByName(entry.getRemappedConfiguration()), project.getConfigurations().getByName(entry.getTargetConfiguration(project.getConfigurations())), sourceRemapper); - } - } + ModCompileRemapper.remapDependencies(project, mappingsKey, extension, sourceRemapper); if (extension.getInstallerJson() == null) { //If we've not found the installer JSON we've probably skipped remapping Fabric loader, let's go looking diff --git a/src/main/java/net/fabricmc/loom/util/ModCompileRemapper.java b/src/main/java/net/fabricmc/loom/util/ModCompileRemapper.java index df05427..d140e3e 100644 --- a/src/main/java/net/fabricmc/loom/util/ModCompileRemapper.java +++ b/src/main/java/net/fabricmc/loom/util/ModCompileRemapper.java @@ -28,7 +28,6 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; import java.util.zip.ZipFile; import org.gradle.api.Project; @@ -51,66 +50,67 @@ import net.fabricmc.loom.processors.dependency.ModDependencyInfo; import net.fabricmc.loom.processors.dependency.RemapData; public class ModCompileRemapper { - public static void remapDependencies(Project project, String mappingsSuffix, LoomGradleExtension extension, Configuration modCompile, Configuration modCompileRemapped, Configuration regularCompile, SourceRemapper sourceRemapper) { + public static void remapDependencies(Project project, String mappingsSuffix, LoomGradleExtension extension, SourceRemapper sourceRemapper) { Logger logger = project.getLogger(); DependencyHandler dependencies = project.getDependencies(); final File modStore = extension.getRemappedModCache(); final RemapData remapData = new RemapData(mappingsSuffix, modStore); - final List modDependencies = new ArrayList<>(); + for (RemappedConfigurationEntry entry : Constants.MOD_COMPILE_ENTRIES) { + Configuration sourceConfig = project.getConfigurations().getByName(entry.getSourceConfiguration()); + Configuration remappedConfig = project.getConfigurations().getByName(entry.getRemappedConfiguration()); + Configuration regularConfig = project.getConfigurations().getByName(entry.getTargetConfiguration(project.getConfigurations())); - for (ResolvedArtifact artifact : modCompile.getResolvedConfiguration().getResolvedArtifacts()) { - String group; - String name; - String version; - String classifierSuffix = artifact.getClassifier() == null ? "" : (":" + artifact.getClassifier()); + List modDependencies = new ArrayList<>(); - if (artifact.getId().getComponentIdentifier() instanceof ModuleComponentIdentifier) { - group = ((ModuleComponentIdentifier) artifact.getId().getComponentIdentifier()).getGroup(); - name = ((ModuleComponentIdentifier) artifact.getId().getComponentIdentifier()).getModule(); - version = ((ModuleComponentIdentifier) artifact.getId().getComponentIdentifier()).getVersion(); - } else { - group = "net.fabricmc.synthetic"; - name = artifact.getId().getComponentIdentifier().getDisplayName().replace('.', '-').replace(" :", "-"); - version = "0.1.0"; + for (ResolvedArtifact artifact : sourceConfig.getResolvedConfiguration().getResolvedArtifacts()) { + String group; + String name; + String version; + String classifierSuffix = artifact.getClassifier() == null ? "" : (":" + artifact.getClassifier()); + + if (artifact.getId().getComponentIdentifier() instanceof ModuleComponentIdentifier) { + group = ((ModuleComponentIdentifier) artifact.getId().getComponentIdentifier()).getGroup(); + name = ((ModuleComponentIdentifier) artifact.getId().getComponentIdentifier()).getModule(); + version = ((ModuleComponentIdentifier) artifact.getId().getComponentIdentifier()).getVersion(); + } else { + group = "net.fabricmc.synthetic"; + name = artifact.getId().getComponentIdentifier().getDisplayName().replace('.', '-').replace(" :", "-"); + version = "0.1.0"; + } + + final String notation = group + ":" + name + ":" + version + classifierSuffix; + + if (!isFabricMod(project, logger, artifact, notation)) { + addToRegularCompile(project, regularConfig, notation); + continue; + } + + File sources = findSources(dependencies, artifact); + + ModDependencyInfo info = new ModDependencyInfo(group, name, version, classifierSuffix, artifact.getFile(), sources, remappedConfig, remapData); + modDependencies.add(info); + + String remappedLog = group + ":" + name + ":" + version + classifierSuffix + " (" + mappingsSuffix + ")"; + project.getLogger().info(":providing " + remappedLog); + + if (sources != null) { + scheduleSourcesRemapping(project, sourceRemapper, info.sourcesFile, info.getRemappedNotation(), info.getRemappedFilename(), modStore); + } } - final String notation = group + ":" + name + ":" + version + classifierSuffix; - - if (!isFabricMod(project, logger, artifact, notation)) { - addToRegularCompile(project, regularCompile, notation); - continue; + try { + ModProcessor.processMods(project, modDependencies); + } catch (IOException e) { + throw new RuntimeException("Failed to remap mods", e); } - File sources = findSources(dependencies, artifact); - - ModDependencyInfo info = new ModDependencyInfo(group, name, version, classifierSuffix, artifact.getFile(), remapData); - modDependencies.add(info); - - String remappedLog = group + ":" + name + ":" + version + classifierSuffix + " (" + mappingsSuffix + ")"; - String remappedFilename = String.format("%s-%s@%s", name, version, mappingsSuffix + classifierSuffix.replace(':', '-')); - project.getLogger().info(":providing " + remappedLog); - - if (sources != null) { - scheduleSourcesRemapping(project, sourceRemapper, sources, remappedLog, remappedFilename, modStore); + // Add all of the remapped mods onto the config + for (ModDependencyInfo info : modDependencies) { + project.getDependencies().add(info.targetConfig.getName(), project.getDependencies().module(info.getRemappedNotation())); } } - - List processList = modDependencies.stream() - .filter(ModDependencyInfo::requiresRemapping) - .collect(Collectors.toList()); - - try { - ModProcessor.processMods(project, processList); - } catch (IOException e) { - throw new RuntimeException("Failed to remap mods", e); - } - - // Add all of the remapped mods onto the config - for (ModDependencyInfo modDependency : modDependencies) { - project.getDependencies().add(modCompileRemapped.getName(), project.getDependencies().module(modDependency.getRemappedNotation())); - } } /** diff --git a/src/main/java/net/fabricmc/loom/util/ModProcessor.java b/src/main/java/net/fabricmc/loom/util/ModProcessor.java index b2e9746..bc1413e 100644 --- a/src/main/java/net/fabricmc/loom/util/ModProcessor.java +++ b/src/main/java/net/fabricmc/loom/util/ModProcessor.java @@ -37,6 +37,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.jar.JarFile; +import java.util.stream.Collectors; import java.util.zip.ZipEntry; import com.google.gson.Gson; @@ -63,12 +64,12 @@ public class ModProcessor { public static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); public static void processMods(Project project, List processList) throws IOException { - if (processList.isEmpty()) { + if (processList.stream().noneMatch(ModDependencyInfo::requiresRemapping)) { return; } for (ModDependencyInfo info : processList) { - if (info.getRemappedOutput().exists()) { + if (info.requiresRemapping() && info.getRemappedOutput().exists()) { info.getRemappedOutput().delete(); } } @@ -80,7 +81,9 @@ public class ModProcessor { throw new RuntimeException("Failed to remap mod" + info); } - stripNestedJars(info.getRemappedOutput()); + if (info.requiresRemapping()) { + stripNestedJars(info.getRemappedOutput()); + } } } @@ -124,7 +127,9 @@ public class ModProcessor { Path mc = mappedProvider.getIntermediaryJar().toPath(); Path[] mcDeps = mappedProvider.getMapperPaths().stream().map(File::toPath).toArray(Path[]::new); - project.getLogger().lifecycle(":remapping " + processList.size() + " mods (TinyRemapper, " + fromM + " -> " + toM + ")"); + List remapList = processList.stream().filter(ModDependencyInfo::requiresRemapping).collect(Collectors.toList()); + + project.getLogger().lifecycle(":remapping " + remapList.size() + " mods (TinyRemapper, " + fromM + " -> " + toM + ")"); TinyRemapper remapper = TinyRemapper.newRemapper() .withMappings(TinyRemapperMappingsHelper.create(mappingsProvider.getMappings(), fromM, toM, false)) @@ -138,14 +143,24 @@ public class ModProcessor { final Map outputConsumerMap = new HashMap<>(); final Map accessWidenerMap = new HashMap<>(); - for (ModDependencyInfo info : processList) { + for (RemappedConfigurationEntry entry : Constants.MOD_COMPILE_ENTRIES) { + for (File inputFile : project.getConfigurations().getByName(entry.getSourceConfiguration()).getFiles()) { + if (remapList.stream().noneMatch(info -> info.getInputFile().equals(inputFile))) { + project.getLogger().info("Adding " + inputFile + " onto the remap classpath"); + remapper.readClassPathAsync(inputFile.toPath()); + } + } + } + + for (ModDependencyInfo info : remapList) { InputTag tag = remapper.createInputTag(); + project.getLogger().info("Adding " + info.getInputFile() + " as a remap input"); remapper.readInputsAsync(tag, info.getInputFile().toPath()); tagMap.put(info, tag); } // Apply this in a second loop as we need to ensure all the inputs are on the classpath before remapping. - for (ModDependencyInfo info : processList) { + for (ModDependencyInfo info : remapList) { OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(info.getRemappedOutput().toPath()).build(); outputConsumer.addNonClassFiles(info.getInputFile().toPath()); outputConsumerMap.put(info, outputConsumer); @@ -160,7 +175,7 @@ public class ModProcessor { remapper.finish(); - for (ModDependencyInfo info : processList) { + for (ModDependencyInfo info : remapList) { outputConsumerMap.get(info).close(); byte[] accessWidener = accessWidenerMap.get(info); diff --git a/src/main/java/net/fabricmc/loom/util/SourceRemapper.java b/src/main/java/net/fabricmc/loom/util/SourceRemapper.java index d99e0d2..7507c1f 100644 --- a/src/main/java/net/fabricmc/loom/util/SourceRemapper.java +++ b/src/main/java/net/fabricmc/loom/util/SourceRemapper.java @@ -30,6 +30,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; import org.cadixdev.lorenz.MappingSet; import org.cadixdev.mercury.Mercury; @@ -39,6 +40,7 @@ import org.zeroturnaround.zip.ZipUtil; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.providers.MappingsProvider; +import net.fabricmc.loom.util.progress.ProgressLogger; import net.fabricmc.lorenztiny.TinyMappingsReader; import net.fabricmc.mapping.tree.TinyTree; import net.fabricmc.stitch.util.StitchUtil; @@ -46,7 +48,7 @@ import net.fabricmc.stitch.util.StitchUtil; public class SourceRemapper { private final Project project; private final boolean toNamed; - private final List remapTasks = new ArrayList<>(); + private final List> remapTasks = new ArrayList<>(); private Mercury mercury; @@ -62,8 +64,9 @@ public class SourceRemapper { } public void scheduleRemapSources(File source, File destination) throws Exception { - remapTasks.add(() -> { + remapTasks.add((logger) -> { try { + logger.progress("remapping sources - " + source.getName()); remapSourcesInner(source, destination); } catch (Exception e) { throw new RuntimeException("Failed to remap sources for " + source, e); @@ -76,7 +79,13 @@ public class SourceRemapper { project.getLogger().lifecycle(":remapping sources"); } - remapTasks.forEach(Runnable::run); + ProgressLogger progressLogger = ProgressLogger.getProgressFactory(project, SourceRemapper.class.getName()); + progressLogger.start("Remapping dependency sources", "sources"); + + remapTasks.forEach(consumer -> consumer.accept(progressLogger)); + + progressLogger.completed(); + // TODO: FIXME - WORKAROUND https://github.com/FabricMC/fabric-loom/issues/45 System.gc(); }