mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2024-11-23 00:40:51 +01:00
Merge pull request #114 from WiredSpast/master
Added GAsync tool and fixed 2 small bugs in parsers
This commit is contained in:
commit
c814b4ab4d
@ -0,0 +1,84 @@
|
||||
package gearth.extensions.extra.tools;
|
||||
|
||||
import gearth.protocol.HMessage;
|
||||
import gearth.protocol.HPacket;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class AwaitingPacket {
|
||||
private final String headerName;
|
||||
private final HMessage.Direction direction;
|
||||
private HPacket packet;
|
||||
private boolean received = false;
|
||||
private final ArrayList<Predicate<HPacket>> conditions = new ArrayList<>();
|
||||
private final long start;
|
||||
private long minWait = 0;
|
||||
|
||||
public AwaitingPacket(String headerName, HMessage.Direction direction, long maxWaitingTimeMillis) {
|
||||
if (maxWaitingTimeMillis < 30) {
|
||||
maxWaitingTimeMillis = 30;
|
||||
}
|
||||
|
||||
AwaitingPacket self = this;
|
||||
new Timer().schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
self.received = true;
|
||||
}
|
||||
}, maxWaitingTimeMillis);
|
||||
|
||||
this.start = System.currentTimeMillis();
|
||||
|
||||
this.direction = direction;
|
||||
this.headerName = headerName;
|
||||
}
|
||||
|
||||
public String getHeaderName() {
|
||||
return this.headerName;
|
||||
}
|
||||
|
||||
public HMessage.Direction getDirection() {
|
||||
return this.direction;
|
||||
}
|
||||
|
||||
public AwaitingPacket setMinWaitingTime(long millis) {
|
||||
this.minWait = millis;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final AwaitingPacket addConditions(Predicate<HPacket>... conditions) {
|
||||
this.conditions.addAll(Arrays.asList(conditions));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
protected void setPacket(HPacket packet) {
|
||||
this.packet = packet;
|
||||
this.received = true;
|
||||
}
|
||||
|
||||
protected HPacket getPacket() {
|
||||
return this.packet;
|
||||
}
|
||||
|
||||
protected boolean test(HMessage hMessage) {
|
||||
for (Predicate<HPacket> condition : this.conditions) {
|
||||
hMessage.getPacket().resetReadIndex();
|
||||
if(!condition.test(hMessage.getPacket())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected boolean isReady() {
|
||||
return this.received && this.start + this.minWait < System.currentTimeMillis();
|
||||
}
|
||||
}
|
106
G-Earth/src/main/java/gearth/extensions/extra/tools/GAsync.java
Normal file
106
G-Earth/src/main/java/gearth/extensions/extra/tools/GAsync.java
Normal file
@ -0,0 +1,106 @@
|
||||
package gearth.extensions.extra.tools;
|
||||
|
||||
import gearth.extensions.ExtensionBase;
|
||||
import gearth.protocol.HMessage;
|
||||
import gearth.protocol.HPacket;
|
||||
import gearth.services.packet_info.PacketInfo;
|
||||
import gearth.services.packet_info.PacketInfoManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class GAsync {
|
||||
private PacketInfoManager packetInfoManager;
|
||||
private final ArrayList<AwaitingPacket> awaitingPackets = new ArrayList<>();
|
||||
|
||||
public GAsync(ExtensionBase ext) {
|
||||
this.packetInfoManager = ext.getPacketInfoManager();
|
||||
ext.onConnect((host, port, hotelversion, clientIdentifier, clientType) ->
|
||||
this.packetInfoManager = ext.getPacketInfoManager()
|
||||
);
|
||||
|
||||
ext.intercept(HMessage.Direction.TOSERVER, this::onMessageToServer);
|
||||
ext.intercept(HMessage.Direction.TOCLIENT, this::onMessageToClient);
|
||||
}
|
||||
|
||||
private void onMessageToClient(HMessage hMessage) {
|
||||
if (packetInfoManager != null) {
|
||||
List<PacketInfo> packetInfoList = packetInfoManager.getAllPacketInfoFromHeaderId(HMessage.Direction.TOCLIENT, hMessage.getPacket().headerId());
|
||||
for (PacketInfo packetInfo : packetInfoList) {
|
||||
synchronized (awaitingPackets) {
|
||||
awaitingPackets.stream()
|
||||
.filter(p -> p.getDirection().equals(HMessage.Direction.TOCLIENT))
|
||||
.filter(p -> p.getHeaderName().equals(packetInfo.getName()))
|
||||
.filter(p -> p.test(hMessage))
|
||||
.forEach(p -> p.setPacket(hMessage.getPacket()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void onMessageToServer(HMessage hMessage) {
|
||||
if (packetInfoManager != null) {
|
||||
List<PacketInfo> packetInfoList = packetInfoManager.getAllPacketInfoFromHeaderId(HMessage.Direction.TOSERVER, hMessage.getPacket().headerId());
|
||||
for (PacketInfo packetInfo : packetInfoList) {
|
||||
synchronized (awaitingPackets) {
|
||||
awaitingPackets.stream()
|
||||
.filter(p -> p.getDirection().equals(HMessage.Direction.TOSERVER))
|
||||
.filter(p -> p.getHeaderName().equals(packetInfo.getName()))
|
||||
.filter(p -> p.test(hMessage))
|
||||
.forEach(p -> p.setPacket(hMessage.getPacket()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HPacket awaitPacket(AwaitingPacket ...packets) {
|
||||
synchronized (awaitingPackets) {
|
||||
awaitingPackets.addAll(Arrays.asList(packets));
|
||||
}
|
||||
|
||||
while (true) {
|
||||
for (AwaitingPacket packet : packets) {
|
||||
if (packet.isReady()) {
|
||||
synchronized (awaitingPackets) {
|
||||
awaitingPackets.removeAll(Arrays.asList(packets));
|
||||
}
|
||||
return packet.getPacket();
|
||||
}
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
public HPacket[] awaitMultiplePackets(AwaitingPacket ...packets) {
|
||||
synchronized (awaitingPackets) {
|
||||
awaitingPackets.addAll(Arrays.asList(packets));
|
||||
}
|
||||
|
||||
while (true) {
|
||||
if (Arrays.stream(packets).allMatch(AwaitingPacket::isReady)) {
|
||||
synchronized (awaitingPackets) {
|
||||
awaitingPackets.removeAll(Arrays.asList(packets));
|
||||
}
|
||||
return Arrays.stream(packets)
|
||||
.map(AwaitingPacket::getPacket)
|
||||
.toArray(HPacket[]::new);
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
synchronized (awaitingPackets) {
|
||||
awaitingPackets.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private void sleep(int millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -105,7 +105,7 @@ public class HFriend {
|
||||
packet.readString();
|
||||
}
|
||||
|
||||
int friendCount = packet.getReadIndex();
|
||||
int friendCount = packet.readInteger();
|
||||
List<Integer> removedIds = new ArrayList<>();
|
||||
for(int i = 0; i < friendCount; i++) {
|
||||
if(packet.readInteger() != -1) {
|
||||
|
@ -128,7 +128,7 @@ public class HNavigatorRoom {
|
||||
}
|
||||
|
||||
packet.appendInt(multiUse);
|
||||
packet.appendObjects(objectsToAppend);
|
||||
packet.appendObjects(objectsToAppend.toArray(new Object[] {}));
|
||||
}
|
||||
|
||||
public int getFlatId() {
|
||||
|
Loading…
Reference in New Issue
Block a user