Пример #1
0
  private Object read(
      DataInput objBuffer,
      Type propertyType,
      boolean array,
      UnrealPackageReadOnly.ExportEntry arrayInner,
      String structName,
      UnrealPackageReadOnly up)
      throws IOException {
    switch (propertyType) {
      case NONE:
        return null;
      case BYTE:
        return objBuffer.readUnsignedByte();
      case INT:
        return objBuffer.readInt();
      case BOOL:
        return array;
      case FLOAT:
        return objBuffer.readFloat();
      case OBJECT:
        return objBuffer.readCompactInt();
      case NAME:
        return objBuffer.readCompactInt();
      case ARRAY:
        int arraySize = objBuffer.readCompactInt();
        List<Object> arrayList = new ArrayList<>(arraySize);

        String a = arrayInner.getObjectClass().getObjectName().getName();
        Property f = unrealClassLoader.getProperty(arrayInner.getObjectFullName());

        array = false;
        arrayInner = null;
        structName = null;
        propertyType = Type.valueOf(a.replace("Property", "").toUpperCase());
        if (propertyType == Type.STRUCT) {
          StructProperty structProperty = (StructProperty) f;
          structName = structProperty.getStructType().getObjectFullName();
        }
        if (propertyType == Type.ARRAY) {
          array = true;
          ArrayProperty arrayProperty = (ArrayProperty) f;
          arrayInner = (UnrealPackageReadOnly.ExportEntry) arrayProperty.getInner();
        }

        for (int i = 0; i < arraySize; i++) {
          arrayList.add(read(objBuffer, propertyType, array, arrayInner, structName, up));
        }
        return arrayList;
      case STRUCT:
        return readStruct(objBuffer, structName, up);
        /*case VECTOR:
            return readStruct(objBuffer, "Vector", up);
        case ROTATOR:
            return readStruct(objBuffer, "Rotator", up);*/
      case STR:
        return objBuffer.readLine();
      default:
        throw new IllegalStateException("Unk type(" + structName + "): " + propertyType);
    }
  }
Пример #2
0
  public List<L2Property> readProperties(
      DataInput dataInput, String objClass, UnrealPackageReadOnly up) throws UnrealException {
    List<L2Property> properties = new ArrayList<>();

    List<Property> classTemplate = unrealClassLoader.getStructProperties(objClass);

    Collections.reverse(classTemplate);

    try {
      String name;
      while (!(name = up.getNameTable().get(dataInput.readCompactInt()).getName()).equals("None")) {
        int info = dataInput.readUnsignedByte();
        Type propertyType = Type.values()[info & 0b1111]; // TODO
        int sizeType = (info >> 4) & 0b111;
        boolean array = info >> 7 == 1;

        String structName =
            propertyType.equals(Type.STRUCT)
                ? up.getNameTable().get(dataInput.readCompactInt()).getName()
                : null;
        int size = readPropertySize(sizeType, dataInput);
        int arrayIndex = array && !propertyType.equals(Type.BOOL) ? dataInput.readCompactInt() : 0;

        byte[] objBytes = new byte[size];
        dataInput.readFully(objBytes);

        final String n = name;
        PropertiesUtil.getAt(properties, n);
        L2Property property = PropertiesUtil.getAt(properties, n);
        if (property == null) {
          Property template =
              classTemplate
                  .stream()
                  .filter(pt -> pt.getEntry().getObjectName().getName().equalsIgnoreCase((n)))
                  .findAny()
                  .orElse(null);
          if (template == null)
            throw new UnrealException(objClass + ": Property template not found: " + name);

          property = new L2Property(template, up);
          properties.add(property);
        }

        if (structName != null
            && !"Vector".equals(structName)
            && !"Rotator".equals(structName)
            && !"Color".equals(structName)) {
          StructProperty structProperty = (StructProperty) property.getTemplate();
          structName = structProperty.getStructType().getObjectFullName();
        }
        UnrealPackageReadOnly.ExportEntry arrayInner = null;
        if (propertyType.equals(Type.ARRAY)) {
          ArrayProperty arrayProperty = (ArrayProperty) property.getTemplate();
          arrayInner = (UnrealPackageReadOnly.ExportEntry) arrayProperty.getInner();
        }

        DataInput objBuffer =
            new DataInputStream(new ByteArrayInputStream(objBytes), dataInput.getCharset());
        property.putAt(
            arrayIndex,
            read(
                objBuffer,
                propertyType,
                array,
                arrayInner,
                structName,
                up,
                objClass,
                property.getName()));
        property.setType(propertyType);
      }
    } catch (IOException e) {
      throw new UnrealException(e);
    }

    return properties;
  }
Пример #3
0
  private void write(
      DataOutput objBuffer,
      Property template,
      Object obj,
      AtomicBoolean array,
      AtomicReference<String> structName,
      AtomicReference<Type> type,
      UnrealPackageReadOnly up)
      throws IOException {
    if (template instanceof ByteProperty) {
      System.out.println(template.getEntry().getObjectInnerFullName() + " [BYTE]");
      objBuffer.writeByte((Integer) obj);
    } else if (template instanceof IntProperty) {
      System.out.println(template.getEntry().getObjectInnerFullName() + " [INT]");
      objBuffer.writeInt((Integer) obj);
    } else if (template instanceof BoolProperty) {
      System.out.println(template.getEntry().getObjectInnerFullName() + " [BOOL]");
      array.set((Boolean) obj);
    } else if (template instanceof FloatProperty) {
      System.out.println(template.getEntry().getObjectInnerFullName() + " [FLOAT]");
      objBuffer.writeFloat((Float) obj);
    } else if (template instanceof ObjectProperty) {
      System.out.println(template.getEntry().getObjectInnerFullName() + " [OBJ]");
      objBuffer.writeCompactInt((Integer) obj);
    } else if (template instanceof NameProperty) {
      System.out.println(template.getEntry().getObjectInnerFullName() + " [NAME]");
      objBuffer.writeCompactInt((Integer) obj);
    } else if (template instanceof ArrayProperty) {
      System.out.println(template.getEntry().getObjectInnerFullName() + " [ARRAY]");
      ArrayProperty arrayProperty = (ArrayProperty) template;

      List<Object> arrayList = (List<Object>) obj;
      objBuffer.writeCompactInt(arrayList.size());

      UnrealPackageReadOnly.ExportEntry arrayInner =
          (UnrealPackageReadOnly.ExportEntry) arrayProperty.getInner();
      String a = arrayInner.getObjectClass().getObjectName().getName();
      try {
        Class<? extends Property> pc =
            Class.forName("acmi.l2.clientmod.unreal." + a).asSubclass(Property.class);
        Property f =
            pc.getConstructor(
                    ByteBuffer.class, UnrealPackageReadOnly.ExportEntry.class, PropertiesUtil.class)
                .newInstance(
                    ByteBuffer.wrap(arrayInner.getObjectRawDataExternally())
                        .order(ByteOrder.LITTLE_ENDIAN),
                    arrayInner,
                    this);

        for (Object arrayObj : arrayList) {
          write(
              objBuffer,
              f,
              arrayObj,
              new AtomicBoolean(),
              new AtomicReference<>(),
              new AtomicReference<>(),
              up);
        }
      } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
      }
    } else if (template instanceof StructProperty) {
      System.out.println(template.getEntry().getObjectInnerFullName() + " [STRUCT]");
      StructProperty structProperty = (StructProperty) template;
      structName.set(structProperty.getStructType().getObjectName().getName());
      writeStruct(objBuffer, structName.get(), up, (List<L2Property>) obj);
      //            if (false) { //Not used in L2?
      //                switch (structName.get()) {
      //                    case "Vector":
      //                        type.set(Type.VECTOR);
      //                        break;
      //                    case "Rotator":
      //                        type.set(Type.ROTATOR);
      //                        break;
      //                }
      //            }
    } else if (template instanceof StrProperty) {
      System.out.println(template.getEntry().getObjectInnerFullName() + " [STR]");
      objBuffer.writeLine((String) obj);
    } else {
      throw new UnsupportedOperationException(
          template.getClass().getSimpleName() + " serialization not implemented");
    }
  }