load method

Future<bool> load(
  1. ByteData data,
  2. dynamic context
)

Implementation

Future<bool> load(ByteData data, dynamic context) async {
  if (data.lengthInBytes < 5) {
    throw UnsupportedError('Not a valid Flare file.');
  }

  bool success = true;

  int F = data.getUint8(0);
  int L = data.getUint8(1);
  int A = data.getUint8(2);
  int R = data.getUint8(3);
  int E = data.getUint8(4);

  dynamic inputData = data;

  if (F != 70 || L != 76 || A != 65 || R != 82 || E != 69) {
    Uint8List charCodes = data.buffer.asUint8List();
    String stringData = String.fromCharCodes(charCodes);
    dynamic jsonActor = jsonDecode(stringData);
    Map jsonObject = <dynamic, dynamic>{};
    jsonObject['container'] = jsonActor;
    inputData = jsonObject;
  }

  StreamReader reader = StreamReader(inputData);
  _version = reader.readVersion();

  StreamReader? block;
  while ((block = reader.readNextBlock(blockTypesMap)) != null) {
    switch (block!.blockType) {
      case BlockTypes.artboards:
        readArtboardsBlock(block);
        break;

      case BlockTypes.atlases:
        List<Uint8List> rawAtlases = await readAtlasesBlock(block, context);
        success = await loadAtlases(rawAtlases);
        break;
    }
  }

  // Resolve now.
  for (final ActorArtboard? artboard in _artboards) {
    artboard!.resolveHierarchy();
  }
  for (final ActorArtboard? artboard in _artboards) {
    artboard!.completeResolveHierarchy();
  }

  for (final ActorArtboard? artboard in _artboards) {
    artboard!.sortDependencies();
  }

  return success;
}