// Compile-time Java checks cannot see the string descriptors used by ClassTransform. tasks.register('validateHooks') { dependsOn tasks.named('compileJava') inputs.files(tasks.named('compileJava').flatMap { it.destinationDirectory }, sourceSets.main.compileClasspath) def report = layout.buildDirectory.file('reports/hooks.txt') outputs.file(report) doLast { def jars = sourceSets.main.compileClasspath.files.findAll { it.name.endsWith('.jar') } .collect { new java.util.zip.ZipFile(it) } try { def cache = [:] Closure readClass = { String name -> if (cache.containsKey(name)) return cache[name] def zip = jars.find { it.getEntry("${name}.class") != null } if (zip == null) return null def node = hookAsm.node() hookAsm.reader(zip.getInputStream(zip.getEntry("${name}.class"))).accept(node, 0) cache[name] = node node } def annotations = { node -> (node.visibleAnnotations ?: []) + (node.invisibleAnnotations ?: []) } def properties = { annotation -> def result = [:] def values = annotation.values ?: [] for (int i = 0; i < values.size(); i += 2) result[values[i]] = values[i + 1] result } def errors = [] int handlers = 0 fileTree(tasks.named('compileJava').get().destinationDirectory).matching { include 'dev/zprestige/prestige/mixin/*.class', 'dev/zprestige/prestige/inject/hooks/*.class' }.each { file -> def hook = hookAsm.node() hookAsm.reader(file.bytes).accept(hook, 0) def transformer = annotations(hook).find { it.desc.endsWith('/CTransformer;') } if (transformer == null) return def targets = properties(transformer).value.collect { readClass(it.internalName) } if (targets.any { it == null }) errors.add("${hook.name}: missing target class") targets.findAll { it != null }.each { target -> hook.fields.each { field -> def shadow = annotations(field).find { it.desc.endsWith('/CShadow;') } if (shadow != null) { def name = properties(shadow).value ?: field.name def owner = target def found = false while (owner != null && !found) { found = owner.fields.any { it.name == name && it.desc == field.desc } owner = found ? null : readClass(owner.superName) } if (!found) errors.add("${hook.name}.${field.name}: missing shadow ${target.name}.${name} ${field.desc}") } } hook.methods.each { method -> annotations(method).findAll { it.desc.contains('/annotations/injection/') }.each { annotation -> def config = properties(annotation) (config.method ?: []).each { selector -> def matches = target.methods.findAll { selector.contains('(') ? it.name + it.desc == selector : it.name == selector } handlers++ if (matches.isEmpty()) errors.add("${hook.name}.${method.name}: missing ${target.name}.${selector}") if (annotation.desc.endsWith('/CInject;')) { def parameters = hookAsm.arguments(method.desc).toList() if (parameters && parameters.last().descriptor == 'Lnet/lenni0451/classtransform/InjectionCallback;') parameters.removeLast() if (parameters && !matches.any { hookAsm.arguments(it.desc).toList() == parameters }) { errors.add("${hook.name}.${method.name}: callback parameters ${parameters} do not match ${target.name}.${selector} ${matches.collect { it.desc }}") } } def points = config.target instanceof List ? config.target : config.target ? [config.target] : [] points.each { point -> def injection = properties(point) if (injection.value == 'INVOKE') { def expected = injection.target def found = matches.any { candidate -> candidate.instructions.any { instruction -> hookAsm.methodInstruction(instruction) && "L${instruction.owner};${instruction.name}${instruction.desc}" == expected } } if (!found) errors.add("${hook.name}.${method.name}: missing invocation ${expected} in ${target.name}.${selector}") } } } } } } } report.get().asFile.parentFile.mkdirs() report.get().asFile.text = "Checked ${handlers} handlers for Minecraft ${project.minecraft_version}\n" + errors.join('\n') + '\n' if (errors) throw new GradleException("${errors.size()} hook compatibility errors; see ${report.get().asFile}") logger.lifecycle("Verified ${handlers} hook handlers for Minecraft ${project.minecraft_version}") } finally { jars.each { it.close() } } } } tasks.named('check') { dependsOn tasks.named('validateHooks') }