13
0
geforkt von Mirrors/Velocity

Don't repeat validation in AvailableCommands

When deserializing an AvailableCommands packet, we do a few sanity checks to ensure the packet is valid. Some of this work was repeated for each cycle (notably the root) so we now check the children and any redirects are defined only once.
Dieser Commit ist enthalten in:
Andrew Steinborn 2020-11-24 12:03:34 -05:00
Ursprung dd8c670ef7
Commit 084b741375

Datei anzeigen

@ -204,6 +204,7 @@ public class AvailableCommands implements MinecraftPacket {
private final int redirectTo; private final int redirectTo;
private final @Nullable ArgumentBuilder<CommandSource, ?> args; private final @Nullable ArgumentBuilder<CommandSource, ?> args;
private @MonotonicNonNull CommandNode<CommandSource> built; private @MonotonicNonNull CommandNode<CommandSource> built;
private boolean validated;
private WireNode(int idx, byte flags, int[] children, int redirectTo, private WireNode(int idx, byte flags, int[] children, int redirectTo,
@Nullable ArgumentBuilder<CommandSource, ?> args) { @Nullable ArgumentBuilder<CommandSource, ?> args) {
@ -212,10 +213,10 @@ public class AvailableCommands implements MinecraftPacket {
this.children = children; this.children = children;
this.redirectTo = redirectTo; this.redirectTo = redirectTo;
this.args = args; this.args = args;
this.validated = false;
} }
boolean toNode(WireNode[] wireNodes) { void validate(WireNode[] wireNodes) {
if (this.built == null) {
// Ensure all children exist. Note that we delay checking if the node has been built yet; // Ensure all children exist. Note that we delay checking if the node has been built yet;
// that needs to come after this node is built. // that needs to come after this node is built.
for (int child : children) { for (int child : children) {
@ -224,6 +225,21 @@ public class AvailableCommands implements MinecraftPacket {
} }
} }
if (redirectTo != -1) {
if (redirectTo >= wireNodes.length) {
throw new IllegalStateException("Node points to non-existent index " + redirectTo);
}
}
this.validated = true;
}
boolean toNode(WireNode[] wireNodes) {
if (!this.validated) {
this.validate(wireNodes);
}
if (this.built == null) {
int type = flags & FLAG_NODE_TYPE; int type = flags & FLAG_NODE_TYPE;
if (type == NODE_TYPE_ROOT) { if (type == NODE_TYPE_ROOT) {
this.built = new RootCommandNode<>(); this.built = new RootCommandNode<>();
@ -234,10 +250,6 @@ public class AvailableCommands implements MinecraftPacket {
// Add any redirects // Add any redirects
if (redirectTo != -1) { if (redirectTo != -1) {
if (redirectTo >= wireNodes.length) {
throw new IllegalStateException("Node points to non-existent index " + redirectTo);
}
WireNode redirect = wireNodes[redirectTo]; WireNode redirect = wireNodes[redirectTo];
if (redirect.built != null) { if (redirect.built != null) {
args.redirect(redirect.built); args.redirect(redirect.built);