ajout checkadv + patch dl

This commit is contained in:
marc barbier
2021-01-14 10:25:28 +01:00
parent 3d6224e3af
commit 3ff34f646b
4 changed files with 140 additions and 27 deletions
+19 -6
View File
@@ -123,7 +123,6 @@ public class Database {
try { try {
if (rs.next()) { if (rs.next()) {
modID = rs.getInt("projectid"); modID = rs.getInt("projectid");
Log.i("DB", "modid:" + modID);
} else { } else {
Log.e("DB", "not found " + slug); Log.e("DB", "not found " + slug);
return -1; return -1;
@@ -169,13 +168,27 @@ public class Database {
try { try {
String downloadUrl = file.getDownloadUrl(modID); String downloadUrl = file.getDownloadUrl(modID);
Log.i("DB", "checking " + pj.getName()); Log.i("MOD", "checking " + pj.getName().replace(" ", "-"));
integrityChecker.checkAndDelete(new File("mods/" + HttpHelper.getFileNameFromURL(downloadUrl)), // si on connais le mod alors on se base si son fichier sinon on essay de le
pj.getSlug()); // 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 // on remplace les espace par des tiret pour eviter toute confusion
Log.i("DB", "downloading " + pj.getName().replace(" ", "-")); Log.i("MOD", "downloading " + pj.getName().replace(" ", "-"));
HttpHelper.readFileFromUrlToFolder(downloadUrl, "mods"); HttpHelper.readFileFromUrlToFolder(downloadUrl, "mods");
Log.i("DB", "download finished"); 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; return true;
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
+11 -4
View File
@@ -58,6 +58,13 @@ public class HttpHelper {
} }
public static File readFileFromUrlToFolder(String urlToFetch, String folder) throws MalformedURLException { 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 { try {
Files.createDirectories(Paths.get(folder)); Files.createDirectories(Paths.get(folder));
} catch (IOException e) { } catch (IOException e) {
@@ -68,10 +75,7 @@ public class HttpHelper {
if (!folder.endsWith("/")) if (!folder.endsWith("/"))
folder += "/"; folder += "/";
String name = getFileNameFromURL(urlToFetch); return readFileFromUrl(urlToFetch, new File((folder + name.toLowerCase()).trim()));
if(name == null)throw new MalformedURLException();
return readFileFromUrl(urlToFetch, new File(folder + name));
} }
private static File readFileFromUrl(String urlToFetch, File destination) throws MalformedURLException { private static File readFileFromUrl(String urlToFetch, File destination) throws MalformedURLException {
@@ -85,14 +89,17 @@ public class HttpHelper {
} }
File downloadingFile = destination; File downloadingFile = destination;
System.out.println(downloadingFile.getAbsolutePath());
if (downloadingFile.exists()) { if (downloadingFile.exists()) {
return downloadingFile; return downloadingFile;
} else { } else {
try { try {
System.out.println(downloadingFile);
downloadingFile.createNewFile(); downloadingFile.createNewFile();
} catch (IOException e) { } catch (IOException e) {
Log.e("HTTPHelper","permission error could not write file"); Log.e("HTTPHelper","permission error could not write file");
e.printStackTrace();
System.exit(1); System.exit(1);
} }
} }
+88 -4
View File
@@ -1,18 +1,77 @@
package downloader; package downloader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; 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; import java.util.zip.ZipFile;
public class IntegrityChecker { public class IntegrityChecker {
public static String[] fileList; public String[] fileList;
public SortedMap<Integer, String> installedMods;
public File modsMap;
@SuppressWarnings("unchecked")
public IntegrityChecker() public IntegrityChecker()
{ {
fileList = new File("mods").list(); fileList = new File("mods").list();
modsMap = new File("mods/modsMap.sav");
if(modsMap.exists())
{
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(modsMap));
installedMods = (SortedMap<Integer, String>) 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()) { if (modToDownload.exists()) {
try { try {
//si sa balance une exception sa veut dire que le jar n'est pas lisible //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) { } catch (IOException e) {
Log.i("integrityChecker","Corruption detected redownloading"); Log.i("integrityChecker","Corruption detected redownloading");
modToDownload.delete(); modToDownload.delete();
return; return false;
} }
for (String s : fileList) { for (String s : fileList) {
@@ -28,12 +87,37 @@ public class IntegrityChecker {
if (!modToDownload.getName().contains(s)) { if (!modToDownload.getName().contains(s)) {
new File(s).delete(); new File(s).delete();
Log.i("integrityChecker","Corruption detected redownloading"); 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) {}
} }
} }
+18 -9
View File
@@ -24,18 +24,19 @@ public class Main {
if (!isAComment(line)) { if (!isAComment(line)) {
if(line.startsWith("direct=")) 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 //le slug est une nom de mods qui se trouve dans le nom du fichier
String slug = extractSlugFromLink(line); String slug = extractSlugFromLink(line);
//on check si le mods est pas déja installé et si il est a jours //on check si le mods est pas déja installé et si il est a jours
updateManager.checkAndDelete(new File("mods/"+filename), slug); boolean upToDate = updateManager.checkAndDelete(new File("mods/"+filename), slug);
if(!upToDate)
String[] url = line.replaceFirst("direct=*@", "").split("/"); {
Log.i("main","downloading "+ url[url.length -1 ]); Log.i("main","downloading "+ filename);
HttpHelper.readFileFromUrlToFolder(url,"mods",filename);
HttpHelper.readFileFromUrlToFolder(filename,"mods");
Log.i("main","done"); Log.i("main","done");
}
}else { }else {
if (line.split("/").length >= 5 && !db.fetchMod(line)) { if (line.split("/").length >= 5 && !db.fetchMod(line)) {
Log.e("main","error could not get " + line); Log.e("main","error could not get " + line);
@@ -44,14 +45,22 @@ public class Main {
} }
} }
in.close(); in.close();
Log.i("Main", "finished");
} }
private static String extractSlugFromLink(String line) { private static String extractSlugFromLink(String line) {
char[] chars = new char[line.length()-line.indexOf("@")-"direct=".length()+1]; char[] chars = new char[line.length()-line.indexOf(";")-"direct=".length()+1];
line.getChars("direct=".length(), line.indexOf("@"), chars, 0); line.getChars("direct=".length(), line.indexOf(";"), chars, 0);
return new String(chars); 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) public static boolean isAComment(String line)
{ {
return line.startsWith("//"); return line.startsWith("//");