Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 379cfed69a | |||
| f100e2d41b | |||
| a2a136a9fc | |||
| 416f0b892e | |||
| f726458f29 | |||
| 6799681c6b | |||
| 155f7ada2e | |||
| c8c23cbe42 | |||
| a93e180c60 |
@@ -8,11 +8,15 @@ public class Log {
|
||||
private static PrintStream lastStream = System.out;
|
||||
|
||||
public static void e(String who, String msg) {
|
||||
printNonStatic(String.valueOf(who) + " : " + msg, System.err);
|
||||
printNonStatic("[ERROR]" + who + " : " + msg, System.err);
|
||||
}
|
||||
|
||||
public static void w(String who, String msg) {
|
||||
printNonStatic("[WARN]" + who + " : " + msg, System.out);
|
||||
}
|
||||
|
||||
public static void i(String who, String msg) {
|
||||
if(Main.verbose) printNonStatic(String.valueOf(who) + " : " + msg, System.out);
|
||||
if(Main.verbose) printNonStatic("[INFO]" + who + " : " + msg, System.out);
|
||||
}
|
||||
|
||||
private static void printNonStatic(String line, PrintStream stream) {
|
||||
|
||||
@@ -10,13 +10,19 @@ import java.util.Iterator;
|
||||
public class Main {
|
||||
|
||||
public static boolean verbose;
|
||||
public static boolean checkingonly;
|
||||
public static Integer threadNb;
|
||||
public static String mcVersion;
|
||||
|
||||
public static void main(String[] args) {
|
||||
if(mcVersion == null) {
|
||||
mcVersion = "1.12.2";
|
||||
|
||||
}
|
||||
else {
|
||||
if(!isVersionValid()) {
|
||||
Log.e("main", "the version number provided is invalid");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
checkModsDirectory();
|
||||
new DatabaseVersionManager().updateIfNeeded();
|
||||
interpretArgs(args);
|
||||
@@ -45,55 +51,73 @@ public class Main {
|
||||
while (it.hasNext()) {
|
||||
String cmd = it.next();
|
||||
switch (cmd) {
|
||||
case "--check":
|
||||
System.out.println("--check is not avaliable");
|
||||
checkingonly = true;
|
||||
continue;
|
||||
case "-t":
|
||||
Log.w("main", "Warning using -t option can lead to corruption during download if you have a lot of cores or a slow internet");
|
||||
|
||||
if (threadNb != null) {
|
||||
Log.e("main", "error conflicting arguments --thread and -t");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
if (cores <= 0) {
|
||||
Log.i("main","java think you have no cpu core... sooo lets go for 4");
|
||||
threadNb = Integer.valueOf(4);
|
||||
continue;
|
||||
Log.i("main","java think you have no cpu core... sooo lets go for 2");
|
||||
threadNb = Integer.valueOf(2);
|
||||
break;
|
||||
}
|
||||
Log.i("main", "running on " + cores + " thread");
|
||||
threadNb = Integer.valueOf(cores);
|
||||
continue;
|
||||
break;
|
||||
|
||||
case "-v":
|
||||
verbose = true;
|
||||
continue;
|
||||
break;
|
||||
|
||||
case "-version":
|
||||
checkNext(it);
|
||||
mcVersion = it.next();
|
||||
break;
|
||||
|
||||
case "--thread":
|
||||
if (threadNb != null) {
|
||||
Log.e("main", "error conflicting arguments --thread and -t");
|
||||
System.exit(1);
|
||||
}
|
||||
if (!it.hasNext()) {
|
||||
Log.e("main", "arguments invalid please refer to --help");
|
||||
System.exit(1);
|
||||
}
|
||||
checkNext(it);
|
||||
String threadNumber = it.next();
|
||||
if (threadNumber.matches("[0-9]*")) {
|
||||
threadNb = Integer.valueOf(Integer.parseInt(threadNumber));
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
Log.e("main","arguments invalid please refer to --help");
|
||||
System.exit(1);
|
||||
continue;
|
||||
break;
|
||||
|
||||
case "--help":
|
||||
Log.i("main", "use --check to check without updating\nuse --help to see this help\nuse -v to see verbose\nuse --threads to specify the number of threads\nuse -t to set the number of thread automaticaly");
|
||||
Log.i("main", "use --help to see this help\nuse -v to see verbose\nuse --threads to specify the number of threads\nuse -t to set the number of thread automaticaly");
|
||||
System.exit(0);
|
||||
continue;
|
||||
break;
|
||||
default:
|
||||
Log.e("main", "unknown args " + cmd + "\nsee --help for help");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isVersionValid() {
|
||||
switch(mcVersion.split(".").length) {
|
||||
case 2:
|
||||
return mcVersion.matches("[0-9]*\\.[0-9]*");
|
||||
case 3:
|
||||
return mcVersion.matches("[0-9]*\\.[0-9]*\\.[0-9]*");
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkNext(Iterator<String> it) {
|
||||
if (!it.hasNext()) {
|
||||
Log.e("main", "arguments invalid please refer to --help");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ public class ModUpdater {
|
||||
private DirectUpdateManager directUpdateManager;
|
||||
private CurseUpdateManager curseUpdateManager;
|
||||
private Database db;
|
||||
private final String ME = "ModUpdater";
|
||||
private static final String ME = "ModUpdater";
|
||||
|
||||
public ModUpdater() {
|
||||
this.db = new Database();
|
||||
@@ -33,6 +33,14 @@ public class ModUpdater {
|
||||
}
|
||||
|
||||
//parse and download
|
||||
startProcessingThreads(modsList);
|
||||
checkForFailures();
|
||||
|
||||
Log.i(ME, "finished");
|
||||
curseUpdateManager.updateFile();
|
||||
}
|
||||
|
||||
private void startProcessingThreads(File modsList) {
|
||||
List<Thread> threads = new ArrayList<>(Main.threadNb);
|
||||
try {
|
||||
BufferedReader in = new BufferedReader(new FileReader(modsList));
|
||||
@@ -44,10 +52,8 @@ public class ModUpdater {
|
||||
threads.add(t);
|
||||
}
|
||||
|
||||
while(Main.threadNb == threads.size()) {
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {}
|
||||
while(Main.threadNb >= threads.size()) {
|
||||
threadSleep();
|
||||
}
|
||||
}
|
||||
in.close();
|
||||
@@ -55,23 +61,20 @@ public class ModUpdater {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
//on attend la fin des threads
|
||||
while( !threads.isEmpty() ) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {}
|
||||
waitThreadsEnd(threads);
|
||||
}
|
||||
|
||||
private void checkForFailures() {
|
||||
if(! UpdaterThread.failedLines.isEmpty()) {
|
||||
List<String> failures = new ArrayList<>(UpdaterThread.failedLines);
|
||||
UpdaterThread.failedLines.clear();
|
||||
|
||||
|
||||
Log.e(ME, "filed to download " + failures.size());
|
||||
Log.e(ME, "Retrying but slower");
|
||||
|
||||
for(String line : failures) {
|
||||
Thread t = new UpdaterThread(line, db, directUpdateManager, curseUpdateManager, threads);
|
||||
Thread t = new UpdaterThread(line, db, directUpdateManager, curseUpdateManager, null);
|
||||
t.start();
|
||||
try {
|
||||
t.join();
|
||||
@@ -85,10 +88,18 @@ public class ModUpdater {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void waitThreadsEnd(List<Thread> threads) {
|
||||
while( !threads.isEmpty() ) {
|
||||
threadSleep();
|
||||
}
|
||||
}
|
||||
|
||||
Log.i(ME, "finished");
|
||||
curseUpdateManager.updateFile();
|
||||
private void threadSleep() {
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
|
||||
public static boolean isAComment(String line)
|
||||
@@ -105,7 +116,7 @@ final class UpdaterThread extends Thread {
|
||||
private final CurseUpdateManager curseUpdateManager;
|
||||
private final List<Thread> threads;
|
||||
private static final String ME = "ModUpdaterThread";
|
||||
public static final List<String> failedLines = new ArrayList<>();
|
||||
protected static final List<String> failedLines = new ArrayList<>();
|
||||
|
||||
public UpdaterThread(String line, Database db, DirectUpdateManager directUpdateManager, CurseUpdateManager curseUpdateManager, List<Thread> threads) {
|
||||
super(line);
|
||||
@@ -118,9 +129,10 @@ final class UpdaterThread extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
//used for post download checks
|
||||
File downloadedFile = null;
|
||||
if(line.startsWith("direct="))
|
||||
if(line.startsWith("direct=") && line.contains(";") && line.contains("@"))
|
||||
{
|
||||
downloadedFile = handleDirectDownload(line);
|
||||
} else if (line.startsWith("del=")) {
|
||||
@@ -130,8 +142,10 @@ final class UpdaterThread extends Thread {
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (line.split("/").length >= 5) {
|
||||
if (line.split("/").length >= 5 && line.contains("curseforge")) {
|
||||
downloadedFile = handleCurseDownload(line);
|
||||
} else {
|
||||
Log.e(ME, "unrecognized line: " + line);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,10 +153,12 @@ final class UpdaterThread extends Thread {
|
||||
downloadedFile.delete();
|
||||
failedLines.add(line);
|
||||
}
|
||||
} catch(Exception e) { e.printStackTrace(); }
|
||||
|
||||
|
||||
super.run();
|
||||
//on se retire de la pool d'execution
|
||||
this.threads.remove(this);
|
||||
if(this.threads != null)this.threads.remove(this);
|
||||
}
|
||||
|
||||
private File handleDirectDownload(String line) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.nio.file.Paths;
|
||||
import java.util.Locale;
|
||||
|
||||
import downloader.Log;
|
||||
import downloader.Main;
|
||||
|
||||
public class HttpHelper {
|
||||
private HttpHelper() {}
|
||||
@@ -126,7 +127,7 @@ public class HttpHelper {
|
||||
if(elapsed != 0) {
|
||||
long estimatedBandwith = (long) (bytesRead / elapsed);
|
||||
//display the estimate
|
||||
Log.setStaticPrint(formatBandwidth(estimatedBandwith));
|
||||
Log.setStaticPrint("estimated bandwidth :" + formatBandwidth(estimatedBandwith * Main.threadNb));
|
||||
start = System.currentTimeMillis();
|
||||
}
|
||||
downloadingWriter.write(buffer, 0, bytesRead);
|
||||
|
||||
Reference in New Issue
Block a user