Search in sources :

Example 1 with NewerFileVersionException

use of riskyken.armourersWorkshop.common.exception.NewerFileVersionException in project Armourers-Workshop by RiskyKen.

the class SkinSerializer method readSkinTypeNameFromStream.

public static ISkinType readSkinTypeNameFromStream(DataInputStream stream) throws IOException, NewerFileVersionException {
    int fileVersion = stream.readInt();
    if (fileVersion > Skin.FILE_VERSION) {
        throw new NewerFileVersionException();
    }
    if (fileVersion > 12) {
        String header = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!header.equals(TAG_SKIN_HEADER)) {
            ModLogger.log(Level.ERROR, "Error loading skin header.");
        }
        String propsHeader = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!propsHeader.equals(TAG_SKIN_PROPS_HEADER)) {
            ModLogger.log(Level.ERROR, "Error loading skin props header.");
        }
    }
    SkinProperties properties = new SkinProperties();
    boolean loadedProps = true;
    IOException e = null;
    if (fileVersion < 12) {
        String authorName = stream.readUTF();
        String customName = stream.readUTF();
        String tags = "";
        if (!(fileVersion < 4)) {
            tags = stream.readUTF();
        } else {
            tags = "";
        }
        properties.setProperty(Skin.KEY_AUTHOR_NAME, authorName);
        properties.setProperty(Skin.KEY_CUSTOM_NAME, customName);
        if (tags != null && !tags.equalsIgnoreCase("")) {
            properties.setProperty(KEY_TAGS, tags);
        }
    } else {
        try {
            properties.readFromStream(stream, fileVersion);
        } catch (IOException propE) {
            ModLogger.log(Level.ERROR, "prop load failed");
            e = propE;
            loadedProps = false;
        }
    }
    if (fileVersion > 12) {
        String propsFooter = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!propsFooter.equals(TAG_SKIN_PROPS_FOOTER)) {
            ModLogger.log(Level.ERROR, "Error loading skin props footer.");
        }
        String typeHeader = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!typeHeader.equals(TAG_SKIN_TYPE_HEADER)) {
            ModLogger.log(Level.ERROR, "Error loading skin type header.");
        }
    }
    ISkinType equipmentSkinType = null;
    if (fileVersion < 5) {
        if (loadedProps) {
            equipmentSkinType = SkinTypeRegistry.INSTANCE.getSkinTypeFromLegacyId(stream.readByte() - 1);
        } else {
            throw e;
        }
    } else {
        if (loadedProps) {
            String regName = stream.readUTF();
            if (regName.equals(SkinTypeRegistry.skinSkirt.getRegistryName())) {
                regName = SkinTypeRegistry.skinLegs.getRegistryName();
            }
            equipmentSkinType = SkinTypeRegistry.INSTANCE.getSkinTypeFromRegistryName(regName);
        } else {
            StringBuilder sb = new StringBuilder();
            while (true) {
                sb.append(new String(new byte[] { stream.readByte() }, "UTF-8"));
                if (sb.toString().endsWith("armourers:")) {
                    break;
                }
            }
            ModLogger.log("Got armourers");
            sb = new StringBuilder();
            sb.append("armourers:");
            while (SkinTypeRegistry.INSTANCE.getSkinTypeFromRegistryName(sb.toString()) == null) {
                sb.append(new String(new byte[] { stream.readByte() }, "UTF-8"));
            }
            ModLogger.log(sb.toString());
            equipmentSkinType = SkinTypeRegistry.INSTANCE.getSkinTypeFromRegistryName(sb.toString());
            ModLogger.log("got failed type " + equipmentSkinType);
        }
    }
    return equipmentSkinType;
}
Also used : ISkinType(riskyken.armourersWorkshop.api.common.skin.type.ISkinType) IOException(java.io.IOException) NewerFileVersionException(riskyken.armourersWorkshop.common.exception.NewerFileVersionException) SkinProperties(riskyken.armourersWorkshop.common.skin.data.SkinProperties)

Example 2 with NewerFileVersionException

use of riskyken.armourersWorkshop.common.exception.NewerFileVersionException in project Armourers-Workshop by RiskyKen.

the class SkinIOUtils method loadSkinFromStream.

public static Skin loadSkinFromStream(InputStream inputStream) {
    DataInputStream stream = null;
    Skin skin = null;
    try {
        stream = new DataInputStream(new BufferedInputStream(inputStream));
        skin = SkinSerializer.readSkinFromStream(stream);
    } catch (FileNotFoundException e) {
        ModLogger.log(Level.WARN, "Skin file not found.");
        e.printStackTrace();
    } catch (IOException e) {
        ModLogger.log(Level.ERROR, "Skin file load failed.");
        e.printStackTrace();
    } catch (NewerFileVersionException e) {
        ModLogger.log(Level.ERROR, "Can not load skin file it was saved in newer version.");
        e.printStackTrace();
    } catch (InvalidCubeTypeException e) {
        ModLogger.log(Level.ERROR, "Unable to load skin. Unknown cube types found.");
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(stream);
        IOUtils.closeQuietly(inputStream);
    }
    return skin;
}
Also used : InvalidCubeTypeException(riskyken.armourersWorkshop.common.exception.InvalidCubeTypeException) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) Skin(riskyken.armourersWorkshop.common.skin.data.Skin) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) NewerFileVersionException(riskyken.armourersWorkshop.common.exception.NewerFileVersionException)

Example 3 with NewerFileVersionException

use of riskyken.armourersWorkshop.common.exception.NewerFileVersionException in project Armourers-Workshop by RiskyKen.

the class SkinIOUtils method getSkinTypeNameFromFile.

public static ISkinType getSkinTypeNameFromFile(File file) {
    DataInputStream stream = null;
    ISkinType skinType = null;
    try {
        stream = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
        skinType = SkinSerializer.readSkinTypeNameFromStream(stream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        ModLogger.log(Level.ERROR, "File name: " + file.getName());
    } catch (IOException e) {
        e.printStackTrace();
        ModLogger.log(Level.ERROR, "File name: " + file.getName());
    } catch (NewerFileVersionException e) {
        e.printStackTrace();
        ModLogger.log(Level.ERROR, "File name: " + file.getName());
    } catch (Exception e) {
        ModLogger.log(Level.ERROR, "Unable to load skin name. Unknown error.");
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(stream);
    }
    return skinType;
}
Also used : ISkinType(riskyken.armourersWorkshop.api.common.skin.type.ISkinType) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) NewerFileVersionException(riskyken.armourersWorkshop.common.exception.NewerFileVersionException) FileInputStream(java.io.FileInputStream) NewerFileVersionException(riskyken.armourersWorkshop.common.exception.NewerFileVersionException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvalidCubeTypeException(riskyken.armourersWorkshop.common.exception.InvalidCubeTypeException)

Example 4 with NewerFileVersionException

use of riskyken.armourersWorkshop.common.exception.NewerFileVersionException in project Armourers-Workshop by RiskyKen.

the class SkinIOUtils method loadSkinFromFile.

public static Skin loadSkinFromFile(File file) {
    DataInputStream stream = null;
    Skin skin = null;
    try {
        stream = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
        skin = SkinSerializer.readSkinFromStream(stream);
    } catch (FileNotFoundException e) {
        ModLogger.log(Level.WARN, "Skin file not found.");
        ModLogger.log(Level.WARN, file);
    } catch (IOException e) {
        ModLogger.log(Level.ERROR, "Skin file load failed.");
        e.printStackTrace();
    } catch (NewerFileVersionException e) {
        ModLogger.log(Level.ERROR, "Can not load skin file it was saved in newer version.");
        e.printStackTrace();
    } catch (InvalidCubeTypeException e) {
        ModLogger.log(Level.ERROR, "Unable to load skin. Unknown cube types found.");
        e.printStackTrace();
    } catch (Exception e) {
        ModLogger.log(Level.ERROR, "Unable to load skin. Unknown error.");
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(stream);
    }
    return skin;
}
Also used : InvalidCubeTypeException(riskyken.armourersWorkshop.common.exception.InvalidCubeTypeException) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) Skin(riskyken.armourersWorkshop.common.skin.data.Skin) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) NewerFileVersionException(riskyken.armourersWorkshop.common.exception.NewerFileVersionException) FileInputStream(java.io.FileInputStream) NewerFileVersionException(riskyken.armourersWorkshop.common.exception.NewerFileVersionException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvalidCubeTypeException(riskyken.armourersWorkshop.common.exception.InvalidCubeTypeException)

Example 5 with NewerFileVersionException

use of riskyken.armourersWorkshop.common.exception.NewerFileVersionException in project Armourers-Workshop by RiskyKen.

the class SkinSerializer method readSkinFromStream.

public static Skin readSkinFromStream(DataInputStream stream) throws IOException, NewerFileVersionException, InvalidCubeTypeException {
    int fileVersion = stream.readInt();
    if (fileVersion > Skin.FILE_VERSION) {
        throw new NewerFileVersionException();
    }
    if (fileVersion > 12) {
        String header = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!header.equals(TAG_SKIN_HEADER)) {
            ModLogger.log(Level.ERROR, "Error loading skin header.");
        }
        String propsHeader = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!propsHeader.equals(TAG_SKIN_PROPS_HEADER)) {
            ModLogger.log(Level.ERROR, "Error loading skin props header.");
        }
    }
    SkinProperties properties = new SkinProperties();
    boolean loadedProps = true;
    IOException e = null;
    if (fileVersion < 12) {
        String authorName = stream.readUTF();
        String customName = stream.readUTF();
        String tags = "";
        if (!(fileVersion < 4)) {
            tags = stream.readUTF();
        } else {
            tags = "";
        }
        properties.setProperty(Skin.KEY_AUTHOR_NAME, authorName);
        properties.setProperty(Skin.KEY_CUSTOM_NAME, customName);
        if (tags != null && !tags.equalsIgnoreCase("")) {
            properties.setProperty(KEY_TAGS, tags);
        }
    } else {
        try {
            properties.readFromStream(stream, fileVersion);
        } catch (IOException propE) {
            ModLogger.log(Level.ERROR, "prop load failed");
            e = propE;
            loadedProps = false;
        }
    }
    if (fileVersion > 12) {
        String propsFooter = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!propsFooter.equals(TAG_SKIN_PROPS_FOOTER)) {
            ModLogger.log(Level.ERROR, "Error loading skin props footer.");
        }
        String typeHeader = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!typeHeader.equals(TAG_SKIN_TYPE_HEADER)) {
            ModLogger.log(Level.ERROR, "Error loading skin type header.");
        }
    }
    ISkinType equipmentSkinType = null;
    if (fileVersion < 5) {
        if (loadedProps) {
            equipmentSkinType = SkinTypeRegistry.INSTANCE.getSkinTypeFromLegacyId(stream.readByte() - 1);
        } else {
            throw e;
        }
    } else {
        if (loadedProps) {
            String regName = stream.readUTF();
            if (regName.equals(SkinTypeRegistry.skinSkirt.getRegistryName())) {
                regName = SkinTypeRegistry.skinLegs.getRegistryName();
            }
            equipmentSkinType = SkinTypeRegistry.INSTANCE.getSkinTypeFromRegistryName(regName);
        } else {
            StringBuilder sb = new StringBuilder();
            while (true) {
                sb.append(new String(new byte[] { stream.readByte() }, "UTF-8"));
                if (sb.toString().endsWith("armourers:")) {
                    break;
                }
            }
            ModLogger.log("Got armourers");
            sb = new StringBuilder();
            sb.append("armourers:");
            while (SkinTypeRegistry.INSTANCE.getSkinTypeFromRegistryName(sb.toString()) == null) {
                sb.append(new String(new byte[] { stream.readByte() }, "UTF-8"));
            }
            ModLogger.log(sb.toString());
            equipmentSkinType = SkinTypeRegistry.INSTANCE.getSkinTypeFromRegistryName(sb.toString());
            ModLogger.log("got failed type " + equipmentSkinType);
        }
    }
    if (fileVersion > 12) {
        String typeFooter = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!typeFooter.equals(TAG_SKIN_TYPE_FOOTER)) {
            ModLogger.log(Level.ERROR, "Error loading skin type footer.");
        }
    }
    if (equipmentSkinType == null) {
        throw new InvalidCubeTypeException();
    }
    if (fileVersion > 12) {
        String typeFooter = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!typeFooter.equals(TAG_SKIN_PAINT_HEADER)) {
            ModLogger.log(Level.ERROR, "Error loading skin paint header.");
        }
    }
    int[] paintData = null;
    if (fileVersion > 7) {
        boolean hasPaintData = stream.readBoolean();
        if (hasPaintData) {
            paintData = new int[SkinTexture.TEXTURE_SIZE];
            for (int i = 0; i < SkinTexture.TEXTURE_SIZE; i++) {
                paintData[i] = stream.readInt();
            }
        }
    }
    if (fileVersion > 12) {
        String typeFooter = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!typeFooter.equals(TAG_SKIN_PAINT_FOOTER)) {
            ModLogger.log(Level.ERROR, "Error loading skin paint footer.");
        }
    }
    int size = stream.readByte();
    ArrayList<SkinPart> parts = new ArrayList<SkinPart>();
    for (int i = 0; i < size; i++) {
        if (fileVersion > 12) {
            String partHeader = StreamUtils.readString(stream, Charsets.US_ASCII);
            if (!partHeader.equals(TAG_SKIN_PART_HEADER)) {
                ModLogger.log(Level.ERROR, "Error loading skin part header.");
            }
        }
        SkinPart part = SkinPartSerializer.loadSkinPart(stream, fileVersion);
        if (fileVersion > 12) {
            String partFooter = StreamUtils.readString(stream, Charsets.US_ASCII);
            if (!partFooter.equals(TAG_SKIN_PART_FOOTER)) {
                ModLogger.log(Level.ERROR, "Error loading skin part footer.");
            }
        }
        parts.add(part);
    }
    if (fileVersion > 12) {
        String footer = StreamUtils.readString(stream, Charsets.US_ASCII);
        if (!footer.equals(TAG_SKIN_FOOTER)) {
            ModLogger.log(Level.ERROR, "Error loading skin footer.");
        }
    }
    return new Skin(properties, equipmentSkinType, paintData, parts);
}
Also used : ISkinType(riskyken.armourersWorkshop.api.common.skin.type.ISkinType) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SkinPart(riskyken.armourersWorkshop.common.skin.data.SkinPart) InvalidCubeTypeException(riskyken.armourersWorkshop.common.exception.InvalidCubeTypeException) Skin(riskyken.armourersWorkshop.common.skin.data.Skin) NewerFileVersionException(riskyken.armourersWorkshop.common.exception.NewerFileVersionException) SkinProperties(riskyken.armourersWorkshop.common.skin.data.SkinProperties)

Aggregations

IOException (java.io.IOException)5 NewerFileVersionException (riskyken.armourersWorkshop.common.exception.NewerFileVersionException)5 InvalidCubeTypeException (riskyken.armourersWorkshop.common.exception.InvalidCubeTypeException)4 BufferedInputStream (java.io.BufferedInputStream)3 DataInputStream (java.io.DataInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 ISkinType (riskyken.armourersWorkshop.api.common.skin.type.ISkinType)3 Skin (riskyken.armourersWorkshop.common.skin.data.Skin)3 FileInputStream (java.io.FileInputStream)2 SkinProperties (riskyken.armourersWorkshop.common.skin.data.SkinProperties)2 ArrayList (java.util.ArrayList)1 SkinPart (riskyken.armourersWorkshop.common.skin.data.SkinPart)1