diff --git a/ModsListDownloader/src/downloader/CurseUpdateManager.java b/ModsListDownloader/src/downloader/CurseUpdateManager.java index 07f19db..a243972 100644 --- a/ModsListDownloader/src/downloader/CurseUpdateManager.java +++ b/ModsListDownloader/src/downloader/CurseUpdateManager.java @@ -6,14 +6,13 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -import java.util.SortedMap; -import java.util.TreeMap; +import java.util.concurrent.ConcurrentHashMap; import downloader.helper.ArchiveHelper; import downloader.helper.SlugHelper; public class CurseUpdateManager { - private SortedMap installedMods; + private ConcurrentHashMap installedMods; public CurseUpdateManager() { @@ -27,18 +26,18 @@ public class CurseUpdateManager { { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(modsMapFile)); - installedMods = (SortedMap) ois.readObject(); + installedMods = (ConcurrentHashMap) ois.readObject(); ois.close(); } catch (Exception e) { modsMapFile.delete(); } - if(installedMods == null)installedMods = new TreeMap<>(); } + if(installedMods == null)installedMods = new ConcurrentHashMap<>(); this.updateFile(); } public boolean checkAndDelete(File modToDownload, int modid) { - String alredyInstalledName = installedMods.get(modid).toLowerCase(); + String alredyInstalledName = installedMods.get(modid).toLowerCase(); File alredyInstalledMods= null; //on cherche le fichier local affin de ne pas voir d'erreur avec la casse @@ -61,7 +60,7 @@ public class CurseUpdateManager { if (alredyInstalledMods.exists()) { //we only check if the jar file still works - if(ArchiveHelper.checkJarIntegrity(alredyInstalledMods)) { + if(!ArchiveHelper.checkJarIntegrity(alredyInstalledMods)) { alredyInstalledMods.delete(); installedMods.remove(modid); updateFile(); @@ -110,18 +109,17 @@ public class CurseUpdateManager { * @param fileName * @param modID */ - public void addModsToTheList(String fileName,int modID) + public synchronized void addModsToTheList(String fileName,int modID) { installedMods.put(modID, fileName); - updateFile(); } - public boolean isModIdKnown(int modId) + public synchronized boolean isModIdKnown(int modId) { return installedMods.get(modId) != null; } - private void updateFile() + public synchronized void updateFile() { File modsMap = new File("mods/modsMap.sav"); try { diff --git a/ModsListDownloader/src/downloader/DatabaseVersionManager.java b/ModsListDownloader/src/downloader/DatabaseVersionManager.java index 1bd627d..f821752 100644 --- a/ModsListDownloader/src/downloader/DatabaseVersionManager.java +++ b/ModsListDownloader/src/downloader/DatabaseVersionManager.java @@ -4,7 +4,9 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; +import java.io.PrintWriter; import java.net.MalformedURLException; import downloader.helper.ArchiveHelper; @@ -29,11 +31,22 @@ public class DatabaseVersionManager { if( version == null || !latestVersion.equals(version) ) { Log.i(ME, "new dbVersion avaliable downloading..."); downloadDatabase(latestVersion); + saveDbVersion(latestVersion); } else { Log.i(ME, "update to Date"); } } + private void saveDbVersion(String latestVersion) { + File versionFile = new File("dbVersion"); + PrintWriter out; + try { + out = new PrintWriter(new FileWriter(versionFile)); + out.println(latestVersion); + out.close(); + } catch (IOException e) {} + } + private void downloadDatabase(String dbVersion) { Log.i(ME, "fetching=" + "http://files.mcdex.net/data/mcdex-v5-" + dbVersion + ".dat.bz2"); File archive; @@ -81,8 +94,7 @@ public class DatabaseVersionManager { versionFile.delete(); return null; } - } else { - throw new FileNotFoundException(); } + throw new FileNotFoundException(); } } diff --git a/ModsListDownloader/src/downloader/Log.java b/ModsListDownloader/src/downloader/Log.java index 752829a..37ca65f 100644 --- a/ModsListDownloader/src/downloader/Log.java +++ b/ModsListDownloader/src/downloader/Log.java @@ -4,7 +4,7 @@ public class Log { private Log() {} public static void e(String who, String msg) { - System.err.println(String.valueOf(who) + " : " + msg); + System.out.println(String.valueOf(who) + " : " + msg); } public static void i(String who, String msg) { diff --git a/ModsListDownloader/src/downloader/ModUpdater.java b/ModsListDownloader/src/downloader/ModUpdater.java index 0a31951..2fa82ca 100644 --- a/ModsListDownloader/src/downloader/ModUpdater.java +++ b/ModsListDownloader/src/downloader/ModUpdater.java @@ -51,12 +51,19 @@ public class ModUpdater { } } in.close(); - Log.i(ME, "finished"); - } catch( IOException e ) { e.printStackTrace(); System.exit(1); } + + //on attend la fin des threads + while( !threads.isEmpty() ) { + try { + Thread.sleep(10); + } catch (InterruptedException e) {} + } + Log.i(ME, "finished"); + curseUpdateManager.updateFile(); } public static boolean isAComment(String line) @@ -143,6 +150,9 @@ final class UpdaterThread extends Thread { else { Log.i("DB", "NEW MOD DETECTED " + modID); upToDate = this.curseUpdateManager.checkAndDelete(new File("mods/" + HttpHelper.getFileNameFromURL(downloadUrl)), pj.getSlug()); + if(upToDate) { + this.curseUpdateManager.addModsToTheList("mods/" + HttpHelper.getFileNameFromURL(downloadUrl), modID); + } } if (!upToDate) { diff --git a/ModsListDownloader/src/downloader/helper/HttpHelper.java b/ModsListDownloader/src/downloader/helper/HttpHelper.java index 265c761..bbfa079 100644 --- a/ModsListDownloader/src/downloader/helper/HttpHelper.java +++ b/ModsListDownloader/src/downloader/helper/HttpHelper.java @@ -79,6 +79,7 @@ public class HttpHelper { return readFileFromUrl(urlToFetch, new File((folder + name.toLowerCase()).trim())); } + //TODO: debug private static File readFileFromUrl(String urlToFetch, File destination) throws MalformedURLException { URL url = new URL(urlToFetch); HttpURLConnection con;