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.
*/
public abstract class Extension {
public abstract class Extension implements IExtension{
public interface MessageListener {
void act(HMessage message);
@ -232,7 +232,7 @@ public abstract class Extension {
* @param packet packet to be sent
* @return success or failure
*/
protected boolean sendToClient(HPacket packet) {
public boolean sendToClient(HPacket packet) {
return send(packet, HMessage.Side.TOCLIENT);
}
@ -241,7 +241,7 @@ public abstract class Extension {
* @param packet packet to be sent
* @return success or failure
*/
protected boolean sendToServer(HPacket packet) {
public boolean sendToServer(HPacket packet) {
return send(packet, HMessage.Side.TOSERVER);
}
private boolean send(HPacket packet, HMessage.Side side) {
@ -263,7 +263,7 @@ public abstract class Extension {
* @param headerId the packet header ID
* @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 =
side == HMessage.Side.TOCLIENT ?
incomingMessageListeners :
@ -284,7 +284,7 @@ public abstract class Extension {
* @param side ToClient or ToServer
* @param messageListener the callback
*/
protected void intercept(HMessage.Side side, MessageListener messageListener) {
public void intercept(HMessage.Side side, MessageListener messageListener) {
intercept(side, -1, messageListener);
}
@ -294,7 +294,7 @@ public abstract class Extension {
* @param flagRequestCallback callback
* @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;
this.flagRequestCallback = flagRequestCallback;
return true;
@ -304,7 +304,7 @@ public abstract class Extension {
* Write to the console in G-Earth
* @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);
packet.appendString(s);
try {
@ -372,7 +372,7 @@ public abstract class Extension {
void act(String host, int port, String hotelversion);
}
private List<OnConnectionListener> onConnectionListeners = new ArrayList<>();
protected void onConnect(OnConnectionListener listener){
public void onConnect(OnConnectionListener listener){
onConnectionListeners.add(listener);
}
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.
*/
public abstract class ExtensionForm {
public abstract class ExtensionForm implements IExtension{
volatile Extension extension;
volatile Stage primaryStage;
@ -25,25 +25,25 @@ public abstract class ExtensionForm {
public abstract ExtensionForm launchForm(Stage primaryStage) throws Exception;
//wrap extension methods
protected boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){
public boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback){
return extension.requestFlags(flagRequestCallback);
}
protected void writeToConsole(String s) {
public void writeToConsole(String 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);
}
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);
}
protected boolean sendToServer(HPacket packet){
public boolean sendToServer(HPacket packet){
return extension.sendToServer(packet);
}
protected boolean sendToClient(HPacket packet){
public boolean sendToClient(HPacket packet){
return extension.sendToClient(packet);
}
protected void onConnect(Extension.OnConnectionListener listener) {
public void onConnect(Extension.OnConnectionListener 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;
import gearth.extensions.Extension;
import gearth.extensions.IExtension;
import gearth.misc.harble_api.HarbleAPI;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
@ -21,37 +22,31 @@ public class HashSupport {
private HarbleAPI harbleAPI = new HarbleAPI(""); //empty
private Map<String, List<Extension.MessageListener>> incomingMessageListeners = new HashMap<>();
private Map<String, List<Extension.MessageListener>> outgoingMessageListeners = new HashMap<>();
private PacketSender toClientSender;
private PacketSender toServerSender;
private IExtension extension;
public interface OnConnectRegistration {
void onConnect(Extension.OnConnectionListener listener);
}
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) -> {
public HashSupport(IExtension extension) {
this.extension = extension;
extension.onConnect((host, port, hotelversion) -> {
// synchronized (lock) {
harbleAPI = new HarbleAPI(hotelversion);
// }
});
interceptRegistration.intercept(HMessage.Side.TOSERVER, message -> {
extension.intercept(HMessage.Side.TOSERVER, message -> {
// synchronized (lock) {
HarbleAPI.HarbleMessage haMessage = harbleAPI.getHarbleMessageFromHeaderId(HMessage.Side.TOSERVER, message.getPacket().headerId());
if (haMessage != null) {
String hash = haMessage.getHash();
List<Extension.MessageListener> listeners = outgoingMessageListeners.get(hash);
if (listeners != null) {
for (Extension.MessageListener listener : listeners) {
List<Extension.MessageListener> listeners_hash = outgoingMessageListeners.get(haMessage.getHash());
List<Extension.MessageListener> listeners_name = outgoingMessageListeners.get(haMessage.getName());
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);
message.getPacket().resetReadIndex();
}
@ -59,14 +54,21 @@ public class HashSupport {
}
// }
});
interceptRegistration.intercept(HMessage.Side.TOCLIENT, message -> {
extension.intercept(HMessage.Side.TOCLIENT, message -> {
// synchronized (lock) {
HarbleAPI.HarbleMessage haMessage = harbleAPI.getHarbleMessageFromHeaderId(HMessage.Side.TOCLIENT, message.getPacket().headerId());
if (haMessage != null) {
String hash = haMessage.getHash();
List<Extension.MessageListener> listeners = incomingMessageListeners.get(hash);
if (listeners != null) {
for (Extension.MessageListener listener : listeners) {
List<Extension.MessageListener> listeners_hash = incomingMessageListeners.get(haMessage.getHash());
List<Extension.MessageListener> listeners_name = incomingMessageListeners.get(haMessage.getName());
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);
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 =
(side == HMessage.Side.TOSERVER
? outgoingMessageListeners
: incomingMessageListeners);
messageListeners.computeIfAbsent(hash, k -> new ArrayList<>());
messageListeners.get(hash).add(messageListener);
messageListeners.computeIfAbsent(hashOrName, k -> new ArrayList<>());
messageListeners.get(hashOrName).add(messageListener);
}
/**
*
* @return if no errors occurred (invalid hash/invalid parameters/connection error)
*/
public boolean sendToClient(String hash, Object... objects) {
List<HarbleAPI.HarbleMessage> possibilities = harbleAPI.getHarbleMessagesFromHash(HMessage.Side.TOCLIENT, hash);
if (possibilities.size() == 0) return false;
int headerId = possibilities.get(0).getHeaderId();
private boolean send(HMessage.Side side, String hashOrName, Object... objects) {
int headerId;
HarbleAPI.HarbleMessage fromname = harbleAPI.getHarbleMessageFromName(side, hashOrName);
if (fromname != null) {
headerId = fromname.getHeaderId();
}
else {
List<HarbleAPI.HarbleMessage> possibilities = harbleAPI.getHarbleMessagesFromHash(side, hashOrName);
if (possibilities.size() == 0) return false;
headerId = possibilities.get(0).getHeaderId();
}
try {
HPacket packetToSend = new HPacket(headerId, objects);
return toClientSender.send(packetToSend);
return (side == HMessage.Side.TOCLIENT
? extension.sendToClient(packetToSend)
: extension.sendToServer(packetToSend));
}
catch (InvalidParameterException e) {
return false;
@ -109,19 +116,16 @@ public class HashSupport {
*
* @return if no errors occurred (invalid hash/invalid parameters/connection error)
*/
public boolean sendToServer(String hash, Object... objects) {
List<HarbleAPI.HarbleMessage> possibilities = harbleAPI.getHarbleMessagesFromHash(HMessage.Side.TOSERVER, hash);
if (possibilities.size() == 0) return false;
int headerId = possibilities.get(0).getHeaderId();
public boolean sendToClient(String hashOrName, Object... objects) {
return send(HMessage.Side.TOCLIENT, hashOrName, objects);
}
try {
HPacket packetToSend = new HPacket(headerId, objects);
return toServerSender.send(packetToSend);
}
catch (InvalidParameterException e) {
return false;
}
/**
*
* @return if no errors occurred (invalid hash/invalid parameters/connection error)
*/
public boolean sendToServer(String hashOrName, Object... objects) {
return send(HMessage.Side.TOSERVER, hashOrName, objects);
}
}