Use `ASM` instead of `Class.forName` to check `shouldApplyMixin`
This commit is contained in:
CPTProgrammer 2025-06-24 21:49:43 +08:00
parent e43426128a
commit f02b2acde5

View File

@ -2,11 +2,14 @@ package cn.revaria.chatplus.plugin;
import cn.revaria.chatplus.plugin.annotation.DisableIfModsLoaded; import cn.revaria.chatplus.plugin.annotation.DisableIfModsLoaded;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo; import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.util.Annotations;
import java.util.Arrays; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -23,14 +26,17 @@ public class MixinConfigPlugin implements IMixinConfigPlugin {
@Override @Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
try { try {
Class<?> mixinClass = Class.forName(mixinClassName); ClassNode classNode = MixinService.getService().getBytecodeProvider().getClassNode(mixinClassName);
DisableIfModsLoaded annotation = mixinClass.getAnnotation(DisableIfModsLoaded.class); AnnotationNode annotationNode = Annotations.getVisible(classNode, DisableIfModsLoaded.class);
if (annotation != null) {
return Arrays.stream(annotation.value()).noneMatch(modId -> FabricLoader.getInstance().isModLoaded(modId)); if (annotationNode == null) {
return true;
} }
return true;
} catch (Exception e) { List<String> modIds = Annotations.getValue(annotationNode);
return true; return modIds.stream().noneMatch(modId -> FabricLoader.getInstance().isModLoaded(modId));
} catch (IOException | ClassNotFoundException e) {
return false;
} }
} }