parseResourceArray static method

AsyncBag<IResource?> parseResourceArray(
  1. DC data,
  2. int offset,
  3. int length,
  4. DistributedConnection connection,
)
Parse an array of bytes into array of resources Array of bytes. Number of bytes to parse. Zero-indexed offset. DistributedConnection is required to fetch resources.

Implementation

static AsyncBag<IResource?> parseResourceArray(
    DC data, int offset, int length, DistributedConnection connection) {
  //print("parseResourceArray ${offset} ${length}");

  var reply = new AsyncBag<IResource?>();
  if (length == 0) {
    reply.seal();
    return reply;
  }

  var end = offset + length;

  //

  // Is typed array ?
  var type = (data[offset] & 0xF0);

  var result = data[offset++] & 0xF;

  if (type == ResourceArrayType.Wrapper) {
    var classId = data.getGuid(offset);
    offset += 16;
    var tmp = Warehouse.getTemplateByClassId(classId, TemplateType.Resource);
    // not mine, look if the type is elsewhere
    if (tmp == null)
      Warehouse.getTemplateByClassId(classId, TemplateType.Wrapper);
    reply.arrayType = tmp?.definedType;
  } else if (type == ResourceArrayType.Static) {
    var classId = data.getGuid(offset);
    offset += 16;
    var tmp = Warehouse.getTemplateByClassId(classId, TemplateType.Wrapper);
    reply.arrayType = tmp?.definedType;
  }

  AsyncReply<IResource?>? previous = null;

  if (result == ResourceComparisonResult.Empty) {
    reply.seal();
    return reply;
  } else if (result == ResourceComparisonResult.Null)
    previous = new AsyncReply<IResource?>.ready(null);
  else if (result == ResourceComparisonResult.Local) {
    previous = Warehouse.getById(data.getUint32(offset));
    offset += 4;
  } else if (result == ResourceComparisonResult.Distributed) {
    previous = connection.fetch(data.getUint32(offset));
    offset += 4;
  }

  reply.add(previous as AsyncReply<IResource?>);

  while (offset < end) {
    result = data[offset++];

    AsyncReply<IResource?>? current = null;

    if (result == ResourceComparisonResult.Empty) {
      reply.seal();
      return reply;
    } else if (result == ResourceComparisonResult.Null) {
      current = new AsyncReply<IResource?>.ready(null);
    } else if (result == ResourceComparisonResult.Same) {
      current = previous;
    } else if (result == ResourceComparisonResult.Local) {
      current = Warehouse.getById(data.getUint32(offset));
      offset += 4;
    } else if (result == ResourceComparisonResult.Distributed) {
      current = connection.fetch(data.getUint32(offset));
      offset += 4;
    }

    reply.add(current as AsyncReply<IResource?>);

    previous = current;
  }

  reply.seal();
  return reply;
}