parseLayerData static method
Implementation
static List<int>? parseLayerData(
Parser parser,
FileEncoding encoding,
Compression? compression,
) {
final dynamic data = parser.formatSpecificParsing<dynamic>(
(json) => json.json['data'],
(xml) {
if (xml.element.children.length != 1) {
return null;
}
final text = xml.element.children.first;
if (text is XmlText) {
return text.text;
}
return null;
},
);
if (data == null) {
return null;
}
if (encoding == FileEncoding.csv) {
if (data is List) {
return data.cast<int>();
} else {
// csv data must be parsed
return List.from(
(data as String).split(',').map<int>((s) => int.parse(s.trim())),
);
}
}
// Ok, its base64
final trim = data.toString().replaceAll('\n', '').trim();
final decodedString = base64.decode(trim);
// zlib, gzip, zstd or empty
List<int> decompressed;
switch (compression) {
case Compression.zlib:
decompressed = const ZLibDecoder().decodeBytes(decodedString);
break;
case Compression.gzip:
decompressed = GZipDecoder().decodeBytes(decodedString);
break;
case Compression.zstd:
throw UnsupportedError('zstd is an unsupported compression');
case null:
decompressed = decodedString;
break;
}
// From the tiled documentation:
// Now you have an array of bytes, which should be interpreted as an array
// of unsigned 32-bit integers using little-endian byte ordering.
final bytes = Uint8List.fromList(decompressed);
final dv = ByteData.view(bytes.buffer);
final uint32 = <int>[];
for (var i = 0; i < decompressed.length; ++i) {
if (i % 4 == 0) {
uint32.add(dv.getUint32(i, Endian.little));
}
}
return uint32;
}