fabric/fabric-transitive-access-wideners-v1/build.gradle
Technici4n b808eab9dd
Add more transitive Access Wideners (#2780)
* Add more transitive Access Wideners

- Expose `StateProviderType` constructor. Fixes #474.
- Expose DefaultBiomeCreator.getSkyColor. Fixes #981.
- Make custom `RenderLayer` registration easier. Fixes #1635.
- Add warning at the beginning of the generated AW to prevent manual
  editing of the file.

* Add warning to datagen generated AW
2022-12-23 15:23:14 +00:00

93 lines
3.3 KiB
Groovy

archivesBaseName = "fabric-transitive-access-wideners-v1"
version = getSubprojectVersion(project)
loom {
accessWidenerPath = file('src/main/resources/fabric-transitive-access-wideners-v1.accesswidener')
}
import org.objectweb.asm.ClassReader
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import org.objectweb.asm.tree.ClassNode
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.util.stream.Collectors
task generateAccessWidener {
doLast {
List<String> lines = new ArrayList<>();
lines.add("accessWidener v2 named")
lines.add("")
lines.add("# DO NOT EDIT BY HAND! This file is generated automatically.")
lines.add("# Edit \"template.accesswidener\" instead then run \"gradlew generateAccessWidener\".")
lines.add("")
lines.addAll(file("template.accesswidener").text.lines().toList())
Path commonJar = loom.namedMinecraftProvider.parentMinecraftProvider.commonJar
try (def fs = FileSystems.newFileSystem(URI.create("jar:${commonJar.toUri()}"), [create: false])) {
generateBlockConstructors(lines, fs)
lines.add("")
}
Path clientJar = loom.namedMinecraftProvider.parentMinecraftProvider.clientOnlyJar
try (def fs = FileSystems.newFileSystem(URI.create("jar:${clientJar.toUri()}"), [create: false])) {
generateRenderPhaseFields(lines, fs)
}
file('src/main/resources/fabric-transitive-access-wideners-v1.accesswidener').text = String.join('\n', lines) + '\n'
}
}
def generateBlockConstructors(List<String> lines, FileSystem fs) {
lines.add("# Constructors of non-abstract block classes")
Files.list(fs.getPath("net/minecraft/block"))
.filter { Files.isRegularFile(it) && it.toString().endsWith(".class") }
.map { loadClass(it) }
.sorted(Comparator.comparing { it.name })
.filter { (it.access & Opcodes.ACC_ABSTRACT) == 0 }
.forEach { node ->
for (def method : node.methods) {
// Checklist for finding block constructors as of 1.18.2:
// - class directly in net.minecraft.block (excluding subpackages)
// - method name == <init> (by definition)
// - contains an AbstractBlock$Settings parameter
// - only taking into account non-abstract classes and non-public constructors
// Block constructor...
if (method.name == "<init>" && Type.getArgumentTypes(method.desc).any { it.internalName == 'net/minecraft/block/AbstractBlock$Settings' }) {
// ...and non-public
if ((method.access & Opcodes.ACC_PUBLIC) == 0) {
lines.add("transitive-accessible method $node.name <init> $method.desc")
}
}
}
}
}
def generateRenderPhaseFields(List<String> lines, FileSystem fs) {
lines.add("# Protected static fields of RenderPhase")
for (def field : loadClass(fs.getPath("net/minecraft/client/render/RenderPhase.class")).fields) {
// All protected static fields of RenderPhase
if ((field.access & Opcodes.ACC_PROTECTED) != 0 && (field.access & Opcodes.ACC_STATIC) != 0) {
lines.add("transitive-accessible field net/minecraft/client/render/RenderPhase ${field.name} ${field.desc}")
}
}
}
ClassNode loadClass(Path path) {
def node = new ClassNode()
try (def is = Files.newInputStream(path)) {
new ClassReader(is).accept(node, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES)
}
return node
}
generateResources.dependsOn generateAccessWidener