diff options
author | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-05-25 21:21:42 +0700 |
---|---|---|
committer | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-05-25 21:21:42 +0700 |
commit | 60b9f99d260361824424b3f3c866e95f4869c262 (patch) | |
tree | d9fe06f8af0839fd7b5a9538d776a40be70719fc /src | |
parent | 368ba3ba0859e00ec245a64c94c1eab1f96d23bd (diff) | |
download | OneConfig-60b9f99d260361824424b3f3c866e95f4869c262.tar.gz OneConfig-60b9f99d260361824424b3f3c866e95f4869c262.tar.bz2 OneConfig-60b9f99d260361824424b3f3c866e95f4869c262.zip |
OC-33 OC-35, additions to ColorUtils, and JsonUtils
Diffstat (limited to 'src')
5 files changed, 240 insertions, 78 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/network/SSLStore.java b/src/main/java/cc/polyfrost/oneconfig/network/SSLStore.java deleted file mode 100644 index 3b8eed2..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/network/SSLStore.java +++ /dev/null @@ -1,76 +0,0 @@ -package cc.polyfrost.oneconfig.network; - -import javax.net.ssl.KeyManager; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManagerFactory; -import java.io.BufferedInputStream; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.security.KeyStore; -import java.security.SecureRandom; -import java.security.cert.Certificate; -import java.security.cert.CertificateFactory; - -/** - * Adds our certificate to the JavaKeyStore to avoid SSL issues. - */ -public class SSLStore { - private final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); - private final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); - - public SSLStore() throws Exception { - Path keyStorePath = Paths.get(System.getProperty("java.home"), "lib", "security", "cacerts"); - this.keyStore.load(Files.newInputStream(keyStorePath),(char[])null); - } - - /** - * Loads the specified SSL certificate. - * @param sslFile A .der filename from the resources/assets/oneconfig/ssl directory. - * @throws Exception Uses Exception to cover the SSL loading and generation - */ - public SSLStore load(String sslFile) throws Exception { - InputStream certificateResource = SSLStore.class.getResourceAsStream("/assets/oneconfig/ssl/" + sslFile + ".der"); - Throwable sslThrowable = null; - - // Try to gen and load the certificate - try { - InputStream certStream = new BufferedInputStream(certificateResource); - Certificate generatedCertificate = this.certificateFactory.generateCertificate(certStream); - - this.keyStore.setCertificateEntry(sslFile, generatedCertificate); - } catch (Throwable sslException) { - sslThrowable = sslException; - throw sslException; - } finally { - if (certificateResource != null) { - try { - certificateResource.close(); - } catch (Throwable closeException) { - sslThrowable.addSuppressed(closeException); - } - } else { - certificateResource.close(); - } - } - return this; - } - - /** - * Generates and returns the SSLContext after the new cert has been added with SSLStore.load(). - * @return The SSLContext generated after init. - * @throws Exception Uses Exception to cover the TMF init and SSLContext init. - */ - public SSLContext finish() throws Exception { - // Initialize TrustManagerFactory with the new KeyStore once the new cert has been added - TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); - trustManagerFactory.init(this.keyStore); - - // Return the SSLContext after init. - SSLContext sslContext = SSLContext.getInstance("TLS"); - sslContext.init((KeyManager[])null, trustManagerFactory.getTrustManagers(), (SecureRandom)null); - - return sslContext; - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java index ae5753a..0d8ae6d 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java @@ -4,6 +4,9 @@ import cc.polyfrost.oneconfig.config.OneConfigConfig; import java.awt.*; +/** + * A class to help with color manipulation. + */ public class ColorUtils { public static int getColor(int currentColor, int colorPalette, boolean hover, boolean click) { @@ -56,7 +59,7 @@ public class ColorUtils { } private static float[] splitColor(int color) { - return new float[]{(color >> 16 & 255) / 255f, (color >> 8 & 255) / 255f, (color & 255) / 255f, (color >> 24 & 255) / 255f}; + return new float[]{getRed(color) / 255f, getGreen(color) / 255f, getBlue(color) / 255f, getAlpha(color) / 255f}; } private static int getColorComponents(float[] currentColor, float[] initColor, float[] finalColor, boolean hover, float speed) { @@ -84,7 +87,114 @@ public class ColorUtils { return current; } + /** + * Get the red component of an RGB color. + * @param color the color. + * @return the red component. + */ + public static int getRed(int color) { + return (color >> 16) & 0xFF; + } + + /** + * Get the green component of an RGB color. + * @param color the color. + * @return the green component. + */ + public static int getGreen(int color) { + return (color >> 8) & 0xFF; + } + + /** + * Get the blue component of an RGB color. + * @param color the color. + * @return the blue component. + */ + public static int getBlue(int color) { + return color & 0xFF; + } + + /** + * Get the alpha component of an ARGB color. + * @param color the color. + * @return the alpha component. + */ + public static int getAlpha(int color) { + return (color >> 24) & 0xFF; + } + + /** + * Get the RGB color from the given color components. + * @param red the red component. + * @param green the green component. + * @param blue the blue component. + * @param alpha the alpha component. + * @return the RGB color. + */ + public static int getColor(float red, float green, float blue, float alpha) { + return getColor((int) red * 255, (int) green * 255, (int) blue * 255, (int) alpha * 255); + } + + /** + * Get the RGB color from the given color components. + * @param red the red component. + * @param green the green component. + * @param blue the blue component. + * @return the RGB color. + */ + public static int getColor(int red, int green, int blue) { + return getColor(red, green, blue, 255); + } + + /** + * Get the RGB color from the given color components. + * @param red the red component. + * @param green the green component. + * @param blue the blue component. + * @param alpha the alpha component. + * @return the RGB color. + */ + public static int getColor(int red, int green, int blue, int alpha) { + return (alpha << 24) | (red << 16) | (green << 8) | blue; + } + + /** + * Return the color with the given red component. + * @param color the color. + * @param red the red component. + * @return the color with the given red component. + */ + public static int setRed(int color, int red) { + return (color & 0x00FFFF) | (red << 16); + } + + /** + * Return the color with the given green component. + * @param color the color. + * @param green the green component. + * @return the color with the given green component. + */ + public static int setGreen(int color, int green) { + return (color & 0xFF00FF) | (green << 8); + } + + /** + * Return the color with the given blue component. + * @param color the color. + * @param blue the blue component. + * @return the color with the given blue component. + */ + public static int setBlue(int color, int blue) { + return (color & 0xFFFF00) | blue; + } + + /** + * Return the color with the given alpha component. + * @param color the color. + * @param alpha the alpha component. + * @return the color with the given alpha component. + */ public static int setAlpha(int color, int alpha) { - return ( alpha << 24 ) | ( color & 0x00ffffff ); + return (color & 0xFFFFFF) | (alpha << 24); } } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java new file mode 100644 index 0000000..53f8d54 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java @@ -0,0 +1,18 @@ +package cc.polyfrost.oneconfig.utils; + +import cc.polyfrost.oneconfig.libs.universal.UScreen; +import net.minecraft.client.gui.GuiScreen; + +/** + * A class containing utility methods for working with GuiScreens. + */ +public class GuiUtils { + + /** + * Displays a screen after a tick, preventing mouse sync issues. + * @param screen the screen to display. + */ + public static void displayScreen(GuiScreen screen) { + new TickDelay(() -> UScreen.displayScreen(screen), 1); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java index aa13c96..db83825 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java @@ -1,4 +1,86 @@ package cc.polyfrost.oneconfig.utils; +import com.google.gson.JsonElement; +import org.apache.commons.io.IOUtils; + +import java.io.*; +import java.net.HttpURLConnection; +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(); + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java new file mode 100644 index 0000000..f450d3b --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java @@ -0,0 +1,28 @@ +package cc.polyfrost.oneconfig.utils; + +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; + +public class JsonUtils { + private static final JsonParser PARSER = new JsonParser(); + + public static JsonParser getParser() { + return PARSER; + } + + public static JsonElement parseString(String string, boolean catchExceptions) { + try { + return PARSER.parse(string); + } catch (Exception e) { + if (catchExceptions) { + return null; + } else { + throw e; + } + } + } + + public static JsonElement parseString(String string) { + return parseString(string, true); + } +} |