ok extension support works

This commit is contained in:
sirjonasxx 2018-06-26 20:14:39 +02:00
parent 83d3de6c6f
commit 2c80e2f2c9
7 changed files with 86 additions and 16 deletions

View File

@ -70,6 +70,7 @@ public abstract class Extension {
byte[] headerandbody = new byte[length + 4]; byte[] headerandbody = new byte[length + 4];
int amountRead = 0; int amountRead = 0;
while (amountRead < length) { while (amountRead < length) {
amountRead += dIn.read(headerandbody, 4 + amountRead, Math.min(dIn.available(), length - amountRead)); amountRead += dIn.read(headerandbody, 4 + amountRead, Math.min(dIn.available(), length - amountRead));
} }
@ -118,10 +119,6 @@ public abstract class Extension {
HMessage habboMessage = new HMessage(stringifiedMessage); HMessage habboMessage = new HMessage(stringifiedMessage);
HPacket habboPacket = habboMessage.getPacket(); HPacket habboPacket = habboMessage.getPacket();
lastwrapper = packet;
lastM = habboMessage;
last = habboPacket;
Map<Integer, List<MessageListener>> listeners = Map<Integer, List<MessageListener>> listeners =
habboMessage.getDestination() == HMessage.Side.TOCLIENT ? habboMessage.getDestination() == HMessage.Side.TOCLIENT ?
incomingMessageListeners : incomingMessageListeners :
@ -130,12 +127,14 @@ public abstract class Extension {
if (listeners.containsKey(-1)) { // registered on all packets if (listeners.containsKey(-1)) { // registered on all packets
for (int i = listeners.get(-1).size() - 1; i >= 0; i--) { for (int i = listeners.get(-1).size() - 1; i >= 0; i--) {
listeners.get(-1).get(i).act(habboMessage); listeners.get(-1).get(i).act(habboMessage);
habboMessage.getPacket().setReadIndex(6);
} }
} }
if (listeners.containsKey(habboPacket.headerId())) { if (listeners.containsKey(habboPacket.headerId())) {
for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) { for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) {
listeners.get(habboPacket.headerId()).get(i).act(habboMessage); listeners.get(habboPacket.headerId()).get(i).act(habboMessage);
habboMessage.getPacket().setReadIndex(6);
} }
} }
@ -143,6 +142,7 @@ public abstract class Extension {
response.appendLongString(habboMessage.stringify()); response.appendLongString(habboMessage.stringify());
writeToStream(response.toBytes()); writeToStream(response.toBytes());
} }
} }
@ -162,7 +162,7 @@ public abstract class Extension {
} }
private void writeToStream(byte[] bytes) throws IOException { private void writeToStream(byte[] bytes) throws IOException {
synchronized (out) { synchronized (this) {
out.write(bytes); out.write(bytes);
} }
} }

View File

@ -1,5 +1,8 @@
package main.extensions; package main.extensions;
import main.protocol.HMessage;
import main.protocol.HPacket;
/** /**
* Created by Jonas on 24/06/18. * Created by Jonas on 24/06/18.
*/ */
@ -16,6 +19,22 @@ public class SimpleTestExtension extends Extension {
@Override @Override
protected void init() { protected void init() {
System.out.println("init"); System.out.println("init");
intercept(HMessage.Side.TOSERVER, 1926, this::onSendMessage);
}
private void onSendMessage(HMessage message) {
HPacket packet = message.getPacket();
String watchasaid = packet.readString();
System.out.println("you said: " + watchasaid);
if (watchasaid.equals("blocked")) {
message.setBlocked(true);
}
packet.replaceString(6, "@cyan@" + watchasaid);
} }
@Override @Override

View File

@ -57,7 +57,7 @@ public class HMessage implements StringifyAble {
@Override @Override
public void constructFromString(String str) { public void constructFromString(String str) {
String[] parts = str.split("\t"); String[] parts = str.split("\t", 4);
this.isBlocked = parts[0].equals("1"); this.isBlocked = parts[0].equals("1");
this.index = Integer.parseInt(parts[1]); this.index = Integer.parseInt(parts[1]);
this.side = parts[2].equals("TOCLIENT") ? Side.TOCLIENT : Side.TOSERVER; this.side = parts[2].equals("TOCLIENT") ? Side.TOCLIENT : Side.TOSERVER;

View File

@ -780,14 +780,15 @@ public class HPacket implements StringifyAble {
@Override @Override
public String stringify() { public String stringify() {
String st = (isEdited ? "1" : "0") + this.toString(); String st = null;
st = (isEdited ? "1" : "0") + new String(packetInBytes, StandardCharsets.ISO_8859_1);
return st; return st;
} }
@Override @Override
public void constructFromString(String str) { public void constructFromString(String str) {
this.isEdited = str.charAt(0) == '1'; this.isEdited = str.charAt(0) == '1';
packetInBytes = fromStringToBytes(str.substring(1)); packetInBytes = str.substring(1).getBytes(StandardCharsets.ISO_8859_1);
} }
@Override @Override
@ -797,4 +798,36 @@ public class HPacket implements StringifyAble {
HPacket packet2 = (HPacket) object; HPacket packet2 = (HPacket) object;
return Arrays.equals(packetInBytes, packet2.packetInBytes) && (isEdited == packet2.isEdited); return Arrays.equals(packetInBytes, packet2.packetInBytes) && (isEdited == packet2.isEdited);
} }
public static void main(String[] args) {
// HPacket packet = new HPacket("{l}{u:500}{i:4}{s:heey}{b:false}");
// System.out.println(packet);
//
// String stringified = packet.stringify();
// System.out.println("stringified: " + stringified);
// System.out.println(stringified.length());
//
//
// HPacket packet1 = new HPacket(new byte[0]);
// packet1.constructFromString(stringified);
//
// System.out.println(packet1);
// System.out.println(packet.equals(packet1));
HPacket packet = new HPacket(555);
for (int i = -128; i < 128; i++) {
packet.appendByte((byte)i);
}
System.out.println(packet);
String stringified = packet.stringify();
System.out.println(stringified.length());
HPacket packet1 = new HPacket(new byte[0]);
packet1.constructFromString(stringified);
System.out.println(packet1);
System.out.println(packet.equals(packet1));
}
} }

View File

@ -148,14 +148,14 @@ public class Extensions extends SubForm {
@Override @Override
public void act(HPacket packet) { public void act(HPacket packet) {
if (packet.headerId() == INCOMING_MESSAGES_IDS.MANIPULATEDPACKET) { if (packet.headerId() == INCOMING_MESSAGES_IDS.MANIPULATEDPACKET) {
String stringifiedresponse = packet.readLongString(); String stringifiedresponse = packet.readLongString(6);
HMessage responseMessage = new HMessage(stringifiedresponse); HMessage responseMessage = new HMessage(stringifiedresponse);
if (responseMessage.getDestination() == message.getDestination() && responseMessage.getIndex() == message.getIndex()) { if (responseMessage.getDestination() == message.getDestination() && responseMessage.getIndex() == message.getIndex()) {
if (!message.equals(responseMessage)) { if (!message.equals(responseMessage)) {
message.constructFromString(stringifiedresponse); message.constructFromString(stringifiedresponse);
if (responseMessage.isBlocked()) { }
isblock[0] = true; if (responseMessage.isBlocked()) {
} isblock[0] = true;
} }
synchronized (collection) { synchronized (collection) {
collection.remove(extension); collection.remove(extension);

View File

@ -30,7 +30,7 @@ public class GEarthExtension {
new Thread(() -> { new Thread(() -> {
try { try {
synchronized (connection.getOutputStream()) { synchronized (connection) {
connection.getOutputStream().write((new HPacket(Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST)).toBytes()); connection.getOutputStream().write((new HPacket(Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST)).toBytes());
} }
@ -96,6 +96,7 @@ public class GEarthExtension {
for (int i = receiveMessageListeners.size() - 1; i >= 0; i--) { for (int i = receiveMessageListeners.size() - 1; i >= 0; i--) {
receiveMessageListeners.get(i).act(packet); receiveMessageListeners.get(i).act(packet);
packet.setReadIndex(6);
} }
} }
@ -150,7 +151,7 @@ public class GEarthExtension {
public boolean sendMessage(HPacket message) { public boolean sendMessage(HPacket message) {
try { try {
synchronized (connection.getOutputStream()) { synchronized (this) {
connection.getOutputStream().write(message.toBytes()); connection.getOutputStream().write(message.toBytes());
} }
return true; return true;

View File

@ -9,11 +9,19 @@ import java.net.Socket;
*/ */
public class GEarthExtensionsRegistrer { public class GEarthExtensionsRegistrer {
final ServerSocket serverSocket; private ServerSocket serverSocket;
GEarthExtensionsRegistrer(ExtensionRegisterObserver observer) throws IOException { GEarthExtensionsRegistrer(ExtensionRegisterObserver observer) throws IOException {
serverSocket = new ServerSocket(0); // serverSocket = new ServerSocket(0);
int port = 9092;
boolean serverSetup = false;
while (!serverSetup) {
serverSetup = createServer(port);
port++;
}
new Thread(() -> { new Thread(() -> {
try { try {
while (!serverSocket.isClosed()) { while (!serverSocket.isClosed()) {
@ -24,6 +32,15 @@ public class GEarthExtensionsRegistrer {
}).start(); }).start();
} }
private boolean createServer(int port) {
try {
serverSocket = new ServerSocket(port);
return true;
} catch (IOException e) {
return false;
}
}
public int getPort() { public int getPort() {
return serverSocket.getLocalPort(); return serverSocket.getLocalPort();
} }