diff options
author | Jordan Byrne <draknyte1@hotmail.com> | 2018-02-22 13:55:56 +1000 |
---|---|---|
committer | Jordan Byrne <draknyte1@hotmail.com> | 2018-02-22 13:55:56 +1000 |
commit | 24905c16017decae4ee60ce4128b6d26de66baf5 (patch) | |
tree | 503cef5b6b77e04b11feea7563cd5f4ef5ef6942 /src/Java/gtPlusPlus/core/util/sys | |
parent | c5ddbd07991eea29132efbd7f4131ab9a4a977cf (diff) | |
download | GT5-Unofficial-24905c16017decae4ee60ce4128b6d26de66baf5.tar.gz GT5-Unofficial-24905c16017decae4ee60ce4128b6d26de66baf5.tar.bz2 GT5-Unofficial-24905c16017decae4ee60ce4128b6d26de66baf5.zip |
% Minor project cleanup.
Diffstat (limited to 'src/Java/gtPlusPlus/core/util/sys')
-rw-r--r-- | src/Java/gtPlusPlus/core/util/sys/GeoUtils.java | 106 | ||||
-rw-r--r-- | src/Java/gtPlusPlus/core/util/sys/KeyboardUtils.java | 23 | ||||
-rw-r--r-- | src/Java/gtPlusPlus/core/util/sys/Log.java | 24 | ||||
-rw-r--r-- | src/Java/gtPlusPlus/core/util/sys/NetworkUtils.java | 153 | ||||
-rw-r--r-- | src/Java/gtPlusPlus/core/util/sys/SystemUtils.java | 77 |
5 files changed, 383 insertions, 0 deletions
diff --git a/src/Java/gtPlusPlus/core/util/sys/GeoUtils.java b/src/Java/gtPlusPlus/core/util/sys/GeoUtils.java new file mode 100644 index 0000000000..ae3e6242ed --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/sys/GeoUtils.java @@ -0,0 +1,106 @@ +package gtPlusPlus.core.util.sys; + +import java.io.*; +import java.net.*; + +import org.apache.http.client.utils.URIBuilder; + +import gtPlusPlus.api.objects.Logger; + +public class GeoUtils { + + public static String determineUsersCountry(){ + try { + if (NetworkUtils.checkNetworkIsAvailableWithValidInterface()){ + return getUsersCountry(); + } + else { + return "Offline."; + } + } catch (Throwable T){ + Logger.INFO("Failed to initialise GeoUtils."); + return "Failed."; + } + } + + private static String getUsersIPAddress() { + try { + String webPage = "http://checkip.amazonaws.com/"; + URL url = new URL(webPage); + URLConnection urlConnection = url.openConnection(); + InputStream is = urlConnection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + int numCharsRead; + char[] charArray = new char[1024]; + StringBuffer sb = new StringBuffer(); + while ((numCharsRead = isr.read(charArray)) > 0) { + sb.append(charArray, 0, numCharsRead); + } + isr.close(); + String result = sb.toString(); + return result; + } catch (IOException e) {} + return "Error getting users IP."; + } + + private static String getUsersCountry() { + + //Get the IP + String ipAddress = getUsersIPAddress(); + + //Build a URL + URIBuilder builder = new URIBuilder() + .setScheme("http") + .setHost("ipinfo.io") + .setPath("/"+ipAddress+"/country/"); + + URI uri; + try { + //Convert the URI Builder to a URI, then to a URL + uri = builder.build(); + URL url = uri.toURL(); + + //Main Check method + try { + URLConnection urlConnection = url.openConnection(); + InputStream is = urlConnection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + int numCharsRead; + char[] charArray = new char[1024]; + StringBuffer sb = new StringBuffer(); + while ((numCharsRead = isr.read(charArray)) > 0) { + sb.append(charArray, 0, numCharsRead); + } + String temp = sb.toString(); + String result = temp.replaceAll("(\\r|\\n)", ""); + isr.close(); + return result; + //Catch block for bad connection + } catch (IOException e) { + Logger.INFO("Method 1 - Failed."); + } + + //Secondary method + try (java.util.Scanner s = new java.util.Scanner(url.openStream(), "UTF-8").useDelimiter("\\A")) { + String r = s.next(); + return r.replaceAll("(\\r|\\n)", ""); + //Catch block for bad connection + } catch (java.io.IOException e) { + Logger.INFO("Method 2 - Failed."); + } + + } + //Catch block for all the Bad URI/URL building + catch (URISyntaxException | MalformedURLException e1) { + if (e1 instanceof URISyntaxException){ + Logger.INFO("Bad URI Syntax for builder."); + } + else { + Logger.INFO("Malformed URL."); + } + Logger.INFO("Country Check - Failed."); + } + return "Error getting users Country. "+ipAddress; + } + +} diff --git a/src/Java/gtPlusPlus/core/util/sys/KeyboardUtils.java b/src/Java/gtPlusPlus/core/util/sys/KeyboardUtils.java new file mode 100644 index 0000000000..ba834e345e --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/sys/KeyboardUtils.java @@ -0,0 +1,23 @@ +package gtPlusPlus.core.util.sys; + +import org.lwjgl.input.Keyboard; + +import net.minecraft.client.Minecraft; + +public class KeyboardUtils { + + public static boolean isCtrlKeyDown(){ + // prioritize CONTROL, but allow OPTION as well on Mac (note: GuiScreen's isCtrlKeyDown only checks for the OPTION key on Mac) + boolean isCtrlKeyDown = Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL); + if (!isCtrlKeyDown && Minecraft.isRunningOnMac) + isCtrlKeyDown = Keyboard.isKeyDown(Keyboard.KEY_LMETA) || Keyboard.isKeyDown(Keyboard.KEY_RMETA); + + return isCtrlKeyDown; + } + + public static boolean isShiftKeyDown(){ + return Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT); + + } + +} diff --git a/src/Java/gtPlusPlus/core/util/sys/Log.java b/src/Java/gtPlusPlus/core/util/sys/Log.java new file mode 100644 index 0000000000..fcd4f34c1b --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/sys/Log.java @@ -0,0 +1,24 @@ +package gtPlusPlus.core.util.sys; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public final class Log { + public static final Logger LOGGER = LogManager.getLogger("MiscUtils"); + + public static void warn(final String msg) { + LOGGER.warn(msg); + } + + public static void error(final String msg) { + LOGGER.error(msg); + } + + public static void info(final String msg) { + LOGGER.info(msg); + } + + public static void debug(final String msg) { + LOGGER.debug(msg); + } +} diff --git a/src/Java/gtPlusPlus/core/util/sys/NetworkUtils.java b/src/Java/gtPlusPlus/core/util/sys/NetworkUtils.java new file mode 100644 index 0000000000..b6324f18f5 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/sys/NetworkUtils.java @@ -0,0 +1,153 @@ +package gtPlusPlus.core.util.sys; + +import java.io.*; +import java.net.*; +import java.util.Enumeration; + +import gtPlusPlus.api.objects.Logger; + +public class NetworkUtils { + + public static String getContentFromURL(final String args) { + if (checkNetworkIsAvailableWithValidInterface()){ + try { + URL url; + // get URL content + url = new URL(args); + final URLConnection conn = url.openConnection(); + // open the stream and put it into BufferedReader + final BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + String inputLine; + String tempLine = null; + while ((inputLine = br.readLine()) != null) { + tempLine = inputLine; + } + br.close(); + return tempLine; + } + catch (final MalformedURLException e) { + Logger.INFO("Bad URL for Version Check."); + } + catch (final IOException e) { + Logger.INFO("IOException during Version Check."); + } + } + Logger.INFO("Network Not Available during Version Check."); + return "offline"; + } + + public static boolean checkNetworkIsAvailableWithValidInterface(){ + try { + if (hasValidNetworkInterface()){ + if (checkAddressWithTimeout("http://www.google.com", 10) || + checkAddressWithTimeout("http://www.baidu.com", 10) || + checkAddressWithTimeout("https://github.com/draknyte1/GTplusplus", 10) || + checkAddressWithTimeout("www.yahoo.com", 10)/* || + netIsAvailableGoogle() || + netIsAvailableBaidu() || + netIsAvailableGithub() || + netIsAvailableOther()*/){ + return true; + } + else { + Logger.INFO("No sites responded to network connectivity test."); + } + } + else { + Logger.INFO("Network Adapter was not valid."); + } + } + catch (SocketException e) {} + return false; + } + + private static boolean netIsAvailableGoogle() { + try { + final URL url = new URL("http://www.google.com"); + final URLConnection conn = url.openConnection(); + conn.connect(); + return true; + } catch (final MalformedURLException e) { + throw new RuntimeException(e); + } catch (final IOException e) { + return false; + } + } + + private static boolean netIsAvailableBaidu() { + try { + final URL url = new URL("http://www.baidu.com"); + final URLConnection conn = url.openConnection(); + conn.connect(); + return true; + } catch (final MalformedURLException e) { + throw new RuntimeException(e); + } catch (final IOException e) { + return false; + } + } + + private static boolean netIsAvailableGithub() { + try { + final URL url = new URL("https://github.com/draknyte1/GTplusplus"); + final URLConnection conn = url.openConnection(); + conn.connect(); + return true; + } catch (final MalformedURLException e) { + throw new RuntimeException(e); + } catch (final IOException e) { + return false; + } + } + + private static boolean netIsAvailableOther() { + try { + final int timeout = 200; + final InetAddress[] addresses = InetAddress.getAllByName("www.yahoo.com"); + for (final InetAddress address : addresses) { + if (address.isReachable(timeout)) { + return true; + } + return false; + } + } catch (final Exception e) { + return false; + } + return false; + } + + private static boolean checkAddressWithTimeout(String URL, int timeout) { + + try { + InetAddress.getByName(URL).isReachable(3000); //Replace with your name + return true; + } catch (Exception e) { + return false; + } + + /*try { + final InetAddress[] addresses = InetAddress.getAllByName(URL); + for (final InetAddress address : addresses) { + if (address.isReachable(timeout)) { + return true; + } + return false; + } + } catch (final Exception e) { + return false; + } + return false;*/ + } + + private static boolean hasValidNetworkInterface() throws SocketException{ + final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces.hasMoreElements()) { + final NetworkInterface interf = interfaces.nextElement(); + if (interf.isUp() && !interf.isLoopback()) { + return true; + } + } + return false; + } + +} diff --git a/src/Java/gtPlusPlus/core/util/sys/SystemUtils.java b/src/Java/gtPlusPlus/core/util/sys/SystemUtils.java new file mode 100644 index 0000000000..efcfaf8d04 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/sys/SystemUtils.java @@ -0,0 +1,77 @@ +package gtPlusPlus.core.util.sys; + +public class SystemUtils { + + private static OS SystemType; + + public static OS getOS(){ + if (SystemType != null){ + return SystemType; + } + else { + SystemType = getOperatingSystem(); + return SystemType; + } + } + + public static boolean isWindows() { + return (getOSString().indexOf("win") >= 0); + } + + public static boolean isMac() { + return (getOSString().indexOf("mac") >= 0); + } + + public static boolean isUnix() { + return (getOSString().indexOf("nix") >= 0 || getOSString().indexOf("nux") >= 0 || getOSString().indexOf("aix") > 0 ); + } + + public static boolean isSolaris() { + return (getOSString().indexOf("sunos") >= 0); + } + + public static String getOSString(){ + try { + return System.getProperty("os.name").toLowerCase(); + } + catch (Throwable t){ + return "other"; + } + } + + public static OS getOperatingSystem(){ + if (isMac()){ + return OS.MAC; + } + else if (isWindows()){ + return OS.WINDOWS; + } + else if (isUnix()){ + return OS.UNIX; + } + else if (isSolaris()){ + return OS.SOLARIS; + } + else { + return OS.OTHER; + } + } + + public static enum OS { + MAC(1), + WINDOWS(2), + UNIX(3), + SOLARIS(4), + OTHER(0); + + private int mID; + private OS (final int ID){ + this.mID = ID; + } + + public int getID() { + return this.mID; + } + } + +} |