add hostinfo to extension protocol

This commit is contained in:
sirjonasxx 2022-02-16 02:33:34 +01:00
parent c294b92d77
commit 6f787f6441
12 changed files with 168 additions and 25 deletions

View File

@ -4,6 +4,7 @@ import gearth.misc.AdminValidator;
import gearth.misc.Cacher; import gearth.misc.Cacher;
import gearth.misc.UpdateChecker; import gearth.misc.UpdateChecker;
import gearth.misc.listenerpattern.Observable; import gearth.misc.listenerpattern.Observable;
import gearth.misc.listenerpattern.ObservableObject;
import gearth.ui.GEarthController; import gearth.ui.GEarthController;
import gearth.ui.subforms.logger.loggerdisplays.PacketLogger; import gearth.ui.subforms.logger.loggerdisplays.PacketLogger;
import gearth.ui.themes.Theme; import gearth.ui.themes.Theme;
@ -27,19 +28,17 @@ public class GEarth extends Application {
public static GEarth main; public static GEarth main;
public static String version = "1.5.1"; public static String version = "1.5.1";
public static String gitApi = "https://api.github.com/repos/sirjonasxx/G-Earth/releases/latest"; public static String gitApi = "https://api.github.com/repos/sirjonasxx/G-Earth/releases/latest";
public static Theme theme; public static ObservableObject<Theme> observableTheme;
public static Observable<Consumer<Theme>> themeObservable = new Observable<>();
private Stage stage; private Stage stage;
private GEarthController controller; private GEarthController controller;
static { static {
if (Cacher.getCacheContents().has("theme")) { observableTheme = new ObservableObject<>(
theme = ThemeFactory.themeForTitle(Cacher.getCacheContents().getString("theme")); Cacher.getCacheContents().has("theme") ?
} ThemeFactory.themeForTitle(Cacher.getCacheContents().getString("theme")) :
else { ThemeFactory.getDefaultTheme()
theme = ThemeFactory.getDefaultTheme(); );
}
} }
@Override @Override
@ -87,13 +86,13 @@ public class GEarth extends Application {
@Override @Override
public Theme getCurrentTheme() { public Theme getCurrentTheme() {
return theme; return observableTheme.getObject();
} }
}); });
primaryStage.setResizable(false); primaryStage.setResizable(false);
primaryStage.sizeToScene(); primaryStage.sizeToScene();
setGearthTheme(theme); setGearthTheme(observableTheme.getObject());
primaryStage.show(); primaryStage.show();
primaryStage.setOnCloseRequest(event -> closeGEarth()); primaryStage.setOnCloseRequest(event -> closeGEarth());
@ -111,8 +110,7 @@ public class GEarth extends Application {
private void setGearthTheme(Theme theme) { private void setGearthTheme(Theme theme) {
Cacher.put("theme", theme.title()); Cacher.put("theme", theme.title());
themeObservable.fireEvent(t -> t.accept(theme)); observableTheme.setObject(theme);
GEarth.theme = theme;
Theme defaultTheme = ThemeFactory.getDefaultTheme(); Theme defaultTheme = ThemeFactory.getDefaultTheme();
// Platform.runLater(() -> { // Platform.runLater(() -> {
@ -161,7 +159,11 @@ public class GEarth extends Application {
return null; return null;
} }
public static Observable<Consumer<Theme>> getThemeObservable() { public static ObservableObject<Theme> getThemeObservable() {
return themeObservable; return observableTheme;
}
public static Theme getTheme() {
return observableTheme.getObject();
} }
} }

View File

@ -1,5 +1,6 @@
package gearth.extensions; package gearth.extensions;
import gearth.misc.HostInfo;
import gearth.services.packet_info.PacketInfoManager; import gearth.services.packet_info.PacketInfoManager;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
@ -159,6 +160,8 @@ public abstract class Extension extends ExtensionBase {
} }
else if (packet.headerId() == NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.INIT) { else if (packet.headerId() == NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.INIT) {
delayed_init = packet.readBoolean(); delayed_init = packet.readBoolean();
HostInfo hostInfo = HostInfo.fromPacket(packet);
updateHostInfo(hostInfo);
if (!delayed_init) { if (!delayed_init) {
initExtension(); initExtension();
} }
@ -177,7 +180,10 @@ public abstract class Extension extends ExtensionBase {
response.appendLongString(habboMessage.stringify()); response.appendLongString(habboMessage.stringify());
writeToStream(response.toBytes()); writeToStream(response.toBytes());
}
else if (packet.headerId() == NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.UPDATEHOSTINFO) {
HostInfo hostInfo = HostInfo.fromPacket(packet);
updateHostInfo(hostInfo);
} }
} }

View File

@ -1,10 +1,13 @@
package gearth.extensions; package gearth.extensions;
import gearth.misc.HostInfo;
import gearth.misc.listenerpattern.Observable; import gearth.misc.listenerpattern.Observable;
import gearth.misc.listenerpattern.ObservableObject;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.services.packet_info.PacketInfo; import gearth.services.packet_info.PacketInfo;
import gearth.services.packet_info.PacketInfoManager; import gearth.services.packet_info.PacketInfoManager;
import javafx.beans.property.ObjectProperty;
import org.reactfx.util.Lists; import org.reactfx.util.Lists;
import java.util.*; import java.util.*;
@ -30,6 +33,11 @@ public abstract class ExtensionBase extends IExtension {
volatile PacketInfoManager packetInfoManager = PacketInfoManager.EMPTY; volatile PacketInfoManager packetInfoManager = PacketInfoManager.EMPTY;
protected ObservableObject<HostInfo> observableHostInfo = new ObservableObject<>(null);
void updateHostInfo(HostInfo hostInfo) {
observableHostInfo.setObject(hostInfo);
}
/** /**
* Register a listener on a specific packet Type * Register a listener on a specific packet Type
@ -187,4 +195,8 @@ public abstract class ExtensionBase extends IExtension {
public PacketInfoManager getPacketInfoManager() { public PacketInfoManager getPacketInfoManager() {
return packetInfoManager; return packetInfoManager;
} }
public HostInfo getHostInfo() {
return observableHostInfo.getObject();
}
} }

View File

@ -1,6 +1,7 @@
package gearth.extensions; package gearth.extensions;
import gearth.misc.HostInfo;
import gearth.services.packet_info.PacketInfoManager; import gearth.services.packet_info.PacketInfoManager;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
@ -118,9 +119,11 @@ public class InternalExtensionBuilder extends GEarthExtension {
extension.onEndConnection(); extension.onEndConnection();
} }
@Override @Override
public void init(boolean isConnected) { public void init(boolean isConnected, HostInfo hostInfo) {
extension.initExtension(); extension.initExtension();
extension.updateHostInfo(hostInfo);
} // not implementing isConnected, only relevant for g-python } // not implementing isConnected, only relevant for g-python
@Override @Override
@ -128,6 +131,11 @@ public class InternalExtensionBuilder extends GEarthExtension {
// no need in internal ext // no need in internal ext
} }
@Override
public void updateHostInfo(HostInfo hostInfo) {
extension.updateHostInfo(hostInfo);
}
@Override @Override
public void packetToStringResponse(String string, String expression) { public void packetToStringResponse(String string, String expression) {
// no need in java ext // no need in java ext

View File

@ -0,0 +1,53 @@
package gearth.misc;
import gearth.protocol.HPacket;
import java.util.HashMap;
public class HostInfo {
private final String packetlogger;
private final String version;
private final HashMap<String, String> attributes;
public HostInfo(String packetlogger, String version, HashMap<String, String> attributes) {
this.packetlogger = packetlogger;
this.version = version;
this.attributes = attributes;
}
public static HostInfo fromPacket(HPacket packet) {
String packetlogger = packet.readString();
String version = packet.readString();
int attributeCount = packet.readInteger();
HashMap<String, String> attributes = new HashMap<>();
for (int i = 0; i < attributeCount; i++) {
String key = packet.readString();
String value = packet.readString();
attributes.put(key, value);
}
return new HostInfo(packetlogger, version, attributes);
}
public void appendToPacket(HPacket packet) {
packet.appendString(packetlogger);
packet.appendString(version);
packet.appendInt(attributes.size());
attributes.keySet().forEach(k -> {
packet.appendString(k);
packet.appendString(attributes.get(k));
});
}
public String getPacketlogger() {
return packetlogger;
}
public String getVersion() {
return version;
}
public HashMap<String, String> getAttributes() {
return attributes;
}
}

View File

@ -0,0 +1,22 @@
package gearth.misc.listenerpattern;
import java.util.function.Consumer;
public class ObservableObject<T> extends Observable<Consumer<T>> {
private T object;
public ObservableObject(T object) {
super();
this.object = object;
}
public void setObject(T object) {
this.object = object;
fireEvent(c -> c.accept(object));
}
public T getObject() {
return object;
}
}

View File

@ -1,6 +1,7 @@
package gearth.services.extension_handler; package gearth.services.extension_handler;
import gearth.GEarth; import gearth.GEarth;
import gearth.misc.HostInfo;
import gearth.misc.listenerpattern.Observable; import gearth.misc.listenerpattern.Observable;
import gearth.protocol.HConnection; import gearth.protocol.HConnection;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
@ -11,10 +12,12 @@ import gearth.services.extension_handler.extensions.GEarthExtension;
import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer;
import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerFactory; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerFactory;
import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducerObserver;
import gearth.ui.themes.Theme;
import javafx.util.Pair; import javafx.util.Pair;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.function.Consumer;
public class ExtensionHandler { public class ExtensionHandler {
@ -44,6 +47,14 @@ public class ExtensionHandler {
} }
private void initialize() { private void initialize() {
GEarth.getThemeObservable().addListener(theme -> {
synchronized (gEarthExtensions) {
for (GEarthExtension extension : gEarthExtensions) {
extension.updateHostInfo(getHostInfo());
}
}
});
hConnection.getStateObservable().addListener((oldState, newState) -> { hConnection.getStateObservable().addListener((oldState, newState) -> {
if (newState == HState.CONNECTED) { if (newState == HState.CONNECTED) {
synchronized (gEarthExtensions) { synchronized (gEarthExtensions) {
@ -241,7 +252,7 @@ public class ExtensionHandler {
extension.getClickedObservable().addListener(extension::doubleclick); extension.getClickedObservable().addListener(extension::doubleclick);
observable.fireEvent(l -> l.onExtensionConnect(extension)); observable.fireEvent(l -> l.onExtensionConnect(extension));
extension.init(hConnection.getState() == HState.CONNECTED); extension.init(hConnection.getState() == HState.CONNECTED, getHostInfo());
if (hConnection.getState() == HState.CONNECTED) { if (hConnection.getState() == HState.CONNECTED) {
extension.connectionStart( extension.connectionStart(
hConnection.getDomain(), hConnection.getDomain(),
@ -256,6 +267,16 @@ public class ExtensionHandler {
}; };
} }
private HostInfo getHostInfo() {
HashMap<String, String> attributes = new HashMap<>();
attributes.put("theme", GEarth.getTheme().title());
return new HostInfo(
"G-Earth",
GEarth.version,
attributes
);
}
public List<ExtensionProducer> getExtensionProducers() { public List<ExtensionProducer> getExtensionProducers() {
return extensionProducers; return extensionProducers;
} }

View File

@ -1,5 +1,6 @@
package gearth.services.extension_handler.extensions; package gearth.services.extension_handler.extensions;
import gearth.misc.HostInfo;
import gearth.misc.listenerpattern.Observable; import gearth.misc.listenerpattern.Observable;
import gearth.misc.listenerpattern.SynchronizedObservable; import gearth.misc.listenerpattern.SynchronizedObservable;
import gearth.services.packet_info.PacketInfoManager; import gearth.services.packet_info.PacketInfoManager;
@ -40,8 +41,9 @@ public abstract class GEarthExtension {
public abstract void provideFlags(String[] flags); public abstract void provideFlags(String[] flags);
public abstract void connectionStart(String host, int port, String hotelVersion, String clientIdentifier, HClient clientType, PacketInfoManager packetInfoManager); public abstract void connectionStart(String host, int port, String hotelVersion, String clientIdentifier, HClient clientType, PacketInfoManager packetInfoManager);
public abstract void connectionEnd(); public abstract void connectionEnd();
public abstract void init(boolean isConnected); public abstract void init(boolean isConnected, HostInfo hostInfo);
public abstract void close(); public abstract void close();
public abstract void updateHostInfo(HostInfo hostInfo);
public abstract void packetToStringResponse(String string, String expression); public abstract void packetToStringResponse(String string, String expression);
public abstract void stringToPacketResponse(HPacket packet); public abstract void stringToPacketResponse(HPacket packet);
// --------------------------------------------------------------- // ---------------------------------------------------------------

View File

@ -1,5 +1,6 @@
package gearth.services.extension_handler.extensions.implementations.network; package gearth.services.extension_handler.extensions.implementations.network;
import gearth.misc.HostInfo;
import gearth.services.packet_info.PacketInfoManager; import gearth.services.packet_info.PacketInfoManager;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.connection.HClient; import gearth.protocol.connection.HClient;
@ -213,10 +214,12 @@ public class NetworkExtension extends GEarthExtension {
} }
@Override @Override
public void init(boolean isConnected) { public void init(boolean isConnected, HostInfo hostInfo) {
sendMessage( HPacket initPacket = new HPacket(NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.INIT);
new HPacket(NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.INIT, isConnected) initPacket.appendBoolean(isConnected);
); hostInfo.appendToPacket(initPacket);
sendMessage(initPacket);
} }
@Override @Override
@ -226,6 +229,13 @@ public class NetworkExtension extends GEarthExtension {
} catch (IOException ignored) { } } catch (IOException ignored) { }
} }
@Override
public void updateHostInfo(HostInfo hostInfo) {
HPacket packet = new HPacket(NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.UPDATEHOSTINFO);
hostInfo.appendToPacket(packet);
sendMessage(packet);
}
@Override @Override
public void packetToStringResponse(String string, String expression) { public void packetToStringResponse(String string, String expression) {
HPacket packet = new HPacket(NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.PACKETTOSTRING_RESPONSE); HPacket packet = new HPacket(NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.PACKETTOSTRING_RESPONSE);

View File

@ -88,6 +88,8 @@ public class NetworkExtensionInfo {
public static final int CONNECTIONEND = 6; public static final int CONNECTIONEND = 6;
public static final int INIT = 7; public static final int INIT = 7;
public static final int UPDATEHOSTINFO = 10;
public static final int PACKETTOSTRING_RESPONSE = 20; public static final int PACKETTOSTRING_RESPONSE = 20;
public static final int STRINGTOPACKET_RESPONSE = 21; public static final int STRINGTOPACKET_RESPONSE = 21;
} }

View File

@ -1,5 +1,6 @@
package gearth.services.extension_handler.extensions.implementations.simple; package gearth.services.extension_handler.extensions.implementations.simple;
import gearth.misc.HostInfo;
import gearth.services.packet_info.PacketInfoManager; import gearth.services.packet_info.PacketInfoManager;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
@ -96,8 +97,9 @@ public class ExampleExtension extends GEarthExtension {
// the habbo connection has ended // the habbo connection has ended
} }
@Override @Override
public void init(boolean isConnected) { public void init(boolean isConnected, HostInfo hostInfo) {
System.out.println("Example extension is connected to G-Earth"); System.out.println("Example extension is connected to G-Earth");
// the extension is now connected with G-Earth // the extension is now connected with G-Earth
} }
@ -110,7 +112,10 @@ public class ExampleExtension extends GEarthExtension {
hasClosed(); hasClosed();
} }
@Override
public void updateHostInfo(HostInfo hostInfo) {
}
// ignore these // ignore these

View File

@ -9,8 +9,8 @@ import java.util.function.Consumer;
public class GEarthThemedTitleBarConfig extends DefaultTitleBarConfig { public class GEarthThemedTitleBarConfig extends DefaultTitleBarConfig {
public GEarthThemedTitleBarConfig(Stage stage) { public GEarthThemedTitleBarConfig(Stage stage) {
super(stage, GEarth.theme); super(stage, GEarth.getTheme());
GEarth.themeObservable.addListener(this::setTheme); GEarth.getThemeObservable().addListener(this::setTheme);
} }
@Override @Override