diff --git a/build.gradle b/build.gradle index d95073b..9280ae1 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ buildscript { apply plugin: 'forge' -version = "2.0" +version = "2.1" group= "com.hrudyplayz.mcinstanceloader" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "mcinstanceloader" diff --git a/src/main/java/com/hrudyplayz/mcinstanceloader/Config.java b/src/main/java/com/hrudyplayz/mcinstanceloader/Config.java index 0a1d3f5..5bdcb75 100644 --- a/src/main/java/com/hrudyplayz/mcinstanceloader/Config.java +++ b/src/main/java/com/hrudyplayz/mcinstanceloader/Config.java @@ -56,9 +56,6 @@ public static void createConfigFile() { curseforgeAPIKey = config.getString("CFCore API key", CATEGORY_BEHAVIOR, "", "The API key to use if you use the official Curseforge API."); - System.out.println(curseforgeURL); - System.out.println(curseforgeAPIKey); - mainMenuClassPaths = config.getStringList("Menu class paths", CATEGORY_GUI, new String[]{"net.minecraft.client.gui.GuiMainMenu"}, "List of Java class paths for menus that will get interrupted by this mod's GUI. May be useful for mods that change the main menu."); closeGameTimer = config.getInt("Close game timer",CATEGORY_GUI, 10, 0, Integer.MAX_VALUE, "Delay before automatically closing the game on the success/fail screen. Set to 0 to disable."); amountOfDisplayedErrors = config.getInt("Maximum amount of displayed errors", CATEGORY_GUI, 5, 0, 64, "Maximum number of errors that can be displayed on the error screen at once."); diff --git a/src/main/java/com/hrudyplayz/mcinstanceloader/ModProperties.java b/src/main/java/com/hrudyplayz/mcinstanceloader/ModProperties.java index 7910e4b..076499c 100644 --- a/src/main/java/com/hrudyplayz/mcinstanceloader/ModProperties.java +++ b/src/main/java/com/hrudyplayz/mcinstanceloader/ModProperties.java @@ -10,7 +10,7 @@ public class ModProperties { // General values public static final String MODID = "mcinstanceloader"; public static final String NAME = "MCInstance Loader"; - public static final String VERSION = "2.0"; + public static final String VERSION = "2.1"; public static final String MC_VERSION = "1.7.10"; public static final String URL = "https://github.com/HRudyPlayZ/MCInstanceLoader/"; diff --git a/src/main/java/com/hrudyplayz/mcinstanceloader/utils/WebHelper.java b/src/main/java/com/hrudyplayz/mcinstanceloader/utils/WebHelper.java index 63b0fc4..6b4429d 100644 --- a/src/main/java/com/hrudyplayz/mcinstanceloader/utils/WebHelper.java +++ b/src/main/java/com/hrudyplayz/mcinstanceloader/utils/WebHelper.java @@ -1,27 +1,19 @@ package com.hrudyplayz.mcinstanceloader.utils; -import com.hrudyplayz.mcinstanceloader.Config; -import com.hrudyplayz.mcinstanceloader.Main; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpEntity; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.impl.client.LaxRedirectStrategy; - -import java.io.*; +import java.io.File; +import java.io.IOException; import java.net.*; import java.util.regex.Pattern; +import org.apache.commons.io.FileUtils; + +import com.hrudyplayz.mcinstanceloader.Config; +import com.hrudyplayz.mcinstanceloader.Main; + @SuppressWarnings("unused") public class WebHelper { // This class will allow to download a file from the internet. -// Most of it is adapted from https://github.com/Janrupf/mod-director/tree/master/mod-director-core/src/main/java/net/jan/moddirector/core/util. -// Shoutout to both Janrupf and HansWasser for making it in the first place. // Defines the client properties, uses Twitch UserAgents to make every website work correctly as they should. public static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) twitch-desktop-electron-platform/1.0.0 Chrome/73.0.3683.121 Electron/5.0.12 Safari/537.36 desklight/8.51.0"; @@ -30,44 +22,42 @@ public class WebHelper { public static boolean downloadFile (String fileURL, String savePath) { // Lets you download a file from a specific URL and save it to a location. - RequestConfig timeouts = RequestConfig.custom() // Creates the request config to set the custom timeout values - .setConnectTimeout(Config.connectionTimeout * 1000) - .setConnectionRequestTimeout(Config.connectionTimeout * 1000) - .setSocketTimeout(Config.connectionTimeout * 1000) - .build(); - - CloseableHttpClient client = HttpClients.custom() // Creates an httpClient with custom properties - .setUserAgent(USER_AGENT) // Adds the user-agent - .setRedirectStrategy(new LaxRedirectStrategy()) // Allows for redirect handling - .setDefaultRequestConfig(timeouts) - .disableContentCompression().build(); // Disables compression (slightly faster and more reliable downloads) and builds the client. + URL url; + try { + url = new URL(fileURL); + } + catch (Exception e) { + Main.errorContext = "The URL is invalid."; + return false; + } try { - HttpGet request = new HttpGet(new URL(fileURL).toURI()); // Creates the GET request. - request.addHeader("Referer", REFERER); // Adds the referer to the request. + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setInstanceFollowRedirects(true); - if (Config.curseforgeAPIKey.length() > 0) request.addHeader("x-api-key", Config.curseforgeAPIKey); + connection.setReadTimeout(Config.connectionTimeout * 1000); + connection.setConnectTimeout(Config.connectionTimeout * 1000); - HttpEntity entity = client.execute(request).getEntity(); - if (entity != null) FileUtils.copyInputStreamToFile(entity.getContent(), new File(savePath)); + connection.setRequestProperty("User-Agent", USER_AGENT); + connection.setRequestProperty("Referer", REFERER); + if (Config.curseforgeAPIKey.length() > 0) connection.setRequestProperty("x-api-key", Config.curseforgeAPIKey); + int responseCode = connection.getResponseCode(); + if (responseCode != HttpURLConnection.HTTP_OK) { + Main.errorContext = "Received an HTTP " + responseCode + " error."; + return false; + } + + File file = new File(savePath); + FileUtils.copyInputStreamToFile(connection.getInputStream(), file); return true; } - catch (URISyntaxException e) { - Main.errorContext = "The URL is invalid."; - return false; - } - catch (ClientProtocolException e) { - Main.errorContext = "HTTP Protocol violation."; - return false; - } catch (IOException e) { Main.errorContext = "There was an issue writing to file."; return false; } - finally { - IOUtils.closeQuietly(client); - } + } @@ -86,9 +76,7 @@ public static boolean downloadFile (String fileURL, String savePath, String[] fo String html = ""; for (String s : lines) html += s; - String matched = ">" + follows[counter] + "<"; - - // Yeah i parse HTML with regex, what are you gonna do? Kill me? x) + // This parses the HTML file using regex. // Thanks to the Melody language for letting us generate regex using a readable syntax :P String[] splitted = html.split(">\\s*" + Pattern.quote(follows[counter]) + "\\s*<"); if (splitted.length == 1) { @@ -101,19 +89,17 @@ public static boolean downloadFile (String fileURL, String savePath, String[] fo href = href.substring(0, href.indexOf("\"")); try { - URL url = new URL(fileURL); - fileURL = url.getProtocol() + "://" + url.getHost() + "/" + href; + if (href.startsWith("http://") || href.startsWith("https://")) fileURL = href; + else { + URL url = new URL(fileURL); + fileURL = url.getProtocol() + "://" + url.getHost() + "/" + href; + } } catch (Exception e) { Main.errorContext = "Following " + follows[counter] + " lead to an invalid URL."; return false; } - if (fileURL.isEmpty()) { - Main.errorContext = "Result URL was empty when following " + follows[counter] + "."; - return false; - } - counter += 1; } diff --git a/versionData.txt b/versionData.txt index bd364d3..b9964da 100644 --- a/versionData.txt +++ b/versionData.txt @@ -1,3 +1,3 @@ -version: 2.0 -fileName: mcinstanceloader-2.0.jar +version: 2.1 +fileName: mcinstanceloader-2.1.jar url: https://github.com/HRudyPlayZ/MCInstanceLoader/releases/download/1.7.10-2.0/mcinstanceloader-2.0.jar \ No newline at end of file