|
| 1 | +package org.fury_phoenix.mixinAp.annotation; |
| 2 | + |
| 3 | +import java.lang.annotation.Annotation; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.function.Function; |
| 10 | +import java.util.stream.Collectors; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +import javax.annotation.processing.Messager; |
| 14 | +import javax.annotation.processing.ProcessingEnvironment; |
| 15 | +import javax.lang.model.element.AnnotationValue; |
| 16 | +import javax.lang.model.element.TypeElement; |
| 17 | +import javax.lang.model.type.TypeMirror; |
| 18 | +import javax.lang.model.util.Elements; |
| 19 | +import javax.lang.model.util.Types; |
| 20 | +import javax.tools.Diagnostic; |
| 21 | + |
| 22 | +import net.fabricmc.api.Environment; |
| 23 | + |
| 24 | +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; |
| 25 | +import org.embeddedt.modernfix.annotation.IgnoreMixin; |
| 26 | +import org.fury_phoenix.mixinAp.util.TypedAccessorMap; |
| 27 | +import org.spongepowered.asm.mixin.Mixin; |
| 28 | + |
| 29 | +import static com.google.auto.common.AnnotationMirrors.getAnnotationValue; |
| 30 | +import static java.util.AbstractMap.SimpleImmutableEntry; |
| 31 | + |
| 32 | +public class ClientMixinValidator { |
| 33 | + |
| 34 | + private final Messager messager; |
| 35 | + |
| 36 | + private final Elements elemUtils; |
| 37 | + |
| 38 | + private final Types types; |
| 39 | + |
| 40 | + private final boolean debug; |
| 41 | + |
| 42 | + private static final TypedAccessorMap<Annotation> markers = new TypedAccessorMap<>(); |
| 43 | + |
| 44 | + private static final Map.Entry<Class<Environment>, Function<? super Environment, ?>> |
| 45 | + FabricAccessor = new SimpleImmutableEntry<>(Environment.class, Environment::value); |
| 46 | + |
| 47 | + private static final Map.Entry< |
| 48 | + Class<net.minecraftforge.api.distmarker.OnlyIn>, |
| 49 | + Function<? super net.minecraftforge.api.distmarker.OnlyIn, ?>> |
| 50 | + ForgeAccessor = new SimpleImmutableEntry<>( |
| 51 | + net.minecraftforge.api.distmarker.OnlyIn.class, |
| 52 | + net.minecraftforge.api.distmarker.OnlyIn::value |
| 53 | + ); |
| 54 | + |
| 55 | + private static final Map.Entry< |
| 56 | + Class<net.neoforged.api.distmarker.OnlyIn>, |
| 57 | + Function<? super net.neoforged.api.distmarker.OnlyIn, ?>> |
| 58 | + NeoForgeAccessor = new SimpleImmutableEntry<>( |
| 59 | + net.neoforged.api.distmarker.OnlyIn.class, |
| 60 | + net.neoforged.api.distmarker.OnlyIn::value |
| 61 | + ); |
| 62 | + |
| 63 | + static { |
| 64 | + markers.put(FabricAccessor); |
| 65 | + markers.put(ForgeAccessor); |
| 66 | + markers.put(NeoForgeAccessor); |
| 67 | + } |
| 68 | + |
| 69 | + private static final Collection<String> unannotatedClasses = new HashSet<>(); |
| 70 | + |
| 71 | + public ClientMixinValidator(ProcessingEnvironment env) { |
| 72 | + debug = Boolean.valueOf(env.getOptions().get("org.fury_phoenix.mixinAp.validator.debug")); |
| 73 | + messager = env.getMessager(); |
| 74 | + elemUtils = env.getElementUtils(); |
| 75 | + types = env.getTypeUtils(); |
| 76 | + } |
| 77 | + |
| 78 | + public boolean validateMixin(TypeElement annotatedMixinClass) { |
| 79 | + return targetsClient(annotatedMixinClass) && |
| 80 | + (annotatedMixinClass.getAnnotation(ClientOnlyMixin.class) == null); |
| 81 | + } |
| 82 | + |
| 83 | + public boolean targetsClient(TypeElement annotatedMixinClass) { |
| 84 | + return targetsClient(getTargets(annotatedMixinClass)) && |
| 85 | + !isIgnored(annotatedMixinClass); |
| 86 | + } |
| 87 | + |
| 88 | + private boolean targetsClient(Collection<?> classTargets) { |
| 89 | + return classTargets.stream().anyMatch(this::targetsClient); |
| 90 | + } |
| 91 | + |
| 92 | + private boolean targetsClient(Object classTarget) { |
| 93 | + return switch (classTarget) { |
| 94 | + case TypeElement te -> |
| 95 | + isClientMarked(te); |
| 96 | + case TypeMirror tm -> { |
| 97 | + var el = types.asElement(tm); |
| 98 | + yield el != null ? targetsClient(el) : warn("TypeMirror of " + tm); |
| 99 | + } |
| 100 | + // If you're using a dollar sign in class names you are insane |
| 101 | + case String s -> { |
| 102 | + var te = |
| 103 | + elemUtils.getTypeElement(toSourceString(s.split("\\$")[0])); |
| 104 | + yield te != null ? targetsClient(te) : warn(s); |
| 105 | + } |
| 106 | + default -> |
| 107 | + throw new IllegalArgumentException("Unhandled type: " |
| 108 | + + classTarget.getClass() + "\n" + "Stringified contents: " |
| 109 | + + classTarget.toString()); |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + private boolean isClientMarked(TypeElement te) { |
| 114 | + for (var entry : markers.entrySet()) { |
| 115 | + var marker = te.getAnnotation(entry.getKey()); |
| 116 | + if(marker == null) continue; |
| 117 | + |
| 118 | + return entry.getValue().apply(marker).toString().equals("CLIENT"); |
| 119 | + } |
| 120 | + if(debug && unannotatedClasses.add(te.toString())) { |
| 121 | + messager.printMessage(Diagnostic.Kind.WARNING, |
| 122 | + "No marker annotations present on " + te + "!"); |
| 123 | + } |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + private boolean isIgnored(TypeElement te) { |
| 128 | + if(te.getAnnotation(IgnoreMixin.class) != null) { |
| 129 | + messager.printMessage(Diagnostic.Kind.WARNING, |
| 130 | + toSourceString(te.toString()) + " is ignored!"); |
| 131 | + return true; |
| 132 | + } |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + private boolean warn(Object o) { |
| 137 | + messager.printMessage(Diagnostic.Kind.WARNING, |
| 138 | + toSourceString(o.toString()) + " can't be loaded, so it is skipped!"); |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + public Map.Entry<? extends CharSequence, ? extends CharSequence> |
| 143 | + getClientMixinEntry(TypeElement annotatedMixinClass) { |
| 144 | + return new SimpleImmutableEntry<>( |
| 145 | + annotatedMixinClass.getQualifiedName(), |
| 146 | + getTargets(annotatedMixinClass) |
| 147 | + .stream() |
| 148 | + .filter(this::targetsClient) |
| 149 | + .map(Object::toString) |
| 150 | + .map(ClientMixinValidator::toSourceString) |
| 151 | + .collect(Collectors.joining(", ")) |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + private Collection<Object> getTargets(TypeElement annotatedMixinClass) { |
| 156 | + Collection<? extends TypeMirror> clzsses = Set.of(); |
| 157 | + Collection<? extends String> imaginaries = Set.of(); |
| 158 | + TypeMirror MixinElement = elemUtils.getTypeElement(Mixin.class.getName()).asType(); |
| 159 | + for (var mirror : annotatedMixinClass.getAnnotationMirrors()) { |
| 160 | + if(!types.isSameType(mirror.getAnnotationType(), MixinElement)) |
| 161 | + continue; |
| 162 | + |
| 163 | + @SuppressWarnings("unchecked") |
| 164 | + var wrappedClzss = (List<? extends AnnotationValue>) |
| 165 | + getAnnotationValue(mirror, "value").getValue(); |
| 166 | + |
| 167 | + clzsses = wrappedClzss.stream() |
| 168 | + .map(AnnotationValue::getValue) |
| 169 | + .map(TypeMirror.class::cast) |
| 170 | + .collect(Collectors.toSet()); |
| 171 | + |
| 172 | + @SuppressWarnings("unchecked") |
| 173 | + var wrappedStrings = (List<? extends AnnotationValue>) |
| 174 | + getAnnotationValue(mirror, "targets").getValue(); |
| 175 | + |
| 176 | + imaginaries = wrappedStrings.stream() |
| 177 | + .map(AnnotationValue::getValue) |
| 178 | + .map(String.class::cast) |
| 179 | + .collect(Collectors.toSet()); |
| 180 | + } |
| 181 | + return Stream.of(clzsses, imaginaries) |
| 182 | + .flatMap(Collection::stream) |
| 183 | + .collect(Collectors.toSet()); |
| 184 | + } |
| 185 | + |
| 186 | + public static String toSourceString(String bytecodeName) { |
| 187 | + return bytecodeName.replaceAll("\\/", "."); |
| 188 | + } |
| 189 | +} |
0 commit comments