|
| 1 | +package template.annotations.processors.impl; |
| 2 | + |
| 3 | +import arc.*; |
| 4 | +import arc.graphics.g2d.*; |
| 5 | +import arc.struct.*; |
| 6 | +import arc.util.*; |
| 7 | +import com.squareup.javapoet.*; |
| 8 | +import mindustry.ctype.*; |
| 9 | +import template.annotations.Annotations.*; |
| 10 | +import template.annotations.processors.*; |
| 11 | + |
| 12 | +import javax.annotation.processing.*; |
| 13 | +import javax.lang.model.element.*; |
| 14 | +import javax.lang.model.type.*; |
| 15 | +import java.util.*; |
| 16 | + |
| 17 | +/** |
| 18 | + * Whenever a {@link arc.graphics.g2d.TextureRegion TextureRegion} is annotated with {@link Load @Load}, |
| 19 | + * it'll generate a finder method inside the ContentRegionRegistry, which must be called for every |
| 20 | + * content in {@link mindustry.game.EventType.ContentInitEvent ContentInitEvent} |
| 21 | + */ |
| 22 | +public class RegionsProcessor extends BaseProcessor { |
| 23 | + public ObjectMap<Element, Seq<Element>> annotated = new ObjectMap<>(); |
| 24 | + |
| 25 | + { |
| 26 | + rounds = 1; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public Set<String> getSupportedAnnotationTypes() { |
| 31 | + Set<String> types = new HashSet<>(); |
| 32 | + String prefix = processingEnv.getOptions().get("modName") + ".annotations.Annotations."; |
| 33 | + types.add(prefix + "Load"); |
| 34 | + types.add(prefix + "EnsureLoad"); |
| 35 | + return types; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void process(RoundEnvironment roundEnv) throws Exception { |
| 40 | + if (round == 1) { |
| 41 | + TypeSpec.Builder regionsClass = TypeSpec.classBuilder(classPrefix + "ContentRegionRegistry") |
| 42 | + .addModifiers(Modifier.PUBLIC) |
| 43 | + .addJavadoc("Class generated for loading regions annotated with {@link template.annotations.Annotations.Load load}"); |
| 44 | + |
| 45 | + MethodSpec.Builder loadMethod = MethodSpec.methodBuilder("load") |
| 46 | + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) |
| 47 | + .addParameter(tName(MappableContent.class), "content"); |
| 48 | + |
| 49 | + for (Element element : roundEnv.getElementsAnnotatedWith(Load.class)) { |
| 50 | + TypeMirror type = element.asType(); |
| 51 | + while (type.getKind() == TypeKind.ARRAY) { |
| 52 | + type = ((ArrayType) type).getComponentType(); |
| 53 | + } |
| 54 | + if (!types.isSameType(type, toType(TextureRegion.class).asType())) { |
| 55 | + throw new IllegalAccessException("Only TextureRegions should be annotated with @Load"); |
| 56 | + } |
| 57 | + |
| 58 | + annotated.get(element.getEnclosingElement(), Seq::new).add(element); |
| 59 | + } |
| 60 | + |
| 61 | + for (Element base : annotated.keys().toSeq()) { |
| 62 | + if (!types.isSameType(base.asType(), toType(MappableContent.class).asType())) { |
| 63 | + throw new IllegalAccessException("@Load annotated TextureRegions must have a MappableContent enclosing class"); |
| 64 | + } |
| 65 | + |
| 66 | + loadMethod.beginControlFlow("if (content instanceof $T)", cName(base)); |
| 67 | + |
| 68 | + for (Element field : annotated.get(base)) { |
| 69 | + Load annotation = field.getAnnotation(Load.class); |
| 70 | + |
| 71 | + StringBuilder depth = new StringBuilder(); |
| 72 | + for (int i = 0; i < annotation.lengths().length; i++) { |
| 73 | + loadMethod.beginControlFlow( |
| 74 | + "for (int INDEX$L = 0; INDEX$L < $L; INDEX$L++)", |
| 75 | + i, i, annotation.lengths()[i], i |
| 76 | + ); |
| 77 | + |
| 78 | + depth.append("[INDEX").append(i).append("]"); |
| 79 | + } |
| 80 | + |
| 81 | + loadMethod.addStatement( |
| 82 | + "(($T) content).$L$L = $T.atlas.find($L, $L)", |
| 83 | + cName(base), |
| 84 | + field.getSimpleName(), |
| 85 | + depth.toString(), |
| 86 | + cName(Core.class), |
| 87 | + parse(annotation.value()), |
| 88 | + parse(annotation.fallBack()) |
| 89 | + ); |
| 90 | + |
| 91 | + for (int i = 0; i < annotation.lengths().length; i++) { |
| 92 | + loadMethod.endControlFlow(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + loadMethod.endControlFlow(); |
| 97 | + } |
| 98 | + |
| 99 | + regionsClass.addMethod(loadMethod.build()); |
| 100 | + |
| 101 | + write(regionsClass.build()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public String parse(String other) { |
| 106 | + other = '"' + other + '"'; |
| 107 | + return other |
| 108 | + .replace("@modname", modName) |
| 109 | + .replace("@size", "\" + ((mindustry.world.Block) content).size + \"") |
| 110 | + .replace("@", "\" + content.name + \"") |
| 111 | + .replace("#", "\" + INDEX") |
| 112 | + .replace("$", " + \""); |
| 113 | + } |
| 114 | +} |
0 commit comments