-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The packet for the current version of Server List Ping are now build …
…using BasePacket class
- Loading branch information
Showing
6 changed files
with
156 additions
and
107 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from ...common.packet import BasePacket | ||
from ...common.types import * | ||
from .types import * | ||
|
||
class SLPCurrentHandshakePacket(BasePacket): | ||
packet_id: int = VarInt(default=0x00) | ||
proto_version: int = VarInt() | ||
server_address: str = String() | ||
server_port: int = BigEndianShort(signed=False) | ||
next_state: int = VarInt() | ||
|
||
class SLPCurrentRequestPacket(BasePacket): | ||
packet_id: int = VarInt(default=0x00) | ||
|
||
class SLPCurrentResponsePacket(BasePacket): | ||
packet_id: int = VarInt() | ||
response: dict = JSONString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from ...common.packet import BasePacket | ||
from ...common.types import * | ||
from .types import * | ||
|
||
class SLPPingPacket(BasePacket): | ||
packet_id: int = VarInt(default=0x01) | ||
payload: int = BigEndianInt64() | ||
|
||
class SLPPongPacket(BasePacket): | ||
packet_id: int = VarInt() | ||
payload: int = BigEndianInt64() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from typing import IO | ||
import struct | ||
import json | ||
from ...common.types import Type, BigEndianShort | ||
|
||
## VarTypes ## | ||
class VarType(Type): | ||
_python_type = int | ||
|
||
def __new__(cls, default=0, **kwargs): | ||
return super().__new__(cls, default=default, **kwargs) | ||
|
||
def __init_subclass__(cls, nbytes: int): | ||
cls.__nbytes = nbytes | ||
|
||
def check_value(self, value: int): | ||
if not isinstance(value, int): | ||
raise TypeError('Wrong type, got `{}` expect `int`'.format(type(value).__name__)) | ||
|
||
@property | ||
def size(self): | ||
return self.__nbytes | ||
|
||
def read(self, buffer: IO): | ||
val = 0 | ||
|
||
for i in range(self.__nbytes): | ||
byte = buffer.read(1) | ||
if len(byte) == 0: | ||
break | ||
|
||
val |= (ord(byte) & 0x7f) << (7*i) | ||
|
||
if ord(byte) & 0x80 == 0: | ||
break | ||
|
||
return val | ||
|
||
def write(self, value: int, buffer: IO): | ||
while True: | ||
byte = value & 0x7f | ||
value >>= 7 | ||
|
||
if value > 0: | ||
byte |= 0x80 | ||
|
||
buffer.write(struct.pack('B', byte)) | ||
|
||
if value == 0: | ||
break | ||
|
||
|
||
class VarInt(VarType, nbytes=5): | ||
pass | ||
|
||
class VarLong(VarType, nbytes=10): | ||
pass | ||
|
||
## Other ## | ||
class String(Type): | ||
_python_type = str | ||
|
||
def __new__(cls, default='', encoding='utf-8', **kwargs): | ||
obj = super().__new__(cls, default=default, **kwargs) | ||
obj.__encoding = encoding | ||
return obj | ||
|
||
@property | ||
def encoding(self): | ||
return self.__encoding | ||
|
||
def check_value(self, value: str): | ||
if not isinstance(value, str): | ||
raise TypeError('Wrong type, got `{}` expect `str`'.format(type(value).__name__)) | ||
|
||
def read(self, buffer: IO): | ||
length = VarInt().read(buffer) | ||
return buffer.read(length).decode(self.__encoding) | ||
|
||
def write(self, value: str, buffer: IO): | ||
self.check_value(value) | ||
VarInt().write(len(value), buffer) | ||
buffer.write(value.encode(self.__encoding)) | ||
|
||
class JSONString(String): | ||
_python_type = dict | ||
|
||
def read(self, buffer: IO): | ||
return json.loads(super().read(buffer)) | ||
|
||
def write(self, value: dict, buffer: IO): | ||
text = json.dumps(value) | ||
super().write(text, buffer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.