From 4ca20fb39e6bb8357177d81163b48399da1a4a80 Mon Sep 17 00:00:00 2001 From: Fudge Date: Wed, 13 Nov 2019 14:32:44 +0200 Subject: [PATCH] Allow source jars to use their own parameter names for mod deps (#147) * formatting * spaaaace --- build.gradle | 2 +- .../loom/util/ModCompileRemapper.java | 4 ++-- .../net/fabricmc/loom/util/ModProcessor.java | 24 ++++++++++++------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 4e6261c..8d6f457 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ dependencies { } // tinyfile management - implementation ('net.fabricmc:tiny-remapper:0.2.0.56') { + implementation ('net.fabricmc:tiny-remapper:0.2.0.57') { transitive = false } diff --git a/src/main/java/net/fabricmc/loom/util/ModCompileRemapper.java b/src/main/java/net/fabricmc/loom/util/ModCompileRemapper.java index aed60ab..d670341 100644 --- a/src/main/java/net/fabricmc/loom/util/ModCompileRemapper.java +++ b/src/main/java/net/fabricmc/loom/util/ModCompileRemapper.java @@ -128,7 +128,7 @@ public class ModCompileRemapper { if (!output.exists() || input.lastModified() <= 0 || input.lastModified() > output.lastModified()) { //If the output doesn't exist, or appears to be outdated compared to the input we'll remap it try { - ModProcessor.processMod(input, output, project, config); + ModProcessor.processMod(input, output, project, config, artifact); } catch (IOException e) { throw new RuntimeException("Failed to remap mod", e); } @@ -143,7 +143,7 @@ public class ModCompileRemapper { } } - private static File findSources(DependencyHandler dependencies, ResolvedArtifact artifact) { + public static File findSources(DependencyHandler dependencies, ResolvedArtifact artifact) { @SuppressWarnings("unchecked") ArtifactResolutionQuery query = dependencies.createArtifactResolutionQuery()// .forComponents(artifact.getId().getComponentIdentifier())// .withArtifacts(JvmLibrary.class, SourcesArtifact.class); diff --git a/src/main/java/net/fabricmc/loom/util/ModProcessor.java b/src/main/java/net/fabricmc/loom/util/ModProcessor.java index 6e2cb37..99b6c8d 100644 --- a/src/main/java/net/fabricmc/loom/util/ModProcessor.java +++ b/src/main/java/net/fabricmc/loom/util/ModProcessor.java @@ -43,6 +43,7 @@ import com.google.gson.JsonObject; import org.apache.commons.io.IOUtils; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.ResolvedArtifact; import org.zeroturnaround.zip.ZipUtil; import org.zeroturnaround.zip.commons.FileUtils; import org.zeroturnaround.zip.transform.StringZipEntryTransformer; @@ -57,23 +58,23 @@ import net.fabricmc.tinyremapper.TinyRemapper; public class ModProcessor { private static final Gson GSON = new Gson(); - public static void processMod(File input, File output, Project project, Configuration config) throws IOException { + public static void processMod(File input, File output, Project project, Configuration config, ResolvedArtifact artifact) throws IOException { if (output.exists()) { output.delete(); } - remapJar(input, output, project); + remapJar(input, output, project, artifact); //Enable this if you want your nested jars to be extracted, this will extract **all** jars if (project.getExtensions().getByType(LoomGradleExtension.class).extractJars) { - handleNestedJars(input, project, config); + handleNestedJars(input, project, config, artifact); } //Always strip the nested jars stripNestedJars(output); } - private static void handleNestedJars(File input, Project project, Configuration config) throws IOException { + private static void handleNestedJars(File input, Project project, Configuration config, ResolvedArtifact artifact) throws IOException { JarFile jarFile = new JarFile(input); JarEntry modJsonEntry = jarFile.getJarEntry("fabric.mod.json"); @@ -94,12 +95,12 @@ public class ModProcessor { JsonObject jsonObject = jsonArray.get(i).getAsJsonObject(); String fileName = jsonObject.get("file").getAsString(); project.getLogger().lifecycle(String.format("Found %s nested in %s", fileName, input.getName())); - processNestedJar(jarFile, fileName, project, config); + processNestedJar(jarFile, fileName, project, config, artifact); } } } - private static void processNestedJar(JarFile parentJar, String fileName, Project project, Configuration config) throws IOException { + private static void processNestedJar(JarFile parentJar, String fileName, Project project, Configuration config, ResolvedArtifact artifact) throws IOException { LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); JarEntry entry = parentJar.getJarEntry(fileName); @@ -116,7 +117,7 @@ public class ModProcessor { File remappedFile = new File(extension.getRemappedModCache(), fileName.substring(fileName.lastIndexOf("/"))); - processMod(nestedFile, remappedFile, project, config); + processMod(nestedFile, remappedFile, project, config, artifact); if (!remappedFile.exists()) { throw new RuntimeException("Failed to find processed nested jar"); @@ -138,7 +139,7 @@ public class ModProcessor { }))}); } - private static void remapJar(File input, File output, Project project) throws IOException { + private static void remapJar(File input, File output, Project project, ResolvedArtifact artifact) throws IOException { LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); String fromM = "intermediary"; String toM = "named"; @@ -163,9 +164,14 @@ public class ModProcessor { project.getLogger().lifecycle(":remapping " + input.getName() + " (TinyRemapper, " + fromM + " -> " + toM + ")"); + // If the sources don't exist, we want remapper to give nicer names to the missing variable names. + // However, if the sources do exist, if remapper gives names to the parameters that prevents IDEs (at least IDEA) + // from replacing the parameters with the actual names from the sources. + boolean sourcesExist = ModCompileRemapper.findSources(project.getDependencies(), artifact) != null; + TinyRemapper remapper = TinyRemapper.newRemapper() .withMappings(TinyRemapperMappingsHelper.create(mappingsProvider.getMappings(), fromM, toM, false)) - .renameInvalidLocals(true) + .renameInvalidLocals(!sourcesExist) .build(); try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(Paths.get(output.getAbsolutePath())).build()) {