示例#1
0
 private int classDataSize(Class<?> cl) {
   int len = 0;
   for (Field f : cl.getDeclaredFields()) {
     if (f.isAnnotationPresent(FlightSimData.class)) {
       len += fieldSize(f);
     }
   }
   return len;
 }
示例#2
0
  private void wrap(Object o, DataWrapper dw) throws IllegalDataDefinition {

    Class<?> cl = o.getClass();
    if (!definitionIds.containsKey(cl))
      throw new IllegalDataDefinition("Class not defined in this wrapper.");

    for (Field f : cl.getDeclaredFields()) {
      if (f.isAnnotationPresent(FlightSimData.class)) {
        try {
          wrapField(o, f, dw);
        } catch (IllegalAccessException e) {
        }
      }
    }
  }
示例#3
0
  /**
   * Unwrap data, i.e. fill an object instance fields from a received response packet.
   *
   * @param data data packet
   * @param o fresh instance to fill
   * @return value of object, or null if it cannot be build for instance if the response packet does
   *     not corresponds to a managed request
   * @throws IllegalDataDefinition if the class of <code>o</code> is not managed by this wrapper
   * @throws IllegalArgumentException from reflection API (bad field type?)
   */
  public Object unwrapToObject(Object o, RecvSimObjectData data) throws IllegalDataDefinition {

    Class<?> cl = o.getClass();
    if (!definitionIds.containsKey(cl))
      throw new IllegalDataDefinition("Class not defined in this wrapper.");

    for (Field f : cl.getDeclaredFields()) {
      if (f.isAnnotationPresent(FlightSimData.class)) {
        try {
          unwrapField(o, f, data);
        } catch (IllegalAccessException e) {
        }
      }
    }
    return o;
  }
示例#4
0
  /**
   * Register a class prior to use. This is mandatory before calling {@link
   * #requestSimObjectData(Class, int, SimConnectPeriod, boolean)} or {@link
   * #unwrap(RecvSimObjectData)} or {@link #setSimObjectData(Object, int)}
   *
   * @param c class to use
   * @param dataDefID force data definition ID
   * @return data definition ID (here <code>dataDefID</code>)
   * @throws IllegalDataDefinition if the class <code>c</code> contains invalid or non-mappable
   *     fields
   * @throws IOException SimConnect IO Errors
   */
  public int registerClass(int dataDefID, Class<?> c) throws IOException, IllegalDataDefinition {

    //
    // don't do it twice
    if (definitionIds.containsKey(c)) {
      return definitionIds.get(c).intValue();
    }

    int fieldAdded = 0;

    for (Field f : c.getDeclaredFields()) {
      if (f.isAnnotationPresent(FlightSimData.class)) {
        // register field
        FlightSimData fsd = f.getAnnotation(FlightSimData.class);

        String variable = fsd.variable();
        String units = fsd.units();

        // determinate field simconnect type from its class

        SimConnectDataType type = SimConnectDataType.INVALID;
        if (f.getType().equals(Float.class) || f.getType().equals(float.class))
          type = SimConnectDataType.FLOAT32;
        else if (f.getType().equals(Double.class) || f.getType().equals(double.class))
          type = SimConnectDataType.FLOAT64;
        else if (f.getType().equals(Integer.class) || f.getType().equals(int.class))
          type = SimConnectDataType.INT32;
        else if (f.getType().equals(Long.class) || f.getType().equals(long.class))
          type = SimConnectDataType.INT64;
        else if (f.getType().equals(Boolean.class) || f.getType().equals(boolean.class))
          type = SimConnectDataType.INT32;
        else if (f.getType().equals(Short.class) || f.getType().equals(short.class))
          type = SimConnectDataType.INT32;
        else if (f.getType().equals(LatLonAlt.class)) type = SimConnectDataType.LATLONALT;
        else if (f.getType().equals(XYZ.class)) type = SimConnectDataType.XYZ;
        else if (f.getType().equals(Waypoint.class)) type = SimConnectDataType.WAYPOINT;
        else if (f.getType().equals(MarkerState.class)) type = SimConnectDataType.MARKERSTATE;
        else if (f.getType().equals(String.class)) {
          int len = fsd.stringWidth();
          switch (len) {
            case 8:
              type = SimConnectDataType.STRING8;
              break;
            case 32:
              type = SimConnectDataType.STRING32;
              break;
            case 64:
              type = SimConnectDataType.STRING64;
              break;
            case 128:
              type = SimConnectDataType.STRING128;
              break;
            case 256:
              type = SimConnectDataType.STRING256;
              break;
            case 260:
              type = SimConnectDataType.STRING260;
              break;
            default:
              throw new IllegalDataDefinition("Invalid string length (" + len + ")");
          }
        }

        if (type == SimConnectDataType.INVALID) {
          throw new IllegalDataDefinition("Invalid field type (" + f.getType().getName() + ")");
        }

        //
        // build simconnect data def
        simConnect.addToDataDefinition(dataDefID, variable, units, type);
        fieldAdded++;
      }
    }

    // register it if we had something to write
    if (fieldAdded > 0) definitionIds.put(c, dataDefID);

    return requestID;
  }