Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-08 17:20:24 +01:00
Avoid List creation and collection reversal in transforming
Dieser Commit ist enthalten in:
Ursprung
b7bf993795
Commit
1ea548f05d
@ -16,13 +16,13 @@ import us.myles.ViaVersion.util.PipelineUtil;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
public class PacketWrapper {
|
public class PacketWrapper {
|
||||||
public static final int PASSTHROUGH_ID = 1000;
|
public static final int PASSTHROUGH_ID = 1000;
|
||||||
|
private static final Protocol[] PROTOCOL_ARRAY = new Protocol[0];
|
||||||
|
|
||||||
private final ByteBuf inputBuffer;
|
private final ByteBuf inputBuffer;
|
||||||
private final UserConnection userConnection;
|
private final UserConnection userConnection;
|
||||||
@ -317,30 +317,33 @@ public class PacketWrapper {
|
|||||||
* @throws Exception if it fails to write
|
* @throws Exception if it fails to write
|
||||||
*/
|
*/
|
||||||
private ByteBuf constructPacket(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, Direction direction) throws Exception {
|
private ByteBuf constructPacket(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, Direction direction) throws Exception {
|
||||||
// Apply current pipeline
|
// Apply current pipeline - for outgoing protocol, the collection will be reversed in the apply method
|
||||||
List<Protocol> protocols = new ArrayList<>(user().getProtocolInfo().getPipeline().pipes());
|
Protocol[] protocols = user().getProtocolInfo().getPipeline().pipes().toArray(PROTOCOL_ARRAY);
|
||||||
if (direction == Direction.OUTGOING) {
|
boolean reverse = direction == Direction.OUTGOING;
|
||||||
// Other way if outgoing
|
|
||||||
Collections.reverse(protocols);
|
|
||||||
}
|
|
||||||
int index = -1;
|
int index = -1;
|
||||||
for (int i = 0; i < protocols.size(); i++) {
|
for (int i = 0; i < protocols.length; i++) {
|
||||||
if (protocols.get(i).getClass().equals(packetProtocol)) {
|
if (protocols[i].getClass() == packetProtocol) {
|
||||||
index = skipCurrentPipeline ? (i + 1) : (i);
|
index = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (index == -1) throw new NoSuchElementException(packetProtocol.getCanonicalName());
|
|
||||||
|
if (index == -1) {
|
||||||
|
// The given protocol is not in the pipeline
|
||||||
|
throw new NoSuchElementException(packetProtocol.getCanonicalName());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (skipCurrentPipeline) {
|
||||||
|
index = reverse ? index - 1 : index + 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Reset reader before we start
|
// Reset reader before we start
|
||||||
resetReader();
|
resetReader();
|
||||||
|
|
||||||
// Apply other protocols
|
// Apply other protocols
|
||||||
apply(direction, user().getProtocolInfo().getState(), index, protocols);
|
apply(direction, user().getProtocolInfo().getState(), index, protocols, reverse);
|
||||||
// Send
|
|
||||||
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
|
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
|
||||||
writeToBuffer(output);
|
writeToBuffer(output);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -417,22 +420,41 @@ public class PacketWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies a pipeline from an index to the wrapper
|
* Applies a pipeline from an index to the wrapper.
|
||||||
*
|
*
|
||||||
* @param direction The direction
|
* @param direction protocol direction
|
||||||
* @param state The state
|
* @param state protocol state
|
||||||
* @param index The index to start from
|
* @param index index to start from, will be reversed depending on the reverse parameter
|
||||||
* @param pipeline The pipeline
|
* @param pipeline protocol pipeline
|
||||||
|
* @param reverse whether the array should be looped in reverse, will also reverse the given index
|
||||||
* @return The current packetwrapper
|
* @return The current packetwrapper
|
||||||
* @throws Exception If it fails to transform a packet, exception will be thrown
|
* @throws Exception If it fails to transform a packet, exception will be thrown
|
||||||
*/
|
*/
|
||||||
public PacketWrapper apply(Direction direction, State state, int index, List<Protocol> pipeline) throws Exception {
|
public PacketWrapper apply(Direction direction, State state, int index, List<Protocol> pipeline, boolean reverse) throws Exception {
|
||||||
for (int i = index; i < pipeline.size(); i++) { // Copy to prevent from removal.
|
Protocol[] array = pipeline.toArray(PROTOCOL_ARRAY);
|
||||||
pipeline.get(i).transform(direction, state, this);
|
return apply(direction, state, reverse ? array.length - 1 : index, array, reverse); // Copy to prevent from removal
|
||||||
// Reset the reader for the packetWrapper (So it can be recycled across packets)
|
|
||||||
resetReader();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see #apply(Direction, State, int, List, boolean)
|
||||||
|
*/
|
||||||
|
public PacketWrapper apply(Direction direction, State state, int index, List<Protocol> pipeline) throws Exception {
|
||||||
|
return apply(direction, state, index, pipeline.toArray(PROTOCOL_ARRAY), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private PacketWrapper apply(Direction direction, State state, int index, Protocol[] pipeline, boolean reverse) throws Exception {
|
||||||
|
// Reset the reader after every transformation for the packetWrapper, so it can be recycled across packets
|
||||||
|
if (reverse) {
|
||||||
|
for (int i = index; i >= 0; i--) {
|
||||||
|
pipeline[i].transform(direction, state, this);
|
||||||
|
resetReader();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (int i = index; i < pipeline.length; i++) {
|
||||||
|
pipeline[i].transform(direction, state, this);
|
||||||
|
resetReader();
|
||||||
|
}
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -459,6 +459,14 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
|
|||||||
storedObjects.put(object.getClass(), object);
|
storedObjects.put(object.getClass(), object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if this Protocol's {@link #loadMappingData()} method should be called.
|
||||||
|
* <p>
|
||||||
|
* This does *not* necessarily mean that {@link #getMappingData()} is non-null, since this may be
|
||||||
|
* overriden, depending on special cases.
|
||||||
|
*
|
||||||
|
* @return true if this Protocol's {@link #loadMappingData()} method should be called
|
||||||
|
*/
|
||||||
public boolean hasMappingDataToLoad() {
|
public boolean hasMappingDataToLoad() {
|
||||||
return getMappingData() != null;
|
return getMappingData() != null;
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ import us.myles.ViaVersion.packets.State;
|
|||||||
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
|
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
@ -19,7 +18,6 @@ public class ProtocolPipeline extends SimpleProtocol {
|
|||||||
private UserConnection userConnection;
|
private UserConnection userConnection;
|
||||||
|
|
||||||
public ProtocolPipeline(UserConnection userConnection) {
|
public ProtocolPipeline(UserConnection userConnection) {
|
||||||
super();
|
|
||||||
init(userConnection);
|
init(userConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,18 +70,18 @@ public class ProtocolPipeline extends SimpleProtocol {
|
|||||||
@Override
|
@Override
|
||||||
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
|
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
|
||||||
int originalID = packetWrapper.getId();
|
int originalID = packetWrapper.getId();
|
||||||
List<Protocol> protocols = new ArrayList<>(protocolList);
|
|
||||||
// Other way if outgoing
|
|
||||||
if (direction == Direction.OUTGOING)
|
|
||||||
Collections.reverse(protocols);
|
|
||||||
|
|
||||||
// Apply protocols
|
// Apply protocols
|
||||||
packetWrapper.apply(direction, state, 0, protocols);
|
packetWrapper.apply(direction, state, 0, protocolList, direction == Direction.OUTGOING);
|
||||||
super.transform(direction, state, packetWrapper);
|
super.transform(direction, state, packetWrapper);
|
||||||
|
|
||||||
if (Via.getManager().isDebug()) {
|
if (Via.getManager().isDebug()) {
|
||||||
|
logPacket(direction, state, packetWrapper, originalID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logPacket(Direction direction, State state, PacketWrapper packetWrapper, int originalID) {
|
||||||
// Debug packet
|
// Debug packet
|
||||||
int serverProtocol = userConnection.getProtocolInfo().getServerProtocolVersion();
|
|
||||||
int clientProtocol = userConnection.getProtocolInfo().getProtocolVersion();
|
int clientProtocol = userConnection.getProtocolInfo().getProtocolVersion();
|
||||||
ViaPlatform platform = Via.getPlatform();
|
ViaPlatform platform = Via.getPlatform();
|
||||||
|
|
||||||
@ -103,7 +101,6 @@ public class ProtocolPipeline extends SimpleProtocol {
|
|||||||
packetWrapper
|
packetWrapper
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the pipeline contains a protocol
|
* Check if the pipeline contains a protocol
|
||||||
|
@ -372,6 +372,7 @@ public class ProtocolRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void shutdownLoaderExecutor() {
|
private static void shutdownLoaderExecutor() {
|
||||||
|
Via.getPlatform().getLogger().info("Shutting down mapping loader executor!");
|
||||||
mappingsLoaded = true;
|
mappingsLoaded = true;
|
||||||
mappingLoaderExecutor.shutdown();
|
mappingLoaderExecutor.shutdown();
|
||||||
mappingLoaderExecutor = null;
|
mappingLoaderExecutor = null;
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren