From 6f769066c589f8a078a631411615a64c9a50fbac Mon Sep 17 00:00:00 2001 From: Fury_Phoenix <64714532+Phoenix-Starlight@users.noreply.github.com> Date: Sun, 10 Dec 2023 17:26:33 -0800 Subject: [PATCH] Don't rely on enums being present --- annotation-processor/build.gradle | 1 - .../annotation/ClientMixinValidator.java | 64 +++++-------------- 2 files changed, 15 insertions(+), 50 deletions(-) diff --git a/annotation-processor/build.gradle b/annotation-processor/build.gradle index 432847e0..afd75192 100644 --- a/annotation-processor/build.gradle +++ b/annotation-processor/build.gradle @@ -17,7 +17,6 @@ dependencies { implementation "net.fabricmc:sponge-mixin:0.12.5+" // Platform classes implementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}" - implementation "net.minecraftforge:mergetool:1.1.0" implementation project(":annotations") } 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 84cf122f..ce7a7202 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 @@ -1,14 +1,10 @@ package org.fury_phoenix.mixinAp.annotation; -import com.google.common.base.Throwables; - import java.lang.annotation.Annotation; -import java.lang.invoke.*; import java.util.Collection; import java.util.HashSet; -import java.util.HashMap; -import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -41,15 +37,13 @@ public class ClientMixinValidator { private final boolean debug; - private static final Map markers = Map.of( - "net.fabricmc.api.Environment", "net.fabricmc.api.EnvType", - "net.minecraftforge.api.distmarker.OnlyIn", "net.minecraftforge.api.distmarker.Dist", - "net.neoforged.api.distmarker.OnlyIn", "net.neoforged.api.distmarker.Dist"); + private static final Iterable markers = Set.of( + "net.fabricmc.api.Environment", + "net.minecraftforge.api.distmarker.OnlyIn", + "net.neoforged.api.distmarker.OnlyIn"); private static final Collection unannotatedClasses = new HashSet<>(); - private static final MethodHandles.Lookup lookup = MethodHandles.publicLookup(); - public ClientMixinValidator(ProcessingEnvironment env) throws ReflectiveOperationException { typeHandleProvider = AnnotatedMixinsAccessor.getMixinAP(env); @@ -97,11 +91,12 @@ public class ClientMixinValidator { } private boolean isClientMarked(TypeElement te) { - for (var entry : getPlatformClasses(markers).entrySet()) { - IAnnotationHandle marker = getAnnotationHandle(te, entry.getKey()); - if(!marker.exists()) continue; + for (var marker : getPlatformMarkers(markers)) { + messager.printMessage(Diagnostic.Kind.WARNING, marker.toString()); + IAnnotationHandle handle = getAnnotationHandle(te, marker); + if(!handle.exists()) continue; - String[] enumValue = marker.getValue("value"); + String[] enumValue = handle.getValue("value"); if(enumValue==null) continue; return enumValue[1].equals("CLIENT"); @@ -119,15 +114,6 @@ public class ClientMixinValidator { return false; } - private static Optional getAccessor(Class markerClass, - Class> enumClass) { - MethodType enumValueAccessorType = MethodType.methodType(enumClass); - try { - return Optional.of(lookup.findVirtual(markerClass, "value", enumValueAccessorType)); - } catch (ReflectiveOperationException e) {} - return Optional.empty(); - } - public SimpleImmutableEntry getEntry(TypeElement annotatedMixinClass) { return new SimpleImmutableEntry<>( @@ -159,14 +145,11 @@ public class ClientMixinValidator { .collect(Collectors.toList()); } - private static Map, Class>> - getPlatformClasses(Map map) { - Map, Class>> platformClasses = new HashMap<>(); - for(var entry : map.entrySet()) { - Optional> annotation = getMarkerClass(entry.getKey()); - Optional>> enumClz = getMarkerEnumClass(entry.getValue()); - if(!annotation.isEmpty() && !enumClz.isEmpty()) - platformClasses.put(annotation.orElseThrow(), enumClz.orElseThrow()); + private static Iterable> + getPlatformMarkers(Iterable markers) { + Set> platformClasses = new HashSet<>(); + for(var marker : markers) { + getMarkerClass(marker).ifPresent(platformClasses::add); } return platformClasses; } @@ -179,24 +162,7 @@ public class ClientMixinValidator { return Optional.empty(); } - @SuppressWarnings("unchecked") - private static Optional>> getMarkerEnumClass(String enumClz) { - try { - Optional.of((Class>)Class.forName(enumClz)); - } catch (ClassNotFoundException e) {} - return Optional.empty(); - } - public static String toSourceString(String bytecodeName) { return bytecodeName.replaceAll("\\/", "."); } - - private Object invoke(MethodHandle mh, Annotation marker) { - try { return mh.invoke(marker); } - catch (Throwable e) { - messager.printMessage(Diagnostic.Kind.ERROR, "Fatal error:" + - Throwables.getStackTraceAsString(e)); - } - return null; - } }