Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-03 14:50:30 +01:00
Merge pull request #2011 from astei/optimize/improve-inlining-transform
Make several tweaks to Protocol to improve inlining and performance on hot paths
Dieser Commit ist enthalten in:
Commit
1afed864b1
@ -411,14 +411,23 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
|
||||
int oldId = packetWrapper.getId();
|
||||
int newId = direction == Direction.OUTGOING ? protocolPacket.getNewID() : protocolPacket.getOldID();
|
||||
packetWrapper.setId(newId);
|
||||
if (protocolPacket.getRemapper() == null) {
|
||||
|
||||
PacketRemapper remapper = protocolPacket.getRemapper();
|
||||
if (remapper != null) {
|
||||
try {
|
||||
remapper.remap(packetWrapper);
|
||||
} catch (InformativeException e) { // Catch InformativeExceptions, pass through CancelExceptions
|
||||
throwRemapError(direction, state, oldId, newId, e);
|
||||
return;
|
||||
}
|
||||
|
||||
// Remap
|
||||
try {
|
||||
protocolPacket.getRemapper().remap(packetWrapper);
|
||||
} catch (InformativeException e) { // Catch InformativeExceptions, pass through CancelExceptions
|
||||
if (packetWrapper.isCancelled()) {
|
||||
throw CancelException.generate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void throwRemapError(Direction direction, State state, int oldId, int newId, InformativeException e) throws InformativeException {
|
||||
// Don't print errors during handshake
|
||||
if (state == State.HANDSHAKE) {
|
||||
throw e;
|
||||
@ -436,11 +445,6 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
|
||||
throw e;
|
||||
}
|
||||
|
||||
if (packetWrapper.isCancelled()) {
|
||||
throw CancelException.generate();
|
||||
}
|
||||
}
|
||||
|
||||
private String toNiceHex(int id) {
|
||||
String hex = Integer.toHexString(id).toUpperCase();
|
||||
return (hex.length() == 1 ? "0x0" : "0x") + hex;
|
||||
|
@ -1,5 +1,6 @@
|
||||
package us.myles.ViaVersion.api.protocol;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import us.myles.ViaVersion.api.PacketWrapper;
|
||||
import us.myles.ViaVersion.api.Via;
|
||||
import us.myles.ViaVersion.api.data.UserConnection;
|
||||
@ -9,13 +10,13 @@ import us.myles.ViaVersion.packets.State;
|
||||
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class ProtocolPipeline extends SimpleProtocol {
|
||||
private List<Protocol> protocolList;
|
||||
private List<Protocol> incoming;
|
||||
private List<Protocol> outgoing;
|
||||
private UserConnection userConnection;
|
||||
|
||||
public ProtocolPipeline(UserConnection userConnection) {
|
||||
@ -25,9 +26,10 @@ public class ProtocolPipeline extends SimpleProtocol {
|
||||
|
||||
@Override
|
||||
protected void registerPackets() {
|
||||
protocolList = new CopyOnWriteArrayList<>();
|
||||
incoming = new CopyOnWriteArrayList<>();
|
||||
outgoing = Lists.reverse(incoming);
|
||||
// This is a pipeline so we register basic pipes
|
||||
protocolList.add(ProtocolRegistry.BASE_PROTOCOL);
|
||||
incoming.add(ProtocolRegistry.BASE_PROTOCOL);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -40,7 +42,7 @@ public class ProtocolPipeline extends SimpleProtocol {
|
||||
userConnection.setProtocolInfo(protocolInfo);
|
||||
|
||||
/* Init through all our pipes */
|
||||
for (Protocol protocol : protocolList) {
|
||||
for (Protocol protocol : incoming) {
|
||||
protocol.init(userConnection);
|
||||
}
|
||||
}
|
||||
@ -52,38 +54,39 @@ public class ProtocolPipeline extends SimpleProtocol {
|
||||
* @param protocol The protocol to add to the end
|
||||
*/
|
||||
public void add(Protocol protocol) {
|
||||
if (protocolList != null) {
|
||||
protocolList.add(protocol);
|
||||
if (incoming != null) {
|
||||
incoming.add(protocol);
|
||||
protocol.init(userConnection);
|
||||
// Move base Protocols to the end, so the login packets can be modified by other protocols
|
||||
List<Protocol> toMove = new ArrayList<>();
|
||||
for (Protocol p : protocolList) {
|
||||
for (Protocol p : incoming) {
|
||||
if (ProtocolRegistry.isBaseProtocol(p)) {
|
||||
toMove.add(p);
|
||||
}
|
||||
}
|
||||
protocolList.removeAll(toMove);
|
||||
protocolList.addAll(toMove);
|
||||
incoming.removeAll(toMove);
|
||||
incoming.addAll(toMove);
|
||||
} else {
|
||||
throw new NullPointerException("Tried to add protocol to early");
|
||||
throw new NullPointerException("Tried to add protocol too early");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
|
||||
int originalID = packetWrapper.getId();
|
||||
List<Protocol> protocols = new ArrayList<>(protocolList);
|
||||
// Other way if outgoing
|
||||
if (direction == Direction.OUTGOING)
|
||||
Collections.reverse(protocols);
|
||||
List<Protocol> protocols = direction == Direction.OUTGOING ? outgoing : incoming;
|
||||
|
||||
// Apply protocols
|
||||
packetWrapper.apply(direction, state, 0, protocols);
|
||||
super.transform(direction, state, packetWrapper);
|
||||
|
||||
if (Via.getManager().isDebug()) {
|
||||
logPacket(direction, state, packetWrapper, originalID);
|
||||
}
|
||||
}
|
||||
|
||||
private void logPacket(Direction direction, State state, PacketWrapper packetWrapper, int originalID) {
|
||||
// Debug packet
|
||||
int serverProtocol = userConnection.getProtocolInfo().getServerProtocolVersion();
|
||||
int clientProtocol = userConnection.getProtocolInfo().getProtocolVersion();
|
||||
ViaPlatform platform = Via.getPlatform();
|
||||
|
||||
@ -103,7 +106,6 @@ public class ProtocolPipeline extends SimpleProtocol {
|
||||
packetWrapper
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the pipeline contains a protocol
|
||||
@ -112,14 +114,14 @@ public class ProtocolPipeline extends SimpleProtocol {
|
||||
* @return True if the protocol class is in the pipeline
|
||||
*/
|
||||
public boolean contains(Class<? extends Protocol> pipeClass) {
|
||||
for (Protocol protocol : protocolList) {
|
||||
for (Protocol protocol : incoming) {
|
||||
if (protocol.getClass().equals(pipeClass)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public <P extends Protocol> P getProtocol(Class<P> pipeClass) {
|
||||
for (Protocol protocol : protocolList) {
|
||||
for (Protocol protocol : incoming) {
|
||||
if (protocol.getClass() == pipeClass) return (P) protocol;
|
||||
}
|
||||
return null;
|
||||
@ -134,7 +136,7 @@ public class ProtocolPipeline extends SimpleProtocol {
|
||||
* @throws Exception If it failed to convert / packet cancelld.
|
||||
*/
|
||||
public boolean filter(Object o, List list) throws Exception {
|
||||
for (Protocol protocol : protocolList) {
|
||||
for (Protocol protocol : incoming) {
|
||||
if (protocol.isFiltered(o.getClass())) {
|
||||
protocol.filterPacket(userConnection, o, list);
|
||||
return true;
|
||||
@ -145,7 +147,7 @@ public class ProtocolPipeline extends SimpleProtocol {
|
||||
}
|
||||
|
||||
public List<Protocol> pipes() {
|
||||
return protocolList;
|
||||
return incoming;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren