composeResourceArray<T extends IResource> static method

DC composeResourceArray<T extends IResource>(
  1. List<T>? resources,
  2. DistributedConnection connection, [
  3. bool prependLength = false
])
Compose an array of resources Array of resources. DistributedConnection is required to check locality. If True, prepend the length of the output at the beginning.

Implementation

static DC composeResourceArray<T extends IResource>(
    List<T>? resources, DistributedConnection connection,
    [bool prependLength = false]) {
  if (resources == null) // || resources.length == 0)
    return prependLength ? new DC(4) : new DC(0);

  var rt = new BinaryList();
  //var comparsion = compareResources(null, resources[0], connection);
  var comparsion = resources.length == 0
      ? ResourceComparisonResult.Empty
      : compareResources(null, resources[0], connection);

  if (T != IResource) {
    // get template
    var tmp = Warehouse.getTemplateByType(T);

    if (tmp == null) // something wrong
      rt.addUint8(comparsion);
    else {
      // typed array
      rt.addUint8((tmp.type == TemplateType.Resource
              ? ResourceArrayType.Static
              : ResourceArrayType.Wrapper) |
          comparsion);
      // add type
      rt.addGuid(tmp.classId);
    }
  } else {
    rt.addUint8(comparsion);
  }

  if (comparsion == ResourceComparisonResult.Local)
    rt.addUint32((resources[0] as DistributedResource).id as int);
  else if (comparsion == ResourceComparisonResult.Distributed)
    rt.addUint32(resources[0].instance?.id as int);

  for (var i = 1; i < resources.length; i++) {
    comparsion = compareResources(resources[i - 1], resources[i], connection);
    rt.addUint8(comparsion);
    if (comparsion == ResourceComparisonResult.Local)
      rt.addUint32((resources[i] as DistributedResource).id as int);
    else if (comparsion == ResourceComparisonResult.Distributed)
      rt.addUint32(resources[i].instance?.id as int);
  }

  if (prependLength) rt.insertInt32(0, rt.length);

  return rt.toDC();
}