Example #1
0
  public static Set<Mod> load(
      final Path path,
      final int order,
      final SettingsModelFactory settingsFactory,
      final DatabaseModelFactory databaseFactory,
      final LocalizerModelFactory localizerFactory) {

    log.debug("Loading mod: {}", path);

    Set<Mod> mods = new HashSet<>();

    // TODO Count all mods and send back progress info

    Set<Archive> archives = null;

    try {
      archives = processModFile(path, settingsFactory);
    } catch (IOException | StarDBException | ParseException e) {
      // TODO Error Dialogue
      log.error("", e);
      return new HashSet<Mod>();
    }

    for (Archive archive : archives) {

      Mod mod = new Mod(localizerFactory, settingsFactory);

      mod.setOrder(order);
      mod.files = new HashSet<>();
      mod.setArchiveName(archive.getFileName());

      // Get the modinfo file and parse it
      JsonObject obj = JsonObject.readFrom(new String(archive.getFile(".modinfo").getData()));

      mod.setInternalName(obj.get("name").asString());

      if (obj.get("version") != null) {
        mod.setGameVersion(obj.get("version").asString());
      } else {
        mod.setGameVersion("Field Empty");
      }

      Set<String> dependencies = new HashSet<>();
      Set<String> ignoredFileNames = new HashSet<>();

      if (obj.get("dependencies") != null) {
        JsonArray arr = obj.get("dependencies").asArray();
        for (int i = 0; i < arr.size(); i++) {
          dependencies.add(arr.get(i).asString());
        }
      }

      mod.setDependencies(dependencies);

      if (obj.get("metadata") != null) {

        JsonObject metadata = obj.get("metadata").asObject();

        mod.setDisplayName(JSONHelper.getString(metadata, "displayname", mod.getInternalName()));
        mod.setAuthor(JSONHelper.getString(metadata, "author", NO_AUTHOR));
        mod.setDescription(JSONHelper.getString(metadata, "description", NO_DESCRIPTION));
        mod.setURL(JSONHelper.getString(metadata, "support_url", ""));
        mod.setModVersion(JSONHelper.getString(metadata, "version", NO_VERSION));

        if (obj.get("ignoredfiles") != null) {
          JsonArray arr = obj.get("ignoredfiles").asArray();
          for (int i = 0; i < arr.size(); i++) {
            ignoredFileNames.add(arr.get(i).asString());
          }
        }

      } else {

        mod.setDisplayName(mod.getInternalName());
        mod.setAuthor(NO_AUTHOR);
        mod.setDescription(NO_DESCRIPTION);
        mod.setURL("");
        mod.setModVersion(NO_VERSION);
      }

      try {
        mod.setChecksum(
            FileHelper.getChecksum(
                new File(
                        settingsFactory.getInstance().getPropertyString("modsdir")
                            + File.separator
                            + mod.archiveName)
                    .toPath())); // TODO Better Path manipulation
      } catch (IOException e) {
        log.error("Setting Checksum", e);
      }

      for (ArchiveFile archiveFile : archive.getFiles()) {

        ModFile modFile = new ModFile();
        modFile.setPath(archiveFile.getPath());

        // Find and list all ignored files
        for (String ignored : ignoredFileNames) {
          if (archiveFile.getPath().endsWith(ignored) || archiveFile.getPath().endsWith(".txt")) {
            modFile.setIgnored(true);
          }
        }

        // Scan all json files and find those with mergeability
        if (!archiveFile.isFolder() && FileHelper.isJSON(archiveFile.getPath())) {

          modFile.setJson(true);

          String fileContents = new String(archiveFile.getData());

          if (fileContents.contains("__merge")) {
            modFile.setAutoMerged(true);
          }
        }

        if (!archiveFile.isFolder()) {
          mod.files.add(modFile);
        }
      }

      try {
        databaseFactory.getInstance().updateMod(mod);
      } catch (SQLException e) {
        log.error("", e);
        MessageDialogue dialogue =
            new MessageDialogue(
                localizer.getMessage("mod.dbconnectionerror"),
                localizer.getMessage("mod.dbconnectionerror.title"),
                MessageType.ERROR,
                new LocalizerFactory());
        dialogue.getResult();
        return new HashSet<Mod>();
      }

      mods.add(mod);
    }

    return mods;
  }
Example #2
0
  private static void processArchive(
      final Path path, final Set<Archive> output, final SettingsModelFactory settingsFactory)
      throws IOException, StarDBException {

    Archive originalArchive = new Archive(path);

    if (!originalArchive.extract()) {
      throw new IOException("Could not extract archive.");
    }

    Set<ArchiveFile> usedPaks = new HashSet<>();

    for (ArchiveFile file : originalArchive.getFiles()) {

      if (FileHelper.getExtension(file.getPath()).equals("modinfo")) {

        JsonObject o = JsonObject.readFrom(new String(file.getData()));

        String preformattedPath = o.get("path").asString().replaceAll("\"", "");

        if (preformattedPath.startsWith("./")) {
          preformattedPath = preformattedPath.replace("./", "");
        }

        if (preformattedPath.startsWith(".")) {
          preformattedPath = preformattedPath.replace(".", "");
        }

        if (preformattedPath.startsWith("/")) {
          preformattedPath = preformattedPath.replace("/", "");
        }

        Path modinfoPath = file.getPath();

        if (modinfoPath.getNameCount() > 2) {
          modinfoPath = modinfoPath.subpath(0, modinfoPath.getNameCount() - 1);
        } else if (modinfoPath.getNameCount() > 1) {
          modinfoPath = modinfoPath.getName(0);
        } else {
          modinfoPath = Paths.get("");
        }

        Archive outputArchive =
            new Archive(
                settingsFactory
                    .getInstance()
                    .getPropertyPath("modsdir")
                    .resolve(Paths.get(o.get("name").asString() + ".zip")));

        if (file.getPath().getNameCount() == 1) {
          outputArchive.addFile(new ArchiveFile(file.getData(), file.getPath(), false));
          log.debug(
              "'{}' -> '{}' relativized to '{}'", modinfoPath, file.getPath(), file.getPath());
        } else {
          outputArchive.addFile(
              new ArchiveFile(
                  file.getData(), modinfoPath.relativize(file.getPath()).normalize(), false));
          log.debug(
              "'{}' -> '{}' relativized to '{}'",
              modinfoPath,
              file.getPath(),
              modinfoPath.relativize(file.getPath()).normalize());
        }

        Path assetsPath = modinfoPath.resolve(Paths.get(preformattedPath));

        log.debug("Assets path for modinfo '{}': {}", file.getPath(), assetsPath);

        if (originalArchive.getFile(assetsPath) != null
            && !assetsPath.toString().isEmpty()
            && !originalArchive.getFile(assetsPath).isFolder()) {

          if (FileHelper.identifyType(originalArchive.getFile(assetsPath).getData())
              .equals("pak")) {

            log.debug("Assets for mod '{}' identified as .pak file: {}", modinfoPath, assetsPath);

            usedPaks.add(originalArchive.getFile(assetsPath));

            ArchiveFile modinfo = outputArchive.getFile(".modinfo");
            JsonObject o2 = JsonObject.readFrom(new String(modinfo.getData()));
            o2.set("path", "assets");

            modinfo.setData(o2.toString().getBytes());

            // TODO Update StarDB to let me pass in a byte array instead of needing to open a file

            Path tempPath = Paths.get("tempPak" + System.nanoTime());

            SeekableByteChannel tempPakFile =
                Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
            tempPakFile.write(ByteBuffer.wrap(originalArchive.getFile(assetsPath).getData()));
            tempPakFile.close();

            AssetDatabase database = AssetDatabase.open(tempPath);

            for (String assetFile : database.getFileList()) {
              outputArchive.addFile(
                  new ArchiveFile(
                      database.getAsset(assetFile), Paths.get("assets/" + assetFile), false));
              log.trace("Asset extracted: {} | {}", assetFile, Paths.get("assets/" + assetFile));
            }

            Files.deleteIfExists(tempPath);
          }

        } else {

          ArchiveFile modinfo = outputArchive.getFile(".modinfo");
          JsonObject o2 = JsonObject.readFrom(new String(modinfo.getData()));
          o2.set("path", "assets");

          modinfo.setData(o2.toString().getBytes());

          log.debug("Assets for mod '{}' is a standard assets folder: {}", modinfoPath, assetsPath);

          for (ArchiveFile f2 : originalArchive.getFiles()) {

            if ((assetsPath.toString().isEmpty() || f2.getPath().startsWith(assetsPath))
                && !f2.isFolder()
                && !f2.getPath().toString().endsWith(".modinfo")) {

              if (modinfoPath.toString().isEmpty()) {

                if (f2.getPath().getNameCount() == 1) {

                  if (!f2.getPath().startsWith(assetsPath)) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(),
                            Paths.get("assets/").resolve(f2.getPath()).normalize(),
                            false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/").resolve(f2.getPath()).normalize());
                  } else {
                    outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        f2.getPath());
                  }

                } else {

                  if (!f2.getPath().startsWith(assetsPath)) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(),
                            Paths.get("assets/").resolve(f2.getPath()).normalize(),
                            false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/").resolve(f2.getPath()).normalize());
                  } else {
                    outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        f2.getPath());
                  }
                }

              } else {

                if (f2.getPath().getNameCount() == 1) {

                  if (!modinfoPath.relativize(f2.getPath()).startsWith("assets")) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(), Paths.get("assets/").resolve(f2.getPath()), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/").resolve(f2.getPath()));
                  } else {
                    outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        f2.getPath());
                  }

                } else {

                  if (!modinfoPath.relativize(f2.getPath()).startsWith("assets")) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(),
                            Paths.get("assets/")
                                .resolve(modinfoPath.relativize(f2.getPath()).normalize()),
                            false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/")
                            .resolve(modinfoPath.relativize(f2.getPath()).normalize()));
                  } else {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(), modinfoPath.relativize(f2.getPath()).normalize(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        modinfoPath.relativize(f2.getPath()).normalize());
                  }
                }
              }
            }
          }
        }

        outputArchive.writeToFile(
            settingsFactory
                .getInstance()
                .getPropertyPath("modsdir")
                .resolve(Paths.get(o.get("name").asString() + ".zip"))
                .toFile()); // TODO

        output.add(outputArchive);
      }
    }

    for (ArchiveFile file : originalArchive.getFiles()) {

      if (!usedPaks.contains(file)
          && !file.isFolder()
          && FileHelper.identifyType(file.getData()).equals("pak")) {

        log.debug("Additional .pak identified: {}", file.getPath());

        Path tempPath = Paths.get("tempPak" + System.nanoTime());

        SeekableByteChannel tempPakFile =
            Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        tempPakFile.write(ByteBuffer.wrap(file.getData()));
        tempPakFile.close();

        AssetDatabase database = AssetDatabase.open(tempPath);
        log.debug(database.getFileList());

        if (database.getAsset("/pak.modinfo") != null) {
          log.debug("{} has a .modinfo file, parsing into mod.", file.getPath());
          processPakFile(tempPath, output, settingsFactory);
        } else {
          log.debug("{} does not contain a .modinfo file, skipping.", file.getPath());
        }

        Files.deleteIfExists(tempPath);
      }
    }
  }