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:
1 parent
4052136d30
commit
969419da3f
29 files changed
+1241
-983
No files matched your search
+9
-11
@@ -24,9 +24,6 @@ import hashlib
|
||||
import uuid
|
||||
|
||||
|
||||
VERSIONS = sorted(SUPPORTED_MINECRAFT_VERSIONS.items(), key=lambda i: i[1])
|
||||
VERSIONS = [v for (v, p) in VERSIONS]
|
||||
|
||||
THREAD_TIMEOUT_S = 2
|
||||
|
||||
|
||||
@@ -108,7 +105,7 @@ class FakeClientHandler(object):
|
||||
level_type='default', reduced_debug_info=False, render_distance=9,
|
||||
respawn_screen=False, is_debug=False, is_flat=False)
|
||||
|
||||
if self.server.context.protocol_version >= 748:
|
||||
if self.server.context.protocol_later_eq(748):
|
||||
packet.dimension = pynbt.TAG_Compound({
|
||||
'natural': pynbt.TAG_Byte(1),
|
||||
'effects': pynbt.TAG_String('minecraft:overworld'),
|
||||
@@ -134,7 +131,7 @@ class FakeClientHandler(object):
|
||||
]),
|
||||
}),
|
||||
}, '')
|
||||
elif self.server.context.protocol_version >= 718:
|
||||
elif self.server.context.protocol_later_eq(718):
|
||||
packet.dimension = 'minecraft:overworld'
|
||||
else:
|
||||
packet.dimension = types.Dimension.OVERWORLD
|
||||
@@ -232,13 +229,13 @@ class FakeClientHandler(object):
|
||||
# Prepare to transition from handshaking to play state (via login),
|
||||
# using the given serverbound HandShakePacket to perform play-specific
|
||||
# processing.
|
||||
if packet.protocol_version == self.server.context.protocol_version:
|
||||
if self.server.context.protocol_version == packet.protocol_version:
|
||||
return self._run_login()
|
||||
if packet.protocol_version < self.server.context.protocol_version:
|
||||
msg = 'Outdated client! Please use %s' \
|
||||
elif self.server.context.protocol_earlier(packet.protocol_version):
|
||||
msg = "Outdated server! I'm still on %s" \
|
||||
% self.server.minecraft_version
|
||||
else:
|
||||
msg = "Outdated server! I'm still on %s" \
|
||||
msg = 'Outdated client! Please use %s' \
|
||||
% self.server.minecraft_version
|
||||
self.handle_login_server_disconnect(msg)
|
||||
|
||||
@@ -372,7 +369,7 @@ class FakeServer(object):
|
||||
client_handler_type=FakeClientHandler, private_key=None,
|
||||
public_key_bytes=None, test_case=None):
|
||||
if minecraft_version is None:
|
||||
minecraft_version = VERSIONS[-1][0]
|
||||
minecraft_version = list(SUPPORTED_MINECRAFT_VERSIONS.keys())[-1]
|
||||
|
||||
if isinstance(minecraft_version, Integral):
|
||||
proto = minecraft_version
|
||||
@@ -458,8 +455,9 @@ class _FakeServerTest(unittest.TestCase):
|
||||
must raise a 'FakeServerTestSuccess' exception.
|
||||
"""
|
||||
|
||||
server_version = VERSIONS[-1]
|
||||
server_version = None
|
||||
# The Minecraft version ID that the server will support.
|
||||
# If None, the latest supported version will be used.
|
||||
|
||||
client_versions = None
|
||||
# The set of Minecraft version IDs or protocol version numbers that the
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import unittest
|
||||
|
||||
from minecraft import SUPPORTED_PROTOCOL_VERSIONS
|
||||
from minecraft import utility
|
||||
from minecraft.networking.connection import ConnectionContext
|
||||
from minecraft.networking import packets
|
||||
from minecraft.networking import types
|
||||
@@ -87,6 +88,12 @@ class LegacyTypesTest(unittest.TestCase):
|
||||
self.assertIsInstance(types.FixedPointInteger, types.FixedPoint)
|
||||
self.assertEqual(types.FixedPointInteger.denominator, 32)
|
||||
|
||||
for attr in ('descriptor', 'overridable_descriptor',
|
||||
'overridable_property', 'attribute_alias',
|
||||
'multi_attribute_alias', 'attribute_transform',
|
||||
'class_and_instancemethod'):
|
||||
self.assertEqual(getattr(types, attr), getattr(utility, attr))
|
||||
|
||||
|
||||
class ClassMemberAliasesTest(unittest.TestCase):
|
||||
def test_alias_values(self):
|
||||
|
||||
+36
-28
@@ -1,5 +1,7 @@
|
||||
from minecraft import SUPPORTED_MINECRAFT_VERSIONS
|
||||
from minecraft import SUPPORTED_PROTOCOL_VERSIONS
|
||||
from minecraft import (
|
||||
SUPPORTED_MINECRAFT_VERSIONS, SUPPORTED_PROTOCOL_VERSIONS,
|
||||
PROTOCOL_VERSION_INDICES,
|
||||
)
|
||||
from minecraft.networking.packets import clientbound, serverbound
|
||||
from minecraft.networking.connection import Connection
|
||||
from minecraft.exceptions import (
|
||||
@@ -119,26 +121,22 @@ class ConnectCompressionHighTest(ConnectTest):
|
||||
|
||||
|
||||
class AllowedVersionsTest(fake_server._FakeServerTest):
|
||||
versions = sorted(SUPPORTED_MINECRAFT_VERSIONS.items(), key=lambda p: p[1])
|
||||
versions = dict((versions[0], versions[len(versions)//2], versions[-1]))
|
||||
versions = list(SUPPORTED_MINECRAFT_VERSIONS.items())
|
||||
test_indices = (0, len(versions) // 2, len(versions) - 1)
|
||||
|
||||
client_handler_type = ConnectTest.client_handler_type
|
||||
|
||||
def test_with_version_names(self):
|
||||
for version, proto in AllowedVersionsTest.versions.items():
|
||||
client_versions = {
|
||||
v for (v, p) in SUPPORTED_MINECRAFT_VERSIONS.items()
|
||||
if p <= proto}
|
||||
for index in self.test_indices:
|
||||
self._test_connect(
|
||||
server_version=version, client_versions=client_versions)
|
||||
server_version=self.versions[index][0],
|
||||
client_versions={v[0] for v in self.versions[:index+1]})
|
||||
|
||||
def test_with_protocol_numbers(self):
|
||||
for version, proto in AllowedVersionsTest.versions.items():
|
||||
client_versions = {
|
||||
p for (v, p) in SUPPORTED_MINECRAFT_VERSIONS.items()
|
||||
if p <= proto}
|
||||
for index in self.test_indices:
|
||||
self._test_connect(
|
||||
server_version=version, client_versions=client_versions)
|
||||
server_version=self.versions[index][0],
|
||||
client_versions={v[1] for v in self.versions[:index+1]})
|
||||
|
||||
|
||||
class LoginDisconnectTest(fake_server._FakeServerTest):
|
||||
@@ -363,12 +361,22 @@ class HandleExceptionTest(ConnectTest):
|
||||
|
||||
|
||||
class VersionNegotiationEdgeCases(fake_server._FakeServerTest):
|
||||
lowest_version = min(SUPPORTED_PROTOCOL_VERSIONS)
|
||||
highest_version = max(SUPPORTED_PROTOCOL_VERSIONS)
|
||||
impossible_version = highest_version + 1
|
||||
earliest_version = SUPPORTED_PROTOCOL_VERSIONS[0]
|
||||
latest_version = SUPPORTED_PROTOCOL_VERSIONS[-1]
|
||||
|
||||
fake_version = max(PROTOCOL_VERSION_INDICES.keys()) + 1
|
||||
fake_version_index = max(PROTOCOL_VERSION_INDICES.values()) + 1
|
||||
|
||||
def setUp(self):
|
||||
PROTOCOL_VERSION_INDICES[self.fake_version] = self.fake_version_index
|
||||
super(VersionNegotiationEdgeCases, self).setUp()
|
||||
|
||||
def tearDown(self):
|
||||
super(VersionNegotiationEdgeCases, self).tearDown()
|
||||
del PROTOCOL_VERSION_INDICES[self.fake_version]
|
||||
|
||||
def test_client_protocol_unsupported(self):
|
||||
self._test_client_protocol(version=self.impossible_version)
|
||||
self._test_client_protocol(version=self.fake_version)
|
||||
|
||||
def test_client_protocol_unknown(self):
|
||||
self._test_client_protocol(version='surprise me!')
|
||||
@@ -383,21 +391,21 @@ class VersionNegotiationEdgeCases(fake_server._FakeServerTest):
|
||||
def test_server_protocol_unsupported(self, client_versions=None):
|
||||
with self.assertRaisesRegexp(VersionMismatch, 'not supported'):
|
||||
self._test_connect(client_versions=client_versions,
|
||||
server_version=self.impossible_version)
|
||||
server_version=self.fake_version)
|
||||
|
||||
def test_server_protocol_unsupported_direct(self):
|
||||
self.test_server_protocol_unsupported({self.highest_version})
|
||||
self.test_server_protocol_unsupported({self.latest_version})
|
||||
|
||||
def test_server_protocol_disallowed(self, client_versions=None):
|
||||
if client_versions is None:
|
||||
client_versions = set(SUPPORTED_PROTOCOL_VERSIONS) \
|
||||
- {self.highest_version}
|
||||
- {self.latest_version}
|
||||
with self.assertRaisesRegexp(VersionMismatch, 'not allowed'):
|
||||
self._test_connect(client_versions={self.lowest_version},
|
||||
server_version=self.highest_version)
|
||||
self._test_connect(client_versions={self.earliest_version},
|
||||
server_version=self.latest_version)
|
||||
|
||||
def test_server_protocol_disallowed_direct(self):
|
||||
self.test_server_protocol_disallowed({self.lowest_version})
|
||||
self.test_server_protocol_disallowed({self.earliest_version})
|
||||
|
||||
def test_default_protocol_version(self, status_response=None):
|
||||
if status_response is None:
|
||||
@@ -414,10 +422,10 @@ class VersionNegotiationEdgeCases(fake_server._FakeServerTest):
|
||||
raise fake_server.FakeServerDisconnect('Test complete.')
|
||||
|
||||
def make_connection(*args, **kwds):
|
||||
kwds['initial_version'] = self.lowest_version
|
||||
kwds['initial_version'] = self.earliest_version
|
||||
return Connection(*args, **kwds)
|
||||
|
||||
self._test_connect(server_version=self.lowest_version,
|
||||
self._test_connect(server_version=self.earliest_version,
|
||||
client_handler_type=ClientHandler,
|
||||
connection_type=make_connection)
|
||||
|
||||
@@ -436,9 +444,9 @@ class VersionNegotiationEdgeCases(fake_server._FakeServerTest):
|
||||
raise fake_server.FakeServerDisconnect('Test complete.')
|
||||
|
||||
def make_connection(*args, **kwds):
|
||||
kwds['initial_version'] = self.lowest_version
|
||||
kwds['initial_version'] = self.earliest_version
|
||||
return Connection(*args, **kwds)
|
||||
|
||||
self._test_connect(server_version=self.lowest_version,
|
||||
self._test_connect(server_version=self.earliest_version,
|
||||
client_handler_type=ClientHandler,
|
||||
connection_type=make_connection)
|
||||
+16
-14
@@ -209,7 +209,7 @@ class TestReadWritePackets(unittest.TestCase):
|
||||
context.protocol_version = protocol_version
|
||||
packet = clientbound.play.MultiBlockChangePacket(context)
|
||||
|
||||
if protocol_version >= 741:
|
||||
if context.protocol_later_eq(741):
|
||||
packet.chunk_section_pos = Vector(167, 17, 33)
|
||||
packet.invert_trust_edges = False
|
||||
else:
|
||||
@@ -239,8 +239,9 @@ class TestReadWritePackets(unittest.TestCase):
|
||||
'type_id', context)
|
||||
|
||||
pos_look = PositionAndLook(
|
||||
position=(Vector(68.0, 38.0, 76.0) if protocol_version >= 100
|
||||
else Vector(68, 38, 76)),
|
||||
position=(Vector(68.0, 38.0, 76.0)
|
||||
if context.protocol_later_eq(100) else
|
||||
Vector(68, 38, 76)),
|
||||
yaw=263.494, pitch=180)
|
||||
velocity = Vector(21, 55, 41)
|
||||
entity_id, type_name, type_id = 49846, 'EGG', EntityType.EGG
|
||||
@@ -252,7 +253,7 @@ class TestReadWritePackets(unittest.TestCase):
|
||||
velocity_x=velocity.x, velocity_y=velocity.y,
|
||||
velocity_z=velocity.z,
|
||||
entity_id=entity_id, type_id=type_id, data=1)
|
||||
if protocol_version >= 49:
|
||||
if context.protocol_later_eq(49):
|
||||
object_uuid = 'd9568851-85bc-4a10-8d6a-261d130626fa'
|
||||
packet.object_uuid = object_uuid
|
||||
self.assertEqual(packet.objectUUID, object_uuid)
|
||||
@@ -267,12 +268,12 @@ class TestReadWritePackets(unittest.TestCase):
|
||||
"object_uuid='d9568851-85bc-4a10-8d6a-261d130626fa', "
|
||||
"type_id=EGG, x=68.0, y=38.0, z=76.0, pitch=180, yaw=263.494, "
|
||||
"data=1, velocity_x=21, velocity_y=55, velocity_z=41)"
|
||||
% packet.id if protocol_version >= 100 else
|
||||
% packet.id if context.protocol_later_eq(100) else
|
||||
"0x%02X SpawnObjectPacket(entity_id=49846, "
|
||||
"object_uuid='d9568851-85bc-4a10-8d6a-261d130626fa', "
|
||||
"type_id=EGG, x=68, y=38, z=76, pitch=180, yaw=263.494, "
|
||||
"data=1, velocity_x=21, velocity_y=55, velocity_z=41)"
|
||||
% packet.id if protocol_version >= 49 else
|
||||
% packet.id if context.protocol_later_eq(49) else
|
||||
"0x%02X SpawnObjectPacket(entity_id=49846, type_id=EGG, "
|
||||
"x=68, y=38, z=76, pitch=180, yaw=263.494, data=1, "
|
||||
"velocity_x=21, velocity_y=55, velocity_z=41)" % packet.id
|
||||
@@ -282,7 +283,7 @@ class TestReadWritePackets(unittest.TestCase):
|
||||
context=context, position_and_look=pos_look,
|
||||
velocity=velocity, type=type_name,
|
||||
entity_id=entity_id, data=1)
|
||||
if protocol_version >= 49:
|
||||
if context.protocol_later_eq(49):
|
||||
packet2.object_uuid = object_uuid
|
||||
self.assertEqual(packet.__dict__, packet2.__dict__)
|
||||
|
||||
@@ -290,7 +291,7 @@ class TestReadWritePackets(unittest.TestCase):
|
||||
self.assertEqual(packet.position, packet2.position)
|
||||
|
||||
packet2.data = 0
|
||||
if protocol_version < 49:
|
||||
if context.protocol_earlier(49):
|
||||
del packet2.velocity
|
||||
self._test_read_write_packet(packet, context,
|
||||
yaw=360/256, pitch=360/256)
|
||||
@@ -304,11 +305,11 @@ class TestReadWritePackets(unittest.TestCase):
|
||||
packet = clientbound.play.SoundEffectPacket(
|
||||
sound_id=545, effect_position=Vector(0.125, 300.0, 50.5),
|
||||
volume=0.75)
|
||||
if protocol_version >= 201:
|
||||
if context.protocol_later_eq(201):
|
||||
packet.pitch = struct.unpack('f', struct.pack('f', 1.5))[0]
|
||||
else:
|
||||
packet.pitch = int(1.5 / 63.5) * 63.5
|
||||
if context.protocol_version >= 95:
|
||||
if context.protocol_later_eq(95):
|
||||
packet.sound_category = \
|
||||
clientbound.play.SoundEffectPacket.SoundCategory.NEUTRAL
|
||||
|
||||
@@ -321,19 +322,20 @@ class TestReadWritePackets(unittest.TestCase):
|
||||
packet = clientbound.play.FacePlayerPacket(context)
|
||||
packet.target = 1.0, -2.0, 3.5
|
||||
packet.entity_id = None
|
||||
if protocol_version >= 353:
|
||||
if context.protocol_later_eq(353):
|
||||
packet.origin = OriginPoint.EYES
|
||||
self.assertEqual(
|
||||
str(packet),
|
||||
"0x%02X FacePlayerPacket(origin=EYES, x=1.0, y=-2.0, z=3.5, "
|
||||
"entity_id=None)" % packet.id if protocol_version >= 353 else
|
||||
"entity_id=None)" % packet.id
|
||||
if context.protocol_later_eq(353) else
|
||||
"0x%02X FacePlayerPacket(entity_id=None, x=1.0, y=-2.0, z=3.5)"
|
||||
% packet.id
|
||||
)
|
||||
self._test_read_write_packet(packet, context)
|
||||
|
||||
packet.entity_id = 123
|
||||
if protocol_version >= 353:
|
||||
if context.protocol_later_eq(353):
|
||||
packet.entity_origin = OriginPoint.FEET
|
||||
else:
|
||||
del packet.target
|
||||
@@ -341,7 +343,7 @@ class TestReadWritePackets(unittest.TestCase):
|
||||
str(packet),
|
||||
"0x%02X FacePlayerPacket(origin=EYES, x=1.0, y=-2.0, z=3.5, "
|
||||
"entity_id=123, entity_origin=FEET)" % packet.id
|
||||
if protocol_version >= 353 else
|
||||
if context.protocol_later_eq(353) else
|
||||
"0x%02X FacePlayerPacket(entity_id=123)" % packet.id
|
||||
)
|
||||
self._test_read_write_packet(packet, context)
|
||||
|
||||
@@ -55,7 +55,7 @@ class MapPacketTest(unittest.TestCase):
|
||||
packet.is_tracking_position = True
|
||||
packet.is_locked = False
|
||||
packet.icons = []
|
||||
d_name = u'Marshmallow' if context.protocol_version >= 364 else None
|
||||
d_name = u'Marshmallow' if context.protocol_later_eq(364) else None
|
||||
packet.icons.append(MapPacket.MapIcon(
|
||||
type=2, direction=2, location=(1, 1), display_name=d_name
|
||||
))
|
||||
|
||||
@@ -11,7 +11,7 @@ from minecraft.networking.connection import (
|
||||
from minecraft.networking.packets import clientbound
|
||||
|
||||
|
||||
max_proto_ver = max(SUPPORTED_PROTOCOL_VERSIONS)
|
||||
latest_proto = SUPPORTED_PROTOCOL_VERSIONS[-1]
|
||||
|
||||
|
||||
class LoginReactorTest(unittest.TestCase):
|
||||
@@ -19,7 +19,7 @@ class LoginReactorTest(unittest.TestCase):
|
||||
@mock.patch('minecraft.networking.connection.encryption')
|
||||
def test_encryption_online_server(self, encrypt):
|
||||
connection = mock.MagicMock()
|
||||
connection.context = ConnectionContext(protocol_version=max_proto_ver)
|
||||
connection.context = ConnectionContext(protocol_version=latest_proto)
|
||||
reactor = LoginReactor(connection)
|
||||
|
||||
packet = clientbound.login.EncryptionRequestPacket()
|
||||
@@ -43,7 +43,7 @@ class LoginReactorTest(unittest.TestCase):
|
||||
@mock.patch('minecraft.networking.connection.encryption')
|
||||
def test_encryption_offline_server(self, encrypt):
|
||||
connection = mock.MagicMock()
|
||||
connection.context = ConnectionContext(protocol_version=max_proto_ver)
|
||||
connection.context = ConnectionContext(protocol_version=latest_proto)
|
||||
reactor = LoginReactor(connection)
|
||||
|
||||
packet = clientbound.login.EncryptionRequestPacket()
|
||||
|
||||
Reference in new issue
Block a user