From f9fa7deb715c024621c9fdfab165629a6997b73c Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Thu, 26 May 2022 22:50:54 +0200 Subject: publishing and rename InternetUtils to NetworkUtils --- .../oneconfig/gui/elements/ColorSelector.java | 4 +- .../cc/polyfrost/oneconfig/gui/pages/HomePage.java | 10 +- .../polyfrost/oneconfig/lwjgl/RenderManager.java | 4 +- .../polyfrost/oneconfig/utils/InternetUtils.java | 108 --------------------- .../cc/polyfrost/oneconfig/utils/NetworkUtils.java | 108 +++++++++++++++++++++ 5 files changed, 117 insertions(+), 117 deletions(-) delete mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java (limited to 'src/main/java/cc') diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java index e85e223..4e04f5c 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java @@ -11,7 +11,7 @@ import cc.polyfrost.oneconfig.lwjgl.image.Images; import cc.polyfrost.oneconfig.lwjgl.image.SVGs; import cc.polyfrost.oneconfig.utils.ColorUtils; import cc.polyfrost.oneconfig.utils.InputUtils; -import cc.polyfrost.oneconfig.utils.InternetUtils; +import cc.polyfrost.oneconfig.utils.NetworkUtils; import cc.polyfrost.oneconfig.utils.MathUtils; import org.lwjgl.input.Mouse; @@ -318,7 +318,7 @@ public class ColorSelector { hueInput.setInput(String.format("%.01f", (float) color.getHue())); hexInput.setInput("#" + color.getHex()); } - if(guideBtn.isClicked()) InternetUtils.browseLink("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); + if(guideBtn.isClicked()) NetworkUtils.browseLink("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); // draw the color preview diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/pages/HomePage.java b/src/main/java/cc/polyfrost/oneconfig/gui/pages/HomePage.java index e2c342c..db19226 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/pages/HomePage.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/pages/HomePage.java @@ -10,7 +10,7 @@ import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.lwjgl.image.SVGs; import cc.polyfrost.oneconfig.utils.ColorUtils; import cc.polyfrost.oneconfig.utils.InputUtils; -import cc.polyfrost.oneconfig.utils.InternetUtils; +import cc.polyfrost.oneconfig.utils.NetworkUtils; import java.awt.*; @@ -32,11 +32,11 @@ public class HomePage extends Page { public HomePage() { super("Home Dashboard"); - socialsBtn.setClickAction(() -> InternetUtils.browseLink("https://twitter.com/polyfrost")); - discordBtn.setClickAction(() -> InternetUtils.browseLink("https://discord.gg/4BdUuGpMdf")); - webBtn.setClickAction(() -> InternetUtils.browseLink("https://polyfrost.cc")); + socialsBtn.setClickAction(() -> NetworkUtils.browseLink("https://twitter.com/polyfrost")); + discordBtn.setClickAction(() -> NetworkUtils.browseLink("https://discord.gg/4BdUuGpMdf")); + webBtn.setClickAction(() -> NetworkUtils.browseLink("https://polyfrost.cc")); creditsBtn.setClickAction(new CreditsPage()); - guideBtn.setClickAction(() -> InternetUtils.browseLink("https://www.youtube.com/watch?v=dQw4w9WgXcQ")); + guideBtn.setClickAction(() -> NetworkUtils.browseLink("https://www.youtube.com/watch?v=dQw4w9WgXcQ")); } diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/RenderManager.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/RenderManager.java index 8f43aa7..8d68715 100644 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/RenderManager.java +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/RenderManager.java @@ -12,7 +12,7 @@ import cc.polyfrost.oneconfig.libs.universal.UGraphics; import cc.polyfrost.oneconfig.libs.universal.UMinecraft; import cc.polyfrost.oneconfig.libs.universal.UResolution; import cc.polyfrost.oneconfig.utils.InputUtils; -import cc.polyfrost.oneconfig.utils.InternetUtils; +import cc.polyfrost.oneconfig.utils.NetworkUtils; import net.minecraft.client.gui.Gui; import net.minecraft.client.shader.Framebuffer; import org.lwjgl.nanovg.*; @@ -199,7 +199,7 @@ public final class RenderManager { float length = getTextWidth(vg, url, size, font); drawRectangle(vg, x, y + size / 2, length, 1, OneConfigConfig.PRIMARY_500); if(InputUtils.isAreaClicked((int) (x - 2), (int) (y - 1), (int) (length + 4), (int) (size / 2 + 3))) { - InternetUtils.browseLink(url); + NetworkUtils.browseLink(url); } } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java deleted file mode 100644 index 6d6d7ed..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java +++ /dev/null @@ -1,108 +0,0 @@ -package cc.polyfrost.oneconfig.utils; - -import com.google.gson.JsonElement; -import org.apache.commons.io.IOUtils; - -import java.awt.*; -import java.io.*; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; - -public class InternetUtils { - private static InputStream setupConnection(String url, String userAgent, int timeout, boolean useCaches) throws IOException { - HttpURLConnection connection = ((HttpURLConnection) new URL(url).openConnection()); - connection.setRequestMethod("GET"); - connection.setUseCaches(useCaches); - connection.addRequestProperty("User-Agent", userAgent); - connection.setReadTimeout(timeout); - connection.setConnectTimeout(timeout); - connection.setDoOutput(true); - return connection.getInputStream(); - } - - public static String getString(String url, String userAgent, int timeout, boolean useCaches) { - try (InputStreamReader input = new InputStreamReader(setupConnection(url, userAgent, timeout, useCaches), StandardCharsets.UTF_8)) { - return IOUtils.toString(input); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - public static String getString(String url) { - return getString(url, "OneConfig/1.0.0", 5000, false); - } - - public static JsonElement getJsonElement(String url, String userAgent, int timeout, boolean useCaches) { - return JsonUtils.parseString(getString(url, userAgent, timeout, useCaches)); - } - - public static JsonElement getJsonElement(String url) { - return getJsonElement(url, "OneConfig/1.0.0", 5000, false); - } - - - public static boolean downloadFile(String url, File file) { - return downloadFile(url, file, "OneConfig/1.0.0", 5000, false); - } - - public static boolean downloadFile(String url, File file, String userAgent, int timeout, boolean useCaches) { - url = url.replace(" ", "%20"); - try (FileOutputStream fileOut = new FileOutputStream(file); BufferedInputStream in = new BufferedInputStream(setupConnection(url, userAgent, timeout, useCaches))) { - IOUtils.copy(in, fileOut); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - public static String getFileChecksum(String filename) { - try (FileInputStream inputStream = new FileInputStream(filename)) { - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - byte[] bytesBuffer = new byte[1024]; - int bytesRead; - - while ((bytesRead = inputStream.read(bytesBuffer)) != -1) { - digest.update(bytesBuffer, 0, bytesRead); - } - - return convertByteArrayToHexString(digest.digest()); - } catch (Exception e) { - e.printStackTrace(); - } - return ""; - } - - private static String convertByteArrayToHexString(byte[] arrayBytes) { - StringBuilder stringBuffer = new StringBuilder(); - for (byte arrayByte : arrayBytes) { - stringBuffer.append(Integer.toString((arrayByte & 0xff) + 0x100, 16) - .substring(1)); - } - return stringBuffer.toString(); - } - - public static void browseLink(String uri) { - try { - browseLink(new URI(uri)); - } catch (Exception e) { - e.printStackTrace(); - System.err.println("Invalid URI: " + uri); - } - } - - public static void browseLink(URI uri) { - if(Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { - try { - Desktop.getDesktop().browse(uri); - } catch (IOException e) { - e.printStackTrace(); - System.err.println("Failed to open URL in browser: " + uri); - } - } - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java new file mode 100644 index 0000000..3192a2e --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java @@ -0,0 +1,108 @@ +package cc.polyfrost.oneconfig.utils; + +import com.google.gson.JsonElement; +import org.apache.commons.io.IOUtils; + +import java.awt.*; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; + +public class NetworkUtils { + private static InputStream setupConnection(String url, String userAgent, int timeout, boolean useCaches) throws IOException { + HttpURLConnection connection = ((HttpURLConnection) new URL(url).openConnection()); + connection.setRequestMethod("GET"); + connection.setUseCaches(useCaches); + connection.addRequestProperty("User-Agent", userAgent); + connection.setReadTimeout(timeout); + connection.setConnectTimeout(timeout); + connection.setDoOutput(true); + return connection.getInputStream(); + } + + public static String getString(String url, String userAgent, int timeout, boolean useCaches) { + try (InputStreamReader input = new InputStreamReader(setupConnection(url, userAgent, timeout, useCaches), StandardCharsets.UTF_8)) { + return IOUtils.toString(input); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static String getString(String url) { + return getString(url, "OneConfig/1.0.0", 5000, false); + } + + public static JsonElement getJsonElement(String url, String userAgent, int timeout, boolean useCaches) { + return JsonUtils.parseString(getString(url, userAgent, timeout, useCaches)); + } + + public static JsonElement getJsonElement(String url) { + return getJsonElement(url, "OneConfig/1.0.0", 5000, false); + } + + + public static boolean downloadFile(String url, File file) { + return downloadFile(url, file, "OneConfig/1.0.0", 5000, false); + } + + public static boolean downloadFile(String url, File file, String userAgent, int timeout, boolean useCaches) { + url = url.replace(" ", "%20"); + try (FileOutputStream fileOut = new FileOutputStream(file); BufferedInputStream in = new BufferedInputStream(setupConnection(url, userAgent, timeout, useCaches))) { + IOUtils.copy(in, fileOut); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + public static String getFileChecksum(String filename) { + try (FileInputStream inputStream = new FileInputStream(filename)) { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] bytesBuffer = new byte[1024]; + int bytesRead; + + while ((bytesRead = inputStream.read(bytesBuffer)) != -1) { + digest.update(bytesBuffer, 0, bytesRead); + } + + return convertByteArrayToHexString(digest.digest()); + } catch (Exception e) { + e.printStackTrace(); + } + return ""; + } + + private static String convertByteArrayToHexString(byte[] arrayBytes) { + StringBuilder stringBuffer = new StringBuilder(); + for (byte arrayByte : arrayBytes) { + stringBuffer.append(Integer.toString((arrayByte & 0xff) + 0x100, 16) + .substring(1)); + } + return stringBuffer.toString(); + } + + public static void browseLink(String uri) { + try { + browseLink(new URI(uri)); + } catch (Exception e) { + e.printStackTrace(); + System.err.println("Invalid URI: " + uri); + } + } + + public static void browseLink(URI uri) { + if(Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { + try { + Desktop.getDesktop().browse(uri); + } catch (IOException e) { + e.printStackTrace(); + System.err.println("Failed to open URL in browser: " + uri); + } + } + } +} -- cgit