open static method
The pack at offset in file, length bytes long, or null when the
bytes there are not one. Reads the index and nothing else.
Implementation
static DVAssetPack? open(String file, {required int offset, required int length}) {
final RandomAccessFile raf;
try {
raf = File(file).openSync();
} on FileSystemException {
return null;
}
try {
if (length < 12) return null;
raf.setPositionSync(offset);
final Uint8List head = raf.readSync(12);
if (head.length < 12 || ascii.decode(head.sublist(0, 8), allowInvalid: true) != _magic) {
return null;
}
final int indexLength = ByteData.sublistView(head).getUint32(8, Endian.little);
if (indexLength > length - 12) return null;
final Object? index = jsonDecode(utf8.decode(raf.readSync(indexLength)));
if (index is! Map || index['entries'] is! List || index['build'] is! String) {
return null;
}
final int data = offset + 12 + indexLength;
final int dataLength = length - 12 - indexLength;
final Map<String, DVPackedAsset> entries = <String, DVPackedAsset>{};
for (final Object? row in index['entries'] as List) {
if (row is! List || row.length < 7) return null;
final int at = row[1] as int;
final int stored = row[2] as int;
if (at < 0 || stored < 0 || at + stored > dataLength) return null;
final int encoding = row[4] as int;
if (encoding < 0 || encoding >= DVAssetEncoding.values.length) return null;
final String path = row[0] as String;
entries[path] = DVPackedAsset._(
path: path,
offset: data + at,
storedLength: stored,
size: row[3] as int,
encoding: DVAssetEncoding.values[encoding],
hash: row[5] as String,
protected: (row[6] as int) & _protectedFlag != 0,
);
}
return DVAssetPack._(file, index['build'] as String, entries);
} on FormatException {
return null;
} on TypeError {
return null;
} on FileSystemException {
return null;
} finally {
raf.closeSync();
}
}