update refacto

This commit is contained in:
marc barbier
2021-07-07 23:36:00 +02:00
parent 7c88e6cf2f
commit 7dfe4c2d3f
27 changed files with 629 additions and 646 deletions
+28
View File
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ModsListDownloader</name>
<comment>Project ModsListDownloader created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1625690947479</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
@@ -0,0 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(6.8))
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/usr/lib/jvm/java-11-graalvm
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
Binary file not shown.
Binary file not shown.
@@ -19,7 +19,6 @@ public class CurseForgeModFile extends ProjectInfo {
public int getFileID() { public int getFileID() {
return this.fileID; return this.fileID;
} }
public int getProjectID() { public int getProjectID() {
return this.projectID; return this.projectID;
} }
@@ -0,0 +1,138 @@
package downloader;
import java.io.File;
import java.net.MalformedURLException;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.google.gson.Gson;
import downloader.forgeSvc.ForgeSvcEntry;
import downloader.forgeSvc.ForgeSvcFile;
import downloader.helper.HttpHelper;
public class Database {
DBConnector connector;
IntegrityChecker integrityChecker;
Gson gson;
public Database() {
this.connector = new DBConnector();
try {
this.connector.connect((new File("database.dat")).getAbsolutePath());
} catch (SQLException e) {
//database hs
System.exit(1);
}
integrityChecker = new IntegrityChecker();
this.gson = new Gson();
}
private int findProjectBySlug(String slug, int ptype) {
int modID = -1;
ResultSet rs = this.connector.executeRequest("select projectid from projects where type =" + ptype + " and slug =\"" +
slug.trim().toLowerCase() + "\"");
try {
if (rs.next()) {
modID = rs.getInt("projectid");
} else {
Log.e("DB", "not found " + slug);
return -1;
}
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
return modID;
}
private int findModBySlug(String slug) {
return findProjectBySlug(slug, 0);
}
private ProjectInfo getProjectInfo(int projectId) {
ResultSet rs = this.connector.executeRequest(
"select slug, name, description from projects where projectid = " + projectId + " and type = 0");
try {
if (rs.next())
return new ProjectInfo(rs.getString("slug"), rs.getString("name"), rs.getString("description"));
} catch (SQLException sQLException) {}
return null;
}
public boolean fetchMod(String string) {
String name = string.split("/")[5];
int modID = findModBySlug(name);
if (modID == -1) {
Log.e("DB", "could not find mod id");
return false;
}
ProjectInfo pj = getProjectInfo(modID);
if (pj == null)
return false;
CurseForgeModFile modfile = new CurseForgeModFile(pj, modID, -1, false);
ForgeSvcFile file = getLastestFile("1.12.2", modfile);
if (file != null) {
try {
boolean upToDate;
String downloadUrl = file.getDownloadUrl(modID);
if (Main.verbose)
Log.i("MOD", "checking " + pj.getName().replace(" ", "-"));
if (this.integrityChecker.isModIdKnown(modID)) {
if (Main.verbose)
Log.i("DB", "found " + name);
upToDate = this.integrityChecker
.checkAndDelete(new File("mods/" + HttpHelper.getFileNameFromURL(downloadUrl)), modID);
} else {
Log.i("DB", "NEW MOD DETECTED " + modID);
upToDate = this.integrityChecker.checkAndDelete(
new File("mods/" + HttpHelper.getFileNameFromURL(downloadUrl)), pj.getSlug());
}
if (!upToDate) {
if (Main.verbose)
Log.i("MOD", "downloading " + pj.getName().replace(" ", "-"));
if (!Main.checkingonly)
HttpHelper.readFileFromUrlToFolder(downloadUrl, "mods");
if (Main.verbose)
Log.i("MOD", "download finished");
if (!this.integrityChecker.isModIdKnown(modID) && !Main.checkingonly) {
this.integrityChecker.addModsToTheList(HttpHelper.getFileNameFromURL(downloadUrl), modID);
if (Main.verbose)
Log.i("DB", "added a mod to the registry");
}
}
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else {
Log.e("DB", "no file in server");
}
return false;
}
public ForgeSvcFile getLastestFile(String minecraftVer, CurseForgeModFile mod) {
String jsonProject, projectUrl = "https://addons-ecs.forgesvc.net/api/v2/addon/" + mod.getProjectID();
try {
jsonProject = HttpHelper.readStringFromUrl(projectUrl);
if (jsonProject.equals("error"))
return null;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
ForgeSvcEntry entry = (ForgeSvcEntry)this.gson.fromJson(jsonProject, ForgeSvcEntry.class);
byte b;
int i;
ForgeSvcFile[] arrayOfForgeSvcFile;
for (i = (arrayOfForgeSvcFile = entry.getFiles()).length, b = 0; b < i; ) {
ForgeSvcFile f = arrayOfForgeSvcFile[b];
if (f.getGameVersion().equals(minecraftVer))
return f;
b++;
}
return null;
}
}
@@ -0,0 +1,88 @@
package downloader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import downloader.helper.ArchiveHelper;
import downloader.helper.HttpHelper;
public class DatabaseVersionManager {
private String version;
private static final String Iam = "DBUpdater";
public DatabaseVersionManager() {
try {
this.version = fetchVersionFromFile();
} catch (FileNotFoundException e) {
this.version = null;
}
}
public void updateIfNeeded() {
String latestVersion = fetchLatestVersion();
if( !version.equals(latestVersion) ) {
Log.i(Iam, "new dbVersion avaliable downloading...");
downloadDatabase(latestVersion);
} else {
Log.i(Iam, "update to Date");
}
}
private void downloadDatabase(String dbVersion) {
Log.i(Iam, "fetching=" + "http://files.mcdex.net/data/mcdex-v5-" + dbVersion + ".dat.bz2");
File archive;
try {
archive = HttpHelper.readFileFromUrl("http://files.mcdex.net/data/mcdex-v5-" + dbVersion + ".dat.bz2");
} catch (MalformedURLException e) {
Log.e(Iam, "Error invalid database download url");
System.exit(1);
//utile pour le lint
return;
}
Log.i(Iam, "download finished");
Log.i(Iam, "decompressing db");
try {
ArchiveHelper.decompressBz2(archive, "database.dat");
archive.delete();
} catch (IOException e) {
Log.e(Iam, "[ERROR]cannot extract database");
if(archive.exists()) archive.delete();
System.exit(1);
}
}
private String fetchLatestVersion() {
try {
return HttpHelper.readStringFromUrl("http://files.mcdex.net/data/latest.v5");
} catch (MalformedURLException e) {
//erreur critique il ne faut pas continuer
e.printStackTrace();
System.exit(1);
}
return null;
}
private static String fetchVersionFromFile() throws FileNotFoundException {
File versionFile = new File("dbVersion");
if( versionFile.exists()) {
BufferedReader bfr = new BufferedReader(new FileReader(versionFile));
try {
String version = bfr.readLine();
bfr.close();
return version;
} catch (IOException e) {
//si on peut pas lire le fichier on le suprime
versionFile.delete();
return null;
}
} else {
throw new FileNotFoundException();
}
}
}
@@ -0,0 +1,158 @@
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 String[] fileList;
public SortedMap<Integer, String> 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<Integer, String>) ois.readObject();
if(installedMods == null)installedMods = new TreeMap<>();
ois.close();
} catch (Exception e) {
installedMods = new TreeMap<>();
modsMap.delete();
}
}else {
try {
modsMap.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
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 boolean checkAndDelete(File modToDownload, int modid) {
String alredyInstalledName = installedMods.get(modid).toLowerCase();
File alredyInstalledMods= null;
//on cherche le fichier local affin de ne pas voir d'erreur avec la casse
File dir = new File("mods/");
for(String fileName : dir.list())
{
if(fileName.equalsIgnoreCase(alredyInstalledName))
{
alredyInstalledMods = new File("mods/"+fileName);
}
}
if(alredyInstalledMods == null) {
Log.e("IntegrityChecker", "alredy installed mod not found ("+alredyInstalledName+")");
installedMods.remove(modid);
return false;
}
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();
installedMods.remove(modid);
updateFile();
return false;
}
if(!modToDownload.getAbsolutePath().equalsIgnoreCase(alredyInstalledMods.getAbsolutePath()))
{
alredyInstalledMods.delete();
Log.i("integrityChecker","Outdated mod detected redownloading ("+alredyInstalledName+" -> "+modToDownload.getName()+")");
installedMods.remove(modid);
updateFile();
return false;
}
return true;
}else {
if(modToDownload.exists())
{
installedMods.put(modid, modToDownload.getName());
updateFile();
Log.i("interessting", modToDownload.getName());
}
}
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
new ZipFile(modToDownload).close();
} catch (IOException e) {
Log.i("integrityChecker","Corruption detected redownloading");
modToDownload.delete();
return false;
}
for (String s : fileList) {
if (s.contains(slug)) {
if (!modToDownload.getName().contains(s)) {
new File(s).delete();
Log.i("integrityChecker","Corruption detected redownloading");
return false;
}
return true;
}
}
return false;
}
return false;
}
/**
* un chemin d'acces ne marche pas
* @param fileName
* @param modID
*/
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();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(modsMap));
oos.writeObject(installedMods);
oos.close();
} catch (IOException e) {}
}
}
@@ -1,6 +1,8 @@
package downloader; package downloader;
public class Log { public class Log {
private Log() {}
public static void e(String who, String msg) { public static void e(String who, String msg) {
System.err.println(String.valueOf(who) + " : " + msg); System.err.println(String.valueOf(who) + " : " + msg);
} }
@@ -0,0 +1,93 @@
package downloader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Iterator;
public class Main {
public static boolean verbose;
public static boolean checkingonly;
public static Integer threadNb;
private static Database db;
public static void main(String[] args) {
checkModsDirectory();
new DatabaseVersionManager().updateIfNeeded();
db = new Database();
interpretArgs(args);
}
public static void checkModsDirectory() {
File modDir = new File("mods");
if(!modDir.exists()) {
try {
Files.createDirectories(Paths.get("mods"));
} catch (IOException e) {
Log.e("Main","could not create folder");
System.exit(0);
}
}
}
private static void interpretArgs(String[] args) {
Iterator<String> it = Arrays.asList(args).iterator();
final int cores = Runtime.getRuntime().availableProcessors();
while (it.hasNext()) {
String cmd = it.next();
switch (cmd) {
case "--check":
checkingonly = true;
continue;
case "-t":
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", "running on " + cores + " thread");
threadNb = Integer.valueOf(cores);
continue;
case "-v":
verbose = true;
continue;
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);
}
String threadNumber = it.next();
if (threadNumber.matches("[0-9]*")) {
threadNb = Integer.valueOf(Integer.parseInt(threadNumber));
continue;
}
Log.e("main","arguments invalid please refer to --help");
System.exit(1);
continue;
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");
System.exit(0);
continue;
default:
Log.e("main", "unknown args " + cmd + "\nsee --help for help");
System.exit(1);
}
}
}
}
@@ -1,8 +1,9 @@
package downloader.forgeSvc; package downloader.forgeSvc;
import downloader.HttpHelper;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import downloader.helper.HttpHelper;
public class ForgeSvcFile { public class ForgeSvcFile {
private String gameVersion; private String gameVersion;
private int fileType; private int fileType;
@@ -0,0 +1,23 @@
package downloader.helper;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
public class ArchiveHelper {
private ArchiveHelper(){}
public static File decompressBz2(File inputFile, String outputFile) throws IOException {
BZip2CompressorInputStream input = new BZip2CompressorInputStream(
new BufferedInputStream(new FileInputStream(inputFile)));
File decompressedFile = new File(outputFile);
FileOutputStream output = new FileOutputStream(decompressedFile);
IOUtils.copy(input, output);
return decompressedFile;
}
}
@@ -1,112 +1,142 @@
package downloader; package downloader.helper;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import downloader.Log;
public class HttpHelper { public class HttpHelper {
private HttpHelper() {}
public static String readStringFromUrl(String urlToFetch) throws MalformedURLException { public static String readStringFromUrl(String urlToFetch) throws MalformedURLException {
HttpURLConnection con;
URL url = new URL(urlToFetch); URL url = new URL(urlToFetch);
HttpURLConnection con;
try { try {
con = (HttpURLConnection)url.openConnection(); con = (HttpURLConnection) url.openConnection();
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); e1.printStackTrace();
return "error"; return "error";
} }
String str = ""; String str = "";
try {
Exception exception2, exception1 = null; try (InputStream in = con.getInputStream()) {
} catch (IOException iOException) {} // par tranche de 4mo
for (;;) {
byte[] buffer = new byte[4096];
int nbBytesRead = in.read(buffer);
if (nbBytesRead < 0)
break;
for (int i = 0; i < nbBytesRead; i++) {
str += (char) buffer[i];
}
}
} catch (IOException e) {}
return str; return str;
} }
public static File readFileFromUrl(String urlToFetch) throws MalformedURLException { public static File readFileFromUrl(String urlToFetch) throws MalformedURLException {
String[] url = urlToFetch.split("/"); String[] url = urlToFetch.split("/");
if (url.length == 0) if (url.length == 0)
throw new MalformedURLException(); throw new MalformedURLException();
return readFileFromUrl(urlToFetch, url[url.length - 1]); return readFileFromUrl(urlToFetch, url[url.length - 1]);
} }
public static File readFileFromUrl(String urlToFetch, String filename) throws MalformedURLException { public static File readFileFromUrl(String urlToFetch, String filename) throws MalformedURLException {
return readFileFromUrl(urlToFetch, new File(filename)); return readFileFromUrl(urlToFetch, new File(filename));
} }
public static File readFileFromUrlToFolder(String urlToFetch, String folder) throws MalformedURLException { public static File readFileFromUrlToFolder(String urlToFetch, String folder) throws MalformedURLException {
String name = getFileNameFromURL(urlToFetch); String name = getFileNameFromURL(urlToFetch);
if (name == null) if(name == null)throw new MalformedURLException();
throw new MalformedURLException();
return readFileFromUrlToFolder(urlToFetch, folder, name); return readFileFromUrlToFolder(urlToFetch, folder,name);
} }
public static File readFileFromUrlToFolder(String urlToFetch, String folder, String name) throws MalformedURLException { public static File readFileFromUrlToFolder(String urlToFetch, String folder,String name) throws MalformedURLException {
try { try {
Files.createDirectories(Paths.get(folder, new String[0]), (FileAttribute<?>[])new FileAttribute[0]); Files.createDirectories(Paths.get(folder));
} catch (IOException e) { } catch (IOException e) {
Log.e("HTTPHelper", "could not create folder"); Log.e("HTTPHelper","could not create folder");
return null; return null;
} }
if (!folder.endsWith("/")) if (!folder.endsWith("/"))
folder = String.valueOf(folder) + "/"; folder += "/";
return readFileFromUrl(urlToFetch, new File((String.valueOf(folder) + name.toLowerCase()).trim()));
return readFileFromUrl(urlToFetch, new File((folder + name.toLowerCase()).trim()));
} }
private static File readFileFromUrl(String urlToFetch, File destination) throws MalformedURLException { private static File readFileFromUrl(String urlToFetch, File destination) throws MalformedURLException {
HttpURLConnection con;
FileOutputStream downloadingWriter;
URL url = new URL(urlToFetch); URL url = new URL(urlToFetch);
HttpURLConnection con;
try { try {
con = (HttpURLConnection)url.openConnection(); con = (HttpURLConnection) url.openConnection();
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); e1.printStackTrace();
return null; return null;
} }
File downloadingFile = destination; File downloadingFile = destination;
System.out.println(downloadingFile.getAbsolutePath()); System.out.println(downloadingFile.getAbsolutePath());
if (downloadingFile.exists())
return downloadingFile; if (downloadingFile.exists()) {
try { return downloadingFile;
System.out.println(downloadingFile); } else {
downloadingFile.createNewFile(); try {
} catch (IOException e) { System.out.println(downloadingFile);
Log.e("HTTPHelper", "permission error could not write file"); downloadingFile.createNewFile();
e.printStackTrace(); } catch (IOException e) {
System.exit(1); Log.e("HTTPHelper","permission error could not write file");
} e.printStackTrace();
System.exit(1);
}
}
FileOutputStream downloadingWriter;
try { try {
downloadingWriter = new FileOutputStream(downloadingFile); downloadingWriter = new FileOutputStream(downloadingFile);
} catch (FileNotFoundException e1) { } catch (FileNotFoundException e1) {
e1.printStackTrace(); e1.printStackTrace();
return null; return null;
} // imposible vu qu'on l'a crée precedament
try { }
Exception exception1 = null, exception2 = null;
try { try (InputStream in = con.getInputStream()) {
// on lit par tranche de 4mo
} finally { byte[] buffer = new byte[4096];
exception2 = null;
if (exception1 == null) { while (true) {
exception1 = exception2; int bytesRead = in.read(buffer);
} else if (exception1 != exception2) { if (bytesRead < 0)
exception1.addSuppressed(exception2); break;
} downloadingWriter.write(buffer, 0, bytesRead);
} }
} catch (IOException iOException) { downloadingWriter.close();
return null; return downloadingFile;
} } catch (IOException e) {}
return null;
} }
public static String getFileNameFromURL(String url) { public static String getFileNameFromURL(String url)
{
String[] urlsplit = url.split("/"); String[] urlsplit = url.split("/");
if (urlsplit.length == 0) if (urlsplit.length == 0)
return null; return null;
return urlsplit[urlsplit.length - 1]; return urlsplit[urlsplit.length - 1];
} }
} }
-220
View File
@@ -1,220 +0,0 @@
package downloader;
import com.google.gson.Gson;
import downloader.forgeSvc.ForgeSvcEntry;
import downloader.forgeSvc.ForgeSvcFile;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
public class Database {
String version;
DBConnector connector;
Gson gson;
IntegrityChecker integrityChecker;
File dbVer = new File("dbVersion");
public Database() {
if (this.dbVer.exists()) {
try {
BufferedReader bfr = new BufferedReader(new FileReader(this.dbVer));
this.version = bfr.readLine();
bfr.close();
this.connector = new DBConnector();
this.connector.connect((new File("database.dat")).getAbsolutePath());
} catch (IOException|SQLException e) {
this.dbVer.delete();
this.version = "";
}
} else {
this.version = "";
}
this.gson = new Gson();
this.integrityChecker = new IntegrityChecker();
}
public void updateDatabase() {
try {
File decompressedDatabase;
String dbVersion = HttpHelper.readStringFromUrl("http://files.mcdex.net/data/latest.v5");
if (dbVersion.equals(this.version)) {
Log.i("DB", "alredy up to date");
return;
}
if (dbVersion.equals("error") || dbVersion.trim().equals("")) {
Log.i("DB", "an error ocured");
System.exit(1);
}
Log.i("DB", "newer version found");
if (!this.version.isEmpty()) {
Log.i("DB", "current=" + this.version + " remote=" + dbVersion);
} else {
Log.i("DB", "new=" + dbVersion);
}
Log.i("DB", "fetching=http://files.mcdex.net/data/mcdex-v5-" + dbVersion + ".dat.bz2");
File archive = HttpHelper.readFileFromUrl("http://files.mcdex.net/data/mcdex-v5-" + dbVersion + ".dat.bz2");
Log.i("DB", "download finished");
Log.i("DB", "decompressing db");
try {
decompressedDatabase = decompressBz2(archive, "database.dat");
} catch (IOException e) {
Log.i("DB", "cannot extract database");
return;
}
Log.i("DB", "done");
Log.i("DB", "loading the database");
this.connector = new DBConnector();
try {
this.connector.connect(decompressedDatabase.getAbsolutePath());
} catch (SQLException e) {
return;
}
Log.i("DB", "database loaded");
this.version = dbVersion;
Log.i("DB", "saving db data");
this.dbVer.delete();
try {
this.dbVer.createNewFile();
PrintWriter p = new PrintWriter(this.dbVer);
p.print(this.version);
p.close();
} catch (Exception e) {
Log.e("DB", "failed to save db");
}
} catch (MalformedURLException e) {
Log.i("DB", "database link is dead");
System.exit(1);
}
}
private File decompressBz2(File inputFile, String outputFile) throws IOException {
BZip2CompressorInputStream input = new BZip2CompressorInputStream(
new BufferedInputStream(new FileInputStream(inputFile)));
File decompressedFile = new File(outputFile);
FileOutputStream output = new FileOutputStream(decompressedFile);
IOUtils.copy((InputStream)input, output);
return decompressedFile;
}
private int findProjectBySlug(String slug, int ptype) {
int modID = -1;
ResultSet rs = this.connector.executeRequest("select projectid from projects where type =" + ptype + " and slug =\"" +
slug.trim().toLowerCase() + "\"");
try {
if (rs.next()) {
modID = rs.getInt("projectid");
} else {
Log.e("DB", "not found " + slug);
return -1;
}
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
return modID;
}
private int findModBySlug(String slug) {
return findProjectBySlug(slug, 0);
}
private ProjectInfo getProjectInfo(int projectId) {
ResultSet rs = this.connector.executeRequest(
"select slug, name, description from projects where projectid = " + projectId + " and type = 0");
try {
if (rs.next())
return new ProjectInfo(rs.getString("slug"), rs.getString("name"), rs.getString("description"));
} catch (SQLException sQLException) {}
return null;
}
public boolean fetchMod(String string) {
String name = string.split("/")[5];
int modID = findModBySlug(name);
if (modID == -1) {
Log.e("DB", "could not find mod id");
return false;
}
ProjectInfo pj = getProjectInfo(modID);
if (pj == null)
return false;
CurseForgeModFile modfile = new CurseForgeModFile(pj, modID, -1, false);
ForgeSvcFile file = getLastestFile("1.12.2", modfile);
if (file != null) {
try {
boolean upToDate;
String downloadUrl = file.getDownloadUrl(modID);
if (Main.isVersbose())
Log.i("MOD", "checking " + pj.getName().replace(" ", "-"));
if (this.integrityChecker.isModIdKnown(modID)) {
if (Main.isVersbose())
Log.i("DB", "found " + name);
upToDate = this.integrityChecker
.checkAndDelete(new File("mods/" + HttpHelper.getFileNameFromURL(downloadUrl)), modID);
} else {
Log.i("DB", "NEW MOD DETECTED " + modID);
upToDate = this.integrityChecker.checkAndDelete(
new File("mods/" + HttpHelper.getFileNameFromURL(downloadUrl)), pj.getSlug());
}
if (!upToDate) {
if (Main.isVersbose())
Log.i("MOD", "downloading " + pj.getName().replace(" ", "-"));
if (!Main.isCheckingOnly())
HttpHelper.readFileFromUrlToFolder(downloadUrl, "mods");
if (Main.isVersbose())
Log.i("MOD", "download finished");
if (!this.integrityChecker.isModIdKnown(modID) && !Main.isCheckingOnly()) {
this.integrityChecker.addModsToTheList(HttpHelper.getFileNameFromURL(downloadUrl), modID);
if (Main.isVersbose())
Log.i("DB", "added a mod to the registry");
}
}
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else {
Log.e("DB", "no file in server");
}
return false;
}
public ForgeSvcFile getLastestFile(String minecraftVer, CurseForgeModFile mod) {
String jsonProject, projectUrl = "https://addons-ecs.forgesvc.net/api/v2/addon/" + mod.getProjectID();
try {
jsonProject = HttpHelper.readStringFromUrl(projectUrl);
if (jsonProject.equals("error"))
return null;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
ForgeSvcEntry entry = (ForgeSvcEntry)this.gson.fromJson(jsonProject, ForgeSvcEntry.class);
byte b;
int i;
ForgeSvcFile[] arrayOfForgeSvcFile;
for (i = (arrayOfForgeSvcFile = entry.getFiles()).length, b = 0; b < i; ) {
ForgeSvcFile f = arrayOfForgeSvcFile[b];
if (f.getGameVersion().equals(minecraftVer))
return f;
b++;
}
return null;
}
}
-137
View File
@@ -1,137 +0,0 @@
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 String[] fileList = (new File("mods")).list();
public SortedMap<Integer, String> installedMods;
public File modsMap = new File("mods/modsMap.sav");
public IntegrityChecker() {
if (this.modsMap.exists()) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(this.modsMap));
this.installedMods = (SortedMap<Integer, String>)ois.readObject();
if (this.installedMods == null)
this.installedMods = new TreeMap<>();
ois.close();
} catch (Exception e) {
this.installedMods = new TreeMap<>();
this.modsMap.delete();
}
} else {
try {
this.modsMap.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(this.modsMap));
oos.writeObject(this.installedMods);
oos.close();
} catch (IOException e) {
Log.e("SAV", "erreur pas de sauvegarde");
System.exit(1);
}
}
public boolean checkAndDelete(File modToDownload, int modid) {
String alredyInstalledName = ((String)this.installedMods.get(Integer.valueOf(modid))).toLowerCase();
File alredyInstalledMods = null;
File dir = new File("mods/");
byte b;
int i;
String[] arrayOfString;
for (i = (arrayOfString = dir.list()).length, b = 0; b < i; ) {
String fileName = arrayOfString[b];
if (fileName.equalsIgnoreCase(alredyInstalledName))
alredyInstalledMods = new File("mods/" + fileName);
b++;
}
if (alredyInstalledMods == null) {
Log.e("IntegrityChecker", "alredy installed mod not found (" + alredyInstalledName + ")");
this.installedMods.remove(Integer.valueOf(modid));
return false;
}
if (alredyInstalledMods.exists()) {
try {
(new ZipFile(alredyInstalledMods)).close();
} catch (IOException e) {
Log.i("integrityChecker", "Corruption detected redownloading");
alredyInstalledMods.delete();
this.installedMods.remove(Integer.valueOf(modid));
updateFile();
return false;
}
if (!modToDownload.getAbsolutePath().equalsIgnoreCase(alredyInstalledMods.getAbsolutePath())) {
alredyInstalledMods.delete();
Log.i("integrityChecker", "Outdated mod detected redownloading (" + alredyInstalledName + " -> " + modToDownload.getName() + ")");
this.installedMods.remove(Integer.valueOf(modid));
updateFile();
return false;
}
return true;
}
if (modToDownload.exists()) {
this.installedMods.put(Integer.valueOf(modid), modToDownload.getName());
updateFile();
Log.i("interessting", modToDownload.getName());
}
return false;
}
public boolean checkAndDelete(File modToDownload, String slug) {
if (modToDownload.exists())
try {
(new ZipFile(modToDownload)).close();
return true;
} catch (IOException e) {
Log.i("integrityChecker", "Corruption detected redownloading");
modToDownload.delete();
return false;
}
byte b;
int i;
String[] arrayOfString;
for (i = (arrayOfString = this.fileList).length, b = 0; b < i; ) {
String s = arrayOfString[b];
if (s.toLowerCase().contains(slug.trim())) {
if (modToDownload.getName().trim().equalsIgnoreCase(s.trim()))
return true;
(new File(s)).delete();
break;
}
b++;
}
return false;
}
public void addModsToTheList(String fileName, int modID) {
this.installedMods.put(Integer.valueOf(modID), fileName);
Main.changes.set(Main.changes.get() + 1);
}
public boolean isModIdKnown(int modId) {
return (this.installedMods.get(Integer.valueOf(modId)) != null);
}
public void updateFile() {
try {
this.modsMap.delete();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(this.modsMap));
oos.writeObject(this.installedMods);
oos.close();
} catch (IOException iOException) {}
}
}
-175
View File
@@ -1,175 +0,0 @@
package downloader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
private static File f = null;
private static boolean verbose = false;
private static boolean checkingonly = false;
public static final AtomicInteger changes = new AtomicInteger(0);
private static Integer threadNb = null;
public static void main(String[] args) throws FileNotFoundException, IOException, URISyntaxException {
try {
Files.createDirectories(Paths.get("mods", new String[0]), (FileAttribute<?>[])new FileAttribute[0]);
} catch (IOException e) {
Log.e("HTTPHelper", "could not create folder");
System.exit(0);
}
interpretArgs(args);
if (f == null)
f = new File("modpack.txt");
if (!f.exists()) {
System.out.println("file not found");
System.exit(1);
}
IntegrityChecker updateManager = new IntegrityChecker();
Database db = new Database();
db.updateDatabase();
BufferedReader in = new BufferedReader(new FileReader(f));
if (threadNb == null)
threadNb = Integer.valueOf(4);
List<Thread> threads = new ArrayList<>(threadNb.intValue());
while (in.ready()) {
String line = in.readLine();
if (!isAComment(line)) {
checkThreadState(threads);
Thread newLineTerpreter = new NewLineInterpreter(line, updateManager, db);
newLineTerpreter.start();
threads.add(newLineTerpreter);
}
}
checkThreadState(threads);
in.close();
while (threads.size() != 0) {
try {
Thread.sleep(100L);
} catch (InterruptedException interruptedException) {}
checkThreadState(threads);
}
if (changes.get() != 0 && verbose)
Log.i("Main", "changed " + changes.get() + " mod(s)");
Log.i("Main", "finished");
}
private static void checkThreadState(List<Thread> threads) {
do {
if (threads.size() >= threadNb.intValue())
try {
Thread.sleep(100L);
} catch (InterruptedException interruptedException) {}
Iterator<Thread> it = threads.iterator();
while (it.hasNext()) {
Thread t = it.next();
if (!t.isAlive())
it.remove();
}
} while (threads.size() >= threadNb.intValue());
}
private static void interpretArgs(String[] args) {
Iterator<String> it = Arrays.<String>asList(args).iterator();
while (it.hasNext()) {
String cmd = it.next();
if (cmd.startsWith("-")) {
String nbt;
int cores;
String str1;
switch ((str1 = cmd).hashCode()) {
case -1629068440:
if (!str1.equals("--check"))
break;
checkingonly = true;
continue;
case 1511:
if (!str1.equals("-t"))
break;
if (threadNb != null) {
System.out.println("error conflicting arguments --thread and -t");
System.exit(1);
}
cores = Runtime.getRuntime().availableProcessors();
if (cores <= 0) {
System.out.println("java think you have no cpu core... sooo lets go for 4");
threadNb = Integer.valueOf(4);
continue;
}
System.out.println("running on " + cores + " thread");
threadNb = Integer.valueOf(cores);
continue;
case 1513:
if (!str1.equals("-v"))
break;
verbose = true;
continue;
case 48044553:
if (!str1.equals("--threads"))
break;
if (threadNb != null) {
System.out.println("error conflicting arguments --thread and -t");
System.exit(1);
}
if (!it.hasNext()) {
System.err.println("arguments invalid please refer to --help");
System.exit(1);
}
nbt = it.next();
if (nbt.matches("[0-9]*")) {
threadNb = Integer.valueOf(Integer.parseInt(nbt));
continue;
}
System.err.println("arguments invalid please refer to --help");
System.exit(1);
continue;
case 1333013276:
if (!str1.equals("--file"))
break;
if (!it.hasNext()) {
System.err.println("arguments invalid please refer to --help");
System.exit(1);
}
f = new File(it.next());
continue;
case 1333069025:
if (!str1.equals("--help"))
break;
System.out.println("use --file to specify the modfile\nuse --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");
System.exit(0);
continue;
}
System.err.print("unknown args " + cmd + "\n" +
"see --help for help");
System.exit(1);
}
}
}
public static boolean isAComment(String line) {
return line.startsWith("//");
}
public static boolean isVersbose() {
return verbose;
}
public static boolean isCheckingOnly() {
return checkingonly;
}
}
-58
View File
@@ -1,58 +0,0 @@
package downloader;
import java.io.File;
import java.net.MalformedURLException;
public class NewLineInterpreter extends Thread {
private String line;
private IntegrityChecker updateManager;
private Database db;
public NewLineInterpreter(String line, IntegrityChecker updateManager, Database db) {
this.line = line;
this.updateManager = updateManager;
this.db = db;
}
public void run() {
super.run();
interpretLine();
}
private void interpretLine() {
if (this.line.startsWith("direct=")) {
String url = this.line.replaceFirst("direct=.*@", "");
String filename = extractNameFromLink(this.line).toLowerCase();
String slug = extractSlugFromLink(this.line).toLowerCase();
boolean upToDate = this.updateManager.checkAndDelete(new File("mods/" + filename), slug);
if (!upToDate) {
if (Main.isVersbose())
Log.i("main", "downloading " + filename);
if (!Main.isCheckingOnly())
try {
HttpHelper.readFileFromUrlToFolder(url, "mods", filename);
} catch (MalformedURLException e) {
Log.e("main", "error bad mod url " + this.line);
}
} else if (Main.isVersbose()) {
Log.i("main", "alredy installed");
}
} else if ((this.line.split("/")).length >= 5 && !this.db.fetchMod(this.line)) {
Log.e("main", "error could not get " + this.line);
}
}
private String extractSlugFromLink(String line) {
char[] chars = new char[line.length() - line.indexOf(";") - "direct=".length() + 1];
line.getChars("direct=".length(), line.indexOf(";"), chars, 0);
return new String(chars);
}
private 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);
}
}