Search in sources :

Example 1 with VersionRange

use of cpw.mods.fml.common.versioning.VersionRange in project LogisticsPipes by RS485.

the class StorageDrawersInventoryHandler method init.

@Override
public boolean init() {
    List<ModContainer> modList = Loader.instance().getModList();
    for (ModContainer mod : modList) {
        if (mod.getModId().equals("StorageDrawers")) {
            try {
                VersionRange validVersions = VersionRange.createFromVersionSpec("[1.7.8,)");
                ArtifactVersion version = new DefaultArtifactVersion(mod.getVersion());
                return validVersions.containsVersion(version);
            } catch (InvalidVersionSpecificationException e) {
                return false;
            }
        }
    }
    return false;
}
Also used : ArtifactVersion(cpw.mods.fml.common.versioning.ArtifactVersion) DefaultArtifactVersion(cpw.mods.fml.common.versioning.DefaultArtifactVersion) InvalidVersionSpecificationException(cpw.mods.fml.common.versioning.InvalidVersionSpecificationException) ModContainer(cpw.mods.fml.common.ModContainer) DefaultArtifactVersion(cpw.mods.fml.common.versioning.DefaultArtifactVersion) VersionRange(cpw.mods.fml.common.versioning.VersionRange)

Example 2 with VersionRange

use of cpw.mods.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(cpw.mods.fml.common.ModContainer) FieldNode(org.objectweb.asm.tree.FieldNode) ArrayList(java.util.ArrayList) DefaultArtifactVersion(cpw.mods.fml.common.versioning.DefaultArtifactVersion) VersionRange(cpw.mods.fml.common.versioning.VersionRange) DefaultArtifactVersion(cpw.mods.fml.common.versioning.DefaultArtifactVersion) ArtifactVersion(cpw.mods.fml.common.versioning.ArtifactVersion) MethodNode(org.objectweb.asm.tree.MethodNode) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ModContainer (cpw.mods.fml.common.ModContainer)2 ArtifactVersion (cpw.mods.fml.common.versioning.ArtifactVersion)2 DefaultArtifactVersion (cpw.mods.fml.common.versioning.DefaultArtifactVersion)2 VersionRange (cpw.mods.fml.common.versioning.VersionRange)2 InvalidVersionSpecificationException (cpw.mods.fml.common.versioning.InvalidVersionSpecificationException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)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