Skip to content

Commit

Permalink
Fixes to mp fetch system
Browse files Browse the repository at this point in the history
  • Loading branch information
XboxBedrock committed Jul 14, 2024
1 parent 95607da commit 37813a4
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 5 deletions.
Empty file modified gradlew
100644 → 100755
Empty file.
37 changes: 37 additions & 0 deletions src/main/java/net/buildtheearth/bteutilities/BTEUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@
import org.apache.logging.log4j.Logger;
import org.lwjgl.opengl.Display;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -73,6 +80,33 @@ public void preInit(FMLPreInitializationEvent event) {
String[] splitVersion = event.getModMetadata().version.split("\\.");
majorVersion = splitVersion[0] + "." + splitVersion[1];

//Hippity hoppity I no longer care about cert verification

TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};

// Enjoy not verifying SSL certs anymore
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (GeneralSecurityException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}


defaultImageFolder = new File(new File(Minecraft.getMinecraft().gameDir, "buildtheearth"), "default");
dynamicImageFolder = new File(new File(Minecraft.getMinecraft().gameDir, "buildtheearth"), "dynamic");
defaultImageFolder.mkdir();
Expand All @@ -90,6 +124,9 @@ public void init(FMLInitializationEvent event) {

@Mod.EventHandler
public void postInit(FMLPostInitializationEvent event) {

//Now the modpack should be able to load everything fine
new ModpackAPI().run();
ModpackAPI.load();
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new ModpackAPI(), 0, 10, TimeUnit.MINUTES);
BTENetworkHandler.registerHandlers();
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/net/buildtheearth/bteutilities/ModpackAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

import com.google.gson.JsonObject;
import lombok.Getter;
import net.minecraft.util.ResourceLocation;

import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -64,10 +66,12 @@ public static void load() {
dyn = BTEUtilities.parser.parse(new FileReader(new File(BTEUtilities.getInstance().dynamicImageFolder, "dynamic.json"))).getAsJsonObject();
} catch (FileNotFoundException e) {
}

for (int i = 0; i < 5; i++) {
boolean dynamic = new File(BTEUtilities.getInstance().dynamicImageFolder, "background" + (i + 1) + ".png").exists();
File image = new File(dynamic ? BTEUtilities.getInstance().dynamicImageFolder : BTEUtilities.getInstance().defaultImageFolder, "background" + (i + 1) + ".png");
JsonObject data = dynamic ? dyn : def;
System.out.println(data);
String credit = "No Credit Provided";
if (data.has(String.valueOf(i + 1))) {
credit = data.get(String.valueOf(i + 1)).getAsJsonObject().get("credit").getAsString();
Expand Down Expand Up @@ -130,7 +134,8 @@ public void run() {
}

String url = result.get("url").getAsString();
String credit = result.get("credit").getAsString();
String credit = null;
if (s != "logo") credit = result.get("credit").getAsString();
JsonObject localQuery = dynamicData.getAsJsonObject(query);

if (localQuery != null && localQuery.get("url").getAsString().equalsIgnoreCase(url)) {
Expand All @@ -141,8 +146,15 @@ public void run() {
BufferedImage image;
try {
URL imageURL = new URL(url);
image = ImageIO.read(imageURL);
HttpURLConnection con = (HttpURLConnection) imageURL.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
image = ImageIO.read(con.getInputStream());
} catch (IOException e) {
System.out.println("Failed to load image from " + url);
System.out.println(e.getMessage());
e.printStackTrace();
BTEUtilities.getLogger().error("Failed to download image: " + query);
continue;
}
Expand Down Expand Up @@ -173,10 +185,12 @@ public void run() {

try {
FileWriter dynamicWriter = new FileWriter(dynamic);
System.out.println(BTEUtilities.gson.toJson(dynamicData));
dynamicWriter.write(BTEUtilities.gson.toJson(dynamicData));
dynamicWriter.flush();
dynamicWriter.close();
} catch (IOException e) {
BTEUtilities.getLogger().error("Failed to save json: " + dynamic);
e.printStackTrace();
}
load();
Expand All @@ -197,6 +211,8 @@ private JsonObject getImageAPI() {
content.append(inputLine);
}
in.close();

System.out.println(content.toString());
return BTEUtilities.parser.parse(content.toString()).getAsJsonObject();
} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected void setup() {
for (int i = 0; i < modpackPictures.length; i++) {
ModpackPicture picture = modpackPictures[i];
backgrounds[i] = picture.getImage();
//Sometimes credit shows up, weird bug
credits[i] = picture.getCredit();
}
Image title = new Image(-137, 40, 512, 512, Alignment.top_center);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,35 @@

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;

public class TextureURL implements ITexture {
@Getter
private URL url;
@Getter
private boolean isURl = true;
@Getter
private InputStream bf;
@Setter
private int textureID;
@Setter
private BufferedImage bi;

public TextureURL(String url) {
public TextureURL(String url) {
this.textureID = -1;
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
isURl = false;
this.bf = getClass().getClassLoader()
.getResourceAsStream("assets/" + url.replace(":", "/") );
BTEUtilities.getLogger().log(Level.ERROR, "Invalid URL: " + url);
e.printStackTrace();
}
new LoadTextureURL(this).start();
}
Expand Down Expand Up @@ -81,7 +91,12 @@ public LoadTextureURL(TextureURL texture) {
public void run() {
BufferedImage bi = null;
try {
bi = ImageIO.read(texture.getUrl());
if (texture.isURl) {
bi = ImageIO.read(texture.getUrl());
} else {
bi = ImageIO.read(texture.getBf());
}

} catch (IOException ignored) {
}

Expand Down
Binary file modified src/main/resources/assets/bteutilities/gui/btelogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 37813a4

Please sign in to comment.