Search in sources :

Example 1 with SerializedObjectData

use of info.ata4.junity.serialize.SerializedObjectData in project disunity by ata4.

the class SerializedFileReader method readObjects.

private void readObjects(DataReader in) throws IOException {
    long ofsMin = Long.MAX_VALUE;
    long ofsMax = Long.MIN_VALUE;
    SerializedFileHeader header = serialized.header();
    SerializedFileMetadata metadata = serialized.metadata();
    Map<Long, ObjectInfo> objectInfoMap = metadata.objectInfoTable().infoMap();
    Map<Integer, TypeRoot<Type>> typeTreeMap = metadata.typeTree().typeMap();
    List<SerializedObjectData> objectData = serialized.objectData();
    for (Map.Entry<Long, ObjectInfo> infoEntry : objectInfoMap.entrySet()) {
        ObjectInfo info = infoEntry.getValue();
        long id = infoEntry.getKey();
        long ofs = header.dataOffset() + info.offset();
        ofsMin = Math.min(ofsMin, ofs);
        ofsMax = Math.max(ofsMax, ofs + info.length());
        SerializedObjectData object = new SerializedObjectData(id);
        object.info(info);
        // create and read object data buffer
        ByteBuffer buf = ByteBufferUtils.allocate((int) info.length());
        in.position(ofs);
        in.readBuffer(buf);
        object.buffer(buf);
        // get type tree if possible
        TypeRoot typeRoot = typeTreeMap.get(info.typeID());
        if (typeRoot != null) {
            object.typeTree(typeRoot.nodes());
        }
        objectData.add(object);
    }
    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) TypeRoot(info.ata4.junity.serialize.typetree.TypeRoot) Map(java.util.Map)

Example 2 with SerializedObjectData

use of info.ata4.junity.serialize.SerializedObjectData 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 3 with SerializedObjectData

use of info.ata4.junity.serialize.SerializedObjectData in project disunity by ata4.

the class AssetUnpack method runSerializedFile.

@Override
protected void runSerializedFile(Path file, SerializedFile asset) {
    try {
        Path outputDir = PathUtils.removeExtension(file);
        if (Files.isRegularFile(outputDir)) {
            outputDir = PathUtils.append(outputDir, "_");
        }
        if (Files.notExists(outputDir)) {
            Files.createDirectory(outputDir);
        }
        try (FileChannel fc = FileChannel.open(file)) {
            DataBlock headerBlock = asset.headerBlock();
            Path headerFile = outputDir.resolve("header.block");
            copyBlock(headerBlock, headerFile, fc);
            if (level > 0) {
                DataBlock typeTreeBlock = asset.metadata().typeTreeBlock();
                Path typeTreeFile = outputDir.resolve("type_tree.block");
                copyBlock(typeTreeBlock, typeTreeFile, fc);
                DataBlock objectInfoBlock = asset.metadata().objectInfoBlock();
                Path objectInfoFile = outputDir.resolve("object_info.block");
                copyBlock(objectInfoBlock, objectInfoFile, fc);
                DataBlock objectIDBlock = asset.metadata().objectIDBlock();
                if (objectIDBlock.length() > 0) {
                    Path objectIDFile = outputDir.resolve("object_ids.block");
                    copyBlock(objectIDBlock, objectIDFile, fc);
                }
                DataBlock fileIdentBlock = asset.metadata().externalsBlock();
                Path fileIdentFile = outputDir.resolve("linked_files.block");
                copyBlock(fileIdentBlock, fileIdentFile, fc);
            } else {
                DataBlock metadataBlock = asset.metadataBlock();
                Path metadataFile = outputDir.resolve("metadata.block");
                copyBlock(metadataBlock, metadataFile, fc);
            }
            if (level < 2) {
                DataBlock objectDataBlock = asset.objectDataBlock();
                Path objectDataFile = outputDir.resolve("object_data.block");
                copyBlock(objectDataBlock, objectDataFile, fc);
            }
        }
        if (level > 1) {
            Path objectDataDir = outputDir.resolve("object_data");
            if (Files.notExists(objectDataDir)) {
                Files.createDirectory(objectDataDir);
            }
            for (SerializedObjectData od : asset.objectData()) {
                String objectDataName = String.format("%010d", od.id());
                Path objectDataFile = objectDataDir.resolve(objectDataName + ".block");
                ByteBuffer objectDataBuffer = od.buffer();
                objectDataBuffer.rewind();
                ByteBufferUtils.save(objectDataFile, objectDataBuffer);
            }
        }
    } catch (IOException ex) {
        L.log(Level.WARNING, "Can't unpack asset file " + file, ex);
    }
}
Also used : Path(java.nio.file.Path) SerializedObjectData(info.ata4.junity.serialize.SerializedObjectData) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) DataBlock(info.ata4.util.io.DataBlock)

Aggregations

DataBlock (info.ata4.util.io.DataBlock)3 ByteBuffer (java.nio.ByteBuffer)3 ObjectInfo (info.ata4.junity.serialize.objectinfo.ObjectInfo)2 SerializedObjectData (info.ata4.junity.serialize.SerializedObjectData)1 TypeRoot (info.ata4.junity.serialize.typetree.TypeRoot)1 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 Path (java.nio.file.Path)1 Map (java.util.Map)1