From e69ab1fef28cf32f55627f02e40d29d13cda6ac0 Mon Sep 17 00:00:00 2001 From: Fury_Phoenix <64714532+Phoenix-Starlight@users.noreply.github.com> Date: Sat, 16 Dec 2023 16:45:13 -0800 Subject: [PATCH] Customize reshadowing of dependencies Refactor AP detection --- annotation-processor/build.gradle | 18 +++ .../annotation/ClientMixinValidator.java | 123 ++++++++++-------- 2 files changed, 84 insertions(+), 57 deletions(-) diff --git a/annotation-processor/build.gradle b/annotation-processor/build.gradle index 776dfa72..223d94bc 100644 --- a/annotation-processor/build.gradle +++ b/annotation-processor/build.gradle @@ -23,6 +23,11 @@ dependencies { implementation project(":annotations") shadow project(":annotations") + // Shadow annotations + implementation 'net.fabricmc:sponge-mixin:0.12.5+' + implementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}" + implementation 'net.minecraftforge:mergetool:1.1.7' + implementation 'net.neoforged:mergetool:2.0.2' } tasks.withType(JavaCompile) { @@ -31,7 +36,20 @@ tasks.withType(JavaCompile) { } shadowJar { + dependencies { + include(dependency('net.fabricmc:sponge-mixin:')) + include(dependency('net.fabricmc:fabric-loader:')) + include(dependency(':mergetool:')) + } + // shadowJar bug + include '*.jar' include 'META-INF/services/javax.annotation.processing.Processor' + include 'org/spongepowered/asm/mixin/Mixin.class' + include 'org/fury_phoenix/**/*' + include {it.getName() == 'OnlyIn.class'} + include {it.getName() == 'Dist.class'} + include {it.getName() == 'Environment.class'} + include {it.getName() == 'EnvType.class'} } spotless { diff --git a/annotation-processor/src/main/java/org/fury_phoenix/mixinAp/annotation/ClientMixinValidator.java b/annotation-processor/src/main/java/org/fury_phoenix/mixinAp/annotation/ClientMixinValidator.java index 7f534710..e21b4b79 100644 --- a/annotation-processor/src/main/java/org/fury_phoenix/mixinAp/annotation/ClientMixinValidator.java +++ b/annotation-processor/src/main/java/org/fury_phoenix/mixinAp/annotation/ClientMixinValidator.java @@ -3,13 +3,16 @@ package org.fury_phoenix.mixinAp.annotation; import java.lang.annotation.Annotation; import java.util.Collection; import java.util.HashSet; -import java.util.Optional; +import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.processing.Messager; import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Elements; @@ -18,17 +21,12 @@ import javax.tools.Diagnostic; import org.embeddedt.modernfix.annotation.ClientOnlyMixin; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.util.asm.IAnnotationHandle; -import org.spongepowered.tools.obfuscation.AnnotatedMixinsAccessor; -import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider; -import org.spongepowered.tools.obfuscation.mirror.TypeHandle; +import static com.google.auto.common.AnnotationMirrors.getAnnotationValue; import static java.util.AbstractMap.SimpleImmutableEntry; public class ClientMixinValidator { - private final ITypeHandleProvider typeHandleProvider; - private final Messager messager; private final Elements elemUtils; @@ -37,16 +35,33 @@ public class ClientMixinValidator { private final boolean debug; - private static final Iterable markers = Set.of( - "net.fabricmc.api.Environment", - "net.minecraftforge.api.distmarker.OnlyIn", - "net.neoforged.api.distmarker.OnlyIn"); + /* + * @author Fury_Phoenix + * @reason This is covariant for ClientMixinValidator.markers + * whilst the direct reference is not as it doesn't cover Annotation + */ + private static final Function + EnvironmentAccessor = net.fabricmc.api.Environment::value; + + private static final Function + ForgeAccessor = net.minecraftforge.api.distmarker.OnlyIn::value; + + private static final Function + NeoForgeAccessor = net.neoforged.api.distmarker.OnlyIn::value; + + /* + * @author Fury_Phoenix + * @reason Partial duck-typing + */ + private static final Map + , Function> + markers = Map.of(net.fabricmc.api.Environment.class, EnvironmentAccessor, + net.minecraftforge.api.distmarker.OnlyIn.class, ForgeAccessor, + net.neoforged.api.distmarker.OnlyIn.class, NeoForgeAccessor); private static final Collection unannotatedClasses = new HashSet<>(); - public ClientMixinValidator(ProcessingEnvironment env) - throws ReflectiveOperationException { - typeHandleProvider = AnnotatedMixinsAccessor.getMixinAP(env); + public ClientMixinValidator(ProcessingEnvironment env) { debug = Boolean.valueOf(env.getOptions().get("org.fury_phoenix.mixinAp.validator.debug")); messager = env.getMessager(); elemUtils = env.getElementUtils(); @@ -55,14 +70,11 @@ public class ClientMixinValidator { public boolean validateMixin(TypeElement annotatedMixinClass) { return targetsClient(annotatedMixinClass) && - !getAnnotationHandle(annotatedMixinClass, ClientOnlyMixin.class).exists(); + (annotatedMixinClass.getAnnotation(ClientOnlyMixin.class) == null); } public boolean targetsClient(TypeElement annotatedMixinClass) { - return targetsClient( - ClientMixinValidator.getTargets( - getAnnotationHandle(annotatedMixinClass, Mixin.class) - )); + return targetsClient(getTargets(annotatedMixinClass)); } private boolean targetsClient(Collection classTargets) { @@ -91,14 +103,15 @@ public class ClientMixinValidator { } private boolean isClientMarked(TypeElement te) { - for (var marker : getPlatformMarkers(markers)) { - IAnnotationHandle handle = getAnnotationHandle(te, marker); - if(!handle.exists()) continue; + for (var entry : markers.entrySet()) { + var marker = te.getAnnotation(entry.getKey()); + if(marker == null) continue; - String[] enumValue = handle.getValue("value"); - if(enumValue==null) continue; - - return enumValue[1].equals("CLIENT"); + // Pretend to accept Annotations + @SuppressWarnings("unchecked") + boolean isClient = ((Function)entry.getValue()) + .apply(marker).toString().equals("CLIENT"); + return isClient; } if(debug && unannotatedClasses.add(te.toString())) { messager.printMessage(Diagnostic.Kind.WARNING, @@ -117,9 +130,8 @@ public class ClientMixinValidator { getEntry(TypeElement annotatedMixinClass) { return new SimpleImmutableEntry<>( annotatedMixinClass.getQualifiedName(), - ClientMixinValidator.getTargets( - getAnnotationHandle(annotatedMixinClass, Mixin.class) - ).stream() + getTargets(annotatedMixinClass) + .stream() .filter(this::targetsClient) .map(Object::toString) .map(ClientMixinValidator::toSourceString) @@ -127,40 +139,37 @@ public class ClientMixinValidator { ); } - private TypeHandle getTypeHandle(Object annotatedClass) { - return typeHandleProvider.getTypeHandle(annotatedClass); - } + private Collection getTargets(TypeElement mixinAnnotatedClass) { + Collection clzsses = Set.of(); + Collection imaginaries = Set.of(); + TypeMirror MixinElement = elemUtils.getTypeElement(Mixin.class.getName()).asType(); + for (var annotationMirror : mixinAnnotatedClass.getAnnotationMirrors()) { + if(!annotationMirror.getAnnotationType().equals(MixinElement)) + continue; - private IAnnotationHandle getAnnotationHandle - (Object annotatedClass, Class annotation) { - return getTypeHandle(annotatedClass).getAnnotation(annotation); - } + @SuppressWarnings("unchecked") + var wrappedClzss = (List) + getAnnotationValue(annotationMirror, "value").getValue(); - private static Collection getTargets(IAnnotationHandle mixinAnnotation) { - Collection clzss = mixinAnnotation.getList("value"); - Collection imaginary = mixinAnnotation.getList("targets"); - return Stream.of(clzss, imaginary) + clzsses = wrappedClzss.stream() + .map(AnnotationValue::getValue) + .map(TypeMirror.class::cast) + .collect(Collectors.toSet()); + + @SuppressWarnings("unchecked") + var wrappedStrings = (List) + getAnnotationValue(annotationMirror, "targets").getValue(); + + imaginaries = wrappedStrings.stream() + .map(AnnotationValue::getValue) + .map(String.class::cast) + .collect(Collectors.toSet()); + } + return Stream.of(clzsses, imaginaries) .flatMap(Collection::stream) .collect(Collectors.toList()); } - private static Iterable> - getPlatformMarkers(Iterable markers) { - Set> platformClasses = new HashSet<>(); - for(var marker : markers) { - getMarkerClass(marker).ifPresent(platformClasses::add); - } - return platformClasses; - } - - @SuppressWarnings("unchecked") - private static Optional> getMarkerClass(String marker) { - try { - return Optional.of((Class)Class.forName(marker)); - } catch (ClassNotFoundException e) {} - return Optional.empty(); - } - public static String toSourceString(String bytecodeName) { return bytecodeName.replaceAll("\\/", "."); }