|  | 
|  | 1 | +package fish.cichlidmc.cichlid_gradle.cache.task.impl; | 
|  | 2 | + | 
|  | 3 | +import fish.cichlidmc.cichlid_gradle.cache.task.CacheTaskEnvironment; | 
|  | 4 | +import fish.cichlidmc.cichlid_gradle.cache.task.processor.ClassEntry; | 
|  | 5 | +import fish.cichlidmc.cichlid_gradle.cache.task.processor.ClassGroup; | 
|  | 6 | +import fish.cichlidmc.cichlid_gradle.cache.task.processor.JarProcessor; | 
|  | 7 | +import fish.cichlidmc.cichlid_gradle.extension.def.TransformersImpl; | 
|  | 8 | +import fish.cichlidmc.cichlid_gradle.util.ConfigurationPair; | 
|  | 9 | +import fish.cichlidmc.cichlid_gradle.util.Utils; | 
|  | 10 | +import fish.cichlidmc.cichlid_gradle.util.io.FileUtils; | 
|  | 11 | +import fish.cichlidmc.sushi.api.TransformerManager; | 
|  | 12 | +import fish.cichlidmc.sushi.api.util.Id; | 
|  | 13 | +import fish.cichlidmc.tinyjson.JsonException; | 
|  | 14 | +import fish.cichlidmc.tinyjson.TinyJson; | 
|  | 15 | +import fish.cichlidmc.tinyjson.value.JsonValue; | 
|  | 16 | +import fish.cichlidmc.tinyjson.value.composite.JsonObject; | 
|  | 17 | +import org.objectweb.asm.ClassReader; | 
|  | 18 | +import org.objectweb.asm.ClassWriter; | 
|  | 19 | +import org.objectweb.asm.tree.ClassNode; | 
|  | 20 | + | 
|  | 21 | +import java.io.File; | 
|  | 22 | +import java.io.IOException; | 
|  | 23 | +import java.nio.file.FileSystem; | 
|  | 24 | +import java.nio.file.FileSystems; | 
|  | 25 | +import java.nio.file.Files; | 
|  | 26 | +import java.nio.file.Path; | 
|  | 27 | +import java.util.Map; | 
|  | 28 | + | 
|  | 29 | +public record ClassTransformer(TransformerManager manager) implements JarProcessor.ClassProcessor { | 
|  | 30 | +	@Override | 
|  | 31 | +	public ClassGroup apply(ClassGroup group) throws IOException { | 
|  | 32 | +		return new ClassGroup(this.transform(group.main()), Utils.map(group.inner(), this::transform)); | 
|  | 33 | +	} | 
|  | 34 | + | 
|  | 35 | +	private ClassEntry transform(ClassEntry entry) throws IOException { | 
|  | 36 | +		if (entry.fileName().endsWith("package-info.class")) | 
|  | 37 | +			return entry; | 
|  | 38 | + | 
|  | 39 | +		byte[] bytes = entry.content().bytes(); | 
|  | 40 | +		ClassReader reader = new ClassReader(bytes); | 
|  | 41 | +		ClassNode node = new ClassNode(); | 
|  | 42 | +		reader.accept(node, 0); | 
|  | 43 | + | 
|  | 44 | +		if (this.manager.transform(node, reader)) { | 
|  | 45 | +			ClassWriter writer = new ClassWriter(reader, 0); | 
|  | 46 | +			node.accept(writer); | 
|  | 47 | +			return entry.withContent(writer.toByteArray()); | 
|  | 48 | +		} | 
|  | 49 | + | 
|  | 50 | +		return entry; | 
|  | 51 | +	} | 
|  | 52 | + | 
|  | 53 | +	public static ClassTransformer create(CacheTaskEnvironment env) throws IOException { | 
|  | 54 | +		TransformerManager manager = prepareTransformers(env.transformers); | 
|  | 55 | +		return new ClassTransformer(manager); | 
|  | 56 | +	} | 
|  | 57 | + | 
|  | 58 | +	private static TransformerManager prepareTransformers(TransformersImpl transformers) throws IOException { | 
|  | 59 | +		TransformerManager.Builder builder = TransformerManager.builder(); | 
|  | 60 | + | 
|  | 61 | +		for (File file : transformers.mods.resolve().getFiles()) { | 
|  | 62 | +			Path path = file.toPath(); | 
|  | 63 | +			if (Files.isDirectory(path)) { | 
|  | 64 | +				readTransformersFromMod(path, builder); | 
|  | 65 | +				continue; | 
|  | 66 | +			} | 
|  | 67 | + | 
|  | 68 | +			if (path.getFileName().toString().endsWith(".cld")) { | 
|  | 69 | +				try (FileSystem fs = FileSystems.newFileSystem(path)) { | 
|  | 70 | +					Path root = FileUtils.getSingleRoot(fs); | 
|  | 71 | +					readTransformersFromMod(root, builder); | 
|  | 72 | +				} | 
|  | 73 | + | 
|  | 74 | +				continue; | 
|  | 75 | +			} | 
|  | 76 | + | 
|  | 77 | +			throw new IllegalArgumentException("Transformer-providing mod is not in a valid format: " + file); | 
|  | 78 | +		} | 
|  | 79 | + | 
|  | 80 | +		for (Map.Entry<String, ConfigurationPair> entry : transformers.namespaced.entrySet()) { | 
|  | 81 | +			String namespace = entry.getKey(); | 
|  | 82 | +			for (File file : entry.getValue().resolve().getFiles()) { | 
|  | 83 | +				readTransformersFromTree(namespace, file.toPath(), builder); | 
|  | 84 | +			} | 
|  | 85 | +		} | 
|  | 86 | + | 
|  | 87 | +		return builder.build(); | 
|  | 88 | +	} | 
|  | 89 | + | 
|  | 90 | +	private static void readTransformersFromMod(Path root, TransformerManager.Builder builder) throws IOException { | 
|  | 91 | +		Path transformers = root.resolve("transformers"); | 
|  | 92 | +		if (!Files.exists(transformers)) | 
|  | 93 | +			return; | 
|  | 94 | + | 
|  | 95 | +		Path metadata = root.resolve("cichlid.mod.json"); | 
|  | 96 | +		if (!Files.exists(metadata)) { | 
|  | 97 | +			throw new IllegalArgumentException("Mod root does not contain metadata: " + root); | 
|  | 98 | +		} | 
|  | 99 | + | 
|  | 100 | +		try { | 
|  | 101 | +			JsonObject json = TinyJson.parse(metadata).asObject(); | 
|  | 102 | +			String id = json.get("id").asString().value(); | 
|  | 103 | +			readTransformersFromTree(id, transformers, builder); | 
|  | 104 | +		} catch (JsonException e) { | 
|  | 105 | +			throw new IllegalArgumentException("Mod root contains malformed metadata: " + root, e); | 
|  | 106 | +		} | 
|  | 107 | +	} | 
|  | 108 | + | 
|  | 109 | +	private static void readTransformersFromTree(String namespace, Path root, TransformerManager.Builder builder) throws IOException { | 
|  | 110 | +		FileUtils.walkFiles(root, file -> { | 
|  | 111 | +			String relative = root.relativize(file).toString(); | 
|  | 112 | +			if (!relative.endsWith(".sushi")) | 
|  | 113 | +				return; | 
|  | 114 | + | 
|  | 115 | +			String path = relative.substring(0, relative.length() - ".sushi".length()); | 
|  | 116 | +			if (!Id.isValidPath(path)) { | 
|  | 117 | +				throw new IllegalArgumentException("Mod '" + namespace + "' has a transformer with an invalid ID: " + path); | 
|  | 118 | +			} | 
|  | 119 | + | 
|  | 120 | +			Id id = new Id(namespace, path); | 
|  | 121 | + | 
|  | 122 | +			try { | 
|  | 123 | +				JsonValue json = TinyJson.parse(file); | 
|  | 124 | + | 
|  | 125 | +				builder.parseAndRegister(id, json).ifPresent(error -> { | 
|  | 126 | +					throw new IllegalArgumentException("Transformer " + id + " could not be registered: " + error); | 
|  | 127 | +				}); | 
|  | 128 | +			} catch (JsonException e) { | 
|  | 129 | +				throw new IllegalArgumentException("Transformer " + id + " is not valid JSON", e); | 
|  | 130 | +			} | 
|  | 131 | +		}); | 
|  | 132 | +	} | 
|  | 133 | +} | 
0 commit comments