Search in sources :

Example 6 with DataBlock

use of info.ata4.util.io.DataBlock 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 7 with DataBlock

use of info.ata4.util.io.DataBlock in project disunity by ata4.

the class SerializedFileTest method offsetsAndSizesValid.

@Test
public void offsetsAndSizesValid() throws IOException {
    SerializedFileHeader header = asset.header();
    long fileSize = Files.size(readFile);
    assertEquals("Header file size and actual file size must be equal", header.fileSize(), fileSize);
    assertTrue("Data offset must be within file", header.dataOffset() < header.fileSize());
    // allowing a difference of 3 bytes because of padding
    assertEquals("Metadata sizes in data block and header must match", header.metadataSize(), asset.metadataBlock().length(), 3);
    // check blocks
    List<DataBlock> blocks = asset.dataBlocks();
    blocks.forEach(block1 -> blocks.forEach(block2 -> {
        if (block1 != block2) {
            assertFalse("Blocks are overlapping: " + block1 + " / " + block2, block1.isIntersecting(block2));
        }
    }));
}
Also used : SerializedFileWriter(info.ata4.junity.serialize.SerializedFileWriter) RunWith(org.junit.runner.RunWith) Type(info.ata4.junity.serialize.typetree.Type) ArrayList(java.util.ArrayList) Path(java.nio.file.Path) WRITE(java.nio.file.StandardOpenOption.WRITE) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) DataWriters(info.ata4.io.DataWriters) ParameterizedUtils(info.ata4.test.ParameterizedUtils) Files(java.nio.file.Files) Node(info.ata4.util.collection.Node) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) DataBlock(info.ata4.util.io.DataBlock) SerializedFile(info.ata4.junity.serialize.SerializedFile) List(java.util.List) SerializedFileHeader(info.ata4.junity.serialize.SerializedFileHeader) Paths(java.nio.file.Paths) Assert.assertFalse(org.junit.Assert.assertFalse) SerializedFileReader(info.ata4.junity.serialize.SerializedFileReader) Assert.assertEquals(org.junit.Assert.assertEquals) SerializedFileHeader(info.ata4.junity.serialize.SerializedFileHeader) DataBlock(info.ata4.util.io.DataBlock) Test(org.junit.Test)

Example 8 with DataBlock

use of info.ata4.util.io.DataBlock in project disunity by ata4.

the class AssetBlocks method tableModel.

@Override
protected TableModel tableModel(SerializedFile serialized) {
    Map<String, DataBlock> blocks = new LinkedHashMap<>();
    blocks.put("Header", serialized.headerBlock());
    blocks.put("Type Tree", serialized.metadata().typeTreeBlock());
    blocks.put("Object Info", serialized.metadata().objectInfoBlock());
    blocks.put("External Refs", serialized.metadata().externalsBlock());
    blocks.put("Object Data", serialized.objectDataBlock());
    TableBuilder table = new TableBuilder();
    table.row("Name", "Offset", "Size");
    blocks.entrySet().stream().sorted((e1, e2) -> Long.compare(e1.getValue().offset(), e2.getValue().offset())).forEach(e -> {
        DataBlock block = e.getValue();
        table.row(e.getKey(), block.offset(), block.length());
    });
    TableModel model = new TableModel("Blocks", table.get());
    TextTableFormat format = model.format();
    format.columnFormatter(1, Formatters::hex);
    return model;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) Parameters(com.beust.jcommander.Parameters) TableModel(info.ata4.disunity.cli.util.TableModel) TableBuilder(info.ata4.disunity.cli.util.TableBuilder) Map(java.util.Map) Formatters(info.ata4.disunity.cli.util.Formatters) TextTableFormat(info.ata4.disunity.cli.util.TextTableFormat) DataBlock(info.ata4.util.io.DataBlock) SerializedFile(info.ata4.junity.serialize.SerializedFile) Formatters(info.ata4.disunity.cli.util.Formatters) TextTableFormat(info.ata4.disunity.cli.util.TextTableFormat) TableBuilder(info.ata4.disunity.cli.util.TableBuilder) DataBlock(info.ata4.util.io.DataBlock) TableModel(info.ata4.disunity.cli.util.TableModel) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with DataBlock

use of info.ata4.util.io.DataBlock 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)9 ByteBuffer (java.nio.ByteBuffer)3 SerializedFile (info.ata4.junity.serialize.SerializedFile)2 ObjectInfo (info.ata4.junity.serialize.objectinfo.ObjectInfo)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Map (java.util.Map)2 Parameters (com.beust.jcommander.Parameters)1 Formatters (info.ata4.disunity.cli.util.Formatters)1 TableBuilder (info.ata4.disunity.cli.util.TableBuilder)1 TableModel (info.ata4.disunity.cli.util.TableModel)1 TextTableFormat (info.ata4.disunity.cli.util.TextTableFormat)1 DataWriters (info.ata4.io.DataWriters)1 SerializedFileHeader (info.ata4.junity.serialize.SerializedFileHeader)1 SerializedFileReader (info.ata4.junity.serialize.SerializedFileReader)1 SerializedFileWriter (info.ata4.junity.serialize.SerializedFileWriter)1 SerializedObjectData (info.ata4.junity.serialize.SerializedObjectData)1 Type (info.ata4.junity.serialize.typetree.Type)1 TypeRoot (info.ata4.junity.serialize.typetree.TypeRoot)1 ParameterizedUtils (info.ata4.test.ParameterizedUtils)1