Skip to content

Commit

Permalink
feat: async library downloading
Browse files Browse the repository at this point in the history
xIGBClutchIx committed Nov 4, 2020
1 parent 977351c commit e5c296f
Showing 2 changed files with 49 additions and 27 deletions.
66 changes: 42 additions & 24 deletions src/main/java/systems/conduit/stream/LibraryProcessor.java
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class LibraryProcessor {

@@ -21,35 +22,52 @@ public static void resetArtifacts() {
public static void downloadLibrary(String type, Path basePath, List<JsonLibraryInfo> libraries, Callback<File> callback) {
Logger.info("Loading " + type);
List<String> loadedLibrariesIds = new ArrayList<>();
List<File> loadedLibrariesFiles = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(libraries.size());
for (JsonLibraryInfo library : libraries) {
if (loadedArtifacts.contains(library.getGroupId() + ":" + library.getArtifactId())) continue;
File libraryPath;
if (basePath != null) {
libraryPath = new File(basePath.toFile() + File.separator + Constants.LIBRARIES_PATH.toFile() + File.separator + getPath(library));
} else {
libraryPath = new File(Constants.LIBRARIES_PATH.toFile() + File.separator + getPath(library));
if (loadedArtifacts.contains(library.getGroupId() + ":" + library.getArtifactId())) {
latch.countDown();
continue;
}
try {
Files.createDirectories(libraryPath.toPath());
File jar = new File(libraryPath, getFileName(library));
if (!jar.exists() && library.getType() != null) {
Logger.info("Downloading " + type + ": " + library.getArtifactId());
if (library.getType().trim().equalsIgnoreCase("maven")) {
SharedLaunch.downloadFile(getUrl(library), jar);
} else if (!library.getType().trim().equalsIgnoreCase("minecraft")) {
SharedLaunch.downloadFile(new URL(library.getUrl()), jar);
loadedArtifacts.add(library.getGroupId() + ":" + library.getArtifactId());
new Thread(() -> {
File libraryPath;
if (basePath != null) {
libraryPath = new File(basePath.toFile() + File.separator + Constants.LIBRARIES_PATH.toFile() + File.separator + getPath(library));
} else {
libraryPath = new File(Constants.LIBRARIES_PATH.toFile() + File.separator + getPath(library));
}
try {
Files.createDirectories(libraryPath.toPath());
File jar = new File(libraryPath, getFileName(library));
if (!jar.exists() && library.getType() != null) {
Logger.info("Downloading " + type + ": " + library.getArtifactId());
if (library.getType().trim().equalsIgnoreCase("maven")) {
SharedLaunch.downloadFile(getUrl(library), jar);
} else if (!library.getType().trim().equalsIgnoreCase("minecraft")) {
SharedLaunch.downloadFile(new URL(library.getUrl()), jar);
}
}
loadedLibrariesIds.add(library.getArtifactId());
loadedLibrariesFiles.add(jar);
} catch (Exception e) {
Logger.exception("Error loading " + type + ": " + library.getArtifactId(), e);
System.exit(0);
} finally {
latch.countDown();
}
loadedArtifacts.add(library.getGroupId() + ":" + library.getArtifactId());
loadedLibrariesIds.add(library.getArtifactId());
callback.callback(jar);
} catch (Exception e) {
Logger.exception("Error loading " + type + ": " + library.getArtifactId(), e);
System.exit(0);
}

}).start();
}
try {
latch.await();
} catch (InterruptedException e) {
Logger.fatal("Error loading libraries");
System.exit(0);
}
if (!loadedLibrariesIds.isEmpty()) {
loadedLibrariesFiles.forEach(callback::callback);
Logger.info("Loaded " + type + ": " + loadedLibrariesIds);
}
if (!loadedLibrariesIds.isEmpty()) Logger.info("Loaded " + type + ": " + loadedLibrariesIds);
}

private static URL getUrl(JsonLibraryInfo library) throws MalformedURLException {
10 changes: 7 additions & 3 deletions src/main/java/systems/conduit/stream/SharedLaunch.java
Original file line number Diff line number Diff line change
@@ -212,12 +212,16 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
}

public static void downloadFile(URL url, File location) throws IOException {
try (InputStream inputStream = getConnection(url)) {
Files.copy(inputStream, location.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}

private static InputStream getConnection(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent", Constants.USER_AGENT);
connection.setRequestMethod("GET");
connection.connect();
try (InputStream inputStream = connection.getInputStream()) {
Files.copy(inputStream, location.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
return connection.getInputStream();
}
}

0 comments on commit e5c296f

Please sign in to comment.