Added GAsync tool

This commit is contained in:
WiredSpast 2022-02-11 00:16:37 +01:00
parent 27b754339c
commit 46fe97d4ae
2 changed files with 190 additions and 0 deletions

View File

@ -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();
}
}

View 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();
}
}
}