Ejemplo n.º 1
0
  private static Class<?> convertFieldTypeToClass(FieldType field) {
    PacketSimpleTypes type = field.getType();

    if (type == null) {
      throw new IllegalArgumentException("Type cannot be null");
    }

    switch (type) {
      case BYTE:
        return short.class;
      case SHORT:
        return int.class;
      case INT:
        return long.class;
      case AGENTID:
        return long.class;
      case LONG:
        return long.class;
      case FLOAT:
        return float.class;
      case VEC_2:
        return Vector2.class;
      case VEC_3:
        return Vector3.class;
      case VEC_4:
        return Vector4.class;
      case DW_3:
        return WorldPosition.class;
      case VARINT:
        return int.class;
      case ASCII:
        return String.class;
      case UTF_16:
        return String.class;
      case UID_16:
        return UUID.class;
      case GUID_18:
        return GUID18.class;
      case BUFFER_STATIC:
        return byte[].class;
      case BUFFER_VAR_SMALL:
        return byte[].class;
      case BUFFER_VAR_BIG:
        return byte[].class;

      default:
        throw new UnsupportedOperationException(
            "Unsupported field type because it was unused until now: " + type);
    }
  }
Ejemplo n.º 2
0
  private static void processField(
      FieldType field,
      JDefinedClass packetClass,
      JCodeModel codeModel,
      AtomicInteger numberOfUnknowns,
      boolean fromClient)
      throws JClassAlreadyExistsException {
    boolean isNested =
        (field.getType() == PacketSimpleTypes.OPTIONAL)
            || (field.getType() == PacketSimpleTypes.ARRAY_STATIC)
            || (field.getType() == PacketSimpleTypes.ARRAY_VAR_SMALL)
            || (field.getType() == PacketSimpleTypes.ARRAY_VAR_BIG);

    boolean isArray =
        (field.getType() == PacketSimpleTypes.BUFFER_STATIC)
            || (field.getType() == PacketSimpleTypes.BUFFER_VAR_SMALL)
            || (field.getType() == PacketSimpleTypes.BUFFER_VAR_BIG)
            || (isNested && (field.getType() != PacketSimpleTypes.OPTIONAL));

    boolean isStaticLength =
        (isArray)
            && ((field.getType() == PacketSimpleTypes.ARRAY_STATIC)
                || (field.getType() == PacketSimpleTypes.BUFFER_STATIC));

    // length is either not set for non arrays and static arrays,
    // its 1byte for small stuff and 2bytes for big stuff.
    int prefixLength =
        (!isArray || isStaticLength)
            ? -1
            : (field.getType() == PacketSimpleTypes.ARRAY_VAR_SMALL)
                    || (field.getType() == PacketSimpleTypes.BUFFER_VAR_SMALL)
                ? 1
                : 2;

    String name = "";

    if (field.getInfo() == null || field.getInfo().getName() == null) {
      // check if we got special fields...
      if (field.getType() == PacketSimpleTypes.AGENTID) {
        name = "AgentID";
      } else if (field.getType() == PacketSimpleTypes.OPTIONAL) {
        name = "Optional";
      } else {
        name = "Unknown";
      }

      name += numberOfUnknowns.incrementAndGet();
    } else {
      name = field.getInfo().getName();
    }

    String fieldName = WordUtils.uncapitalize(name);
    String fieldDescription =
        (field.getInfo() == null
                || field.getInfo().getDescription() == null
                || field.getInfo().getDescription().isEmpty())
            ? ""
            : "\n" + WordUtils.wrap(field.getInfo().getDescription(), /* maximumLength */ 50);

    JType fieldType;

    if (isNested) {
      JDefinedClass nestedClass =
          packetClass
              ._class(JMod.FINAL | JMod.STATIC | JMod.PUBLIC, "Nested" + name)
              ._implements(NestedMarker.class);

      AtomicInteger numberOfUnknownsNested = new AtomicInteger();

      for (FieldType nested : field.getField()) {
        processField(nested, nestedClass, codeModel, numberOfUnknownsNested, fromClient);
      }

      // generate getters, setters
      for (JFieldVar fieldVar : nestedClass.fields().values()) {
        processAccessors(fieldVar, nestedClass, fromClient);
      }

      processToString(nestedClass, codeModel);

      // nested classes are either arrays or optional...
      // meaning we will later have to test if they are null before reading/writing
      fieldType = isArray ? nestedClass.array() : nestedClass;
    } else {
      Class<?> fieldClass = convertFieldTypeToClass(field);
      fieldType = codeModel._ref(fieldClass);
    }

    LOGGER.debug("|+-Processing field: {}, of type: {}", fieldName, fieldType);

    // add the field
    JFieldVar packetField = packetClass.field(JMod.PRIVATE, fieldType, fieldName);

    if (fieldDescription != null && !fieldDescription.trim().equals("")) {
      JDocComment jDocComment = packetField.javadoc();
      jDocComment.add(fieldDescription);
    }

    // and dont forget array annotations if necessary
    if (isArray) {
      int size = (int) (field.getElements() == null ? -1 : field.getElements());

      packetField
          .annotate(IsArray.class)
          .param("constant", isStaticLength)
          .param("size", size)
          .param("prefixLength", prefixLength);
    }

    // or any special annotations
    if (field.getType() == PacketSimpleTypes.LONG) {
      packetField.annotate(IsInt64.class);
    }
    if (field.getType() == PacketSimpleTypes.VARINT) {
      packetField.annotate(IsVarInt.class);
    }
    if (field.getType() == PacketSimpleTypes.ASCII) {
      packetField.annotate(IsASCII.class);
    }
  }