Ejemplo n.º 1
0
  @Override
  protected DataBuffer encodeBespoke() {
    int size = 3 * Short.BYTES;
    for (Model model : Arrays.asList(primary, secondary)) {
      size += model.getId().isPresent() ? Short.BYTES : Byte.BYTES;
      size += model.getAnimation().isPresent() ? Short.BYTES : Byte.BYTES;
    }

    DataBuffer buffer = DataBuffer.allocate(size);
    for (OptionalInt optional :
        Arrays.asList(
            primary.getId(), secondary.getId(), primary.getAnimation(), secondary.getAnimation())) {
      if (optional.isPresent()) {
        int id = optional.getAsInt();
        buffer.putByte(id >> 8 + 1);
        buffer.putByte(id);
      } else {
        buffer.putBoolean(false);
      }
    }

    buffer.putShort(scale);
    buffer.putShort(pitch);
    buffer.putShort(roll);

    return buffer.flip().asReadOnlyBuffer();
  }
Ejemplo n.º 2
0
  /**
   * Decodes a ClientScript.
   *
   * @param buffer The Buffer.
   * @return The ClientScript.
   */
  @SuppressWarnings("unused")
  public static ClientScript666 decode(DataBuffer buffer) {
    ClientScript666 cs = new ClientScript666();

    buffer.position(buffer.limit() - 2);
    int trailerLength = buffer.getShort() & 0xFFFF;
    int trailerPosition = buffer.limit() - 18 - trailerLength;
    buffer.position(trailerPosition);

    int operations = buffer.getInt();
    int intVariables = buffer.getUnsignedShort();
    int stringVariables = buffer.getUnsignedShort();
    int longVariables = buffer.getUnsignedShort();

    int intParameterCount = buffer.getUnsignedShort();
    int stringParameterCount = buffer.getUnsignedShort();
    int longParameterCount = buffer.getUnsignedShort();

    int switches = buffer.getUnsignedByte();

    List<Map<Integer, Integer>> tables = new ArrayList<>(switches);
    for (int index = 0; index < switches; index++) {
      int cases = buffer.getUnsignedShort();
      Map<Integer, Integer> table = new HashMap<>();

      while (cases-- > 0) {
        int value = buffer.getInt();
        int offset = buffer.getInt();
        table.put(value, offset);
      }

      tables.add(table);
    }

    cs.switchTables = tables;

    buffer.position(0);

    String name = buffer.getCString();
    cs.name = name;
    cs.opcodes = new int[operations];
    cs.intOperands = new int[operations];
    cs.stringOperands = new String[operations];
    cs.longOperands = new long[operations];

    int index = 0;
    while (buffer.position() < trailerPosition) {
      int opcode = buffer.getUnsignedShort();

      if (opcode == 3) {
        cs.stringOperands[index] = buffer.getCString();
      } else if (opcode == 54) {
        cs.longOperands[index] = buffer.getLong();
      } else if (opcode >= 150 || opcode == 21 || opcode == 38 || opcode == 39) {
        cs.intOperands[index] = buffer.getUnsignedByte();
      } else {
        cs.intOperands[index] = buffer.getInt();
      }

      cs.opcodes[index++] = opcode;
    }

    return cs;
  }