Fix: non-monotonic protocol versions are not correctly handled

After 1.16.3, Mojang started publishing snapshot, pre-release and release
candidate versions of Minecraft with protocol version numbers of the form
`(1 << 30) | n' where 'n' is a small non-negative integer increasing with each
such version; the release versions continued to use the old format. For
example, these are the last 8 published Minecraft versions as of this commit:

release           1.16.3      uses protocol version 753
pre-release       1.16.4-pre1 uses protocol version 1073741825 == (1 << 30) | 1
pre-release       1.16.4-pre2 uses protocol version 1073741826 == (1 << 30) | 2
release candidate 1.16.4-rc1  uses protocol version 1073741827 == (1 << 30) | 3
release           1.16.4      uses protocol version 754
snapshot          20w45a      uses protocol version 1073741829 == (1 << 30) | 5
snapshot          20w46a      uses protocol version 1073741830 == (1 << 30) | 6
snapshot          20w48a      uses protocol version 1073741831 == (1 << 30) | 7

This means that protocol versions no longer increase monotonically with respect
to publication history, a property that was assumed to hold in much of
pyCraft's code relating to support of multiple protocol versions. This commit
rectifies the issue by replacing any comparison of protocol versions by their
numerical value with a comparison based on their publication time.

Newly defined is the dictionary `minecraft.PROTOCOL_VERSION_INDICES', which
maps each known protocol version to its index in the protocol chronology. As
such, the bound method `minecraft.PROTOCOL_VERSION_INDICES.get` can be used as
a key function for the built-in `sorted`, `min` and `max` functions to collate
protocol versions chronologically.

Two utility functions are provided for direct comparison of protocol versions:
    `minecraft.utility.protocol_earlier` and
    `minecraft.utility.protocol_earlier_eq`.

Additionally, four methods are added to the `ConnectionContext` type to ease
the most common cases where the protocol of a given context must be compared to
a given version number:
    `minecraft.connection.ConnectionContext.protocol_earlier`,
    `minecraft.connection.ConnectionContext.protocol_earlier_eq`,
    `minecraft.connection.ConnectionContext.protocol_later` and
    `minecraft.connection.ConnectionContext.protocol_later_eq`.
This commit is contained in:
joo committed 2020-12-02 15:11:39 +01:00
1 parent 4052136d30
commit 969419da3f
29 files changed
+1241 -983

No files matched your search

+35 -9
View File
@@ -11,9 +11,11 @@ import re
from .types import VarInt
from .packets import clientbound, serverbound
from . import packets
from . import encryption
from .. import SUPPORTED_PROTOCOL_VERSIONS, SUPPORTED_MINECRAFT_VERSIONS
from . import packets, encryption
from .. import (
utility, KNOWN_MINECRAFT_VERSIONS, SUPPORTED_MINECRAFT_VERSIONS,
SUPPORTED_PROTOCOL_VERSIONS, PROTOCOL_VERSION_INDICES
)
from ..exceptions import (
VersionMismatch, LoginDisconnect, IgnorePacket, InvalidState
)
@@ -31,6 +33,26 @@ class ConnectionContext(object):
def __init__(self, **kwds):
self.protocol_version = kwds.get('protocol_version')
def protocol_earlier(self, other_pv):
"""Returns True if the protocol version of this context was published
earlier than 'other_pv', or else False."""
return utility.protocol_earlier(self.protocol_version, other_pv)
def protocol_earlier_eq(self, other_pv):
"""Returns True if the protocol version of this context was published
earlier than, or is equal to, 'other_pv', or else False."""
return utility.protocol_earlier_eq(self.protocol_version, other_pv)
def protocol_later(self, other_pv):
"""Returns True if the protocol version of this context was published
later than 'other_pv', or else False."""
return utility.protocol_earlier(other_pv, self.protocol_version)
def protocol_later_eq(self, other_pv):
"""Returns True if the protocol version of this context was published
later than, or is equal to, 'other_pv', or else False."""
return utility.protocol_earlier_eq(other_pv, self.protocol_version)
class _ConnectionOptions(object):
def __init__(self, address=None, port=None, compression_threshold=-1,
@@ -129,13 +151,15 @@ class Connection(object):
allowed_versions = set(map(proto_version, allowed_versions))
self.allowed_proto_versions = allowed_versions
latest_allowed_proto = max(self.allowed_proto_versions,
key=PROTOCOL_VERSION_INDICES.get)
if initial_version is None:
self.default_proto_version = max(self.allowed_proto_versions)
self.default_proto_version = latest_allowed_proto
else:
self.default_proto_version = proto_version(initial_version)
self.context = ConnectionContext(
protocol_version=max(self.allowed_proto_versions))
self.context = ConnectionContext(protocol_version=latest_allowed_proto)
self.options = _ConnectionOptions()
self.options.address = address
@@ -362,7 +386,9 @@ class Connection(object):
# It is important that this is set correctly even when connecting
# in status mode, as some servers, e.g. SpigotMC with the
# ProtocolSupport plugin, use it to determine the correct response.
self.context.protocol_version = max(self.allowed_proto_versions)
self.context.protocol_version \
= max(self.allowed_proto_versions,
key=PROTOCOL_VERSION_INDICES.get)
self.spawned = False
self._connect()
@@ -496,7 +522,7 @@ class Connection(object):
def _version_mismatch(self, server_protocol=None, server_version=None):
if server_protocol is None:
server_protocol = SUPPORTED_MINECRAFT_VERSIONS.get(server_version)
server_protocol = KNOWN_MINECRAFT_VERSIONS.get(server_version)
if server_protocol is None:
vs = 'version' if server_version is None else \
@@ -751,7 +777,7 @@ class PlayingReactor(PacketReactor):
self.connection.write_packet(keep_alive_packet)
elif packet.packet_name == "player position and look":
if self.connection.context.protocol_version >= 107:
if self.connection.context.protocol_later_eq(107):
teleport_confirm = serverbound.play.TeleportConfirmPacket()
teleport_confirm.teleport_id = packet.teleport_id
self.connection.write_packet(teleport_confirm)