From 3ff34f646b394d8ea98ccce272e83219f14bedce Mon Sep 17 00:00:00 2001 From: marc barbier Date: Thu, 14 Jan 2021 10:25:28 +0100 Subject: [PATCH] ajout checkadv + patch dl --- downloader/Database.java | 29 +++++++--- downloader/HttpHelper.java | 17 ++++-- downloader/IntegrityChecker.java | 92 ++++++++++++++++++++++++++++++-- downloader/Main.java | 29 ++++++---- 4 files changed, 140 insertions(+), 27 deletions(-) diff --git a/downloader/Database.java b/downloader/Database.java index 7ee87d6..f36d9cd 100644 --- a/downloader/Database.java +++ b/downloader/Database.java @@ -123,7 +123,6 @@ public class Database { try { if (rs.next()) { modID = rs.getInt("projectid"); - Log.i("DB", "modid:" + modID); } else { Log.e("DB", "not found " + slug); return -1; @@ -169,13 +168,27 @@ public class Database { try { String downloadUrl = file.getDownloadUrl(modID); - Log.i("DB", "checking " + pj.getName()); - integrityChecker.checkAndDelete(new File("mods/" + HttpHelper.getFileNameFromURL(downloadUrl)), - pj.getSlug()); - // on remplace les espace par des tiret pour eviter toute confusion - Log.i("DB", "downloading " + pj.getName().replace(" ", "-")); - HttpHelper.readFileFromUrlToFolder(downloadUrl, "mods"); - Log.i("DB", "download finished"); + Log.i("MOD", "checking " + pj.getName().replace(" ", "-")); + // si on connais le mod alors on se base si son fichier sinon on essay de le + // trouver + boolean upToDate; + if (integrityChecker.isModIdKnown(modID)) { + upToDate = integrityChecker + .checkAndDelete(new File("mods/" + HttpHelper.getFileNameFromURL(downloadUrl)), modID); + } else { + upToDate = integrityChecker.checkAndDelete( + new File("mods/" + HttpHelper.getFileNameFromURL(downloadUrl)), pj.getSlug()); + } + if (!upToDate) { + // on remplace les espace par des tiret pour eviter toute confusion + Log.i("MOD", "downloading " + pj.getName().replace(" ", "-")); + HttpHelper.readFileFromUrlToFolder(downloadUrl, "mods"); + Log.i("MOD", "download finished"); + if (!integrityChecker.isModIdKnown(modID)) { + integrityChecker.addModsToTheList(HttpHelper.getFileNameFromURL(downloadUrl), modID); + Log.i("DB", "added a mod to the registry"); + } + } return true; } catch (MalformedURLException e) { e.printStackTrace(); diff --git a/downloader/HttpHelper.java b/downloader/HttpHelper.java index f448b79..1f6bf63 100644 --- a/downloader/HttpHelper.java +++ b/downloader/HttpHelper.java @@ -58,6 +58,13 @@ public class HttpHelper { } public static File readFileFromUrlToFolder(String urlToFetch, String folder) throws MalformedURLException { + String name = getFileNameFromURL(urlToFetch); + if(name == null)throw new MalformedURLException(); + + return readFileFromUrlToFolder(urlToFetch, folder,name); + } + + public static File readFileFromUrlToFolder(String urlToFetch, String folder,String name) throws MalformedURLException { try { Files.createDirectories(Paths.get(folder)); } catch (IOException e) { @@ -68,10 +75,7 @@ public class HttpHelper { if (!folder.endsWith("/")) folder += "/"; - String name = getFileNameFromURL(urlToFetch); - if(name == null)throw new MalformedURLException(); - - return readFileFromUrl(urlToFetch, new File(folder + name)); + return readFileFromUrl(urlToFetch, new File((folder + name.toLowerCase()).trim())); } private static File readFileFromUrl(String urlToFetch, File destination) throws MalformedURLException { @@ -85,14 +89,17 @@ public class HttpHelper { } File downloadingFile = destination; + System.out.println(downloadingFile.getAbsolutePath()); if (downloadingFile.exists()) { return downloadingFile; } else { try { + System.out.println(downloadingFile); downloadingFile.createNewFile(); } catch (IOException e) { Log.e("HTTPHelper","permission error could not write file"); + e.printStackTrace(); System.exit(1); } } @@ -115,7 +122,7 @@ public class HttpHelper { if (bytesRead < 0) break; //totalRead += bytesRead; - downloadingWriter.write(buffer, 0, bytesRead); + downloadingWriter.write(buffer, 0, bytesRead); //TODO afficher cette valeur si on a un ui //System.out.println("\r" + totalRead); } diff --git a/downloader/IntegrityChecker.java b/downloader/IntegrityChecker.java index bbb4464..3ee004f 100644 --- a/downloader/IntegrityChecker.java +++ b/downloader/IntegrityChecker.java @@ -1,18 +1,77 @@ package downloader; import java.io.File; +import java.io.FileInputStream; +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.zip.ZipFile; public class IntegrityChecker { - public static String[] fileList; + public String[] fileList; + public SortedMap installedMods; + public File modsMap; + @SuppressWarnings("unchecked") public IntegrityChecker() { fileList = new File("mods").list(); + modsMap = new File("mods/modsMap.sav"); + if(modsMap.exists()) + { + try { + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(modsMap)); + installedMods = (SortedMap) ois.readObject(); + if(installedMods == null)installedMods = new TreeMap<>(); + ois.close(); + } catch (Exception e) { + installedMods = new TreeMap<>(); + modsMap.delete(); + } + } + try { + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(modsMap)); + oos.writeObject(installedMods); + oos.close(); + } catch (IOException e) { + Log.e("SAV", "erreur pas de sauvegarde"); + System.exit(1); + } } - public void checkAndDelete(File modToDownload, String slug) { + public boolean checkAndDelete(File modToDownload, int modid) { + File alredyInstalledMods = new File("mods/"+installedMods.get(modid)); + if (alredyInstalledMods.exists()) { + try { + //si sa balance une exception sa veut dire que le jar n'est pas lisible + new ZipFile(alredyInstalledMods).close(); + } catch (IOException e) { + Log.i("integrityChecker","Corruption detected redownloading"); + alredyInstalledMods.delete(); + return false; + } + if(!modToDownload.getAbsolutePath().equals(alredyInstalledMods.getAbsolutePath())) + { + alredyInstalledMods.delete(); + Log.i("integrityChecker","Corruption detected redownloading"); + return false; + } + return true; + }else { + if(modToDownload.exists()) + { + modToDownload.delete(); + } + } + return false; + } + + + + public boolean checkAndDelete(File modToDownload, String slug) { if (modToDownload.exists()) { try { //si sa balance une exception sa veut dire que le jar n'est pas lisible @@ -20,7 +79,7 @@ public class IntegrityChecker { } catch (IOException e) { Log.i("integrityChecker","Corruption detected redownloading"); modToDownload.delete(); - return; + return false; } for (String s : fileList) { @@ -28,12 +87,37 @@ public class IntegrityChecker { if (!modToDownload.getName().contains(s)) { new File(s).delete(); Log.i("integrityChecker","Corruption detected redownloading"); + return false; } - return; + return true; } } + return false; } + return false; + } + + public void addModsToTheList(String fileName,int modID) + { + installedMods.put(modID, fileName); + updateFile(); + } + + public boolean isModIdKnown(int modId) + { + return installedMods.get(modId) != null; + } + + private void updateFile() + { + try { + modsMap.delete(); + //modsMap.createNewFile(); + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(modsMap)); + oos.writeObject(installedMods); + oos.close(); + } catch (IOException e) {} } } diff --git a/downloader/Main.java b/downloader/Main.java index a8d5276..da587a6 100644 --- a/downloader/Main.java +++ b/downloader/Main.java @@ -24,18 +24,19 @@ public class Main { if (!isAComment(line)) { if(line.startsWith("direct=")) { - String filename = line.replaceFirst("direct=*@", ""); + String url = line.replaceFirst("direct=.*@", ""); + String filename=extractNameFromLink(line); //le slug est une nom de mods qui se trouve dans le nom du fichier String slug = extractSlugFromLink(line); //on check si le mods est pas déja installé et si il est a jours - updateManager.checkAndDelete(new File("mods/"+filename), slug); - - String[] url = line.replaceFirst("direct=*@", "").split("/"); - Log.i("main","downloading "+ url[url.length -1 ]); - - HttpHelper.readFileFromUrlToFolder(filename,"mods"); - Log.i("main","done"); + boolean upToDate = updateManager.checkAndDelete(new File("mods/"+filename), slug); + if(!upToDate) + { + Log.i("main","downloading "+ filename); + HttpHelper.readFileFromUrlToFolder(url,"mods",filename); + Log.i("main","done"); + } }else { if (line.split("/").length >= 5 && !db.fetchMod(line)) { Log.e("main","error could not get " + line); @@ -44,13 +45,21 @@ public class Main { } } in.close(); + Log.i("Main", "finished"); } private static String extractSlugFromLink(String line) { - char[] chars = new char[line.length()-line.indexOf("@")-"direct=".length()+1]; - line.getChars("direct=".length(), line.indexOf("@"), chars, 0); + char[] chars = new char[line.length()-line.indexOf(";")-"direct=".length()+1]; + line.getChars("direct=".length(), line.indexOf(";"), chars, 0); return new String(chars); } + + private static String extractNameFromLink(String line) { + char[] chars = new char[line.indexOf("@")-line.indexOf(";")]; + line.getChars(line.indexOf(";")+1, line.indexOf("@"), chars, 0); + return new String(chars); + } + public static boolean isAComment(String line) {