Skip to content

Commit

Permalink
Pushed release 2.1
Browse files Browse the repository at this point in the history
- Rewrote the WebHelper class again
- Fixed a crash linked to the Apache HTTP library missing on servers for some dumb reasons
- Fixed an error that could happen when following a link with a URL and not just a file name.
- Removed accidental debug print leftovers while loading the config
- Removed the empty URL follow error. (It was useless anyways)
  • Loading branch information
HRudyPlayZ committed Jul 15, 2022
1 parent 5d9fe1e commit 5053507
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 59 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/hrudyplayz/mcinstanceloader/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/";

Expand Down
90 changes: 38 additions & 52 deletions src/main/java/com/hrudyplayz/mcinstanceloader/utils/WebHelper.java
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
}

}


Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions versionData.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5053507

Please sign in to comment.