Search in sources :

Example 1 with ModelFormatException

use of net.minecraftforge.client.model.ModelFormatException in project Engine by VoltzEngine-Project.

the class FixedTechneModel method loadTechneModel.

// TODO break into smaller methods
private void loadTechneModel(InputStream stream) throws ModelFormatException {
    try {
        ZipInputStream zipInput = new ZipInputStream(stream);
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            byte[] data = new byte[(int) entry.getSize()];
            // For some reason, using read(byte[]) makes reading stall upon reaching a 0x1E byte
            int i = 0;
            while (zipInput.available() > 0 && i < data.length) {
                data[i++] = (byte) zipInput.read();
            }
            zipContents.put(entry.getName(), data);
        }
        byte[] modelXml = zipContents.get("model.xml");
        if (modelXml == null) {
            throw new ModelFormatException("Model " + fileName + " contains no model.xml file");
        }
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(new ByteArrayInputStream(modelXml));
        NodeList nodeListTechne = document.getElementsByTagName("Techne");
        if (nodeListTechne.getLength() < 1) {
            throw new ModelFormatException("Model " + fileName + " contains no Techne tag");
        }
        NodeList nodeListModel = document.getElementsByTagName("Model");
        if (nodeListModel.getLength() < 1) {
            throw new ModelFormatException("Model " + fileName + " contains no Model tag");
        }
        NamedNodeMap modelAttributes = nodeListModel.item(0).getAttributes();
        if (modelAttributes == null) {
            throw new ModelFormatException("Model " + fileName + " contains a Model tag with no attributes");
        }
        Node modelTexture = modelAttributes.getNamedItem("texture");
        if (modelTexture != null) {
        // TODO texture = modelTexture.getTextContent();
        }
        NodeList textureSize = document.getElementsByTagName("TextureSize");
        for (int i = 0; i < textureSize.getLength(); i++) {
            String size = textureSize.item(i).getTextContent();
            String[] textureDimensions = size.split(",");
            textureWidth = Integer.parseInt(textureDimensions[0]);
            textureHeight = Integer.parseInt(textureDimensions[1]);
        }
        NodeList shapes = document.getElementsByTagName("Shape");
        for (int i = 0; i < shapes.getLength(); i++) {
            Node shape = shapes.item(i);
            NamedNodeMap shapeAttributes = shape.getAttributes();
            if (shapeAttributes == null) {
                throw new ModelFormatException("Shape #" + (i + 1) + " in " + fileName + " has no attributes");
            }
            Node name = shapeAttributes.getNamedItem("name");
            String shapeName = null;
            if (name != null) {
                shapeName = name.getNodeValue();
            }
            if (shapeName == null) {
                shapeName = "Shape #" + (i + 1);
            }
            String shapeType = null;
            Node type = shapeAttributes.getNamedItem("type");
            if (type != null) {
                shapeType = type.getNodeValue();
            }
            if (shapeType != null && !cubeTypes.contains(shapeType)) {
                FMLLog.warning("Model shape [" + shapeName + "] in " + fileName + " is not a cube, ignoring");
                continue;
            }
            try {
                boolean mirrored = false;
                String[] offset = new String[3];
                String[] position = new String[3];
                String[] rotation = new String[3];
                String[] size = new String[3];
                String[] textureOffset = new String[2];
                NodeList shapeChildren = shape.getChildNodes();
                for (int j = 0; j < shapeChildren.getLength(); j++) {
                    Node shapeChild = shapeChildren.item(j);
                    String shapeChildName = shapeChild.getNodeName();
                    String shapeChildValue = shapeChild.getTextContent();
                    if (shapeChildValue != null) {
                        shapeChildValue = shapeChildValue.trim();
                        switch(shapeChildName) {
                            case "IsMirrored":
                                mirrored = !shapeChildValue.equals("False");
                                break;
                            case "Offset":
                                offset = shapeChildValue.split(",");
                                break;
                            case "Position":
                                position = shapeChildValue.split(",");
                                break;
                            case "Rotation":
                                rotation = shapeChildValue.split(",");
                                break;
                            case "Size":
                                size = shapeChildValue.split(",");
                                break;
                            case "TextureOffset":
                                textureOffset = shapeChildValue.split(",");
                                break;
                        }
                    }
                }
                // That's what the ModelBase subclassing is needed for
                ModelRenderer cube = new ModelRenderer(this, shapeName);
                cube.setTextureOffset(Integer.parseInt(textureOffset[0]), Integer.parseInt(textureOffset[1]));
                cube.mirror = mirrored;
                cube.addBox(Float.parseFloat(offset[0]), Float.parseFloat(offset[1]), Float.parseFloat(offset[2]), Integer.parseInt(size[0]), Integer.parseInt(size[1]), Integer.parseInt(size[2]));
                cube.setRotationPoint(Float.parseFloat(position[0]), Float.parseFloat(position[1]) - 16, Float.parseFloat(position[2]));
                cube.rotateAngleX = (float) Math.toRadians(Float.parseFloat(rotation[0]));
                cube.rotateAngleY = (float) Math.toRadians(Float.parseFloat(rotation[1]));
                cube.rotateAngleZ = (float) Math.toRadians(Float.parseFloat(rotation[2]));
                if (parts.containsKey(shapeName)) {
                    throw new ModelFormatException("Model contained duplicate part name: '" + shapeName + "' node #" + i);
                }
                parts.put(shapeName, cube);
            } catch (NumberFormatException e) {
                FMLLog.warning("Model shape [" + shapeName + "] in " + fileName + " contains malformed integers within its data, ignoring");
                e.printStackTrace();
            }
        }
    } catch (ZipException e) {
        throw new ModelFormatException("Model " + fileName + " is not a valid zip file");
    } catch (IOException e) {
        throw new ModelFormatException("Model " + fileName + " could not be read", e);
    } catch (ParserConfigurationException e) {
    // hush
    } catch (SAXException e) {
        throw new ModelFormatException("Model " + fileName + " contains invalid XML", e);
    }
}
Also used : ModelFormatException(net.minecraftforge.client.model.ModelFormatException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) ZipEntry(java.util.zip.ZipEntry) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) ZipInputStream(java.util.zip.ZipInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ModelRenderer(net.minecraft.client.model.ModelRenderer) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipException (java.util.zip.ZipException)1 ZipInputStream (java.util.zip.ZipInputStream)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ModelRenderer (net.minecraft.client.model.ModelRenderer)1 ModelFormatException (net.minecraftforge.client.model.ModelFormatException)1 Document (org.w3c.dom.Document)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 SAXException (org.xml.sax.SAXException)1