EmbeddedResource.java

// <editor-fold defaultstate="collapsed" desc="license">
/*
 * Copyright (c) 2018, Karl H. Beckers
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * * Neither the name of the <ORGANIZATION> nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 **/
// </editor-fold>
package net.jarre_de_the.griffin.file;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import net.jarre_de_the.griffin.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author charly4711
 */
public abstract class EmbeddedResource implements Cloneable {

    private static final Logger LOGGER = LoggerFactory.getLogger(EmbeddedResource.class);

    private long offset = 0;
    private ResourceType resourceType = ResourceType.GFF;
    private long resourceLength = 0;
    private Object data = null;

    public abstract void read(RandomAccessFile in, long offset) throws IOException;

    public Object readResource(RandomAccessFile in) {
        Object result = null;
        Class clazz = getResourceType().contentType().typeClass();
        if (null != clazz) {
            try {
                in.seek(getOffset());

                if (Gff.class.isAssignableFrom(clazz)) {
                    Constructor ctor = clazz.getConstructor();
                    EmbeddableNwnFile instance = (EmbeddableNwnFile) ctor.newInstance();

                    Method m = clazz.getDeclaredMethod("setEmbeddedOffset", long.class);
                    m.invoke(instance, getOffset());

                    m = clazz.getDeclaredMethod("parse", RandomAccessFile.class);
                    m.invoke(instance, in);
                    result = instance;
                } else if (TwoDa.class.isAssignableFrom(clazz)) {
                    TwoDa file = new TwoDa();
                    file.setEmbeddedOffset(getOffset());
                    file.setEmbeddedLength(getResourceLength());
                    file.parse(in);
                    result = file;
                } else if (String.class.isAssignableFrom(clazz)) {
                    long bytesToRead = getResourceLength();
                    // the handling of bytesToRead > MAX_INT is likely overkill
                    if (bytesToRead <= Integer.MAX_VALUE) {
                        byte[] buf = new byte[(int) bytesToRead];
                        in.readFully(buf);
                        result = new String(buf, Util.CHARSET_US_ASCII);
                    }
                } else {
                    // binary
                    long bytesToRead = getResourceLength();
                    // the handling of bytesToRead > MAX_INT is likely overkill
                    if (bytesToRead <= Integer.MAX_VALUE) {
                        byte[] buf = new byte[(int) bytesToRead];
                        in.readFully(buf);
                        result = buf;
                    }
                }
            } catch (NoSuchMethodException ex) {
                LOGGER.error(null, ex);
            } catch (SecurityException ex) {
                LOGGER.error(null, ex);
            } catch (InstantiationException ex) {
                LOGGER.error(null, ex);
            } catch (IllegalAccessException ex) {
                LOGGER.error(null, ex);
            } catch (IllegalArgumentException ex) {
                LOGGER.error(null, ex);
            } catch (InvocationTargetException ex) {
                LOGGER.error(null, ex);
            } catch (IOException ex) {
                LOGGER.error(null, ex);
//                    } catch (Throwable t) {
//                        t.printStackTrace();
            }
        } else {
            // treat as binary
            long bytesToRead = getResourceLength();
            // the handling of bytesToRead > MAX_INT is likely overkill
            if (bytesToRead <= Integer.MAX_VALUE) {
                try {
                    in.seek(getOffset());

                    byte[] buf = new byte[(int) bytesToRead];
                    in.readFully(buf);
                    result = buf;
                } catch (IOException ex) {
                    LOGGER.error(null, ex);
                }
            }
        }

        return result;
    }

    public ResourceType getResourceType() {
        return resourceType;
    }

    public void setResourceType(ResourceType resourceType) {
        this.resourceType = resourceType;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    protected long getOffset() {
        return offset;
    }

    protected void setOffset(long offset) {
        this.offset = offset;
    }

    protected long getResourceLength() {
        return resourceLength;
    }

    protected void setResourceLength(long resourceLength) {
        this.resourceLength = resourceLength;
    }

    @Override
    public EmbeddedResource clone() throws CloneNotSupportedException {
        EmbeddedResource clone = (EmbeddedResource) super.clone();

        clone.setOffset(getOffset());
        clone.setResourceType(getResourceType());
        clone.setResourceLength(getResourceLength());

        Object _data = getData();
        if (_data instanceof PersistableNwnFile) {
            PersistableNwnFile file = (PersistableNwnFile) _data;
            clone.setData(file.clone());
        } else if (_data instanceof byte[]) {
            byte[] dataOrig = (byte[]) _data;
            byte[] dataClone = new byte[dataOrig.length];
            System.arraycopy(dataOrig, 0, dataClone, 0, dataOrig.length);
            clone.setData(dataClone);
        } else {
            clone.setData(_data);
        }

        return clone;
    }

    @Override
    public boolean equals(Object compare) {
        if (compare == this) {
            return true;
        }

        if (!(compare instanceof EmbeddedResource)) {
            return false;
        }

        EmbeddedResource file = (EmbeddedResource) compare;

        return PersistableNwnFile.areNullablePropertiesEqual(this.getData(), file.getData())
                && PersistableNwnFile.areNullablePropertiesEqual(this.getResourceType(), file.getResourceType());

        // because we normalize files, both the offset and the resource
        // length may differ, though files are semantically the same
        //
//        return this.getOffset() == file.getOffset()
//                && this.getResourceLength() == file.getResourceLength();
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (int) (this.offset ^ (this.offset >>> 32));
        hash = 97 * hash + Objects.hashCode(this.resourceType);
        hash = 97 * hash + (int) (this.resourceLength ^ (this.resourceLength >>> 32));
        hash = 97 * hash + Objects.hashCode(this.data);
        return hash;
    }
}