Allow source jars to use their own parameter names for mod deps (#147)
* formatting * spaaaace
This commit is contained in:
		
							parent
							
								
									54ac2df5dc
								
							
						
					
					
						commit
						4ca20fb39e
					
				
					 3 changed files with 18 additions and 12 deletions
				
			
		|  | @ -44,7 +44,7 @@ dependencies { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// tinyfile management | 	// tinyfile management | ||||||
| 	implementation ('net.fabricmc:tiny-remapper:0.2.0.56') { | 	implementation ('net.fabricmc:tiny-remapper:0.2.0.57') { | ||||||
| 		transitive = false | 		transitive = false | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -128,7 +128,7 @@ public class ModCompileRemapper { | ||||||
| 		if (!output.exists() || input.lastModified() <= 0 || input.lastModified() > output.lastModified()) { | 		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 | 			//If the output doesn't exist, or appears to be outdated compared to the input we'll remap it | ||||||
| 			try { | 			try { | ||||||
| 				ModProcessor.processMod(input, output, project, config); | 				ModProcessor.processMod(input, output, project, config, artifact); | ||||||
| 			} catch (IOException e) { | 			} catch (IOException e) { | ||||||
| 				throw new RuntimeException("Failed to remap mod", 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()// | 		@SuppressWarnings("unchecked") ArtifactResolutionQuery query = dependencies.createArtifactResolutionQuery()// | ||||||
| 				.forComponents(artifact.getId().getComponentIdentifier())// | 				.forComponents(artifact.getId().getComponentIdentifier())// | ||||||
| 				.withArtifacts(JvmLibrary.class, SourcesArtifact.class); | 				.withArtifacts(JvmLibrary.class, SourcesArtifact.class); | ||||||
|  |  | ||||||
|  | @ -43,6 +43,7 @@ import com.google.gson.JsonObject; | ||||||
| import org.apache.commons.io.IOUtils; | import org.apache.commons.io.IOUtils; | ||||||
| import org.gradle.api.Project; | import org.gradle.api.Project; | ||||||
| import org.gradle.api.artifacts.Configuration; | import org.gradle.api.artifacts.Configuration; | ||||||
|  | import org.gradle.api.artifacts.ResolvedArtifact; | ||||||
| import org.zeroturnaround.zip.ZipUtil; | import org.zeroturnaround.zip.ZipUtil; | ||||||
| import org.zeroturnaround.zip.commons.FileUtils; | import org.zeroturnaround.zip.commons.FileUtils; | ||||||
| import org.zeroturnaround.zip.transform.StringZipEntryTransformer; | import org.zeroturnaround.zip.transform.StringZipEntryTransformer; | ||||||
|  | @ -57,23 +58,23 @@ import net.fabricmc.tinyremapper.TinyRemapper; | ||||||
| public class ModProcessor { | public class ModProcessor { | ||||||
| 	private static final Gson GSON = new Gson(); | 	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()) { | 		if (output.exists()) { | ||||||
| 			output.delete(); | 			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 | 		//Enable this if you want your nested jars to be extracted, this will extract **all** jars | ||||||
| 		if (project.getExtensions().getByType(LoomGradleExtension.class).extractJars) { | 		if (project.getExtensions().getByType(LoomGradleExtension.class).extractJars) { | ||||||
| 			handleNestedJars(input, project, config); | 			handleNestedJars(input, project, config, artifact); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		//Always strip the nested jars | 		//Always strip the nested jars | ||||||
| 		stripNestedJars(output); | 		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); | 		JarFile jarFile = new JarFile(input); | ||||||
| 		JarEntry modJsonEntry = jarFile.getJarEntry("fabric.mod.json"); | 		JarEntry modJsonEntry = jarFile.getJarEntry("fabric.mod.json"); | ||||||
| 
 | 
 | ||||||
|  | @ -94,12 +95,12 @@ public class ModProcessor { | ||||||
| 				JsonObject jsonObject = jsonArray.get(i).getAsJsonObject(); | 				JsonObject jsonObject = jsonArray.get(i).getAsJsonObject(); | ||||||
| 				String fileName = jsonObject.get("file").getAsString(); | 				String fileName = jsonObject.get("file").getAsString(); | ||||||
| 				project.getLogger().lifecycle(String.format("Found %s nested in %s", fileName, input.getName())); | 				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); | 		LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); | ||||||
| 
 | 
 | ||||||
| 		JarEntry entry = parentJar.getJarEntry(fileName); | 		JarEntry entry = parentJar.getJarEntry(fileName); | ||||||
|  | @ -116,7 +117,7 @@ public class ModProcessor { | ||||||
| 
 | 
 | ||||||
| 		File remappedFile = new File(extension.getRemappedModCache(), fileName.substring(fileName.lastIndexOf("/"))); | 		File remappedFile = new File(extension.getRemappedModCache(), fileName.substring(fileName.lastIndexOf("/"))); | ||||||
| 
 | 
 | ||||||
| 		processMod(nestedFile, remappedFile, project, config); | 		processMod(nestedFile, remappedFile, project, config, artifact); | ||||||
| 
 | 
 | ||||||
| 		if (!remappedFile.exists()) { | 		if (!remappedFile.exists()) { | ||||||
| 			throw new RuntimeException("Failed to find processed nested jar"); | 			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); | 		LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); | ||||||
| 		String fromM = "intermediary"; | 		String fromM = "intermediary"; | ||||||
| 		String toM = "named"; | 		String toM = "named"; | ||||||
|  | @ -163,9 +164,14 @@ public class ModProcessor { | ||||||
| 
 | 
 | ||||||
| 		project.getLogger().lifecycle(":remapping " + input.getName() + " (TinyRemapper, " + fromM + " -> " + toM + ")"); | 		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() | 		TinyRemapper remapper = TinyRemapper.newRemapper() | ||||||
| 						.withMappings(TinyRemapperMappingsHelper.create(mappingsProvider.getMappings(), fromM, toM, false)) | 						.withMappings(TinyRemapperMappingsHelper.create(mappingsProvider.getMappings(), fromM, toM, false)) | ||||||
| 						.renameInvalidLocals(true) | 						.renameInvalidLocals(!sourcesExist) | ||||||
| 						.build(); | 						.build(); | ||||||
| 
 | 
 | ||||||
| 		try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(Paths.get(output.getAbsolutePath())).build()) { | 		try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(Paths.get(output.getAbsolutePath())).build()) { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue