Advertisement
SoKnight

Untitled

May 10th, 2023
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1.     public List<MissingLibraryArtifact> filterMissingLibraries(
  2.             Iterator<? extends Library> librariesIterator,
  3.             String nativesKey,
  4.             boolean autoRetrieveHash,
  5.             boolean autoRetrieveSize,
  6.             int asyncPoolSize
  7.     ) {
  8.         List<MissingLibraryArtifact> missingLibraries = new ArrayList<>();
  9.         List<Library> legacyLibraries = new ArrayList<>();
  10.  
  11.         while (librariesIterator.hasNext()) {
  12.             Library library = librariesIterator.next();
  13.             if (library.isLegacyLibrary()) {
  14.                 legacyLibraries.add(library);
  15.             } else {
  16.                 String name = library.getName();
  17.                 checkMissingLibraryArtifact(missingLibraries::add, name, library.getArtifact());
  18.                 checkMissingLibraryArtifact(missingLibraries::add, name, library.getNativesFor(nativesKey));
  19.             }
  20.         }
  21.  
  22.         if (!legacyLibraries.isEmpty()) {
  23.             ExecutorService asyncPool = Executors.newFixedThreadPool(asyncPoolSize);
  24.             List<LegacyLibraryFutureData> futureData = new ArrayList<>();
  25.  
  26.             // retrieve hashes and sizes from the remote sources
  27.             for (Library library : legacyLibraries) {
  28.                 String downloadUrl = library.constructDownloadUrl();
  29.                 Path libraryPath = getLibraryPath(library.constructPathFromCoordinates());
  30.  
  31.                 Supplier<String> hashRetriever = null;
  32.                 LongSupplier sizeRetriever = null;
  33.  
  34.                 if (library instanceof ForgeLibrary forgeLibrary && forgeLibrary.hasHashSHA1()) {
  35.                     hashRetriever = forgeLibrary::getHashSHA1;
  36.                 }
  37.  
  38.                 if (autoRetrieveHash && hashRetriever == null && Files.isRegularFile(libraryPath)) {
  39.                     String hashsumUrl = downloadUrl + ".sha1";
  40.                     CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> retrieveHash(downloadUrl), asyncPool);
  41.                     hashRetriever = future::join;
  42.                 }
  43.  
  44.                 if (autoRetrieveSize) {
  45.                     CompletableFuture<Long> future = CompletableFuture.supplyAsync(() -> retrieveSize(downloadUrl), asyncPool);
  46.                     sizeRetriever = future::join;
  47.                 }
  48.  
  49.                 futureData.add(new LegacyLibraryFutureData(library, hashRetriever, sizeRetriever));
  50.             }
  51.  
  52.             // check libraries for missing
  53.             for (LegacyLibraryFutureData data : futureData) {
  54.                 Library library = data.library();
  55.                 String name = library.getName();
  56.                 String hash = data.hash();
  57.  
  58.                 String downloadUrl = library.constructDownloadUrl();
  59.                 Path libraryPath = getLibraryPath(library.constructPathFromCoordinates());
  60.  
  61.                 byte cause = isMissingLibrary(libraryPath, hash);
  62.                 if (cause != 0) {
  63.                     long size = data.size();
  64.                     missingLibraries.add(new MissingLibraryArtifact(name, downloadUrl, libraryPath, size, hash, cause));
  65.                 }
  66.             }
  67.         }
  68.  
  69.         return missingLibraries;
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement