10 Commits

Author SHA1 Message Date
marc barbier 379cfed69a bandwidth precision improvement 2021-07-10 17:49:12 +02:00
marc barbier f100e2d41b add version option 2021-07-10 17:11:33 +02:00
marc barbier a2a136a9fc refacto main 2021-07-10 16:59:44 +02:00
marc barbier 416f0b892e thread warning + better logging 2021-07-10 16:57:53 +02:00
marc barbier f726458f29 removed check 2021-07-10 16:53:47 +02:00
marc barbier 6799681c6b refactoring ModUpdater 2021-07-10 16:52:06 +02:00
marc barbier 155f7ada2e exception non blockante dans les threads 2021-07-09 23:28:33 +02:00
marc barbier c8c23cbe42 safer parse - direct download 2021-07-09 23:27:23 +02:00
marc barbier a93e180c60 safer parse 2021-07-09 23:26:09 +02:00
marc barbier b0c1519063 nb thread fix + corruption check 2021-07-09 19:26:04 +02:00
5 changed files with 146 additions and 63 deletions
+2 -2
View File
@@ -4,8 +4,8 @@ then
fi fi
-rm release-${VER}.zip 2> /dev/null -rm release-${VER}.zip 2> /dev/null
mv mcDownloader.jar ModPackDl.jar mv mcDownloader.jar ModPackDl.jar
echo "java -jar ModPackDl.jar -t -v" > run.sh echo "java -jar ModPackDl.jar --thread 2 -v" > run.sh
echo "java -jar ModPackDl.jar -t -v" > run.bat echo "java -jar ModPackDl.jar --thread 2 -v" > run.bat
echo "pause" >> run.bat echo "pause" >> run.bat
zip release-${VER}.zip ModPackDl.jar run.sh run.bat zip release-${VER}.zip ModPackDl.jar run.sh run.bat
rm run.sh rm run.sh
+8 -4
View File
@@ -8,23 +8,27 @@ public class Log {
private static PrintStream lastStream = System.out; private static PrintStream lastStream = System.out;
public static void e(String who, String msg) { 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) { 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) { private static void printNonStatic(String line, PrintStream stream) {
//erase last stream since we used \r //erase last stream since we used \r
lastStream.print(""); lastStream.print("");
stream.println(line); stream.println(line);
stream.print(staticLine + "\r"); stream.print(staticLine + " \r");
lastStream = stream; lastStream = stream;
} }
public static void setStaticPrint(String staticString) { public static void setStaticPrint(String staticString) {
staticLine = staticString; staticLine = staticString;
System.out.print(staticString + "\r"); System.out.print(staticString + " \r");
} }
} }
+44 -19
View File
@@ -10,18 +10,25 @@ import java.util.Iterator;
public class Main { public class Main {
public static boolean verbose; public static boolean verbose;
public static boolean checkingonly;
public static Integer threadNb; public static Integer threadNb;
public static String mcVersion; public static String mcVersion;
public static void main(String[] args) { public static void main(String[] args) {
if(mcVersion == null) {
mcVersion = "1.12.2"; mcVersion = "1.12.2";
}
else {
if(!isVersionValid()) {
Log.e("main", "the version number provided is invalid");
System.exit(1);
}
}
checkModsDirectory(); checkModsDirectory();
new DatabaseVersionManager().updateIfNeeded(); new DatabaseVersionManager().updateIfNeeded();
interpretArgs(args); interpretArgs(args);
//no threading option called => only one thread will be used //no threading option called => only one thread will be used
if( threadNb == null)threadNb = 1; if( threadNb == null)threadNb = 1;
Log.i("Main", "Running on " + threadNb + " thread");
new ModUpdater().update(); new ModUpdater().update();
} }
@@ -44,55 +51,73 @@ public class Main {
while (it.hasNext()) { while (it.hasNext()) {
String cmd = it.next(); String cmd = it.next();
switch (cmd) { switch (cmd) {
case "--check":
System.out.println("--check is not avaliable");
checkingonly = true;
continue;
case "-t": 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) { if (threadNb != null) {
Log.e("main", "error conflicting arguments --thread and -t"); Log.e("main", "error conflicting arguments --thread and -t");
System.exit(1); System.exit(1);
} }
if (cores <= 0) { if (cores <= 0) {
Log.i("main","java think you have no cpu core... sooo lets go for 4"); Log.i("main","java think you have no cpu core... sooo lets go for 2");
threadNb = Integer.valueOf(4); threadNb = Integer.valueOf(2);
continue; break;
} }
Log.i("main", "running on " + cores + " thread"); Log.i("main", "running on " + cores + " thread");
threadNb = Integer.valueOf(cores); threadNb = Integer.valueOf(cores);
continue; break;
case "-v": case "-v":
verbose = true; verbose = true;
continue; break;
case "-version":
checkNext(it);
mcVersion = it.next();
break;
case "--thread": case "--thread":
if (threadNb != null) { if (threadNb != null) {
Log.e("main", "error conflicting arguments --thread and -t"); Log.e("main", "error conflicting arguments --thread and -t");
System.exit(1); System.exit(1);
} }
if (!it.hasNext()) { checkNext(it);
Log.e("main", "arguments invalid please refer to --help");
System.exit(1);
}
String threadNumber = it.next(); String threadNumber = it.next();
if (threadNumber.matches("[0-9]*")) { if (threadNumber.matches("[0-9]*")) {
threadNb = Integer.valueOf(Integer.parseInt(threadNumber)); threadNb = Integer.valueOf(Integer.parseInt(threadNumber));
continue; break;
} }
Log.e("main","arguments invalid please refer to --help"); Log.e("main","arguments invalid please refer to --help");
System.exit(1); System.exit(1);
continue; break;
case "--help": 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); System.exit(0);
continue; break;
default: default:
Log.e("main", "unknown args " + cmd + "\nsee --help for help"); Log.e("main", "unknown args " + cmd + "\nsee --help for help");
System.exit(1); 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);
}
}
} }
@@ -9,13 +9,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import downloader.forgeSvc.ForgeSvcFile; import downloader.forgeSvc.ForgeSvcFile;
import downloader.helper.ArchiveHelper;
import downloader.helper.HttpHelper; import downloader.helper.HttpHelper;
public class ModUpdater { public class ModUpdater {
private DirectUpdateManager directUpdateManager; private DirectUpdateManager directUpdateManager;
private CurseUpdateManager curseUpdateManager; private CurseUpdateManager curseUpdateManager;
private Database db; private Database db;
private final String ME = "ModUpdater"; private static final String ME = "ModUpdater";
public ModUpdater() { public ModUpdater() {
this.db = new Database(); this.db = new Database();
@@ -32,6 +33,14 @@ public class ModUpdater {
} }
//parse and download //parse and download
startProcessingThreads(modsList);
checkForFailures();
Log.i(ME, "finished");
curseUpdateManager.updateFile();
}
private void startProcessingThreads(File modsList) {
List<Thread> threads = new ArrayList<>(Main.threadNb); List<Thread> threads = new ArrayList<>(Main.threadNb);
try { try {
BufferedReader in = new BufferedReader(new FileReader(modsList)); BufferedReader in = new BufferedReader(new FileReader(modsList));
@@ -43,10 +52,8 @@ public class ModUpdater {
threads.add(t); threads.add(t);
} }
while(Main.threadNb == threads.size()) { while(Main.threadNb >= threads.size()) {
try { threadSleep();
Thread.sleep(10);
} catch (InterruptedException e) {}
} }
} }
in.close(); in.close();
@@ -54,15 +61,45 @@ public class ModUpdater {
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
} }
waitThreadsEnd(threads);
}
//on attend la fin des threads private void checkForFailures() {
while( !threads.isEmpty() ) { 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, null);
t.start();
try { try {
Thread.sleep(100); t.join();
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
} }
Log.i(ME, "finished");
curseUpdateManager.updateFile(); if(! UpdaterThread.failedLines.isEmpty()) {
Log.e(ME, "could not download the following mods : ");
for(String s: failures) {
Log.e(ME, " " + s);
}
}
}
}
private void waitThreadsEnd(List<Thread> threads) {
while( !threads.isEmpty() ) {
threadSleep();
}
}
private void threadSleep() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
} }
public static boolean isAComment(String line) public static boolean isAComment(String line)
@@ -79,6 +116,7 @@ final class UpdaterThread extends Thread {
private final CurseUpdateManager curseUpdateManager; private final CurseUpdateManager curseUpdateManager;
private final List<Thread> threads; private final List<Thread> threads;
private static final String ME = "ModUpdaterThread"; private static final String ME = "ModUpdaterThread";
protected static final List<String> failedLines = new ArrayList<>();
public UpdaterThread(String line, Database db, DirectUpdateManager directUpdateManager, CurseUpdateManager curseUpdateManager, List<Thread> threads) { public UpdaterThread(String line, Database db, DirectUpdateManager directUpdateManager, CurseUpdateManager curseUpdateManager, List<Thread> threads) {
super(line); super(line);
@@ -91,9 +129,12 @@ final class UpdaterThread extends Thread {
@Override @Override
public void run() { public void run() {
if(line.startsWith("direct=")) try {
//used for post download checks
File downloadedFile = null;
if(line.startsWith("direct=") && line.contains(";") && line.contains("@"))
{ {
handleDirectDownload(line); downloadedFile = handleDirectDownload(line);
} else if (line.startsWith("del=")) { } else if (line.startsWith("del=")) {
String[] delete = line.split("del="); String[] delete = line.split("del=");
if(new File("mods/" + delete[1]).delete()) { if(new File("mods/" + delete[1]).delete()) {
@@ -101,16 +142,26 @@ final class UpdaterThread extends Thread {
} }
} }
else { else {
if (line.split("/").length >= 5) { if (line.split("/").length >= 5 && line.contains("curseforge")) {
handleCurseDownload(line); downloadedFile = handleCurseDownload(line);
} else {
Log.e(ME, "unrecognized line: " + line);
} }
} }
super.run();
//on se retire de la pool d'execution
this.threads.remove(this);
}
private void handleDirectDownload(String line) { if(downloadedFile != null && !ArchiveHelper.checkJarIntegrity(downloadedFile)) {
downloadedFile.delete();
failedLines.add(line);
}
} catch(Exception e) { e.printStackTrace(); }
super.run();
//on se retire de la pool d'execution
if(this.threads != null)this.threads.remove(this);
}
private File handleDirectDownload(String line) {
String url = line.replaceFirst("direct=.*@", ""); String url = line.replaceFirst("direct=.*@", "");
String filename=extractNameFromLink(line); String filename=extractNameFromLink(line);
@@ -122,15 +173,16 @@ final class UpdaterThread extends Thread {
{ {
Log.i(ME,"downloading "+ filename); Log.i(ME,"downloading "+ filename);
try { try {
HttpHelper.readFileFromUrlToFolder(url,"mods",filename);
Log.i(ME,"done"); Log.i(ME,"done");
return HttpHelper.readFileFromUrlToFolder(url,"mods",filename);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
Log.e(ME, "could not update mod, error while downloading"); Log.e(ME, "could not update mod, error while downloading");
} }
} }
return null;
} }
private void handleCurseDownload(String line) { private File handleCurseDownload(String line) {
String name = line.split("/")[5]; String name = line.split("/")[5];
int modID = db.findModBySlug(name); int modID = db.findModBySlug(name);
ProjectInfo pj = db.getProjectInfo(modID); ProjectInfo pj = db.getProjectInfo(modID);
@@ -157,13 +209,14 @@ final class UpdaterThread extends Thread {
if (!upToDate) { if (!upToDate) {
Log.i("MOD", "downloading " + pj.getName().replace(" ", "-")); Log.i("MOD", "downloading " + pj.getName().replace(" ", "-"));
HttpHelper.readFileFromUrlToFolder(downloadUrl, "mods"); File downloaded = HttpHelper.readFileFromUrlToFolder(downloadUrl, "mods");
Log.i("MOD", "download finished"); Log.i("MOD", "download finished");
if (!this.curseUpdateManager.isModIdKnown(modID)) { if (!this.curseUpdateManager.isModIdKnown(modID)) {
this.curseUpdateManager.addModsToTheList(HttpHelper.getFileNameFromURL(downloadUrl), modID); this.curseUpdateManager.addModsToTheList(HttpHelper.getFileNameFromURL(downloadUrl), modID);
Log.i("DB", "added a mod to the registry"); Log.i("DB", "added a mod to the registry");
} }
return downloaded;
} }
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
@@ -171,6 +224,7 @@ final class UpdaterThread extends Thread {
} else { } else {
Log.e("DB", "no file in server"); Log.e("DB", "no file in server");
} }
return null;
} }
private static String extractNameFromLink(String line) { private static String extractNameFromLink(String line) {
@@ -13,6 +13,7 @@ import java.nio.file.Paths;
import java.util.Locale; import java.util.Locale;
import downloader.Log; import downloader.Log;
import downloader.Main;
public class HttpHelper { public class HttpHelper {
private HttpHelper() {} private HttpHelper() {}
@@ -121,15 +122,14 @@ public class HttpHelper {
int bytesRead = in.read(buffer); int bytesRead = in.read(buffer);
if (bytesRead < 0) if (bytesRead < 0)
break; break;
{
double elapsed = ( System.currentTimeMillis() - start ) / 1000; double elapsed = ( System.currentTimeMillis() - start ) / 1000;
if(elapsed != 0) { if(elapsed != 0) {
long estimatedBandwith = (long) (bytesRead / elapsed); long estimatedBandwith = (long) (bytesRead / elapsed);
//display the estimate //display the estimate
Log.setStaticPrint(formatBandwidth(estimatedBandwith)); Log.setStaticPrint("estimated bandwidth :" + formatBandwidth(estimatedBandwith * Main.threadNb));
start = System.currentTimeMillis(); start = System.currentTimeMillis();
} }
}
downloadingWriter.write(buffer, 0, bytesRead); downloadingWriter.write(buffer, 0, bytesRead);
} }
downloadingWriter.close(); downloadingWriter.close();