4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-11-17 23:41:55 +01:00

misc improvements

This commit is contained in:
LordMZTE 2020-07-18 14:48:58 +02:00
parent 960fc91265
commit 30e8201d92
3 changed files with 17 additions and 26 deletions

View file

@ -14,7 +14,6 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -25,6 +24,7 @@ public class DownloadMods implements ICommand {
public CommandReturn execute(String[] args) {
if(args.length < 2)
return CommandReturn.fail("Invalid args");
Function<String, URL> toURL = spec -> {
try {
return new URL(spec);
@ -42,15 +42,16 @@ public class DownloadMods implements ICommand {
)),
Main.CONFIG.CONFIG.getPath(Long.class, "Downloads/maxThreads").intValue(),
r -> {
System.out.println(r.getResponseCode() + " " + r.getResponseMessage() + " " + r.getUrl() + " " + r.getFile());
if(r.getException() != null)
System.out.println(r.getException().getMessage());
//Synced to prevent the exception being printed too late
synchronized(this) {
System.out.println(r.getResponseCode() + " " + r.getResponseMessage() + " " + r.getUrl() + " " + r.getFile());
if(r.getException() != null)
System.out.println(r.getException().getMessage());
}
},
1,
2,
TimeUnit.MINUTES,
5,
Main.CONFIG.CONFIG.getPath(Long.class, "Downloads/httpTimeout").intValue(),
Arrays.stream(args).anyMatch("force"::equals) ? FileDownloader.AsyncDownloader.ExistingFileBehaviour.OVERWRITE : FileDownloader.AsyncDownloader.ExistingFileBehaviour.SKIP
Arrays.asList(args).contains("force") ? FileDownloader.AsyncDownloader.ExistingFileBehaviour.OVERWRITE : FileDownloader.AsyncDownloader.ExistingFileBehaviour.SKIP
);
return CommandReturn.success();
}
@ -69,6 +70,6 @@ public class DownloadMods implements ICommand {
@Nonnull
@Override
public String getHelpMessage() {
return "Downloads all mods. force always downloads files even if they are already presentSyntax: <OutDir> [force]";
return "Downloads all mods. force always downloads files even if they are already present Syntax: <OutDir> [force]";
}
}

View file

@ -1,6 +1,10 @@
package ley.anvil.modpacktools.util;
import java.io.*;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
//TODO add config integrity check to make sure each key is present (and maybe of correct type?)
@ -56,18 +60,14 @@ public class Config {
}
/**
* Copes the Config file from the resources into the tool's folder
* Copies the Config file from the resources into the tool's folder
*/
public void copyConfig() {
//copy from resources
try {
InputStream in = ClassLoader.getSystemResourceAsStream(CONFIG_NAME);
byte[] buff = new byte[in.available()];
in.read(buff);
OutputStream out = new FileOutputStream(CONFIG_LOCATION);
out.write(buff);
FileUtils.copyInputStreamToFile(in, CONFIG_LOCATION);
in.close();
out.close();
CONFIG = readConfig();
}catch(IOException e) {
e.printStackTrace();

View file

@ -27,23 +27,17 @@ public class FileDownloader {
* @param nThreads how many worker threads to use
* @param callback this callback will get the return code of each HTTP request and the file once a file has finished
* @param tries how many times a download will be tried in case it fails
* @param threadTimeout the time after which the download will be canceled
* @param threadTimeoutUnit the unit of the threadTimeout time
*/
public static void downloadAsync(Map<URL, File> files,
int nThreads,
Consumer<AsyncDownloader.DownloadFileTask.Return> callback,
int tries,
long threadTimeout,
TimeUnit threadTimeoutUnit,
int httpTimeout,
AsyncDownloader.ExistingFileBehaviour existingFileBehaviour) {
new AsyncDownloader(files,
nThreads,
callback,
tries,
threadTimeout,
threadTimeoutUnit,
httpTimeout,
existingFileBehaviour);
}
@ -62,15 +56,11 @@ public class FileDownloader {
int nThreads,
Consumer<DownloadFileTask.Return> callback,
int tries,
long threadTimeout,
TimeUnit threadTimeoutUnit,
int httpTimeout,
ExistingFileBehaviour existingFileBehaviour) {
this.files = files;
this.callback = callback;
this.tries = tries;
this.threadTimeout = threadTimeout;
this.threadTimeoutUnit = threadTimeoutUnit;
this.service = Executors.newFixedThreadPool(nThreads);
this.httpTimeout = httpTimeout;
this.existingFileBehaviour = existingFileBehaviour;