From 5f050e3eb42eac21435b89901d48536a0efd8b1a Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 15 Feb 2016 23:57:43 -0600 Subject: [PATCH 01/86] Add mod updater functionality WIP Lots of messy hardcoded stuff right now --- .../modpackdownloader/ModPackDownloader.java | 19 ++- .../modpackdownloader/ModUpdater.java | 113 ++++++++++++++++++ .../modpackdownloader/util/Reference.java | 3 + src/main/resources/manifest.json | 6 + 4 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/nincraft/modpackdownloader/ModUpdater.java diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 4f9bbcf..407690f 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -34,11 +34,16 @@ public static void main(String[] args) { } else { processArguments(args); } - logger.info("Starting download with parameters: " + Reference.manifestFile + ", " + Reference.modFolder); + setupRepo(); - downloadCurseMods(Reference.manifestFile, Reference.modFolder); - downloadThirdPartyMods(Reference.manifestFile, Reference.modFolder); - logger.info("Finished downloading mods"); + if (Reference.updateMods) { + ModUpdater.updateCurseMods(Reference.manifestFile, Reference.mcVersion, Reference.releaseType); + } else { + logger.info("Starting download with parameters: " + Reference.manifestFile + ", " + Reference.modFolder); + downloadCurseMods(Reference.manifestFile, Reference.modFolder); + downloadThirdPartyMods(Reference.manifestFile, Reference.modFolder); + logger.info("Finished downloading mods"); + } } private static void processArguments(String[] args) { @@ -48,6 +53,12 @@ private static void processArguments(String[] args) { for (String arg : args) { if (arg.equals("-forceDownload")) { Reference.forceDownload = true; + } else if (arg.equals("-updateMods")) { + Reference.updateMods = true; + } else if (arg.startsWith("-mcVersion")) { + Reference.mcVersion = arg.substring(arg.lastIndexOf("-mcVersion") + "-mcVersion".length()); + } else if (arg.startsWith("-releaseType")) { + Reference.releaseType = arg.substring(arg.indexOf("-releaseType") + "-releaseType".length()); } } } diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java new file mode 100644 index 0000000..1bab458 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -0,0 +1,113 @@ +package com.nincraft.modpackdownloader; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Iterator; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.nincraft.modpackdownloader.util.Reference; + +public class ModUpdater { + + static Logger logger = LogManager.getRootLogger(); + + private static final String[] formats = { "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ssZ", + "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", + "yyyy-MM-dd HH:mm:ss", "MM/dd/yyyy HH:mm:ss", "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'", "MM/dd/yyyy'T'HH:mm:ss.SSSZ", + "MM/dd/yyyy'T'HH:mm:ss.SSS", "MM/dd/yyyy'T'HH:mm:ssZ", "MM/dd/yyyy'T'HH:mm:ss", "yyyy:MM:dd HH:mm:ss", }; + + public static void updateCurseMods(String manifestFile, String mcVersion, String releaseType) { + try { + Long projectID; + Long fileID; + JSONParser parser = new JSONParser(); + JSONObject jsons = (JSONObject) parser.parse(new FileReader(manifestFile)); + JSONArray fileList = (JSONArray) jsons.get("curseFiles"); + if (fileList == null) { + fileList = (JSONArray) jsons.get("files"); + } + if (fileList != null) { + Iterator iterator = fileList.iterator(); + while (iterator.hasNext()) { + JSONObject modJson = (JSONObject) iterator.next(); + projectID = (Long) modJson.get("projectID"); + fileID = (Long) modJson.get("fileID"); + String url = Reference.CURSEFORGE_BASE_URL + projectID + Reference.COOKIE_TEST_1; + HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection()); + con.setInstanceFollowRedirects(false); + con.connect(); + String location = con.getHeaderField("Location"); + String projectName = location.split("/")[2]; + JSONParser projectParser = new JSONParser(); + JSONObject projectJson = (JSONObject) projectParser.parse(new BufferedReader(new InputStreamReader( + new URL("http://widget.mcf.li/mc-mods/minecraft/237892-nincrafty-things.json") + .openStream()))); + JSONObject fileListJson = (JSONObject) projectJson.get("files"); + Date lastDate = null; + Long mostRecent = fileID; + for (Object thing : fileListJson.keySet()) { + JSONObject file = (JSONObject) fileListJson.get(thing); + Date date = parseDate((String) file.get("created_at")); + if (lastDate == null) { + lastDate = date; + } + if (lastDate.before(date) && file.get("type").equals(releaseType) + && file.get("version").equals(mcVersion)) { + mostRecent = (Long) file.get("id"); + lastDate = date; + } + } + logger.info("Most recent version of " + projectName + " is " + mostRecent+". Old version was "+fileID); + modJson.remove("fileID"); + modJson.put("fileID", mostRecent); + } + } + FileWriter file = new FileWriter(manifestFile); + try { + file.write(jsons.toJSONString()); + } finally { + file.flush(); + file.close(); + } + + } catch (FileNotFoundException e) { + logger.error(e.getMessage()); + } catch (IOException e) { + logger.error(e.getMessage()); + } catch (ParseException e) { + logger.error(e.getMessage()); + } + } + + private static Date parseDate(String date) { + Date d = null; + if (date != null) { + for (String parse : formats) { + SimpleDateFormat sdf = new SimpleDateFormat(parse); + try { + d = sdf.parse(date); + logger.info("using format " + parse); + } catch (java.text.ParseException e) { + + } + } + } + return d; + } + +} diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index 8242809..f56d778 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -12,4 +12,7 @@ public class Reference { public static String manifestFile; public static String modFolder; public static boolean forceDownload = false; + public static boolean updateMods; + public static String mcVersion; + public static String releaseType; } diff --git a/src/main/resources/manifest.json b/src/main/resources/manifest.json index 863c09e..01516ef 100644 --- a/src/main/resources/manifest.json +++ b/src/main/resources/manifest.json @@ -1,4 +1,10 @@ { + "curseFiles": [ + { + "fileID": 2269703, + "projectID": 237892 + } + ], "thirdParty": [ { "name": "Yampst-TWBB", From f31a0f001c3d96e22175b3b2a40c3d747f4c374f Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Tue, 16 Feb 2016 00:19:02 -0600 Subject: [PATCH 02/86] Remove hardcoded URL --- .../modpackdownloader/ModUpdater.java | 30 +++++++++++++------ src/main/resources/manifest.json | 8 ++--- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java index 1bab458..39c7ebc 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -6,7 +6,6 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; -import java.io.StringWriter; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; @@ -26,8 +25,8 @@ public class ModUpdater { static Logger logger = LogManager.getRootLogger(); - private static final String[] formats = { "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ssZ", - "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", + private static final String[] formats = { "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss'Z'", + "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd HH:mm:ss", "MM/dd/yyyy HH:mm:ss", "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'", "MM/dd/yyyy'T'HH:mm:ss.SSSZ", "MM/dd/yyyy'T'HH:mm:ss.SSS", "MM/dd/yyyy'T'HH:mm:ssZ", "MM/dd/yyyy'T'HH:mm:ss", "yyyy:MM:dd HH:mm:ss", }; @@ -54,9 +53,7 @@ public static void updateCurseMods(String manifestFile, String mcVersion, String String location = con.getHeaderField("Location"); String projectName = location.split("/")[2]; JSONParser projectParser = new JSONParser(); - JSONObject projectJson = (JSONObject) projectParser.parse(new BufferedReader(new InputStreamReader( - new URL("http://widget.mcf.li/mc-mods/minecraft/237892-nincrafty-things.json") - .openStream()))); + JSONObject projectJson = getCurseProjectJson(projectID, projectName, projectParser); JSONObject fileListJson = (JSONObject) projectJson.get("files"); Date lastDate = null; Long mostRecent = fileID; @@ -72,9 +69,12 @@ public static void updateCurseMods(String manifestFile, String mcVersion, String lastDate = date; } } - logger.info("Most recent version of " + projectName + " is " + mostRecent+". Old version was "+fileID); - modJson.remove("fileID"); - modJson.put("fileID", mostRecent); + if (!mostRecent.equals(fileID)) { + logger.info("Most recent version of " + projectName + " is " + mostRecent + ". Old version was " + + fileID); + modJson.remove("fileID"); + modJson.put("fileID", mostRecent); + } } } FileWriter file = new FileWriter(manifestFile); @@ -94,6 +94,18 @@ public static void updateCurseMods(String manifestFile, String mcVersion, String } } + private static JSONObject getCurseProjectJson(Long projectID, String projectName, JSONParser projectParser) + throws ParseException, IOException { + try { + return (JSONObject) projectParser.parse(new BufferedReader(new InputStreamReader( + new URL("http://widget.mcf.li/mc-mods/minecraft/" + projectName + ".json").openStream()))); + } catch (FileNotFoundException e) { + return (JSONObject) projectParser.parse(new BufferedReader(new InputStreamReader( + new URL("http://widget.mcf.li/mc-mods/minecraft/" + projectID + "-" + projectName + ".json") + .openStream()))); + } + } + private static Date parseDate(String date) { Date d = null; if (date != null) { diff --git a/src/main/resources/manifest.json b/src/main/resources/manifest.json index 01516ef..123b25b 100644 --- a/src/main/resources/manifest.json +++ b/src/main/resources/manifest.json @@ -7,16 +7,16 @@ ], "thirdParty": [ { - "name": "Yampst-TWBB", + "name": "Yampst-TWBB", "url": "http://en.yampst.net/gettracker.php/Yampst-There%20Will%20Be%20Blood-0.5.0.jar?trackid=fa65e1dc97876791e521a3d6e6b0ddea27e86ee4010b5d47e9d48767&file=e775b851a66544fcc52a1f3404ee7eb023c7964a47edfc639afa9bcb" }, { - "name": "Buildcraft", + "name": "Buildcraft", "url": "https://www.dropbox.com/s/q1qppz3sq447173/buildcraft-7.2.0-pre6.jar?dl=1" }, { - "name": "Charset", - "url": "http://charset.asie.pl/files/Charset-0.2.1-945baf5589832f727c42cf77.jar" + "name": "Charset", + "url": "http://charset.asie.pl/files/Charset-0.2.1-945baf5589832f727c42cf77.jar" } ] } \ No newline at end of file From 3a8f0dd828ab65bd8c2f42c226f6f12788a7b72c Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Tue, 16 Feb 2016 00:28:53 -0600 Subject: [PATCH 03/86] Remove unneeded logging --- src/main/java/com/nincraft/modpackdownloader/ModUpdater.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java index 39c7ebc..b0a8ad4 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -113,7 +113,6 @@ private static Date parseDate(String date) { SimpleDateFormat sdf = new SimpleDateFormat(parse); try { d = sdf.parse(date); - logger.info("using format " + parse); } catch (java.text.ParseException e) { } From 2902f16e2829bcc3d8c0ce467717a1dbcb3e897e Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Tue, 16 Feb 2016 00:44:13 -0600 Subject: [PATCH 04/86] Fix some logging to display new and old file when updating --- .../com/nincraft/modpackdownloader/ModUpdater.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java index b0a8ad4..f62f590 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -57,6 +57,8 @@ public static void updateCurseMods(String manifestFile, String mcVersion, String JSONObject fileListJson = (JSONObject) projectJson.get("files"); Date lastDate = null; Long mostRecent = fileID; + String mostRecentFile = null; + String currentFile = null; for (Object thing : fileListJson.keySet()) { JSONObject file = (JSONObject) fileListJson.get(thing); Date date = parseDate((String) file.get("created_at")); @@ -66,15 +68,22 @@ public static void updateCurseMods(String manifestFile, String mcVersion, String if (lastDate.before(date) && file.get("type").equals(releaseType) && file.get("version").equals(mcVersion)) { mostRecent = (Long) file.get("id"); + mostRecentFile = (String) file.get("name"); lastDate = date; } + if(fileID.equals((Long) file.get("id"))){ + currentFile = (String) file.get("name"); + } } if (!mostRecent.equals(fileID)) { - logger.info("Most recent version of " + projectName + " is " + mostRecent + ". Old version was " - + fileID); + logger.info("Most recent version of " + projectName + " is " + mostRecentFile + ". Old version was " + + currentFile); + modJson.remove("fileID"); modJson.put("fileID", mostRecent); } + modJson.remove("name"); + modJson.put("name", projectName); } } FileWriter file = new FileWriter(manifestFile); From 94bce7eb21d20b29efcc899ef2b9a3beebc15ab3 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Thu, 18 Feb 2016 23:35:46 -0600 Subject: [PATCH 05/86] Add additional logging to updating --- .../com/nincraft/modpackdownloader/ModUpdater.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java index f62f590..60fb728 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -42,6 +42,7 @@ public static void updateCurseMods(String manifestFile, String mcVersion, String } if (fileList != null) { Iterator iterator = fileList.iterator(); + logger.info("Checking for updates from " + fileList.size() + " mods"); while (iterator.hasNext()) { JSONObject modJson = (JSONObject) iterator.next(); projectID = (Long) modJson.get("projectID"); @@ -71,19 +72,20 @@ public static void updateCurseMods(String manifestFile, String mcVersion, String mostRecentFile = (String) file.get("name"); lastDate = date; } - if(fileID.equals((Long) file.get("id"))){ + if (fileID.equals((Long) file.get("id"))) { currentFile = (String) file.get("name"); } } if (!mostRecent.equals(fileID)) { - logger.info("Most recent version of " + projectName + " is " + mostRecentFile + ". Old version was " - + currentFile); + logger.info("Update found for " + projectName + ". Most recent version is " + mostRecentFile + + ". Old version was " + currentFile); modJson.remove("fileID"); modJson.put("fileID", mostRecent); } - modJson.remove("name"); - modJson.put("name", projectName); + if (!modJson.containsKey("name")) { + modJson.put("name", projectName); + } } } FileWriter file = new FileWriter(manifestFile); From d660f1742138290ee8ea2d2ff4ad95ef51bec8bf Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Fri, 19 Feb 2016 00:12:53 -0600 Subject: [PATCH 06/86] Fix release type handling for updates --- .../nincraft/modpackdownloader/ModPackDownloader.java | 4 ++-- .../java/com/nincraft/modpackdownloader/ModUpdater.java | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 407690f..1163559 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -56,9 +56,9 @@ private static void processArguments(String[] args) { } else if (arg.equals("-updateMods")) { Reference.updateMods = true; } else if (arg.startsWith("-mcVersion")) { - Reference.mcVersion = arg.substring(arg.lastIndexOf("-mcVersion") + "-mcVersion".length()); + Reference.mcVersion = arg.substring(arg.lastIndexOf("=")+1); } else if (arg.startsWith("-releaseType")) { - Reference.releaseType = arg.substring(arg.indexOf("-releaseType") + "-releaseType".length()); + Reference.releaseType = arg.substring(arg.lastIndexOf("=")+1); } } } diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java index 60fb728..26d3e1c 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -66,7 +66,7 @@ public static void updateCurseMods(String manifestFile, String mcVersion, String if (lastDate == null) { lastDate = date; } - if (lastDate.before(date) && file.get("type").equals(releaseType) + if (lastDate.before(date) && equalOrLessThan((String) file.get("type"), releaseType) && file.get("version").equals(mcVersion)) { mostRecent = (Long) file.get("id"); mostRecentFile = (String) file.get("name"); @@ -105,6 +105,13 @@ public static void updateCurseMods(String manifestFile, String mcVersion, String } } + private static boolean equalOrLessThan(String modRelease, String releaseType) { + if (releaseType.equals(modRelease) || "beta".equals(releaseType) && "release".equals(modRelease)) { + return true; + } + return false; + } + private static JSONObject getCurseProjectJson(Long projectID, String projectName, JSONParser projectParser) throws ParseException, IOException { try { From eb945cbf4a6343912f2c3ec350633447f8c03ed4 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Fri, 19 Feb 2016 00:19:21 -0600 Subject: [PATCH 07/86] Refactor Curse mods with ModContainer Might be nicer? I dunno --- pom.xml | 6 ++ .../modpackdownloader/ModPackDownloader.java | 59 ++++++++++++------- .../container/ModContainer.java | 25 ++++++++ .../modpackdownloader/util/ModType.java | 5 ++ .../modpackdownloader/util/Reference.java | 1 + 5 files changed, 74 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/util/ModType.java diff --git a/pom.xml b/pom.xml index 826cbb9..2fd66a7 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,12 @@ commons-io 2.1 + + org.projectlombok + lombok + 1.16.6 + provided + ${project.artifactId} ${project.version}+${build.number} diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 1163559..c06c02d 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -20,6 +20,8 @@ import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.util.ModType; import com.nincraft.modpackdownloader.util.Reference; public class ModPackDownloader { @@ -59,6 +61,8 @@ private static void processArguments(String[] args) { Reference.mcVersion = arg.substring(arg.lastIndexOf("=")+1); } else if (arg.startsWith("-releaseType")) { Reference.releaseType = arg.substring(arg.lastIndexOf("=")+1); + } else if (arg.equals("-generateUrlTxt")) { + Reference.generateUrlTxt = true; } } } @@ -124,8 +128,6 @@ private static void downloadThirdPartyMods(String manifestFile, String modFolder private static void downloadCurseMods(String manifestFile, String modFolder) { JSONParser parser = new JSONParser(); try { - Long projectID; - Long fileID; JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(manifestFile)); JSONArray fileList = (JSONArray) jsonObject.get("curseFiles"); if (fileList == null) { @@ -136,18 +138,15 @@ private static void downloadCurseMods(String manifestFile, String modFolder) { Iterator iterator = fileList.iterator(); DOWNLOAD_COUNT = 1; while (iterator.hasNext()) { - JSONObject modJson = (JSONObject) iterator.next(); - projectID = (Long) modJson.get("projectID"); - fileID = (Long) modJson.get("fileID"); - String url = Reference.CURSEFORGE_BASE_URL + projectID + Reference.COOKIE_TEST_1; + ModContainer mod = new ModContainer((JSONObject) iterator.next(), ModType.CURSE); + String url = Reference.CURSEFORGE_BASE_URL + mod.getProjectId() + Reference.COOKIE_TEST_1; HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection()); con.setInstanceFollowRedirects(false); con.connect(); - String location = con.getHeaderField("Location"); - String projectName = location.split("/")[2]; - logger.info("Downloading " + projectName + ". Mod " + DOWNLOAD_COUNT + " of " + fileList.size()); - downloadCurseForgeFile(createCurseDownloadUrl(projectName, fileID), modFolder, projectName, - modJson); + mod.setProjectName(con.getHeaderField("Location").split("/")[2]); + logger.info("Downloading " + mod.getProjectName() + ". Mod " + DOWNLOAD_COUNT + " of " + + fileList.size()); + downloadCurseForgeFile(mod); DOWNLOAD_COUNT++; } } @@ -160,8 +159,8 @@ private static void downloadCurseMods(String manifestFile, String modFolder) { } } - private static String createCurseDownloadUrl(String projectName, Long fileID) { - return Reference.CURSEFORGE_BASE_URL + projectName + "/files/" + fileID + "/download"; + private static String createCurseDownloadUrl(ModContainer mod) { + return Reference.CURSEFORGE_BASE_URL + mod.getProjectName() + "/files/" + mod.getFileId() + "/download"; } private static void createFolder(String folder) { @@ -173,15 +172,16 @@ private static void createFolder(String folder) { } } - private static void downloadCurseForgeFile(String url, String folder, String projectName, JSONObject modJson) { - String fileName = projectName; + private static void downloadCurseForgeFile(ModContainer mod) { + String fileName = mod.getProjectName(); + String url = createCurseDownloadUrl(mod); try { - if (modJson.get("rename") == null) { - fileName = getCurseForgeDownloadLocation(url, projectName, fileName); + if (mod.getRename() == null) { + fileName = getCurseForgeDownloadLocation(url, mod.getProjectName(), fileName); } else { - fileName = (String) modJson.get("rename"); + fileName = mod.getRename(); } - downloadFile(url, folder, fileName, projectName, false); + downloadFile(url, mod.getFolder(), fileName, mod.getProjectName(), false); } catch (MalformedURLException e) { logger.error(e.getMessage()); } catch (FileNotFoundException e) { @@ -201,7 +201,6 @@ private static void downloadFile(String url, String folder, String fileName, Str HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.addRequestProperty("User-Agent", "Mozilla/4.0"); rbc = Channels.newChannel(con.getInputStream()); - } else { URL fileThing = new URL(url); rbc = Channels.newChannel(fileThing.openStream()); @@ -218,13 +217,29 @@ private static void downloadFile(String url, String folder, String fileName, Str } fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); + if (Reference.generateUrlTxt) { + generateUrlTxt(downloadedFile, url, folder); + } copyToLocalRepo(projectName, downloadedFile); } else { copyFromLocalRepo(projectName, fileName, folder); } } catch (IOException e) { - logger.warn("Error getting " + fileName + ". Attempting redownload with alternate method."); - downloadFile(url, folder, fileName, projectName, true); + if (!useUserAgent) { + logger.warn("Error getting " + fileName + ". Attempting redownload with alternate method."); + downloadFile(url, folder, fileName, projectName, true); + } else { + logger.error("Could not download " + fileName, e.getMessage()); + } + } + } + + private static void generateUrlTxt(File downloadedFile, String url, String folder) { + File urlTxt = null; + if (folder != null) { + urlTxt = new File(folder + File.separator + downloadedFile.getName() + ".url.txt"); + } else { + urlTxt = new File(downloadedFile.getName() + "url.txt"); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java new file mode 100644 index 0000000..c9956cd --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java @@ -0,0 +1,25 @@ +package com.nincraft.modpackdownloader.container; + +import org.json.simple.JSONObject; + +import com.nincraft.modpackdownloader.util.ModType; + +import lombok.Data; + +@Data +public class ModContainer { + public ModContainer(JSONObject modJson, ModType type) { + if (type.equals(ModType.CURSE)) { + setProjectId((Long) modJson.get("projectID")); + setFileId((Long) modJson.get("fileID")); + } + } + + private String modName; + private Long projectId; + private Long fileId; + private String projectName; + private String rename; + private String fileName; + private String folder; +} diff --git a/src/main/java/com/nincraft/modpackdownloader/util/ModType.java b/src/main/java/com/nincraft/modpackdownloader/util/ModType.java new file mode 100644 index 0000000..18a2268 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/util/ModType.java @@ -0,0 +1,5 @@ +package com.nincraft.modpackdownloader.util; + +public enum ModType { + CURSE, THIRDPARTY +} diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index f56d778..bf0fd3e 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -15,4 +15,5 @@ public class Reference { public static boolean updateMods; public static String mcVersion; public static String releaseType; + public static boolean generateUrlTxt; } From 8b5c72ad92de3392a3c6433bb0a4812674349ab3 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Fri, 19 Feb 2016 00:24:23 -0600 Subject: [PATCH 08/86] Set folder for mod whoooops --- .../java/com/nincraft/modpackdownloader/ModPackDownloader.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index c06c02d..83ee22f 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -143,6 +143,7 @@ private static void downloadCurseMods(String manifestFile, String modFolder) { HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection()); con.setInstanceFollowRedirects(false); con.connect(); + mod.setFolder(modFolder); mod.setProjectName(con.getHeaderField("Location").split("/")[2]); logger.info("Downloading " + mod.getProjectName() + ". Mod " + DOWNLOAD_COUNT + " of " + fileList.size()); From c09c888afe654e5276414f39b02433d2367faa92 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Fri, 19 Feb 2016 20:21:32 -0600 Subject: [PATCH 09/86] Add null check for projects that don't have any files --- src/main/java/com/nincraft/modpackdownloader/ModUpdater.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java index 26d3e1c..94e155c 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -56,6 +56,10 @@ public static void updateCurseMods(String manifestFile, String mcVersion, String JSONParser projectParser = new JSONParser(); JSONObject projectJson = getCurseProjectJson(projectID, projectName, projectParser); JSONObject fileListJson = (JSONObject) projectJson.get("files"); + if (fileListJson == null) { + logger.error("No file list found for " + projectName); + return; + } Date lastDate = null; Long mostRecent = fileID; String mostRecentFile = null; From 95922bc9c6e15a1199f966dbd3c162f1c5cffb8f Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Wed, 24 Feb 2016 19:03:32 -0600 Subject: [PATCH 10/86] Initial Commit to Refactor ALL the Things! It seems to compile, but I have no idea if it works as designed yet. Still WIP. --- pom.xml | 8 + .../modpackdownloader/ModPackDownloader.java | 280 +++++++++--------- .../modpackdownloader/ModUpdater.java | 129 ++++---- .../modpackdownloader/container/CurseMod.java | 32 ++ .../container/ModContainer.java | 20 +- .../handler/CurseModHandler.java | 5 + .../modpackdownloader/handler/ModHandler.java | 5 + .../modpackdownloader/util/Reference.java | 13 +- .../modpackdownloader/util/URLHelper.java | 14 + 9 files changed, 284 insertions(+), 222 deletions(-) create mode 100644 src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java diff --git a/pom.xml b/pom.xml index 2fd66a7..58229c3 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,14 @@ ${project.artifactId} ${project.version}+${build.number} + + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + maven-assembly-plugin diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 83ee22f..d687425 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -10,7 +10,6 @@ import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; -import java.util.Iterator; import org.apache.commons.io.FileUtils; import org.apache.logging.log4j.LogManager; @@ -20,16 +19,19 @@ import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import com.nincraft.modpackdownloader.container.CurseMod; import com.nincraft.modpackdownloader.container.ModContainer; -import com.nincraft.modpackdownloader.util.ModType; import com.nincraft.modpackdownloader.util.Reference; +import com.nincraft.modpackdownloader.util.URLHelper; + +import lombok.val; public class ModPackDownloader { private static int DOWNLOAD_COUNT = 1; static Logger logger = LogManager.getRootLogger(); - public static void main(String[] args) { + public static void main(final String[] args) { if (args.length < 2) { logger.error("Arguments required: manifest file location, mod download location"); return; @@ -42,192 +44,178 @@ public static void main(String[] args) { ModUpdater.updateCurseMods(Reference.manifestFile, Reference.mcVersion, Reference.releaseType); } else { logger.info("Starting download with parameters: " + Reference.manifestFile + ", " + Reference.modFolder); - downloadCurseMods(Reference.manifestFile, Reference.modFolder); - downloadThirdPartyMods(Reference.manifestFile, Reference.modFolder); + downloadMods(Reference.manifestFile, Reference.modFolder); logger.info("Finished downloading mods"); } } - private static void processArguments(String[] args) { + private static void processArguments(final String[] args) { Reference.manifestFile = args[0]; Reference.modFolder = args[1]; + if (args.length > 2) { - for (String arg : args) { - if (arg.equals("-forceDownload")) { - Reference.forceDownload = true; - } else if (arg.equals("-updateMods")) { - Reference.updateMods = true; - } else if (arg.startsWith("-mcVersion")) { - Reference.mcVersion = arg.substring(arg.lastIndexOf("=")+1); - } else if (arg.startsWith("-releaseType")) { - Reference.releaseType = arg.substring(arg.lastIndexOf("=")+1); - } else if (arg.equals("-generateUrlTxt")) { - Reference.generateUrlTxt = true; - } + for (val arg : args) { + processArgument(arg); } } } + private static void processArgument(final String arg) { + if (arg.equals("-forceDownload")) { + Reference.forceDownload = true; + } else if (arg.equals("-updateMods")) { + Reference.updateMods = true; + } else if (arg.startsWith("-mcVersion")) { + Reference.mcVersion = arg.substring(arg.lastIndexOf("=") + 1); + } else if (arg.startsWith("-releaseType")) { + Reference.releaseType = arg.substring(arg.lastIndexOf("=") + 1); + } else if (arg.equals("-generateUrlTxt")) { + Reference.generateUrlTxt = true; + } + } + private static void setupRepo() { Reference.userhome = System.getProperty("user.home"); Reference.os = System.getProperty("os.name"); if (Reference.os.startsWith("Windows")) { - Reference.userhome += Reference.windowsFolder; + Reference.userhome += Reference.WINDOWS_FOLDER; } else if (Reference.os.startsWith("Mac")) { - Reference.userhome += Reference.macFolder; + Reference.userhome += Reference.MAC_FOLDER; } else { - Reference.userhome += Reference.otherFolder; + Reference.userhome += Reference.OTHER_FOLDER; } createFolder(Reference.userhome); } - private static void downloadFromGithubSource(String manifestFile, String modFolder) { - try { - String URL = "https://github.com/TPPIDev/Modpack-Tweaks/archive/824ef29f76bab126f4299724ab4f9e658b340639.zip"; - downloadFile(URL, "github", "Modpack-Tweaks.zip", "Modpack Tweaks", false); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } + private static void downloadMods(final String manifestFile, final String modFolder) { + downloadCurseMods(manifestFile, modFolder); + downloadThirdPartyMods(manifestFile, modFolder); } - private static void downloadThirdPartyMods(String manifestFile, String modFolder) { - JSONParser parser = new JSONParser(); + private static void downloadThirdPartyMods(final String manifestFile, final String modFolder) { try { - JSONObject jsons = (JSONObject) parser.parse(new FileReader(manifestFile)); - JSONArray urlList = (JSONArray) jsons.get("thirdParty"); + val jsons = (JSONObject) new JSONParser().parse(new FileReader(manifestFile)); + val urlList = (JSONArray) jsons.get("thirdParty"); + if (urlList != null) { - Iterator iterator = urlList.iterator(); logger.info("Starting download of " + urlList.size() + " 3rd party mods"); DOWNLOAD_COUNT = 1; - while (iterator.hasNext()) { - JSONObject urlJson = (JSONObject) iterator.next(); - String url = (String) urlJson.get("url"); - String projectName = (String) urlJson.get("name"); - String fileName; - if (urlJson.get("rename") == null) { - fileName = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".jar") + 4); - } else { - fileName = (String) urlJson.get("rename"); - } - logger.info("Downloading " + fileName + ". Mod " + DOWNLOAD_COUNT + " of " + urlList.size()); + + for (val item : urlList) { + val urlJson = (JSONObject) item; + val url = (String) urlJson.get("url"); + val projectName = (String) urlJson.get("name"); + val fileName = urlJson.get("rename") == null + ? url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".jar") + 4) + : (String) urlJson.get("rename"); + + logger.info( + String.format("Downloading {}. Mod {} of {}", fileName, DOWNLOAD_COUNT, urlList.size())); downloadFile(url, modFolder, fileName, projectName, false); DOWNLOAD_COUNT++; } } - } catch (FileNotFoundException e) { - logger.error(e.getMessage()); - } catch (IOException e) { - logger.error(e.getMessage()); - } catch (ParseException e) { + } catch (final IOException | ParseException e) { logger.error(e.getMessage()); } } - private static void downloadCurseMods(String manifestFile, String modFolder) { - JSONParser parser = new JSONParser(); + private static void downloadCurseMods(final String manifestFile, final String modFolder) { try { - JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(manifestFile)); - JSONArray fileList = (JSONArray) jsonObject.get("curseFiles"); - if (fileList == null) { - fileList = (JSONArray) jsonObject.get("files"); - } + val jsonObject = (JSONObject) new JSONParser().parse(new FileReader(manifestFile)); + val fileList = (JSONArray) (jsonObject.containsKey("curseFiles") ? jsonObject.get("curseFiles") + : jsonObject.get("files")); + if (fileList != null) { - logger.info("Starting download of " + fileList.size() + " mods from Curse"); - Iterator iterator = fileList.iterator(); + logger.info(String.format("Starting download of {} mods from Curse.", fileList.size())); DOWNLOAD_COUNT = 1; - while (iterator.hasNext()) { - ModContainer mod = new ModContainer((JSONObject) iterator.next(), ModType.CURSE); - String url = Reference.CURSEFORGE_BASE_URL + mod.getProjectId() + Reference.COOKIE_TEST_1; - HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection()); - con.setInstanceFollowRedirects(false); - con.connect(); + for (val file : fileList) { + val mod = new CurseMod((JSONObject) file); + + val conn = (HttpURLConnection) new URL(mod.getProjectURL()).openConnection(); + conn.setInstanceFollowRedirects(false); + conn.connect(); + mod.setFolder(modFolder); - mod.setProjectName(con.getHeaderField("Location").split("/")[2]); - logger.info("Downloading " + mod.getProjectName() + ". Mod " + DOWNLOAD_COUNT + " of " - + fileList.size()); + mod.setProjectName(conn.getHeaderField("Location").split("/")[2]); + + logger.info(String.format("Downloading {}. Mod {} of {}", mod.getProjectName(), DOWNLOAD_COUNT, + fileList.size())); downloadCurseForgeFile(mod); DOWNLOAD_COUNT++; } } - } catch (FileNotFoundException e) { + } catch (final IOException e) { logger.error(e.getMessage()); - } catch (IOException e) { - logger.error(e.getMessage()); - } catch (ParseException e) { + } catch (final ParseException e) { logger.error(e.getMessage()); } } - private static String createCurseDownloadUrl(ModContainer mod) { - return Reference.CURSEFORGE_BASE_URL + mod.getProjectName() + "/files/" + mod.getFileId() + "/download"; - } - - private static void createFolder(String folder) { + private static void createFolder(final String folder) { if (folder != null) { - File dir = new File(folder); + final File dir = new File(folder); if (!dir.exists()) { dir.mkdirs(); } } } - private static void downloadCurseForgeFile(ModContainer mod) { - String fileName = mod.getProjectName(); - String url = createCurseDownloadUrl(mod); + private static void downloadCurseForgeFile(final ModContainer mod) { try { - if (mod.getRename() == null) { - fileName = getCurseForgeDownloadLocation(url, mod.getProjectName(), fileName); - } else { - fileName = mod.getRename(); - } - downloadFile(url, mod.getFolder(), fileName, mod.getProjectName(), false); - } catch (MalformedURLException e) { + val fileName = mod.getRename() == null + ? getCurseForgeDownloadLocation(mod.getUrl(), mod.getProjectName(), mod.getProjectName()) + : mod.getRename(); + + downloadFile(mod.getUrl(), mod.getFolder(), fileName, mod.getProjectName(), false); + } catch (final MalformedURLException e) { logger.error(e.getMessage()); - } catch (FileNotFoundException e) { - logger.error("Could not find: " + fileName, e); - } catch (IOException e) { + } catch (final FileNotFoundException e) { + logger.error(String.format("Could not find: {}", mod.getProjectName()), e); + } catch (final IOException e) { logger.error(e.getMessage()); } } - private static void downloadFile(String url, String folder, String fileName, String projectName, - boolean useUserAgent) throws MalformedURLException, FileNotFoundException { + private static void downloadFile(final String url, final String folder, final String fileName, + final String projectName, final boolean useUserAgent) throws MalformedURLException, FileNotFoundException { try { - fileName = fileName.replace("%20", " "); - if (!isInLocalRepo(projectName, fileName) || Reference.forceDownload) { + val decodedFileName = URLHelper.decodeSpaces(fileName); + if (!isInLocalRepo(projectName, decodedFileName) || Reference.forceDownload) { ReadableByteChannel rbc; + if (useUserAgent) { - HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); - con.addRequestProperty("User-Agent", "Mozilla/4.0"); - rbc = Channels.newChannel(con.getInputStream()); + val conn = (HttpURLConnection) new URL(url).openConnection(); + conn.addRequestProperty("User-Agent", "Mozilla/4.0"); + rbc = Channels.newChannel(conn.getInputStream()); } else { - URL fileThing = new URL(url); - rbc = Channels.newChannel(fileThing.openStream()); + rbc = Channels.newChannel(new URL(url).openStream()); } - FileOutputStream fos; + File downloadedFile; if (folder != null) { createFolder(folder); - downloadedFile = new File(folder + File.separator + fileName); - fos = new FileOutputStream(downloadedFile); + downloadedFile = new File(folder + File.separator + decodedFileName); } else { - downloadedFile = new File(fileName); - fos = new FileOutputStream(downloadedFile); + downloadedFile = new File(decodedFileName); } + + val fos = new FileOutputStream(downloadedFile); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); + if (Reference.generateUrlTxt) { generateUrlTxt(downloadedFile, url, folder); } + copyToLocalRepo(projectName, downloadedFile); } else { - copyFromLocalRepo(projectName, fileName, folder); + copyFromLocalRepo(projectName, decodedFileName, folder); } - } catch (IOException e) { + } catch (final IOException e) { if (!useUserAgent) { - logger.warn("Error getting " + fileName + ". Attempting redownload with alternate method."); + logger.warn( + String.format("Error getting {}. Attempting to redownload using alternate method.", fileName)); downloadFile(url, folder, fileName, projectName, true); } else { logger.error("Could not download " + fileName, e.getMessage()); @@ -235,79 +223,83 @@ private static void downloadFile(String url, String folder, String fileName, Str } } - private static void generateUrlTxt(File downloadedFile, String url, String folder) { - File urlTxt = null; + private static void generateUrlTxt(final File downloadedFile, final String url, final String folder) { if (folder != null) { - urlTxt = new File(folder + File.separator + downloadedFile.getName() + ".url.txt"); + new File(folder + File.separator + downloadedFile.getName() + ".url.txt"); } else { - urlTxt = new File(downloadedFile.getName() + "url.txt"); + new File(downloadedFile.getName() + "url.txt"); } } - private static void copyToLocalRepo(String projectName, File downloadedFile) { + private static void copyToLocalRepo(final String projectName, final File downloadedFile) { + val newProjectName = projectName != null ? projectName : "thirdParty"; try { - if (projectName == null) { - projectName = "thirdParty"; - } - File localRepoFolder = new File(Reference.userhome + projectName); + val localRepoFolder = new File(Reference.userhome + newProjectName); FileUtils.copyFileToDirectory(downloadedFile, localRepoFolder); - } catch (IOException e) { - logger.error("Could not copy " + projectName + " to local repo", e); + } catch (final IOException e) { + logger.error(String.format("Could not copy {} to local repo.", newProjectName), e); } } - private static void copyFromLocalRepo(String projectName, String fileName, String folder) { + private static void copyFromLocalRepo(String projectName, final String fileName, final String folder) { try { if (projectName == null) { projectName = "thirdParty"; } - File localRepoMod = new File(Reference.userhome + projectName + File.separator + fileName); + final File localRepoMod = new File(Reference.userhome + projectName + File.separator + fileName); FileUtils.copyFileToDirectory(localRepoMod, new File(folder)); - } catch (IOException e) { + } catch (final IOException e) { logger.error("Could not copy " + projectName + " from local repo", e); } } - private static boolean isInLocalRepo(String projectName, String fileName) { + private static boolean isInLocalRepo(String projectName, final String fileName) { if (projectName == null) { projectName = "thirdParty"; } - File localCheck = new File(Reference.userhome + projectName + File.separator + fileName); + final File localCheck = new File(Reference.userhome + projectName + File.separator + fileName); return localCheck.exists(); } - private static String getCurseForgeDownloadLocation(String url, String projectName, String downloadLocation) - throws IOException, MalformedURLException { - final String jarext = ".jar"; - if (downloadLocation.indexOf(jarext) == -1) { - url = url + Reference.COOKIE_TEST_1; - HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection()); - con.setInstanceFollowRedirects(false); - con.connect(); - String actualURL = con.getURL().toString(); + private static String getCurseForgeDownloadLocation(final String url, final String projectName, + final String downloadLocation) throws IOException, MalformedURLException { + String encodedDownloadLocation = URLHelper.encodeSpaces(downloadLocation); + + if (encodedDownloadLocation.indexOf(Reference.JAR_FILE_EXT) == -1) { + val newUrl = url + Reference.COOKIE_TEST_1; + + HttpURLConnection conn = (HttpURLConnection) new URL(newUrl).openConnection(); + conn.setInstanceFollowRedirects(false); + conn.connect(); + + String actualURL = conn.getURL().toString(); int retryCount = 0; - String headerLocation; - while (con.getResponseCode() != 200 || actualURL.indexOf(jarext) == -1) { - headerLocation = con.getHeaderField("Location"); + + while (conn.getResponseCode() != 200 || actualURL.indexOf(Reference.JAR_FILE_EXT) == -1) { + val headerLocation = conn.getHeaderField("Location"); if (headerLocation != null) { actualURL = headerLocation; } else { - actualURL = con.getURL().toString(); + actualURL = conn.getURL().toString(); } + if (retryCount > Reference.RETRY_COUNTER) { break; } - con = (HttpURLConnection) (new URL(url).openConnection()); + + conn = (HttpURLConnection) new URL(newUrl).openConnection(); retryCount++; } - if (actualURL.substring(actualURL.lastIndexOf('/') + 1).indexOf(jarext) != -1) - downloadLocation = actualURL.substring(actualURL.lastIndexOf('/') + 1); - else - downloadLocation = projectName + jarext; + if (actualURL.substring(actualURL.lastIndexOf(Reference.URL_DELIMITER) + 1) + .indexOf(Reference.JAR_FILE_EXT) != -1) { + encodedDownloadLocation = actualURL.substring(actualURL.lastIndexOf(Reference.URL_DELIMITER) + 1); + } else { + encodedDownloadLocation = projectName + Reference.JAR_FILE_EXT; + } } - return downloadLocation.replace("%20", " "); + return URLHelper.decodeSpaces(encodedDownloadLocation); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java index 94e155c..bf2166e 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -1,5 +1,9 @@ package com.nincraft.modpackdownloader; +import static com.nincraft.modpackdownloader.util.Reference.COOKIE_TEST_1; +import static com.nincraft.modpackdownloader.util.Reference.CURSEFORGE_BASE_URL; +import static com.nincraft.modpackdownloader.util.Reference.DATE_FORMATS; + import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; @@ -10,7 +14,6 @@ import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.Iterator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -21,126 +24,118 @@ import com.nincraft.modpackdownloader.util.Reference; -public class ModUpdater { +import lombok.val; +public class ModUpdater { static Logger logger = LogManager.getRootLogger(); - private static final String[] formats = { "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss'Z'", - "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", - "yyyy-MM-dd HH:mm:ss", "MM/dd/yyyy HH:mm:ss", "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'", "MM/dd/yyyy'T'HH:mm:ss.SSSZ", - "MM/dd/yyyy'T'HH:mm:ss.SSS", "MM/dd/yyyy'T'HH:mm:ssZ", "MM/dd/yyyy'T'HH:mm:ss", "yyyy:MM:dd HH:mm:ss", }; - - public static void updateCurseMods(String manifestFile, String mcVersion, String releaseType) { + public static void updateCurseMods(final String manifestFile, final String mcVersion, final String releaseType) { try { - Long projectID; - Long fileID; - JSONParser parser = new JSONParser(); - JSONObject jsons = (JSONObject) parser.parse(new FileReader(manifestFile)); - JSONArray fileList = (JSONArray) jsons.get("curseFiles"); - if (fileList == null) { - fileList = (JSONArray) jsons.get("files"); - } + val jsons = (JSONObject) new JSONParser().parse(new FileReader(manifestFile)); + val fileList = (JSONArray) (jsons.containsKey("curseFiles") ? jsons.get("curseFiles") : jsons.get("files")); + if (fileList != null) { - Iterator iterator = fileList.iterator(); logger.info("Checking for updates from " + fileList.size() + " mods"); - while (iterator.hasNext()) { - JSONObject modJson = (JSONObject) iterator.next(); - projectID = (Long) modJson.get("projectID"); - fileID = (Long) modJson.get("fileID"); - String url = Reference.CURSEFORGE_BASE_URL + projectID + Reference.COOKIE_TEST_1; - HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection()); + for (val file : fileList) { + val modJson = (JSONObject) file; + val projectID = (Long) modJson.get("projectID"); + val fileID = (Long) modJson.get("fileID"); + val url = CURSEFORGE_BASE_URL + projectID + COOKIE_TEST_1; + + final HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setInstanceFollowRedirects(false); con.connect(); - String location = con.getHeaderField("Location"); - String projectName = location.split("/")[2]; - JSONParser projectParser = new JSONParser(); - JSONObject projectJson = getCurseProjectJson(projectID, projectName, projectParser); - JSONObject fileListJson = (JSONObject) projectJson.get("files"); + + val location = con.getHeaderField("Location"); + val projectName = location.split("/")[2]; + val projectParser = new JSONParser(); + val fileListJson = (JSONObject) getCurseProjectJson(projectID, projectName, projectParser) + .get("files"); + if (fileListJson == null) { - logger.error("No file list found for " + projectName); + logger.error(String.format("No file list found for {}.", projectName)); return; } + Date lastDate = null; Long mostRecent = fileID; String mostRecentFile = null; String currentFile = null; - for (Object thing : fileListJson.keySet()) { - JSONObject file = (JSONObject) fileListJson.get(thing); - Date date = parseDate((String) file.get("created_at")); + + for (val thing : fileListJson.values()) { + val mod = (JSONObject) fileListJson.get(thing); + val date = parseDate((String) mod.get("created_at")); + if (lastDate == null) { lastDate = date; } - if (lastDate.before(date) && equalOrLessThan((String) file.get("type"), releaseType) - && file.get("version").equals(mcVersion)) { - mostRecent = (Long) file.get("id"); - mostRecentFile = (String) file.get("name"); + + if (lastDate.before(date) && equalOrLessThan((String) mod.get("type"), releaseType) + && mod.get("version").equals(mcVersion)) { + mostRecent = (Long) mod.get("id"); + mostRecentFile = (String) mod.get("name"); lastDate = date; } - if (fileID.equals((Long) file.get("id"))) { - currentFile = (String) file.get("name"); + + if (fileID.equals(mod.get("id"))) { + currentFile = (String) mod.get("name"); } } + if (!mostRecent.equals(fileID)) { - logger.info("Update found for " + projectName + ". Most recent version is " + mostRecentFile - + ". Old version was " + currentFile); + logger.info( + String.format("Update found for {}. Most recent version is {}. Old version was {}.", + projectName, mostRecentFile, currentFile)); modJson.remove("fileID"); modJson.put("fileID", mostRecent); } + if (!modJson.containsKey("name")) { modJson.put("name", projectName); } } } - FileWriter file = new FileWriter(manifestFile); + + val file = new FileWriter(manifestFile); + try { file.write(jsons.toJSONString()); } finally { file.flush(); file.close(); } - - } catch (FileNotFoundException e) { - logger.error(e.getMessage()); - } catch (IOException e) { + } catch (final IOException e) { logger.error(e.getMessage()); - } catch (ParseException e) { + } catch (final ParseException e) { logger.error(e.getMessage()); } } - private static boolean equalOrLessThan(String modRelease, String releaseType) { - if (releaseType.equals(modRelease) || "beta".equals(releaseType) && "release".equals(modRelease)) { - return true; - } - return false; + private static boolean equalOrLessThan(final String modRelease, final String releaseType) { + return releaseType.equals(modRelease) || "beta".equals(releaseType) && "release".equals(modRelease); } - private static JSONObject getCurseProjectJson(Long projectID, String projectName, JSONParser projectParser) - throws ParseException, IOException { + private static JSONObject getCurseProjectJson(final Long projectID, final String projectName, + final JSONParser projectParser) throws ParseException, IOException { try { return (JSONObject) projectParser.parse(new BufferedReader(new InputStreamReader( - new URL("http://widget.mcf.li/mc-mods/minecraft/" + projectName + ".json").openStream()))); - } catch (FileNotFoundException e) { + new URL(String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectName)).openStream()))); + } catch (final FileNotFoundException e) { return (JSONObject) projectParser.parse(new BufferedReader(new InputStreamReader( - new URL("http://widget.mcf.li/mc-mods/minecraft/" + projectID + "-" + projectName + ".json") + new URL(String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectID + "-" + projectName)) .openStream()))); } } - private static Date parseDate(String date) { - Date d = null; - if (date != null) { - for (String parse : formats) { - SimpleDateFormat sdf = new SimpleDateFormat(parse); - try { - d = sdf.parse(date); - } catch (java.text.ParseException e) { - - } + private static Date parseDate(final String date) { + for (val parse : DATE_FORMATS) { + try { + return new SimpleDateFormat(parse).parse(date); + } catch (final java.text.ParseException e) { } } - return d; + return null; } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java new file mode 100644 index 0000000..0aea2fe --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java @@ -0,0 +1,32 @@ +package com.nincraft.modpackdownloader.container; + +import static com.nincraft.modpackdownloader.util.Reference.COOKIE_TEST_1; +import static com.nincraft.modpackdownloader.util.Reference.CURSEFORGE_BASE_URL; + +import org.json.simple.JSONObject; + +import com.nincraft.modpackdownloader.util.ModType; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = false) +public class CurseMod extends ModContainer { + private final String projectURL; + + public CurseMod(final JSONObject modJson) { + super(modJson, ModType.CURSE); + projectURL = buildProjectUrl(modJson); + } + + private String buildProjectUrl(final JSONObject modJson) { + return String.format(CURSEFORGE_BASE_URL + "{}" + COOKIE_TEST_1, modJson.get("projectID")); + } + + @Override + String buildUrl(final JSONObject modJson) { + return String.format(CURSEFORGE_BASE_URL + "{}/files/{}/download", modJson.get("projectID"), + modJson.get("fileID")); + } +} diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java index c9956cd..e6eb996 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java @@ -7,14 +7,7 @@ import lombok.Data; @Data -public class ModContainer { - public ModContainer(JSONObject modJson, ModType type) { - if (type.equals(ModType.CURSE)) { - setProjectId((Long) modJson.get("projectID")); - setFileId((Long) modJson.get("fileID")); - } - } - +public abstract class ModContainer { private String modName; private Long projectId; private Long fileId; @@ -22,4 +15,15 @@ public ModContainer(JSONObject modJson, ModType type) { private String rename; private String fileName; private String folder; + private String url; + + public ModContainer(final JSONObject modJson, final ModType type) { + if (type.equals(ModType.CURSE)) { + setProjectId((Long) modJson.get("projectID")); + setFileId((Long) modJson.get("fileID")); + setUrl(buildUrl(modJson)); + } + } + + abstract String buildUrl(final JSONObject modJson); } diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java new file mode 100644 index 0000000..d0286d4 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -0,0 +1,5 @@ +package com.nincraft.modpackdownloader.handler; + +public class CurseModHandler extends ModHandler { + +} diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java new file mode 100644 index 0000000..394320a --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java @@ -0,0 +1,5 @@ +package com.nincraft.modpackdownloader.handler; + +public abstract class ModHandler { + +} diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index bf0fd3e..d2e451b 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -2,11 +2,18 @@ public class Reference { public static final String CURSEFORGE_BASE_URL = "http://minecraft.curseforge.com/projects/"; + public static final String CURSEFORGE_WIDGET_JSON_URL = "http://widget.mcf.li/mc-mods/minecraft/{}.json"; public static final String COOKIE_TEST_1 = "?cookieTest=1"; - public static final String windowsFolder = "\\.modpackdownloader\\"; - public static final String macFolder = "/Library/Application Support/modpackdownloader/"; - public static final String otherFolder = "/.modpackdownloader/"; + public static final String WINDOWS_FOLDER = "\\.modpackdownloader\\"; + public static final String MAC_FOLDER = "/Library/Application Support/modpackdownloader/"; + public static final String OTHER_FOLDER = "/.modpackdownloader/"; + public static final String JAR_FILE_EXT = ".jar"; + public static final String[] DATE_FORMATS = { "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss'Z'", + "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", + "yyyy-MM-dd HH:mm:ss", "MM/dd/yyyy HH:mm:ss", "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'", "MM/dd/yyyy'T'HH:mm:ss.SSSZ", + "MM/dd/yyyy'T'HH:mm:ss.SSS", "MM/dd/yyyy'T'HH:mm:ssZ", "MM/dd/yyyy'T'HH:mm:ss", "yyyy:MM:dd HH:mm:ss", }; public static final int RETRY_COUNTER = 5; + public static final char URL_DELIMITER = '/'; public static String userhome; public static String os; public static String manifestFile; diff --git a/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java new file mode 100644 index 0000000..11750a3 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java @@ -0,0 +1,14 @@ +package com.nincraft.modpackdownloader.util; + +public class URLHelper { + private static final String WHITESPACE = " "; + private static final String WHITESPACE_ENCODED = "%20"; + + public static String encodeSpaces(final String url) { + return url.replace(WHITESPACE, WHITESPACE_ENCODED); + } + + public static String decodeSpaces(final String url) { + return url.replace(WHITESPACE_ENCODED, WHITESPACE); + } +} From 491518a85e8fa21ceb294a22a6f3ae41f083867f Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Wed, 24 Feb 2016 19:26:46 -0600 Subject: [PATCH 11/86] Add Some Logging Statements Also cleaned up code a bit, and added a ThirdPartyMod class to mirror the CurseMod class. --- .../modpackdownloader/ModPackDownloader.java | 22 +++++++++++++++++-- .../modpackdownloader/container/CurseMod.java | 17 +++++++------- .../container/ModContainer.java | 10 ++------- .../container/ThirdPartyMod.java | 20 +++++++++++++++++ 4 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index d687425..87814f4 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -40,12 +40,17 @@ public static void main(final String[] args) { } setupRepo(); + if (Reference.updateMods) { + logger.info(String.format("Updating mods with parameters: {}, {}, {}", Reference.manifestFile, + Reference.mcVersion, Reference.releaseType)); ModUpdater.updateCurseMods(Reference.manifestFile, Reference.mcVersion, Reference.releaseType); + logger.info("Finished updating mods."); } else { - logger.info("Starting download with parameters: " + Reference.manifestFile + ", " + Reference.modFolder); + logger.info(String.format("Starting download with parameters: {}, {}", Reference.manifestFile, + Reference.modFolder)); downloadMods(Reference.manifestFile, Reference.modFolder); - logger.info("Finished downloading mods"); + logger.info("Finished downloading mods."); } } @@ -63,20 +68,30 @@ private static void processArguments(final String[] args) { private static void processArgument(final String arg) { if (arg.equals("-forceDownload")) { Reference.forceDownload = true; + logger.debug("Downloads are now being forced."); } else if (arg.equals("-updateMods")) { Reference.updateMods = true; + logger.debug("mods will be updated instead of downloaded."); } else if (arg.startsWith("-mcVersion")) { Reference.mcVersion = arg.substring(arg.lastIndexOf("=") + 1); + logger.debug(String.format("Minecraft Version set to: {}", Reference.mcVersion)); } else if (arg.startsWith("-releaseType")) { Reference.releaseType = arg.substring(arg.lastIndexOf("=") + 1); + logger.debug(String.format("Checking against mod release type: {}", Reference.releaseType)); } else if (arg.equals("-generateUrlTxt")) { Reference.generateUrlTxt = true; + logger.debug("Mod URL Text files will now be generated."); } } private static void setupRepo() { + logger.info("Setting up local repository..."); Reference.userhome = System.getProperty("user.home"); + logger.debug(String.format("User Home System Property detected as: {}", Reference.userhome)); + Reference.os = System.getProperty("os.name"); + logger.debug(String.format("Operating System detected as: {}", Reference.os)); + if (Reference.os.startsWith("Windows")) { Reference.userhome += Reference.WINDOWS_FOLDER; } else if (Reference.os.startsWith("Mac")) { @@ -84,7 +99,10 @@ private static void setupRepo() { } else { Reference.userhome += Reference.OTHER_FOLDER; } + logger.debug(String.format("User Home Folder set to: {}", Reference.userhome)); + createFolder(Reference.userhome); + logger.info("Finished setting up local repository."); } private static void downloadMods(final String manifestFile, final String modFolder) { diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java index 0aea2fe..df8ab0a 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java @@ -1,11 +1,8 @@ package com.nincraft.modpackdownloader.container; -import static com.nincraft.modpackdownloader.util.Reference.COOKIE_TEST_1; -import static com.nincraft.modpackdownloader.util.Reference.CURSEFORGE_BASE_URL; - import org.json.simple.JSONObject; -import com.nincraft.modpackdownloader.util.ModType; +import com.nincraft.modpackdownloader.util.Reference; import lombok.Data; import lombok.EqualsAndHashCode; @@ -13,20 +10,22 @@ @Data @EqualsAndHashCode(callSuper = false) public class CurseMod extends ModContainer { - private final String projectURL; + private String projectURL; public CurseMod(final JSONObject modJson) { - super(modJson, ModType.CURSE); - projectURL = buildProjectUrl(modJson); + super(modJson); + setProjectId((Long) modJson.get("projectID")); + setFileId((Long) modJson.get("fileID")); + setProjectURL(buildProjectUrl(modJson)); } private String buildProjectUrl(final JSONObject modJson) { - return String.format(CURSEFORGE_BASE_URL + "{}" + COOKIE_TEST_1, modJson.get("projectID")); + return String.format(Reference.CURSEFORGE_BASE_URL + "{}" + Reference.COOKIE_TEST_1, modJson.get("projectID")); } @Override String buildUrl(final JSONObject modJson) { - return String.format(CURSEFORGE_BASE_URL + "{}/files/{}/download", modJson.get("projectID"), + return String.format(Reference.CURSEFORGE_BASE_URL + "{}/files/{}/download", modJson.get("projectID"), modJson.get("fileID")); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java index e6eb996..daed884 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java @@ -2,8 +2,6 @@ import org.json.simple.JSONObject; -import com.nincraft.modpackdownloader.util.ModType; - import lombok.Data; @Data @@ -17,12 +15,8 @@ public abstract class ModContainer { private String folder; private String url; - public ModContainer(final JSONObject modJson, final ModType type) { - if (type.equals(ModType.CURSE)) { - setProjectId((Long) modJson.get("projectID")); - setFileId((Long) modJson.get("fileID")); - setUrl(buildUrl(modJson)); - } + public ModContainer(final JSONObject modJson) { + setUrl(buildUrl(modJson)); } abstract String buildUrl(final JSONObject modJson); diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java new file mode 100644 index 0000000..2d2eea9 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java @@ -0,0 +1,20 @@ +package com.nincraft.modpackdownloader.container; + +import org.json.simple.JSONObject; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = false) +public class ThirdPartyMod extends ModContainer { + + public ThirdPartyMod(final JSONObject modJson) { + super(modJson); + } + + @Override + String buildUrl(final JSONObject modJson) { + return modJson.get("url").toString(); + } +} From ee09e8b67fd0175651d72ba03288e00aabacb24a Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 24 Feb 2016 21:05:01 -0600 Subject: [PATCH 12/86] Fix formatting and download URL errors --- .../modpackdownloader/ModPackDownloader.java | 30 +++++++++---------- .../modpackdownloader/container/CurseMod.java | 18 ++++++----- .../container/ModContainer.java | 4 +-- .../container/ThirdPartyMod.java | 8 ++--- .../modpackdownloader/util/ModType.java | 5 ---- 5 files changed, 30 insertions(+), 35 deletions(-) delete mode 100644 src/main/java/com/nincraft/modpackdownloader/util/ModType.java diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 87814f4..d1d86cc 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -42,12 +42,12 @@ public static void main(final String[] args) { setupRepo(); if (Reference.updateMods) { - logger.info(String.format("Updating mods with parameters: {}, {}, {}", Reference.manifestFile, + logger.info(String.format("Updating mods with parameters: %s, %s, %s", Reference.manifestFile, Reference.mcVersion, Reference.releaseType)); ModUpdater.updateCurseMods(Reference.manifestFile, Reference.mcVersion, Reference.releaseType); logger.info("Finished updating mods."); } else { - logger.info(String.format("Starting download with parameters: {}, {}", Reference.manifestFile, + logger.info(String.format("Starting download with parameters: %s, %s", Reference.manifestFile, Reference.modFolder)); downloadMods(Reference.manifestFile, Reference.modFolder); logger.info("Finished downloading mods."); @@ -74,10 +74,10 @@ private static void processArgument(final String arg) { logger.debug("mods will be updated instead of downloaded."); } else if (arg.startsWith("-mcVersion")) { Reference.mcVersion = arg.substring(arg.lastIndexOf("=") + 1); - logger.debug(String.format("Minecraft Version set to: {}", Reference.mcVersion)); + logger.debug(String.format("Minecraft Version set to: %s", Reference.mcVersion)); } else if (arg.startsWith("-releaseType")) { Reference.releaseType = arg.substring(arg.lastIndexOf("=") + 1); - logger.debug(String.format("Checking against mod release type: {}", Reference.releaseType)); + logger.debug(String.format("Checking against mod release type: %s", Reference.releaseType)); } else if (arg.equals("-generateUrlTxt")) { Reference.generateUrlTxt = true; logger.debug("Mod URL Text files will now be generated."); @@ -87,10 +87,10 @@ private static void processArgument(final String arg) { private static void setupRepo() { logger.info("Setting up local repository..."); Reference.userhome = System.getProperty("user.home"); - logger.debug(String.format("User Home System Property detected as: {}", Reference.userhome)); + logger.debug(String.format("User Home System Property detected as: %s", Reference.userhome)); Reference.os = System.getProperty("os.name"); - logger.debug(String.format("Operating System detected as: {}", Reference.os)); + logger.debug(String.format("Operating System detected as: %s", Reference.os)); if (Reference.os.startsWith("Windows")) { Reference.userhome += Reference.WINDOWS_FOLDER; @@ -99,7 +99,7 @@ private static void setupRepo() { } else { Reference.userhome += Reference.OTHER_FOLDER; } - logger.debug(String.format("User Home Folder set to: {}", Reference.userhome)); + logger.debug(String.format("User Home Folder set to: %s", Reference.userhome)); createFolder(Reference.userhome); logger.info("Finished setting up local repository."); @@ -128,7 +128,7 @@ private static void downloadThirdPartyMods(final String manifestFile, final Stri : (String) urlJson.get("rename"); logger.info( - String.format("Downloading {}. Mod {} of {}", fileName, DOWNLOAD_COUNT, urlList.size())); + String.format("Downloading %s. Mod %s of %s", fileName, DOWNLOAD_COUNT, urlList.size())); downloadFile(url, modFolder, fileName, projectName, false); DOWNLOAD_COUNT++; } @@ -145,7 +145,7 @@ private static void downloadCurseMods(final String manifestFile, final String mo : jsonObject.get("files")); if (fileList != null) { - logger.info(String.format("Starting download of {} mods from Curse.", fileList.size())); + logger.info(String.format("Starting download of %s mods from Curse.", fileList.size())); DOWNLOAD_COUNT = 1; for (val file : fileList) { val mod = new CurseMod((JSONObject) file); @@ -157,7 +157,7 @@ private static void downloadCurseMods(final String manifestFile, final String mo mod.setFolder(modFolder); mod.setProjectName(conn.getHeaderField("Location").split("/")[2]); - logger.info(String.format("Downloading {}. Mod {} of {}", mod.getProjectName(), DOWNLOAD_COUNT, + logger.info(String.format("Downloading %s. Mod %s of %s", mod.getProjectName(), DOWNLOAD_COUNT, fileList.size())); downloadCurseForgeFile(mod); DOWNLOAD_COUNT++; @@ -182,14 +182,14 @@ private static void createFolder(final String folder) { private static void downloadCurseForgeFile(final ModContainer mod) { try { val fileName = mod.getRename() == null - ? getCurseForgeDownloadLocation(mod.getUrl(), mod.getProjectName(), mod.getProjectName()) + ? getCurseForgeDownloadLocation(mod.getDownloadUrl(), mod.getProjectName(), mod.getProjectName()) : mod.getRename(); - downloadFile(mod.getUrl(), mod.getFolder(), fileName, mod.getProjectName(), false); + downloadFile(mod.getDownloadUrl(), mod.getFolder(), fileName, mod.getProjectName(), false); } catch (final MalformedURLException e) { logger.error(e.getMessage()); } catch (final FileNotFoundException e) { - logger.error(String.format("Could not find: {}", mod.getProjectName()), e); + logger.error(String.format("Could not find: %s", mod.getProjectName()), e); } catch (final IOException e) { logger.error(e.getMessage()); } @@ -233,7 +233,7 @@ private static void downloadFile(final String url, final String folder, final St } catch (final IOException e) { if (!useUserAgent) { logger.warn( - String.format("Error getting {}. Attempting to redownload using alternate method.", fileName)); + String.format("Error getting %s. Attempting to redownload using alternate method.", fileName)); downloadFile(url, folder, fileName, projectName, true); } else { logger.error("Could not download " + fileName, e.getMessage()); @@ -255,7 +255,7 @@ private static void copyToLocalRepo(final String projectName, final File downloa val localRepoFolder = new File(Reference.userhome + newProjectName); FileUtils.copyFileToDirectory(downloadedFile, localRepoFolder); } catch (final IOException e) { - logger.error(String.format("Could not copy {} to local repo.", newProjectName), e); + logger.error(String.format("Could not copy %s to local repo.", newProjectName), e); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java index df8ab0a..30236d4 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java @@ -16,16 +16,20 @@ public CurseMod(final JSONObject modJson) { super(modJson); setProjectId((Long) modJson.get("projectID")); setFileId((Long) modJson.get("fileID")); - setProjectURL(buildProjectUrl(modJson)); + setProjectURL(buildProjectUrl()); } - private String buildProjectUrl(final JSONObject modJson) { - return String.format(Reference.CURSEFORGE_BASE_URL + "{}" + Reference.COOKIE_TEST_1, modJson.get("projectID")); + @Override + public String getDownloadUrl() { + return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectId(), + getProjectName(), getFileId()); } - @Override - String buildUrl(final JSONObject modJson) { - return String.format(Reference.CURSEFORGE_BASE_URL + "{}/files/{}/download", modJson.get("projectID"), - modJson.get("fileID")); + String getAlternateDownloadUrl(final JSONObject modJson) { + return String.format(Reference.CURSEFORGE_BASE_URL + "%s/files/%s/download", getProjectName(), getFileId()); + } + + String buildProjectUrl() { + return String.format(Reference.CURSEFORGE_BASE_URL + "%s" + Reference.COOKIE_TEST_1, getProjectId()); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java index daed884..4f0e56a 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java @@ -14,10 +14,10 @@ public abstract class ModContainer { private String fileName; private String folder; private String url; + private String downloadUrl; public ModContainer(final JSONObject modJson) { - setUrl(buildUrl(modJson)); + } - abstract String buildUrl(final JSONObject modJson); } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java index 2d2eea9..06d48a0 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java @@ -8,13 +8,9 @@ @Data @EqualsAndHashCode(callSuper = false) public class ThirdPartyMod extends ModContainer { - + public ThirdPartyMod(final JSONObject modJson) { super(modJson); - } - - @Override - String buildUrl(final JSONObject modJson) { - return modJson.get("url").toString(); + setDownloadUrl(modJson.get("url").toString()); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/util/ModType.java b/src/main/java/com/nincraft/modpackdownloader/util/ModType.java deleted file mode 100644 index 18a2268..0000000 --- a/src/main/java/com/nincraft/modpackdownloader/util/ModType.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.nincraft.modpackdownloader.util; - -public enum ModType { - CURSE, THIRDPARTY -} From ce0b757f61ae649ab8ae72cea382f210c3a67b11 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 24 Feb 2016 23:15:00 -0600 Subject: [PATCH 13/86] Refactor --- .../modpackdownloader/ModPackDownloader.java | 68 +++++++++---------- .../container/ModContainer.java | 3 +- .../container/ThirdPartyMod.java | 7 +- 3 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index d1d86cc..ad211ab 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -21,6 +21,7 @@ import com.nincraft.modpackdownloader.container.CurseMod; import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.container.ThirdPartyMod; import com.nincraft.modpackdownloader.util.Reference; import com.nincraft.modpackdownloader.util.URLHelper; @@ -106,43 +107,40 @@ private static void setupRepo() { } private static void downloadMods(final String manifestFile, final String modFolder) { - downloadCurseMods(manifestFile, modFolder); - downloadThirdPartyMods(manifestFile, modFolder); - } - - private static void downloadThirdPartyMods(final String manifestFile, final String modFolder) { try { - val jsons = (JSONObject) new JSONParser().parse(new FileReader(manifestFile)); - val urlList = (JSONArray) jsons.get("thirdParty"); - - if (urlList != null) { - logger.info("Starting download of " + urlList.size() + " 3rd party mods"); - DOWNLOAD_COUNT = 1; + val jsonList = (JSONObject) new JSONParser().parse(new FileReader(manifestFile)); + downloadCurseMods(jsonList, modFolder); + downloadThirdPartyMods(jsonList, modFolder); + } catch (IOException | ParseException e) { + logger.error(e.getMessage()); + } + } - for (val item : urlList) { - val urlJson = (JSONObject) item; - val url = (String) urlJson.get("url"); - val projectName = (String) urlJson.get("name"); - val fileName = urlJson.get("rename") == null - ? url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".jar") + 4) - : (String) urlJson.get("rename"); - - logger.info( - String.format("Downloading %s. Mod %s of %s", fileName, DOWNLOAD_COUNT, urlList.size())); - downloadFile(url, modFolder, fileName, projectName, false); - DOWNLOAD_COUNT++; + private static void downloadThirdPartyMods(final JSONObject jsonList, final String modFolder) { + val urlList = (JSONArray) jsonList.get("thirdParty"); + + if (urlList != null) { + logger.info("Starting download of " + urlList.size() + " 3rd party mods"); + DOWNLOAD_COUNT = 1; + + for (val item : urlList) { + val mod = new ThirdPartyMod((JSONObject) item); + logger.info(String.format("Downloading %s. Mod %s of %s", mod.getFileName(), DOWNLOAD_COUNT, + urlList.size())); + try { + downloadFile(mod.getDownloadUrl(), modFolder, mod.getFileName(), mod.getProjectName(), false); + } catch (MalformedURLException | FileNotFoundException e) { + logger.error(e.getMessage()); } + DOWNLOAD_COUNT++; } - } catch (final IOException | ParseException e) { - logger.error(e.getMessage()); } } - private static void downloadCurseMods(final String manifestFile, final String modFolder) { + private static void downloadCurseMods(final JSONObject jsonList, final String modFolder) { try { - val jsonObject = (JSONObject) new JSONParser().parse(new FileReader(manifestFile)); - val fileList = (JSONArray) (jsonObject.containsKey("curseFiles") ? jsonObject.get("curseFiles") - : jsonObject.get("files")); + val fileList = (JSONArray) (jsonList.containsKey("curseFiles") ? jsonList.get("curseFiles") + : jsonList.get("files")); if (fileList != null) { logger.info(String.format("Starting download of %s mods from Curse.", fileList.size())); @@ -165,8 +163,6 @@ private static void downloadCurseMods(final String manifestFile, final String mo } } catch (final IOException e) { logger.error(e.getMessage()); - } catch (final ParseException e) { - logger.error(e.getMessage()); } } @@ -259,15 +255,13 @@ private static void copyToLocalRepo(final String projectName, final File downloa } } - private static void copyFromLocalRepo(String projectName, final String fileName, final String folder) { + private static void copyFromLocalRepo(final String projectName, final String fileName, final String folder) { + val newProjectName = projectName != null ? projectName : "thirdParty"; try { - if (projectName == null) { - projectName = "thirdParty"; - } - final File localRepoMod = new File(Reference.userhome + projectName + File.separator + fileName); + final File localRepoMod = new File(Reference.userhome + newProjectName + File.separator + fileName); FileUtils.copyFileToDirectory(localRepoMod, new File(folder)); } catch (final IOException e) { - logger.error("Could not copy " + projectName + " from local repo", e); + logger.error("Could not copy " + newProjectName + " from local repo", e); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java index 4f0e56a..f2a69bc 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java @@ -13,11 +13,10 @@ public abstract class ModContainer { private String rename; private String fileName; private String folder; - private String url; private String downloadUrl; public ModContainer(final JSONObject modJson) { - + setRename(modJson.get("rename").toString()); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java index 06d48a0..b020e02 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java @@ -8,9 +8,14 @@ @Data @EqualsAndHashCode(callSuper = false) public class ThirdPartyMod extends ModContainer { - + + private String fileName; + public ThirdPartyMod(final JSONObject modJson) { super(modJson); setDownloadUrl(modJson.get("url").toString()); + setProjectName(modJson.get("name").toString()); + setFileName(getRename() == null ? getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, + getDownloadUrl().lastIndexOf(".jar") + 4) : getRename()); } } From 277e4fdb7766b6bc93bf079d8936a7e339410cfd Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Thu, 25 Feb 2016 03:13:16 -0600 Subject: [PATCH 14/86] Make downloading multi threaded In the sense that multiple downloads can happen at once. There is currently not a limit so all the mods will basically all try to download at the same time. Probably bad. --- .../modpackdownloader/ModPackDownloader.java | 60 +++++++++++++------ .../container/ModContainer.java | 2 +- .../container/ThirdPartyMod.java | 10 ++-- 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index ad211ab..79a45a9 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -29,7 +29,11 @@ public class ModPackDownloader { - private static int DOWNLOAD_COUNT = 1; + private static int CURSE_DOWNLOAD_COUNT = 0; + private static int CURSE_DOWNLOAD_TOTAL = 0; + private static int THIRD_PARTY_DOWNLOAD_COUNT = 0; + private static int THIRD_PARTY_DOWNLOAD_TOTAL = 0; + static Logger logger = LogManager.getRootLogger(); public static void main(final String[] args) { @@ -51,10 +55,16 @@ public static void main(final String[] args) { logger.info(String.format("Starting download with parameters: %s, %s", Reference.manifestFile, Reference.modFolder)); downloadMods(Reference.manifestFile, Reference.modFolder); + while (!checkFinished()) { + } logger.info("Finished downloading mods."); } } + private static boolean checkFinished() { + return CURSE_DOWNLOAD_COUNT == CURSE_DOWNLOAD_TOTAL && THIRD_PARTY_DOWNLOAD_COUNT == THIRD_PARTY_DOWNLOAD_TOTAL; + } + private static void processArguments(final String[] args) { Reference.manifestFile = args[0]; Reference.modFolder = args[1]; @@ -86,7 +96,7 @@ private static void processArgument(final String arg) { } private static void setupRepo() { - logger.info("Setting up local repository..."); + logger.trace("Setting up local repository..."); Reference.userhome = System.getProperty("user.home"); logger.debug(String.format("User Home System Property detected as: %s", Reference.userhome)); @@ -103,7 +113,7 @@ private static void setupRepo() { logger.debug(String.format("User Home Folder set to: %s", Reference.userhome)); createFolder(Reference.userhome); - logger.info("Finished setting up local repository."); + logger.trace("Finished setting up local repository."); } private static void downloadMods(final String manifestFile, final String modFolder) { @@ -121,18 +131,27 @@ private static void downloadThirdPartyMods(final JSONObject jsonList, final Stri if (urlList != null) { logger.info("Starting download of " + urlList.size() + " 3rd party mods"); - DOWNLOAD_COUNT = 1; + int thirdPartyCount = 1; for (val item : urlList) { val mod = new ThirdPartyMod((JSONObject) item); - logger.info(String.format("Downloading %s. Mod %s of %s", mod.getFileName(), DOWNLOAD_COUNT, - urlList.size())); - try { - downloadFile(mod.getDownloadUrl(), modFolder, mod.getFileName(), mod.getProjectName(), false); - } catch (MalformedURLException | FileNotFoundException e) { - logger.error(e.getMessage()); - } - DOWNLOAD_COUNT++; + THIRD_PARTY_DOWNLOAD_TOTAL = urlList.size(); + logger.info(String.format("Downloading %s. Mod %s of %s", mod.getFileName(), thirdPartyCount, + THIRD_PARTY_DOWNLOAD_TOTAL)); + new Thread(new Runnable() { + public void run() { + try { + downloadFile(mod.getDownloadUrl(), modFolder, mod.getFileName(), mod.getProjectName(), + false); + THIRD_PARTY_DOWNLOAD_COUNT++; + logger.info(String.format("Finished downloading %s", mod.getFileName())); + } catch (MalformedURLException | FileNotFoundException e) { + logger.error(e.getMessage()); + } + } + }).start(); + + thirdPartyCount++; } } } @@ -143,8 +162,9 @@ private static void downloadCurseMods(final JSONObject jsonList, final String mo : jsonList.get("files")); if (fileList != null) { - logger.info(String.format("Starting download of %s mods from Curse.", fileList.size())); - DOWNLOAD_COUNT = 1; + CURSE_DOWNLOAD_TOTAL = fileList.size(); + logger.info(String.format("Starting download of %s mods from Curse.", CURSE_DOWNLOAD_TOTAL)); + int curseCount = 1; for (val file : fileList) { val mod = new CurseMod((JSONObject) file); @@ -155,10 +175,16 @@ private static void downloadCurseMods(final JSONObject jsonList, final String mo mod.setFolder(modFolder); mod.setProjectName(conn.getHeaderField("Location").split("/")[2]); - logger.info(String.format("Downloading %s. Mod %s of %s", mod.getProjectName(), DOWNLOAD_COUNT, + logger.info(String.format("Downloading %s. Mod %s of %s", mod.getProjectName(), curseCount, fileList.size())); - downloadCurseForgeFile(mod); - DOWNLOAD_COUNT++; + new Thread(new Runnable() { + public void run() { + downloadCurseForgeFile(mod); + CURSE_DOWNLOAD_COUNT++; + logger.info(String.format("Finished downloading %s", mod.getProjectName())); + } + }).start(); + curseCount++; } } } catch (final IOException e) { diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java index f2a69bc..4f30d13 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java @@ -16,7 +16,7 @@ public abstract class ModContainer { private String downloadUrl; public ModContainer(final JSONObject modJson) { - setRename(modJson.get("rename").toString()); + setRename((String) modJson.get("rename")); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java index b020e02..0f13d1f 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java @@ -9,13 +9,11 @@ @EqualsAndHashCode(callSuper = false) public class ThirdPartyMod extends ModContainer { - private String fileName; - public ThirdPartyMod(final JSONObject modJson) { super(modJson); - setDownloadUrl(modJson.get("url").toString()); - setProjectName(modJson.get("name").toString()); - setFileName(getRename() == null ? getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, - getDownloadUrl().lastIndexOf(".jar") + 4) : getRename()); + setProjectName((String) modJson.get("name")); + setDownloadUrl((String) modJson.get("url")); + setFileName(getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, + getDownloadUrl().lastIndexOf(".jar") + 4)); } } From 63778a5e16e233b66f1c05b4e7c312621004abb9 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Thu, 25 Feb 2016 03:19:34 -0600 Subject: [PATCH 15/86] Fix weird lag Without this it just sits there forever :| --- .../com/nincraft/modpackdownloader/ModPackDownloader.java | 3 ++- src/main/resources/forge.json | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/forge.json diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 79a45a9..396d6c4 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -36,7 +36,7 @@ public class ModPackDownloader { static Logger logger = LogManager.getRootLogger(); - public static void main(final String[] args) { + public static void main(final String[] args) throws InterruptedException { if (args.length < 2) { logger.error("Arguments required: manifest file location, mod download location"); return; @@ -56,6 +56,7 @@ public static void main(final String[] args) { Reference.modFolder)); downloadMods(Reference.manifestFile, Reference.modFolder); while (!checkFinished()) { + Thread.sleep(1); } logger.info("Finished downloading mods."); } diff --git a/src/main/resources/forge.json b/src/main/resources/forge.json new file mode 100644 index 0000000..8d7aecf --- /dev/null +++ b/src/main/resources/forge.json @@ -0,0 +1,8 @@ +{ + "thirdParty": [ + { + "name": "Forge", + "url": "http://files.minecraftforge.net/maven/net/minecraftforge/forge/1.8.9-11.15.1.1724/forge-1.8.9-11.15.1.1724-installer.jar" + } + ] +} \ No newline at end of file From dfa0d48ab51e98582afa9d52ddc6a68b884ad2b9 Mon Sep 17 00:00:00 2001 From: Undead Zeratul Date: Thu, 25 Feb 2016 10:44:43 -0600 Subject: [PATCH 16/86] Fix Curseforge Widget JSON Url Template --- .../java/com/nincraft/modpackdownloader/util/Reference.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index d2e451b..98259db 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -2,7 +2,7 @@ public class Reference { public static final String CURSEFORGE_BASE_URL = "http://minecraft.curseforge.com/projects/"; - public static final String CURSEFORGE_WIDGET_JSON_URL = "http://widget.mcf.li/mc-mods/minecraft/{}.json"; + public static final String CURSEFORGE_WIDGET_JSON_URL = "http://widget.mcf.li/mc-mods/minecraft/%s.json"; public static final String COOKIE_TEST_1 = "?cookieTest=1"; public static final String WINDOWS_FOLDER = "\\.modpackdownloader\\"; public static final String MAC_FOLDER = "/Library/Application Support/modpackdownloader/"; From abba283eabf34f8ad9730a60a748cc2b353668a8 Mon Sep 17 00:00:00 2001 From: Undead Zeratul Date: Thu, 25 Feb 2016 11:01:30 -0600 Subject: [PATCH 17/86] Clean Up Logging Statements --- .../modpackdownloader/ModPackDownloader.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 396d6c4..0a3f2e2 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -78,6 +78,7 @@ private static void processArguments(final String[] args) { } private static void processArgument(final String arg) { + logger.trace("Processing given arguments..."); if (arg.equals("-forceDownload")) { Reference.forceDownload = true; logger.debug("Downloads are now being forced."); @@ -94,6 +95,7 @@ private static void processArgument(final String arg) { Reference.generateUrlTxt = true; logger.debug("Mod URL Text files will now be generated."); } + logger.trace("Finished processing given arguments."); } private static void setupRepo() { @@ -131,17 +133,18 @@ private static void downloadThirdPartyMods(final JSONObject jsonList, final Stri val urlList = (JSONArray) jsonList.get("thirdParty"); if (urlList != null) { - logger.info("Starting download of " + urlList.size() + " 3rd party mods"); + THIRD_PARTY_DOWNLOAD_TOTAL = urlList.size(); + logger.info(String.format("Starting download of %s thirdparty mods.", THIRD_PARTY_DOWNLOAD_TOTAL)); int thirdPartyCount = 1; for (val item : urlList) { val mod = new ThirdPartyMod((JSONObject) item); - THIRD_PARTY_DOWNLOAD_TOTAL = urlList.size(); - logger.info(String.format("Downloading %s. Mod %s of %s", mod.getFileName(), thirdPartyCount, - THIRD_PARTY_DOWNLOAD_TOTAL)); + new Thread(new Runnable() { public void run() { try { + logger.info(String.format("Downloading %s. Mod %s of %s", mod.getFileName(), thirdPartyCount, + THIRD_PARTY_DOWNLOAD_TOTAL)); downloadFile(mod.getDownloadUrl(), modFolder, mod.getFileName(), mod.getProjectName(), false); THIRD_PARTY_DOWNLOAD_COUNT++; @@ -166,6 +169,7 @@ private static void downloadCurseMods(final JSONObject jsonList, final String mo CURSE_DOWNLOAD_TOTAL = fileList.size(); logger.info(String.format("Starting download of %s mods from Curse.", CURSE_DOWNLOAD_TOTAL)); int curseCount = 1; + for (val file : fileList) { val mod = new CurseMod((JSONObject) file); @@ -176,10 +180,10 @@ private static void downloadCurseMods(final JSONObject jsonList, final String mo mod.setFolder(modFolder); mod.setProjectName(conn.getHeaderField("Location").split("/")[2]); - logger.info(String.format("Downloading %s. Mod %s of %s", mod.getProjectName(), curseCount, - fileList.size())); new Thread(new Runnable() { public void run() { + logger.info(String.format("Downloading %s. Mod %s of %s", mod.getProjectName(), curseCount, + fileList.size())); downloadCurseForgeFile(mod); CURSE_DOWNLOAD_COUNT++; logger.info(String.format("Finished downloading %s", mod.getProjectName())); @@ -259,7 +263,7 @@ private static void downloadFile(final String url, final String folder, final St String.format("Error getting %s. Attempting to redownload using alternate method.", fileName)); downloadFile(url, folder, fileName, projectName, true); } else { - logger.error("Could not download " + fileName, e.getMessage()); + logger.error(String.format("Could not download %s.", fileName), e.getMessage()); } } } From 15eb0238d3c69e48d5648f9fe6d18f1cb7c05ff5 Mon Sep 17 00:00:00 2001 From: Undead Zeratul Date: Thu, 25 Feb 2016 11:08:56 -0600 Subject: [PATCH 18/86] Move Logging Statements out of Threads Whoops. --- .../com/nincraft/modpackdownloader/ModPackDownloader.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 0a3f2e2..c2700dd 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -139,12 +139,12 @@ private static void downloadThirdPartyMods(final JSONObject jsonList, final Stri for (val item : urlList) { val mod = new ThirdPartyMod((JSONObject) item); + logger.info(String.format("Downloading %s. Mod %s of %s", mod.getFileName(), thirdPartyCount, + THIRD_PARTY_DOWNLOAD_TOTAL)); new Thread(new Runnable() { public void run() { try { - logger.info(String.format("Downloading %s. Mod %s of %s", mod.getFileName(), thirdPartyCount, - THIRD_PARTY_DOWNLOAD_TOTAL)); downloadFile(mod.getDownloadUrl(), modFolder, mod.getFileName(), mod.getProjectName(), false); THIRD_PARTY_DOWNLOAD_COUNT++; @@ -172,6 +172,8 @@ private static void downloadCurseMods(final JSONObject jsonList, final String mo for (val file : fileList) { val mod = new CurseMod((JSONObject) file); + logger.info(String.format("Downloading %s. Mod %s of %s", mod.getProjectName(), curseCount, + CURSE_DOWNLOAD_TOTAL)); val conn = (HttpURLConnection) new URL(mod.getProjectURL()).openConnection(); conn.setInstanceFollowRedirects(false); @@ -182,8 +184,6 @@ private static void downloadCurseMods(final JSONObject jsonList, final String mo new Thread(new Runnable() { public void run() { - logger.info(String.format("Downloading %s. Mod %s of %s", mod.getProjectName(), curseCount, - fileList.size())); downloadCurseForgeFile(mod); CURSE_DOWNLOAD_COUNT++; logger.info(String.format("Finished downloading %s", mod.getProjectName())); From 3f93c8e3725e1184f06ecb390a52c7b7c09a4695 Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Thu, 25 Feb 2016 17:23:12 -0600 Subject: [PATCH 19/86] Add Rolling Log File Appender Because Log files --- src/main/resources/log4j2.xml | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index 602b5ab..06fc002 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -1,13 +1,25 @@ - - - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + From f0be8239affaa9c6bb60a1b387669f14fbd240de Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Fri, 26 Feb 2016 18:44:28 -0600 Subject: [PATCH 20/86] Fix Duplicate Root Tag --- src/main/resources/log4j2.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index 06fc002..cebc874 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -17,8 +17,6 @@ - - From 1d5b4e9b7fd7845cfb016d595d2b13cfe90237cc Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Sun, 28 Feb 2016 13:26:23 -0600 Subject: [PATCH 21/86] Major Refactoring, Round 2 Both the Downloader and Updater should be working now. --- pom.xml | 10 + .../modpackdownloader/ModPackDownloader.java | 320 +++++++++--------- .../modpackdownloader/ModUpdater.java | 173 +++++----- .../modpackdownloader/container/CurseMod.java | 40 ++- .../container/ModContainer.java | 4 +- .../container/ThirdPartyMod.java | 6 - .../handler/CurseModHandler.java | 14 +- .../modpackdownloader/handler/ModHandler.java | 4 +- .../handler/ThirdPartyModHandler.java | 17 + .../mapper/CurseModMapper.java | 29 ++ .../mapper/ThirdPartyMapper.java | 24 ++ .../modpackdownloader/util/Reference.java | 1 + 12 files changed, 393 insertions(+), 249 deletions(-) create mode 100644 src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/mapper/CurseModMapper.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java diff --git a/pom.xml b/pom.xml index 58229c3..ca1678e 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,16 @@ 1.16.6 provided + + com.google.guava + guava + 19.0 + + + com.google.code.gson + gson + 2.6.1 + ${project.artifactId} ${project.version}+${build.number} diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index c2700dd..515994b 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -1,7 +1,6 @@ package com.nincraft.modpackdownloader; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; @@ -10,10 +9,10 @@ import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; +import java.util.ArrayList; +import java.util.List; import org.apache.commons.io.FileUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -26,19 +25,19 @@ import com.nincraft.modpackdownloader.util.URLHelper; import lombok.val; +import lombok.extern.log4j.Log4j2; +@Log4j2 public class ModPackDownloader { - private static int CURSE_DOWNLOAD_COUNT = 0; - private static int CURSE_DOWNLOAD_TOTAL = 0; - private static int THIRD_PARTY_DOWNLOAD_COUNT = 0; - private static int THIRD_PARTY_DOWNLOAD_TOTAL = 0; + private static int DOWNLOAD_COUNT = 0; + private static int DOWNLOAD_TOTAL = 0; - static Logger logger = LogManager.getRootLogger(); + private static final List MOD_LIST = new ArrayList(); public static void main(final String[] args) throws InterruptedException { if (args.length < 2) { - logger.error("Arguments required: manifest file location, mod download location"); + log.error("Arguments required: manifest file location, mod download location"); return; } else { processArguments(args); @@ -46,24 +45,7 @@ public static void main(final String[] args) throws InterruptedException { setupRepo(); - if (Reference.updateMods) { - logger.info(String.format("Updating mods with parameters: %s, %s, %s", Reference.manifestFile, - Reference.mcVersion, Reference.releaseType)); - ModUpdater.updateCurseMods(Reference.manifestFile, Reference.mcVersion, Reference.releaseType); - logger.info("Finished updating mods."); - } else { - logger.info(String.format("Starting download with parameters: %s, %s", Reference.manifestFile, - Reference.modFolder)); - downloadMods(Reference.manifestFile, Reference.modFolder); - while (!checkFinished()) { - Thread.sleep(1); - } - logger.info("Finished downloading mods."); - } - } - - private static boolean checkFinished() { - return CURSE_DOWNLOAD_COUNT == CURSE_DOWNLOAD_TOTAL && THIRD_PARTY_DOWNLOAD_COUNT == THIRD_PARTY_DOWNLOAD_TOTAL; + processMods(); } private static void processArguments(final String[] args) { @@ -78,33 +60,33 @@ private static void processArguments(final String[] args) { } private static void processArgument(final String arg) { - logger.trace("Processing given arguments..."); + log.trace("Processing given arguments..."); if (arg.equals("-forceDownload")) { Reference.forceDownload = true; - logger.debug("Downloads are now being forced."); + log.debug("Downloads are now being forced."); } else if (arg.equals("-updateMods")) { Reference.updateMods = true; - logger.debug("mods will be updated instead of downloaded."); + log.debug("mods will be updated instead of downloaded."); } else if (arg.startsWith("-mcVersion")) { Reference.mcVersion = arg.substring(arg.lastIndexOf("=") + 1); - logger.debug(String.format("Minecraft Version set to: %s", Reference.mcVersion)); + log.debug(String.format("Minecraft Version set to: %s", Reference.mcVersion)); } else if (arg.startsWith("-releaseType")) { Reference.releaseType = arg.substring(arg.lastIndexOf("=") + 1); - logger.debug(String.format("Checking against mod release type: %s", Reference.releaseType)); + log.debug(String.format("Checking against mod release type: %s", Reference.releaseType)); } else if (arg.equals("-generateUrlTxt")) { Reference.generateUrlTxt = true; - logger.debug("Mod URL Text files will now be generated."); + log.debug("Mod URL Text files will now be generated."); } - logger.trace("Finished processing given arguments."); + log.trace("Finished processing given arguments."); } private static void setupRepo() { - logger.trace("Setting up local repository..."); + log.trace("Setting up local repository..."); Reference.userhome = System.getProperty("user.home"); - logger.debug(String.format("User Home System Property detected as: %s", Reference.userhome)); + log.debug(String.format("User Home System Property detected as: %s", Reference.userhome)); Reference.os = System.getProperty("os.name"); - logger.debug(String.format("Operating System detected as: %s", Reference.os)); + log.debug(String.format("Operating System detected as: %s", Reference.os)); if (Reference.os.startsWith("Windows")) { Reference.userhome += Reference.WINDOWS_FOLDER; @@ -113,88 +95,119 @@ private static void setupRepo() { } else { Reference.userhome += Reference.OTHER_FOLDER; } - logger.debug(String.format("User Home Folder set to: %s", Reference.userhome)); + log.debug(String.format("User Home Folder set to: %s", Reference.userhome)); createFolder(Reference.userhome); - logger.trace("Finished setting up local repository."); + log.trace("Finished setting up local repository."); } - private static void downloadMods(final String manifestFile, final String modFolder) { + private static void processMods() throws InterruptedException { + log.trace("Processing Mods..."); + buildModList(); + + if (Reference.updateMods) { + log.info(String.format("Updating mods with parameters: %s, %s, %s", Reference.manifestFile, + Reference.mcVersion, Reference.releaseType)); + ModUpdater.updateCurseMods(MOD_LIST, Reference.mcVersion, Reference.releaseType); + log.info("Finished updating mods."); + } else { + log.info(String.format("Starting download with parameters: %s, %s", Reference.manifestFile, + Reference.modFolder)); + downloadMods(MOD_LIST, Reference.modFolder); + + while (!checkFinished()) { + Thread.sleep(1); + } + log.info("Finished downloading mods."); + } + log.trace("Finished Processing Mods."); + } + + private static void buildModList() { + log.trace("Building Mod List..."); + JSONObject jsonLists = null; try { - val jsonList = (JSONObject) new JSONParser().parse(new FileReader(manifestFile)); - downloadCurseMods(jsonList, modFolder); - downloadThirdPartyMods(jsonList, modFolder); + jsonLists = (JSONObject) new JSONParser().parse(new FileReader(Reference.manifestFile)); } catch (IOException | ParseException e) { - logger.error(e.getMessage()); + log.error(e.getMessage()); + return; + } + + val curseMods = getCurseModList(jsonLists); + if (curseMods != null) { + for (val curseMod : curseMods) { + val mod = new CurseMod((JSONObject) curseMod); + MOD_LIST.add(mod); + log.debug(String.format("Curse Mod '%s' found.", mod.getModName())); + } } - } - private static void downloadThirdPartyMods(final JSONObject jsonList, final String modFolder) { - val urlList = (JSONArray) jsonList.get("thirdParty"); - - if (urlList != null) { - THIRD_PARTY_DOWNLOAD_TOTAL = urlList.size(); - logger.info(String.format("Starting download of %s thirdparty mods.", THIRD_PARTY_DOWNLOAD_TOTAL)); - int thirdPartyCount = 1; - - for (val item : urlList) { - val mod = new ThirdPartyMod((JSONObject) item); - logger.info(String.format("Downloading %s. Mod %s of %s", mod.getFileName(), thirdPartyCount, - THIRD_PARTY_DOWNLOAD_TOTAL)); - - new Thread(new Runnable() { - public void run() { - try { - downloadFile(mod.getDownloadUrl(), modFolder, mod.getFileName(), mod.getProjectName(), - false); - THIRD_PARTY_DOWNLOAD_COUNT++; - logger.info(String.format("Finished downloading %s", mod.getFileName())); - } catch (MalformedURLException | FileNotFoundException e) { - logger.error(e.getMessage()); - } - } - }).start(); - - thirdPartyCount++; + val thirdPartyMods = getThirdPartyModList(jsonLists); + if (thirdPartyMods != null) { + for (val thirdPartyMod : thirdPartyMods) { + val mod = new ThirdPartyMod((JSONObject) thirdPartyMod); + MOD_LIST.add(mod); + log.debug(String.format("Third Party Mod '%s' found.", mod.getModName())); } } + + DOWNLOAD_TOTAL = MOD_LIST.size(); + log.debug(String.format("A total of %s mods will be downloaded.", DOWNLOAD_TOTAL)); + + MOD_LIST.sort((mod1, mod2) -> mod1.getModName().compareToIgnoreCase(mod2.getModName())); + log.trace("Finished Building Mod List."); } - private static void downloadCurseMods(final JSONObject jsonList, final String modFolder) { - try { - val fileList = (JSONArray) (jsonList.containsKey("curseFiles") ? jsonList.get("curseFiles") - : jsonList.get("files")); - - if (fileList != null) { - CURSE_DOWNLOAD_TOTAL = fileList.size(); - logger.info(String.format("Starting download of %s mods from Curse.", CURSE_DOWNLOAD_TOTAL)); - int curseCount = 1; - - for (val file : fileList) { - val mod = new CurseMod((JSONObject) file); - logger.info(String.format("Downloading %s. Mod %s of %s", mod.getProjectName(), curseCount, - CURSE_DOWNLOAD_TOTAL)); - - val conn = (HttpURLConnection) new URL(mod.getProjectURL()).openConnection(); - conn.setInstanceFollowRedirects(false); - conn.connect(); - - mod.setFolder(modFolder); - mod.setProjectName(conn.getHeaderField("Location").split("/")[2]); - - new Thread(new Runnable() { - public void run() { - downloadCurseForgeFile(mod); - CURSE_DOWNLOAD_COUNT++; - logger.info(String.format("Finished downloading %s", mod.getProjectName())); - } - }).start(); - curseCount++; - } + private static JSONArray getCurseModList(final JSONObject jsonList) { + return (JSONArray) (jsonList.containsKey("curseFiles") ? jsonList.get("curseFiles") : jsonList.get("files")); + } + + private static JSONArray getThirdPartyModList(final JSONObject jsonLists) { + return (JSONArray) jsonLists.get("thirdParty"); + } + + private static boolean checkFinished() { + return DOWNLOAD_COUNT == DOWNLOAD_TOTAL; + } + + private static void downloadMods(final List modList, final String modFolder) { + log.trace(String.format("Starting download of %s mods...", DOWNLOAD_TOTAL)); + int curseCount = 1; + int thirdPartyCount = 1; + + for (val mod : modList) { + mod.getModName(); + + if (mod instanceof CurseMod) { + downloadCurseMod((CurseMod) mod, curseCount++, modFolder); + } else if (mod instanceof ThirdPartyMod) { + downloadThirdPartyMod((ThirdPartyMod) mod, thirdPartyCount++, modFolder); } - } catch (final IOException e) { - logger.error(e.getMessage()); } + log.trace(String.format("Finished downloading %s mods.", DOWNLOAD_TOTAL)); + } + + private static void downloadCurseMod(final CurseMod mod, final int downloadCount, final String modFolder) { + new Thread(() -> { + String modName = mod.getModName(); + + log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, downloadCount, DOWNLOAD_TOTAL)); + downloadCurseForgeFile(mod); + DOWNLOAD_COUNT++; + log.info(String.format("Finished downloading %s", modName)); + }).start(); + } + + private static void downloadThirdPartyMod(final ThirdPartyMod mod, final int downloadCount, + final String modFolder) { + new Thread(() -> { + String modName = mod.getModName(); + + log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, downloadCount, DOWNLOAD_TOTAL)); + downloadFile(mod, false); + DOWNLOAD_COUNT++; + log.info(String.format("Finished downloading %s", modName)); + }).start(); } private static void createFolder(final String folder) { @@ -206,69 +219,74 @@ private static void createFolder(final String folder) { } } - private static void downloadCurseForgeFile(final ModContainer mod) { + private static void downloadCurseForgeFile(final CurseMod mod) { + val modName = mod.getModName(); + try { - val fileName = mod.getRename() == null - ? getCurseForgeDownloadLocation(mod.getDownloadUrl(), mod.getProjectName(), mod.getProjectName()) - : mod.getRename(); - - downloadFile(mod.getDownloadUrl(), mod.getFolder(), fileName, mod.getProjectName(), false); - } catch (final MalformedURLException e) { - logger.error(e.getMessage()); - } catch (final FileNotFoundException e) { - logger.error(String.format("Could not find: %s", mod.getProjectName()), e); + val fileName = !mod.getRename().isEmpty() ? mod.getRename() + : getCurseForgeDownloadLocation(mod.getDownloadUrl(), modName, modName); + mod.setFileName(fileName); + + downloadFile(mod, false); } catch (final IOException e) { - logger.error(e.getMessage()); + log.error(e.getMessage()); } } - private static void downloadFile(final String url, final String folder, final String fileName, - final String projectName, final boolean useUserAgent) throws MalformedURLException, FileNotFoundException { - try { - val decodedFileName = URLHelper.decodeSpaces(fileName); - if (!isInLocalRepo(projectName, decodedFileName) || Reference.forceDownload) { + private static void downloadFile(final ModContainer mod, final boolean useUserAgent) { + val decodedFileName = URLHelper.decodeSpaces(mod.getFileName()); + + if (!isInLocalRepo(mod.getModName(), decodedFileName) || Reference.forceDownload) { + try { ReadableByteChannel rbc; if (useUserAgent) { - val conn = (HttpURLConnection) new URL(url).openConnection(); + val conn = (HttpURLConnection) new URL(mod.getDownloadUrl()).openConnection(); conn.addRequestProperty("User-Agent", "Mozilla/4.0"); rbc = Channels.newChannel(conn.getInputStream()); } else { - rbc = Channels.newChannel(new URL(url).openStream()); + rbc = Channels.newChannel(new URL(mod.getDownloadUrl()).openStream()); } - File downloadedFile; - if (folder != null) { - createFolder(folder); - downloadedFile = new File(folder + File.separator + decodedFileName); - } else { - downloadedFile = new File(decodedFileName); - } + val downloadedFile = getDownloadedFile(mod, decodedFileName); val fos = new FileOutputStream(downloadedFile); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); if (Reference.generateUrlTxt) { - generateUrlTxt(downloadedFile, url, folder); + generateUrlTxt(downloadedFile, mod); } - copyToLocalRepo(projectName, downloadedFile); - } else { - copyFromLocalRepo(projectName, decodedFileName, folder); - } - } catch (final IOException e) { - if (!useUserAgent) { - logger.warn( - String.format("Error getting %s. Attempting to redownload using alternate method.", fileName)); - downloadFile(url, folder, fileName, projectName, true); - } else { - logger.error(String.format("Could not download %s.", fileName), e.getMessage()); + copyToLocalRepo(mod.getModName(), downloadedFile); + } catch (final IOException e) { + if (!useUserAgent) { + log.warn(String.format("Error getting %s. Attempting to redownload using alternate method.", + mod.getFileName())); + downloadFile(mod, true); + } else { + log.error(String.format("Could not download %s.", mod.getFileName()), e.getMessage()); + } } + } else { + copyFromLocalRepo(mod.getModName(), decodedFileName, mod.getFolder()); + } + } + + private static File getDownloadedFile(final ModContainer mod, final String fileName) { + val folder = mod.getFolder(); + + if (folder != null) { + createFolder(folder); + return new File(folder + File.separator + fileName); + } else { + return new File(fileName); } } - private static void generateUrlTxt(final File downloadedFile, final String url, final String folder) { + private static void generateUrlTxt(final File downloadedFile, final ModContainer mod) { + val folder = mod.getFolder(); + if (folder != null) { new File(folder + File.separator + downloadedFile.getName() + ".url.txt"); } else { @@ -278,30 +296,30 @@ private static void generateUrlTxt(final File downloadedFile, final String url, private static void copyToLocalRepo(final String projectName, final File downloadedFile) { val newProjectName = projectName != null ? projectName : "thirdParty"; + try { val localRepoFolder = new File(Reference.userhome + newProjectName); FileUtils.copyFileToDirectory(downloadedFile, localRepoFolder); } catch (final IOException e) { - logger.error(String.format("Could not copy %s to local repo.", newProjectName), e); + log.error(String.format("Could not copy %s to local repo.", newProjectName), e); } } private static void copyFromLocalRepo(final String projectName, final String fileName, final String folder) { val newProjectName = projectName != null ? projectName : "thirdParty"; + try { final File localRepoMod = new File(Reference.userhome + newProjectName + File.separator + fileName); FileUtils.copyFileToDirectory(localRepoMod, new File(folder)); } catch (final IOException e) { - logger.error("Could not copy " + newProjectName + " from local repo", e); + log.error(String.format("Could not copy %s from local repo.", newProjectName), e); } } - private static boolean isInLocalRepo(String projectName, final String fileName) { - if (projectName == null) { - projectName = "thirdParty"; - } - final File localCheck = new File(Reference.userhome + projectName + File.separator + fileName); - return localCheck.exists(); + private static boolean isInLocalRepo(final String projectName, final String fileName) { + val newProjectName = projectName != null ? projectName : "thirdParty"; + + return new File(Reference.userhome + newProjectName + File.separator + fileName).exists(); } private static String getCurseForgeDownloadLocation(final String url, final String projectName, diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java index bf2166e..301c605 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -1,9 +1,5 @@ package com.nincraft.modpackdownloader; -import static com.nincraft.modpackdownloader.util.Reference.COOKIE_TEST_1; -import static com.nincraft.modpackdownloader.util.Reference.CURSEFORGE_BASE_URL; -import static com.nincraft.modpackdownloader.util.Reference.DATE_FORMATS; - import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; @@ -14,102 +10,116 @@ import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import com.google.common.base.Strings; +import com.nincraft.modpackdownloader.container.CurseMod; +import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.mapper.CurseModMapper; import com.nincraft.modpackdownloader.util.Reference; import lombok.val; +import lombok.extern.log4j.Log4j2; +@Log4j2 public class ModUpdater { - static Logger logger = LogManager.getRootLogger(); - public static void updateCurseMods(final String manifestFile, final String mcVersion, final String releaseType) { + public static void updateCurseMods(final List modList, final String mcVersion, + final String releaseType) { + log.trace("Updating Curse Mods..."); + val curseMods = new JSONArray(); try { - val jsons = (JSONObject) new JSONParser().parse(new FileReader(manifestFile)); - val fileList = (JSONArray) (jsons.containsKey("curseFiles") ? jsons.get("curseFiles") : jsons.get("files")); - - if (fileList != null) { - logger.info("Checking for updates from " + fileList.size() + " mods"); - for (val file : fileList) { - val modJson = (JSONObject) file; - val projectID = (Long) modJson.get("projectID"); - val fileID = (Long) modJson.get("fileID"); - val url = CURSEFORGE_BASE_URL + projectID + COOKIE_TEST_1; - - final HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); - con.setInstanceFollowRedirects(false); - con.connect(); - - val location = con.getHeaderField("Location"); - val projectName = location.split("/")[2]; - val projectParser = new JSONParser(); - val fileListJson = (JSONObject) getCurseProjectJson(projectID, projectName, projectParser) - .get("files"); - - if (fileListJson == null) { - logger.error(String.format("No file list found for {}.", projectName)); - return; - } + log.info(String.format("Checking for updates from %s mods.", modList.size())); + for (val mod : modList) { + if (!(mod instanceof CurseMod)) { + log.debug(String.format("Mod '%s' is not a Curse Mod, and will be skipped.", mod.getModName())); + continue; + } - Date lastDate = null; - Long mostRecent = fileID; - String mostRecentFile = null; - String currentFile = null; - - for (val thing : fileListJson.values()) { - val mod = (JSONObject) fileListJson.get(thing); - val date = parseDate((String) mod.get("created_at")); - - if (lastDate == null) { - lastDate = date; - } - - if (lastDate.before(date) && equalOrLessThan((String) mod.get("type"), releaseType) - && mod.get("version").equals(mcVersion)) { - mostRecent = (Long) mod.get("id"); - mostRecentFile = (String) mod.get("name"); - lastDate = date; - } - - if (fileID.equals(mod.get("id"))) { - currentFile = (String) mod.get("name"); - } - } + val curseMod = (CurseMod) mod; + + final HttpURLConnection conn = (HttpURLConnection) new URL(curseMod.getProjectUrl()).openConnection(); + conn.setInstanceFollowRedirects(false); + conn.connect(); + + val location = conn.getHeaderField("Location"); + val projectName = location.split("/")[2]; + val fileListJson = (JSONObject) getCurseProjectJson(curseMod.getProjectId(), projectName, + new JSONParser()).get("files"); + + if (fileListJson == null) { + log.error(String.format("No file list found for %s.", projectName)); + continue; + } + + Date lastDate = null; + Long mostRecent = curseMod.getFileId(); + String mostRecentFile = null; + String currentFile = null; - if (!mostRecent.equals(fileID)) { - logger.info( - String.format("Update found for {}. Most recent version is {}. Old version was {}.", - projectName, mostRecentFile, currentFile)); + log.info("Getting most recent available file..."); + for (val newFileJson : fileListJson.values()) { + val newMod = (JSONObject) newFileJson; + val date = parseDate((String) newMod.get("created_at")); - modJson.remove("fileID"); - modJson.put("fileID", mostRecent); + if (lastDate == null) { + lastDate = date; } - if (!modJson.containsKey("name")) { - modJson.put("name", projectName); + if (lastDate.before(date) && equalOrLessThan((String) newMod.get("type"), releaseType) + && newMod.get("version").equals(mcVersion)) { + mostRecent = (Long) newMod.get("id"); + mostRecentFile = (String) newMod.get("name"); + lastDate = date; + } + + if (curseMod.getFileId().equals(newMod.get("id"))) { + currentFile = (String) newMod.get("name"); } } - } + log.info("Finished getting most recent available file."); + + if (!mostRecent.equals(curseMod.getFileId())) { + log.info(String.format("Update found for %s. Most recent version is %s. Old version was %s.", + projectName, mostRecentFile, currentFile)); + curseMod.setFileId(mostRecent); + } + + if (Strings.isNullOrEmpty(curseMod.getModName())) { + curseMod.setModName(projectName); + } - val file = new FileWriter(manifestFile); + val json = CurseModMapper.map(curseMod); + log.debug(json); + curseMods.add(json); + } + log.info("Finished checking for updates."); + log.info("Updating Manifest File..."); + FileWriter file = null; try { - file.write(jsons.toJSONString()); + val jsonLists = (JSONObject) new JSONParser().parse(new FileReader(Reference.manifestFile)); + log.debug(jsonLists); + jsonLists.remove("curseFiles"); + log.debug(jsonLists); + jsonLists.put("curseFiles", curseMods); + log.debug(jsonLists); + + file = new FileWriter(Reference.manifestFile); + file.write(jsonLists.toJSONString()); } finally { file.flush(); file.close(); } - } catch (final IOException e) { - logger.error(e.getMessage()); - } catch (final ParseException e) { - logger.error(e.getMessage()); + } catch (final IOException | ParseException e) { + log.error(e.getMessage()); } + log.trace("Finished Updating Curse Mods."); } private static boolean equalOrLessThan(final String modRelease, final String releaseType) { @@ -118,21 +128,28 @@ private static boolean equalOrLessThan(final String modRelease, final String rel private static JSONObject getCurseProjectJson(final Long projectID, final String projectName, final JSONParser projectParser) throws ParseException, IOException { + log.trace("Getting CurseForge Widget JSON..."); try { - return (JSONObject) projectParser.parse(new BufferedReader(new InputStreamReader( - new URL(String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectName)).openStream()))); + String urlStr = String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectName); + log.debug(urlStr); + return (JSONObject) projectParser + .parse(new BufferedReader(new InputStreamReader(new URL(urlStr).openStream()))); } catch (final FileNotFoundException e) { - return (JSONObject) projectParser.parse(new BufferedReader(new InputStreamReader( - new URL(String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectID + "-" + projectName)) - .openStream()))); + String urlStr = String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectID + "-" + projectName); + log.debug(urlStr); + return (JSONObject) projectParser + .parse(new BufferedReader(new InputStreamReader(new URL(urlStr).openStream()))); + } finally { + log.trace("Finished Getting CurseForge Widget JSON."); } } private static Date parseDate(final String date) { - for (val parse : DATE_FORMATS) { + for (val parse : Reference.DATE_FORMATS) { try { return new SimpleDateFormat(parse).parse(date); } catch (final java.text.ParseException e) { + log.error(e.getMessage()); } } return null; diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java index 30236d4..e0cc98b 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java @@ -1,35 +1,57 @@ package com.nincraft.modpackdownloader.container; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + import org.json.simple.JSONObject; import com.nincraft.modpackdownloader.util.Reference; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.val; +import lombok.extern.log4j.Log4j2; +@Log4j2 @Data @EqualsAndHashCode(callSuper = false) public class CurseMod extends ModContainer { - private String projectURL; + private Long fileId; + private Long projectId; + private String projectName; + private String projectUrl; public CurseMod(final JSONObject modJson) { super(modJson); setProjectId((Long) modJson.get("projectID")); setFileId((Long) modJson.get("fileID")); - setProjectURL(buildProjectUrl()); + setProjectUrl(buildProjectUrl()); + setDownloadUrl(getDownloadUrl()); + + try { + val conn = (HttpURLConnection) new URL(getProjectUrl()).openConnection(); + conn.setInstanceFollowRedirects(false); + conn.connect(); + + setFolder(Reference.modFolder); + setProjectName(conn.getHeaderField("Location").split("/")[2]); + } catch (final IOException e) { + log.error(e.getMessage()); + } } - @Override - public String getDownloadUrl() { - return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectId(), - getProjectName(), getFileId()); + public String buildProjectUrl() { + return String.format(Reference.CURSEFORGE_BASE_URL + "%s" + Reference.COOKIE_TEST_1, getProjectId()); } - String getAlternateDownloadUrl(final JSONObject modJson) { + public String getAlternateDownloadUrl(final JSONObject modJson) { return String.format(Reference.CURSEFORGE_BASE_URL + "%s/files/%s/download", getProjectName(), getFileId()); } - String buildProjectUrl() { - return String.format(Reference.CURSEFORGE_BASE_URL + "%s" + Reference.COOKIE_TEST_1, getProjectId()); + @Override + public String getDownloadUrl() { + return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectId(), + getProjectName(), getFileId()); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java index 4f30d13..b439a74 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java @@ -7,15 +7,13 @@ @Data public abstract class ModContainer { private String modName; - private Long projectId; - private Long fileId; - private String projectName; private String rename; private String fileName; private String folder; private String downloadUrl; public ModContainer(final JSONObject modJson) { + setModName((String) modJson.get("name")); setRename((String) modJson.get("rename")); } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java index 0f13d1f..056fbd6 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java @@ -2,16 +2,10 @@ import org.json.simple.JSONObject; -import lombok.Data; -import lombok.EqualsAndHashCode; - -@Data -@EqualsAndHashCode(callSuper = false) public class ThirdPartyMod extends ModContainer { public ThirdPartyMod(final JSONObject modJson) { super(modJson); - setProjectName((String) modJson.get("name")); setDownloadUrl((String) modJson.get("url")); setFileName(getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, getDownloadUrl().lastIndexOf(".jar") + 4)); diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index d0286d4..84e87d4 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -1,5 +1,17 @@ package com.nincraft.modpackdownloader.handler; -public class CurseModHandler extends ModHandler { +public class CurseModHandler implements ModHandler { + + @Override + public void downloadMod() { + // TODO Auto-generated method stub + + } + + @Override + public void updateMod() { + // TODO Auto-generated method stub + + } } diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java index 394320a..17c0431 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java @@ -1,5 +1,7 @@ package com.nincraft.modpackdownloader.handler; -public abstract class ModHandler { +public interface ModHandler { + public void downloadMod(); + public void updateMod(); } diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java new file mode 100644 index 0000000..82c092e --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java @@ -0,0 +1,17 @@ +package com.nincraft.modpackdownloader.handler; + +public class ThirdPartyModHandler implements ModHandler { + + @Override + public void downloadMod() { + // TODO Auto-generated method stub + + } + + @Override + public void updateMod() { + // TODO Auto-generated method stub + + } + +} diff --git a/src/main/java/com/nincraft/modpackdownloader/mapper/CurseModMapper.java b/src/main/java/com/nincraft/modpackdownloader/mapper/CurseModMapper.java new file mode 100644 index 0000000..6b8362e --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/mapper/CurseModMapper.java @@ -0,0 +1,29 @@ +package com.nincraft.modpackdownloader.mapper; + +import org.json.simple.JSONObject; + +import com.google.common.base.Strings; +import com.nincraft.modpackdownloader.container.CurseMod; + +import lombok.val; + +public class CurseModMapper { + @SuppressWarnings("unchecked") + public static JSONObject map(final CurseMod mod) { + val json = new JSONObject(); + + json.put("name", mod.getModName()); + json.put("projectID", mod.getProjectId()); + json.put("fileID", mod.getFileId()); + + if (!Strings.isNullOrEmpty(mod.getRename())) { + json.put("rename", mod.getRename()); + } + + return json; + } + + public static CurseMod map(final JSONObject json) { + return new CurseMod(json); + } +} diff --git a/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java b/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java new file mode 100644 index 0000000..d1056a8 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java @@ -0,0 +1,24 @@ +package com.nincraft.modpackdownloader.mapper; + +import org.json.simple.JSONObject; + +import com.nincraft.modpackdownloader.container.ThirdPartyMod; + +import lombok.val; + +public class ThirdPartyMapper { + @SuppressWarnings("unchecked") + public JSONObject map(final ThirdPartyMod mod) { + val json = new JSONObject(); + + json.put("name", mod.getModName()); + json.put("url", mod.getDownloadUrl()); + json.put("rename", mod.getRename()); + + return json; + } + + public ThirdPartyMod map(final JSONObject json) { + return new ThirdPartyMod(json); + } +} diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index 98259db..c1ad8fb 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -4,6 +4,7 @@ public class Reference { public static final String CURSEFORGE_BASE_URL = "http://minecraft.curseforge.com/projects/"; public static final String CURSEFORGE_WIDGET_JSON_URL = "http://widget.mcf.li/mc-mods/minecraft/%s.json"; public static final String COOKIE_TEST_1 = "?cookieTest=1"; + public static final String DOWNLOADING_MOD_X_OF_Y = "Downloading %s. Mod %s of %s"; public static final String WINDOWS_FOLDER = "\\.modpackdownloader\\"; public static final String MAC_FOLDER = "/Library/Application Support/modpackdownloader/"; public static final String OTHER_FOLDER = "/.modpackdownloader/"; From e263ad41629c04182a1e16b5c0cdde10b239264f Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Sun, 28 Feb 2016 14:49:15 -0600 Subject: [PATCH 22/86] Refactored more of Updater --- .../modpackdownloader/ModUpdater.java | 158 ++++++++++-------- .../modpackdownloader/container/CurseMod.java | 8 + .../container/ModContainer.java | 11 +- .../container/ThirdPartyMod.java | 19 ++- .../mapper/ThirdPartyMapper.java | 6 +- 5 files changed, 130 insertions(+), 72 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java index 301c605..838c453 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -28,98 +28,120 @@ @Log4j2 public class ModUpdater { - public static void updateCurseMods(final List modList, final String mcVersion, final String releaseType) { log.trace("Updating Curse Mods..."); val curseMods = new JSONArray(); + + checkForUpdates(modList, mcVersion, releaseType, curseMods); + + updateManifest(curseMods); + log.trace("Finished Updating Curse Mods."); + } + + @SuppressWarnings("unchecked") + private static void updateManifest(final JSONArray curseMods) { + log.info("Updating Manifest File..."); try { - log.info(String.format("Checking for updates from %s mods.", modList.size())); - for (val mod : modList) { - if (!(mod instanceof CurseMod)) { - log.debug(String.format("Mod '%s' is not a Curse Mod, and will be skipped.", mod.getModName())); - continue; - } + val jsonLists = (JSONObject) new JSONParser().parse(new FileReader(Reference.manifestFile)); + log.debug(jsonLists); + jsonLists.remove("curseFiles"); + log.debug(jsonLists); + jsonLists.put("curseFiles", curseMods); + log.debug(jsonLists); + + val file = new FileWriter(Reference.manifestFile); + file.write(jsonLists.toJSONString()); + file.flush(); + file.close(); + } catch (final IOException | ParseException e) { + log.error(e.getMessage()); + } + } - val curseMod = (CurseMod) mod; + @SuppressWarnings({ "unchecked" }) + private static void checkForUpdates(final List modList, final String mcVersion, + final String releaseType, final org.json.simple.JSONArray curseMods) { + log.info(String.format("Checking for updates from %s mods.", modList.size())); + for (val mod : modList) { + if (!(mod instanceof CurseMod)) { + log.debug(String.format("Mod '%s' is not a Curse Mod, and will be skipped.", mod.getModName())); + continue; + } + + val curseMod = (CurseMod) mod; - final HttpURLConnection conn = (HttpURLConnection) new URL(curseMod.getProjectUrl()).openConnection(); + JSONObject fileListJson = null; + try { + val conn = (HttpURLConnection) new URL(curseMod.getProjectUrl()).openConnection(); conn.setInstanceFollowRedirects(false); conn.connect(); val location = conn.getHeaderField("Location"); - val projectName = location.split("/")[2]; - val fileListJson = (JSONObject) getCurseProjectJson(curseMod.getProjectId(), projectName, + curseMod.setProjectName(location.split("/")[2]); + fileListJson = (JSONObject) getCurseProjectJson(curseMod.getProjectId(), curseMod.getProjectName(), new JSONParser()).get("files"); if (fileListJson == null) { - log.error(String.format("No file list found for %s.", projectName)); + log.error(String.format("No file list found for %s, and will be skipped.", + curseMod.getProjectName())); continue; } + } catch (IOException | ParseException e) { + log.error(e.getMessage()); + continue; + } - Date lastDate = null; - Long mostRecent = curseMod.getFileId(); - String mostRecentFile = null; - String currentFile = null; - - log.info("Getting most recent available file..."); - for (val newFileJson : fileListJson.values()) { - val newMod = (JSONObject) newFileJson; - val date = parseDate((String) newMod.get("created_at")); - - if (lastDate == null) { - lastDate = date; - } - - if (lastDate.before(date) && equalOrLessThan((String) newMod.get("type"), releaseType) - && newMod.get("version").equals(mcVersion)) { - mostRecent = (Long) newMod.get("id"); - mostRecentFile = (String) newMod.get("name"); - lastDate = date; - } - - if (curseMod.getFileId().equals(newMod.get("id"))) { - currentFile = (String) newMod.get("name"); - } - } - log.info("Finished getting most recent available file."); + val newMod = getLatestVersion(mcVersion, releaseType, curseMod, fileListJson); + log.debug(newMod); + if (curseMod.getFileId().compareTo(newMod.getFileId()) < 0) { + log.info(String.format("Update found for %s. Most recent version is %s. Old version was %s.", + curseMod.getProjectName(), newMod.getVersion(), curseMod.getVersion())); + curseMod.setFileId(newMod.getFileId()); + curseMod.setVersion(newMod.getVersion()); + } - if (!mostRecent.equals(curseMod.getFileId())) { - log.info(String.format("Update found for %s. Most recent version is %s. Old version was %s.", - projectName, mostRecentFile, currentFile)); - curseMod.setFileId(mostRecent); - } + if (Strings.isNullOrEmpty(curseMod.getModName())) { + curseMod.setModName(curseMod.getProjectName()); + } - if (Strings.isNullOrEmpty(curseMod.getModName())) { - curseMod.setModName(projectName); - } + val json = CurseModMapper.map(curseMod); + log.debug(json); + curseMods.add(json); + } + log.info("Finished checking for updates."); + } - val json = CurseModMapper.map(curseMod); - log.debug(json); - curseMods.add(json); + private static CurseMod getLatestVersion(final String mcVersion, final String releaseType, final CurseMod curseMod, + final JSONObject fileListJson) { + log.trace("Getting most recent available file..."); + CurseMod newMod = null; + try { + newMod = curseMod.clone(); + } catch (CloneNotSupportedException e) { + log.warn("Couldn't clone existing mod reference, creating new one instead."); + newMod = new CurseMod(); + } + + for (val newFileJson : fileListJson.values()) { + val newModJson = (JSONObject) newFileJson; + val date = parseDate((String) newModJson.get("created_at")); + + Date latestDate = date; + if (!latestDate.after(date) && equalOrLessThan((String) newModJson.get("type"), releaseType) + && newModJson.get("version").equals(mcVersion)) { + newMod.setFileId((Long) newModJson.get("id")); + newMod.setVersion((String) newModJson.get("name")); + latestDate = date; } - log.info("Finished checking for updates."); - log.info("Updating Manifest File..."); - FileWriter file = null; - try { - val jsonLists = (JSONObject) new JSONParser().parse(new FileReader(Reference.manifestFile)); - log.debug(jsonLists); - jsonLists.remove("curseFiles"); - log.debug(jsonLists); - jsonLists.put("curseFiles", curseMods); - log.debug(jsonLists); - - file = new FileWriter(Reference.manifestFile); - file.write(jsonLists.toJSONString()); - } finally { - file.flush(); - file.close(); + if (curseMod.getFileId().equals(newMod.getFileId())) { + log.debug("Ensuring the current version is set on the mod."); + curseMod.setVersion(newMod.getVersion()); } - } catch (final IOException | ParseException e) { - log.error(e.getMessage()); } - log.trace("Finished Updating Curse Mods."); + log.trace("Finished getting most recent available file."); + return newMod; } private static boolean equalOrLessThan(final String modRelease, final String releaseType) { diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java index e0cc98b..e8c6437 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java @@ -22,6 +22,9 @@ public class CurseMod extends ModContainer { private String projectName; private String projectUrl; + public CurseMod() { + } + public CurseMod(final JSONObject modJson) { super(modJson); setProjectId((Long) modJson.get("projectID")); @@ -54,4 +57,9 @@ public String getDownloadUrl() { return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectId(), getProjectName(), getFileId()); } + + @Override + public CurseMod clone() throws CloneNotSupportedException { + return (CurseMod) super.clone(); + } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java index b439a74..dae3d3a 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java @@ -5,16 +5,25 @@ import lombok.Data; @Data -public abstract class ModContainer { +public abstract class ModContainer implements Cloneable { private String modName; private String rename; private String fileName; private String folder; private String downloadUrl; + private String version; + + public ModContainer() { + } public ModContainer(final JSONObject modJson) { setModName((String) modJson.get("name")); setRename((String) modJson.get("rename")); + setVersion((String) modJson.get("version")); } + @Override + public ModContainer clone() throws CloneNotSupportedException { + return (ModContainer) super.clone(); + } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java index 056fbd6..ab2e100 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java @@ -4,10 +4,25 @@ public class ThirdPartyMod extends ModContainer { + public ThirdPartyMod() { + } + public ThirdPartyMod(final JSONObject modJson) { super(modJson); setDownloadUrl((String) modJson.get("url")); - setFileName(getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, - getDownloadUrl().lastIndexOf(".jar") + 4)); + setFileName(buildFileName()); + } + + private String buildFileName() { + if (getDownloadUrl().endsWith(".jar")) { + return getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, + getDownloadUrl().lastIndexOf(".jar") + 4); + } + return null; + } + + @Override + public ThirdPartyMod clone() throws CloneNotSupportedException { + return (ThirdPartyMod) super.clone(); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java b/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java index d1056a8..a1f3fb6 100644 --- a/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java +++ b/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java @@ -2,6 +2,7 @@ import org.json.simple.JSONObject; +import com.google.common.base.Strings; import com.nincraft.modpackdownloader.container.ThirdPartyMod; import lombok.val; @@ -13,7 +14,10 @@ public JSONObject map(final ThirdPartyMod mod) { json.put("name", mod.getModName()); json.put("url", mod.getDownloadUrl()); - json.put("rename", mod.getRename()); + + if (!Strings.isNullOrEmpty(mod.getRename())) { + json.put("rename", mod.getRename()); + } return json; } From e558c50968ed26c2489ee4d0e11f80acde4d057b Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Sun, 28 Feb 2016 15:10:50 -0600 Subject: [PATCH 23/86] Bugfixes for Downloader Third Party now uses the "Rename" property if it cannot find an appropriate one in the url. Instead of storing the download folder in the mod itself, we're just going to use the one from the arguments, since it stays constant throughout. --- .../modpackdownloader/ModPackDownloader.java | 19 ++++++++----------- .../modpackdownloader/container/CurseMod.java | 1 - .../container/ModContainer.java | 1 - .../container/ThirdPartyMod.java | 2 +- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 515994b..3a05d4e 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -18,6 +18,7 @@ import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import com.google.common.base.Strings; import com.nincraft.modpackdownloader.container.CurseMod; import com.nincraft.modpackdownloader.container.ModContainer; import com.nincraft.modpackdownloader.container.ThirdPartyMod; @@ -223,7 +224,7 @@ private static void downloadCurseForgeFile(final CurseMod mod) { val modName = mod.getModName(); try { - val fileName = !mod.getRename().isEmpty() ? mod.getRename() + val fileName = !Strings.isNullOrEmpty(mod.getRename()) ? mod.getRename() : getCurseForgeDownloadLocation(mod.getDownloadUrl(), modName, modName); mod.setFileName(fileName); @@ -269,26 +270,22 @@ private static void downloadFile(final ModContainer mod, final boolean useUserAg } } } else { - copyFromLocalRepo(mod.getModName(), decodedFileName, mod.getFolder()); + copyFromLocalRepo(mod.getModName(), decodedFileName, Reference.modFolder); } } private static File getDownloadedFile(final ModContainer mod, final String fileName) { - val folder = mod.getFolder(); - - if (folder != null) { - createFolder(folder); - return new File(folder + File.separator + fileName); + if (Reference.modFolder != null) { + createFolder(Reference.modFolder); + return new File(Reference.modFolder + File.separator + fileName); } else { return new File(fileName); } } private static void generateUrlTxt(final File downloadedFile, final ModContainer mod) { - val folder = mod.getFolder(); - - if (folder != null) { - new File(folder + File.separator + downloadedFile.getName() + ".url.txt"); + if (Reference.modFolder != null) { + new File(Reference.modFolder + File.separator + downloadedFile.getName() + ".url.txt"); } else { new File(downloadedFile.getName() + "url.txt"); } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java index e8c6437..ca3a42f 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java @@ -37,7 +37,6 @@ public CurseMod(final JSONObject modJson) { conn.setInstanceFollowRedirects(false); conn.connect(); - setFolder(Reference.modFolder); setProjectName(conn.getHeaderField("Location").split("/")[2]); } catch (final IOException e) { log.error(e.getMessage()); diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java index dae3d3a..18d3ad8 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java @@ -9,7 +9,6 @@ public abstract class ModContainer implements Cloneable { private String modName; private String rename; private String fileName; - private String folder; private String downloadUrl; private String version; diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java index ab2e100..cce03ac 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java @@ -18,7 +18,7 @@ private String buildFileName() { return getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, getDownloadUrl().lastIndexOf(".jar") + 4); } - return null; + return getRename(); } @Override From 50c2d215453dd0370bd8c35c36ece7db6b9b121f Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sun, 28 Feb 2016 20:05:21 -0600 Subject: [PATCH 24/86] Switch third party file name builder Closes #9 as a temp fix --- .../com/nincraft/modpackdownloader/container/ThirdPartyMod.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java index cce03ac..910751b 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java @@ -14,7 +14,7 @@ public ThirdPartyMod(final JSONObject modJson) { } private String buildFileName() { - if (getDownloadUrl().endsWith(".jar")) { + if (getDownloadUrl().contains(".jar")) { return getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, getDownloadUrl().lastIndexOf(".jar") + 4); } From 36219990744e52768c872a6eaf9c04bd9c9b45be Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 29 Feb 2016 15:36:26 -0600 Subject: [PATCH 25/86] Fix download count out of sync --- .../nincraft/modpackdownloader/ModPackDownloader.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 3a05d4e..dcc95dd 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -11,6 +11,7 @@ import java.nio.channels.ReadableByteChannel; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.io.FileUtils; import org.json.simple.JSONArray; @@ -31,8 +32,8 @@ @Log4j2 public class ModPackDownloader { - private static int DOWNLOAD_COUNT = 0; private static int DOWNLOAD_TOTAL = 0; + private static AtomicInteger DOWNLOAD_COUNT = new AtomicInteger(0); private static final List MOD_LIST = new ArrayList(); @@ -168,7 +169,7 @@ private static JSONArray getThirdPartyModList(final JSONObject jsonLists) { } private static boolean checkFinished() { - return DOWNLOAD_COUNT == DOWNLOAD_TOTAL; + return DOWNLOAD_COUNT.get() == DOWNLOAD_TOTAL; } private static void downloadMods(final List modList, final String modFolder) { @@ -194,7 +195,7 @@ private static void downloadCurseMod(final CurseMod mod, final int downloadCount log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, downloadCount, DOWNLOAD_TOTAL)); downloadCurseForgeFile(mod); - DOWNLOAD_COUNT++; + DOWNLOAD_COUNT.incrementAndGet(); log.info(String.format("Finished downloading %s", modName)); }).start(); } @@ -206,7 +207,7 @@ private static void downloadThirdPartyMod(final ThirdPartyMod mod, final int dow log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, downloadCount, DOWNLOAD_TOTAL)); downloadFile(mod, false); - DOWNLOAD_COUNT++; + DOWNLOAD_COUNT.incrementAndGet(); log.info(String.format("Finished downloading %s", modName)); }).start(); } From 6df852316b577e92ed5a5cae1a613c377690a878 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 29 Feb 2016 15:44:35 -0600 Subject: [PATCH 26/86] Actually fix download count desync maybe --- .../com/nincraft/modpackdownloader/ModPackDownloader.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index dcc95dd..22c93f7 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -193,9 +193,8 @@ private static void downloadCurseMod(final CurseMod mod, final int downloadCount new Thread(() -> { String modName = mod.getModName(); - log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, downloadCount, DOWNLOAD_TOTAL)); + log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, DOWNLOAD_COUNT.incrementAndGet(), DOWNLOAD_TOTAL)); downloadCurseForgeFile(mod); - DOWNLOAD_COUNT.incrementAndGet(); log.info(String.format("Finished downloading %s", modName)); }).start(); } @@ -205,9 +204,8 @@ private static void downloadThirdPartyMod(final ThirdPartyMod mod, final int dow new Thread(() -> { String modName = mod.getModName(); - log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, downloadCount, DOWNLOAD_TOTAL)); + log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, DOWNLOAD_COUNT.incrementAndGet(), DOWNLOAD_TOTAL)); downloadFile(mod, false); - DOWNLOAD_COUNT.incrementAndGet(); log.info(String.format("Finished downloading %s", modName)); }).start(); } From a9a816cb9fa1f6269b4174ddcfc9a5f5efd8bf06 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 29 Feb 2016 15:51:41 -0600 Subject: [PATCH 27/86] Fix things I broke while fixing I swear it's ok this time --- .../com/nincraft/modpackdownloader/ModPackDownloader.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 22c93f7..ccd4188 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -193,8 +193,9 @@ private static void downloadCurseMod(final CurseMod mod, final int downloadCount new Thread(() -> { String modName = mod.getModName(); - log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, DOWNLOAD_COUNT.incrementAndGet(), DOWNLOAD_TOTAL)); + log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, DOWNLOAD_COUNT.get(), DOWNLOAD_TOTAL)); downloadCurseForgeFile(mod); + DOWNLOAD_COUNT.incrementAndGet(); log.info(String.format("Finished downloading %s", modName)); }).start(); } @@ -204,8 +205,9 @@ private static void downloadThirdPartyMod(final ThirdPartyMod mod, final int dow new Thread(() -> { String modName = mod.getModName(); - log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, DOWNLOAD_COUNT.incrementAndGet(), DOWNLOAD_TOTAL)); + log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, DOWNLOAD_COUNT.get(), DOWNLOAD_TOTAL)); downloadFile(mod, false); + DOWNLOAD_COUNT.incrementAndGet(); log.info(String.format("Finished downloading %s", modName)); }).start(); } From f3e48e90d9ecd13255d34ceb44bf5dc3a75fd34d Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Mon, 29 Feb 2016 20:06:58 -0600 Subject: [PATCH 28/86] Major Refactoring, Round 3 It's getting there, next step is to get the updater logic pulled out into the handlers. --- .../modpackdownloader/ModPackDownloader.java | 239 +++--------------- .../handler/CurseModHandler.java | 82 +++++- .../modpackdownloader/handler/ModHandler.java | 72 +++++- .../handler/ThirdPartyModHandler.java | 14 +- .../manager/DownloadManager.java | 27 ++ .../manager/UpdateManager.java | 27 ++ .../util/FileSystemHelper.java | 67 +++++ .../modpackdownloader/util/Reference.java | 14 +- .../modpackdownloader/util/URLHelper.java | 5 +- src/main/resources/log4j2.xml | 2 +- 10 files changed, 321 insertions(+), 228 deletions(-) create mode 100644 src/main/java/com/nincraft/modpackdownloader/manager/DownloadManager.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/manager/UpdateManager.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index ccd4188..dc78019 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -1,30 +1,23 @@ package com.nincraft.modpackdownloader; -import java.io.File; -import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.nio.channels.Channels; -import java.nio.channels.ReadableByteChannel; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; -import org.apache.commons.io.FileUtils; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; -import com.google.common.base.Strings; import com.nincraft.modpackdownloader.container.CurseMod; import com.nincraft.modpackdownloader.container.ModContainer; import com.nincraft.modpackdownloader.container.ThirdPartyMod; +import com.nincraft.modpackdownloader.handler.CurseModHandler; +import com.nincraft.modpackdownloader.handler.ThirdPartyModHandler; +import com.nincraft.modpackdownloader.manager.DownloadManager; +import com.nincraft.modpackdownloader.util.FileSystemHelper; import com.nincraft.modpackdownloader.util.Reference; -import com.nincraft.modpackdownloader.util.URLHelper; import lombok.val; import lombok.extern.log4j.Log4j2; @@ -32,9 +25,6 @@ @Log4j2 public class ModPackDownloader { - private static int DOWNLOAD_TOTAL = 0; - private static AtomicInteger DOWNLOAD_COUNT = new AtomicInteger(0); - private static final List MOD_LIST = new ArrayList(); public static void main(final String[] args) throws InterruptedException { @@ -45,6 +35,8 @@ public static void main(final String[] args) throws InterruptedException { processArguments(args); } + init(); + setupRepo(); processMods(); @@ -61,6 +53,13 @@ private static void processArguments(final String[] args) { } } + private static void init() { + log.trace("Registering various mod type handlers..."); + Reference.MOD_HANDLERS.put(CurseMod.class, new CurseModHandler()); + Reference.MOD_HANDLERS.put(ThirdPartyMod.class, new ThirdPartyModHandler()); + log.trace("Finished registering various mod type handlers."); + } + private static void processArgument(final String arg) { log.trace("Processing given arguments..."); if (arg.equals("-forceDownload")) { @@ -99,7 +98,7 @@ private static void setupRepo() { } log.debug(String.format("User Home Folder set to: %s", Reference.userhome)); - createFolder(Reference.userhome); + FileSystemHelper.createFolder(Reference.userhome); log.trace("Finished setting up local repository."); } @@ -110,14 +109,19 @@ private static void processMods() throws InterruptedException { if (Reference.updateMods) { log.info(String.format("Updating mods with parameters: %s, %s, %s", Reference.manifestFile, Reference.mcVersion, Reference.releaseType)); + // UpdateManager.updateMods(MOD_LIST); ModUpdater.updateCurseMods(MOD_LIST, Reference.mcVersion, Reference.releaseType); + + while (!checkUpdateFinished()) { + Thread.sleep(1); + } log.info("Finished updating mods."); } else { - log.info(String.format("Starting download with parameters: %s, %s", Reference.manifestFile, + log.info(String.format("Downloading mods with parameters: %s, %s", Reference.manifestFile, Reference.modFolder)); - downloadMods(MOD_LIST, Reference.modFolder); + DownloadManager.downloadMods(MOD_LIST); - while (!checkFinished()) { + while (!checkDownloadFinished()) { Thread.sleep(1); } log.info("Finished downloading mods."); @@ -153,8 +157,9 @@ private static void buildModList() { } } - DOWNLOAD_TOTAL = MOD_LIST.size(); - log.debug(String.format("A total of %s mods will be downloaded.", DOWNLOAD_TOTAL)); + Reference.updateCount = Reference.downloadTotal = MOD_LIST.size(); + log.debug(String.format("A total of %s mods will be %s.", Reference.downloadTotal, + Reference.updateMods ? "updated" : "downloaded")); MOD_LIST.sort((mod1, mod2) -> mod1.getModName().compareToIgnoreCase(mod2.getModName())); log.trace("Finished Building Mod List."); @@ -168,197 +173,11 @@ private static JSONArray getThirdPartyModList(final JSONObject jsonLists) { return (JSONArray) jsonLists.get("thirdParty"); } - private static boolean checkFinished() { - return DOWNLOAD_COUNT.get() == DOWNLOAD_TOTAL; - } - - private static void downloadMods(final List modList, final String modFolder) { - log.trace(String.format("Starting download of %s mods...", DOWNLOAD_TOTAL)); - int curseCount = 1; - int thirdPartyCount = 1; - - for (val mod : modList) { - mod.getModName(); - - if (mod instanceof CurseMod) { - downloadCurseMod((CurseMod) mod, curseCount++, modFolder); - } else if (mod instanceof ThirdPartyMod) { - downloadThirdPartyMod((ThirdPartyMod) mod, thirdPartyCount++, modFolder); - } - } - log.trace(String.format("Finished downloading %s mods.", DOWNLOAD_TOTAL)); - } - - private static void downloadCurseMod(final CurseMod mod, final int downloadCount, final String modFolder) { - new Thread(() -> { - String modName = mod.getModName(); - - log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, DOWNLOAD_COUNT.get(), DOWNLOAD_TOTAL)); - downloadCurseForgeFile(mod); - DOWNLOAD_COUNT.incrementAndGet(); - log.info(String.format("Finished downloading %s", modName)); - }).start(); - } - - private static void downloadThirdPartyMod(final ThirdPartyMod mod, final int downloadCount, - final String modFolder) { - new Thread(() -> { - String modName = mod.getModName(); - - log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, modName, DOWNLOAD_COUNT.get(), DOWNLOAD_TOTAL)); - downloadFile(mod, false); - DOWNLOAD_COUNT.incrementAndGet(); - log.info(String.format("Finished downloading %s", modName)); - }).start(); - } - - private static void createFolder(final String folder) { - if (folder != null) { - final File dir = new File(folder); - if (!dir.exists()) { - dir.mkdirs(); - } - } - } - - private static void downloadCurseForgeFile(final CurseMod mod) { - val modName = mod.getModName(); - - try { - val fileName = !Strings.isNullOrEmpty(mod.getRename()) ? mod.getRename() - : getCurseForgeDownloadLocation(mod.getDownloadUrl(), modName, modName); - mod.setFileName(fileName); - - downloadFile(mod, false); - } catch (final IOException e) { - log.error(e.getMessage()); - } - } - - private static void downloadFile(final ModContainer mod, final boolean useUserAgent) { - val decodedFileName = URLHelper.decodeSpaces(mod.getFileName()); - - if (!isInLocalRepo(mod.getModName(), decodedFileName) || Reference.forceDownload) { - try { - ReadableByteChannel rbc; - - if (useUserAgent) { - val conn = (HttpURLConnection) new URL(mod.getDownloadUrl()).openConnection(); - conn.addRequestProperty("User-Agent", "Mozilla/4.0"); - rbc = Channels.newChannel(conn.getInputStream()); - } else { - rbc = Channels.newChannel(new URL(mod.getDownloadUrl()).openStream()); - } - - val downloadedFile = getDownloadedFile(mod, decodedFileName); - - val fos = new FileOutputStream(downloadedFile); - fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); - fos.close(); - - if (Reference.generateUrlTxt) { - generateUrlTxt(downloadedFile, mod); - } - - copyToLocalRepo(mod.getModName(), downloadedFile); - } catch (final IOException e) { - if (!useUserAgent) { - log.warn(String.format("Error getting %s. Attempting to redownload using alternate method.", - mod.getFileName())); - downloadFile(mod, true); - } else { - log.error(String.format("Could not download %s.", mod.getFileName()), e.getMessage()); - } - } - } else { - copyFromLocalRepo(mod.getModName(), decodedFileName, Reference.modFolder); - } - } - - private static File getDownloadedFile(final ModContainer mod, final String fileName) { - if (Reference.modFolder != null) { - createFolder(Reference.modFolder); - return new File(Reference.modFolder + File.separator + fileName); - } else { - return new File(fileName); - } + private static boolean checkUpdateFinished() { + return Reference.updateCount == Reference.updateTotal; } - private static void generateUrlTxt(final File downloadedFile, final ModContainer mod) { - if (Reference.modFolder != null) { - new File(Reference.modFolder + File.separator + downloadedFile.getName() + ".url.txt"); - } else { - new File(downloadedFile.getName() + "url.txt"); - } + private static boolean checkDownloadFinished() { + return Reference.downloadCount == Reference.downloadTotal; } - - private static void copyToLocalRepo(final String projectName, final File downloadedFile) { - val newProjectName = projectName != null ? projectName : "thirdParty"; - - try { - val localRepoFolder = new File(Reference.userhome + newProjectName); - FileUtils.copyFileToDirectory(downloadedFile, localRepoFolder); - } catch (final IOException e) { - log.error(String.format("Could not copy %s to local repo.", newProjectName), e); - } - } - - private static void copyFromLocalRepo(final String projectName, final String fileName, final String folder) { - val newProjectName = projectName != null ? projectName : "thirdParty"; - - try { - final File localRepoMod = new File(Reference.userhome + newProjectName + File.separator + fileName); - FileUtils.copyFileToDirectory(localRepoMod, new File(folder)); - } catch (final IOException e) { - log.error(String.format("Could not copy %s from local repo.", newProjectName), e); - } - } - - private static boolean isInLocalRepo(final String projectName, final String fileName) { - val newProjectName = projectName != null ? projectName : "thirdParty"; - - return new File(Reference.userhome + newProjectName + File.separator + fileName).exists(); - } - - private static String getCurseForgeDownloadLocation(final String url, final String projectName, - final String downloadLocation) throws IOException, MalformedURLException { - String encodedDownloadLocation = URLHelper.encodeSpaces(downloadLocation); - - if (encodedDownloadLocation.indexOf(Reference.JAR_FILE_EXT) == -1) { - val newUrl = url + Reference.COOKIE_TEST_1; - - HttpURLConnection conn = (HttpURLConnection) new URL(newUrl).openConnection(); - conn.setInstanceFollowRedirects(false); - conn.connect(); - - String actualURL = conn.getURL().toString(); - int retryCount = 0; - - while (conn.getResponseCode() != 200 || actualURL.indexOf(Reference.JAR_FILE_EXT) == -1) { - val headerLocation = conn.getHeaderField("Location"); - if (headerLocation != null) { - actualURL = headerLocation; - } else { - actualURL = conn.getURL().toString(); - } - - if (retryCount > Reference.RETRY_COUNTER) { - break; - } - - conn = (HttpURLConnection) new URL(newUrl).openConnection(); - retryCount++; - } - - if (actualURL.substring(actualURL.lastIndexOf(Reference.URL_DELIMITER) + 1) - .indexOf(Reference.JAR_FILE_EXT) != -1) { - encodedDownloadLocation = actualURL.substring(actualURL.lastIndexOf(Reference.URL_DELIMITER) + 1); - } else { - encodedDownloadLocation = projectName + Reference.JAR_FILE_EXT; - } - } - - return URLHelper.decodeSpaces(encodedDownloadLocation); - } - } diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 84e87d4..214ece1 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -1,17 +1,89 @@ package com.nincraft.modpackdownloader.handler; -public class CurseModHandler implements ModHandler { +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; - @Override - public void downloadMod() { - // TODO Auto-generated method stub +import com.google.common.base.Strings; +import com.nincraft.modpackdownloader.container.CurseMod; +import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.util.Reference; +import com.nincraft.modpackdownloader.util.URLHelper; + +import lombok.val; +import lombok.extern.log4j.Log4j2; +@Log4j2 +public class CurseModHandler extends ModHandler { + + @Override + public void downloadMod(final ModContainer mod) { + downloadCurseMod((CurseMod) mod); } @Override - public void updateMod() { + public void updateMod(final ModContainer mod) { + updateCurseMod((CurseMod) mod); + } + + private void downloadCurseMod(final CurseMod mod) { + val modName = mod.getModName(); + + try { + val fileName = !Strings.isNullOrEmpty(mod.getRename()) ? mod.getRename() + : getCurseForgeDownloadLocation(mod.getDownloadUrl(), modName, modName); + mod.setFileName(fileName); + + downloadFile(mod, false); + } catch (final IOException e) { + log.error(e.getMessage()); + } + } + + private void updateCurseMod(final CurseMod mod) { // TODO Auto-generated method stub } + private static String getCurseForgeDownloadLocation(final String url, final String projectName, + final String downloadLocation) throws IOException, MalformedURLException { + String encodedDownloadLocation = URLHelper.encodeSpaces(downloadLocation); + + if (encodedDownloadLocation.indexOf(Reference.JAR_FILE_EXT) == -1) { + val newUrl = url + Reference.COOKIE_TEST_1; + + HttpURLConnection conn = (HttpURLConnection) new URL(newUrl).openConnection(); + conn.setInstanceFollowRedirects(false); + conn.connect(); + + String actualURL = conn.getURL().toString(); + int retryCount = 0; + + while (conn.getResponseCode() != 200 || actualURL.indexOf(Reference.JAR_FILE_EXT) == -1) { + val headerLocation = conn.getHeaderField("Location"); + if (headerLocation != null) { + actualURL = headerLocation; + } else { + actualURL = conn.getURL().toString(); + } + + if (retryCount > Reference.RETRY_COUNTER) { + break; + } + + conn = (HttpURLConnection) new URL(newUrl).openConnection(); + retryCount++; + } + + if (actualURL.substring(actualURL.lastIndexOf(Reference.URL_DELIMITER) + 1) + .indexOf(Reference.JAR_FILE_EXT) != -1) { + encodedDownloadLocation = actualURL.substring(actualURL.lastIndexOf(Reference.URL_DELIMITER) + 1); + } else { + encodedDownloadLocation = projectName + Reference.JAR_FILE_EXT; + } + } + + return URLHelper.decodeSpaces(encodedDownloadLocation); + } } diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java index 17c0431..253857e 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java @@ -1,7 +1,73 @@ package com.nincraft.modpackdownloader.handler; -public interface ModHandler { - public void downloadMod(); +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; - public void updateMod(); +import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.util.FileSystemHelper; +import com.nincraft.modpackdownloader.util.Reference; +import com.nincraft.modpackdownloader.util.URLHelper; + +import lombok.val; +import lombok.extern.log4j.Log4j2; + +@Log4j2 +public abstract class ModHandler { + + public abstract void downloadMod(final ModContainer mod); + + public abstract void updateMod(final ModContainer mod); + + protected static void downloadFile(final ModContainer mod, final boolean useUserAgent) { + val decodedFileName = URLHelper.decodeSpaces(mod.getFileName()); + + if (!FileSystemHelper.isInLocalRepo(mod.getModName(), decodedFileName) || Reference.forceDownload) { + try { + ReadableByteChannel rbc; + + if (useUserAgent) { + val conn = (HttpURLConnection) new URL(mod.getDownloadUrl()).openConnection(); + conn.addRequestProperty("User-Agent", "Mozilla/4.0"); + rbc = Channels.newChannel(conn.getInputStream()); + } else { + rbc = Channels.newChannel(new URL(mod.getDownloadUrl()).openStream()); + } + + val downloadedFile = FileSystemHelper.getDownloadedFile(mod, decodedFileName); + + val fos = new FileOutputStream(downloadedFile); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + + if (Reference.generateUrlTxt) { + generateUrlTxt(downloadedFile, mod); + } + + FileSystemHelper.copyToLocalRepo(mod.getModName(), downloadedFile); + } catch (final IOException e) { + if (!useUserAgent) { + log.warn(String.format("Error getting %s. Attempting to redownload using alternate method.", + mod.getFileName())); + downloadFile(mod, true); + } else { + log.error(String.format("Could not download %s.", mod.getFileName()), e.getMessage()); + } + } + } else { + FileSystemHelper.copyFromLocalRepo(mod.getModName(), decodedFileName, Reference.modFolder); + } + } + + protected static void generateUrlTxt(final File downloadedFile, final ModContainer mod) { + if (Reference.modFolder != null) { + new File(Reference.modFolder + File.separator + downloadedFile.getName() + ".url.txt"); + } else { + new File(downloadedFile.getName() + "url.txt"); + } + } } diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java index 82c092e..7b7240f 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java @@ -1,17 +1,17 @@ package com.nincraft.modpackdownloader.handler; -public class ThirdPartyModHandler implements ModHandler { +import com.nincraft.modpackdownloader.container.ModContainer; - @Override - public void downloadMod() { - // TODO Auto-generated method stub +public class ThirdPartyModHandler extends ModHandler { + @Override + public void downloadMod(final ModContainer mod) { + downloadFile(mod, false); } @Override - public void updateMod() { - // TODO Auto-generated method stub - + public void updateMod(final ModContainer mod) { + // no-op } } diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/DownloadManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/DownloadManager.java new file mode 100644 index 0000000..766cc15 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/manager/DownloadManager.java @@ -0,0 +1,27 @@ +package com.nincraft.modpackdownloader.manager; + +import java.util.List; + +import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.util.Reference; + +import lombok.val; +import lombok.extern.log4j.Log4j2; + +@Log4j2 +public class DownloadManager { + public static final void downloadMods(final List mods) { + log.trace(String.format("Downloading %s mods...", mods.size())); + int downloadCount = 1; + for (val mod : mods) { + log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, mod.getModName(), downloadCount++, + Reference.downloadTotal)); + new Thread(() -> { + Reference.MOD_HANDLERS.get(mod.getClass()).downloadMod(mod); + Reference.downloadCount++; + log.info(String.format("Finished downloading %s", mod.getModName())); + }).start(); + } + log.trace(String.format("Finished downloading %s mods.", mods.size())); + } +} diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/UpdateManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/UpdateManager.java new file mode 100644 index 0000000..3fde8d1 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/manager/UpdateManager.java @@ -0,0 +1,27 @@ +package com.nincraft.modpackdownloader.manager; + +import java.util.List; + +import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.util.Reference; + +import lombok.val; +import lombok.extern.log4j.Log4j2; + +@Log4j2 +public class UpdateManager { + public static final void updateMods(final List mods) { + log.trace(String.format("Updating %s mods...", Reference.updateTotal)); + int updateCount = 1; + for (val mod : mods) { + log.info(String.format(Reference.UPDATING_MOD_X_OF_Y, mod.getModName(), updateCount++, + Reference.updateTotal)); + new Thread(() -> { + Reference.MOD_HANDLERS.get(mod.getClass()).updateMod(mod); + Reference.updateCount++; + log.info(String.format("Finished updating %s", mod.getModName())); + }).start(); + } + log.trace(String.format("Finished updating %s mods.", Reference.updateTotal)); + } +} diff --git a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java new file mode 100644 index 0000000..26a81fc --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java @@ -0,0 +1,67 @@ +package com.nincraft.modpackdownloader.util; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; + +import com.nincraft.modpackdownloader.container.ModContainer; + +import lombok.val; +import lombok.extern.log4j.Log4j2; + +@Log4j2 +public final class FileSystemHelper { + private FileSystemHelper() { + } + + public static void createFolder(final String folder) { + if (folder != null) { + final File dir = new File(folder); + if (!dir.exists()) { + dir.mkdirs(); + } + } + } + + public static void copyToLocalRepo(final String projectName, final File downloadedFile) { + val newProjectName = getProjectNameOrDefault(projectName); + + try { + FileUtils.copyFileToDirectory(downloadedFile, new File(Reference.userhome + newProjectName)); + } catch (final IOException e) { + log.error(String.format("Could not copy %s to local repo.", newProjectName), e); + } + } + + public static void copyFromLocalRepo(final String projectName, final String fileName, final String folder) { + val newProjectName = getProjectNameOrDefault(projectName); + + try { + FileUtils.copyFileToDirectory(getLocalFile(fileName, newProjectName), new File(folder)); + } catch (final IOException e) { + log.error(String.format("Could not copy %s from local repo.", newProjectName), e); + } + } + + public static boolean isInLocalRepo(final String projectName, final String fileName) { + return getLocalFile(fileName, getProjectNameOrDefault(projectName)).exists(); + } + + public static File getDownloadedFile(final ModContainer mod, final String fileName) { + if (Reference.modFolder != null) { + createFolder(Reference.modFolder); + return new File(Reference.modFolder + File.separator + fileName); + } else { + return new File(fileName); + } + } + + public static String getProjectNameOrDefault(final String projectName) { + return projectName != null ? projectName : "thirdParty"; + } + + public static File getLocalFile(final String fileName, final String newProjectName) { + return new File(Reference.userhome + newProjectName + File.separator + fileName); + } +} diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index c1ad8fb..e98e165 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -1,10 +1,18 @@ package com.nincraft.modpackdownloader.util; +import java.util.Map; + +import com.google.common.collect.Maps; +import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.handler.ModHandler; + public class Reference { + public static final Map, ModHandler> MOD_HANDLERS = Maps.newHashMap(); public static final String CURSEFORGE_BASE_URL = "http://minecraft.curseforge.com/projects/"; public static final String CURSEFORGE_WIDGET_JSON_URL = "http://widget.mcf.li/mc-mods/minecraft/%s.json"; public static final String COOKIE_TEST_1 = "?cookieTest=1"; - public static final String DOWNLOADING_MOD_X_OF_Y = "Downloading %s. Mod %s of %s"; + public static final String DOWNLOADING_MOD_X_OF_Y = "Downloading %s. Mod %s of %s."; + public static final String UPDATING_MOD_X_OF_Y = "Updating %s. Mod %s of %s."; public static final String WINDOWS_FOLDER = "\\.modpackdownloader\\"; public static final String MAC_FOLDER = "/Library/Application Support/modpackdownloader/"; public static final String OTHER_FOLDER = "/.modpackdownloader/"; @@ -24,4 +32,8 @@ public class Reference { public static String mcVersion; public static String releaseType; public static boolean generateUrlTxt; + public static int downloadCount = 0; + public static int downloadTotal = 0; + public static int updateCount = 0; + public static int updateTotal = 0; } diff --git a/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java index 11750a3..053ec14 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java @@ -1,9 +1,12 @@ package com.nincraft.modpackdownloader.util; -public class URLHelper { +public final class URLHelper { private static final String WHITESPACE = " "; private static final String WHITESPACE_ENCODED = "%20"; + private URLHelper() { + } + public static String encodeSpaces(final String url) { return url.replace(WHITESPACE, WHITESPACE_ENCODED); } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index cebc874..de71dc7 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -15,7 +15,7 @@ - + From f3f64274f5d77d3e9fbef2e1a441ab15c66aadfc Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 29 Feb 2016 21:35:04 -0600 Subject: [PATCH 29/86] Fix updater hanging --- .../java/com/nincraft/modpackdownloader/ModPackDownloader.java | 2 +- src/main/java/com/nincraft/modpackdownloader/ModUpdater.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index dc78019..f6050e3 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -157,7 +157,7 @@ private static void buildModList() { } } - Reference.updateCount = Reference.downloadTotal = MOD_LIST.size(); + Reference.updateTotal = Reference.downloadTotal = MOD_LIST.size(); log.debug(String.format("A total of %s mods will be %s.", Reference.downloadTotal, Reference.updateMods ? "updated" : "downloaded")); diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java index 838c453..0118251 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java @@ -66,6 +66,7 @@ private static void checkForUpdates(final List modList, final Stri for (val mod : modList) { if (!(mod instanceof CurseMod)) { log.debug(String.format("Mod '%s' is not a Curse Mod, and will be skipped.", mod.getModName())); + Reference.updateCount++; continue; } @@ -85,6 +86,7 @@ private static void checkForUpdates(final List modList, final Stri if (fileListJson == null) { log.error(String.format("No file list found for %s, and will be skipped.", curseMod.getProjectName())); + Reference.updateCount++; continue; } } catch (IOException | ParseException e) { @@ -108,6 +110,7 @@ private static void checkForUpdates(final List modList, final Stri val json = CurseModMapper.map(curseMod); log.debug(json); curseMods.add(json); + Reference.updateCount++; } log.info("Finished checking for updates."); } From 93e70d28c66c53d38306e5a8cdb5d1851c065520 Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Tue, 1 Mar 2016 20:35:24 -0600 Subject: [PATCH 30/86] Major Refactoring, Round 4 Fourth time's the charm? I think this is all we have left to do for refactoring out the Modpack Downloader/Manager/etc. We just need to test this before we merge it. --- .../modpackdownloader/ModPackDownloader.java | 93 +-------- .../modpackdownloader/ModUpdater.java | 183 ------------------ .../modpackdownloader/container/CurseMod.java | 2 +- .../container/{ModContainer.java => Mod.java} | 10 +- .../container/ThirdPartyMod.java | 2 +- .../handler/CurseModHandler.java | 120 +++++++++++- .../modpackdownloader/handler/ModHandler.java | 10 +- .../handler/ThirdPartyModHandler.java | 11 +- .../manager/DownloadManager.java | 27 --- .../manager/ModListManager.java | 171 ++++++++++++++++ .../manager/UpdateManager.java | 27 --- .../mapper/ThirdPartyMapper.java | 4 +- .../util/FileSystemHelper.java | 4 +- .../modpackdownloader/util/Reference.java | 7 - src/main/resources/log4j2.xml | 2 +- 15 files changed, 314 insertions(+), 359 deletions(-) delete mode 100644 src/main/java/com/nincraft/modpackdownloader/ModUpdater.java rename src/main/java/com/nincraft/modpackdownloader/container/{ModContainer.java => Mod.java} (63%) delete mode 100644 src/main/java/com/nincraft/modpackdownloader/manager/DownloadManager.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java delete mode 100644 src/main/java/com/nincraft/modpackdownloader/manager/UpdateManager.java diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index f6050e3..f3fca60 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -1,21 +1,6 @@ package com.nincraft.modpackdownloader; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import com.nincraft.modpackdownloader.container.CurseMod; -import com.nincraft.modpackdownloader.container.ModContainer; -import com.nincraft.modpackdownloader.container.ThirdPartyMod; -import com.nincraft.modpackdownloader.handler.CurseModHandler; -import com.nincraft.modpackdownloader.handler.ThirdPartyModHandler; -import com.nincraft.modpackdownloader.manager.DownloadManager; +import com.nincraft.modpackdownloader.manager.ModListManager; import com.nincraft.modpackdownloader.util.FileSystemHelper; import com.nincraft.modpackdownloader.util.Reference; @@ -24,9 +9,6 @@ @Log4j2 public class ModPackDownloader { - - private static final List MOD_LIST = new ArrayList(); - public static void main(final String[] args) throws InterruptedException { if (args.length < 2) { log.error("Arguments required: manifest file location, mod download location"); @@ -35,8 +17,6 @@ public static void main(final String[] args) throws InterruptedException { processArguments(args); } - init(); - setupRepo(); processMods(); @@ -53,13 +33,6 @@ private static void processArguments(final String[] args) { } } - private static void init() { - log.trace("Registering various mod type handlers..."); - Reference.MOD_HANDLERS.put(CurseMod.class, new CurseModHandler()); - Reference.MOD_HANDLERS.put(ThirdPartyMod.class, new ThirdPartyModHandler()); - log.trace("Finished registering various mod type handlers."); - } - private static void processArgument(final String arg) { log.trace("Processing given arguments..."); if (arg.equals("-forceDownload")) { @@ -104,80 +77,28 @@ private static void setupRepo() { private static void processMods() throws InterruptedException { log.trace("Processing Mods..."); - buildModList(); + ModListManager.buildModList(); if (Reference.updateMods) { log.info(String.format("Updating mods with parameters: %s, %s, %s", Reference.manifestFile, Reference.mcVersion, Reference.releaseType)); - // UpdateManager.updateMods(MOD_LIST); - ModUpdater.updateCurseMods(MOD_LIST, Reference.mcVersion, Reference.releaseType); + ModListManager.updateMods(); - while (!checkUpdateFinished()) { + while (!(Reference.updateCount >= Reference.updateTotal)) { Thread.sleep(1); } + ModListManager.updateManifest(); log.info("Finished updating mods."); } else { log.info(String.format("Downloading mods with parameters: %s, %s", Reference.manifestFile, Reference.modFolder)); - DownloadManager.downloadMods(MOD_LIST); + ModListManager.downloadMods(); - while (!checkDownloadFinished()) { + while (!(Reference.downloadCount >= Reference.downloadTotal)) { Thread.sleep(1); } log.info("Finished downloading mods."); } log.trace("Finished Processing Mods."); } - - private static void buildModList() { - log.trace("Building Mod List..."); - JSONObject jsonLists = null; - try { - jsonLists = (JSONObject) new JSONParser().parse(new FileReader(Reference.manifestFile)); - } catch (IOException | ParseException e) { - log.error(e.getMessage()); - return; - } - - val curseMods = getCurseModList(jsonLists); - if (curseMods != null) { - for (val curseMod : curseMods) { - val mod = new CurseMod((JSONObject) curseMod); - MOD_LIST.add(mod); - log.debug(String.format("Curse Mod '%s' found.", mod.getModName())); - } - } - - val thirdPartyMods = getThirdPartyModList(jsonLists); - if (thirdPartyMods != null) { - for (val thirdPartyMod : thirdPartyMods) { - val mod = new ThirdPartyMod((JSONObject) thirdPartyMod); - MOD_LIST.add(mod); - log.debug(String.format("Third Party Mod '%s' found.", mod.getModName())); - } - } - - Reference.updateTotal = Reference.downloadTotal = MOD_LIST.size(); - log.debug(String.format("A total of %s mods will be %s.", Reference.downloadTotal, - Reference.updateMods ? "updated" : "downloaded")); - - MOD_LIST.sort((mod1, mod2) -> mod1.getModName().compareToIgnoreCase(mod2.getModName())); - log.trace("Finished Building Mod List."); - } - - private static JSONArray getCurseModList(final JSONObject jsonList) { - return (JSONArray) (jsonList.containsKey("curseFiles") ? jsonList.get("curseFiles") : jsonList.get("files")); - } - - private static JSONArray getThirdPartyModList(final JSONObject jsonLists) { - return (JSONArray) jsonLists.get("thirdParty"); - } - - private static boolean checkUpdateFinished() { - return Reference.updateCount == Reference.updateTotal; - } - - private static boolean checkDownloadFinished() { - return Reference.downloadCount == Reference.downloadTotal; - } } diff --git a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java b/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java deleted file mode 100644 index 0118251..0000000 --- a/src/main/java/com/nincraft/modpackdownloader/ModUpdater.java +++ /dev/null @@ -1,183 +0,0 @@ -package com.nincraft.modpackdownloader; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; - -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import com.google.common.base.Strings; -import com.nincraft.modpackdownloader.container.CurseMod; -import com.nincraft.modpackdownloader.container.ModContainer; -import com.nincraft.modpackdownloader.mapper.CurseModMapper; -import com.nincraft.modpackdownloader.util.Reference; - -import lombok.val; -import lombok.extern.log4j.Log4j2; - -@Log4j2 -public class ModUpdater { - public static void updateCurseMods(final List modList, final String mcVersion, - final String releaseType) { - log.trace("Updating Curse Mods..."); - val curseMods = new JSONArray(); - - checkForUpdates(modList, mcVersion, releaseType, curseMods); - - updateManifest(curseMods); - log.trace("Finished Updating Curse Mods."); - } - - @SuppressWarnings("unchecked") - private static void updateManifest(final JSONArray curseMods) { - log.info("Updating Manifest File..."); - try { - val jsonLists = (JSONObject) new JSONParser().parse(new FileReader(Reference.manifestFile)); - log.debug(jsonLists); - jsonLists.remove("curseFiles"); - log.debug(jsonLists); - jsonLists.put("curseFiles", curseMods); - log.debug(jsonLists); - - val file = new FileWriter(Reference.manifestFile); - file.write(jsonLists.toJSONString()); - file.flush(); - file.close(); - } catch (final IOException | ParseException e) { - log.error(e.getMessage()); - } - } - - @SuppressWarnings({ "unchecked" }) - private static void checkForUpdates(final List modList, final String mcVersion, - final String releaseType, final org.json.simple.JSONArray curseMods) { - log.info(String.format("Checking for updates from %s mods.", modList.size())); - for (val mod : modList) { - if (!(mod instanceof CurseMod)) { - log.debug(String.format("Mod '%s' is not a Curse Mod, and will be skipped.", mod.getModName())); - Reference.updateCount++; - continue; - } - - val curseMod = (CurseMod) mod; - - JSONObject fileListJson = null; - try { - val conn = (HttpURLConnection) new URL(curseMod.getProjectUrl()).openConnection(); - conn.setInstanceFollowRedirects(false); - conn.connect(); - - val location = conn.getHeaderField("Location"); - curseMod.setProjectName(location.split("/")[2]); - fileListJson = (JSONObject) getCurseProjectJson(curseMod.getProjectId(), curseMod.getProjectName(), - new JSONParser()).get("files"); - - if (fileListJson == null) { - log.error(String.format("No file list found for %s, and will be skipped.", - curseMod.getProjectName())); - Reference.updateCount++; - continue; - } - } catch (IOException | ParseException e) { - log.error(e.getMessage()); - continue; - } - - val newMod = getLatestVersion(mcVersion, releaseType, curseMod, fileListJson); - log.debug(newMod); - if (curseMod.getFileId().compareTo(newMod.getFileId()) < 0) { - log.info(String.format("Update found for %s. Most recent version is %s. Old version was %s.", - curseMod.getProjectName(), newMod.getVersion(), curseMod.getVersion())); - curseMod.setFileId(newMod.getFileId()); - curseMod.setVersion(newMod.getVersion()); - } - - if (Strings.isNullOrEmpty(curseMod.getModName())) { - curseMod.setModName(curseMod.getProjectName()); - } - - val json = CurseModMapper.map(curseMod); - log.debug(json); - curseMods.add(json); - Reference.updateCount++; - } - log.info("Finished checking for updates."); - } - - private static CurseMod getLatestVersion(final String mcVersion, final String releaseType, final CurseMod curseMod, - final JSONObject fileListJson) { - log.trace("Getting most recent available file..."); - CurseMod newMod = null; - try { - newMod = curseMod.clone(); - } catch (CloneNotSupportedException e) { - log.warn("Couldn't clone existing mod reference, creating new one instead."); - newMod = new CurseMod(); - } - - for (val newFileJson : fileListJson.values()) { - val newModJson = (JSONObject) newFileJson; - val date = parseDate((String) newModJson.get("created_at")); - - Date latestDate = date; - if (!latestDate.after(date) && equalOrLessThan((String) newModJson.get("type"), releaseType) - && newModJson.get("version").equals(mcVersion)) { - newMod.setFileId((Long) newModJson.get("id")); - newMod.setVersion((String) newModJson.get("name")); - latestDate = date; - } - - if (curseMod.getFileId().equals(newMod.getFileId())) { - log.debug("Ensuring the current version is set on the mod."); - curseMod.setVersion(newMod.getVersion()); - } - } - log.trace("Finished getting most recent available file."); - return newMod; - } - - private static boolean equalOrLessThan(final String modRelease, final String releaseType) { - return releaseType.equals(modRelease) || "beta".equals(releaseType) && "release".equals(modRelease); - } - - private static JSONObject getCurseProjectJson(final Long projectID, final String projectName, - final JSONParser projectParser) throws ParseException, IOException { - log.trace("Getting CurseForge Widget JSON..."); - try { - String urlStr = String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectName); - log.debug(urlStr); - return (JSONObject) projectParser - .parse(new BufferedReader(new InputStreamReader(new URL(urlStr).openStream()))); - } catch (final FileNotFoundException e) { - String urlStr = String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectID + "-" + projectName); - log.debug(urlStr); - return (JSONObject) projectParser - .parse(new BufferedReader(new InputStreamReader(new URL(urlStr).openStream()))); - } finally { - log.trace("Finished Getting CurseForge Widget JSON."); - } - } - - private static Date parseDate(final String date) { - for (val parse : Reference.DATE_FORMATS) { - try { - return new SimpleDateFormat(parse).parse(date); - } catch (final java.text.ParseException e) { - log.error(e.getMessage()); - } - } - return null; - } - -} diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java index ca3a42f..4374abd 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java @@ -16,7 +16,7 @@ @Log4j2 @Data @EqualsAndHashCode(callSuper = false) -public class CurseMod extends ModContainer { +public class CurseMod extends Mod { private Long fileId; private Long projectId; private String projectName; diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java similarity index 63% rename from src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java rename to src/main/java/com/nincraft/modpackdownloader/container/Mod.java index 18d3ad8..4d196d5 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModContainer.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java @@ -5,24 +5,24 @@ import lombok.Data; @Data -public abstract class ModContainer implements Cloneable { +public abstract class Mod implements Cloneable { private String modName; private String rename; private String fileName; private String downloadUrl; private String version; - public ModContainer() { + public Mod() { } - public ModContainer(final JSONObject modJson) { + public Mod(final JSONObject modJson) { setModName((String) modJson.get("name")); setRename((String) modJson.get("rename")); setVersion((String) modJson.get("version")); } @Override - public ModContainer clone() throws CloneNotSupportedException { - return (ModContainer) super.clone(); + public Mod clone() throws CloneNotSupportedException { + return (Mod) super.clone(); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java index 910751b..945a21c 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java @@ -2,7 +2,7 @@ import org.json.simple.JSONObject; -public class ThirdPartyMod extends ModContainer { +public class ThirdPartyMod extends Mod { public ThirdPartyMod() { } diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 214ece1..47b6bfe 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -1,13 +1,22 @@ package com.nincraft.modpackdownloader.handler; +import java.io.BufferedReader; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; import com.google.common.base.Strings; import com.nincraft.modpackdownloader.container.CurseMod; -import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.container.Mod; import com.nincraft.modpackdownloader.util.Reference; import com.nincraft.modpackdownloader.util.URLHelper; @@ -18,12 +27,12 @@ public class CurseModHandler extends ModHandler { @Override - public void downloadMod(final ModContainer mod) { + public void downloadMod(final Mod mod) { downloadCurseMod((CurseMod) mod); } @Override - public void updateMod(final ModContainer mod) { + public void updateMod(final Mod mod) { updateCurseMod((CurseMod) mod); } @@ -41,11 +50,6 @@ private void downloadCurseMod(final CurseMod mod) { } } - private void updateCurseMod(final CurseMod mod) { - // TODO Auto-generated method stub - - } - private static String getCurseForgeDownloadLocation(final String url, final String projectName, final String downloadLocation) throws IOException, MalformedURLException { String encodedDownloadLocation = URLHelper.encodeSpaces(downloadLocation); @@ -86,4 +90,104 @@ private static String getCurseForgeDownloadLocation(final String url, final Stri return URLHelper.decodeSpaces(encodedDownloadLocation); } + + private void updateCurseMod(final CurseMod mod) { + JSONObject fileListJson = null; + try { + val conn = (HttpURLConnection) new URL(mod.getProjectUrl()).openConnection(); + conn.setInstanceFollowRedirects(false); + conn.connect(); + + val location = conn.getHeaderField("Location"); + mod.setProjectName(location.split("/")[2]); + fileListJson = (JSONObject) getCurseProjectJson(mod.getProjectId(), mod.getProjectName(), new JSONParser()) + .get("files"); + + if (fileListJson == null) { + log.error(String.format("No file list found for %s, and will be skipped.", mod.getProjectName())); + return; + } + } catch (IOException | ParseException e) { + log.error(e.getMessage()); + return; + } + + val newMod = getLatestVersion(Reference.mcVersion, Reference.releaseType, mod, fileListJson); + log.debug(newMod); + if (mod.getFileId().compareTo(newMod.getFileId()) < 0) { + log.info(String.format("Update found for %s. Most recent version is %s. Old version was %s.", + mod.getProjectName(), newMod.getVersion(), mod.getVersion())); + mod.setFileId(newMod.getFileId()); + mod.setVersion(newMod.getVersion()); + } + + if (Strings.isNullOrEmpty(mod.getModName())) { + mod.setModName(mod.getProjectName()); + } + } + + private static CurseMod getLatestVersion(final String mcVersion, final String releaseType, final CurseMod curseMod, + final JSONObject fileListJson) { + log.trace("Getting most recent available file..."); + CurseMod newMod = null; + try { + newMod = curseMod.clone(); + } catch (CloneNotSupportedException e) { + log.warn("Couldn't clone existing mod reference, creating new one instead."); + newMod = new CurseMod(); + } + + for (val newFileJson : fileListJson.values()) { + val newModJson = (JSONObject) newFileJson; + val date = parseDate((String) newModJson.get("created_at")); + + Date latestDate = date; + if (!latestDate.after(date) && equalOrLessThan((String) newModJson.get("type"), releaseType) + && newModJson.get("version").equals(mcVersion)) { + newMod.setFileId((Long) newModJson.get("id")); + newMod.setVersion((String) newModJson.get("name")); + latestDate = date; + } + + if (curseMod.getFileId().equals(newMod.getFileId())) { + log.debug("Ensuring the current version is set on the mod."); + curseMod.setVersion(newMod.getVersion()); + } + } + log.trace("Finished getting most recent available file."); + return newMod; + } + + private static boolean equalOrLessThan(final String modRelease, final String releaseType) { + return releaseType.equals(modRelease) || "beta".equals(releaseType) && "release".equals(modRelease); + } + + private static JSONObject getCurseProjectJson(final Long projectID, final String projectName, + final JSONParser projectParser) throws ParseException, IOException { + log.trace("Getting CurseForge Widget JSON..."); + try { + String urlStr = String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectName); + log.debug(urlStr); + return (JSONObject) projectParser + .parse(new BufferedReader(new InputStreamReader(new URL(urlStr).openStream()))); + } catch (final FileNotFoundException e) { + String urlStr = String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectID + "-" + projectName); + log.debug(urlStr); + return (JSONObject) projectParser + .parse(new BufferedReader(new InputStreamReader(new URL(urlStr).openStream()))); + } finally { + log.trace("Finished Getting CurseForge Widget JSON."); + } + } + + private static Date parseDate(final String date) { + for (val parse : Reference.DATE_FORMATS) { + try { + return new SimpleDateFormat(parse).parse(date); + } catch (final java.text.ParseException e) { + log.error(e.getMessage()); + } + } + return null; + } } diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java index 253857e..80e1b53 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java @@ -8,7 +8,7 @@ import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; -import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.container.Mod; import com.nincraft.modpackdownloader.util.FileSystemHelper; import com.nincraft.modpackdownloader.util.Reference; import com.nincraft.modpackdownloader.util.URLHelper; @@ -19,11 +19,11 @@ @Log4j2 public abstract class ModHandler { - public abstract void downloadMod(final ModContainer mod); + public abstract void downloadMod(final Mod mod); - public abstract void updateMod(final ModContainer mod); + public abstract void updateMod(final Mod mod); - protected static void downloadFile(final ModContainer mod, final boolean useUserAgent) { + protected static void downloadFile(final Mod mod, final boolean useUserAgent) { val decodedFileName = URLHelper.decodeSpaces(mod.getFileName()); if (!FileSystemHelper.isInLocalRepo(mod.getModName(), decodedFileName) || Reference.forceDownload) { @@ -63,7 +63,7 @@ protected static void downloadFile(final ModContainer mod, final boolean useUser } } - protected static void generateUrlTxt(final File downloadedFile, final ModContainer mod) { + protected static void generateUrlTxt(final File downloadedFile, final Mod mod) { if (Reference.modFolder != null) { new File(Reference.modFolder + File.separator + downloadedFile.getName() + ".url.txt"); } else { diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java index 7b7240f..137d955 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ThirdPartyModHandler.java @@ -1,17 +1,20 @@ package com.nincraft.modpackdownloader.handler; -import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.container.Mod; +import lombok.extern.log4j.Log4j2; + +@Log4j2 public class ThirdPartyModHandler extends ModHandler { @Override - public void downloadMod(final ModContainer mod) { + public void downloadMod(final Mod mod) { downloadFile(mod, false); } @Override - public void updateMod(final ModContainer mod) { - // no-op + public void updateMod(final Mod mod) { + log.info("Updating Third Party Mods is not supported."); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/DownloadManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/DownloadManager.java deleted file mode 100644 index 766cc15..0000000 --- a/src/main/java/com/nincraft/modpackdownloader/manager/DownloadManager.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.nincraft.modpackdownloader.manager; - -import java.util.List; - -import com.nincraft.modpackdownloader.container.ModContainer; -import com.nincraft.modpackdownloader.util.Reference; - -import lombok.val; -import lombok.extern.log4j.Log4j2; - -@Log4j2 -public class DownloadManager { - public static final void downloadMods(final List mods) { - log.trace(String.format("Downloading %s mods...", mods.size())); - int downloadCount = 1; - for (val mod : mods) { - log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, mod.getModName(), downloadCount++, - Reference.downloadTotal)); - new Thread(() -> { - Reference.MOD_HANDLERS.get(mod.getClass()).downloadMod(mod); - Reference.downloadCount++; - log.info(String.format("Finished downloading %s", mod.getModName())); - }).start(); - } - log.trace(String.format("Finished downloading %s mods.", mods.size())); - } -} diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java new file mode 100644 index 0000000..00a0af5 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -0,0 +1,171 @@ +package com.nincraft.modpackdownloader.manager; + +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.nincraft.modpackdownloader.container.CurseMod; +import com.nincraft.modpackdownloader.container.Mod; +import com.nincraft.modpackdownloader.container.ThirdPartyMod; +import com.nincraft.modpackdownloader.handler.CurseModHandler; +import com.nincraft.modpackdownloader.handler.ModHandler; +import com.nincraft.modpackdownloader.handler.ThirdPartyModHandler; +import com.nincraft.modpackdownloader.mapper.CurseModMapper; +import com.nincraft.modpackdownloader.mapper.ThirdPartyMapper; +import com.nincraft.modpackdownloader.util.Reference; + +import lombok.val; +import lombok.extern.log4j.Log4j2; + +@Log4j2 +public class ModListManager { + private static final List MOD_LIST = Lists.newArrayList(); + + public static final Map, ModHandler> MOD_HANDLERS = Maps.newHashMap(); + + private static boolean processedCurseMods = false; + private static boolean processedThirdPartyMods = false; + + static { + log.trace("Registering various mod type handlers..."); + MOD_HANDLERS.put(CurseMod.class, new CurseModHandler()); + MOD_HANDLERS.put(ThirdPartyMod.class, new ThirdPartyModHandler()); + log.trace("Finished registering various mod type handlers."); + } + + public static void addMod(final Mod mod) { + MOD_LIST.add(mod); + } + + public static void clearModList() { + MOD_LIST.clear(); + } + + public static void sortModList() { + MOD_LIST.sort((mod1, mod2) -> mod1.getModName().compareToIgnoreCase(mod2.getModName())); + } + + public static int getModListCount() { + return MOD_LIST.size(); + } + + public static void buildModList() { + log.trace("Building Mod List..."); + JSONObject jsonLists = null; + try { + jsonLists = (JSONObject) new JSONParser().parse(new FileReader(Reference.manifestFile)); + } catch (IOException | ParseException e) { + log.error(e.getMessage()); + return; + } + + val curseMods = getCurseModList(jsonLists); + if (curseMods != null) { + for (val curseMod : curseMods) { + val mod = new CurseMod((JSONObject) curseMod); + addMod(mod); + processedCurseMods = true; + log.debug(String.format("Curse Mod '%s' found.", mod.getModName())); + } + } + + val thirdPartyMods = getThirdPartyModList(jsonLists); + if (thirdPartyMods != null) { + for (val thirdPartyMod : thirdPartyMods) { + val mod = new ThirdPartyMod((JSONObject) thirdPartyMod); + addMod(mod); + processedThirdPartyMods = true; + log.debug(String.format("Third Party Mod '%s' found.", mod.getModName())); + } + } + + Reference.updateTotal = Reference.downloadTotal = getModListCount(); + log.debug(String.format("A total of %s mods will be %s.", Reference.downloadTotal, + Reference.updateMods ? "updated" : "downloaded")); + + sortModList(); + log.trace("Finished Building Mod List."); + } + + public static JSONArray getCurseModList(final JSONObject jsonList) { + return (JSONArray) (jsonList.containsKey("curseFiles") ? jsonList.get("curseFiles") : jsonList.get("files")); + } + + public static JSONArray getThirdPartyModList(final JSONObject jsonLists) { + return (JSONArray) jsonLists.get("thirdParty"); + } + + public static final void downloadMods() { + log.trace(String.format("Downloading %s mods...", MOD_LIST.size())); + int downloadCount = 1; + for (val mod : MOD_LIST) { + log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, mod.getModName(), downloadCount++, + Reference.downloadTotal)); + new Thread(() -> { + MOD_HANDLERS.get(mod.getClass()).downloadMod(mod); + Reference.downloadCount++; + log.info(String.format("Finished downloading %s", mod.getModName())); + }).start(); + } + log.trace(String.format("Finished downloading %s mods.", MOD_LIST.size())); + } + + public static final void updateMods() { + log.trace(String.format("Updating %s mods...", Reference.updateTotal)); + int updateCount = 1; + for (val mod : MOD_LIST) { + log.info(String.format(Reference.UPDATING_MOD_X_OF_Y, mod.getModName(), updateCount++, + Reference.updateTotal)); + new Thread(() -> { + MOD_HANDLERS.get(mod.getClass()).updateMod(mod); + Reference.updateCount++; + log.info(String.format("Finished updating %s", mod.getModName())); + }).start(); + } + log.trace(String.format("Finished updating %s mods.", Reference.updateTotal)); + } + + public static void updateManifest() { + log.info("Updating Manifest File..."); + try { + val file = new FileWriter(Reference.manifestFile); + file.write(buildManifestJson()); + file.flush(); + file.close(); + } catch (final IOException e) { + log.error(e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + public static String buildManifestJson() { + JSONObject manifest = new JSONObject(); + + if (processedCurseMods) { + manifest.put("curseFiles", new JSONArray()); + } + + if (processedThirdPartyMods) { + manifest.put("thirdParty", new JSONArray()); + } + + for (val mod : MOD_LIST) { + if (mod instanceof CurseMod && manifest.containsKey("curseFiles")) { + ((JSONArray) manifest.get("curseFiles")).add(CurseModMapper.map((CurseMod) mod)); + } else if (mod instanceof ThirdPartyMod && manifest.containsKey("thirdParty")) { + ((JSONArray) manifest.get("thirdParty")).add(ThirdPartyMapper.map((ThirdPartyMod) mod)); + } + } + + return manifest.toString(); + } +} diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/UpdateManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/UpdateManager.java deleted file mode 100644 index 3fde8d1..0000000 --- a/src/main/java/com/nincraft/modpackdownloader/manager/UpdateManager.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.nincraft.modpackdownloader.manager; - -import java.util.List; - -import com.nincraft.modpackdownloader.container.ModContainer; -import com.nincraft.modpackdownloader.util.Reference; - -import lombok.val; -import lombok.extern.log4j.Log4j2; - -@Log4j2 -public class UpdateManager { - public static final void updateMods(final List mods) { - log.trace(String.format("Updating %s mods...", Reference.updateTotal)); - int updateCount = 1; - for (val mod : mods) { - log.info(String.format(Reference.UPDATING_MOD_X_OF_Y, mod.getModName(), updateCount++, - Reference.updateTotal)); - new Thread(() -> { - Reference.MOD_HANDLERS.get(mod.getClass()).updateMod(mod); - Reference.updateCount++; - log.info(String.format("Finished updating %s", mod.getModName())); - }).start(); - } - log.trace(String.format("Finished updating %s mods.", Reference.updateTotal)); - } -} diff --git a/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java b/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java index a1f3fb6..81e6b0c 100644 --- a/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java +++ b/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java @@ -9,7 +9,7 @@ public class ThirdPartyMapper { @SuppressWarnings("unchecked") - public JSONObject map(final ThirdPartyMod mod) { + public static JSONObject map(final ThirdPartyMod mod) { val json = new JSONObject(); json.put("name", mod.getModName()); @@ -22,7 +22,7 @@ public JSONObject map(final ThirdPartyMod mod) { return json; } - public ThirdPartyMod map(final JSONObject json) { + public static ThirdPartyMod map(final JSONObject json) { return new ThirdPartyMod(json); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java index 26a81fc..2c324b3 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java @@ -5,7 +5,7 @@ import org.apache.commons.io.FileUtils; -import com.nincraft.modpackdownloader.container.ModContainer; +import com.nincraft.modpackdownloader.container.Mod; import lombok.val; import lombok.extern.log4j.Log4j2; @@ -48,7 +48,7 @@ public static boolean isInLocalRepo(final String projectName, final String fileN return getLocalFile(fileName, getProjectNameOrDefault(projectName)).exists(); } - public static File getDownloadedFile(final ModContainer mod, final String fileName) { + public static File getDownloadedFile(final Mod mod, final String fileName) { if (Reference.modFolder != null) { createFolder(Reference.modFolder); return new File(Reference.modFolder + File.separator + fileName); diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index e98e165..c6acbf9 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -1,13 +1,6 @@ package com.nincraft.modpackdownloader.util; -import java.util.Map; - -import com.google.common.collect.Maps; -import com.nincraft.modpackdownloader.container.ModContainer; -import com.nincraft.modpackdownloader.handler.ModHandler; - public class Reference { - public static final Map, ModHandler> MOD_HANDLERS = Maps.newHashMap(); public static final String CURSEFORGE_BASE_URL = "http://minecraft.curseforge.com/projects/"; public static final String CURSEFORGE_WIDGET_JSON_URL = "http://widget.mcf.li/mc-mods/minecraft/%s.json"; public static final String COOKIE_TEST_1 = "?cookieTest=1"; diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index de71dc7..cebc874 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -15,7 +15,7 @@ - + From 6bac9e4faeeb08ddf00953da3437b01f5f1e5b5a Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Fri, 4 Mar 2016 21:34:34 -0600 Subject: [PATCH 31/86] Use FileUtils for Downloading Mods It's a lot simpler now, that's for sure... --- .../modpackdownloader/ModPackDownloader.java | 5 +++ .../handler/CurseModHandler.java | 5 +-- .../modpackdownloader/handler/ModHandler.java | 45 ++++++------------- 3 files changed, 20 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index f3fca60..1fb7815 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -72,6 +72,10 @@ private static void setupRepo() { log.debug(String.format("User Home Folder set to: %s", Reference.userhome)); FileSystemHelper.createFolder(Reference.userhome); + + log.debug("Setting User Agent..."); + System.setProperty("http.agent", "Mozilla/4.0"); + log.trace("Finished setting up local repository."); } @@ -87,6 +91,7 @@ private static void processMods() throws InterruptedException { while (!(Reference.updateCount >= Reference.updateTotal)) { Thread.sleep(1); } + ModListManager.updateManifest(); log.info("Finished updating mods."); } else { diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 47b6bfe..d9adc66 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -36,7 +36,7 @@ public void updateMod(final Mod mod) { updateCurseMod((CurseMod) mod); } - private void downloadCurseMod(final CurseMod mod) { + private static void downloadCurseMod(final CurseMod mod) { val modName = mod.getModName(); try { @@ -91,7 +91,7 @@ private static String getCurseForgeDownloadLocation(final String url, final Stri return URLHelper.decodeSpaces(encodedDownloadLocation); } - private void updateCurseMod(final CurseMod mod) { + private static void updateCurseMod(final CurseMod mod) { JSONObject fileListJson = null; try { val conn = (HttpURLConnection) new URL(mod.getProjectUrl()).openConnection(); @@ -113,7 +113,6 @@ private void updateCurseMod(final CurseMod mod) { } val newMod = getLatestVersion(Reference.mcVersion, Reference.releaseType, mod, fileListJson); - log.debug(newMod); if (mod.getFileId().compareTo(newMod.getFileId()) < 0) { log.info(String.format("Update found for %s. Most recent version is %s. Old version was %s.", mod.getProjectName(), newMod.getVersion(), mod.getVersion())); diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java index 80e1b53..8aba979 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java @@ -1,12 +1,10 @@ package com.nincraft.modpackdownloader.handler; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.net.HttpURLConnection; import java.net.URL; -import java.nio.channels.Channels; -import java.nio.channels.ReadableByteChannel; + +import org.apache.commons.io.FileUtils; import com.nincraft.modpackdownloader.container.Mod; import com.nincraft.modpackdownloader.util.FileSystemHelper; @@ -27,37 +25,20 @@ protected static void downloadFile(final Mod mod, final boolean useUserAgent) { val decodedFileName = URLHelper.decodeSpaces(mod.getFileName()); if (!FileSystemHelper.isInLocalRepo(mod.getModName(), decodedFileName) || Reference.forceDownload) { + val downloadedFile = FileSystemHelper.getDownloadedFile(mod, decodedFileName); try { - ReadableByteChannel rbc; - - if (useUserAgent) { - val conn = (HttpURLConnection) new URL(mod.getDownloadUrl()).openConnection(); - conn.addRequestProperty("User-Agent", "Mozilla/4.0"); - rbc = Channels.newChannel(conn.getInputStream()); - } else { - rbc = Channels.newChannel(new URL(mod.getDownloadUrl()).openStream()); - } - - val downloadedFile = FileSystemHelper.getDownloadedFile(mod, decodedFileName); - - val fos = new FileOutputStream(downloadedFile); - fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); - fos.close(); - - if (Reference.generateUrlTxt) { - generateUrlTxt(downloadedFile, mod); - } - - FileSystemHelper.copyToLocalRepo(mod.getModName(), downloadedFile); + FileUtils.copyURLToFile(new URL(mod.getDownloadUrl()), downloadedFile); } catch (final IOException e) { - if (!useUserAgent) { - log.warn(String.format("Error getting %s. Attempting to redownload using alternate method.", - mod.getFileName())); - downloadFile(mod, true); - } else { - log.error(String.format("Could not download %s.", mod.getFileName()), e.getMessage()); - } + log.error(String.format("Could not download %s.", mod.getFileName()), e.getMessage()); + Reference.downloadCount++; + return; + } + + if (Reference.generateUrlTxt) { + generateUrlTxt(downloadedFile, mod); } + + FileSystemHelper.copyToLocalRepo(mod.getModName(), downloadedFile); } else { FileSystemHelper.copyFromLocalRepo(mod.getModName(), decodedFileName, Reference.modFolder); } From 25e46417c0879f256ae90d733aeb9d03522e00dc Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sat, 5 Mar 2016 00:25:51 -0600 Subject: [PATCH 32/86] Add Optionals --- .../manager/ModListManager.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 00a0af5..c6d6700 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.util.List; import java.util.Map; +import java.util.Optional; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -69,8 +70,8 @@ public static void buildModList() { } val curseMods = getCurseModList(jsonLists); - if (curseMods != null) { - for (val curseMod : curseMods) { + if (curseMods.isPresent()) { + for (val curseMod : curseMods.get()) { val mod = new CurseMod((JSONObject) curseMod); addMod(mod); processedCurseMods = true; @@ -79,8 +80,8 @@ public static void buildModList() { } val thirdPartyMods = getThirdPartyModList(jsonLists); - if (thirdPartyMods != null) { - for (val thirdPartyMod : thirdPartyMods) { + if (thirdPartyMods.isPresent()) { + for (val thirdPartyMod : thirdPartyMods.get()) { val mod = new ThirdPartyMod((JSONObject) thirdPartyMod); addMod(mod); processedThirdPartyMods = true; @@ -96,12 +97,13 @@ public static void buildModList() { log.trace("Finished Building Mod List."); } - public static JSONArray getCurseModList(final JSONObject jsonList) { - return (JSONArray) (jsonList.containsKey("curseFiles") ? jsonList.get("curseFiles") : jsonList.get("files")); + public static Optional getCurseModList(final JSONObject jsonList) { + return Optional.ofNullable( + (JSONArray) (jsonList.containsKey("curseFiles") ? jsonList.get("curseFiles") : jsonList.get("files"))); } - public static JSONArray getThirdPartyModList(final JSONObject jsonLists) { - return (JSONArray) jsonLists.get("thirdParty"); + public static Optional getThirdPartyModList(final JSONObject jsonLists) { + return Optional.ofNullable((JSONArray) jsonLists.get("thirdParty")); } public static final void downloadMods() { From 1393f1a887843ae155cb684108730448cd571abe Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sun, 6 Mar 2016 01:31:54 -0600 Subject: [PATCH 33/86] Add Gson refactoring Not very well tested but should work --- pom.xml | 2 +- .../{CurseMod.java => CurseFile.java} | 45 +++--- .../modpackdownloader/container/Manifest.java | 24 +++ .../modpackdownloader/container/Mod.java | 16 +- .../container/ThirdParty.java | 27 ++++ .../container/ThirdPartyMod.java | 28 ---- .../handler/CurseModHandler.java | 40 ++--- .../modpackdownloader/handler/ModHandler.java | 6 +- .../manager/ModListManager.java | 107 +++++--------- .../mapper/CurseModMapper.java | 29 ---- .../mapper/ThirdPartyMapper.java | 28 ---- src/main/resources/all-mods.json | 137 ++++++++++++------ src/main/resources/manifest.json | 41 +++--- 13 files changed, 256 insertions(+), 274 deletions(-) rename src/main/java/com/nincraft/modpackdownloader/container/{CurseMod.java => CurseFile.java} (58%) create mode 100644 src/main/java/com/nincraft/modpackdownloader/container/Manifest.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/container/ThirdParty.java delete mode 100644 src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java delete mode 100644 src/main/java/com/nincraft/modpackdownloader/mapper/CurseModMapper.java delete mode 100644 src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java diff --git a/pom.xml b/pom.xml index ca1678e..4bde9bd 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ com.google.code.gson gson - 2.6.1 + 2.6.2 diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java similarity index 58% rename from src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java rename to src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java index 4374abd..4042764 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseMod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java @@ -4,8 +4,10 @@ import java.net.HttpURLConnection; import java.net.URL; -import org.json.simple.JSONObject; +import javax.annotation.Generated; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; import com.nincraft.modpackdownloader.util.Reference; import lombok.Data; @@ -13,24 +15,27 @@ import lombok.val; import lombok.extern.log4j.Log4j2; +@Generated("org.jsonschema2pojo") @Log4j2 @Data @EqualsAndHashCode(callSuper = false) -public class CurseMod extends Mod { - private Long fileId; - private Long projectId; - private String projectName; +public class CurseFile extends Mod { + + @SerializedName("fileID") + @Expose + public Integer fileID; + @SerializedName("projectID") + @Expose + public Integer projectID; private String projectUrl; + private String projectName; + + public CurseFile() { - public CurseMod() { } - public CurseMod(final JSONObject modJson) { - super(modJson); - setProjectId((Long) modJson.get("projectID")); - setFileId((Long) modJson.get("fileID")); + public void init() { setProjectUrl(buildProjectUrl()); - setDownloadUrl(getDownloadUrl()); try { val conn = (HttpURLConnection) new URL(getProjectUrl()).openConnection(); @@ -41,24 +46,18 @@ public CurseMod(final JSONObject modJson) { } catch (final IOException e) { log.error(e.getMessage()); } - } + setDownloadUrl(getDownloadUrl()); - public String buildProjectUrl() { - return String.format(Reference.CURSEFORGE_BASE_URL + "%s" + Reference.COOKIE_TEST_1, getProjectId()); } - public String getAlternateDownloadUrl(final JSONObject modJson) { - return String.format(Reference.CURSEFORGE_BASE_URL + "%s/files/%s/download", getProjectName(), getFileId()); + public String buildProjectUrl() { + return String.format(Reference.CURSEFORGE_BASE_URL + "%s" + Reference.COOKIE_TEST_1, getProjectID()); } @Override public String getDownloadUrl() { - return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectId(), - getProjectName(), getFileId()); + return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectID(), + getProjectName(), getFileID()); } - @Override - public CurseMod clone() throws CloneNotSupportedException { - return (CurseMod) super.clone(); - } -} +} \ No newline at end of file diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java new file mode 100644 index 0000000..5629e87 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java @@ -0,0 +1,24 @@ +package com.nincraft.modpackdownloader.container; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Generated; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +import lombok.Getter; + +@Generated("org.jsonschema2pojo") +@Getter +public class Manifest { + + @SerializedName("curseFiles") + @Expose + public List curseFiles = new ArrayList(); + @SerializedName("thirdParty") + @Expose + public List thirdParty = new ArrayList(); + +} diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java index 4d196d5..b5c34ed 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java @@ -1,28 +1,28 @@ package com.nincraft.modpackdownloader.container; -import org.json.simple.JSONObject; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; import lombok.Data; @Data public abstract class Mod implements Cloneable { - private String modName; private String rename; private String fileName; private String downloadUrl; private String version; + @SerializedName("name") + @Expose + public String name; + public Mod() { } - public Mod(final JSONObject modJson) { - setModName((String) modJson.get("name")); - setRename((String) modJson.get("rename")); - setVersion((String) modJson.get("version")); - } - @Override public Mod clone() throws CloneNotSupportedException { return (Mod) super.clone(); } + + public abstract void init(); } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdParty.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdParty.java new file mode 100644 index 0000000..1c684e8 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdParty.java @@ -0,0 +1,27 @@ +package com.nincraft.modpackdownloader.container; + +import javax.annotation.Generated; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +@Generated("org.jsonschema2pojo") +public class ThirdParty extends Mod{ + + @SerializedName("url") + @Expose + public String url; + + public String buildFileName() { + if (getDownloadUrl().contains(".jar")) { + return getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, + getDownloadUrl().lastIndexOf(".jar") + 4); + } + return getRename(); + } + + public void init() { + setDownloadUrl(url); + setFileName(buildFileName()); + } +} \ No newline at end of file diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java deleted file mode 100644 index 945a21c..0000000 --- a/src/main/java/com/nincraft/modpackdownloader/container/ThirdPartyMod.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.nincraft.modpackdownloader.container; - -import org.json.simple.JSONObject; - -public class ThirdPartyMod extends Mod { - - public ThirdPartyMod() { - } - - public ThirdPartyMod(final JSONObject modJson) { - super(modJson); - setDownloadUrl((String) modJson.get("url")); - setFileName(buildFileName()); - } - - private String buildFileName() { - if (getDownloadUrl().contains(".jar")) { - return getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, - getDownloadUrl().lastIndexOf(".jar") + 4); - } - return getRename(); - } - - @Override - public ThirdPartyMod clone() throws CloneNotSupportedException { - return (ThirdPartyMod) super.clone(); - } -} diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index d9adc66..0537a7f 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -15,7 +15,7 @@ import org.json.simple.parser.ParseException; import com.google.common.base.Strings; -import com.nincraft.modpackdownloader.container.CurseMod; +import com.nincraft.modpackdownloader.container.CurseFile; import com.nincraft.modpackdownloader.container.Mod; import com.nincraft.modpackdownloader.util.Reference; import com.nincraft.modpackdownloader.util.URLHelper; @@ -28,16 +28,16 @@ public class CurseModHandler extends ModHandler { @Override public void downloadMod(final Mod mod) { - downloadCurseMod((CurseMod) mod); + downloadCurseMod((CurseFile) mod); } @Override public void updateMod(final Mod mod) { - updateCurseMod((CurseMod) mod); + updateCurseMod((CurseFile) mod); } - private static void downloadCurseMod(final CurseMod mod) { - val modName = mod.getModName(); + private static void downloadCurseMod(final CurseFile mod) { + val modName = mod.getName(); try { val fileName = !Strings.isNullOrEmpty(mod.getRename()) ? mod.getRename() @@ -91,7 +91,7 @@ private static String getCurseForgeDownloadLocation(final String url, final Stri return URLHelper.decodeSpaces(encodedDownloadLocation); } - private static void updateCurseMod(final CurseMod mod) { + private static void updateCurseMod(final CurseFile mod) { JSONObject fileListJson = null; try { val conn = (HttpURLConnection) new URL(mod.getProjectUrl()).openConnection(); @@ -100,7 +100,7 @@ private static void updateCurseMod(final CurseMod mod) { val location = conn.getHeaderField("Location"); mod.setProjectName(location.split("/")[2]); - fileListJson = (JSONObject) getCurseProjectJson(mod.getProjectId(), mod.getProjectName(), new JSONParser()) + fileListJson = (JSONObject) getCurseProjectJson(mod.getProjectID(), mod.getProjectName(), new JSONParser()) .get("files"); if (fileListJson == null) { @@ -113,27 +113,27 @@ private static void updateCurseMod(final CurseMod mod) { } val newMod = getLatestVersion(Reference.mcVersion, Reference.releaseType, mod, fileListJson); - if (mod.getFileId().compareTo(newMod.getFileId()) < 0) { + if (mod.getFileID().compareTo(newMod.getFileID()) < 0) { log.info(String.format("Update found for %s. Most recent version is %s. Old version was %s.", mod.getProjectName(), newMod.getVersion(), mod.getVersion())); - mod.setFileId(newMod.getFileId()); + mod.setFileID(newMod.getFileID()); mod.setVersion(newMod.getVersion()); } - if (Strings.isNullOrEmpty(mod.getModName())) { - mod.setModName(mod.getProjectName()); + if (Strings.isNullOrEmpty(mod.getName())) { + mod.setName(mod.getProjectName()); } } - private static CurseMod getLatestVersion(final String mcVersion, final String releaseType, final CurseMod curseMod, - final JSONObject fileListJson) { + private static CurseFile getLatestVersion(final String mcVersion, final String releaseType, + final CurseFile curseMod, final JSONObject fileListJson) { log.trace("Getting most recent available file..."); - CurseMod newMod = null; + CurseFile newMod = null; try { - newMod = curseMod.clone(); + newMod = (CurseFile) curseMod.clone(); } catch (CloneNotSupportedException e) { log.warn("Couldn't clone existing mod reference, creating new one instead."); - newMod = new CurseMod(); + newMod = new CurseFile(); } for (val newFileJson : fileListJson.values()) { @@ -143,12 +143,12 @@ private static CurseMod getLatestVersion(final String mcVersion, final String re Date latestDate = date; if (!latestDate.after(date) && equalOrLessThan((String) newModJson.get("type"), releaseType) && newModJson.get("version").equals(mcVersion)) { - newMod.setFileId((Long) newModJson.get("id")); + newMod.setFileID(((Long) newModJson.get("id")).intValue()); newMod.setVersion((String) newModJson.get("name")); latestDate = date; } - if (curseMod.getFileId().equals(newMod.getFileId())) { + if (curseMod.getFileID().equals(newMod.getFileID())) { log.debug("Ensuring the current version is set on the mod."); curseMod.setVersion(newMod.getVersion()); } @@ -161,7 +161,7 @@ private static boolean equalOrLessThan(final String modRelease, final String rel return releaseType.equals(modRelease) || "beta".equals(releaseType) && "release".equals(modRelease); } - private static JSONObject getCurseProjectJson(final Long projectID, final String projectName, + private static JSONObject getCurseProjectJson(final Integer integer, final String projectName, final JSONParser projectParser) throws ParseException, IOException { log.trace("Getting CurseForge Widget JSON..."); try { @@ -170,7 +170,7 @@ private static JSONObject getCurseProjectJson(final Long projectID, final String return (JSONObject) projectParser .parse(new BufferedReader(new InputStreamReader(new URL(urlStr).openStream()))); } catch (final FileNotFoundException e) { - String urlStr = String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectID + "-" + projectName); + String urlStr = String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, integer + "-" + projectName); log.debug(urlStr); return (JSONObject) projectParser .parse(new BufferedReader(new InputStreamReader(new URL(urlStr).openStream()))); diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java index 8aba979..8cb900d 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java @@ -24,7 +24,7 @@ public abstract class ModHandler { protected static void downloadFile(final Mod mod, final boolean useUserAgent) { val decodedFileName = URLHelper.decodeSpaces(mod.getFileName()); - if (!FileSystemHelper.isInLocalRepo(mod.getModName(), decodedFileName) || Reference.forceDownload) { + if (!FileSystemHelper.isInLocalRepo(mod.getName(), decodedFileName) || Reference.forceDownload) { val downloadedFile = FileSystemHelper.getDownloadedFile(mod, decodedFileName); try { FileUtils.copyURLToFile(new URL(mod.getDownloadUrl()), downloadedFile); @@ -38,9 +38,9 @@ protected static void downloadFile(final Mod mod, final boolean useUserAgent) { generateUrlTxt(downloadedFile, mod); } - FileSystemHelper.copyToLocalRepo(mod.getModName(), downloadedFile); + FileSystemHelper.copyToLocalRepo(mod.getName(), downloadedFile); } else { - FileSystemHelper.copyFromLocalRepo(mod.getModName(), decodedFileName, Reference.modFolder); + FileSystemHelper.copyFromLocalRepo(mod.getName(), decodedFileName, Reference.modFolder); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index c6d6700..c180625 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -3,6 +3,9 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Optional; @@ -14,14 +17,15 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.nincraft.modpackdownloader.container.CurseMod; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.nincraft.modpackdownloader.container.CurseFile; +import com.nincraft.modpackdownloader.container.Manifest; import com.nincraft.modpackdownloader.container.Mod; -import com.nincraft.modpackdownloader.container.ThirdPartyMod; +import com.nincraft.modpackdownloader.container.ThirdParty; import com.nincraft.modpackdownloader.handler.CurseModHandler; import com.nincraft.modpackdownloader.handler.ModHandler; import com.nincraft.modpackdownloader.handler.ThirdPartyModHandler; -import com.nincraft.modpackdownloader.mapper.CurseModMapper; -import com.nincraft.modpackdownloader.mapper.ThirdPartyMapper; import com.nincraft.modpackdownloader.util.Reference; import lombok.val; @@ -35,30 +39,16 @@ public class ModListManager { private static boolean processedCurseMods = false; private static boolean processedThirdPartyMods = false; + private static Manifest manifestFile; + private static Gson gson = new Gson(); static { log.trace("Registering various mod type handlers..."); - MOD_HANDLERS.put(CurseMod.class, new CurseModHandler()); - MOD_HANDLERS.put(ThirdPartyMod.class, new ThirdPartyModHandler()); + MOD_HANDLERS.put(CurseFile.class, new CurseModHandler()); + MOD_HANDLERS.put(ThirdParty.class, new ThirdPartyModHandler()); log.trace("Finished registering various mod type handlers."); } - public static void addMod(final Mod mod) { - MOD_LIST.add(mod); - } - - public static void clearModList() { - MOD_LIST.clear(); - } - - public static void sortModList() { - MOD_LIST.sort((mod1, mod2) -> mod1.getModName().compareToIgnoreCase(mod2.getModName())); - } - - public static int getModListCount() { - return MOD_LIST.size(); - } - public static void buildModList() { log.trace("Building Mod List..."); JSONObject jsonLists = null; @@ -69,31 +59,29 @@ public static void buildModList() { return; } - val curseMods = getCurseModList(jsonLists); - if (curseMods.isPresent()) { - for (val curseMod : curseMods.get()) { - val mod = new CurseMod((JSONObject) curseMod); - addMod(mod); - processedCurseMods = true; - log.debug(String.format("Curse Mod '%s' found.", mod.getModName())); - } - } + manifestFile = gson.fromJson(jsonLists.toString(), Manifest.class); - val thirdPartyMods = getThirdPartyModList(jsonLists); - if (thirdPartyMods.isPresent()) { - for (val thirdPartyMod : thirdPartyMods.get()) { - val mod = new ThirdPartyMod((JSONObject) thirdPartyMod); - addMod(mod); - processedThirdPartyMods = true; - log.debug(String.format("Third Party Mod '%s' found.", mod.getModName())); - } - } + MOD_LIST.addAll(manifestFile.getCurseFiles()); + processedCurseMods = true; - Reference.updateTotal = Reference.downloadTotal = getModListCount(); + MOD_LIST.addAll(manifestFile.getThirdParty()); + processedThirdPartyMods = true; + + Reference.updateTotal = Reference.downloadTotal = MOD_LIST.size(); log.debug(String.format("A total of %s mods will be %s.", Reference.downloadTotal, Reference.updateMods ? "updated" : "downloaded")); - sortModList(); + Comparator compareMods = new Comparator() { + @Override + public int compare(Mod mod1, Mod mod2) { + return mod1.getName().toLowerCase().compareTo(mod2.getName().toLowerCase()); + } + }; + + Collections.sort(MOD_LIST, compareMods); + + MOD_LIST.forEach(Mod::init); + log.trace("Finished Building Mod List."); } @@ -110,12 +98,12 @@ public static final void downloadMods() { log.trace(String.format("Downloading %s mods...", MOD_LIST.size())); int downloadCount = 1; for (val mod : MOD_LIST) { - log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, mod.getModName(), downloadCount++, + log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, mod.getName(), downloadCount++, Reference.downloadTotal)); new Thread(() -> { MOD_HANDLERS.get(mod.getClass()).downloadMod(mod); Reference.downloadCount++; - log.info(String.format("Finished downloading %s", mod.getModName())); + log.info(String.format("Finished downloading %s", mod.getName())); }).start(); } log.trace(String.format("Finished downloading %s mods.", MOD_LIST.size())); @@ -125,12 +113,12 @@ public static final void updateMods() { log.trace(String.format("Updating %s mods...", Reference.updateTotal)); int updateCount = 1; for (val mod : MOD_LIST) { - log.info(String.format(Reference.UPDATING_MOD_X_OF_Y, mod.getModName(), updateCount++, + log.info(String.format(Reference.UPDATING_MOD_X_OF_Y, mod.getName(), updateCount++, Reference.updateTotal)); new Thread(() -> { MOD_HANDLERS.get(mod.getClass()).updateMod(mod); Reference.updateCount++; - log.info(String.format("Finished updating %s", mod.getModName())); + log.info(String.format("Finished updating %s", mod.getName())); }).start(); } log.trace(String.format("Finished updating %s mods.", Reference.updateTotal)); @@ -139,35 +127,14 @@ public static final void updateMods() { public static void updateManifest() { log.info("Updating Manifest File..."); try { + Gson prettyGson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation() + .disableHtmlEscaping().create(); val file = new FileWriter(Reference.manifestFile); - file.write(buildManifestJson()); + file.write(prettyGson.toJson(manifestFile)); file.flush(); file.close(); } catch (final IOException e) { log.error(e.getMessage()); } } - - @SuppressWarnings("unchecked") - public static String buildManifestJson() { - JSONObject manifest = new JSONObject(); - - if (processedCurseMods) { - manifest.put("curseFiles", new JSONArray()); - } - - if (processedThirdPartyMods) { - manifest.put("thirdParty", new JSONArray()); - } - - for (val mod : MOD_LIST) { - if (mod instanceof CurseMod && manifest.containsKey("curseFiles")) { - ((JSONArray) manifest.get("curseFiles")).add(CurseModMapper.map((CurseMod) mod)); - } else if (mod instanceof ThirdPartyMod && manifest.containsKey("thirdParty")) { - ((JSONArray) manifest.get("thirdParty")).add(ThirdPartyMapper.map((ThirdPartyMod) mod)); - } - } - - return manifest.toString(); - } } diff --git a/src/main/java/com/nincraft/modpackdownloader/mapper/CurseModMapper.java b/src/main/java/com/nincraft/modpackdownloader/mapper/CurseModMapper.java deleted file mode 100644 index 6b8362e..0000000 --- a/src/main/java/com/nincraft/modpackdownloader/mapper/CurseModMapper.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.nincraft.modpackdownloader.mapper; - -import org.json.simple.JSONObject; - -import com.google.common.base.Strings; -import com.nincraft.modpackdownloader.container.CurseMod; - -import lombok.val; - -public class CurseModMapper { - @SuppressWarnings("unchecked") - public static JSONObject map(final CurseMod mod) { - val json = new JSONObject(); - - json.put("name", mod.getModName()); - json.put("projectID", mod.getProjectId()); - json.put("fileID", mod.getFileId()); - - if (!Strings.isNullOrEmpty(mod.getRename())) { - json.put("rename", mod.getRename()); - } - - return json; - } - - public static CurseMod map(final JSONObject json) { - return new CurseMod(json); - } -} diff --git a/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java b/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java deleted file mode 100644 index 81e6b0c..0000000 --- a/src/main/java/com/nincraft/modpackdownloader/mapper/ThirdPartyMapper.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.nincraft.modpackdownloader.mapper; - -import org.json.simple.JSONObject; - -import com.google.common.base.Strings; -import com.nincraft.modpackdownloader.container.ThirdPartyMod; - -import lombok.val; - -public class ThirdPartyMapper { - @SuppressWarnings("unchecked") - public static JSONObject map(final ThirdPartyMod mod) { - val json = new JSONObject(); - - json.put("name", mod.getModName()); - json.put("url", mod.getDownloadUrl()); - - if (!Strings.isNullOrEmpty(mod.getRename())) { - json.put("rename", mod.getRename()); - } - - return json; - } - - public static ThirdPartyMod map(final JSONObject json) { - return new ThirdPartyMod(json); - } -} diff --git a/src/main/resources/all-mods.json b/src/main/resources/all-mods.json index 19dba28..951a2c5 100644 --- a/src/main/resources/all-mods.json +++ b/src/main/resources/all-mods.json @@ -1,114 +1,163 @@ { "curseFiles": [ - { - "fileID": 2273274, - "projectID": 227083 - }, { "fileID": 2277394, + "name": "applecore", "projectID": 224472 }, + { + "fileID": 2284622, + "name": "aroma1997core", + "projectID": 223735 + }, + { + "fileID": 2277001, + "name": "aromabackup", + "projectID": 225658 + }, + { + "fileID": 2273274, + "name": "baubles", + "projectID": 227083 + }, { "fileID": 2274840, + "name": "baubleshud", "projectID": 230751 }, { "fileID": 2279388, + "name": "biomes-o-plenty", "projectID": 220318 }, { "fileID": 2279356, + "name": "blood-magic", "projectID": 224791 }, { "fileID": 2269766, + "name": "bloodmoon", "projectID": 226321 }, { - "fileID": 2279333, + "fileID": 2280253, + "name": "chisels-bits", "projectID": 231095 }, { - "fileID": 2273148, - "projectID": 226099 + "fileID": 2273148, + "name": "durability-show", + "projectID": 226099 + }, + { + "fileID": 2269767, + "name": "fast-leaf-decay", + "projectID": 225839 + }, + { + "fileID": 2272962, + "name": "helpfixer", + "projectID": 223797 + }, + { + "fileID": 2281942, + "name": "immersive-craft", + "projectID": 241268 }, { - "fileID": 2269767, - "projectID": 225839 + "fileID": 2283028, + "name": "in-case-of-emergency", + "projectID": 241925 }, { - "fileID": 2272962, - "projectID": 223797 + "fileID": 2280666, + "name": "intangible", + "projectID": 241350 }, { - "fileID": 2279274, - "projectID": 241268 + "fileID": 2275675, + "name": "inventory-tweaks", + "projectID": 223094 }, { - "fileID": 2275675, - "projectID": 223094 + "fileID": 2278859, + "name": "iron-chests", + "projectID": 228756 }, { - "fileID": 2278859, - "projectID": 228756 + "fileID": 2282784, + "name": "just-enough-items-jei", + "projectID": 238222 }, { - "fileID": 2279280, - "projectID": 238222 + "fileID": 2276884, + "name": "mantle", + "projectID": 74924 }, { - "fileID": 2276884, - "projectID": 74924 + "fileID": 2276306, + "name": "mcmultipart", + "projectID": 239431 }, { - "fileID": 2276306, - "projectID": 239431 + "fileID": 2280806, + "name": "not-enough-wands", + "projectID": 235595 }, { - "fileID": 2278855, - "projectID": 235595 + "fileID": 2280569, + "name": "opencomputers", + "projectID": 223008 }, { - "fileID": 2279372, - "projectID": 223008 + "fileID": 2284425, + "name": "psi", + "projectID": 241665 }, { - "fileID": 2276399, - "projectID": 59816 + "fileID": 2283433, + "name": "random-things", + "projectID": 59816 }, { - "fileID": 2275910, - "projectID": 240566 + "fileID": 2275910, + "name": "simpleretrogen", + "projectID": 240566 }, { - "fileID": 2279246, - "projectID": 241113 + "fileID": 2279246, + "name": "substratum", + "projectID": 241113 }, { - "fileID": 2276883, - "projectID": 74072 + "fileID": 2279928, + "name": "thaumcraft", + "projectID": 223628 }, { - "fileID": 2276528, - "projectID": 223628 + "fileID": 2276883, + "name": "tinkers-construct", + "projectID": 74072 }, { - "fileID": 2276285, - "projectID": 238603 + "fileID": 2279788, + "name": "tis-3d", + "projectID": 238603 } ], "thirdParty": [ { - "name": "Botania", - "url": "https://github.com/williewillus/Botania/releases/download/v0.5.3-beta/Botania.r1.8-248-MC1.8-779cc76.jar" + "name": "Botania", + "url": "https://github.com/williewillus/Botania/releases/download/v0.6.2-beta/Botania.r1.8-248-unofficial-0.6.2.jar" }, { - "name": "Buildcraft", + "name": "Buildcraft", "url": "https://www.dropbox.com/s/q1qppz3sq447173/buildcraft-7.2.0-pre6.jar?dl=1" }, { - "name": "Charset", - "url": "http://charset.asie.pl/files/Charset-0.2.1-945baf5589832f727c42cf77.jar" + "name": "Charset", + "url": "http://charset.asie.pl/files/Charset-0.2.1-945baf5589832f727c42cf77.jar" } ] } \ No newline at end of file diff --git a/src/main/resources/manifest.json b/src/main/resources/manifest.json index 123b25b..34b3819 100644 --- a/src/main/resources/manifest.json +++ b/src/main/resources/manifest.json @@ -1,22 +1,23 @@ { - "curseFiles": [ - { - "fileID": 2269703, - "projectID": 237892 - } - ], - "thirdParty": [ - { - "name": "Yampst-TWBB", - "url": "http://en.yampst.net/gettracker.php/Yampst-There%20Will%20Be%20Blood-0.5.0.jar?trackid=fa65e1dc97876791e521a3d6e6b0ddea27e86ee4010b5d47e9d48767&file=e775b851a66544fcc52a1f3404ee7eb023c7964a47edfc639afa9bcb" - }, - { - "name": "Buildcraft", - "url": "https://www.dropbox.com/s/q1qppz3sq447173/buildcraft-7.2.0-pre6.jar?dl=1" - }, - { - "name": "Charset", - "url": "http://charset.asie.pl/files/Charset-0.2.1-945baf5589832f727c42cf77.jar" - } - ] + "curseFiles": [ + { + "fileID": 2269703, + "projectID": 237892, + "name": "thaumcraft" + } + ], + "thirdParty": [ + { + "url": "http://en.yampst.net/gettracker.php/Yampst-There%20Will%20Be%20Blood-0.5.0.jar?trackid=fa65e1dc97876791e521a3d6e6b0ddea27e86ee4010b5d47e9d48767&file=e775b851a66544fcc52a1f3404ee7eb023c7964a47edfc639afa9bcb", + "name": "Yampst-TWBB" + }, + { + "url": "https://www.dropbox.com/s/q1qppz3sq447173/buildcraft-7.2.0-pre6.jar?dl=1", + "name": "Buildcraft" + }, + { + "url": "http://charset.asie.pl/files/Charset-0.2.1-945baf5589832f727c42cf77.jar", + "name": "Charset" + } + ] } \ No newline at end of file From fe939147fd6bf1e0031eab37c3f61f9bf12621a3 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sun, 6 Mar 2016 20:38:51 -0600 Subject: [PATCH 34/86] Fixed logic with mod updating --- .../nincraft/modpackdownloader/handler/CurseModHandler.java | 5 ++++- .../nincraft/modpackdownloader/manager/ModListManager.java | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 0537a7f..159fcb0 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -136,11 +136,14 @@ private static CurseFile getLatestVersion(final String mcVersion, final String r newMod = new CurseFile(); } + Date latestDate = null; for (val newFileJson : fileListJson.values()) { val newModJson = (JSONObject) newFileJson; val date = parseDate((String) newModJson.get("created_at")); - Date latestDate = date; + if(latestDate == null){ + latestDate = date; + } if (!latestDate.after(date) && equalOrLessThan((String) newModJson.get("type"), releaseType) && newModJson.get("version").equals(mcVersion)) { newMod.setFileID(((Long) newModJson.get("id")).intValue()); diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index c180625..045cf35 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -3,7 +3,6 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; From 6796804b441ac23c5387619e3733e3ac4821c9fb Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 7 Mar 2016 00:39:21 -0600 Subject: [PATCH 35/86] Refactor updater That was more complicated than it needed to be --- .gitignore | 1 + .../handler/CurseModHandler.java | 45 ++++++------------- src/main/resources/manifest.json | 2 +- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 56ba4cf..699bda7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ target/ cache/ all/ *.jar +logs/ diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 159fcb0..01e2602 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -7,8 +7,9 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -136,26 +137,19 @@ private static CurseFile getLatestVersion(final String mcVersion, final String r newMod = new CurseFile(); } - Date latestDate = null; - for (val newFileJson : fileListJson.values()) { - val newModJson = (JSONObject) newFileJson; - val date = parseDate((String) newModJson.get("created_at")); - - if(latestDate == null){ - latestDate = date; - } - if (!latestDate.after(date) && equalOrLessThan((String) newModJson.get("type"), releaseType) - && newModJson.get("version").equals(mcVersion)) { - newMod.setFileID(((Long) newModJson.get("id")).intValue()); - newMod.setVersion((String) newModJson.get("name")); - latestDate = date; - } - - if (curseMod.getFileID().equals(newMod.getFileID())) { - log.debug("Ensuring the current version is set on the mod."); - curseMod.setVersion(newMod.getVersion()); + List fileList = new ArrayList(fileListJson.values()); + List fileIds = new ArrayList(); + for (JSONObject file : fileList){ + if (equalOrLessThan((String) file.get("type"), releaseType) && file.get("version").equals(mcVersion)) { + fileIds.add((Long) file.get("id")); } } + Collections.sort(fileIds); + Collections.reverse(fileIds); + if(fileIds.get(0).intValue() != curseMod.getFileID()){ + newMod.setFileID(fileIds.get(0).intValue()); + } + log.trace("Finished getting most recent available file."); return newMod; } @@ -181,15 +175,4 @@ private static JSONObject getCurseProjectJson(final Integer integer, final Strin log.trace("Finished Getting CurseForge Widget JSON."); } } - - private static Date parseDate(final String date) { - for (val parse : Reference.DATE_FORMATS) { - try { - return new SimpleDateFormat(parse).parse(date); - } catch (final java.text.ParseException e) { - log.error(e.getMessage()); - } - } - return null; - } } diff --git a/src/main/resources/manifest.json b/src/main/resources/manifest.json index 34b3819..652e275 100644 --- a/src/main/resources/manifest.json +++ b/src/main/resources/manifest.json @@ -2,7 +2,7 @@ "curseFiles": [ { "fileID": 2269703, - "projectID": 237892, + "projectID": 223628, "name": "thaumcraft" } ], From 56f2efdeba058db74e087c7e8cf677e038987967 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 9 Mar 2016 19:08:19 -0600 Subject: [PATCH 36/86] Fix null pointer --- .../com/nincraft/modpackdownloader/handler/CurseModHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 01e2602..14a66c3 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -146,7 +146,7 @@ private static CurseFile getLatestVersion(final String mcVersion, final String r } Collections.sort(fileIds); Collections.reverse(fileIds); - if(fileIds.get(0).intValue() != curseMod.getFileID()){ + if(!fileIds.isEmpty() && fileIds.get(0).intValue() != curseMod.getFileID()){ newMod.setFileID(fileIds.get(0).intValue()); } From ee304c96ba4fdf87f060aba3e2ce71c9fa0e4a26 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 9 Mar 2016 21:23:05 -0600 Subject: [PATCH 37/86] Refactor out project name in favor of name --- .../modpackdownloader/container/CurseFile.java | 5 ++--- .../modpackdownloader/container/Manifest.java | 2 ++ .../modpackdownloader/handler/CurseModHandler.java | 12 ++++-------- .../modpackdownloader/manager/ModListManager.java | 3 ++- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java index 4042764..d785675 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java @@ -28,7 +28,6 @@ public class CurseFile extends Mod { @Expose public Integer projectID; private String projectUrl; - private String projectName; public CurseFile() { @@ -42,7 +41,7 @@ public void init() { conn.setInstanceFollowRedirects(false); conn.connect(); - setProjectName(conn.getHeaderField("Location").split("/")[2]); + setName(conn.getHeaderField("Location").split("/")[2]); } catch (final IOException e) { log.error(e.getMessage()); } @@ -57,7 +56,7 @@ public String buildProjectUrl() { @Override public String getDownloadUrl() { return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectID(), - getProjectName(), getFileID()); + getName(), getFileID()); } } \ No newline at end of file diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java index 5629e87..45adf5c 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java @@ -17,6 +17,8 @@ public class Manifest { @SerializedName("curseFiles") @Expose public List curseFiles = new ArrayList(); + @SerializedName("files") + public List curseManifestFiles = new ArrayList(); @SerializedName("thirdParty") @Expose public List thirdParty = new ArrayList(); diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 14a66c3..b28d062 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -100,12 +100,12 @@ private static void updateCurseMod(final CurseFile mod) { conn.connect(); val location = conn.getHeaderField("Location"); - mod.setProjectName(location.split("/")[2]); - fileListJson = (JSONObject) getCurseProjectJson(mod.getProjectID(), mod.getProjectName(), new JSONParser()) + mod.setName(location.split("/")[2]); + fileListJson = (JSONObject) getCurseProjectJson(mod.getProjectID(), mod.getName(), new JSONParser()) .get("files"); if (fileListJson == null) { - log.error(String.format("No file list found for %s, and will be skipped.", mod.getProjectName())); + log.error(String.format("No file list found for %s, and will be skipped.", mod.getName())); return; } } catch (IOException | ParseException e) { @@ -116,14 +116,10 @@ private static void updateCurseMod(final CurseFile mod) { val newMod = getLatestVersion(Reference.mcVersion, Reference.releaseType, mod, fileListJson); if (mod.getFileID().compareTo(newMod.getFileID()) < 0) { log.info(String.format("Update found for %s. Most recent version is %s. Old version was %s.", - mod.getProjectName(), newMod.getVersion(), mod.getVersion())); + mod.getName(), newMod.getVersion(), mod.getVersion())); mod.setFileID(newMod.getFileID()); mod.setVersion(newMod.getVersion()); } - - if (Strings.isNullOrEmpty(mod.getName())) { - mod.setName(mod.getProjectName()); - } } private static CurseFile getLatestVersion(final String mcVersion, final String releaseType, diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 045cf35..3808ad5 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -59,7 +59,8 @@ public static void buildModList() { } manifestFile = gson.fromJson(jsonLists.toString(), Manifest.class); - + + manifestFile.getCurseFiles().addAll(manifestFile.getCurseManifestFiles()); MOD_LIST.addAll(manifestFile.getCurseFiles()); processedCurseMods = true; From ef4960de0abddcaf4b05aba3a2b7531ede0ffa90 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Thu, 10 Mar 2016 00:26:16 -0600 Subject: [PATCH 38/86] Sort manifest JSONs during updates --- .../modpackdownloader/manager/ModListManager.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 3808ad5..cd9a996 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -40,6 +40,13 @@ public class ModListManager { private static boolean processedThirdPartyMods = false; private static Manifest manifestFile; private static Gson gson = new Gson(); + + private static Comparator compareMods = new Comparator() { + @Override + public int compare(Mod mod1, Mod mod2) { + return mod1.getName().toLowerCase().compareTo(mod2.getName().toLowerCase()); + } + }; static { log.trace("Registering various mod type handlers..."); @@ -71,12 +78,6 @@ public static void buildModList() { log.debug(String.format("A total of %s mods will be %s.", Reference.downloadTotal, Reference.updateMods ? "updated" : "downloaded")); - Comparator compareMods = new Comparator() { - @Override - public int compare(Mod mod1, Mod mod2) { - return mod1.getName().toLowerCase().compareTo(mod2.getName().toLowerCase()); - } - }; Collections.sort(MOD_LIST, compareMods); @@ -127,6 +128,8 @@ public static final void updateMods() { public static void updateManifest() { log.info("Updating Manifest File..."); try { + manifestFile.getCurseFiles().sort(compareMods); + manifestFile.getThirdParty().sort(compareMods); Gson prettyGson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation() .disableHtmlEscaping().create(); val file = new FileWriter(Reference.manifestFile); From d66b17168c4266963b0e574ce993a9686c0590ed Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Thu, 10 Mar 2016 20:40:29 -0600 Subject: [PATCH 39/86] Add @Utility and remove unused variables --- .../nincraft/modpackdownloader/manager/ModListManager.java | 6 ------ .../nincraft/modpackdownloader/util/FileSystemHelper.java | 4 ++-- .../java/com/nincraft/modpackdownloader/util/URLHelper.java | 6 +++--- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index cd9a996..adf2583 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -36,8 +36,6 @@ public class ModListManager { public static final Map, ModHandler> MOD_HANDLERS = Maps.newHashMap(); - private static boolean processedCurseMods = false; - private static boolean processedThirdPartyMods = false; private static Manifest manifestFile; private static Gson gson = new Gson(); @@ -69,11 +67,7 @@ public static void buildModList() { manifestFile.getCurseFiles().addAll(manifestFile.getCurseManifestFiles()); MOD_LIST.addAll(manifestFile.getCurseFiles()); - processedCurseMods = true; - MOD_LIST.addAll(manifestFile.getThirdParty()); - processedThirdPartyMods = true; - Reference.updateTotal = Reference.downloadTotal = MOD_LIST.size(); log.debug(String.format("A total of %s mods will be %s.", Reference.downloadTotal, Reference.updateMods ? "updated" : "downloaded")); diff --git a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java index 2c324b3..4262f46 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java @@ -8,12 +8,12 @@ import com.nincraft.modpackdownloader.container.Mod; import lombok.val; +import lombok.experimental.UtilityClass; import lombok.extern.log4j.Log4j2; @Log4j2 +@UtilityClass public final class FileSystemHelper { - private FileSystemHelper() { - } public static void createFolder(final String folder) { if (folder != null) { diff --git a/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java index 053ec14..1a3c41d 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/URLHelper.java @@ -1,12 +1,12 @@ package com.nincraft.modpackdownloader.util; +import lombok.experimental.UtilityClass; + +@UtilityClass public final class URLHelper { private static final String WHITESPACE = " "; private static final String WHITESPACE_ENCODED = "%20"; - private URLHelper() { - } - public static String encodeSpaces(final String url) { return url.replace(WHITESPACE, WHITESPACE_ENCODED); } From 6f8812c147531155bd80a6cfe1bc239d23a52b4f Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Thu, 10 Mar 2016 22:54:22 -0600 Subject: [PATCH 40/86] Fix new version not logging Closes #12 --- .../modpackdownloader/handler/CurseModHandler.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index b28d062..dea0ecc 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -115,8 +115,8 @@ private static void updateCurseMod(final CurseFile mod) { val newMod = getLatestVersion(Reference.mcVersion, Reference.releaseType, mod, fileListJson); if (mod.getFileID().compareTo(newMod.getFileID()) < 0) { - log.info(String.format("Update found for %s. Most recent version is %s. Old version was %s.", - mod.getName(), newMod.getVersion(), mod.getVersion())); + log.info(String.format("Update found for %s. Most recent version is %s.", mod.getName(), + newMod.getVersion())); mod.setFileID(newMod.getFileID()); mod.setVersion(newMod.getVersion()); } @@ -135,17 +135,18 @@ private static CurseFile getLatestVersion(final String mcVersion, final String r List fileList = new ArrayList(fileListJson.values()); List fileIds = new ArrayList(); - for (JSONObject file : fileList){ + for (JSONObject file : fileList) { if (equalOrLessThan((String) file.get("type"), releaseType) && file.get("version").equals(mcVersion)) { fileIds.add((Long) file.get("id")); } } Collections.sort(fileIds); Collections.reverse(fileIds); - if(!fileIds.isEmpty() && fileIds.get(0).intValue() != curseMod.getFileID()){ + if (!fileIds.isEmpty() && fileIds.get(0).intValue() != curseMod.getFileID()) { newMod.setFileID(fileIds.get(0).intValue()); + newMod.setVersion((String) ((JSONObject) fileListJson.get(newMod.getFileID().toString())).get("name")); } - + log.trace("Finished getting most recent available file."); return newMod; } From b4727f3f2d86b96a4b2fe493272d10ca14fe53f6 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Thu, 10 Mar 2016 23:02:48 -0600 Subject: [PATCH 41/86] Add backup for Curse manifest Just in case --- .../manager/ModListManager.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index adf2583..27bbb76 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -1,5 +1,6 @@ package com.nincraft.modpackdownloader.manager; +import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; @@ -9,6 +10,7 @@ import java.util.Map; import java.util.Optional; +import org.apache.commons.io.FileUtils; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -38,7 +40,7 @@ public class ModListManager { private static Manifest manifestFile; private static Gson gson = new Gson(); - + private static Comparator compareMods = new Comparator() { @Override public int compare(Mod mod1, Mod mod2) { @@ -64,7 +66,9 @@ public static void buildModList() { } manifestFile = gson.fromJson(jsonLists.toString(), Manifest.class); - + if (!manifestFile.getCurseManifestFiles().isEmpty()) { + backupCurseManifest(); + } manifestFile.getCurseFiles().addAll(manifestFile.getCurseManifestFiles()); MOD_LIST.addAll(manifestFile.getCurseFiles()); MOD_LIST.addAll(manifestFile.getThirdParty()); @@ -72,14 +76,21 @@ public static void buildModList() { log.debug(String.format("A total of %s mods will be %s.", Reference.downloadTotal, Reference.updateMods ? "updated" : "downloaded")); - Collections.sort(MOD_LIST, compareMods); - + MOD_LIST.forEach(Mod::init); - + log.trace("Finished Building Mod List."); } + private static void backupCurseManifest() { + try { + FileUtils.copyFile(new File(Reference.manifestFile), new File(Reference.manifestFile + ".bak"), true); + } catch (IOException e) { + log.error("Could not backup Curse manifest file", e.getMessage()); + } + } + public static Optional getCurseModList(final JSONObject jsonList) { return Optional.ofNullable( (JSONArray) (jsonList.containsKey("curseFiles") ? jsonList.get("curseFiles") : jsonList.get("files"))); @@ -108,8 +119,7 @@ public static final void updateMods() { log.trace(String.format("Updating %s mods...", Reference.updateTotal)); int updateCount = 1; for (val mod : MOD_LIST) { - log.info(String.format(Reference.UPDATING_MOD_X_OF_Y, mod.getName(), updateCount++, - Reference.updateTotal)); + log.info(String.format(Reference.UPDATING_MOD_X_OF_Y, mod.getName(), updateCount++, Reference.updateTotal)); new Thread(() -> { MOD_HANDLERS.get(mod.getClass()).updateMod(mod); Reference.updateCount++; From f8abc5a2461613d15e7365bb30343c47403dac91 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 14 Mar 2016 23:18:11 -0500 Subject: [PATCH 42/86] Add application update option --- pom.xml | 8 ++- .../modpackdownloader/ModPackDownloader.java | 7 ++- .../handler/ApplicationUpdateHandeler.java | 51 +++++++++++++++++++ .../modpackdownloader/util/Reference.java | 1 + src/main/resources/latest.json | 3 ++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/nincraft/modpackdownloader/handler/ApplicationUpdateHandeler.java create mode 100644 src/main/resources/latest.json diff --git a/pom.xml b/pom.xml index 4bde9bd..4c51313 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,13 @@ - ${project.artifactId} ${project.version}+${build.number} + ${project.artifactId}-${project.version}+${build.number} + + + src/main/resources + true + + maven-compiler-plugin diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 1fb7815..d0da765 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -1,5 +1,6 @@ package com.nincraft.modpackdownloader; +import com.nincraft.modpackdownloader.handler.ApplicationUpdateHandeler; import com.nincraft.modpackdownloader.manager.ModListManager; import com.nincraft.modpackdownloader.util.FileSystemHelper; import com.nincraft.modpackdownloader.util.Reference; @@ -10,7 +11,11 @@ @Log4j2 public class ModPackDownloader { public static void main(final String[] args) throws InterruptedException { - if (args.length < 2) { + if ("-updateApp".equals(args[0])){ + ApplicationUpdateHandeler.update(); + return; + } + else if (args.length < 2) { log.error("Arguments required: manifest file location, mod download location"); return; } else { diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ApplicationUpdateHandeler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ApplicationUpdateHandeler.java new file mode 100644 index 0000000..db0f2e3 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ApplicationUpdateHandeler.java @@ -0,0 +1,51 @@ +package com.nincraft.modpackdownloader.handler; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; + +import org.apache.commons.io.FileUtils; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.nincraft.modpackdownloader.util.FileSystemHelper; +import com.nincraft.modpackdownloader.util.Reference; +import com.nincraft.modpackdownloader.util.URLHelper; + +import lombok.extern.log4j.Log4j2; + +@Log4j2 +public class ApplicationUpdateHandeler { + + public static void update() { + JSONParser parser = new JSONParser(); + JSONObject appJson = null; + try { + appJson = (JSONObject) parser + .parse(new BufferedReader(new InputStreamReader(new URL(Reference.updateAppURL).openStream()))); + } catch (IOException | ParseException e) { + log.error("Failed to get latest download link, Nincraft server down?", e.getMessage()); + return; + } + String downloadUrl = (String) appJson.get("url"); + String appName = URLHelper + .decodeSpaces(downloadUrl.substring(downloadUrl.lastIndexOf('/') + 1, downloadUrl.length())); + File updatedApp = FileSystemHelper.getDownloadedFile(appName); + if (updatedApp.exists()) { + log.info("No new updates found"); + return; + } else { + log.info("Update found, downloading " + appName); + } + try { + FileUtils.copyURLToFile(new URL(downloadUrl), updatedApp); + } catch (IOException e) { + log.error("Failed to download update", e.getMessage()); + } + log.info("Downloaded " + appName); + } + +} diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index c6acbf9..1dba279 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -29,4 +29,5 @@ public class Reference { public static int downloadTotal = 0; public static int updateCount = 0; public static int updateTotal = 0; + public static String updateAppURL = "http://play.nincraft.com:8080/job/Mod%20Pack%20Downloader/lastSuccessfulBuild/artifact/target/classes/latest.json"; } diff --git a/src/main/resources/latest.json b/src/main/resources/latest.json new file mode 100644 index 0000000..461fc65 --- /dev/null +++ b/src/main/resources/latest.json @@ -0,0 +1,3 @@ +{ + "url": "http://play.nincraft.com:8080/job/Mod%20Pack%20Downloader/lastSuccessfulBuild/artifact/target/${project.build.finalName}" +} \ No newline at end of file From 7d2bc09c5c3ed2a3c7b822503d676183ea60dbff Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 14 Mar 2016 23:20:01 -0500 Subject: [PATCH 43/86] Remove param from function We weren't even using it --- .../java/com/nincraft/modpackdownloader/handler/ModHandler.java | 2 +- .../com/nincraft/modpackdownloader/util/FileSystemHelper.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java index 8cb900d..cbde079 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java @@ -25,7 +25,7 @@ protected static void downloadFile(final Mod mod, final boolean useUserAgent) { val decodedFileName = URLHelper.decodeSpaces(mod.getFileName()); if (!FileSystemHelper.isInLocalRepo(mod.getName(), decodedFileName) || Reference.forceDownload) { - val downloadedFile = FileSystemHelper.getDownloadedFile(mod, decodedFileName); + val downloadedFile = FileSystemHelper.getDownloadedFile(decodedFileName); try { FileUtils.copyURLToFile(new URL(mod.getDownloadUrl()), downloadedFile); } catch (final IOException e) { diff --git a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java index 4262f46..2cb8aa3 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java @@ -48,7 +48,7 @@ public static boolean isInLocalRepo(final String projectName, final String fileN return getLocalFile(fileName, getProjectNameOrDefault(projectName)).exists(); } - public static File getDownloadedFile(final Mod mod, final String fileName) { + public static File getDownloadedFile(final String fileName) { if (Reference.modFolder != null) { createFolder(Reference.modFolder); return new File(Reference.modFolder + File.separator + fileName); From 7c6a486edf67b51c9687b3f75ed1ab775f88160d Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 14 Mar 2016 23:25:25 -0500 Subject: [PATCH 44/86] Fix app updater --- .../modpackdownloader/handler/ApplicationUpdateHandeler.java | 1 + src/main/resources/latest.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ApplicationUpdateHandeler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ApplicationUpdateHandeler.java index db0f2e3..f1dc880 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ApplicationUpdateHandeler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ApplicationUpdateHandeler.java @@ -44,6 +44,7 @@ public static void update() { FileUtils.copyURLToFile(new URL(downloadUrl), updatedApp); } catch (IOException e) { log.error("Failed to download update", e.getMessage()); + return; } log.info("Downloaded " + appName); } diff --git a/src/main/resources/latest.json b/src/main/resources/latest.json index 461fc65..79faa32 100644 --- a/src/main/resources/latest.json +++ b/src/main/resources/latest.json @@ -1,3 +1,3 @@ { - "url": "http://play.nincraft.com:8080/job/Mod%20Pack%20Downloader/lastSuccessfulBuild/artifact/target/${project.build.finalName}" + "url": "http://play.nincraft.com:8080/job/Mod%20Pack%20Downloader/lastSuccessfulBuild/artifact/target/${project.build.finalName}.jar" } \ No newline at end of file From 6995bdd0c9a52c259399e1cb38ccfe7924a8326a Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Tue, 15 Mar 2016 22:17:50 -0500 Subject: [PATCH 45/86] Add release override for Curse mods Closes #4 --- .../com/nincraft/modpackdownloader/container/CurseFile.java | 3 +++ .../nincraft/modpackdownloader/handler/CurseModHandler.java | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java index d785675..1ace4af 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java @@ -27,6 +27,9 @@ public class CurseFile extends Mod { @SerializedName("projectID") @Expose public Integer projectID; + @SerializedName("release") + @Expose + public String releaseType; private String projectUrl; public CurseFile() { diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index dea0ecc..e11dff9 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -113,7 +113,8 @@ private static void updateCurseMod(final CurseFile mod) { return; } - val newMod = getLatestVersion(Reference.mcVersion, Reference.releaseType, mod, fileListJson); + val newMod = getLatestVersion(Reference.mcVersion, mod.getReleaseType() != null ? + mod.getReleaseType() : Reference.releaseType, mod, fileListJson); if (mod.getFileID().compareTo(newMod.getFileID()) < 0) { log.info(String.format("Update found for %s. Most recent version is %s.", mod.getName(), newMod.getVersion())); @@ -152,7 +153,7 @@ private static CurseFile getLatestVersion(final String mcVersion, final String r } private static boolean equalOrLessThan(final String modRelease, final String releaseType) { - return releaseType.equals(modRelease) || "beta".equals(releaseType) && "release".equals(modRelease); + return "alpha".equals(releaseType) || releaseType.equals(modRelease) || "beta".equals(releaseType) && "release".equals(modRelease); } private static JSONObject getCurseProjectJson(final Integer integer, final String projectName, From 4388b2dcbb00711afc56a665af02ab6942c9f07b Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Thu, 17 Mar 2016 19:38:14 -0500 Subject: [PATCH 46/86] Remove Unused Import --- .../com/nincraft/modpackdownloader/util/FileSystemHelper.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java index 2cb8aa3..240d5df 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java @@ -5,8 +5,6 @@ import org.apache.commons.io.FileUtils; -import com.nincraft.modpackdownloader.container.Mod; - import lombok.val; import lombok.experimental.UtilityClass; import lombok.extern.log4j.Log4j2; From 28fc0769ca46156b9063b368cda870798fba275d Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Thu, 17 Mar 2016 19:38:49 -0500 Subject: [PATCH 47/86] Add BetterStorage & Dimensional doors to mods JSON They were breaking before, at least now we can use them to test. --- src/main/resources/all-mods.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/resources/all-mods.json b/src/main/resources/all-mods.json index 951a2c5..97d0101 100644 --- a/src/main/resources/all-mods.json +++ b/src/main/resources/all-mods.json @@ -151,6 +151,10 @@ "name": "Botania", "url": "https://github.com/williewillus/Botania/releases/download/v0.6.2-beta/Botania.r1.8-248-unofficial-0.6.2.jar" }, + { + "name": "BetterStorage", + "url": "https://github.com/copygirl/BetterStorage/releases/download/v0.13.1.127/BetterStorage-1.7.10-0.13.1.127.jar" + }, { "name": "Buildcraft", "url": "https://www.dropbox.com/s/q1qppz3sq447173/buildcraft-7.2.0-pre6.jar?dl=1" @@ -158,6 +162,10 @@ { "name": "Charset", "url": "http://charset.asie.pl/files/Charset-0.2.1-945baf5589832f727c42cf77.jar" + }, + { + "name": "Dimensional Doors", + "url": "https://github.com/CannibalVox/DimDoors/releases/download/2.2.5-test9/DimensionalDoors-2.2.5-test9.jar" } ] } \ No newline at end of file From 4815c50696051286bc6b71cbfc5b716345c61eb4 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Thu, 17 Mar 2016 19:57:02 -0500 Subject: [PATCH 48/86] Add InteliJ Idea files/folders to ignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 699bda7..2b118cc 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ cache/ all/ *.jar logs/ +.idea/ +*.iml From b8455a58cd7780c86ff8764244babb700423bf40 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Fri, 18 Mar 2016 01:08:28 -0500 Subject: [PATCH 49/86] Fix empty tags after updates --- .../nincraft/modpackdownloader/container/Manifest.java | 2 ++ .../modpackdownloader/manager/ModListManager.java | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java index 45adf5c..7273995 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java @@ -9,9 +9,11 @@ import com.google.gson.annotations.SerializedName; import lombok.Getter; +import lombok.Setter; @Generated("org.jsonschema2pojo") @Getter +@Setter public class Manifest { @SerializedName("curseFiles") diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 27bbb76..e145266 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -134,7 +134,13 @@ public static void updateManifest() { try { manifestFile.getCurseFiles().sort(compareMods); manifestFile.getThirdParty().sort(compareMods); - Gson prettyGson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation() + if (manifestFile.getCurseFiles().isEmpty()) { + manifestFile.setCurseFiles(null); + } + if (manifestFile.getThirdParty().isEmpty()) { + manifestFile.setThirdParty(null); + } + Gson prettyGson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation() .disableHtmlEscaping().create(); val file = new FileWriter(Reference.manifestFile); file.write(prettyGson.toJson(manifestFile)); From 4b97ca627fedbf9a3e55dcd58d2c69a3545b8e70 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 21 Mar 2016 20:39:09 -0500 Subject: [PATCH 50/86] Actually use rename on mod Whoops --- .../java/com/nincraft/modpackdownloader/container/Mod.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java index b5c34ed..1e18a58 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java @@ -7,7 +7,9 @@ @Data public abstract class Mod implements Cloneable { - private String rename; + @SerializedName("rename") + @Expose + private String rename; private String fileName; private String downloadUrl; private String version; From 19cff146ade64cd435fdded9da7fccb674cf2df0 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 21 Mar 2016 21:23:37 -0500 Subject: [PATCH 51/86] Add skipUpdate for mods Adding skipUpdate: true will make the updater ignore updating a mod --- .../java/com/nincraft/modpackdownloader/container/Mod.java | 3 +++ .../nincraft/modpackdownloader/handler/CurseModHandler.java | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java index 1e18a58..21ec0df 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java @@ -16,6 +16,9 @@ public abstract class Mod implements Cloneable { @SerializedName("name") @Expose public String name; + @SerializedName("skipUpdate") + @Expose + private boolean skipUpdate; public Mod() { diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index e11dff9..186dbd5 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -93,6 +93,10 @@ private static String getCurseForgeDownloadLocation(final String url, final Stri } private static void updateCurseMod(final CurseFile mod) { + if(mod.isSkipUpdate()){ + log.info("Skipped updating " + mod.getName()); + return; + } JSONObject fileListJson = null; try { val conn = (HttpURLConnection) new URL(mod.getProjectUrl()).openConnection(); From 82cbe3f70e65b5485507c509f01a2f3fe8ef9af5 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 21 Mar 2016 21:25:43 -0500 Subject: [PATCH 52/86] Fix formatting --- .../modpackdownloader/ModPackDownloader.java | 5 ++--- .../modpackdownloader/container/CurseFile.java | 4 ++-- .../nincraft/modpackdownloader/container/Mod.java | 13 ++++++------- .../modpackdownloader/container/ThirdParty.java | 4 ++-- .../handler/CurseModHandler.java | 15 ++++++++------- .../modpackdownloader/manager/ModListManager.java | 14 +++++++------- 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index d0da765..8df6a39 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -11,11 +11,10 @@ @Log4j2 public class ModPackDownloader { public static void main(final String[] args) throws InterruptedException { - if ("-updateApp".equals(args[0])){ + if ("-updateApp".equals(args[0])) { ApplicationUpdateHandeler.update(); return; - } - else if (args.length < 2) { + } else if (args.length < 2) { log.error("Arguments required: manifest file location, mod download location"); return; } else { diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java index 1ace4af..28adad3 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java @@ -58,8 +58,8 @@ public String buildProjectUrl() { @Override public String getDownloadUrl() { - return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectID(), - getName(), getFileID()); + return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectID(), getName(), + getFileID()); } } \ No newline at end of file diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java index 21ec0df..aed0880 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java @@ -7,9 +7,9 @@ @Data public abstract class Mod implements Cloneable { - @SerializedName("rename") - @Expose - private String rename; + @SerializedName("rename") + @Expose + private String rename; private String fileName; private String downloadUrl; private String version; @@ -17,10 +17,9 @@ public abstract class Mod implements Cloneable { @Expose public String name; @SerializedName("skipUpdate") - @Expose - private boolean skipUpdate; + @Expose + private boolean skipUpdate; - public Mod() { } @@ -28,6 +27,6 @@ public Mod() { public Mod clone() throws CloneNotSupportedException { return (Mod) super.clone(); } - + public abstract void init(); } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ThirdParty.java b/src/main/java/com/nincraft/modpackdownloader/container/ThirdParty.java index 1c684e8..8808094 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ThirdParty.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ThirdParty.java @@ -6,12 +6,12 @@ import com.google.gson.annotations.SerializedName; @Generated("org.jsonschema2pojo") -public class ThirdParty extends Mod{ +public class ThirdParty extends Mod { @SerializedName("url") @Expose public String url; - + public String buildFileName() { if (getDownloadUrl().contains(".jar")) { return getDownloadUrl().substring(getDownloadUrl().lastIndexOf("/") + 1, diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 186dbd5..2863397 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -93,10 +93,10 @@ private static String getCurseForgeDownloadLocation(final String url, final Stri } private static void updateCurseMod(final CurseFile mod) { - if(mod.isSkipUpdate()){ - log.info("Skipped updating " + mod.getName()); - return; - } + if (mod.isSkipUpdate()) { + log.info("Skipped updating " + mod.getName()); + return; + } JSONObject fileListJson = null; try { val conn = (HttpURLConnection) new URL(mod.getProjectUrl()).openConnection(); @@ -117,8 +117,8 @@ private static void updateCurseMod(final CurseFile mod) { return; } - val newMod = getLatestVersion(Reference.mcVersion, mod.getReleaseType() != null ? - mod.getReleaseType() : Reference.releaseType, mod, fileListJson); + val newMod = getLatestVersion(Reference.mcVersion, + mod.getReleaseType() != null ? mod.getReleaseType() : Reference.releaseType, mod, fileListJson); if (mod.getFileID().compareTo(newMod.getFileID()) < 0) { log.info(String.format("Update found for %s. Most recent version is %s.", mod.getName(), newMod.getVersion())); @@ -157,7 +157,8 @@ private static CurseFile getLatestVersion(final String mcVersion, final String r } private static boolean equalOrLessThan(final String modRelease, final String releaseType) { - return "alpha".equals(releaseType) || releaseType.equals(modRelease) || "beta".equals(releaseType) && "release".equals(modRelease); + return "alpha".equals(releaseType) || releaseType.equals(modRelease) + || "beta".equals(releaseType) && "release".equals(modRelease); } private static JSONObject getCurseProjectJson(final Integer integer, final String projectName, diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index e145266..9f1bdf1 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -134,13 +134,13 @@ public static void updateManifest() { try { manifestFile.getCurseFiles().sort(compareMods); manifestFile.getThirdParty().sort(compareMods); - if (manifestFile.getCurseFiles().isEmpty()) { - manifestFile.setCurseFiles(null); - } - if (manifestFile.getThirdParty().isEmpty()) { - manifestFile.setThirdParty(null); - } - Gson prettyGson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation() + if (manifestFile.getCurseFiles().isEmpty()) { + manifestFile.setCurseFiles(null); + } + if (manifestFile.getThirdParty().isEmpty()) { + manifestFile.setThirdParty(null); + } + Gson prettyGson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation() .disableHtmlEscaping().create(); val file = new FileWriter(Reference.manifestFile); file.write(prettyGson.toJson(manifestFile)); From 1cd8395a39802e50b75584a5046fd172a88ab6d4 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 21 Mar 2016 22:57:16 -0500 Subject: [PATCH 53/86] Fix weird tags and null pointers --- src/main/java/com/nincraft/modpackdownloader/container/Mod.java | 2 +- .../com/nincraft/modpackdownloader/handler/CurseModHandler.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java index aed0880..a4baaa7 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java @@ -18,7 +18,7 @@ public abstract class Mod implements Cloneable { public String name; @SerializedName("skipUpdate") @Expose - private boolean skipUpdate; + private Boolean skipUpdate; public Mod() { } diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 2863397..e345325 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -93,7 +93,7 @@ private static String getCurseForgeDownloadLocation(final String url, final Stri } private static void updateCurseMod(final CurseFile mod) { - if (mod.isSkipUpdate()) { + if (mod.getSkipUpdate() != null && mod.getSkipUpdate()) { log.info("Skipped updating " + mod.getName()); return; } From 436aaa6e00ab716ae8d92aa0af50d3dafbef469e Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Fri, 11 Mar 2016 23:05:06 -0600 Subject: [PATCH 54/86] Remove MC version as option MC version is now a property in the manifest and not an option passed in on command line WIP --- .../modpackdownloader/ModPackDownloader.java | 9 +- .../modpackdownloader/container/Manifest.java | 17 ++ .../container/Minecraft.java | 24 +++ .../container/ModLoader.java | 23 ++ .../manager/ModListManager.java | 11 +- src/main/resources/botania-manifest.json | 200 ++++++++++++++++++ 6 files changed, 277 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/nincraft/modpackdownloader/container/Minecraft.java create mode 100644 src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java create mode 100644 src/main/resources/botania-manifest.json diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 8df6a39..cb593f0 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -45,9 +45,6 @@ private static void processArgument(final String arg) { } else if (arg.equals("-updateMods")) { Reference.updateMods = true; log.debug("mods will be updated instead of downloaded."); - } else if (arg.startsWith("-mcVersion")) { - Reference.mcVersion = arg.substring(arg.lastIndexOf("=") + 1); - log.debug(String.format("Minecraft Version set to: %s", Reference.mcVersion)); } else if (arg.startsWith("-releaseType")) { Reference.releaseType = arg.substring(arg.lastIndexOf("=") + 1); log.debug(String.format("Checking against mod release type: %s", Reference.releaseType)); @@ -85,8 +82,10 @@ private static void setupRepo() { private static void processMods() throws InterruptedException { log.trace("Processing Mods..."); - ModListManager.buildModList(); - + int returnCode = ModListManager.buildModList(); + if (returnCode == -1) { + return; + } if (Reference.updateMods) { log.info(String.format("Updating mods with parameters: %s, %s, %s", Reference.manifestFile, Reference.mcVersion, Reference.releaseType)); diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java index 7273995..e260f72 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java @@ -16,6 +16,9 @@ @Setter public class Manifest { + @SerializedName("minecraft") + @Expose + public Minecraft minecraft; @SerializedName("curseFiles") @Expose public List curseFiles = new ArrayList(); @@ -25,4 +28,18 @@ public class Manifest { @Expose public List thirdParty = new ArrayList(); + public String getMinecraftVersion() { + if (minecraft != null) { + return minecraft.getVersion(); + } + return null; + } + + public String getForgeVersion() { + if (!minecraft.getModLoaders().isEmpty()) { + return minecraft.getModLoaders().get(0).getId(); + } + return null; + } + } diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Minecraft.java b/src/main/java/com/nincraft/modpackdownloader/container/Minecraft.java new file mode 100644 index 0000000..3d8cc7a --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/container/Minecraft.java @@ -0,0 +1,24 @@ +package com.nincraft.modpackdownloader.container; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Generated; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +import lombok.Getter; + +@Generated("org.jsonschema2pojo") +@Getter +public class Minecraft { + + @SerializedName("version") + @Expose + public String version; + @SerializedName("modLoaders") + @Expose + public List modLoaders = new ArrayList(); + +} \ No newline at end of file diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java b/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java new file mode 100644 index 0000000..b92e5d2 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java @@ -0,0 +1,23 @@ +package com.nincraft.modpackdownloader.container; + +import javax.annotation.Generated; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +import lombok.Getter; + +@Generated("org.jsonschema2pojo") +@Getter +public class ModLoader { + + @SerializedName("id") + @Expose + public String id; + @SerializedName("primary") + @Expose + public Boolean primary; + @SerializedName("folder") + @Expose + public String folder; +} \ No newline at end of file diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 9f1bdf1..43bfa16 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -16,6 +16,7 @@ import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import com.google.common.base.Strings; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.gson.Gson; @@ -55,20 +56,25 @@ public int compare(Mod mod1, Mod mod2) { log.trace("Finished registering various mod type handlers."); } - public static void buildModList() { + public static int buildModList() { log.trace("Building Mod List..."); JSONObject jsonLists = null; try { jsonLists = (JSONObject) new JSONParser().parse(new FileReader(Reference.manifestFile)); } catch (IOException | ParseException e) { log.error(e.getMessage()); - return; + return -1; } manifestFile = gson.fromJson(jsonLists.toString(), Manifest.class); if (!manifestFile.getCurseManifestFiles().isEmpty()) { backupCurseManifest(); } + Reference.mcVersion = manifestFile.getMinecraftVersion(); + if(Strings.isNullOrEmpty(Reference.mcVersion)){ + log.error("No Minecraft version found in manifest file"); + return -1; + } manifestFile.getCurseFiles().addAll(manifestFile.getCurseManifestFiles()); MOD_LIST.addAll(manifestFile.getCurseFiles()); MOD_LIST.addAll(manifestFile.getThirdParty()); @@ -81,6 +87,7 @@ public static void buildModList() { MOD_LIST.forEach(Mod::init); log.trace("Finished Building Mod List."); + return 0; } private static void backupCurseManifest() { diff --git a/src/main/resources/botania-manifest.json b/src/main/resources/botania-manifest.json new file mode 100644 index 0000000..4972ab9 --- /dev/null +++ b/src/main/resources/botania-manifest.json @@ -0,0 +1,200 @@ +{ + "minecraft": { + "version": "1.7.10", + "modLoaders": [ + { + "id": "forge-10.13.4.1566", + "primary": true + } + ] + }, + "manifestType": "minecraftModpack", + "manifestVersion": 1, + "name": "Botania Skyblock The Modpack The Mod The Modpack", + "version": "1.0.28", + "author": "Vazkii", + "projectID": 233293, + "files": [ + { + "projectID": 227706, + "fileID": 2226922, + "required": true + }, + { + "projectID": 227979, + "fileID": 2229254, + "required": true + }, + { + "projectID": 231454, + "fileID": 2242852, + "required": true + }, + { + "projectID": 232502, + "fileID": 2246106, + "required": true + }, + { + "projectID": 232919, + "fileID": 2248061, + "required": true + }, + { + "projectID": 231879, + "fileID": 2243821, + "required": true + }, + { + "projectID": 223094, + "fileID": 2210792, + "required": true + }, + { + "projectID": 229068, + "fileID": 2232104, + "required": true + }, + { + "projectID": 235577, + "fileID": 2267308, + "required": true + }, + { + "projectID": 233716, + "fileID": 2254315, + "required": true + }, + { + "projectID": 238372, + "fileID": 2268525, + "required": true + }, + { + "projectID": 238618, + "fileID": 2269669, + "required": true + }, + { + "projectID": 233342, + "fileID": 2270358, + "required": true + }, + { + "projectID": 228932, + "fileID": 2255823, + "required": true + }, + { + "projectID": 231453, + "fileID": 2242214, + "required": true + }, + { + "projectID": 227083, + "fileID": 2224857, + "required": true + }, + { + "projectID": 227441, + "fileID": 2272557, + "required": true + }, + { + "projectID": 237852, + "fileID": 2272796, + "required": true + }, + { + "projectID": 227795, + "fileID": 2271214, + "required": true + }, + { + "projectID": 224472, + "fileID": 2277393, + "required": true + }, + { + "projectID": 238534, + "fileID": 2275426, + "required": true + }, + { + "projectID": 232131, + "fileID": 2277197, + "required": true + }, + { + "projectID": 230114, + "fileID": 2281148, + "required": true + }, + { + "projectID": 69118, + "fileID": 2280761, + "required": true + }, + { + "projectID": 225643, + "fileID": 2283837, + "required": true + }, + { + "projectID": 239049, + "fileID": 2271870, + "required": true + }, + { + "projectID": 238891, + "fileID": 2282307, + "required": true + }, + { + "projectID": 222789, + "fileID": 2224003, + "required": true + }, + { + "projectID": 230898, + "fileID": 2274165, + "required": true + }, + { + "projectID": 241895, + "fileID": 2284547, + "required": true + }, + { + "projectID": 228525, + "fileID": 2278984, + "required": true + }, + { + "projectID": 222213, + "fileID": 2262089, + "required": true + }, + { + "projectID": 222211, + "fileID": 2262091, + "required": true + }, + { + "projectID": 222303, + "fileID": 2284819, + "required": true + }, + { + "projectID": 233071, + "fileID": 2285865, + "required": true + }, + { + "projectID": 223852, + "fileID": 2284904, + "required": true + } + ], + "overrides": "overrides" +} \ No newline at end of file From 8b5888356df382e36f184412f258ff1e31e24e8b Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Tue, 22 Mar 2016 23:30:52 -0500 Subject: [PATCH 55/86] Add default mod folder if none specified --- .../modpackdownloader/ModPackDownloader.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index cb593f0..4b51210 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -14,8 +14,8 @@ public static void main(final String[] args) throws InterruptedException { if ("-updateApp".equals(args[0])) { ApplicationUpdateHandeler.update(); return; - } else if (args.length < 2) { - log.error("Arguments required: manifest file location, mod download location"); + } else if (args.length < 1) { + log.error("Arguments required: manifest file location"); return; } else { processArguments(args); @@ -28,7 +28,13 @@ public static void main(final String[] args) throws InterruptedException { private static void processArguments(final String[] args) { Reference.manifestFile = args[0]; - Reference.modFolder = args[1]; + + if (args.length < 2) { + log.info("No mod folder specified, defaulting to \"mods\""); + Reference.modFolder = "mods"; + } else { + Reference.modFolder = args[1]; + } if (args.length > 2) { for (val arg : args) { From a2dbf247a4de0f3024fbb3e600371e1075f5a2bc Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 23 Mar 2016 00:11:32 -0500 Subject: [PATCH 56/86] Add Forge handler --- .../handler/ForgeHandler.java | 39 +++++++++++++++++++ .../manager/ModListManager.java | 12 +++--- .../modpackdownloader/util/Reference.java | 5 ++- 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java new file mode 100644 index 0000000..38ed661 --- /dev/null +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -0,0 +1,39 @@ +package com.nincraft.modpackdownloader.handler; + +import com.nincraft.modpackdownloader.util.FileSystemHelper; +import com.nincraft.modpackdownloader.util.Reference; +import lombok.extern.log4j.Log4j2; +import lombok.val; +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +@Log4j2 +public class ForgeHandler { + public static void downloadForgeInstaller(String minecraftVersion, String forgeVersion) { + log.info(String.format("Downloading Forge version %s", forgeVersion)); + + String forgeId = forgeVersion.substring(forgeVersion.indexOf("-") + 1); + String forgeURL = Reference.forgeURL + minecraftVersion + "-" + forgeId + "-" + minecraftVersion + + "/forge-" + minecraftVersion + "-" + forgeId + "-" + minecraftVersion + "-installer.jar"; + String forgeFileName = "forge-" + minecraftVersion + "-" + forgeId + "-" + minecraftVersion + "-installer.jar"; + File forge = FileSystemHelper.getDownloadedFile(forgeFileName); + + if (!FileSystemHelper.isInLocalRepo("forge", forgeFileName) || Reference.forceDownload) { + val downloadedFile = FileSystemHelper.getDownloadedFile(forgeFileName); + try { + FileUtils.copyURLToFile(new URL(forgeURL), downloadedFile); + } catch (final IOException e) { + log.error(String.format("Could not download %s.", forgeFileName), e.getMessage()); + return; + } + FileSystemHelper.copyToLocalRepo("forge", downloadedFile); + } else { + FileSystemHelper.copyFromLocalRepo("forge", forgeFileName, Reference.modFolder); + } + + log.info(String.format("Completed downloading Forge version %s", forgeFileName)); + } +} diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 43bfa16..7885ade 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -10,6 +10,8 @@ import java.util.Map; import java.util.Optional; +import com.nincraft.modpackdownloader.container.*; +import com.nincraft.modpackdownloader.handler.ForgeHandler; import org.apache.commons.io.FileUtils; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -21,10 +23,6 @@ import com.google.common.collect.Maps; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.nincraft.modpackdownloader.container.CurseFile; -import com.nincraft.modpackdownloader.container.Manifest; -import com.nincraft.modpackdownloader.container.Mod; -import com.nincraft.modpackdownloader.container.ThirdParty; import com.nincraft.modpackdownloader.handler.CurseModHandler; import com.nincraft.modpackdownloader.handler.ModHandler; import com.nincraft.modpackdownloader.handler.ThirdPartyModHandler; @@ -71,7 +69,7 @@ public static int buildModList() { backupCurseManifest(); } Reference.mcVersion = manifestFile.getMinecraftVersion(); - if(Strings.isNullOrEmpty(Reference.mcVersion)){ + if (Strings.isNullOrEmpty(Reference.mcVersion)) { log.error("No Minecraft version found in manifest file"); return -1; } @@ -108,6 +106,10 @@ public static Optional getThirdPartyModList(final JSONObject jsonList } public static final void downloadMods() { + new Thread(() -> { + ForgeHandler.downloadForgeInstaller(manifestFile.getMinecraftVersion(), manifestFile.getForgeVersion()); + }).start(); + log.trace(String.format("Downloading %s mods...", MOD_LIST.size())); int downloadCount = 1; for (val mod : MOD_LIST) { diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index 1dba279..e52136f 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -10,10 +10,10 @@ public class Reference { public static final String MAC_FOLDER = "/Library/Application Support/modpackdownloader/"; public static final String OTHER_FOLDER = "/.modpackdownloader/"; public static final String JAR_FILE_EXT = ".jar"; - public static final String[] DATE_FORMATS = { "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss'Z'", + public static final String[] DATE_FORMATS = {"yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd HH:mm:ss", "MM/dd/yyyy HH:mm:ss", "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'", "MM/dd/yyyy'T'HH:mm:ss.SSSZ", - "MM/dd/yyyy'T'HH:mm:ss.SSS", "MM/dd/yyyy'T'HH:mm:ssZ", "MM/dd/yyyy'T'HH:mm:ss", "yyyy:MM:dd HH:mm:ss", }; + "MM/dd/yyyy'T'HH:mm:ss.SSS", "MM/dd/yyyy'T'HH:mm:ssZ", "MM/dd/yyyy'T'HH:mm:ss", "yyyy:MM:dd HH:mm:ss",}; public static final int RETRY_COUNTER = 5; public static final char URL_DELIMITER = '/'; public static String userhome; @@ -30,4 +30,5 @@ public class Reference { public static int updateCount = 0; public static int updateTotal = 0; public static String updateAppURL = "http://play.nincraft.com:8080/job/Mod%20Pack%20Downloader/lastSuccessfulBuild/artifact/target/classes/latest.json"; + public static String forgeURL = "http://files.minecraftforge.net/maven/net/minecraftforge/forge/"; } From 7cd978151bf582f405953b54f918df6d912ee244 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 23 Mar 2016 01:11:57 -0500 Subject: [PATCH 57/86] Skip Forge download if none found WIP --- .../nincraft/modpackdownloader/ModPackDownloader.java | 6 ++++++ .../nincraft/modpackdownloader/container/Manifest.java | 2 +- .../nincraft/modpackdownloader/handler/ForgeHandler.java | 6 ++++++ .../modpackdownloader/manager/ModListManager.java | 5 +---- src/main/resources/manifest.json | 9 +++++++++ 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 4b51210..9c601af 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -1,5 +1,6 @@ package com.nincraft.modpackdownloader; +import com.google.common.base.Strings; import com.nincraft.modpackdownloader.handler.ApplicationUpdateHandeler; import com.nincraft.modpackdownloader.manager.ModListManager; import com.nincraft.modpackdownloader.util.FileSystemHelper; @@ -93,6 +94,11 @@ private static void processMods() throws InterruptedException { return; } if (Reference.updateMods) { + if (Strings.isNullOrEmpty(Reference.mcVersion)) { + log.error("No Minecraft version found in manifest file"); + return; + } + log.info(String.format("Updating mods with parameters: %s, %s, %s", Reference.manifestFile, Reference.mcVersion, Reference.releaseType)); ModListManager.updateMods(); diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java index e260f72..1d83c5d 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java @@ -36,7 +36,7 @@ public String getMinecraftVersion() { } public String getForgeVersion() { - if (!minecraft.getModLoaders().isEmpty()) { + if (minecraft != null && !minecraft.getModLoaders().isEmpty()) { return minecraft.getModLoaders().get(0).getId(); } return null; diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index 38ed661..6c9e8a6 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -1,5 +1,6 @@ package com.nincraft.modpackdownloader.handler; +import com.google.common.base.Strings; import com.nincraft.modpackdownloader.util.FileSystemHelper; import com.nincraft.modpackdownloader.util.Reference; import lombok.extern.log4j.Log4j2; @@ -13,6 +14,11 @@ @Log4j2 public class ForgeHandler { public static void downloadForgeInstaller(String minecraftVersion, String forgeVersion) { + if(Strings.isNullOrEmpty(forgeVersion) || Strings.isNullOrEmpty(minecraftVersion)){ + log.debug("No Forge or Minecraft version found in manifest, skipping"); + return; + } + log.info(String.format("Downloading Forge version %s", forgeVersion)); String forgeId = forgeVersion.substring(forgeVersion.indexOf("-") + 1); diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 7885ade..3a103f6 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -69,10 +69,7 @@ public static int buildModList() { backupCurseManifest(); } Reference.mcVersion = manifestFile.getMinecraftVersion(); - if (Strings.isNullOrEmpty(Reference.mcVersion)) { - log.error("No Minecraft version found in manifest file"); - return -1; - } + manifestFile.getCurseFiles().addAll(manifestFile.getCurseManifestFiles()); MOD_LIST.addAll(manifestFile.getCurseFiles()); MOD_LIST.addAll(manifestFile.getThirdParty()); diff --git a/src/main/resources/manifest.json b/src/main/resources/manifest.json index 652e275..a158c8d 100644 --- a/src/main/resources/manifest.json +++ b/src/main/resources/manifest.json @@ -1,4 +1,13 @@ { + "minecraft": { + "version": "1.8.9", + "modLoaders": [ + { + "id": "forge-11.15.1.1764", + "primary": true + } + ] + }, "curseFiles": [ { "fileID": 2269703, From f9cd8ff8c66587ad30176d578a3ce174856e5dbf Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 23 Mar 2016 01:25:47 -0500 Subject: [PATCH 58/86] Only use stable builds for updating --- .../java/com/nincraft/modpackdownloader/util/Reference.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index e52136f..9cdda61 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -29,6 +29,6 @@ public class Reference { public static int downloadTotal = 0; public static int updateCount = 0; public static int updateTotal = 0; - public static String updateAppURL = "http://play.nincraft.com:8080/job/Mod%20Pack%20Downloader/lastSuccessfulBuild/artifact/target/classes/latest.json"; + public static String updateAppURL = "http://play.nincraft.com:8080/job/Mod%20Pack%20Downloader/lastStableBuild/artifact/target/classes/latest.json"; public static String forgeURL = "http://files.minecraftforge.net/maven/net/minecraftforge/forge/"; } From 50974610ac8e75a6c6b543bf7c582dc8e5f9f132 Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Wed, 23 Mar 2016 11:04:05 -0500 Subject: [PATCH 59/86] Update Readme Might as well add the other modpacks that implement the downloader. --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d957fd3..b76b038 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,7 @@ Example: java -jar modpackdownloader.jar mods.json mods This will read the mods.json and download all mods to the mods folder. -For additional examples check out the [NEB3 modpack repo](https://github.com/Nincraft/NincraftElectricBoogaloo3TheLightAmongTheLongForgottenDarkness/tree/develop). +For additional examples check out some of our modpacks that implement this: +- [NEB3 Modpack Repository](https://github.com/Nincraft/NincraftElectricBoogaloo3TheLightAmongTheLongForgottenDarkness/tree/develop) +- [TWBB Modpack Repository](https://github.com/UndeadZeratul/ThereWillBeBlood/tree/develop) +- [TWBB2 Modpack Repository](https://github.com/UndeadZeratul/ThereWillBeBlood2/tree/develop) From cb7737f837d897bc910f55b9c606b5eeb27b8c87 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 23 Mar 2016 15:50:20 -0500 Subject: [PATCH 60/86] Fix issue with Curse manifests Curse manifests have no mod names included so the sorting would null pointer --- .../nincraft/modpackdownloader/manager/ModListManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 3a103f6..79a21fe 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -77,9 +77,9 @@ public static int buildModList() { log.debug(String.format("A total of %s mods will be %s.", Reference.downloadTotal, Reference.updateMods ? "updated" : "downloaded")); - Collections.sort(MOD_LIST, compareMods); - MOD_LIST.forEach(Mod::init); + + Collections.sort(MOD_LIST, compareMods); log.trace("Finished Building Mod List."); return 0; From d109e7d24acb283679087770967a0e2e75884223 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sat, 26 Mar 2016 12:59:50 -0500 Subject: [PATCH 61/86] Add folder override for mods Because I thought we had this already --- .../modpackdownloader/container/Mod.java | 3 +++ .../modpackdownloader/handler/ModHandler.java | 4 +-- .../util/FileSystemHelper.java | 25 +++++++++++++------ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java index a4baaa7..2786155 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java @@ -19,6 +19,9 @@ public abstract class Mod implements Cloneable { @SerializedName("skipUpdate") @Expose private Boolean skipUpdate; + @SerializedName("folder") + @Expose + private String folder; public Mod() { } diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java index cbde079..c0eb143 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ModHandler.java @@ -25,7 +25,7 @@ protected static void downloadFile(final Mod mod, final boolean useUserAgent) { val decodedFileName = URLHelper.decodeSpaces(mod.getFileName()); if (!FileSystemHelper.isInLocalRepo(mod.getName(), decodedFileName) || Reference.forceDownload) { - val downloadedFile = FileSystemHelper.getDownloadedFile(decodedFileName); + val downloadedFile = FileSystemHelper.getDownloadedFile(decodedFileName, mod.getFolder()); try { FileUtils.copyURLToFile(new URL(mod.getDownloadUrl()), downloadedFile); } catch (final IOException e) { @@ -40,7 +40,7 @@ protected static void downloadFile(final Mod mod, final boolean useUserAgent) { FileSystemHelper.copyToLocalRepo(mod.getName(), downloadedFile); } else { - FileSystemHelper.copyFromLocalRepo(mod.getName(), decodedFileName, Reference.modFolder); + FileSystemHelper.copyFromLocalRepo(mod.getName(), decodedFileName, mod.getFolder()); } } diff --git a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java index 240d5df..120f9bc 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java @@ -32,9 +32,11 @@ public static void copyToLocalRepo(final String projectName, final File download } } - public static void copyFromLocalRepo(final String projectName, final String fileName, final String folder) { + public static void copyFromLocalRepo(final String projectName, final String fileName, String folder) { val newProjectName = getProjectNameOrDefault(projectName); - + if (folder == null) { + folder = Reference.modFolder; + } try { FileUtils.copyFileToDirectory(getLocalFile(fileName, newProjectName), new File(folder)); } catch (final IOException e) { @@ -47,12 +49,7 @@ public static boolean isInLocalRepo(final String projectName, final String fileN } public static File getDownloadedFile(final String fileName) { - if (Reference.modFolder != null) { - createFolder(Reference.modFolder); - return new File(Reference.modFolder + File.separator + fileName); - } else { - return new File(fileName); - } + return getDownloadedFile(fileName, null); } public static String getProjectNameOrDefault(final String projectName) { @@ -62,4 +59,16 @@ public static String getProjectNameOrDefault(final String projectName) { public static File getLocalFile(final String fileName, final String newProjectName) { return new File(Reference.userhome + newProjectName + File.separator + fileName); } + + public static File getDownloadedFile(String fileName, String folder) { + if (folder != null) { + createFolder(folder); + return new File(folder + File.separator + fileName); + } else if (Reference.modFolder != null) { + createFolder(Reference.modFolder); + return new File(Reference.modFolder + File.separator + fileName); + } else { + return new File(fileName); + } + } } From a75e65406ed336b954a4aa5f6071af934322c12f Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sat, 26 Mar 2016 19:16:37 -0500 Subject: [PATCH 62/86] Fix some Forge URL formatting between MC versions --- .../modpackdownloader/handler/ForgeHandler.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index 6c9e8a6..3c23174 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -14,7 +14,7 @@ @Log4j2 public class ForgeHandler { public static void downloadForgeInstaller(String minecraftVersion, String forgeVersion) { - if(Strings.isNullOrEmpty(forgeVersion) || Strings.isNullOrEmpty(minecraftVersion)){ + if (Strings.isNullOrEmpty(forgeVersion) || Strings.isNullOrEmpty(minecraftVersion)) { log.debug("No Forge or Minecraft version found in manifest, skipping"); return; } @@ -22,10 +22,14 @@ public static void downloadForgeInstaller(String minecraftVersion, String forgeV log.info(String.format("Downloading Forge version %s", forgeVersion)); String forgeId = forgeVersion.substring(forgeVersion.indexOf("-") + 1); - String forgeURL = Reference.forgeURL + minecraftVersion + "-" + forgeId + "-" + minecraftVersion + - "/forge-" + minecraftVersion + "-" + forgeId + "-" + minecraftVersion + "-installer.jar"; - String forgeFileName = "forge-" + minecraftVersion + "-" + forgeId + "-" + minecraftVersion + "-installer.jar"; - File forge = FileSystemHelper.getDownloadedFile(forgeFileName); + String forgeFileName = "forge-" + minecraftVersion + "-" + forgeId; + String forgeURL = Reference.forgeURL + minecraftVersion + "-" + forgeId; + if (!minecraftVersion.startsWith("1.8")) { + forgeFileName += "-" + minecraftVersion; + forgeURL += "-" + minecraftVersion; + } + forgeFileName += "-installer.jar"; + forgeURL += "/" + forgeFileName; if (!FileSystemHelper.isInLocalRepo("forge", forgeFileName) || Reference.forceDownload) { val downloadedFile = FileSystemHelper.getDownloadedFile(forgeFileName); From e5f162abe5bec14f83fb56023634f69a5a200525 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sat, 26 Mar 2016 21:49:36 -0500 Subject: [PATCH 63/86] Update logging layout --- src/main/resources/log4j2.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index cebc874..1d84649 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -6,7 +6,7 @@ - + From 5bed06933ff89b2e8a234ef7a9f1f54263165ffd Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sat, 26 Mar 2016 22:23:35 -0500 Subject: [PATCH 64/86] Add rename override for Forge --- .../modpackdownloader/container/ModLoader.java | 3 +++ .../handler/ForgeHandler.java | 18 +++++++++++++++--- .../manager/ModListManager.java | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java b/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java index b92e5d2..3829c78 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java @@ -20,4 +20,7 @@ public class ModLoader { @SerializedName("folder") @Expose public String folder; + @SerializedName("rename") + @Expose + public String rename; } \ No newline at end of file diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index 3c23174..8f6f846 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -1,6 +1,7 @@ package com.nincraft.modpackdownloader.handler; import com.google.common.base.Strings; +import com.nincraft.modpackdownloader.container.ModLoader; import com.nincraft.modpackdownloader.util.FileSystemHelper; import com.nincraft.modpackdownloader.util.Reference; import lombok.extern.log4j.Log4j2; @@ -10,15 +11,20 @@ import java.io.File; import java.io.IOException; import java.net.URL; +import java.util.List; @Log4j2 public class ForgeHandler { - public static void downloadForgeInstaller(String minecraftVersion, String forgeVersion) { - if (Strings.isNullOrEmpty(forgeVersion) || Strings.isNullOrEmpty(minecraftVersion)) { + public static void downloadForgeInstaller(String minecraftVersion, List modLoaders) { + if (modLoaders == null || Strings.isNullOrEmpty(minecraftVersion)) { log.debug("No Forge or Minecraft version found in manifest, skipping"); return; } + ModLoader modLoader = modLoaders.get(0); + String forgeVersion = modLoader.getId(); + String folder = modLoader.getFolder(); + log.info(String.format("Downloading Forge version %s", forgeVersion)); String forgeId = forgeVersion.substring(forgeVersion.indexOf("-") + 1); @@ -32,7 +38,13 @@ public static void downloadForgeInstaller(String minecraftVersion, String forgeV forgeURL += "/" + forgeFileName; if (!FileSystemHelper.isInLocalRepo("forge", forgeFileName) || Reference.forceDownload) { - val downloadedFile = FileSystemHelper.getDownloadedFile(forgeFileName); + File downloadedFile; + if (modLoader.getRename() != null) { + downloadedFile = FileSystemHelper.getDownloadedFile(modLoader.getRename(), folder); + } else { + downloadedFile = FileSystemHelper.getDownloadedFile(forgeFileName, folder); + } + try { FileUtils.copyURLToFile(new URL(forgeURL), downloadedFile); } catch (final IOException e) { diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 79a21fe..3ef1b8b 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -104,7 +104,7 @@ public static Optional getThirdPartyModList(final JSONObject jsonList public static final void downloadMods() { new Thread(() -> { - ForgeHandler.downloadForgeInstaller(manifestFile.getMinecraftVersion(), manifestFile.getForgeVersion()); + ForgeHandler.downloadForgeInstaller(manifestFile.getMinecraftVersion(), manifestFile.getMinecraft().getModLoaders()); }).start(); log.trace(String.format("Downloading %s mods...", MOD_LIST.size())); From a1d3bfdadd42ca6b22a59005ee35a22dccdcfd70 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sat, 26 Mar 2016 22:36:46 -0500 Subject: [PATCH 65/86] Add option to download Forge installer and universal --- .../modpackdownloader/container/ModLoader.java | 14 ++++++++++---- .../modpackdownloader/handler/ForgeHandler.java | 16 ++++++++++++---- .../manager/ModListManager.java | 3 +-- .../modpackdownloader/util/Reference.java | 2 ++ 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java b/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java index 3829c78..080bee8 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java @@ -13,14 +13,20 @@ public class ModLoader { @SerializedName("id") @Expose - public String id; + private String id; @SerializedName("primary") @Expose - public Boolean primary; + private Boolean primary; @SerializedName("folder") @Expose - public String folder; + private String folder; @SerializedName("rename") @Expose - public String rename; + private String rename; + @SerializedName("downloadInstaller") + @Expose + private Boolean downloadInstaller; + @SerializedName("downloadUniversal") + @Expose + private Boolean downloadUniversal; } \ No newline at end of file diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index 8f6f846..0c37059 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -15,7 +15,7 @@ @Log4j2 public class ForgeHandler { - public static void downloadForgeInstaller(String minecraftVersion, List modLoaders) { + public static void downloadForge(String minecraftVersion, List modLoaders) { if (modLoaders == null || Strings.isNullOrEmpty(minecraftVersion)) { log.debug("No Forge or Minecraft version found in manifest, skipping"); return; @@ -24,17 +24,26 @@ public static void downloadForgeInstaller(String minecraftVersion, List getThirdPartyModList(final JSONObject jsonList public static final void downloadMods() { new Thread(() -> { - ForgeHandler.downloadForgeInstaller(manifestFile.getMinecraftVersion(), manifestFile.getMinecraft().getModLoaders()); + ForgeHandler.downloadForge(manifestFile.getMinecraftVersion(), manifestFile.getMinecraft().getModLoaders()); }).start(); log.trace(String.format("Downloading %s mods...", MOD_LIST.size())); diff --git a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java index 9cdda61..8eb62b9 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/Reference.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/Reference.java @@ -31,4 +31,6 @@ public class Reference { public static int updateTotal = 0; public static String updateAppURL = "http://play.nincraft.com:8080/job/Mod%20Pack%20Downloader/lastStableBuild/artifact/target/classes/latest.json"; public static String forgeURL = "http://files.minecraftforge.net/maven/net/minecraftforge/forge/"; + public static String forgeInstaller = "-installer.jar"; + public static String forgeUniversal = "-universal.jar"; } From a8beff4d5c27a35db98a0a521a9c9566f5f6657f Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sat, 26 Mar 2016 22:49:28 -0500 Subject: [PATCH 66/86] Remove unused import --- .../com/nincraft/modpackdownloader/handler/ForgeHandler.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index 0c37059..b8e5e55 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -5,7 +5,6 @@ import com.nincraft.modpackdownloader.util.FileSystemHelper; import com.nincraft.modpackdownloader.util.Reference; import lombok.extern.log4j.Log4j2; -import lombok.val; import org.apache.commons.io.FileUtils; import java.io.File; From 6c68695d97389199a7612f7adfb6872659ff0e32 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 28 Mar 2016 13:35:16 -0500 Subject: [PATCH 67/86] Fix null pointer and folder override when copying from local repo --- .../nincraft/modpackdownloader/handler/ForgeHandler.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index b8e5e55..8f30384 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -27,10 +27,10 @@ public static void downloadForge(String minecraftVersion, List modLoa log.info(String.format("Downloading Forge version %s", forgeVersion)); - if (modLoader.getDownloadInstaller()) { + if (modLoader.getDownloadInstaller() != null && modLoader.getDownloadInstaller()) { downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, Reference.forgeInstaller); } - if (modLoader.getDownloadUniversal()) { + if (modLoader.getDownloadUniversal() != null && modLoader.getDownloadUniversal()) { downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, Reference.forgeUniversal); } } @@ -61,7 +61,7 @@ private static void downloadForgeFile(String minecraftVersion, ModLoader modLoad } FileSystemHelper.copyToLocalRepo("forge", downloadedFile); } else { - FileSystemHelper.copyFromLocalRepo("forge", forgeFileName, Reference.modFolder); + FileSystemHelper.copyFromLocalRepo("forge", forgeFileName, folder); } log.info(String.format("Completed downloading Forge version %s", forgeFileName)); } From 7984ec5a9ff7154b1dc9e88b778e630e0a69b1c8 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Tue, 29 Mar 2016 13:45:47 -0500 Subject: [PATCH 68/86] Fix empty mod loader list causing errors Closes #17 --- .../com/nincraft/modpackdownloader/handler/ForgeHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index 8f30384..a13927a 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -15,7 +15,7 @@ @Log4j2 public class ForgeHandler { public static void downloadForge(String minecraftVersion, List modLoaders) { - if (modLoaders == null || Strings.isNullOrEmpty(minecraftVersion)) { + if (modLoaders == null || modLoaders.isEmpty() || Strings.isNullOrEmpty(minecraftVersion)) { log.debug("No Forge or Minecraft version found in manifest, skipping"); return; } From 8c59f794de3ccebaa6e1d51749e3ae45470938fa Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Tue, 29 Mar 2016 13:58:02 -0500 Subject: [PATCH 69/86] Add Apache commons Because its nice --- pom.xml | 7 ++++++- .../nincraft/modpackdownloader/handler/ForgeHandler.java | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 4c51313..80eb954 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,12 @@ log4j-api 2.5 - + + org.apache.commons + commons-collections4 + 4.1 + + org.apache.logging.log4j log4j-core 2.5 diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index a13927a..34cfd80 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -5,6 +5,7 @@ import com.nincraft.modpackdownloader.util.FileSystemHelper; import com.nincraft.modpackdownloader.util.Reference; import lombok.extern.log4j.Log4j2; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.FileUtils; import java.io.File; @@ -15,7 +16,7 @@ @Log4j2 public class ForgeHandler { public static void downloadForge(String minecraftVersion, List modLoaders) { - if (modLoaders == null || modLoaders.isEmpty() || Strings.isNullOrEmpty(minecraftVersion)) { + if (CollectionUtils.isNotEmpty(modLoaders) || Strings.isNullOrEmpty(minecraftVersion)) { log.debug("No Forge or Minecraft version found in manifest, skipping"); return; } From bac0b56a5770d313a2e01b9591de4ac5d0d8ac9b Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Tue, 29 Mar 2016 14:00:12 -0500 Subject: [PATCH 70/86] Formatting fixes --- pom.xml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 80eb954..9445330 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.nincraft ModPackDownloader @@ -20,12 +20,12 @@ log4j-api 2.5 - - org.apache.commons - commons-collections4 - 4.1 - - + + org.apache.commons + commons-collections4 + 4.1 + + org.apache.logging.log4j log4j-core 2.5 From 2576f97597bcd8c264982b798464ad747cc44300 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 30 Mar 2016 17:48:37 -0500 Subject: [PATCH 71/86] Fix already set names being overwritten --- .../com/nincraft/modpackdownloader/container/CurseFile.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java index 28adad3..4facca4 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java @@ -44,7 +44,9 @@ public void init() { conn.setInstanceFollowRedirects(false); conn.connect(); - setName(conn.getHeaderField("Location").split("/")[2]); + if (getName() == null) { + setName(conn.getHeaderField("Location").split("/")[2]); + } } catch (final IOException e) { log.error(e.getMessage()); } From f50f05e28999846fd2125289396a7b786492214e Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 30 Mar 2016 17:52:41 -0500 Subject: [PATCH 72/86] Fix modloader not being set to null if empty --- .../modpackdownloader/container/Minecraft.java | 12 ++++++------ .../modpackdownloader/manager/ModListManager.java | 5 ++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Minecraft.java b/src/main/java/com/nincraft/modpackdownloader/container/Minecraft.java index 3d8cc7a..4ccf366 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Minecraft.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Minecraft.java @@ -1,17 +1,17 @@ package com.nincraft.modpackdownloader.container; -import java.util.ArrayList; -import java.util.List; - -import javax.annotation.Generated; - import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; - import lombok.Getter; +import lombok.Setter; + +import javax.annotation.Generated; +import java.util.ArrayList; +import java.util.List; @Generated("org.jsonschema2pojo") @Getter +@Setter public class Minecraft { @SerializedName("version") diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 81ed13f..39b4efc 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -77,7 +77,7 @@ public static int buildModList() { Reference.updateMods ? "updated" : "downloaded")); MOD_LIST.forEach(Mod::init); - + Collections.sort(MOD_LIST, compareMods); log.trace("Finished Building Mod List."); @@ -145,6 +145,9 @@ public static void updateManifest() { if (manifestFile.getThirdParty().isEmpty()) { manifestFile.setThirdParty(null); } + if (manifestFile.getMinecraft().getModLoaders().isEmpty()) { + manifestFile.getMinecraft().setModLoaders(null); + } Gson prettyGson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation() .disableHtmlEscaping().create(); val file = new FileWriter(Reference.manifestFile); From 779a4170872c791f80f17af25cb94157948d12d7 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 30 Mar 2016 17:59:29 -0500 Subject: [PATCH 73/86] Actually fix names --- .../com/nincraft/modpackdownloader/container/CurseFile.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java index 4facca4..2aa89ec 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java @@ -6,6 +6,7 @@ import javax.annotation.Generated; +import com.google.common.base.Strings; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import com.nincraft.modpackdownloader.util.Reference; @@ -44,7 +45,7 @@ public void init() { conn.setInstanceFollowRedirects(false); conn.connect(); - if (getName() == null) { + if (Strings.isNullOrEmpty(getName())) { setName(conn.getHeaderField("Location").split("/")[2]); } } catch (final IOException e) { From 87039dc1bfe666ef1537b94adf52f8e69318371d Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Wed, 30 Mar 2016 18:31:51 -0500 Subject: [PATCH 74/86] Actually and truly prevent mod name from being forcefully overwritten. Also updated the test manifests so that we can make sure they don't revert. --- .../handler/CurseModHandler.java | 2 - src/main/resources/all-mods.json | 58 +++++++++---------- src/main/resources/manifest.json | 12 ++-- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index e345325..7a57858 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -103,8 +103,6 @@ private static void updateCurseMod(final CurseFile mod) { conn.setInstanceFollowRedirects(false); conn.connect(); - val location = conn.getHeaderField("Location"); - mod.setName(location.split("/")[2]); fileListJson = (JSONObject) getCurseProjectJson(mod.getProjectID(), mod.getName(), new JSONParser()) .get("files"); diff --git a/src/main/resources/all-mods.json b/src/main/resources/all-mods.json index 97d0101..1628eb7 100644 --- a/src/main/resources/all-mods.json +++ b/src/main/resources/all-mods.json @@ -2,147 +2,147 @@ "curseFiles": [ { "fileID": 2277394, - "name": "applecore", + "name": "AppleCore", "projectID": 224472 }, { "fileID": 2284622, - "name": "aroma1997core", + "name": "Aroma1997Core", "projectID": 223735 }, { "fileID": 2277001, - "name": "aromabackup", + "name": "AromaBackup", "projectID": 225658 }, { "fileID": 2273274, - "name": "baubles", + "name": "Baubles", "projectID": 227083 }, { "fileID": 2274840, - "name": "baubleshud", + "name": "Baubles HUD", "projectID": 230751 }, { "fileID": 2279388, - "name": "biomes-o-plenty", + "name": "Biomes O' Plenty", "projectID": 220318 }, { "fileID": 2279356, - "name": "blood-magic", + "name": "Blood Magic", "projectID": 224791 }, { "fileID": 2269766, - "name": "bloodmoon", + "name": "Bloodmoon", "projectID": 226321 }, { "fileID": 2280253, - "name": "chisels-bits", + "name": "Chisels and Bits", "projectID": 231095 }, { "fileID": 2273148, - "name": "durability-show", + "name": "Durability Show", "projectID": 226099 }, { "fileID": 2269767, - "name": "fast-leaf-decay", + "name": "Fast Leaf Decay", "projectID": 225839 }, { "fileID": 2272962, - "name": "helpfixer", + "name": "Help Fixer", "projectID": 223797 }, { "fileID": 2281942, - "name": "immersive-craft", + "name": "Immersive Craft", "projectID": 241268 }, { "fileID": 2283028, - "name": "in-case-of-emergency", + "name": "In Case of Emergency", "projectID": 241925 }, { "fileID": 2280666, - "name": "intangible", + "name": "Intangible", "projectID": 241350 }, { "fileID": 2275675, - "name": "inventory-tweaks", + "name": "Inventory Tweaks", "projectID": 223094 }, { "fileID": 2278859, - "name": "iron-chests", + "name": "Iron Chests", "projectID": 228756 }, { "fileID": 2282784, - "name": "just-enough-items-jei", + "name": "Just Enough Items", "projectID": 238222 }, { "fileID": 2276884, - "name": "mantle", + "name": "Mantle", "projectID": 74924 }, { "fileID": 2276306, - "name": "mcmultipart", + "name": "MCMultipart", "projectID": 239431 }, { "fileID": 2280806, - "name": "not-enough-wands", + "name": "Not Enough Wands", "projectID": 235595 }, { "fileID": 2280569, - "name": "opencomputers", + "name": "Open Computers", "projectID": 223008 }, { "fileID": 2284425, - "name": "psi", + "name": "Psi", "projectID": 241665 }, { "fileID": 2283433, - "name": "random-things", + "name": "Random Things", "projectID": 59816 }, { "fileID": 2275910, - "name": "simpleretrogen", + "name": "SimpleRetrogen", "projectID": 240566 }, { "fileID": 2279246, - "name": "substratum", + "name": "Substratum", "projectID": 241113 }, { "fileID": 2279928, - "name": "thaumcraft", + "name": "Thaumcraft", "projectID": 223628 }, { "fileID": 2276883, - "name": "tinkers-construct", + "name": "Tinkers' Construct", "projectID": 74072 }, { "fileID": 2279788, - "name": "tis-3d", + "name": "TIS 3D", "projectID": 238603 } ], diff --git a/src/main/resources/manifest.json b/src/main/resources/manifest.json index a158c8d..b73944c 100644 --- a/src/main/resources/manifest.json +++ b/src/main/resources/manifest.json @@ -10,16 +10,12 @@ }, "curseFiles": [ { - "fileID": 2269703, + "fileID": 2287496, "projectID": 223628, - "name": "thaumcraft" + "name": "Thaumcraft" } ], "thirdParty": [ - { - "url": "http://en.yampst.net/gettracker.php/Yampst-There%20Will%20Be%20Blood-0.5.0.jar?trackid=fa65e1dc97876791e521a3d6e6b0ddea27e86ee4010b5d47e9d48767&file=e775b851a66544fcc52a1f3404ee7eb023c7964a47edfc639afa9bcb", - "name": "Yampst-TWBB" - }, { "url": "https://www.dropbox.com/s/q1qppz3sq447173/buildcraft-7.2.0-pre6.jar?dl=1", "name": "Buildcraft" @@ -27,6 +23,10 @@ { "url": "http://charset.asie.pl/files/Charset-0.2.1-945baf5589832f727c42cf77.jar", "name": "Charset" + }, + { + "url": "http://en.yampst.net/gettracker.php/Yampst-There%20Will%20Be%20Blood-0.5.0.jar?trackid=fa65e1dc97876791e521a3d6e6b0ddea27e86ee4010b5d47e9d48767&file=e775b851a66544fcc52a1f3404ee7eb023c7964a47edfc639afa9bcb", + "name": "Yampst-TWBB" } ] } \ No newline at end of file From e78489fdcf18016725437d5abe49d821185f6cf1 Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Wed, 30 Mar 2016 18:44:18 -0500 Subject: [PATCH 75/86] Invert Empty Check Otherwise it does nothing. --- .../com/nincraft/modpackdownloader/handler/ForgeHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index 34cfd80..42aaced 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -16,7 +16,7 @@ @Log4j2 public class ForgeHandler { public static void downloadForge(String minecraftVersion, List modLoaders) { - if (CollectionUtils.isNotEmpty(modLoaders) || Strings.isNullOrEmpty(minecraftVersion)) { + if (CollectionUtils.isEmpty(modLoaders) || Strings.isNullOrEmpty(minecraftVersion)) { log.debug("No Forge or Minecraft version found in manifest, skipping"); return; } From ced5f54ca8c3ac8077d64bdc2492d9e4c9bb3820 Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Wed, 30 Mar 2016 19:07:26 -0500 Subject: [PATCH 76/86] Fix Changing the Mod name breaking downloads for Curse Mods. Added a new manifest test file that should prove that this works. --- .../container/CurseFile.java | 8 +- src/main/resources/twbb-manifest.json | 478 ++++++++++++++++++ 2 files changed, 484 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/twbb-manifest.json diff --git a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java index 2aa89ec..248fcb4 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/CurseFile.java @@ -32,11 +32,13 @@ public class CurseFile extends Mod { @Expose public String releaseType; private String projectUrl; + private String projectName; public CurseFile() { } + @Override public void init() { setProjectUrl(buildProjectUrl()); @@ -45,8 +47,10 @@ public void init() { conn.setInstanceFollowRedirects(false); conn.connect(); + setProjectName(conn.getHeaderField("Location").split("/")[2]); + if (Strings.isNullOrEmpty(getName())) { - setName(conn.getHeaderField("Location").split("/")[2]); + setName(getProjectName()); } } catch (final IOException e) { log.error(e.getMessage()); @@ -61,7 +65,7 @@ public String buildProjectUrl() { @Override public String getDownloadUrl() { - return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectID(), getName(), + return String.format(Reference.CURSEFORGE_BASE_URL + "%s-%s/files/%s/download", getProjectID(), getProjectName(), getFileID()); } diff --git a/src/main/resources/twbb-manifest.json b/src/main/resources/twbb-manifest.json new file mode 100644 index 0000000..cf286c2 --- /dev/null +++ b/src/main/resources/twbb-manifest.json @@ -0,0 +1,478 @@ +{ + "minecraft": { + "version": "1.7.10", + "modLoaders": [ + { + "id": "forge-10.13.4.1566", + "primary": true, + "folder": "common/base/loaders", + "downloadInstaller": true, + "downloadUniversal": true + } + ] + }, + "curseFiles": [ + { + "fileID": 2284130, + "projectID": 225635, + "name": "Agricraft" + }, + { + "fileID": 2273753, + "projectID": 221863, + "name": "Another One Bites The Dust" + }, + { + "fileID": 2263870, + "projectID": 230292, + "name": "Another One Bites The Dust Berry Bushes" + }, + { + "fileID": 2271214, + "projectID": 227795, + "name": "Antique Atlas" + }, + { + "fileID": 2277393, + "projectID": 224472, + "name": "AppleCore" + }, + { + "fileID": 2257644, + "projectID": 223735, + "name": "Aroma1997Core" + }, + { + "fileID": 2284754, + "projectID": 225658, + "name": "AromaBackup" + }, + { + "fileID": 2231153, + "projectID": 228356, + "name": "ATG" + }, + { + "fileID": 2274344, + "projectID": 230120, + "name": "Better Beginnings" + }, + { + "fileID": 2281726, + "projectID": 220318, + "name": "Biomes O' Plenty" + }, + { + "fileID": 2276097, + "projectID": 230368, + "name": "Block Properties" + }, + { + "fileID": 2264826, + "projectID": 224791, + "name": "Blood Magic" + }, + { + "fileID": 2227503, + "projectID": 59218, + "name": "bspkrsCore" + }, + { + "fileID": 2262089, + "projectID": 222213, + "name": "CodeChickenCore" + }, + { + "fileID": 2283118, + "projectID": 69162, + "name": "CoFHCore" + }, + { + "fileID": 2248699, + "projectID": 222908, + "name": "CookieCore" + }, + { + "fileID": 2263858, + "projectID": 231484, + "name": "Cooking For Blockheads" + }, + { + "fileID": 2285744, + "projectID": 237749, + "name": "CoroUtil" + }, + { + "fileID": 2264037, + "projectID": 237130, + "name": "Custom Ore Gen" + }, + { + "fileID": 2240043, + "projectID": 224039, + "name": "Dark Menagerie" + }, + { + "fileID": 2214317, + "projectID": 60098, + "name": "Deadly World" + }, + { + "fileID": 2234692, + "projectID": 224942, + "name": "Dense Ores" + }, + { + "fileID": 2248782, + "projectID": 74246, + "name": "Doomlike Dungeons" + }, + { + "fileID": 2266611, + "projectID": 231868, + "name": "EnderCore" + }, + { + "fileID": 2269710, + "projectID": 230236, + "name": "Enviromine" + }, + { + "fileID": 2288067, + "projectID": 233180, + "name": "ExCore" + }, + { + "fileID": 2242266, + "projectID": 66776, + "name": "Falling Meteors" + }, + { + "fileID": 2281555, + "projectID": 229708, + "name": "Farseek API" + }, + { + "fileID": 2265558, + "projectID": 224867, + "name": "Flaxbeard's Steam Power" + }, + { + "fileID": 2242993, + "projectID": 229323, + "name": "Forge Multipart" + }, + { + "fileID": 2280216, + "projectID": 238928, + "name": "Hammerz" + }, + { + "fileID": 2262352, + "projectID": 225957, + "name": "Hardcore Darkness" + }, + { + "fileID": 2274119, + "projectID": 228015, + "name": "Hardcore Ender Expansion" + }, + { + "fileID": 2282932, + "projectID": 232257, + "name": "Harder Wildlife" + }, + { + "fileID": 2282930, + "projectID": 232071, + "name": "HardLib" + }, + { + "fileID": 2237679, + "projectID": 224476, + "name": "Hunger Overhaul" + }, + { + "fileID": 2245004, + "projectID": 228798, + "name": "Iguana Tweaks" + }, + { + "fileID": 2237600, + "projectID": 223248, + "name": "Iguana's Tinker Tweaks" + }, + { + "fileID": 2227584, + "projectID": 227875, + "name": "Infernal Mobs" + }, + { + "fileID": 2238507, + "projectID": 223792, + "name": "INPureCore" + }, + { + "fileID": 2210792, + "projectID": 223094, + "name": "Inventory Tweaks" + }, + { + "fileID": 2283911, + "projectID": 237746, + "name": "Localized Weather and Storms 2" + }, + { + "fileID": 2225146, + "projectID": 225605, + "name": "LunatriusCore" + }, + { + "fileID": 2268607, + "projectID": 224770, + "name": "Lycanite's Mobs" + }, + { + "fileID": 2264244, + "projectID": 74924, + "name": "Mantle" + }, + { + "fileID": 2217136, + "projectID": 59641, + "name": "Metallurgy" + }, + { + "fileID": 2215714, + "projectID": 78640, + "name": "Metallurgy Core" + }, + { + "fileID": 2262994, + "projectID": 59710, + "name": "Mine and Blade Battlegear 2" + }, + { + "fileID": 2259434, + "projectID": 224029, + "name": "MineTweaker3" + }, + { + "fileID": 2281386, + "projectID": 224298, + "name": "Mob Properties" + }, + { + "fileID": 2232516, + "projectID": 76734, + "name": "MobiusCore" + }, + { + "fileID": 2270009, + "projectID": 220954, + "name": "ModTweaker" + }, + { + "fileID": 2216125, + "projectID": 74120, + "name": "Natura" + }, + { + "fileID": 2214321, + "projectID": 224296, + "name": "Natural Absorption" + }, + { + "fileID": 2282522, + "projectID": 225251, + "name": "NEI Integration" + }, + { + "fileID": 2277488, + "projectID": 66675, + "name": "Nether Ores" + }, + { + "fileID": 2288662, + "projectID": 238622, + "name": "NincraftLib" + }, + { + "fileID": 2262091, + "projectID": 222211, + "name": "Not Enough Items" + }, + { + "fileID": 2272129, + "projectID": 233472, + "name": "Ore Flowers" + }, + { + "fileID": 2270206, + "projectID": 221857, + "name": "Pam's Harvestcraft" + }, + { + "fileID": 2206430, + "projectID": 221916, + "name": "Pam's Temperate Plants" + }, + { + "fileID": 2285499, + "projectID": 224125, + "name": "PneumaticCraft" + }, + { + "fileID": 2229278, + "projectID": 224492, + "name": "PoorOres" + }, + { + "fileID": 2225310, + "projectID": 59816, + "name": "Random Things" + }, + { + "fileID": 2209081, + "projectID": 220611, + "name": "RPG Advanced Mod" + }, + { + "fileID": 2280980, + "projectID": 234840, + "name": "Salty Mod" + }, + { + "fileID": 2264022, + "projectID": 231126, + "name": "Simple Achievements" + }, + { + "fileID": 2281145, + "projectID": 224301, + "name": "Special AI" + }, + { + "fileID": 2279900, + "projectID": 59968, + "name": "Special Mobs" + }, + { + "fileID": 2248339, + "projectID": 225606, + "name": "Stackie" + }, + { + "fileID": 2246908, + "projectID": 227886, + "name": "Stalker Creepers" + }, + { + "fileID": 2281587, + "projectID": 229769, + "name": "Streams" + }, + { + "fileID": 2277396, + "projectID": 220811, + "name": "The Spice of Life" + }, + { + "fileID": 2282520, + "projectID": 222880, + "name": "Thermal Foundation" + }, + { + "fileID": 2277012, + "projectID": 74072, + "name": "Tinkers' Construct" + }, + { + "fileID": 2269014, + "projectID": 233076, + "name": "Tinkers' Steelworks" + }, + { + "fileID": 2281619, + "projectID": 227449, + "name": "TNTUtils" + }, + { + "fileID": 2261887, + "projectID": 220845, + "name": "Too Much Loot" + }, + { + "fileID": 2276961, + "projectID": 241052, + "name": "TWBB Tweaks" + }, + { + "fileID": 2238492, + "projectID": 227639, + "name": "Twilight Forest" + }, + { + "fileID": 2272810, + "projectID": 236923, + "name": "Ye Olde Tanks" + }, + { + "fileID": 2265743, + "projectID": 237754, + "name": "Zombie Awareness" + } + ], + "thirdParty": [ + { + "url": "http://files.vex.tty.sh/AsieLib/AsieLib-1.7.10-0.4.5.jar", + "name": "AsieLib" + }, + { + "url": "https://github.com/copygirl/BetterStorage/releases/download/v0.13.1.127/BetterStorage-1.7.10-0.13.1.127.jar", + "name": "BetterStorage" + }, + { + "url": "http://download1645.mediafire.com/54mtw8wd48bg/ciaw1m3e7nt1cw8/ChameleonCreepers-1.2-1.7.10.jar", + "name": "Chameleon Creepers" + }, + { + "url": "https://github.com/INpureProjects/INpureCore/raw/master/libs/commons-codec-1.9.jar", + "name": "common Codec", + "folder": "common/base/mods/1.7.10" + }, + { + "url": "https://github.com/INpureProjects/INpureCore/raw/master/libs/commons-compress-1.8.1.jar", + "name": "Common Compress", + "folder": "common/base/mods/1.7.10" + }, + { + "url": "http://download1507.mediafire.com/3ham0imat20g/2gaj88zw41opn8q/AvoidExplodingCreepers-1.7.10-LATEST.jar", + "name": "Creeper Awareness" + }, + { + "url": "https://github.com/CannibalVox/DimDoors/releases/download/2.2.5-test9/DimensionalDoors-2.2.5-test9.jar", + "name": "Dimensional Doors" + }, + { + "url": "http://download2218.mediafire.com/arly2nfr77sg/kdxf6q3kzef75fa/airoverhaul-1.7.10_1.0.jar", + "name": "Drowning Overhaul" + }, + { + "url": "http://ci.tterrag.com/job/ModpackTweaks/26/artifact/build/libs/ModpackTweaks-MC1.7.10-1.2.0-26.jar", + "name": "Modpack Tweaks" + }, + { + "url": "http://dl.dropboxusercontent.com/u/4295615/OpenEye-0.6-1.7.10.jar", + "name": "OpenEye" + }, + { + "url": "https://www.dropbox.com/s/6t4vp3wn4xr83xn/primitivemobs-1.0c-1.7.10.jar?dl=1", + "name": "Primitive Mobs" + }, + { + "url": "http://codeheist.net:8080/view/Somnia/job/Somnia%20-%201.7.10/55/artifact/dist/Somnia-1.4.8.55.jar", + "name": "Somnia" + } + ] +} \ No newline at end of file From ac5ea60195cc77671f1ceb2f14da25e01fac123c Mon Sep 17 00:00:00 2001 From: UndeadZeratul Date: Wed, 30 Mar 2016 19:11:21 -0500 Subject: [PATCH 77/86] Also fix Updating mods that have had their names changed. --- .../com/nincraft/modpackdownloader/handler/CurseModHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 7a57858..6f8e493 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -103,7 +103,7 @@ private static void updateCurseMod(final CurseFile mod) { conn.setInstanceFollowRedirects(false); conn.connect(); - fileListJson = (JSONObject) getCurseProjectJson(mod.getProjectID(), mod.getName(), new JSONParser()) + fileListJson = (JSONObject) getCurseProjectJson(mod.getProjectID(), mod.getProjectName(), new JSONParser()) .get("files"); if (fileListJson == null) { From 566d9efab31f864381fddd7619b19db391184f06 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sat, 9 Apr 2016 01:09:52 -0500 Subject: [PATCH 78/86] Fix rename for Forge downloads --- .../container/ModLoader.java | 13 ++++++++--- .../handler/ForgeHandler.java | 17 +++++++------- .../util/FileSystemHelper.java | 22 ++++++++++++++++++- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java b/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java index 080bee8..fc8dd33 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/ModLoader.java @@ -20,13 +20,20 @@ public class ModLoader { @SerializedName("folder") @Expose private String folder; - @SerializedName("rename") - @Expose - private String rename; @SerializedName("downloadInstaller") @Expose private Boolean downloadInstaller; @SerializedName("downloadUniversal") @Expose private Boolean downloadUniversal; + @SerializedName("renameInstaller") + @Expose + private String renameInstaller; + @SerializedName("renameUniversal") + @Expose + private String renameUniversal; + + public String getRename(boolean downloadInstaller) { + return downloadInstaller ? getRenameInstaller() : getRenameUniversal(); + } } \ No newline at end of file diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index 42aaced..eefa0de 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -29,27 +29,28 @@ public static void downloadForge(String minecraftVersion, List modLoa log.info(String.format("Downloading Forge version %s", forgeVersion)); if (modLoader.getDownloadInstaller() != null && modLoader.getDownloadInstaller()) { - downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, Reference.forgeInstaller); + downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, true); } if (modLoader.getDownloadUniversal() != null && modLoader.getDownloadUniversal()) { - downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, Reference.forgeUniversal); + downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, false); } } - private static void downloadForgeFile(String minecraftVersion, ModLoader modLoader, String folder, String forgeId, String fileType) { + private static void downloadForgeFile(String minecraftVersion, ModLoader modLoader, String folder, String forgeId, boolean downloadInstaller) { String forgeFileName = "forge-" + minecraftVersion + "-" + forgeId; String forgeURL = Reference.forgeURL + minecraftVersion + "-" + forgeId; if (!minecraftVersion.startsWith("1.8")) { forgeFileName += "-" + minecraftVersion; forgeURL += "-" + minecraftVersion; } - forgeFileName += fileType; + + forgeFileName += downloadInstaller ? Reference.forgeInstaller : Reference.forgeUniversal; forgeURL += "/" + forgeFileName; if (!FileSystemHelper.isInLocalRepo("forge", forgeFileName) || Reference.forceDownload) { File downloadedFile; - if (modLoader.getRename() != null) { - downloadedFile = FileSystemHelper.getDownloadedFile(modLoader.getRename(), folder); + if (modLoader.getRename(downloadInstaller) != null) { + downloadedFile = FileSystemHelper.getDownloadedFile(modLoader.getRename(downloadInstaller), folder); } else { downloadedFile = FileSystemHelper.getDownloadedFile(forgeFileName, folder); } @@ -60,9 +61,9 @@ private static void downloadForgeFile(String minecraftVersion, ModLoader modLoad log.error(String.format("Could not download %s.", forgeFileName), e.getMessage()); return; } - FileSystemHelper.copyToLocalRepo("forge", downloadedFile); + FileSystemHelper.copyToLocalRepo("forge", downloadedFile, forgeFileName); } else { - FileSystemHelper.copyFromLocalRepo("forge", forgeFileName, folder); + FileSystemHelper.copyFromLocalRepo("forge", forgeFileName, folder, modLoader.getRename(downloadInstaller)); } log.info(String.format("Completed downloading Forge version %s", forgeFileName)); } diff --git a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java index 120f9bc..675df42 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java @@ -32,13 +32,33 @@ public static void copyToLocalRepo(final String projectName, final File download } } + public static void copyToLocalRepo(final String projectName, File downloadedFile, String originalName) { + val newProjectName = getProjectNameOrDefault(projectName); + + try { + File copyFile = getLocalFile(downloadedFile.getName(), newProjectName); + FileUtils.copyFileToDirectory(downloadedFile, new File(Reference.userhome + newProjectName)); + copyFile.renameTo(new File(copyFile.getParent() + File.separator + originalName)); + } catch (final IOException e) { + log.error(String.format("Could not copy %s to local repo.", newProjectName), e); + } + } + public static void copyFromLocalRepo(final String projectName, final String fileName, String folder) { + copyFromLocalRepo(projectName, fileName, folder, null); + } + + public static void copyFromLocalRepo(final String projectName, final String fileName, String folder, String rename) { val newProjectName = getProjectNameOrDefault(projectName); if (folder == null) { folder = Reference.modFolder; } try { - FileUtils.copyFileToDirectory(getLocalFile(fileName, newProjectName), new File(folder)); + File copyFile = getLocalFile(fileName, newProjectName); + FileUtils.copyFileToDirectory(copyFile, new File(folder)); + if (rename != null) { + copyFile.renameTo(new File(copyFile.getParent() + File.separator + rename)); + } } catch (final IOException e) { log.error(String.format("Could not copy %s from local repo.", newProjectName), e); } From e1f59f4f272c22113ca86e835c112c8bc2835d32 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Tue, 12 Apr 2016 21:52:02 -0500 Subject: [PATCH 79/86] Fix renaming local repo files Closes #19 --- .../nincraft/modpackdownloader/util/FileSystemHelper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java index 675df42..126e1cb 100644 --- a/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java +++ b/src/main/java/com/nincraft/modpackdownloader/util/FileSystemHelper.java @@ -54,10 +54,10 @@ public static void copyFromLocalRepo(final String projectName, final String file folder = Reference.modFolder; } try { - File copyFile = getLocalFile(fileName, newProjectName); - FileUtils.copyFileToDirectory(copyFile, new File(folder)); + FileUtils.copyFileToDirectory(getLocalFile(fileName, newProjectName), new File(folder)); + File downloadedFile = getDownloadedFile(fileName); if (rename != null) { - copyFile.renameTo(new File(copyFile.getParent() + File.separator + rename)); + downloadedFile.renameTo(new File(downloadedFile.getParent() + File.separator + rename)); } } catch (final IOException e) { log.error(String.format("Could not copy %s from local repo.", newProjectName), e); From a014d0ce2c5795deed3dee0c809be22f0a9044b3 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 13 Apr 2016 22:34:22 -0500 Subject: [PATCH 80/86] Refactor modloader processing --- pom.xml | 5 +++++ .../handler/ForgeHandler.java | 22 ++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 9445330..6c8fd71 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,11 @@ commons-collections4 4.1 + + org.apache.commons + commons-lang3 + 3.4 + org.apache.logging.log4j log4j-core diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java index eefa0de..d1acd75 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/ForgeHandler.java @@ -7,6 +7,7 @@ import lombok.extern.log4j.Log4j2; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.BooleanUtils; import java.io.File; import java.io.IOException; @@ -21,18 +22,19 @@ public static void downloadForge(String minecraftVersion, List modLoa return; } - ModLoader modLoader = modLoaders.get(0); - String forgeVersion = modLoader.getId(); - String folder = modLoader.getFolder(); - String forgeId = forgeVersion.substring(forgeVersion.indexOf("-") + 1); + for (ModLoader modLoader : modLoaders) { + String forgeVersion = modLoader.getId(); + String folder = modLoader.getFolder(); + String forgeId = forgeVersion.substring(forgeVersion.indexOf("-") + 1); - log.info(String.format("Downloading Forge version %s", forgeVersion)); + log.info(String.format("Downloading Forge version %s", forgeVersion)); - if (modLoader.getDownloadInstaller() != null && modLoader.getDownloadInstaller()) { - downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, true); - } - if (modLoader.getDownloadUniversal() != null && modLoader.getDownloadUniversal()) { - downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, false); + if (BooleanUtils.isTrue(modLoader.getDownloadInstaller())) { + downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, true); + } + if (BooleanUtils.isTrue(modLoader.getDownloadUniversal())) { + downloadForgeFile(minecraftVersion, modLoader, folder, forgeId, false); + } } } From c0a6de2a49201db533a7773febdf577de4ea7573 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 13 Apr 2016 23:26:17 -0500 Subject: [PATCH 81/86] Update curseFiles JSON tag to files Closes #21 --- .../com/nincraft/modpackdownloader/container/Manifest.java | 4 +--- .../nincraft/modpackdownloader/manager/ModListManager.java | 3 +-- src/main/resources/all-mods.json | 2 +- src/main/resources/client-mods.json | 2 +- src/main/resources/manifest.json | 2 +- src/main/resources/twbb-manifest.json | 2 +- 6 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java index 1d83c5d..3afa71e 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Manifest.java @@ -19,11 +19,9 @@ public class Manifest { @SerializedName("minecraft") @Expose public Minecraft minecraft; - @SerializedName("curseFiles") + @SerializedName("files") @Expose public List curseFiles = new ArrayList(); - @SerializedName("files") - public List curseManifestFiles = new ArrayList(); @SerializedName("thirdParty") @Expose public List thirdParty = new ArrayList(); diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 39b4efc..978e958 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -64,12 +64,11 @@ public static int buildModList() { } manifestFile = gson.fromJson(jsonLists.toString(), Manifest.class); - if (!manifestFile.getCurseManifestFiles().isEmpty()) { + if (!manifestFile.getCurseFiles().isEmpty()) { backupCurseManifest(); } Reference.mcVersion = manifestFile.getMinecraftVersion(); - manifestFile.getCurseFiles().addAll(manifestFile.getCurseManifestFiles()); MOD_LIST.addAll(manifestFile.getCurseFiles()); MOD_LIST.addAll(manifestFile.getThirdParty()); Reference.updateTotal = Reference.downloadTotal = MOD_LIST.size(); diff --git a/src/main/resources/all-mods.json b/src/main/resources/all-mods.json index 1628eb7..2879e82 100644 --- a/src/main/resources/all-mods.json +++ b/src/main/resources/all-mods.json @@ -1,5 +1,5 @@ { - "curseFiles": [ + "files": [ { "fileID": 2277394, "name": "AppleCore", diff --git a/src/main/resources/client-mods.json b/src/main/resources/client-mods.json index 0fd0d4a..45551ce 100644 --- a/src/main/resources/client-mods.json +++ b/src/main/resources/client-mods.json @@ -1,5 +1,5 @@ { - "curseFiles": [ + "files": [ { "fileID": 2275454, "projectID": 224223 diff --git a/src/main/resources/manifest.json b/src/main/resources/manifest.json index b73944c..393b162 100644 --- a/src/main/resources/manifest.json +++ b/src/main/resources/manifest.json @@ -8,7 +8,7 @@ } ] }, - "curseFiles": [ + "files": [ { "fileID": 2287496, "projectID": 223628, diff --git a/src/main/resources/twbb-manifest.json b/src/main/resources/twbb-manifest.json index cf286c2..eeb737f 100644 --- a/src/main/resources/twbb-manifest.json +++ b/src/main/resources/twbb-manifest.json @@ -11,7 +11,7 @@ } ] }, - "curseFiles": [ + "files": [ { "fileID": 2284130, "projectID": 225635, From be3b7f439a39e3d1ac78b0c4879f3edec6e82eaa Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 13 Apr 2016 23:27:14 -0500 Subject: [PATCH 82/86] Implement ExecutorService Hopefully will prevent any hanging. Closes #20 --- .../modpackdownloader/ModPackDownloader.java | 4 +-- .../manager/ModListManager.java | 27 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java index 9c601af..505e90a 100644 --- a/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java +++ b/src/main/java/com/nincraft/modpackdownloader/ModPackDownloader.java @@ -103,7 +103,7 @@ private static void processMods() throws InterruptedException { Reference.mcVersion, Reference.releaseType)); ModListManager.updateMods(); - while (!(Reference.updateCount >= Reference.updateTotal)) { + while (!ModListManager.getExecutorService().isTerminated()) { Thread.sleep(1); } @@ -114,7 +114,7 @@ private static void processMods() throws InterruptedException { Reference.modFolder)); ModListManager.downloadMods(); - while (!(Reference.downloadCount >= Reference.downloadTotal)) { + while (!ModListManager.getExecutorService().isTerminated()) { Thread.sleep(1); } log.info("Finished downloading mods."); diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 978e958..6d966a2 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -9,9 +9,12 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import com.nincraft.modpackdownloader.container.*; import com.nincraft.modpackdownloader.handler.ForgeHandler; +import lombok.Getter; import org.apache.commons.io.FileUtils; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -36,6 +39,9 @@ public class ModListManager { public static final Map, ModHandler> MOD_HANDLERS = Maps.newHashMap(); + @Getter + public static ExecutorService executorService; + private static Manifest manifestFile; private static Gson gson = new Gson(); @@ -101,35 +107,44 @@ public static Optional getThirdPartyModList(final JSONObject jsonList } public static final void downloadMods() { - new Thread(() -> { + executorService = Executors.newFixedThreadPool(MOD_LIST.size()+1); + Runnable forgeThread = new Thread(() -> { ForgeHandler.downloadForge(manifestFile.getMinecraftVersion(), manifestFile.getMinecraft().getModLoaders()); - }).start(); + }); + + executorService.execute(forgeThread); log.trace(String.format("Downloading %s mods...", MOD_LIST.size())); int downloadCount = 1; for (val mod : MOD_LIST) { log.info(String.format(Reference.DOWNLOADING_MOD_X_OF_Y, mod.getName(), downloadCount++, Reference.downloadTotal)); - new Thread(() -> { + + Runnable modDownload = new Thread(() -> { MOD_HANDLERS.get(mod.getClass()).downloadMod(mod); Reference.downloadCount++; log.info(String.format("Finished downloading %s", mod.getName())); - }).start(); + }); + executorService.execute(modDownload); } + executorService.shutdown(); log.trace(String.format("Finished downloading %s mods.", MOD_LIST.size())); } public static final void updateMods() { log.trace(String.format("Updating %s mods...", Reference.updateTotal)); + executorService = Executors.newFixedThreadPool(MOD_LIST.size()); int updateCount = 1; for (val mod : MOD_LIST) { log.info(String.format(Reference.UPDATING_MOD_X_OF_Y, mod.getName(), updateCount++, Reference.updateTotal)); - new Thread(() -> { + Runnable modUpdate = new Thread(() -> { MOD_HANDLERS.get(mod.getClass()).updateMod(mod); Reference.updateCount++; log.info(String.format("Finished updating %s", mod.getName())); - }).start(); + }); + executorService.execute(modUpdate); } + executorService.shutdown(); log.trace(String.format("Finished updating %s mods.", Reference.updateTotal)); } From f7d914aaa350f1502f94c313a9a2b11a6d3a22c2 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 13 Apr 2016 23:57:18 -0500 Subject: [PATCH 83/86] Remove unused methods --- .../modpackdownloader/manager/ModListManager.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java index 6d966a2..c077d95 100644 --- a/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java +++ b/src/main/java/com/nincraft/modpackdownloader/manager/ModListManager.java @@ -8,7 +8,6 @@ import java.util.Comparator; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -16,7 +15,6 @@ import com.nincraft.modpackdownloader.handler.ForgeHandler; import lombok.Getter; import org.apache.commons.io.FileUtils; -import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -97,15 +95,6 @@ private static void backupCurseManifest() { } } - public static Optional getCurseModList(final JSONObject jsonList) { - return Optional.ofNullable( - (JSONArray) (jsonList.containsKey("curseFiles") ? jsonList.get("curseFiles") : jsonList.get("files"))); - } - - public static Optional getThirdPartyModList(final JSONObject jsonLists) { - return Optional.ofNullable((JSONArray) jsonLists.get("thirdParty")); - } - public static final void downloadMods() { executorService = Executors.newFixedThreadPool(MOD_LIST.size()+1); Runnable forgeThread = new Thread(() -> { From bff48806b3500ddff08e7592c7551bd2249c4c8a Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 9 May 2016 15:32:38 -0500 Subject: [PATCH 84/86] Add ability to skip downloading mods if no files found This will allow moving between Minecraft versions to be a little easier. If no files are found and its not an alpha, then it will disable the mod from downloading. Mods that are disabled but now have files found will be re enabled. --- .../modpackdownloader/handler/CurseModHandler.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 6f8e493..11fa68d 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -11,6 +11,7 @@ import java.util.Collections; import java.util.List; +import org.apache.commons.lang3.BooleanUtils; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -149,6 +150,14 @@ private static CurseFile getLatestVersion(final String mcVersion, final String r newMod.setFileID(fileIds.get(0).intValue()); newMod.setVersion((String) ((JSONObject) fileListJson.get(newMod.getFileID().toString())).get("name")); } + if (!releaseType.equals("alpha") && fileIds.isEmpty()) { + log.info(String.format("No files found for this Minecraft version, disabling download of %s", curseMod.getName())); + curseMod.setSkipUpdate(true); + } + if (BooleanUtils.isTrue(curseMod.getSkipUpdate()) && !fileIds.isEmpty()) { + log.info(String.format("Found files for this Minecraft version, enabling download of %s", curseMod.getName())); + curseMod.setSkipUpdate(null); + } log.trace("Finished getting most recent available file."); return newMod; @@ -159,7 +168,7 @@ private static boolean equalOrLessThan(final String modRelease, final String rel || "beta".equals(releaseType) && "release".equals(modRelease); } - private static JSONObject getCurseProjectJson(final Integer integer, final String projectName, + private static JSONObject getCurseProjectJson(final Integer projectId, final String projectName, final JSONParser projectParser) throws ParseException, IOException { log.trace("Getting CurseForge Widget JSON..."); try { @@ -168,7 +177,7 @@ private static JSONObject getCurseProjectJson(final Integer integer, final Strin return (JSONObject) projectParser .parse(new BufferedReader(new InputStreamReader(new URL(urlStr).openStream()))); } catch (final FileNotFoundException e) { - String urlStr = String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, integer + "-" + projectName); + String urlStr = String.format(Reference.CURSEFORGE_WIDGET_JSON_URL, projectId + "-" + projectName); log.debug(urlStr); return (JSONObject) projectParser .parse(new BufferedReader(new InputStreamReader(new URL(urlStr).openStream()))); From 7cc0a0be5aa86aeb9074292050be75bfb61cc54c Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Mon, 9 May 2016 15:43:28 -0500 Subject: [PATCH 85/86] Actually skip downloading and not updating gj 10/10 --- .../com/nincraft/modpackdownloader/container/Mod.java | 3 +++ .../modpackdownloader/handler/CurseModHandler.java | 11 ++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java index 2786155..714287b 100644 --- a/src/main/java/com/nincraft/modpackdownloader/container/Mod.java +++ b/src/main/java/com/nincraft/modpackdownloader/container/Mod.java @@ -19,6 +19,9 @@ public abstract class Mod implements Cloneable { @SerializedName("skipUpdate") @Expose private Boolean skipUpdate; + @SerializedName("skipDownload") + @Expose + private Boolean skipDownload; @SerializedName("folder") @Expose private String folder; diff --git a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java index 11fa68d..1fe68b4 100644 --- a/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java +++ b/src/main/java/com/nincraft/modpackdownloader/handler/CurseModHandler.java @@ -39,6 +39,11 @@ public void updateMod(final Mod mod) { } private static void downloadCurseMod(final CurseFile mod) { + if(BooleanUtils.isTrue(mod.getSkipDownload())){ + log.info(String.format("Skipped downloading %s", mod.getName())); + return; + } + val modName = mod.getName(); try { @@ -150,13 +155,13 @@ private static CurseFile getLatestVersion(final String mcVersion, final String r newMod.setFileID(fileIds.get(0).intValue()); newMod.setVersion((String) ((JSONObject) fileListJson.get(newMod.getFileID().toString())).get("name")); } - if (!releaseType.equals("alpha") && fileIds.isEmpty()) { + if (!"alpha".equals(releaseType) && fileIds.isEmpty()) { log.info(String.format("No files found for this Minecraft version, disabling download of %s", curseMod.getName())); - curseMod.setSkipUpdate(true); + curseMod.setSkipDownload(true); } if (BooleanUtils.isTrue(curseMod.getSkipUpdate()) && !fileIds.isEmpty()) { log.info(String.format("Found files for this Minecraft version, enabling download of %s", curseMod.getName())); - curseMod.setSkipUpdate(null); + curseMod.setSkipDownload(null); } log.trace("Finished getting most recent available file."); From e2e38a54a76447c67303d19ea8c1d8ffadbb60c2 Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Wed, 11 May 2016 21:16:42 -0500 Subject: [PATCH 86/86] Update version for release --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6c8fd71..a47b74b 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.nincraft ModPackDownloader - 0.0.1 + 0.1.0 Mod Pack Downloader jar