change things

This commit is contained in:
sirjonasxx 2018-11-17 15:13:24 +01:00
parent 2486d5a902
commit d14b21af62
4 changed files with 88 additions and 65 deletions

View File

@ -14,7 +14,7 @@ import java.util.Map;
/** /**
* Created by Jonas on 23/06/18. * Created by Jonas on 23/06/18.
*/ */
public abstract class Extension { public abstract class Extension implements IExtension{
public interface MessageListener { public interface MessageListener {
void act(HMessage message); void act(HMessage message);
@ -232,7 +232,7 @@ public abstract class Extension {
* @param packet packet to be sent * @param packet packet to be sent
* @return success or failure * @return success or failure
*/ */
protected boolean sendToClient(HPacket packet) { public boolean sendToClient(HPacket packet) {
return send(packet, HMessage.Side.TOCLIENT); return send(packet, HMessage.Side.TOCLIENT);
} }
@ -241,7 +241,7 @@ public abstract class Extension {
* @param packet packet to be sent * @param packet packet to be sent
* @return success or failure * @return success or failure
*/ */
protected boolean sendToServer(HPacket packet) { public boolean sendToServer(HPacket packet) {
return send(packet, HMessage.Side.TOSERVER); return send(packet, HMessage.Side.TOSERVER);
} }
private boolean send(HPacket packet, HMessage.Side side) { private boolean send(HPacket packet, HMessage.Side side) {
@ -263,7 +263,7 @@ public abstract class Extension {
* @param headerId the packet header ID * @param headerId the packet header ID
* @param messageListener the callback * @param messageListener the callback
*/ */
protected void intercept(HMessage.Side side, int headerId, MessageListener messageListener) { public void intercept(HMessage.Side side, int headerId, MessageListener messageListener) {
Map<Integer, List<MessageListener>> listeners = Map<Integer, List<MessageListener>> listeners =
side == HMessage.Side.TOCLIENT ? side == HMessage.Side.TOCLIENT ?
incomingMessageListeners : incomingMessageListeners :
@ -284,7 +284,7 @@ public abstract class Extension {
* @param side ToClient or ToServer * @param side ToClient or ToServer
* @param messageListener the callback * @param messageListener the callback
*/ */
protected void intercept(HMessage.Side side, MessageListener messageListener) { public void intercept(HMessage.Side side, MessageListener messageListener) {
intercept(side, -1, messageListener); intercept(side, -1, messageListener);
} }
@ -294,7 +294,7 @@ public abstract class Extension {
* @param flagRequestCallback callback * @param flagRequestCallback callback
* @return if the request was successful, will return false if another flagrequest is busy * @return if the request was successful, will return false if another flagrequest is busy
*/ */
protected boolean requestFlags(FlagsCheckListener flagRequestCallback) { public boolean requestFlags(FlagsCheckListener flagRequestCallback) {
if (this.flagRequestCallback != null) return false; if (this.flagRequestCallback != null) return false;
this.flagRequestCallback = flagRequestCallback; this.flagRequestCallback = flagRequestCallback;
return true; return true;
@ -304,7 +304,7 @@ public abstract class Extension {
* Write to the console in G-Earth * Write to the console in G-Earth
* @param s the text to be written * @param s the text to be written
*/ */
protected void writeToConsole(String s) { public void writeToConsole(String s) {
HPacket packet = new HPacket(Extensions.INCOMING_MESSAGES_IDS.EXTENSIONCONSOLELOG); HPacket packet = new HPacket(Extensions.INCOMING_MESSAGES_IDS.EXTENSIONCONSOLELOG);
packet.appendString(s); packet.appendString(s);
try { try {
@ -372,7 +372,7 @@ public abstract class Extension {
void act(String host, int port, String hotelversion); void act(String host, int port, String hotelversion);
} }
private List<OnConnectionListener> onConnectionListeners = new ArrayList<>(); private List<OnConnectionListener> onConnectionListeners = new ArrayList<>();
protected void onConnect(OnConnectionListener listener){ public void onConnect(OnConnectionListener listener){
onConnectionListeners.add(listener); onConnectionListeners.add(listener);
} }
private void notifyConnectionListeners(String host, int port, String hotelversion) { private void notifyConnectionListeners(String host, int port, String hotelversion) {

View File

@ -11,7 +11,7 @@ import java.util.concurrent.Semaphore;
/** /**
* Created by Jonas on 22/09/18. * Created by Jonas on 22/09/18.
*/ */
public abstract class ExtensionForm { public abstract class ExtensionForm implements IExtension{
volatile Extension extension; volatile Extension extension;
volatile Stage primaryStage; volatile Stage primaryStage;
@ -25,25 +25,25 @@ public abstract class ExtensionForm {
public abstract ExtensionForm launchForm(Stage primaryStage) throws Exception; public abstract ExtensionForm launchForm(Stage primaryStage) throws Exception;
//wrap extension methods //wrap extension methods
protected boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){ public boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){
return extension.requestFlags(flagRequestCallback); return extension.requestFlags(flagRequestCallback);
} }
protected void writeToConsole(String s) { public void writeToConsole(String s) {
extension.writeToConsole(s); extension.writeToConsole(s);
} }
protected void intercept(HMessage.Side side, Extension.MessageListener messageListener) { public void intercept(HMessage.Side side, Extension.MessageListener messageListener) {
extension.intercept(side, messageListener); extension.intercept(side, messageListener);
} }
protected void intercept(HMessage.Side side, int headerId, Extension.MessageListener messageListener){ public void intercept(HMessage.Side side, int headerId, Extension.MessageListener messageListener){
extension.intercept(side, headerId, messageListener); extension.intercept(side, headerId, messageListener);
} }
protected boolean sendToServer(HPacket packet){ public boolean sendToServer(HPacket packet){
return extension.sendToServer(packet); return extension.sendToServer(packet);
} }
protected boolean sendToClient(HPacket packet){ public boolean sendToClient(HPacket packet){
return extension.sendToClient(packet); return extension.sendToClient(packet);
} }
protected void onConnect(Extension.OnConnectionListener listener) { public void onConnect(Extension.OnConnectionListener listener) {
extension.onConnect(listener); extension.onConnect(listener);
} }

View File

@ -0,0 +1,19 @@
package gearth.extensions;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
/**
* Created by Jeunez on 17/11/2018.
*/
public interface IExtension {
boolean sendToClient(HPacket packet);
boolean sendToServer(HPacket packet);
void intercept(HMessage.Side side, int headerId, Extension.MessageListener messageListener);
void intercept(HMessage.Side side, Extension.MessageListener messageListener);
boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback);
void writeToConsole(String s); // not implemented in g-earth yet
void onConnect(Extension.OnConnectionListener listener);
}

View File

@ -1,6 +1,7 @@
package gearth.extensions.extra.hashing; package gearth.extensions.extra.hashing;
import gearth.extensions.Extension; import gearth.extensions.Extension;
import gearth.extensions.IExtension;
import gearth.misc.harble_api.HarbleAPI; import gearth.misc.harble_api.HarbleAPI;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
@ -21,37 +22,31 @@ public class HashSupport {
private HarbleAPI harbleAPI = new HarbleAPI(""); //empty private HarbleAPI harbleAPI = new HarbleAPI(""); //empty
private Map<String, List<Extension.MessageListener>> incomingMessageListeners = new HashMap<>(); private Map<String, List<Extension.MessageListener>> incomingMessageListeners = new HashMap<>();
private Map<String, List<Extension.MessageListener>> outgoingMessageListeners = new HashMap<>(); private Map<String, List<Extension.MessageListener>> outgoingMessageListeners = new HashMap<>();
private PacketSender toClientSender;
private PacketSender toServerSender;
private IExtension extension;
public interface OnConnectRegistration { public HashSupport(IExtension extension) {
void onConnect(Extension.OnConnectionListener listener); this.extension = extension;
} extension.onConnect((host, port, hotelversion) -> {
public interface InterceptRegistration {
void intercept(HMessage.Side side, Extension.MessageListener messageListener);
}
public interface PacketSender {
boolean send(HPacket packet);
}
public HashSupport(OnConnectRegistration onConnectRegistration, InterceptRegistration interceptRegistration, PacketSender sendToClient, PacketSender sendToServer) {
toClientSender = sendToClient;
toServerSender = sendToServer;
onConnectRegistration.onConnect((host, port, hotelversion) -> {
// synchronized (lock) { // synchronized (lock) {
harbleAPI = new HarbleAPI(hotelversion); harbleAPI = new HarbleAPI(hotelversion);
// } // }
}); });
interceptRegistration.intercept(HMessage.Side.TOSERVER, message -> { extension.intercept(HMessage.Side.TOSERVER, message -> {
// synchronized (lock) { // synchronized (lock) {
HarbleAPI.HarbleMessage haMessage = harbleAPI.getHarbleMessageFromHeaderId(HMessage.Side.TOSERVER, message.getPacket().headerId()); HarbleAPI.HarbleMessage haMessage = harbleAPI.getHarbleMessageFromHeaderId(HMessage.Side.TOSERVER, message.getPacket().headerId());
if (haMessage != null) { if (haMessage != null) {
String hash = haMessage.getHash(); List<Extension.MessageListener> listeners_hash = outgoingMessageListeners.get(haMessage.getHash());
List<Extension.MessageListener> listeners = outgoingMessageListeners.get(hash); List<Extension.MessageListener> listeners_name = outgoingMessageListeners.get(haMessage.getName());
if (listeners != null) { if (listeners_hash != null) {
for (Extension.MessageListener listener : listeners) { for (Extension.MessageListener listener : listeners_hash) {
listener.act(message);
message.getPacket().resetReadIndex();
}
}
if (listeners_name != null) {
for (Extension.MessageListener listener : listeners_name) {
listener.act(message); listener.act(message);
message.getPacket().resetReadIndex(); message.getPacket().resetReadIndex();
} }
@ -59,14 +54,21 @@ public class HashSupport {
} }
// } // }
}); });
interceptRegistration.intercept(HMessage.Side.TOCLIENT, message -> { extension.intercept(HMessage.Side.TOCLIENT, message -> {
// synchronized (lock) { // synchronized (lock) {
HarbleAPI.HarbleMessage haMessage = harbleAPI.getHarbleMessageFromHeaderId(HMessage.Side.TOCLIENT, message.getPacket().headerId()); HarbleAPI.HarbleMessage haMessage = harbleAPI.getHarbleMessageFromHeaderId(HMessage.Side.TOCLIENT, message.getPacket().headerId());
if (haMessage != null) { if (haMessage != null) {
String hash = haMessage.getHash(); String hash = haMessage.getHash();
List<Extension.MessageListener> listeners = incomingMessageListeners.get(hash); List<Extension.MessageListener> listeners_hash = incomingMessageListeners.get(haMessage.getHash());
if (listeners != null) { List<Extension.MessageListener> listeners_name = incomingMessageListeners.get(haMessage.getName());
for (Extension.MessageListener listener : listeners) { if (listeners_hash != null) {
for (Extension.MessageListener listener : listeners_hash) {
listener.act(message);
message.getPacket().resetReadIndex();
}
}
if (listeners_name != null) {
for (Extension.MessageListener listener : listeners_name) {
listener.act(message); listener.act(message);
message.getPacket().resetReadIndex(); message.getPacket().resetReadIndex();
} }
@ -76,29 +78,34 @@ public class HashSupport {
}); });
} }
public void intercept(HMessage.Side side, String hash, Extension.MessageListener messageListener) { public void intercept(HMessage.Side side, String hashOrName, Extension.MessageListener messageListener) {
Map<String, List<Extension.MessageListener>> messageListeners = Map<String, List<Extension.MessageListener>> messageListeners =
(side == HMessage.Side.TOSERVER (side == HMessage.Side.TOSERVER
? outgoingMessageListeners ? outgoingMessageListeners
: incomingMessageListeners); : incomingMessageListeners);
messageListeners.computeIfAbsent(hash, k -> new ArrayList<>()); messageListeners.computeIfAbsent(hashOrName, k -> new ArrayList<>());
messageListeners.get(hash).add(messageListener); messageListeners.get(hashOrName).add(messageListener);
} }
/** private boolean send(HMessage.Side side, String hashOrName, Object... objects) {
* int headerId;
* @return if no errors occurred (invalid hash/invalid parameters/connection error) HarbleAPI.HarbleMessage fromname = harbleAPI.getHarbleMessageFromName(side, hashOrName);
*/ if (fromname != null) {
public boolean sendToClient(String hash, Object... objects) { headerId = fromname.getHeaderId();
List<HarbleAPI.HarbleMessage> possibilities = harbleAPI.getHarbleMessagesFromHash(HMessage.Side.TOCLIENT, hash); }
if (possibilities.size() == 0) return false; else {
int headerId = possibilities.get(0).getHeaderId(); List<HarbleAPI.HarbleMessage> possibilities = harbleAPI.getHarbleMessagesFromHash(side, hashOrName);
if (possibilities.size() == 0) return false;
headerId = possibilities.get(0).getHeaderId();
}
try { try {
HPacket packetToSend = new HPacket(headerId, objects); HPacket packetToSend = new HPacket(headerId, objects);
return toClientSender.send(packetToSend); return (side == HMessage.Side.TOCLIENT
? extension.sendToClient(packetToSend)
: extension.sendToServer(packetToSend));
} }
catch (InvalidParameterException e) { catch (InvalidParameterException e) {
return false; return false;
@ -109,19 +116,16 @@ public class HashSupport {
* *
* @return if no errors occurred (invalid hash/invalid parameters/connection error) * @return if no errors occurred (invalid hash/invalid parameters/connection error)
*/ */
public boolean sendToServer(String hash, Object... objects) { public boolean sendToClient(String hashOrName, Object... objects) {
List<HarbleAPI.HarbleMessage> possibilities = harbleAPI.getHarbleMessagesFromHash(HMessage.Side.TOSERVER, hash); return send(HMessage.Side.TOCLIENT, hashOrName, objects);
if (possibilities.size() == 0) return false; }
int headerId = possibilities.get(0).getHeaderId();
try { /**
HPacket packetToSend = new HPacket(headerId, objects); *
* @return if no errors occurred (invalid hash/invalid parameters/connection error)
return toServerSender.send(packetToSend); */
} public boolean sendToServer(String hashOrName, Object... objects) {
catch (InvalidParameterException e) { return send(HMessage.Side.TOSERVER, hashOrName, objects);
return false;
}
} }
} }