Search in sources :

Example 1 with VersionRange

use of net.minecraftforge.fml.common.versioning.VersionRange in project ForestryMC by ForestryMC.

the class ModUtil method isModLoaded.

public static boolean isModLoaded(String modname, @Nullable String versionRangeString) {
    if (!isModLoaded(modname)) {
        return false;
    }
    if (versionRangeString != null) {
        ModContainer mod = Loader.instance().getIndexedModList().get(modname);
        ArtifactVersion modVersion = mod.getProcessedVersion();
        VersionRange versionRange = VersionParser.parseRange(versionRangeString);
        DefaultArtifactVersion requiredVersion = new DefaultArtifactVersion(modname, versionRange);
        if (!requiredVersion.containsVersion(modVersion)) {
            return false;
        }
    }
    return true;
}
Also used : DefaultArtifactVersion(net.minecraftforge.fml.common.versioning.DefaultArtifactVersion) ArtifactVersion(net.minecraftforge.fml.common.versioning.ArtifactVersion) ModContainer(net.minecraftforge.fml.common.ModContainer) DefaultArtifactVersion(net.minecraftforge.fml.common.versioning.DefaultArtifactVersion) VersionRange(net.minecraftforge.fml.common.versioning.VersionRange)

Example 2 with VersionRange

use of net.minecraftforge.fml.common.versioning.VersionRange in project ForestryMC by ForestryMC.

the class ModUtil method isAPILoaded.

/**
 * Checks to see if an API within a specific version range is loaded.
 *
 * @param apiName            the package name of the package-info.java file to check. (for example "cofh.api.energy")
 * @param versionRangeString the version range, as defined in {@link VersionParser}.
 */
public static boolean isAPILoaded(String apiName, @Nullable String versionRangeString) {
    Package apiPackage = Package.getPackage(apiName);
    if (apiPackage == null) {
        return false;
    }
    API apiAnnotation = apiPackage.getAnnotation(API.class);
    if (apiAnnotation == null) {
        return false;
    }
    if (versionRangeString != null) {
        String apiVersionString = apiAnnotation.apiVersion();
        if (apiVersionString == null) {
            return false;
        }
        VersionRange versionRange = VersionParser.parseRange(versionRangeString);
        DefaultArtifactVersion givenVersion = new DefaultArtifactVersion(apiName, apiVersionString);
        DefaultArtifactVersion requiredVersion = new DefaultArtifactVersion(apiName, versionRange);
        if (!requiredVersion.containsVersion(givenVersion)) {
            return false;
        }
    }
    return true;
}
Also used : DefaultArtifactVersion(net.minecraftforge.fml.common.versioning.DefaultArtifactVersion) API(net.minecraftforge.fml.common.API) VersionRange(net.minecraftforge.fml.common.versioning.VersionRange)

Example 3 with VersionRange

use of net.minecraftforge.fml.common.versioning.VersionRange in project LogisticsPipes by RS485.

the class LogisticsClassTransformer method handleLPTransformation.

@SuppressWarnings("unchecked")
private byte[] handleLPTransformation(byte[] bytes) {
    final ClassNode node = new ClassNode();
    ClassReader reader = new ClassReader(bytes);
    reader.accept(node, 0);
    boolean changed = false;
    if (node.visibleAnnotations != null) {
        for (AnnotationNode a : node.visibleAnnotations) {
            if (a.desc.equals("Llogisticspipes/asm/ModDependentInterface;")) {
                if (a.values.size() == 4 && a.values.get(0).equals("modId") && a.values.get(2).equals("interfacePath")) {
                    List<String> modId = (List<String>) a.values.get(1);
                    List<String> interfacePath = (List<String>) a.values.get(3);
                    if (modId.size() != interfacePath.size()) {
                        throw new RuntimeException("The Arrays have to be of the same size.");
                    }
                    for (int i = 0; i < modId.size(); i++) {
                        if (!ModStatusHelper.isModLoaded(modId.get(i))) {
                            interfacesToClearA.add(interfacePath.get(i));
                            interfacesToClearB.add(interfacePath.get(i));
                            for (String inter : node.interfaces) {
                                if (inter.replace("/", ".").equals(interfacePath.get(i))) {
                                    node.interfaces.remove(inter);
                                    changed = true;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    throw new UnsupportedOperationException("Can't parse the annotations correctly");
                }
            }
        }
    }
    List<MethodNode> methodsToRemove = new ArrayList<>();
    for (MethodNode m : node.methods) {
        if (m.visibleAnnotations != null) {
            for (AnnotationNode a : m.visibleAnnotations) {
                if (a.desc.equals("Llogisticspipes/asm/ModDependentMethod;")) {
                    if (a.values.size() == 2 && a.values.get(0).equals("modId")) {
                        String modId = a.values.get(1).toString();
                        if (!ModStatusHelper.isModLoaded(modId)) {
                            methodsToRemove.add(m);
                            break;
                        }
                    } else {
                        throw new UnsupportedOperationException("Can't parse the annotation correctly");
                    }
                } else if (a.desc.equals("Llogisticspipes/asm/ClientSideOnlyMethodContent;")) {
                    if (FMLCommonHandler.instance().getSide().equals(Side.SERVER)) {
                        m.instructions.clear();
                        m.localVariables.clear();
                        m.tryCatchBlocks.clear();
                        m.visitCode();
                        Label l0 = new Label();
                        m.visitLabel(l0);
                        m.visitMethodInsn(Opcodes.INVOKESTATIC, "logisticspipes/asm/LogisticsASMHookClass", "callingClearedMethod", "()V");
                        Label l1 = new Label();
                        m.visitLabel(l1);
                        m.visitInsn(Opcodes.RETURN);
                        Label l2 = new Label();
                        m.visitLabel(l2);
                        m.visitLocalVariable("this", "Llogisticspipes/network/packets/DummyPacket;", null, l0, l2, 0);
                        m.visitLocalVariable("player", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l2, 1);
                        m.visitMaxs(0, 2);
                        m.visitEnd();
                        changed = true;
                        break;
                    }
                } else if (a.desc.equals("Llogisticspipes/asm/ModDependentMethodName;")) {
                    if (a.values.size() == 6 && a.values.get(0).equals("modId") && a.values.get(2).equals("newName") && a.values.get(4).equals("version")) {
                        String modId = a.values.get(1).toString();
                        final String newName = a.values.get(3).toString();
                        final String version = a.values.get(5).toString();
                        boolean loaded = ModStatusHelper.isModLoaded(modId);
                        if (loaded && !version.equals("")) {
                            ModContainer mod = Loader.instance().getIndexedModList().get(modId);
                            if (mod != null) {
                                VersionRange range = VersionParser.parseRange(version);
                                ArtifactVersion artifactVersion = new DefaultArtifactVersion("Version", mod.getVersion());
                                loaded = range.containsVersion(artifactVersion);
                            } else {
                                loaded = false;
                            }
                        }
                        if (loaded) {
                            final String oldName = m.name;
                            m.name = newName;
                            MethodNode newM = new MethodNode(Opcodes.ASM4, m.access, m.name, m.desc, m.signature, m.exceptions.toArray(new String[0])) {

                                @Override
                                public void visitMethodInsn(int opcode, String owner, String name, String desc) {
                                    if (name.equals(oldName) && owner.equals(node.superName)) {
                                        super.visitMethodInsn(opcode, owner, newName, desc);
                                    } else {
                                        super.visitMethodInsn(opcode, owner, name, desc);
                                    }
                                }
                            };
                            m.accept(newM);
                            node.methods.set(node.methods.indexOf(m), newM);
                            changed = true;
                            break;
                        }
                    } else {
                        throw new UnsupportedOperationException("Can't parse the annotation correctly");
                    }
                }
            }
        }
    }
    for (MethodNode m : methodsToRemove) {
        node.methods.remove(m);
    }
    List<FieldNode> fieldsToRemove = new ArrayList<>();
    for (FieldNode f : node.fields) {
        if (f.visibleAnnotations != null) {
            for (AnnotationNode a : f.visibleAnnotations) {
                if (a.desc.equals("Llogisticspipes/asm/ModDependentField;")) {
                    if (a.values.size() == 2 && a.values.get(0).equals("modId")) {
                        String modId = a.values.get(1).toString();
                        if (!ModStatusHelper.isModLoaded(modId)) {
                            fieldsToRemove.add(f);
                            break;
                        }
                    } else {
                        throw new UnsupportedOperationException("Can't parse the annotation correctly");
                    }
                }
            }
        }
    }
    for (FieldNode f : fieldsToRemove) {
        node.fields.remove(f);
    }
    if (!changed && methodsToRemove.isEmpty() && fieldsToRemove.isEmpty()) {
        return bytes;
    }
    ClassWriter writer = new ClassWriter(0);
    node.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ModContainer(net.minecraftforge.fml.common.ModContainer) FieldNode(org.objectweb.asm.tree.FieldNode) ArrayList(java.util.ArrayList) Label(org.objectweb.asm.Label) DefaultArtifactVersion(net.minecraftforge.fml.common.versioning.DefaultArtifactVersion) VersionRange(net.minecraftforge.fml.common.versioning.VersionRange) ClassWriter(org.objectweb.asm.ClassWriter) ArtifactVersion(net.minecraftforge.fml.common.versioning.ArtifactVersion) DefaultArtifactVersion(net.minecraftforge.fml.common.versioning.DefaultArtifactVersion) MethodNode(org.objectweb.asm.tree.MethodNode) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) ClassReader(org.objectweb.asm.ClassReader) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

DefaultArtifactVersion (net.minecraftforge.fml.common.versioning.DefaultArtifactVersion)3 VersionRange (net.minecraftforge.fml.common.versioning.VersionRange)3 ModContainer (net.minecraftforge.fml.common.ModContainer)2 ArtifactVersion (net.minecraftforge.fml.common.versioning.ArtifactVersion)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 API (net.minecraftforge.fml.common.API)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassWriter (org.objectweb.asm.ClassWriter)1 Label (org.objectweb.asm.Label)1 AnnotationNode (org.objectweb.asm.tree.AnnotationNode)1 ClassNode (org.objectweb.asm.tree.ClassNode)1 FieldNode (org.objectweb.asm.tree.FieldNode)1 MethodNode (org.objectweb.asm.tree.MethodNode)1