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);
}
}
Aggregations