readRuntimeObject<T extends Core<CoreContext>> function

T readRuntimeObject<T extends Core<CoreContext>>(
  1. BinaryReader reader,
  2. [T instance]
)

Implementation

T readRuntimeObject<T extends Core<CoreContext>>(BinaryReader reader,
    [T instance]) {
  int coreObjectKey = reader.readVarUint();

  var object = instance ?? RiveCoreContext.makeCoreInstance(coreObjectKey);
  if (object is! T) {
    return null;
  }

  while (true) {
    int propertyKey = reader.readVarUint();
    if (propertyKey == 0) {
      // Terminator. https://media.giphy.com/media/7TtvTUMm9mp20/giphy.gif
      break;
    }
    int propertyLength = reader.readVarUint();
    Uint8List valueBytes = reader.read(propertyLength);

    var fieldType = RiveCoreContext.coreType(propertyKey);
    if (fieldType == null) {
      // This is considered an acceptable failure. A runtime may not support
      // the same properties that were exported. The older object could still
      // function without them, however, so it's up to the implementation to
      // make sure that major versions are revved when breaking properties are
      // added. Note that we intentionally first read the entire value bytes
      // for the property so we can advance as expected even if we are
      // skipping this value.
      continue;
    }

    // We know what to expect, let's try to read the value. We instance a new
    // reader here so that we don't overflow the exact length we're allowed to
    // read.
    var valueReader = BinaryReader.fromList(valueBytes);

    // This will attempt to set the object property, but failure here is
    // acceptable.
    RiveCoreContext.setObjectProperty(
        object, propertyKey, fieldType.deserialize(valueReader));
  }
  return object as T;
}