|
| 1 | +package dev.ryanhcode.sable.mixin; |
| 2 | + |
| 3 | +import com.mojang.logging.LogUtils; |
| 4 | +import dev.ryanhcode.sable.annotation.MixinModVersionConstraint; |
| 5 | +import dev.ryanhcode.sable.platform.SableLoaderPlatform; |
| 6 | +import foundry.veil.Veil; |
| 7 | +import foundry.veil.api.compat.SodiumCompat; |
| 8 | +import it.unimi.dsi.fastutil.objects.Object2BooleanMap; |
| 9 | +import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap; |
| 10 | +import it.unimi.dsi.fastutil.objects.Object2ObjectMap; |
| 11 | +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; |
| 12 | +import org.apache.maven.artifact.versioning.ArtifactVersion; |
| 13 | +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; |
| 14 | +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; |
| 15 | +import org.apache.maven.artifact.versioning.VersionRange; |
| 16 | +import org.objectweb.asm.Type; |
| 17 | +import org.objectweb.asm.tree.AnnotationNode; |
| 18 | +import org.objectweb.asm.tree.ClassNode; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; |
| 21 | +import org.spongepowered.asm.mixin.extensibility.IMixinInfo; |
| 22 | +import org.spongepowered.asm.service.MixinService; |
| 23 | +import org.spongepowered.asm.util.Annotations; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +public abstract class AbstractSableMixinPlugin implements IMixinConfigPlugin { |
| 29 | + public static final Logger LOGGER = LogUtils.getLogger(); |
| 30 | + private final Object2BooleanMap<String> modLoadedCache = new Object2BooleanOpenHashMap<>(); |
| 31 | + private boolean sodiumPresent; |
| 32 | + |
| 33 | + @Override |
| 34 | + public void onLoad(final String mixinPackage) { |
| 35 | + this.sodiumPresent = SodiumCompat.isLoaded(); |
| 36 | + |
| 37 | + LOGGER.info("Using {} renderer mixins", this.sodiumPresent ? "Sodium" : "Vanilla"); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getRefMapperConfig() { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean shouldApplyMixin(final String targetClassName, final String mixinClassName) { |
| 47 | + // TODO: Housekeeping |
| 48 | + if (mixinClassName.startsWith("dev.ryanhcode.sable.mixin.sublevel_render.impl")) { |
| 49 | + return this.sodiumPresent ? mixinClassName.startsWith("dev.ryanhcode.sable.mixin.sublevel_render.impl.sodium") : mixinClassName.startsWith("dev.ryanhcode.sable.mixin.sublevel_render.impl.vanilla"); |
| 50 | + } |
| 51 | + |
| 52 | + if (isCompatibilityMixin(mixinClassName)) { |
| 53 | + final String modId = extractModId(mixinClassName); |
| 54 | + if (modId == null) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + final boolean isModLoaded = this.modLoadedCache.computeIfAbsent(modId, x -> Veil.platform().isModLoaded(modId)); |
| 59 | + return isModLoaded && MixinConstraints.handleClassAnnotation(mixinClassName, modId); |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void acceptTargets(final Set<String> myTargets, final Set<String> otherTargets) { |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public List<String> getMixins() { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void preApply(final String targetClassName, final ClassNode targetClass, final String mixinClassName, final IMixinInfo mixinInfo) { |
| 76 | + if (isCompatibilityMixin(mixinClassName)) { |
| 77 | + final String modId = extractModId(mixinClassName); |
| 78 | + if (modId == null) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // Handle removing methods if there are constraints on them |
| 83 | + targetClass.methods.removeIf(method -> { |
| 84 | + final List<AnnotationNode> nodes = method.visibleAnnotations; |
| 85 | + |
| 86 | + try { |
| 87 | + return nodes != null && MixinConstraints.shouldApply(nodes, modId); |
| 88 | + } catch (final Throwable e) { |
| 89 | + throw new RuntimeException(e); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void postApply(final String targetClassName, final ClassNode targetClass, final String mixinClassName, final IMixinInfo mixinInfo) { |
| 97 | + } |
| 98 | + |
| 99 | + static boolean isCompatibilityMixin(final String mixinClassName) { |
| 100 | + return mixinClassName.startsWith("dev.ryanhcode.sable.mixin.compatibility.") || |
| 101 | + mixinClassName.startsWith("dev.ryanhcode.sable.neoforge.mixin.compatibility.") || |
| 102 | + mixinClassName.startsWith("dev.ryanhcode.sable.fabric.mixin.compatibility."); |
| 103 | + } |
| 104 | + |
| 105 | + static String extractModId(final String mixinClassName) { |
| 106 | + final String[] parts = mixinClassName.split("\\."); |
| 107 | + if (parts.length < 5) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + return parts[3].equals("mixin") ? parts[5] : parts[6]; |
| 112 | + } |
| 113 | + |
| 114 | + // Constraint handling |
| 115 | + static class MixinConstraints { |
| 116 | + private static final Object2ObjectMap<String, String> MOD_VERSION_CACHE = new Object2ObjectOpenHashMap<>(); |
| 117 | + |
| 118 | + // Looks for if there's a @MixinModVersionConstraint annotation which declares a range for when a mixin should be loaded |
| 119 | + static boolean handleClassAnnotation(final String mixinClassName, final String modId) { |
| 120 | + try { |
| 121 | + final List<AnnotationNode> nodes = MixinService.getService().getBytecodeProvider().getClassNode(mixinClassName).visibleAnnotations; |
| 122 | + |
| 123 | + return nodes != null && shouldApply(nodes, modId); |
| 124 | + } catch (final Throwable e) { |
| 125 | + throw new RuntimeException(e); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + static boolean shouldApply(final List<AnnotationNode> nodes, final String modId) throws InvalidVersionSpecificationException { |
| 130 | + for (final AnnotationNode node : nodes) { |
| 131 | + if (node.desc.equals(Type.getDescriptor(MixinModVersionConstraint.class))) { |
| 132 | + final String range = Annotations.getValue(node, "value"); |
| 133 | + final VersionRange versionRange = VersionRange.createFromVersionSpec(range); |
| 134 | + |
| 135 | + final String modVersion = MOD_VERSION_CACHE.computeIfAbsent(modId, x -> SableLoaderPlatform.INSTANCE.getModVersion(modId)); |
| 136 | + final ArtifactVersion artifactVersion = new DefaultArtifactVersion(modVersion); |
| 137 | + |
| 138 | + return versionRange.containsVersion(artifactVersion); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return true; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments