Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-27 08:30:09 +01:00
PacketWrapper now has an ID attached and can be reset so that it can be read from stored values.
Removed some parameters from protocol, the ID will now be attached to the PacketWrapper (made more sense) BaseProtocol doesn't handle ProtocolInfo anymore Implement ProtocolPipeline (WIP)
Dieser Commit ist enthalten in:
Ursprung
fce7f1740e
Commit
89427cb2da
@ -2,21 +2,30 @@ package us.myles.ViaVersion2.api;
|
|||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import us.myles.ViaVersion2.api.data.UserConnection;
|
import us.myles.ViaVersion2.api.data.UserConnection;
|
||||||
import us.myles.ViaVersion2.api.remapper.ValueCreator;
|
import us.myles.ViaVersion2.api.remapper.ValueCreator;
|
||||||
import us.myles.ViaVersion2.api.type.Type;
|
import us.myles.ViaVersion2.api.type.Type;
|
||||||
import us.myles.ViaVersion2.api.util.Pair;
|
import us.myles.ViaVersion2.api.util.Pair;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class PacketWrapper {
|
public class PacketWrapper {
|
||||||
private final ByteBuf inputBuffer;
|
private final ByteBuf inputBuffer;
|
||||||
private final UserConnection userConnection;
|
private final UserConnection userConnection;
|
||||||
private boolean send = true;
|
private boolean send = true;
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
|
private int id = -1;
|
||||||
|
private LinkedList<Pair<Type, Object>> readableObjects = new LinkedList<>();
|
||||||
private List<Pair<Type, Object>> packetValues = new ArrayList<>();
|
private List<Pair<Type, Object>> packetValues = new ArrayList<>();
|
||||||
|
|
||||||
public PacketWrapper(ByteBuf inputBuffer, UserConnection userConnection) {
|
public PacketWrapper(int packetID, ByteBuf inputBuffer, UserConnection userConnection) {
|
||||||
|
this.id = packetID;
|
||||||
this.inputBuffer = inputBuffer;
|
this.inputBuffer = inputBuffer;
|
||||||
this.userConnection = userConnection;
|
this.userConnection = userConnection;
|
||||||
}
|
}
|
||||||
@ -49,10 +58,19 @@ public class PacketWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T> T read(Type<T> type) throws Exception {
|
public <T> T read(Type<T> type) throws Exception {
|
||||||
Preconditions.checkNotNull(inputBuffer, "This packet does not have an input buffer.");
|
if (readableObjects.isEmpty()) {
|
||||||
System.out.println("Reading: " + type.getTypeName());
|
Preconditions.checkNotNull(inputBuffer, "This packet does not have an input buffer.");
|
||||||
// We could in the future log input read values, but honestly for things like bulk maps, mem waste D:
|
System.out.println("Reading: " + type.getTypeName());
|
||||||
return type.read(inputBuffer);
|
// We could in the future log input read values, but honestly for things like bulk maps, mem waste D:
|
||||||
|
return type.read(inputBuffer);
|
||||||
|
} else {
|
||||||
|
Pair<Type, Object> read = readableObjects.poll();
|
||||||
|
if (read.getKey().equals(type)) {
|
||||||
|
return (T) read.getValue();
|
||||||
|
} else {
|
||||||
|
throw new IOException("Unable to read type " + type.getTypeName() + ", found " + type.getTypeName());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -68,12 +86,17 @@ public class PacketWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void writeToBuffer(ByteBuf buffer) throws Exception {
|
public void writeToBuffer(ByteBuf buffer) throws Exception {
|
||||||
|
if (id != -1) {
|
||||||
|
Type.VAR_INT.write(buffer, id);
|
||||||
|
}
|
||||||
|
|
||||||
for (Pair<Type, Object> packetValue : packetValues) {
|
for (Pair<Type, Object> packetValue : packetValues) {
|
||||||
packetValue.getKey().write(buffer, packetValue.getValue());
|
packetValue.getKey().write(buffer, packetValue.getValue());
|
||||||
}
|
}
|
||||||
|
writeRemaining(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeRemaining(ByteBuf output) {
|
private void writeRemaining(ByteBuf output) {
|
||||||
if (inputBuffer != null) {
|
if (inputBuffer != null) {
|
||||||
System.out.println("Writing remaining: " + output.readableBytes());
|
System.out.println("Writing remaining: " + output.readableBytes());
|
||||||
output.writeBytes(inputBuffer);
|
output.writeBytes(inputBuffer);
|
||||||
@ -83,17 +106,15 @@ public class PacketWrapper {
|
|||||||
public void send() throws Exception {
|
public void send() throws Exception {
|
||||||
ByteBuf output = inputBuffer.alloc().buffer();
|
ByteBuf output = inputBuffer.alloc().buffer();
|
||||||
writeToBuffer(output);
|
writeToBuffer(output);
|
||||||
writeRemaining(output);
|
|
||||||
|
|
||||||
user().sendRawPacket(output);
|
user().sendRawPacket(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PacketWrapper create() throws Exception {
|
public PacketWrapper create(int packetID) throws Exception {
|
||||||
return new PacketWrapper(null, user());
|
return new PacketWrapper(packetID, null, user());
|
||||||
}
|
}
|
||||||
|
|
||||||
public PacketWrapper create(ValueCreator init) throws Exception {
|
public PacketWrapper create(int packetID, ValueCreator init) throws Exception {
|
||||||
PacketWrapper wrapper = create();
|
PacketWrapper wrapper = create(packetID);
|
||||||
init.write(wrapper);
|
init.write(wrapper);
|
||||||
return wrapper;
|
return wrapper;
|
||||||
}
|
}
|
||||||
@ -109,4 +130,8 @@ public class PacketWrapper {
|
|||||||
public UserConnection user() {
|
public UserConnection user() {
|
||||||
return this.userConnection;
|
return this.userConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void resetReader() {
|
||||||
|
this.readableObjects.addAll(packetValues);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package us.myles.ViaVersion2.api.protocol;
|
package us.myles.ViaVersion2.api.protocol;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import us.myles.ViaVersion.CancelException;
|
import us.myles.ViaVersion.CancelException;
|
||||||
@ -8,17 +7,13 @@ import us.myles.ViaVersion.packets.Direction;
|
|||||||
import us.myles.ViaVersion.packets.State;
|
import us.myles.ViaVersion.packets.State;
|
||||||
import us.myles.ViaVersion2.api.PacketWrapper;
|
import us.myles.ViaVersion2.api.PacketWrapper;
|
||||||
import us.myles.ViaVersion2.api.data.UserConnection;
|
import us.myles.ViaVersion2.api.data.UserConnection;
|
||||||
import us.myles.ViaVersion2.api.protocol.base.BaseProtocol;
|
|
||||||
import us.myles.ViaVersion2.api.remapper.PacketRemapper;
|
import us.myles.ViaVersion2.api.remapper.PacketRemapper;
|
||||||
import us.myles.ViaVersion2.api.type.Type;
|
|
||||||
import us.myles.ViaVersion2.api.util.Pair;
|
import us.myles.ViaVersion2.api.util.Pair;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public abstract class Protocol {
|
public abstract class Protocol {
|
||||||
public static final Protocol BASE_PROTOCOL = new BaseProtocol();
|
|
||||||
|
|
||||||
private Map<Pair<State, Integer>, ProtocolPacket> incoming = new HashMap<>();
|
private Map<Pair<State, Integer>, ProtocolPacket> incoming = new HashMap<>();
|
||||||
private Map<Pair<State, Integer>, ProtocolPacket> outgoing = new HashMap<>();
|
private Map<Pair<State, Integer>, ProtocolPacket> outgoing = new HashMap<>();
|
||||||
|
|
||||||
@ -48,32 +43,25 @@ public abstract class Protocol {
|
|||||||
outgoing.put(new Pair<>(state, oldPacketID), protocolPacket);
|
outgoing.put(new Pair<>(state, oldPacketID), protocolPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void transform(Direction direction, State state, int packetID, PacketWrapper packetWrapper, ByteBuf output) throws Exception {
|
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
|
||||||
Pair<State, Integer> statePacket = new Pair<>(state, packetID);
|
Pair<State, Integer> statePacket = new Pair<>(state, packetWrapper.getId());
|
||||||
Map<Pair<State, Integer>, ProtocolPacket> packetMap = (direction == Direction.OUTGOING ? outgoing : incoming);
|
Map<Pair<State, Integer>, ProtocolPacket> packetMap = (direction == Direction.OUTGOING ? outgoing : incoming);
|
||||||
ProtocolPacket protocolPacket;
|
ProtocolPacket protocolPacket;
|
||||||
if (packetMap.containsKey(statePacket)) {
|
if (packetMap.containsKey(statePacket)) {
|
||||||
protocolPacket = packetMap.get(statePacket);
|
protocolPacket = packetMap.get(statePacket);
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Packet not found: " + packetID);
|
System.out.println("Packet not found: " + packetWrapper.getId());
|
||||||
// simply translate
|
|
||||||
Type.VAR_INT.write(output, packetID);
|
|
||||||
// pass through
|
|
||||||
packetWrapper.writeRemaining(output);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// write packet id
|
// write packet id
|
||||||
Type.VAR_INT.write(output, direction == Direction.OUTGOING ? protocolPacket.getNewID() : protocolPacket.getOldID());
|
int newID = direction == Direction.OUTGOING ? protocolPacket.getNewID() : protocolPacket.getOldID();
|
||||||
|
packetWrapper.setId(newID);
|
||||||
// remap
|
// remap
|
||||||
if (protocolPacket.getRemapper() != null) {
|
if (protocolPacket.getRemapper() != null) {
|
||||||
protocolPacket.getRemapper().remap(packetWrapper);
|
protocolPacket.getRemapper().remap(packetWrapper);
|
||||||
if(packetWrapper.isCancelled())
|
if(packetWrapper.isCancelled())
|
||||||
throw new CancelException();
|
throw new CancelException();
|
||||||
// write to output
|
|
||||||
packetWrapper.writeToBuffer(output);
|
|
||||||
}
|
}
|
||||||
// pass through
|
|
||||||
packetWrapper.writeRemaining(output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
package us.myles.ViaVersion2.api.protocol;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import us.myles.ViaVersion.CancelException;
|
||||||
|
import us.myles.ViaVersion.packets.Direction;
|
||||||
|
import us.myles.ViaVersion.packets.State;
|
||||||
|
import us.myles.ViaVersion2.api.PacketWrapper;
|
||||||
|
import us.myles.ViaVersion2.api.data.UserConnection;
|
||||||
|
import us.myles.ViaVersion2.api.protocol.base.BaseProtocol;
|
||||||
|
import us.myles.ViaVersion2.api.protocol.base.ProtocolInfo;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class ProtocolPipeline extends Protocol {
|
||||||
|
LinkedList<Protocol> protocolList = new LinkedList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void registerPackets() {
|
||||||
|
// This is a pipeline so we register basic pipes
|
||||||
|
protocolList.addLast(new BaseProtocol());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(UserConnection userConnection) {
|
||||||
|
ProtocolInfo protocolInfo = new ProtocolInfo();
|
||||||
|
protocolInfo.setPipeline(this);
|
||||||
|
|
||||||
|
userConnection.put(protocolInfo);
|
||||||
|
|
||||||
|
/* Init through all our pipes */
|
||||||
|
for (Protocol protocol : protocolList) {
|
||||||
|
protocol.init(userConnection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
|
||||||
|
|
||||||
|
for (Protocol protocol : new ArrayList<>(protocolList)) { // Copy to prevent from removal.
|
||||||
|
protocol.transform(direction, state, packetWrapper);
|
||||||
|
// Reset the reader for the packetWrapper (So it can be recycled across packets)
|
||||||
|
packetWrapper.resetReader();
|
||||||
|
}
|
||||||
|
super.transform(direction, state, packetWrapper);
|
||||||
|
}
|
||||||
|
}
|
@ -107,6 +107,6 @@ public class BaseProtocol extends Protocol {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(UserConnection userConnection) {
|
public void init(UserConnection userConnection) {
|
||||||
userConnection.put(new ProtocolInfo());
|
// Nothing gets added, ProtocolPipeline handles ProtocolInfo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import lombok.Getter;
|
|||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import us.myles.ViaVersion.packets.State;
|
import us.myles.ViaVersion.packets.State;
|
||||||
import us.myles.ViaVersion2.api.data.StoredObject;
|
import us.myles.ViaVersion2.api.data.StoredObject;
|
||||||
|
import us.myles.ViaVersion2.api.protocol.ProtocolPipeline;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -14,4 +15,5 @@ public class ProtocolInfo extends StoredObject{
|
|||||||
private int protocolVersion = -1;
|
private int protocolVersion = -1;
|
||||||
private String username;
|
private String username;
|
||||||
private UUID uuid;
|
private UUID uuid;
|
||||||
|
private ProtocolPipeline pipeline;
|
||||||
}
|
}
|
||||||
|
@ -40,8 +40,7 @@ public class EntityPackets {
|
|||||||
|
|
||||||
wrapper.cancel(); // Don't send current packet
|
wrapper.cancel(); // Don't send current packet
|
||||||
|
|
||||||
PacketWrapper passengerPacket = wrapper.create();
|
PacketWrapper passengerPacket = wrapper.create(0x40); // Passenger Packet ID
|
||||||
passengerPacket.write(Type.VAR_INT, 0x40); // Passenger Packet ID
|
|
||||||
if (vehicle == -1) {
|
if (vehicle == -1) {
|
||||||
if (!tracker.getVehicleMap().containsKey(passenger))
|
if (!tracker.getVehicleMap().containsKey(passenger))
|
||||||
return null; // Cancel
|
return null; // Cancel
|
||||||
|
@ -136,7 +136,7 @@ public class PlayerPackets {
|
|||||||
map(Type.UNSIGNED_BYTE); // 4 - Max Players (Tab)
|
map(Type.UNSIGNED_BYTE); // 4 - Max Players (Tab)
|
||||||
map(Type.STRING); // 5 - Level Type
|
map(Type.STRING); // 5 - Level Type
|
||||||
map(Type.BOOLEAN); // 6 - Reduced Debug info
|
map(Type.BOOLEAN); // 6 - Reduced Debug info
|
||||||
|
|
||||||
handler(new PacketHandler() {
|
handler(new PacketHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void handle(PacketWrapper wrapper) throws Exception {
|
public void handle(PacketWrapper wrapper) throws Exception {
|
||||||
@ -228,9 +228,7 @@ public class PlayerPackets {
|
|||||||
protocol.registerOutgoing(State.PLAY, 0x3F, 0x18); // Plugin Message
|
protocol.registerOutgoing(State.PLAY, 0x3F, 0x18); // Plugin Message
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// Login Success - Save UUID and Username
|
|
||||||
// Server Difficulty - Activate Auto-Team
|
// Server Difficulty - Activate Auto-Team
|
||||||
// TODO: Status Response, implement? (Might be implemented by a base protocol?)
|
|
||||||
|
|
||||||
/* Incoming Packets */
|
/* Incoming Packets */
|
||||||
|
|
||||||
|
@ -197,9 +197,7 @@ public class SpawnPackets {
|
|||||||
});
|
});
|
||||||
|
|
||||||
map(Type.STRING); // 2 - Title
|
map(Type.STRING); // 2 - Title
|
||||||
|
|
||||||
map(Type.POSITION); // 3 - Position
|
map(Type.POSITION); // 3 - Position
|
||||||
|
|
||||||
map(Type.BYTE); // 4 - Direction
|
map(Type.BYTE); // 4 - Direction
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren