concurrency fixes & introducing extension annotations

This commit is contained in:
sirjonasxx 2018-09-25 18:58:58 +02:00
parent 2b3a2caa06
commit b864228553
7 changed files with 108 additions and 91 deletions

View File

@ -17,7 +17,6 @@ public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(GEarthController.class.getResource("G-Earth.fxml"));
Parent root = loader.load();
@ -29,7 +28,6 @@ public class Main extends Application {
primaryStage.setTitle("G-Earth");
primaryStage.setScene(new Scene(root, 620, 295));
primaryStage.show();
primaryStage.getScene().getStylesheets().add(GEarthController.class.getResource("bootstrap3.css").toExternalForm());
primaryStage.setOnCloseRequest( event -> {

View File

@ -5,11 +5,12 @@ import main.protocol.HPacket;
import main.ui.extensions.Extensions;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.AnnotatedType;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* Created by Jonas on 23/06/18.
@ -27,8 +28,8 @@ public abstract class Extension {
private static final String[] PORT_FLAG = {"--port", "-p"};
private OutputStream out = null;
private Map<Integer, List<MessageListener>> incomingMessageListeners = new HashMap<>();
private Map<Integer, List<MessageListener>> outgoingMessageListeners = new HashMap<>();
private final Map<Integer, List<MessageListener>> incomingMessageListeners = new HashMap<>();
private final Map<Integer, List<MessageListener>> outgoingMessageListeners = new HashMap<>();
private FlagsCheckListener flagRequestCallback = null;
@ -38,6 +39,19 @@ public abstract class Extension {
*/
public Extension(String[] args) {
//obtain port
if (getInfoAnnotations() == null) {
System.err.println("Extension info not found\n\n" +
"Usage:\n" +
"@ExtensionInfo ( \n" +
" Title = \"...\",\n" +
" Description = \"...\",\n" +
" Version = \"...\",\n" +
" Author = \"...\"" +
"\n)");
return;
}
int port = 0;
outerloop:
@ -52,8 +66,7 @@ public abstract class Extension {
Socket gEarthExtensionServer = null;
try {
gEarthExtensionServer = new Socket("localhost", port);
gEarthExtensionServer = new Socket("127.0.0.2", port);
InputStream in = gEarthExtensionServer.getInputStream();
DataInputStream dIn = new DataInputStream(in);
out = gEarthExtensionServer.getOutputStream();
@ -82,11 +95,13 @@ public abstract class Extension {
if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST) {
ExtensionInfo info = getInfoAnnotations();
HPacket response = new HPacket(Extensions.INCOMING_MESSAGES_IDS.EXTENSIONINFO);
response.appendString(getTitle())
.appendString(getAuthor())
.appendString(getVersion())
.appendString(getDescription())
response.appendString(info.Title())
.appendString(info.Author())
.appendString(info.Version())
.appendString(info.Description())
.appendBoolean(isOnClickMethodUsed());
writeToStream(response.toBytes());
}
@ -127,19 +142,29 @@ public abstract class Extension {
incomingMessageListeners :
outgoingMessageListeners;
if (listeners.containsKey(-1)) { // registered on all packets
for (int i = listeners.get(-1).size() - 1; i >= 0; i--) {
listeners.get(-1).get(i).act(habboMessage);
habboMessage.getPacket().setReadIndex(6);
Set<MessageListener> correctListeners = new HashSet<>();
synchronized (incomingMessageListeners) {
synchronized (outgoingMessageListeners) {
if (listeners.containsKey(-1)) { // registered on all packets
for (int i = listeners.get(-1).size() - 1; i >= 0; i--) {
correctListeners.add(listeners.get(-1).get(i));
}
}
if (listeners.containsKey(habboPacket.headerId())) {
for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) {
correctListeners.add(listeners.get(habboPacket.headerId()).get(i));
}
}
}
}
if (listeners.containsKey(habboPacket.headerId())) {
for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) {
listeners.get(habboPacket.headerId()).get(i).act(habboMessage);
habboMessage.getPacket().setReadIndex(6);
}
for(MessageListener listener : correctListeners) {
habboMessage.getPacket().setReadIndex(6);
listener.act(habboMessage);
}
habboMessage.getPacket().setReadIndex(6);
HPacket response = new HPacket(Extensions.INCOMING_MESSAGES_IDS.MANIPULATEDPACKET);
response.appendLongString(habboMessage.stringify());
@ -148,10 +173,9 @@ public abstract class Extension {
}
}
System.out.println("Extension closed");
} catch (IOException | ArrayIndexOutOfBoundsException e) {
System.err.println("Connection failed; is G-Earth open?");
System.err.println("Connection failed; is G-Earth active?");
// e.printStackTrace();
}
finally {
@ -213,10 +237,13 @@ public abstract class Extension {
incomingMessageListeners :
outgoingMessageListeners;
if (!listeners.containsKey(headerId)) {
listeners.put(headerId, new ArrayList<>());
synchronized (listeners) {
if (!listeners.containsKey(headerId)) {
listeners.put(headerId, new ArrayList<>());
}
}
listeners.get(headerId).add(messageListener);
}
@ -296,8 +323,8 @@ public abstract class Extension {
*/
protected void onEndConnection(){}
protected abstract String getTitle();
protected abstract String getDescription();
protected abstract String getVersion();
protected abstract String getAuthor();
ExtensionInfo getInfoAnnotations() {
return getClass().getAnnotation(ExtensionInfo.class);
}
}

View File

@ -32,28 +32,11 @@ public abstract class ExtensionForm extends Application {
Platform.runLater(primaryStage::hide);
});
ExtensionForm thiss = this;
ExtensionInfo extInfo = getClass().getAnnotation(ExtensionInfo.class);
Thread t = new Thread(() -> {
extension = new Extension(args) {
@Override
protected String getTitle() {
return thiss.getTitle();
}
@Override
protected String getDescription() {
return thiss.getDescription();
}
@Override
protected String getVersion() {
return thiss.getVersion();
}
@Override
protected String getAuthor() {
return thiss.getAuthor();
}
@Override
protected void init() {
thiss.initExtension();
@ -73,8 +56,15 @@ public abstract class ExtensionForm extends Application {
protected void onEndConnection() {
thiss.onEndConnection();
}
@Override
ExtensionInfo getInfoAnnotations() {
return extInfo;
}
};
Platform.runLater(primaryStage::close);
// Platform.runLater(primaryStage::close);
//when the extension has ended, close this process
Platform.exit();
});
t.start();
}
@ -125,10 +115,4 @@ public abstract class ExtensionForm extends Application {
* A connection with Habbo has ended
*/
protected void onEndConnection(){}
protected abstract String getTitle();
protected abstract String getDescription();
protected abstract String getVersion();
protected abstract String getAuthor();
}

View File

@ -0,0 +1,15 @@
package main.extensions;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Created by Jonas on 25/09/18.
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ExtensionInfo {
String Title();
String Description();
String Version();
String Author();
}

View File

@ -1,15 +1,24 @@
package main.extensions.examples.adminonconnect;
import main.extensions.Extension;
import main.extensions.ExtensionInfo;
import main.protocol.HMessage;
import main.protocol.HPacket;
/**
* Created by Jonas on 26/06/18.
*/
public class AdminOnConnect extends Extension {
@ExtensionInfo(
Title = "Always admin!",
Description = "Gives you admin permission on connect",
Version = "1.0",
Author = "sirjonasxx"
)
public class AdminOnConnect extends Extension {
public static void main(String[] args) {
new AdminOnConnect(args);
}
@ -39,22 +48,4 @@ public class AdminOnConnect extends Extension {
protected void onStartConnection() {
done = false;
}
// @Override
// protected void onClick() {
// System.out.println("clicked");
// }
protected String getTitle() {
return "Always admin!";
}
protected String getDescription() {
return "Gives you admin permission on connect";
}
protected String getVersion() {
return "1.0";
}
protected String getAuthor() {
return "sirjonasxx";
}
}

View File

@ -5,6 +5,7 @@ import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import main.extensions.ExtensionForm;
import main.extensions.ExtensionInfo;
import main.ui.GEarthController;
import java.net.URL;
@ -12,6 +13,14 @@ import java.net.URL;
/**
* Created by Jonas on 22/09/18.
*/
@ExtensionInfo(
Title = "iManipulate",
Description = "Block &/ replace packets",
Version = "0.1",
Author = "sirjonasxx"
)
public class BlockAndReplacePackets extends ExtensionForm {
public static void main(String[] args) {
@ -32,17 +41,4 @@ public class BlockAndReplacePackets extends ExtensionForm {
primaryStage.setTitle("Packet blocker and replacer");
primaryStage.setScene(new Scene(root, 565, 262));
}
protected String getTitle() {
return "iManipulate";
}
protected String getDescription() {
return "Block &/ replace packets";
}
protected String getVersion() {
return "0.1";
}
protected String getAuthor() {
return "sirjonasxx";
}
}

View File

@ -1,6 +1,7 @@
package main.extensions.examples.speechcolorizer;
import main.extensions.Extension;
import main.extensions.ExtensionInfo;
import main.protocol.HMessage;
import main.protocol.HPacket;
@ -15,7 +16,12 @@ import java.util.Random;
*
*/
@ExtensionInfo(
Title = "Colorize me!",
Description = "Because we like to be weird",
Version = "1.0",
Author = "sirjonasxx"
)
public class SpeechColorizer extends Extension {
public static void main(String[] args) {