Search in sources :

Example 6 with DataWriter

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

the class TypeTreeV3 method writeNode.

private void writeNode(DataWriter out, Node<T> node) throws IOException {
    List<T> types = new ArrayList<>();
    serializeNode(node, types, 0);
    // build string table
    AtomicInteger index = new AtomicInteger();
    Map<String, Integer> localMap = new LinkedHashMap<>();
    Map<String, Integer> commonMap = StringTable.commonStrings(revision.major()).inverse();
    Function<String, Integer> addStringOffset = typeName -> {
        if (commonMap.containsKey(typeName)) {
            return commonMap.get(typeName);
        } else if (localMap.containsKey(typeName)) {
            return localMap.get(typeName);
        } else {
            int stringIndex = index.getAndAdd(typeName.length() + 1);
            localMap.put(typeName, stringIndex);
            return stringIndex;
        }
    };
    // apply string offsets
    types.forEach(type -> {
        type.typeOffset(addStringOffset.apply(type.typeName()));
        type.nameOffset(addStringOffset.apply(type.fieldName()));
    });
    out.writeInt(types.size());
    out.writeInt(index.get());
    for (T type : types) {
        out.writeStruct(type);
    }
    for (String string : localMap.keySet()) {
        out.writeStringNull(string);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiMap(com.google.common.collect.BiMap) DataWriter(info.ata4.io.DataWriter) Node(info.ata4.util.collection.Node) IOException(java.io.IOException) SerializedFileException(info.ata4.junity.serialize.SerializedFileException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) UnityHash128(info.ata4.junity.UnityHash128) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) DataReader(info.ata4.io.DataReader) UnityVersion(info.ata4.junity.UnityVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap)

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