mirror of
https://github.com/ammaraskar/pyCraft.git
synced 2024-12-23 00:57:47 +01:00
Minor cleanup, pep8 compliance
This commit is contained in:
parent
85112fba1d
commit
1982716f73
@ -1,15 +1,15 @@
|
||||
import urllib2
|
||||
import urllib
|
||||
import json
|
||||
|
||||
BASE_URL = 'https://authserver.mojang.com/'
|
||||
AGENT_INFO = {"name": "Minecraft", "version": 1}
|
||||
|
||||
|
||||
"""Class to hold responses from Yggdrasil
|
||||
"""
|
||||
class Response:
|
||||
class Response(object):
|
||||
"""Class to hold responses from Yggdrasil
|
||||
"""
|
||||
error = False
|
||||
payload = None
|
||||
|
||||
|
||||
def make_request(url, payload):
|
||||
@ -50,10 +50,11 @@ def make_request(url, payload):
|
||||
response.payload = json_response
|
||||
return response
|
||||
|
||||
"""Yet another container class, this time to hold login info since it'll probably
|
||||
be need to passed around a lot afterwards
|
||||
"""
|
||||
class LoginResponse:
|
||||
|
||||
class LoginResponse(object):
|
||||
"""Yet another container class, this time to hold login info since it'll probably
|
||||
be need to passed around a lot afterwards
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
@ -64,7 +65,7 @@ def login_to_minecraft(username, password):
|
||||
If there is an error, it will be accompanied with a `human_error` field.
|
||||
Otherwise `access_token`, `profile_id` and `username` fields will be present in the response.
|
||||
"""
|
||||
payload = { "username": username, "password": password, "agent": AGENT_INFO }
|
||||
payload = {"username": username, "password": password, "agent": AGENT_INFO}
|
||||
response = make_request(BASE_URL + "authenticate", payload)
|
||||
|
||||
login_response = LoginResponse()
|
||||
|
@ -1,13 +1,14 @@
|
||||
from packets import *
|
||||
from start import PROTOCOL_VERSION
|
||||
from collections import deque
|
||||
from threading import Lock
|
||||
from types import VarInt
|
||||
import threading
|
||||
import socket
|
||||
import time
|
||||
import select
|
||||
|
||||
from packets import *
|
||||
from start import PROTOCOL_VERSION
|
||||
from types import VarInt
|
||||
|
||||
|
||||
class Connection:
|
||||
"""This class represents a connection to a minecraft
|
||||
@ -62,7 +63,7 @@ class Connection:
|
||||
self._handshake()
|
||||
|
||||
def _connect(self):
|
||||
#Connect a socket to the server and create a file object from the socket
|
||||
# Connect a socket to the server and create a file object from the socket
|
||||
#The file object is used to read any and all data from the socket since it's "guaranteed"
|
||||
#to read the number of bytes specified, the socket itself will mostly be
|
||||
#used to write data upstream to the server
|
||||
@ -108,7 +109,7 @@ class NetworkingThread(threading.Thread):
|
||||
|
||||
# Read and react to as many as 50 packets
|
||||
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:
|
||||
num_packets += 1
|
||||
|
||||
@ -116,13 +117,12 @@ class NetworkingThread(threading.Thread):
|
||||
if num_packets >= 50:
|
||||
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)
|
||||
|
||||
|
||||
class PacketReactor:
|
||||
|
||||
state_name = None
|
||||
clientbound_packets = None
|
||||
|
||||
@ -131,7 +131,7 @@ class PacketReactor:
|
||||
def __init__(self, connection):
|
||||
self.connection = connection
|
||||
|
||||
def read_packet(self, socket, stream):
|
||||
def read_packet(self, stream):
|
||||
ready = select.select([self.connection.socket], [], [], 0.5)
|
||||
if ready[0]:
|
||||
length = VarInt.read(stream)
|
||||
@ -152,17 +152,16 @@ class PacketReactor:
|
||||
|
||||
|
||||
class HandshakeReactor(PacketReactor):
|
||||
|
||||
clientbound_packets = state_handshake_clientbound
|
||||
|
||||
|
||||
class StatusReactor(PacketReactor):
|
||||
|
||||
clientbound_packets = state_status_clientbound
|
||||
|
||||
def react(self, packet):
|
||||
if packet.name == "response":
|
||||
if packet.id == ResponsePacket.id:
|
||||
import json
|
||||
|
||||
print json.loads(packet.json_response)
|
||||
|
||||
ping_packet = PingPacket()
|
||||
|
@ -1,8 +1,9 @@
|
||||
from types import *
|
||||
from io import BytesIO
|
||||
|
||||
from types import *
|
||||
|
||||
class PacketBuffer:
|
||||
|
||||
class PacketBuffer(object):
|
||||
b = BytesIO()
|
||||
|
||||
def send(self, value):
|
||||
@ -11,11 +12,15 @@ class PacketBuffer:
|
||||
def get_writable(self):
|
||||
return self.b.getvalue()
|
||||
|
||||
class Packet:
|
||||
|
||||
class Packet(object):
|
||||
|
||||
name = "base"
|
||||
definition = []
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def read(self, file_object):
|
||||
for field in self.definition:
|
||||
for var_name, data_type in field.iteritems():
|
||||
@ -37,9 +42,8 @@ class Packet:
|
||||
|
||||
|
||||
# Handshake State
|
||||
#==============
|
||||
# ==============
|
||||
class HandShakePacket(Packet):
|
||||
|
||||
id = 0x00
|
||||
name = "handshake"
|
||||
definition = [
|
||||
@ -48,6 +52,7 @@ class HandShakePacket(Packet):
|
||||
{'server_port': UnsignedShort},
|
||||
{'next_state': VarInt}]
|
||||
|
||||
|
||||
state_handshake_clientbound = {
|
||||
|
||||
}
|
||||
@ -55,6 +60,7 @@ state_handshake_serverbound = {
|
||||
0x00: HandShakePacket
|
||||
}
|
||||
|
||||
|
||||
# Status State
|
||||
#==============
|
||||
class ResponsePacket(Packet):
|
||||
@ -63,28 +69,33 @@ class ResponsePacket(Packet):
|
||||
definition = [
|
||||
{'json_response': String}]
|
||||
|
||||
|
||||
class PingPacket(Packet):
|
||||
id = 0x01
|
||||
name = "ping"
|
||||
definition = [
|
||||
{'time': Long}]
|
||||
|
||||
|
||||
state_status_clientbound = {
|
||||
0x00: ResponsePacket,
|
||||
0x01: PingPacket
|
||||
}
|
||||
|
||||
|
||||
class RequestPacket(Packet):
|
||||
id = 0x00
|
||||
name = "request"
|
||||
definition = []
|
||||
|
||||
|
||||
class PingPacket(Packet):
|
||||
id = 0x01
|
||||
name = "ping"
|
||||
definition = [
|
||||
{'time': Long}]
|
||||
|
||||
|
||||
state_status_serverbound = {
|
||||
0x00: RequestPacket,
|
||||
0x01: PingPacket
|
||||
@ -93,14 +104,13 @@ state_status_serverbound = {
|
||||
# Login State
|
||||
#==============
|
||||
class DisconnectPacket(Packet):
|
||||
|
||||
id = 0x00
|
||||
name = "disconnect"
|
||||
definition = [
|
||||
{'json_data': String}]
|
||||
|
||||
class EncryptionRequestPacket(Packet):
|
||||
|
||||
class EncryptionRequestPacket(Packet):
|
||||
id = 0x01
|
||||
name = "encryption request"
|
||||
definition = [
|
||||
@ -108,35 +118,37 @@ class EncryptionRequestPacket(Packet):
|
||||
{'public_key': ByteArray},
|
||||
{'verify_token': ByteArray}]
|
||||
|
||||
class LoginSucessPacket(Packet):
|
||||
|
||||
class LoginSucessPacket(Packet):
|
||||
id = 0x02
|
||||
name = "login success"
|
||||
definition = [
|
||||
{'UUID': String},
|
||||
{'Username': String}]
|
||||
|
||||
|
||||
state_login_clientbound = {
|
||||
0x00: DisconnectPacket,
|
||||
0x01: EncryptionRequestPacket,
|
||||
0x02: LoginSucessPacket
|
||||
}
|
||||
|
||||
class LoginStartPacket(Packet):
|
||||
|
||||
class LoginStartPacket(Packet):
|
||||
id = 0x00
|
||||
name = "login start"
|
||||
definition = [
|
||||
{'name': String}]
|
||||
|
||||
class EncryptionResponsePacket(Packet):
|
||||
|
||||
class EncryptionResponsePacket(Packet):
|
||||
id = 0x01
|
||||
name = "encryption response"
|
||||
definition = [
|
||||
{'shared_secret': ByteArray},
|
||||
{'verify_token': ByteArray}]
|
||||
|
||||
|
||||
state_login_serverbound = {
|
||||
0x00: LoginStartPacket,
|
||||
0x01: EncryptionResponsePacket
|
||||
|
@ -4,8 +4,8 @@ These definitions and methods are used by the packet definitions
|
||||
"""
|
||||
import struct
|
||||
|
||||
class Type:
|
||||
|
||||
class Type:
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
pass
|
||||
@ -14,11 +14,11 @@ class Type:
|
||||
def send(value, socket):
|
||||
pass
|
||||
|
||||
#=========================================================
|
||||
|
||||
# =========================================================
|
||||
|
||||
|
||||
class Boolean(Type):
|
||||
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
return struct.unpack('?', file_object.read(1))[0]
|
||||
@ -29,7 +29,6 @@ class Boolean(Type):
|
||||
|
||||
|
||||
class Byte(Type):
|
||||
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
return struct.unpack('>b', file_object.read(1))[0]
|
||||
@ -40,7 +39,6 @@ class Byte(Type):
|
||||
|
||||
|
||||
class Short(Type):
|
||||
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
return struct.unpack('>h', file_object.read(2))[0]
|
||||
@ -51,7 +49,6 @@ class Short(Type):
|
||||
|
||||
|
||||
class UnsignedShort(Type):
|
||||
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
return struct.unpack('>H', file_object.read(2))[0]
|
||||
@ -62,7 +59,6 @@ class UnsignedShort(Type):
|
||||
|
||||
|
||||
class Integer(Type):
|
||||
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
return struct.unpack('>i', file_object.read(4))[0]
|
||||
@ -73,7 +69,6 @@ class Integer(Type):
|
||||
|
||||
|
||||
class VarInt(Type):
|
||||
|
||||
@staticmethod
|
||||
def read_socket(s):
|
||||
d = 0
|
||||
@ -107,7 +102,6 @@ class VarInt(Type):
|
||||
|
||||
|
||||
class Long(Type):
|
||||
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
return struct.unpack('>q', file_object.read(8))[0]
|
||||
@ -118,7 +112,6 @@ class Long(Type):
|
||||
|
||||
|
||||
class Float(Type):
|
||||
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
return struct.unpack('>f', file_object.read(4))[0]
|
||||
@ -127,8 +120,8 @@ class Float(Type):
|
||||
def send(svalue, socket):
|
||||
socket.send(struct.pack('>f', value))
|
||||
|
||||
class Double(Type):
|
||||
|
||||
class Double(Type):
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
return struct.unpack('>d', file_object.read(8))[0]
|
||||
@ -139,7 +132,6 @@ class Double(Type):
|
||||
|
||||
|
||||
class ByteArray(Type):
|
||||
|
||||
@staticmethod
|
||||
def read(file_object, length=None):
|
||||
if length is None:
|
||||
@ -153,7 +145,6 @@ class ByteArray(Type):
|
||||
|
||||
|
||||
class String(Type):
|
||||
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
length = VarInt.read(file_object)
|
||||
|
6
start.py
6
start.py
@ -1,10 +1,13 @@
|
||||
import getpass
|
||||
import sys
|
||||
from optparse import OptionParser
|
||||
|
||||
import authentication
|
||||
|
||||
|
||||
PROTOCOL_VERSION = 5
|
||||
|
||||
|
||||
def main():
|
||||
parser = OptionParser()
|
||||
|
||||
@ -19,7 +22,6 @@ def main():
|
||||
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
|
||||
if not options.username:
|
||||
options.username = raw_input("Enter your username: ")
|
||||
|
||||
@ -28,6 +30,7 @@ def main():
|
||||
|
||||
login_response = authentication.login_to_minecraft(options.username, options.password)
|
||||
from pprint import pprint # TODO: remove debug
|
||||
|
||||
pprint(vars(login_response)) # TODO: remove debug
|
||||
|
||||
if login_response.error:
|
||||
@ -48,6 +51,7 @@ def main():
|
||||
port = 25565
|
||||
|
||||
from network.connection import Connection
|
||||
|
||||
connection = Connection(address, port, login_response)
|
||||
connection.status()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user