parent
							
								
									32837d71eb
								
							
						
					
					
						commit
						e124d48387
					
				
					 9 changed files with 99 additions and 73 deletions
				
			
		|  | @ -25,13 +25,21 @@ | ||||||
| package net.fabricmc.loom.build; | package net.fabricmc.loom.build; | ||||||
| 
 | 
 | ||||||
| import java.io.File; | import java.io.File; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStreamReader; | ||||||
| import java.nio.file.Path; | import java.nio.file.Path; | ||||||
|  | import java.util.Collection; | ||||||
| import java.util.Objects; | import java.util.Objects; | ||||||
|  | import java.util.stream.Collectors; | ||||||
| import java.util.stream.Stream; | import java.util.stream.Stream; | ||||||
|  | import java.util.stream.StreamSupport; | ||||||
| import java.util.zip.ZipEntry; | import java.util.zip.ZipEntry; | ||||||
|  | import java.util.zip.ZipFile; | ||||||
| 
 | 
 | ||||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||||
|  | import com.google.gson.JsonPrimitive; | ||||||
| import org.gradle.api.Project; | import org.gradle.api.Project; | ||||||
|  | import org.jetbrains.annotations.NotNull; | ||||||
| import org.zeroturnaround.zip.ZipUtil; | import org.zeroturnaround.zip.ZipUtil; | ||||||
| import org.zeroturnaround.zip.transform.StringZipEntryTransformer; | import org.zeroturnaround.zip.transform.StringZipEntryTransformer; | ||||||
| import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry; | import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry; | ||||||
|  | @ -43,29 +51,72 @@ import net.fabricmc.loom.extension.MixinApExtension; | ||||||
| public final class MixinRefmapHelper { | public final class MixinRefmapHelper { | ||||||
| 	private MixinRefmapHelper() { } | 	private MixinRefmapHelper() { } | ||||||
| 
 | 
 | ||||||
|  | 	private static final String FABRIC_MOD_JSON = "fabric.mod.json"; | ||||||
|  | 
 | ||||||
| 	public static boolean addRefmapName(Project project, Path outputPath) { | 	public static boolean addRefmapName(Project project, Path outputPath) { | ||||||
| 		MixinApExtension mixin = LoomGradleExtension.get(project).getMixin(); | 		try { | ||||||
| 		File output = outputPath.toFile(); | 			MixinApExtension mixin = LoomGradleExtension.get(project).getMixin(); | ||||||
|  | 			File output = outputPath.toFile(); | ||||||
| 
 | 
 | ||||||
| 		return mixin.getMixinSourceSetsStream().map(sourceSet -> { | 			Collection<String> allMixinConfigs = getMixinConfigurationFiles(readFabricModJson(output)); | ||||||
| 			MixinApExtension.MixinInformationContainer container = Objects.requireNonNull( |  | ||||||
| 					MixinApExtension.getMixinInformationContainer(sourceSet) |  | ||||||
| 			); |  | ||||||
| 			Stream<String> mixinJsonNames = container.getMixinJsonNames(); |  | ||||||
| 			String refmapName = container.getRefmapName(); |  | ||||||
| 
 | 
 | ||||||
| 			return ZipUtil.transformEntries(output, mixinJsonNames.map(f -> new ZipEntryTransformerEntry(f, new StringZipEntryTransformer("UTF-8") { | 			return mixin.getMixinSourceSetsStream().map(sourceSet -> { | ||||||
| 				@Override | 				MixinApExtension.MixinInformationContainer container = Objects.requireNonNull( | ||||||
| 				protected String transform(ZipEntry zipEntry, String input) { | 						MixinApExtension.getMixinInformationContainer(sourceSet) | ||||||
| 					JsonObject json = LoomGradlePlugin.GSON.fromJson(input, JsonObject.class); | 				); | ||||||
| 
 | 
 | ||||||
| 					if (!json.has("refmap")) { | 				Stream<String> mixinConfigs = sourceSet.getResources() | ||||||
| 						json.addProperty("refmap", refmapName); | 						.matching(container.mixinConfigPattern()) | ||||||
|  | 						.getFiles() | ||||||
|  | 						.stream() | ||||||
|  | 						.map(File::getName) | ||||||
|  | 						.filter(allMixinConfigs::contains); | ||||||
|  | 
 | ||||||
|  | 				String refmapName = container.refmapNameProvider().get(); | ||||||
|  | 
 | ||||||
|  | 				return ZipUtil.transformEntries(output, mixinConfigs.map(f -> new ZipEntryTransformerEntry(f, new StringZipEntryTransformer("UTF-8") { | ||||||
|  | 					@Override | ||||||
|  | 					protected String transform(ZipEntry zipEntry, String input) { | ||||||
|  | 						JsonObject json = LoomGradlePlugin.GSON.fromJson(input, JsonObject.class); | ||||||
|  | 
 | ||||||
|  | 						if (!json.has("refmap")) { | ||||||
|  | 							json.addProperty("refmap", refmapName); | ||||||
|  | 						} | ||||||
|  | 
 | ||||||
|  | 						return LoomGradlePlugin.GSON.toJson(json); | ||||||
| 					} | 					} | ||||||
|  | 				})).toArray(ZipEntryTransformerEntry[]::new)); | ||||||
|  | 			}).reduce(false, Boolean::logicalOr); | ||||||
|  | 		} catch (Exception e) { | ||||||
|  | 			project.getLogger().error(e.getMessage()); | ||||||
|  | 			return false; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 					return LoomGradlePlugin.GSON.toJson(json); | 	@NotNull | ||||||
| 				} | 	private static JsonObject readFabricModJson(File output) { | ||||||
| 			})).toArray(ZipEntryTransformerEntry[]::new)); | 		try (ZipFile zip = new ZipFile(output)) { | ||||||
| 		}).reduce(false, Boolean::logicalOr); | 			ZipEntry entry = zip.getEntry(FABRIC_MOD_JSON); | ||||||
|  | 
 | ||||||
|  | 			try (InputStreamReader reader = new InputStreamReader(zip.getInputStream(entry))) { | ||||||
|  | 				return LoomGradlePlugin.GSON.fromJson(reader, JsonObject.class); | ||||||
|  | 			} | ||||||
|  | 		} catch (IOException e) { | ||||||
|  | 			throw new RuntimeException("Cannot read file fabric.mod.json in the output jar.", e); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@NotNull | ||||||
|  | 	private static Collection<String> getMixinConfigurationFiles(JsonObject fabricModJson) { | ||||||
|  | 		return StreamSupport.stream(fabricModJson.getAsJsonArray("mixins").spliterator(), false) | ||||||
|  | 				.map(e -> { | ||||||
|  | 					if (e instanceof JsonPrimitive str) { | ||||||
|  | 						return str.getAsString(); | ||||||
|  | 					} else if (e instanceof JsonObject obj) { | ||||||
|  | 						return obj.get("config").getAsString(); | ||||||
|  | 					} else { | ||||||
|  | 						throw new RuntimeException("Incorrect fabric.mod.json format"); | ||||||
|  | 					} | ||||||
|  | 				}).collect(Collectors.toSet()); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -80,7 +80,7 @@ public abstract class AnnotationProcessorInvoker<T extends Task> { | ||||||
| 	private void passMixinArguments(T task, SourceSet sourceSet) { | 	private void passMixinArguments(T task, SourceSet sourceSet) { | ||||||
| 		try { | 		try { | ||||||
| 			LoomGradleExtension loom = LoomGradleExtension.get(project); | 			LoomGradleExtension loom = LoomGradleExtension.get(project); | ||||||
| 			String refmapName = Objects.requireNonNull(MixinApExtension.getMixinInformationContainer(sourceSet)).getRefmapName(); | 			String refmapName = Objects.requireNonNull(MixinApExtension.getMixinInformationContainer(sourceSet)).refmapNameProvider().get(); | ||||||
| 			Map<String, String> args = new HashMap<>() {{ | 			Map<String, String> args = new HashMap<>() {{ | ||||||
| 					put(Constants.MixinArguments.IN_MAP_FILE_NAMED_INTERMEDIARY, loom.getMappingsProvider().tinyMappings.getCanonicalPath()); | 					put(Constants.MixinArguments.IN_MAP_FILE_NAMED_INTERMEDIARY, loom.getMappingsProvider().tinyMappings.getCanonicalPath()); | ||||||
| 					put(Constants.MixinArguments.OUT_MAP_FILE_NAMED_INTERMEDIARY, loom.getNextMixinMappings().getCanonicalPath()); | 					put(Constants.MixinArguments.OUT_MAP_FILE_NAMED_INTERMEDIARY, loom.getNextMixinMappings().getCanonicalPath()); | ||||||
|  |  | ||||||
|  | @ -82,7 +82,7 @@ public class KaptApInvoker extends AnnotationProcessorInvoker<JavaCompile> { | ||||||
| 			SourceSet sourceSet = entry.getKey(); | 			SourceSet sourceSet = entry.getKey(); | ||||||
| 			task.doLast(t -> { | 			task.doLast(t -> { | ||||||
| 				try { | 				try { | ||||||
| 					String refmapName = Objects.requireNonNull(MixinApExtension.getMixinInformationContainer(sourceSet)).getRefmapName(); | 					String refmapName = Objects.requireNonNull(MixinApExtension.getMixinInformationContainer(sourceSet)).refmapNameProvider().get(); | ||||||
| 					Path src = Paths.get(getRefmapDestination(task, refmapName)); | 					Path src = Paths.get(getRefmapDestination(task, refmapName)); | ||||||
| 					Path dest = Paths.get(task.getDestinationDir().toString(), refmapName); | 					Path dest = Paths.get(task.getDestinationDir().toString(), refmapName); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -26,7 +26,6 @@ package net.fabricmc.loom.extension; | ||||||
| 
 | 
 | ||||||
| import java.util.Collection; | import java.util.Collection; | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
| import java.util.Objects; |  | ||||||
| import java.util.function.Function; | import java.util.function.Function; | ||||||
| import java.util.stream.Stream; | import java.util.stream.Stream; | ||||||
| 
 | 
 | ||||||
|  | @ -56,42 +55,7 @@ public interface MixinApExtension extends MixinApExtensionAPI { | ||||||
| 	 * for configuring the mixin annotation processor. It's stored | 	 * for configuring the mixin annotation processor. It's stored | ||||||
| 	 * in [SourceSet].ext.mixin. | 	 * in [SourceSet].ext.mixin. | ||||||
| 	 */ | 	 */ | ||||||
| 	final class MixinInformationContainer { | 	record MixinInformationContainer(SourceSet sourceSet, Provider<String> refmapNameProvider, PatternSet mixinConfigPattern) { } | ||||||
| 		private final SourceSet sourceSet; |  | ||||||
| 		private final Provider<String> refmapName; |  | ||||||
| 		private Stream<String> mixinJsonNames; |  | ||||||
| 
 |  | ||||||
| 		final PatternSet mixinJsonPattern; |  | ||||||
| 
 |  | ||||||
| 		public MixinInformationContainer(@NotNull SourceSet sourceSet, |  | ||||||
| 										@NotNull Provider<String> refmapName, |  | ||||||
| 										@NotNull PatternSet mixinJsonPattern) { |  | ||||||
| 			this.sourceSet = sourceSet; |  | ||||||
| 			this.refmapName = refmapName; |  | ||||||
| 			this.mixinJsonPattern = mixinJsonPattern; |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		void setMixinJsonNames(@NotNull Stream<String> mixinJsonNames) { |  | ||||||
| 			if (this.mixinJsonNames == null) { |  | ||||||
| 				this.mixinJsonNames = mixinJsonNames; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		@NotNull |  | ||||||
| 		public Stream<String> getMixinJsonNames() { |  | ||||||
| 			return Objects.requireNonNull(mixinJsonNames); |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		@NotNull |  | ||||||
| 		public SourceSet getSourceSet() { |  | ||||||
| 			return sourceSet; |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		@NotNull |  | ||||||
| 		public String getRefmapName() { |  | ||||||
| 			return refmapName.get(); |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Nullable | 	@Nullable | ||||||
| 	static MixinInformationContainer getMixinInformationContainer(SourceSet sourceSet) { | 	static MixinInformationContainer getMixinInformationContainer(SourceSet sourceSet) { | ||||||
|  |  | ||||||
|  | @ -24,7 +24,6 @@ | ||||||
| 
 | 
 | ||||||
| package net.fabricmc.loom.extension; | package net.fabricmc.loom.extension; | ||||||
| 
 | 
 | ||||||
| import java.io.File; |  | ||||||
| import java.util.AbstractMap; | import java.util.AbstractMap; | ||||||
| import java.util.Collection; | import java.util.Collection; | ||||||
| import java.util.Collections; | import java.util.Collections; | ||||||
|  | @ -79,7 +78,7 @@ public class MixinApExtensionImpl extends MixinApExtensionApiImpl implements Mix | ||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	protected PatternSet add0(SourceSet sourceSet, Provider<String> refmapName) { | 	protected PatternSet add0(SourceSet sourceSet, Provider<String> refmapName) { | ||||||
| 		PatternSet pattern = new PatternSet().setIncludes(Collections.singletonList("*.mixins.json")); | 		PatternSet pattern = new PatternSet().setIncludes(Collections.singletonList("*.json")); | ||||||
| 		MixinApExtension.setMixinInformationContainer(sourceSet, new MixinApExtension.MixinInformationContainer(sourceSet, refmapName, pattern)); | 		MixinApExtension.setMixinInformationContainer(sourceSet, new MixinApExtension.MixinInformationContainer(sourceSet, refmapName, pattern)); | ||||||
| 
 | 
 | ||||||
| 		isDefault = false; | 		isDefault = false; | ||||||
|  | @ -91,19 +90,7 @@ public class MixinApExtensionImpl extends MixinApExtensionApiImpl implements Mix | ||||||
| 	@NotNull | 	@NotNull | ||||||
| 	public Stream<SourceSet> getMixinSourceSetsStream() { | 	public Stream<SourceSet> getMixinSourceSetsStream() { | ||||||
| 		return project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().stream() | 		return project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().stream() | ||||||
| 				.filter(sourceSet -> { | 				.filter(sourceSet -> MixinApExtension.getMixinInformationContainer(sourceSet) != null); | ||||||
| 					MixinApExtension.MixinInformationContainer container = MixinApExtension.getMixinInformationContainer(sourceSet); |  | ||||||
| 
 |  | ||||||
| 					if (container != null) { |  | ||||||
| 						PatternSet pattern = container.mixinJsonPattern; |  | ||||||
| 						Stream<String> mixinJsonNames = sourceSet.getResources() |  | ||||||
| 								.matching(pattern).getFiles().stream().map(File::getName); |  | ||||||
| 						container.setMixinJsonNames(mixinJsonNames); |  | ||||||
| 						return true; |  | ||||||
| 					} |  | ||||||
| 
 |  | ||||||
| 					return false; |  | ||||||
| 				}); |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
|  |  | ||||||
|  | @ -56,7 +56,7 @@ class MixinApAutoRefmapTest extends Specification implements ProjectTestTrait { | ||||||
|         def j1 = JsonParser.parseReader(new InputStreamReader(jar.getInputStream(jar.getEntry("main.mixins.json")))) |         def j1 = JsonParser.parseReader(new InputStreamReader(jar.getInputStream(jar.getEntry("main.mixins.json")))) | ||||||
|         j1.asJsonObject.getAsJsonPrimitive("refmap").getAsString() == "refmap0001.json" |         j1.asJsonObject.getAsJsonPrimitive("refmap").getAsString() == "refmap0001.json" | ||||||
| 
 | 
 | ||||||
|         def j2 = JsonParser.parseReader(new InputStreamReader(jar.getInputStream(jar.getEntry("m0.mixins.json")))) |         def j2 = JsonParser.parseReader(new InputStreamReader(jar.getInputStream(jar.getEntry("blabla.json")))) | ||||||
|         j2.asJsonObject.getAsJsonPrimitive("refmap").getAsString() == "refmap0002.json" |         j2.asJsonObject.getAsJsonPrimitive("refmap").getAsString() == "refmap0002.json" | ||||||
| 
 | 
 | ||||||
|         def j3 = JsonParser.parseReader(new InputStreamReader(jar.getInputStream(jar.getEntry("m1_1.mixins.json")))) |         def j3 = JsonParser.parseReader(new InputStreamReader(jar.getInputStream(jar.getEntry("m1_1.mixins.json")))) | ||||||
|  | @ -65,6 +65,9 @@ class MixinApAutoRefmapTest extends Specification implements ProjectTestTrait { | ||||||
|         def j4 = JsonParser.parseReader(new InputStreamReader(jar.getInputStream(jar.getEntry("m1_2.mixins.json")))) |         def j4 = JsonParser.parseReader(new InputStreamReader(jar.getInputStream(jar.getEntry("m1_2.mixins.json")))) | ||||||
|         !j4.asJsonObject.has("refmap") |         !j4.asJsonObject.has("refmap") | ||||||
| 
 | 
 | ||||||
|  |         def j5 = JsonParser.parseReader(new InputStreamReader(jar.getInputStream(jar.getEntry("irrelevant.mixins.json")))) | ||||||
|  |         !j5.asJsonObject.has("refmap") | ||||||
|  | 
 | ||||||
|         where: |         where: | ||||||
|         gradle              | _ |         gradle              | _ | ||||||
|         DEFAULT_GRADLE      | _ |         DEFAULT_GRADLE      | _ | ||||||
|  |  | ||||||
|  | @ -23,6 +23,13 @@ | ||||||
|     ] |     ] | ||||||
|   }, |   }, | ||||||
|   "mixins": [ |   "mixins": [ | ||||||
|  |     "main.mixins.json", | ||||||
|  |     { | ||||||
|  |       "config": "blabla.json", | ||||||
|  |       "environment": "client" | ||||||
|  |     }, | ||||||
|  |     "m1_1.mixins.json", | ||||||
|  |     "m1_2.mixins.json" | ||||||
|   ], |   ], | ||||||
| 
 | 
 | ||||||
|   "depends": { |   "depends": { | ||||||
|  |  | ||||||
|  | @ -0,0 +1,14 @@ | ||||||
|  | { | ||||||
|  |   "required": true, | ||||||
|  |   "minVersion": "0.8", | ||||||
|  |   "package": "net.fabricmc.example.mixin", | ||||||
|  |   "compatibilityLevel": "JAVA_16", | ||||||
|  |   "mixins": [ | ||||||
|  |   ], | ||||||
|  |   "client": [ | ||||||
|  |       "ExampleMixin0" | ||||||
|  |   ], | ||||||
|  |   "injectors": { | ||||||
|  |     "defaultRequire": 1 | ||||||
|  |   } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in a new issue