コード例 #1
0
  private EntityData.Component serializeComponent(Component base, Component delta) {
    ComponentMetadata<?> componentMetadata = componentLibrary.getMetadata(base.getClass());
    if (componentMetadata == null) {
      logger.log(Level.SEVERE, "Unregistered component type: " + base.getClass());
      return null;
    }

    EntityData.Component.Builder componentMessage = EntityData.Component.newBuilder();
    if (useLookupTables) {
      componentMessage.setTypeIndex(componentIdTable.inverse().get(base.getClass()));
    } else {
      componentMessage.setType(ComponentUtil.getComponentClassName(delta));
    }

    boolean changed = false;
    for (FieldMetadata field : componentMetadata.iterateFields()) {
      try {
        Object origValue = field.getValue(base);
        Object deltaValue = field.getValue(delta);

        if (!Objects.equal(origValue, deltaValue)) {
          EntityData.Value value = field.serialize(deltaValue);
          componentMessage.addField(
              EntityData.NameValue.newBuilder().setName(field.getName()).setValue(value).build());
          changed = true;
        }
      } catch (IllegalAccessException e) {
        logger.log(
            Level.SEVERE, "Exception during serializing component type: " + base.getClass(), e);
      } catch (InvocationTargetException e) {
        logger.log(
            Level.SEVERE, "Exception during serializing component type: " + base.getClass(), e);
      }
    }
    if (changed) {
      return componentMessage.build();
    }
    return null;
  }
コード例 #2
0
  @Override
  public EntityData.Component serializeComponent(Component component) {
    ComponentMetadata<?> componentMetadata = componentLibrary.getMetadata(component.getClass());
    if (componentMetadata == null) {
      logger.log(Level.SEVERE, "Unregistered component type: " + component.getClass());
      return null;
    }
    EntityData.Component.Builder componentMessage = EntityData.Component.newBuilder();
    if (useLookupTables) {
      componentMessage.setTypeIndex(componentIdTable.inverse().get(component.getClass()));
    } else {
      componentMessage.setType(ComponentUtil.getComponentClassName(component));
    }

    for (FieldMetadata field : componentMetadata.iterateFields()) {
      try {
        Object rawValue = field.getValue(component);
        if (rawValue == null) continue;

        EntityData.Value value = field.serialize(rawValue);
        if (value == null) continue;

        componentMessage.addField(
            EntityData.NameValue.newBuilder().setName(field.getName()).setValue(value).build());
      } catch (IllegalAccessException e) {
        logger.log(
            Level.SEVERE,
            "Exception during serializing component type: " + component.getClass(),
            e);
      } catch (InvocationTargetException e) {
        logger.log(
            Level.SEVERE,
            "Exception during serializing component type: " + component.getClass(),
            e);
      }
    }

    return componentMessage.build();
  }