Minor cleanup, pep8 compliance

This commit is contained in:
Ammar Askar committed 2014-10-08 22:12:37 +05:00
1 parent 85112fba1d
commit 1982716f73
5 files changed
+42 -35

No files matched your search

+4 -3
View File
@@ -1,15 +1,15 @@
import urllib2 import urllib2
import urllib
import json import json
BASE_URL = 'https://authserver.mojang.com/' BASE_URL = 'https://authserver.mojang.com/'
AGENT_INFO = {"name": "Minecraft", "version": 1} AGENT_INFO = {"name": "Minecraft", "version": 1}
class Response(object):
"""Class to hold responses from Yggdrasil """Class to hold responses from Yggdrasil
""" """
class Response:
error = False error = False
payload = None
def make_request(url, payload): def make_request(url, payload):
@@ -50,10 +50,11 @@ def make_request(url, payload):
response.payload = json_response response.payload = json_response
return response return response
class LoginResponse(object):
"""Yet another container class, this time to hold login info since it'll probably """Yet another container class, this time to hold login info since it'll probably
be need to passed around a lot afterwards be need to passed around a lot afterwards
""" """
class LoginResponse:
pass pass
+9 -10
View File
@@ -1,13 +1,14 @@
from packets import *
from start import PROTOCOL_VERSION
from collections import deque from collections import deque
from threading import Lock from threading import Lock
from types import VarInt
import threading import threading
import socket import socket
import time import time
import select import select
from packets import *
from start import PROTOCOL_VERSION
from types import VarInt
class Connection: class Connection:
"""This class represents a connection to a minecraft """This class represents a connection to a minecraft
@@ -108,7 +109,7 @@ class NetworkingThread(threading.Thread):
# Read and react to as many as 50 packets # Read and react to as many as 50 packets
num_packets = 0 num_packets = 0
packet = self.connection.reactor.read_packet(self.connection.socket, self.connection.file_object) packet = self.connection.reactor.read_packet(self.connection.file_object)
while packet: while packet:
num_packets += 1 num_packets += 1
@@ -116,13 +117,12 @@ class NetworkingThread(threading.Thread):
if num_packets >= 50: if num_packets >= 50:
break break
packet = self.connection.reactor.read_packet(self.connection.socket, self.connection.file_object) packet = self.connection.reactor.read_packet(self.connection.file_object)
time.sleep(0.05) time.sleep(0.05)
class PacketReactor: class PacketReactor:
state_name = None state_name = None
clientbound_packets = None clientbound_packets = None
@@ -131,7 +131,7 @@ class PacketReactor:
def __init__(self, connection): def __init__(self, connection):
self.connection = connection self.connection = connection
def read_packet(self, socket, stream): def read_packet(self, stream):
ready = select.select([self.connection.socket], [], [], 0.5) ready = select.select([self.connection.socket], [], [], 0.5)
if ready[0]: if ready[0]:
length = VarInt.read(stream) length = VarInt.read(stream)
@@ -152,17 +152,16 @@ class PacketReactor:
class HandshakeReactor(PacketReactor): class HandshakeReactor(PacketReactor):
clientbound_packets = state_handshake_clientbound clientbound_packets = state_handshake_clientbound
class StatusReactor(PacketReactor): class StatusReactor(PacketReactor):
clientbound_packets = state_status_clientbound clientbound_packets = state_status_clientbound
def react(self, packet): def react(self, packet):
if packet.name == "response": if packet.id == ResponsePacket.id:
import json import json
print json.loads(packet.json_response) print json.loads(packet.json_response)
ping_packet = PingPacket() ping_packet = PingPacket()
+21 -9
View File
@@ -1,8 +1,9 @@
from types import *
from io import BytesIO from io import BytesIO
from types import *
class PacketBuffer:
class PacketBuffer(object):
b = BytesIO() b = BytesIO()
def send(self, value): def send(self, value):
@@ -11,11 +12,15 @@ class PacketBuffer:
def get_writable(self): def get_writable(self):
return self.b.getvalue() return self.b.getvalue()
class Packet:
class Packet(object):
name = "base" name = "base"
definition = [] definition = []
def __init__(self):
pass
def read(self, file_object): def read(self, file_object):
for field in self.definition: for field in self.definition:
for var_name, data_type in field.iteritems(): for var_name, data_type in field.iteritems():
@@ -39,7 +44,6 @@ class Packet:
# Handshake State # Handshake State
# ============== # ==============
class HandShakePacket(Packet): class HandShakePacket(Packet):
id = 0x00 id = 0x00
name = "handshake" name = "handshake"
definition = [ definition = [
@@ -48,6 +52,7 @@ class HandShakePacket(Packet):
{'server_port': UnsignedShort}, {'server_port': UnsignedShort},
{'next_state': VarInt}] {'next_state': VarInt}]
state_handshake_clientbound = { state_handshake_clientbound = {
} }
@@ -55,6 +60,7 @@ state_handshake_serverbound = {
0x00: HandShakePacket 0x00: HandShakePacket
} }
# Status State # Status State
#============== #==============
class ResponsePacket(Packet): class ResponsePacket(Packet):
@@ -63,28 +69,33 @@ class ResponsePacket(Packet):
definition = [ definition = [
{'json_response': String}] {'json_response': String}]
class PingPacket(Packet): class PingPacket(Packet):
id = 0x01 id = 0x01
name = "ping" name = "ping"
definition = [ definition = [
{'time': Long}] {'time': Long}]
state_status_clientbound = { state_status_clientbound = {
0x00: ResponsePacket, 0x00: ResponsePacket,
0x01: PingPacket 0x01: PingPacket
} }
class RequestPacket(Packet): class RequestPacket(Packet):
id = 0x00 id = 0x00
name = "request" name = "request"
definition = [] definition = []
class PingPacket(Packet): class PingPacket(Packet):
id = 0x01 id = 0x01
name = "ping" name = "ping"
definition = [ definition = [
{'time': Long}] {'time': Long}]
state_status_serverbound = { state_status_serverbound = {
0x00: RequestPacket, 0x00: RequestPacket,
0x01: PingPacket 0x01: PingPacket
@@ -93,14 +104,13 @@ state_status_serverbound = {
# Login State # Login State
#============== #==============
class DisconnectPacket(Packet): class DisconnectPacket(Packet):
id = 0x00 id = 0x00
name = "disconnect" name = "disconnect"
definition = [ definition = [
{'json_data': String}] {'json_data': String}]
class EncryptionRequestPacket(Packet):
class EncryptionRequestPacket(Packet):
id = 0x01 id = 0x01
name = "encryption request" name = "encryption request"
definition = [ definition = [
@@ -108,35 +118,37 @@ class EncryptionRequestPacket(Packet):
{'public_key': ByteArray}, {'public_key': ByteArray},
{'verify_token': ByteArray}] {'verify_token': ByteArray}]
class LoginSucessPacket(Packet):
class LoginSucessPacket(Packet):
id = 0x02 id = 0x02
name = "login success" name = "login success"
definition = [ definition = [
{'UUID': String}, {'UUID': String},
{'Username': String}] {'Username': String}]
state_login_clientbound = { state_login_clientbound = {
0x00: DisconnectPacket, 0x00: DisconnectPacket,
0x01: EncryptionRequestPacket, 0x01: EncryptionRequestPacket,
0x02: LoginSucessPacket 0x02: LoginSucessPacket
} }
class LoginStartPacket(Packet):
class LoginStartPacket(Packet):
id = 0x00 id = 0x00
name = "login start" name = "login start"
definition = [ definition = [
{'name': String}] {'name': String}]
class EncryptionResponsePacket(Packet):
class EncryptionResponsePacket(Packet):
id = 0x01 id = 0x01
name = "encryption response" name = "encryption response"
definition = [ definition = [
{'shared_secret': ByteArray}, {'shared_secret': ByteArray},
{'verify_token': ByteArray}] {'verify_token': ByteArray}]
state_login_serverbound = { state_login_serverbound = {
0x00: LoginStartPacket, 0x00: LoginStartPacket,
0x01: EncryptionResponsePacket 0x01: EncryptionResponsePacket
+3 -12
View File
@@ -4,8 +4,8 @@ These definitions and methods are used by the packet definitions
""" """
import struct import struct
class Type:
class Type:
@staticmethod @staticmethod
def read(file_object): def read(file_object):
pass pass
@@ -14,11 +14,11 @@ class Type:
def send(value, socket): def send(value, socket):
pass pass
# ========================================================= # =========================================================
class Boolean(Type): class Boolean(Type):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
return struct.unpack('?', file_object.read(1))[0] return struct.unpack('?', file_object.read(1))[0]
@@ -29,7 +29,6 @@ class Boolean(Type):
class Byte(Type): class Byte(Type):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
return struct.unpack('>b', file_object.read(1))[0] return struct.unpack('>b', file_object.read(1))[0]
@@ -40,7 +39,6 @@ class Byte(Type):
class Short(Type): class Short(Type):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
return struct.unpack('>h', file_object.read(2))[0] return struct.unpack('>h', file_object.read(2))[0]
@@ -51,7 +49,6 @@ class Short(Type):
class UnsignedShort(Type): class UnsignedShort(Type):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
return struct.unpack('>H', file_object.read(2))[0] return struct.unpack('>H', file_object.read(2))[0]
@@ -62,7 +59,6 @@ class UnsignedShort(Type):
class Integer(Type): class Integer(Type):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
return struct.unpack('>i', file_object.read(4))[0] return struct.unpack('>i', file_object.read(4))[0]
@@ -73,7 +69,6 @@ class Integer(Type):
class VarInt(Type): class VarInt(Type):
@staticmethod @staticmethod
def read_socket(s): def read_socket(s):
d = 0 d = 0
@@ -107,7 +102,6 @@ class VarInt(Type):
class Long(Type): class Long(Type):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
return struct.unpack('>q', file_object.read(8))[0] return struct.unpack('>q', file_object.read(8))[0]
@@ -118,7 +112,6 @@ class Long(Type):
class Float(Type): class Float(Type):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
return struct.unpack('>f', file_object.read(4))[0] return struct.unpack('>f', file_object.read(4))[0]
@@ -127,8 +120,8 @@ class Float(Type):
def send(svalue, socket): def send(svalue, socket):
socket.send(struct.pack('>f', value)) socket.send(struct.pack('>f', value))
class Double(Type):
class Double(Type):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
return struct.unpack('>d', file_object.read(8))[0] return struct.unpack('>d', file_object.read(8))[0]
@@ -139,7 +132,6 @@ class Double(Type):
class ByteArray(Type): class ByteArray(Type):
@staticmethod @staticmethod
def read(file_object, length=None): def read(file_object, length=None):
if length is None: if length is None:
@@ -153,7 +145,6 @@ class ByteArray(Type):
class String(Type): class String(Type):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
length = VarInt.read(file_object) length = VarInt.read(file_object)
+5 -1
View File
@@ -1,10 +1,13 @@
import getpass import getpass
import sys import sys
from optparse import OptionParser from optparse import OptionParser
import authentication import authentication
PROTOCOL_VERSION = 5 PROTOCOL_VERSION = 5
def main(): def main():
parser = OptionParser() parser = OptionParser()
@@ -19,7 +22,6 @@ def main():
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if not options.username: if not options.username:
options.username = raw_input("Enter your username: ") options.username = raw_input("Enter your username: ")
@@ -28,6 +30,7 @@ def main():
login_response = authentication.login_to_minecraft(options.username, options.password) login_response = authentication.login_to_minecraft(options.username, options.password)
from pprint import pprint # TODO: remove debug from pprint import pprint # TODO: remove debug
pprint(vars(login_response)) # TODO: remove debug pprint(vars(login_response)) # TODO: remove debug
if login_response.error: if login_response.error:
@@ -48,6 +51,7 @@ def main():
port = 25565 port = 25565
from network.connection import Connection from network.connection import Connection
connection = Connection(address, port, login_response) connection = Connection(address, port, login_response)
connection.status() connection.status()