Search in sources :

Example 1 with DataWriter

use of info.ata4.io.DataWriter in project disunity by ata4.

the class BundleWriter method write.

public void write(Bundle bundle, Progress progress) throws IOException {
    this.bundle = bundle;
    // add offset placeholders
    levelOffsetMap.clear();
    bundle.entries().stream().filter(entry -> {
        if (bundle.entries().size() == 1) {
            return true;
        }
        String name = entry.name();
        return name.equals("mainData") || name.startsWith("level");
    }).forEach(entry -> levelOffsetMap.put(entry, new MutablePair<>(0L, 0L)));
    BundleHeader header = bundle.header();
    header.levelByteEnd().clear();
    header.levelByteEnd().addAll(levelOffsetMap.values());
    header.numberOfLevelsToDownload(levelOffsetMap.size());
    // write header
    out.writeStruct(header);
    header.headerSize((int) out.position());
    // write bundle data
    if (header.compressed()) {
        // write data to temporary file
        try (DataWriter outData = DataWriters.forFile(dataFile, CREATE, WRITE, TRUNCATE_EXISTING)) {
            writeData(outData, progress);
        }
        // configure LZMA encoder
        LzmaEncoderProps props = new LzmaEncoderProps();
        // 8 MiB
        props.setDictionarySize(1 << 23);
        // maximum
        props.setNumFastBytes(273);
        props.setUncompressedSize(Files.size(dataFile));
        props.setEndMarkerMode(true);
        // stream the temporary bundle data compressed into the bundle file
        try (OutputStream os = new LzmaOutputStream(new BufferedOutputStream(out.stream()), props)) {
            Files.copy(dataFile, os);
        }
        for (MutablePair<Long, Long> levelOffset : levelOffsetMap.values()) {
            levelOffset.setLeft(out.size());
        }
    } else {
        // write data directly to file
        writeData(out, progress);
    }
    // update header
    int fileSize = (int) out.size();
    header.completeFileSize(fileSize);
    header.minimumStreamedBytes(fileSize);
    out.position(0);
    out.writeStruct(header);
}
Also used : DataWriters(info.ata4.io.DataWriters) OutputStream(java.io.OutputStream) LzmaOutputStream(net.contrapunctus.lzma.LzmaOutputStream) Progress(info.ata4.junity.progress.Progress) DataWriter(info.ata4.io.DataWriter) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Closeable(java.io.Closeable) Map(java.util.Map) Optional(java.util.Optional) LzmaEncoderProps(info.ata4.io.lzma.LzmaEncoderProps) Path(java.nio.file.Path) InputStream(java.io.InputStream) MutablePair(org.apache.commons.lang3.tuple.MutablePair) OutputStream(java.io.OutputStream) LzmaOutputStream(net.contrapunctus.lzma.LzmaOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) LzmaEncoderProps(info.ata4.io.lzma.LzmaEncoderProps) LzmaOutputStream(net.contrapunctus.lzma.LzmaOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DataWriter(info.ata4.io.DataWriter)

Example 2 with DataWriter

use of info.ata4.io.DataWriter in project disunity by ata4.

the class SerializedFileWriter method writeMetadata.

private void writeMetadata(DataWriter out) throws IOException {
    SerializedFileMetadata metadata = serialized.metadata();
    SerializedFileHeader header = serialized.header();
    DataBlock metadataBlock = serialized.metadataBlock();
    metadataBlock.markBegin(out);
    metadata.version(header.version());
    out.writeStruct(metadata);
    metadataBlock.markEnd(out);
    L.log(Level.FINER, "metadataBlock: {0}", metadataBlock);
}
Also used : DataBlock(info.ata4.util.io.DataBlock)

Example 3 with DataWriter

use of info.ata4.io.DataWriter in project disunity by ata4.

the class SerializedFileWriter method writeObjects.

private void writeObjects(DataWriter out) throws IOException {
    long ofsMin = Long.MAX_VALUE;
    long ofsMax = Long.MIN_VALUE;
    for (SerializedObjectData data : serialized.objectData()) {
        ByteBuffer bb = data.buffer();
        bb.rewind();
        out.align(8);
        ofsMin = Math.min(ofsMin, out.position());
        ofsMax = Math.max(ofsMax, out.position() + bb.remaining());
        ObjectInfo info = data.info();
        info.offset(out.position() - serialized.header().dataOffset());
        info.length(bb.remaining());
        out.writeBuffer(bb);
    }
    DataBlock objectDataBlock = serialized.objectDataBlock();
    objectDataBlock.offset(ofsMin);
    objectDataBlock.endOffset(ofsMax);
    L.log(Level.FINER, "objectDataBlock: {0}", objectDataBlock);
}
Also used : ByteBuffer(java.nio.ByteBuffer) ObjectInfo(info.ata4.junity.serialize.objectinfo.ObjectInfo) DataBlock(info.ata4.util.io.DataBlock)

Example 4 with DataWriter

use of info.ata4.io.DataWriter in project disunity by ata4.

the class SerializedFileWriter method writeHeader.

private void writeHeader(DataWriter out) throws IOException {
    DataBlock headerBlock = serialized.headerBlock();
    headerBlock.markBegin(out);
    out.writeStruct(serialized.header());
    headerBlock.markEnd(out);
    L.log(Level.FINER, "headerBlock: {0}", headerBlock);
}
Also used : DataBlock(info.ata4.util.io.DataBlock)

Example 5 with DataWriter

use of info.ata4.io.DataWriter in project disunity by ata4.

the class TypeTreeV1 method writeNode.

private void writeNode(DataWriter out, Node<T> node) throws IOException {
    T type = node.data();
    out.writeStruct(type);
    int numChildren = node.size();
    out.writeInt(numChildren);
    for (Node child : node) {
        writeNode(out, child);
    }
}
Also used : Node(info.ata4.util.collection.Node)

Aggregations

DataBlock (info.ata4.util.io.DataBlock)3 DataWriter (info.ata4.io.DataWriter)2 Node (info.ata4.util.collection.Node)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 BiMap (com.google.common.collect.BiMap)1 DataReader (info.ata4.io.DataReader)1 DataWriters (info.ata4.io.DataWriters)1 LzmaEncoderProps (info.ata4.io.lzma.LzmaEncoderProps)1 UnityHash128 (info.ata4.junity.UnityHash128)1 UnityVersion (info.ata4.junity.UnityVersion)1 Progress (info.ata4.junity.progress.Progress)1 SerializedFileException (info.ata4.junity.serialize.SerializedFileException)1 ObjectInfo (info.ata4.junity.serialize.objectinfo.ObjectInfo)1 BufferedOutputStream (java.io.BufferedOutputStream)1 Closeable (java.io.Closeable)1 InputStream (java.io.InputStream)1