From dd01c90d6938d26f9f07d6968487d09b7b39cc79 Mon Sep 17 00:00:00 2001 From: ArachisH Date: Thu, 17 Jan 2019 21:12:02 -0800 Subject: [PATCH] * Exposed the 'getCacheDir' method in 'Cacher' for grabbing the current running directory of the application. * Utilized the newly exposed static method in Cache, override the toString() method in 'HarbleAPI' for returning the path to the cached habbo messages. * Appened the path/location of the cached messages file to the 'CONNECTIONSTART' packet being sent to the extensions. --- G-Earth/src/main/java/gearth/misc/Cacher.java | 2 +- .../gearth/misc/harble_api/HarbleAPI.java | 47 ++++++++++++------- .../java/gearth/ui/extensions/Extensions.java | 2 + 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/G-Earth/src/main/java/gearth/misc/Cacher.java b/G-Earth/src/main/java/gearth/misc/Cacher.java index 9933bec..3e1ce2d 100644 --- a/G-Earth/src/main/java/gearth/misc/Cacher.java +++ b/G-Earth/src/main/java/gearth/misc/Cacher.java @@ -18,7 +18,7 @@ public class Cacher { private static final String DEFAULT_CACHE_FILENAME = "cache.json"; - private static String getCacheDir() { + public static String getCacheDir() { File GEarthDir = null; try { GEarthDir = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile(); diff --git a/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPI.java b/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPI.java index 29f911d..3546ed7 100644 --- a/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPI.java +++ b/G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPI.java @@ -2,16 +2,13 @@ package gearth.misc.harble_api; import gearth.misc.Cacher; import gearth.protocol.HMessage; -import org.json.JSONArray; import org.json.JSONObject; -import sun.misc.Cache; -import javax.print.attribute.standard.Destination; +import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.Consumer; /** * Created by Jonas on 10/11/2018. @@ -33,24 +30,29 @@ public class HarbleAPI { this.name = (name == null || name.equals("null") ? null : name); this.structure = (structure == null || structure.equals("null") ? null : structure); } + public String getName() { return name; } + public int getHeaderId() { return headerId; } + public HMessage.Side getDestination() { return destination; } + public String getHash() { return hash; } + public String getStructure() { return structure; } public String toString() { - String s = (headerId+": " + "["+hash+"]["+name+"]["+ structure+"]"); + String s = (headerId + ": " + "[" + hash + "][" + name + "][" + structure + "]"); return s; } } @@ -65,9 +67,11 @@ public class HarbleAPI { private Map nameToMessage_outgoing = new HashMap<>(); private boolean success = false; + private String cachedMessagesPath; /** * cache file must be generated first within G-Earth, inb4 20 extensions requesting it at the same time + * * @param hotelversion */ @@ -79,10 +83,12 @@ public class HarbleAPI { return wannabe; } - public HarbleAPI (String hotelversion) { - if (Cacher.cacheFileExists(HarbleAPIFetcher.CACHE_PREFIX + hotelversion)) { - JSONObject object = Cacher.getCacheContents(HarbleAPIFetcher.CACHE_PREFIX + hotelversion); + public HarbleAPI(String hotelversion) { + String possibleCachedMessagesPath = HarbleAPIFetcher.CACHE_PREFIX + hotelversion; + if (Cacher.cacheFileExists(possibleCachedMessagesPath)) { + JSONObject object = Cacher.getCacheContents(possibleCachedMessagesPath); success = true; + cachedMessagesPath = possibleCachedMessagesPath; parse(object); } } @@ -91,8 +97,7 @@ public class HarbleAPI { String name; try { name = object.getString("Name"); - } - catch (Exception e) { + } catch (Exception e) { name = null; } String hash = object.getString("Hash"); @@ -101,8 +106,7 @@ public class HarbleAPI { try { structure = object.getString("Structure"); - } - catch (Exception e) { + } catch (Exception e) { structure = null; } @@ -132,6 +136,7 @@ public class HarbleAPI { nameToMessag.put(message.getName(), message); } } + private void parse(JSONObject object) { try { JSONObject incoming = object.getJSONObject("Incoming"); @@ -142,8 +147,7 @@ public class HarbleAPI { try { JSONObject inMsg = incoming.getJSONObject(key); addMessage(HMessage.Side.TOCLIENT, inMsg, key); - } - catch( Exception e) { + } catch (Exception e) { e.printStackTrace(); } } @@ -151,12 +155,11 @@ public class HarbleAPI { try { JSONObject outMsg = outgoing.getJSONObject(key); addMessage(HMessage.Side.TOSERVER, outMsg, key); + } catch (Exception e) { } - catch( Exception e) {} } } - } - catch (Exception e) { + } catch (Exception e) { success = false; } @@ -170,6 +173,7 @@ public class HarbleAPI { return headerIdToMessage.get(headerId); } + public List getHarbleMessagesFromHash(HMessage.Side side, String hash) { Map> hashToMessage = (side == HMessage.Side.TOSERVER @@ -179,6 +183,7 @@ public class HarbleAPI { List result = hashToMessage.get(hash); return result == null ? new ArrayList<>() : result; } + public HarbleMessage getHarbleMessageFromName(HMessage.Side side, String name) { Map nameToMessage = (side == HMessage.Side.TOSERVER @@ -188,5 +193,11 @@ public class HarbleAPI { return nameToMessage.get(name); } - + @Override + public String toString() { + if (Cacher.cacheFileExists(cachedMessagesPath)) { + return Cacher.getCacheDir() + File.separator + cachedMessagesPath; + } + return super.toString(); + } } diff --git a/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java b/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java index a4e6001..28011ab 100644 --- a/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java +++ b/G-Earth/src/main/java/gearth/ui/extensions/Extensions.java @@ -158,6 +158,7 @@ public class Extensions extends SubForm { .appendString(getHConnection().getDomain()) .appendInt(getHConnection().getPort()) .appendString(getHConnection().getHotelVersion()) + .appendString(HarbleAPIFetcher.HARBLEAPI.toString()) ); } } @@ -294,6 +295,7 @@ public class Extensions extends SubForm { .appendString(getHConnection().getDomain()) .appendInt(getHConnection().getPort()) .appendString(getHConnection().getHotelVersion()) + .appendString(HarbleAPIFetcher.HARBLEAPI.toString()) ); }