Fixes and stuff

This commit is contained in:
Ammar Askar 2012-07-17 22:42:42 +01:00
parent 8d55cec884
commit 22c19334b3
4 changed files with 207 additions and 63 deletions

185
GUI.py
View File

@ -5,10 +5,15 @@ import start
import threading
import time
import NetworkManager
import PacketSenderManager
import thread
from threading import Lock
#pydev error fix
wx=wx
connection = None
class MainFrame(wx.Frame):
def __init__(self, *args, **kwds):
@ -17,11 +22,9 @@ class MainFrame(wx.Frame):
self.Freeze()
self.LoginPanel = LoginFrame(self)
self.LoginPanel.Show()
self.ConnectPanel = ServerPanel(self)
self.ConnectPanel.Hide()
self.ChatPanel = ServerChatPanel(self)
self.ChatPanel.Hide()
self.LoginPanel.Show()
self.SetSize((520, 310))
self.SetTitle("pyCraft")
self.Thaw()
@ -30,6 +33,7 @@ class MainFrame(wx.Frame):
self.socket = socket
def showConnectionPanel(self):
#self.LoginPanel.Destroy()
self.LoginPanel.Hide()
self.SetMinSize((600, 310))
self.SetSize((600, 310))
@ -38,14 +42,22 @@ class MainFrame(wx.Frame):
self.ConnectPanel.Layout()
def showChatPanel(self):
self.ChatPanel.Hide()
self.Hide()
#self.app = wx.PySimpleApp()
#self.ChatPanel = ServerChatPanel(None, -1, "pyCraft")
#self.ConnectPanel.connection.setWindow(ChatPanel)
#self.app.SetTopWindow(self.ChatPanel)
#self.ChatPanel.Show()
#self.app.MainLoop()
self.Destroy()
"""
self.ConnectPanel.Hide()
self.LoginPanel.Hide()
self.SetSize((600, 450))
self.SetMinSize((600, 450))
self.SetSize((600, 450))
self.ChatPanel.Show()
self.Layout()
self.ChatPanel.Layout()
"""
class LoginFrame(wx.Panel):
@ -82,12 +94,12 @@ class LoginFrame(wx.Panel):
self.LoginStaticText.SetBackgroundColour(wx.Colour(171, 171, 171))
self.LoginStaticText.SetFont(wx.Font(65, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Minecraft"))
self.UsernameStaticText.SetBackgroundColour(wx.Colour(171, 171, 171))
self.UsernameStaticText.SetFont(wx.Font(14, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Lucida Sans"))
self.UsernameStaticText.SetFont(wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Lucida Sans"))
self.UsernameEntry.SetMinSize((150, 25))
self.UsernameEntry.SetFont(wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, ""))
self.PasswordStaticText.SetMinSize((87, 35))
self.PasswordStaticText.SetBackgroundColour(wx.Colour(171, 171, 171))
self.PasswordStaticText.SetFont(wx.Font(14, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Lucida Sans"))
self.PasswordStaticText.SetFont(wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Lucida Sans"))
self.PasswordEntry.SetMinSize((150, 25))
self.PasswordEntry.SetFont(wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, ""))
self.LoginButton.SetMinSize((80, 32))
@ -218,6 +230,7 @@ class ServerPanel(wx.Panel):
port = int(StuffEnteredIntoBox[1])
else:
port = 25565
global connection
connection = NetworkManager.ServerConnection(self.parent, self.parent.username, "", self.parent.sessionID, host, port)
connection.start()
self.Status.SetLabel("Connecting.")
@ -228,58 +241,68 @@ class ServerPanel(wx.Panel):
self.RotationThread.Kill = True
self.parent.showChatPanel()
class ServerChatPanel(wx.Panel):
class ServerChatPanel(wx.Frame):
def __init__(self, parent):
# begin wxGlade: wxScrolledPanel.__init__
wx.Panel.__init__(self, parent=parent)
self.parent = parent
def __init__(self, *args, **kwds):
self.lock = Lock()
self.lock.acquire()
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.SetMinSize((600, 450))
self.SetSize((600, 450))
self.Status = wx.StaticText(self, -1, "HERP DERP TEST STUFF")
self.messageEntry = wx.TextCtrl(self, -1, "", style=wx.EXPAND|wx.TE_PROCESS_ENTER)
self.messageEntry.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
self.sendButton = wx.Button(self, -1, "&Send", size=(100, wx.Button.GetDefaultSize()[1]))
self.sendButton.SetMinSize((100, wx.Button.GetDefaultSize()[1]))
self.sendButton.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE))
"""
self.ChatPanel = scrolled.ScrolledPanel(self, 1, size=(self.parent.GetSize()[0], self.parent.GetSize()[1] - 40),
style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER|wx.EXPAND|wx.ALL, name="ChatPanel" )
self.ChatPanelSizer = wx.BoxSizer(wx.VERTICAL)
"""
self.__set_properties()
self.__do_layout()
self.set_properties()
self.do_layout()
self.locked = True
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ICONIZE, self.OnPaint)
self.text.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
self.messageEntry.Bind(wx.EVT_TEXT_ENTER, self.sendMessage)
self.SetAutoLayout(True)
self.text.BeginFontSize(12)
#self.Bind(wx.EVT_PAINT, self.OnPaint)
#self.Bind(wx.EVT_ICONIZE, self.iconize)
#self.text.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
self.sendButton.Bind(wx.EVT_BUTTON, self.sendMessage)
self.messageEntry.Bind(wx.EVT_TEXT_ENTER, self.sendMessage)
#self.text.Bind(wx.EVT_LEFT_DOWN, self.clickCanceller)
self.text.BeginFontSize(10)
self.text.Scroll(0, self.text.GetScrollRange(wx.VERTICAL))
global connection
connection.setWindow(self)
self.lock.release()
# end wxGlade
def __set_properties(self):
def set_properties(self):
# begin wxGlade: ServerConnectionPanel.__set_properties
self.SetBackgroundColour(wx.Colour(171, 171, 171))
self.Status.SetBackgroundColour(wx.Colour(171, 171, 171))
self.Status.SetFont(wx.Font(14, wx.MODERN, wx.NORMAL, wx.BOLD, 0, "Minecraft"))
self.messageEntry.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
self.sendButton.SetMinSize((100, wx.Button.GetDefaultSize()[1]))
self.sendButton.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE))
#self.ChatPanel.SetupScrolling()
# end wxGlade
def __do_layout(self):
def do_layout(self):
# begin wxGlade: ServerConnectionPanel.__do_layout
self.SetSizeHints(-1,self.GetSize().y,-1,self.GetSize().y );
#self.SetSizeHints(-1,self.GetSize().y,-1,self.GetSize().y );
self.RootSizer = wx.BoxSizer(wx.VERTICAL)
#sizer_4 = wx.BoxSizer(wx.VERTICAL)
self.sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
self.RootSizer.Add(self.Status, 0, wx.LEFT, 6)
#self.RootSizer.Add(self.ChatPanel, 1, wx.EXPAND|wx.ALL)
self.hbox5 = wx.BoxSizer(wx.HORIZONTAL)
self.text = rt.RichTextCtrl(self, 1, style=wx.VSCROLL|wx.EXPAND|wx.SUNKEN_BORDER|wx.ALL, size=(self.parent.GetSize()[0] - 20, self.parent.GetSize()[1] - 100))
self.text = rt.RichTextCtrl(self, 1, style=wx.VSCROLL|wx.EXPAND|wx.SUNKEN_BORDER|wx.ALL, size=(self.GetSize()[0] - 20, self.GetSize()[1] - 100))
self.RootSizer.Add(self.text, 1, wx.ALL|wx.EXPAND, 3)
self.RootSizer.Fit(self)
self.RootSizer.Add(self.sizer_6, 0, wx.EXPAND, 0)
@ -291,6 +314,9 @@ class ServerChatPanel(wx.Panel):
self.Layout()
# end wxGlade
def clickCanceller(self, event):
pass
def OnScroll(self, event):
event.Skip()
if(self.text.GetScrollRange(wx.VERTICAL) - self.text.GetScrollPos(wx.VERTICAL) == 81):
@ -299,17 +325,110 @@ class ServerChatPanel(wx.Panel):
self.locked = False
def OnPaint(self, event):
self.text.SetSize((self.parent.GetSize()[0] - 20, self.parent.GetSize()[1] - 100))
self.text.SetSize((self.GetSize()[0] - 20, self.GetSize()[1] - 100))
if(self.locked):
self.text.Scroll(0, self.text.GetScrollRange(wx.VERTICAL))
wx.PaintDC(self)
wx.PaintDC(self)
def iconize(self, event):
self.text.SetSize((self.GetSize()[0] - 20, self.GetSize()[1] - 100))
if(self.locked):
self.text.Scroll(0, self.text.GetScrollRange(wx.VERTICAL))
def sendMessage(self, event):
if(self.messageEntry.GetValue() == ""):
self.Status.SetLabel("No message entered QQ")
return False
thread.start_new_thread(PacketSenderManager.send03, (connection.grabSocket(), self.messageEntry.GetValue()))
self.messageEntry.SetValue("")
def handleChat(self, message):
pass
self.lock.acquire()
self.text.BeginFontSize(10)
"""
if(u'\xa7' not in message):
self.text.WriteText(message)
self.text.Newline()
self.text.BeginTextColour('#000000')
self.lock.release()
return
"""
message2 = message
message2 = message2.split(u'\xa7')
first = True
for part in message2:
if(first == True):
first = False
self.text.WriteText(part)
continue
if(part.__len__() == 0):
continue
if(message2.__len__() == 1):
self.text.WriteText(message)
continue
colourcode = part[0]
stringpart = part[1:]
if(colourcode == "0"):
self.text.BeginTextColour('#e3dde1')
elif(colourcode == "1"):
self.text.BeginTextColour('#0000aa')
elif(colourcode == "2"):
self.text.BeginTextColour('#00aa00')
elif(colourcode == "3"):
self.text.BeginTextColour('#00aaaa')
elif(colourcode == "4"):
self.text.BeginTextColour('#aa0000')
elif(colourcode == "5"):
self.text.BeginTextColour('#aa00aa')
elif(colourcode == "6"):
self.text.BeginTextColour('#ffaa00')
elif(colourcode == "7"):
self.text.BeginTextColour('#aaaaaa')
elif(colourcode == "8"):
self.text.BeginTextColour('#555555')
elif(colourcode == "9"):
self.text.BeginTextColour('#5555ff')
elif(colourcode == "a"):
self.text.BeginTextColour('#55ff55')
elif(colourcode == "b"):
self.text.BeginTextColour('#55ffff')
elif(colourcode == "c"):
self.text.BeginTextColour('#ff5555')
elif(colourcode == "d"):
self.text.BeginTextColour('#ff55ff')
elif(colourcode == "e"):
self.text.BeginTextColour('#E6E222')
elif(colourcode == "f"):
self.text.BeginTextColour('#000000')
elif(colourcode == "k"):
self.text.BeginUnderline()
elif(colourcode == "l"):
self.text.BeginBold()
elif(colourcode == "m"):
self.text.BeginUnderline()
elif(colourcode == "n"):
self.text.BeginUnderline()
elif(colourcode == "o"):
self.text.BeginItalic()
elif(colourcode == "r"):
self.text.EndItalic()
self.text.EndUnderline()
self.text.EndBold()
"""
else:
self.text.WriteText(u'\xa7')
self.text.WriteText(colourcode)
"""
self.text.WriteText(stringpart)
self.text.EndItalic()
self.text.EndUnderline()
self.text.EndBold()
self.text.BeginTextColour('#000000')
#if(self.locked):
self.text.Scroll(0, self.text.GetScrollRange(wx.VERTICAL))
self.text.Newline()
self.lock.release()
# end of class wxScrolledPanel

View File

@ -5,6 +5,7 @@ import urllib2
import traceback
import threading
import struct
from sys import stdout
#Eclipse pyDev error fix
wx=wx
@ -26,6 +27,9 @@ class ServerConnection(threading.Thread):
self.NoGUI = False
self.window = window
def setWindow(self, window):
self.window = window
def grabSocket(self):
return self.socket
@ -39,26 +43,36 @@ class ServerConnection(threading.Thread):
response = PacketListenerManager.handle02(self.FileObject)
else:
print "Server responded with a malformed packet"
pass
return False
serverid = response
if(serverid != '-'):
url = "http://session.minecraft.net/game/joinserver.jsp?user=" + self.username + "&sessionId=" + self.sessionID + "&serverId=" + serverid
response = urllib2.urlopen(url).read()
if(response != "OK"):
try:
url = "http://session.minecraft.net/game/joinserver.jsp?user=" + self.username + "&sessionId=" + self.sessionID + "&serverId=" + serverid
response = urllib2.urlopen(url).read()
if(response != "OK"):
if(self.NoGUI == False):
self.window.ConnectPanel.Status.SetFont(wx.Font(15, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Minecraft"))
self.window.ConnectPanel.Status.SetLabel("Response from sessions.minecraft.net wasn't OK")
else:
print "Response from sessions.minecraft.net wasn't OK, it was " + response
return False
PacketListenerManager.sendLoginRequest(self.socket, self.username)
if(self.NoGUI == False):
self.window.ConnectPanel.Status.SetFont(wx.Font(15, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Minecraft"))
self.window.ConnectPanel.Status.SetLabel("Response from sessions.minecraft.net wasn't OK")
else:
print "Response from sessions.minecraft.net wasn't OK, it was " + response
return False
PacketListenerManager.sendLoginRequest(self.socket, self.username)
PacketListener(self, self.window, self.socket, self.FileObject).start()
self.window.ConnectPanel.callbackAfterConnect()
while(not hasattr(self.window, 'text')):
continue
PacketListener(self, self.window, self.socket, self.FileObject).start()
except Exception, e:
if(self.NoGUI == False and hasattr(self.window, 'ConnectPanel')):
self.window.ConnectPanel.Status.SetForegroundColour(wx.RED)
self.window.ConnectPanel.Status.SetLabel("Conection to sessions.mc.net failed")
self.window.ConnectPanel.RotationThread.Kill = True
else:
if(self.NoGUI):
print "Server is in offline mode"
PacketListenerManager.sendLoginRequest(self.socket, self.username)
except Exception, e:
if(self.NoGUI == False):
if(self.NoGUI == False and self.window):
self.window.ConnectPanel.Status.SetForegroundColour(wx.RED)
self.window.ConnectPanel.Status.SetLabel("Connection to server failed")
self.window.ConnectPanel.RotationThread.Kill = True
@ -66,7 +80,7 @@ class ServerConnection(threading.Thread):
print "Connection to server failed"
traceback.print_exc()
return False
self.window.ConnectPanel.callbackAfterConnect()
#self.window.Status.SetLabel("Connected to " + self.server + "!")
class PacketListener(threading.Thread):
@ -82,12 +96,13 @@ class PacketListener(threading.Thread):
while True:
try:
response = self.socket.recv(1)
except socket.timeout, e:
if(self.NoGUI == False):
self.window.connectStatus.SetLabel("Ping timeout")
except Exception, e:
if(self.window):
self.window.Status.SetLabel("Ping timeout")
else:
print "Ping timeout"
break
#stdout.write((hex(ord(response[0]))) + ": ")
if(response[0] == "\x00"):
PacketListenerManager.handle00(self.FileObject, self.socket)
if(response[0] == "\x01"):
@ -96,6 +111,9 @@ class PacketListener(threading.Thread):
message = PacketListenerManager.handle03(self.FileObject)
if(self.connection.NoGUI):
print message.replace(u'\xa7', '&')
elif(self.window):
self.window.handleChat(message)
print message
if(response[0] == "\x04"):
PacketListenerManager.handle04(self.FileObject)
if(response[0] == "\x05"):
@ -206,7 +224,11 @@ class PacketListener(threading.Thread):
DisconMessage = PacketListenerManager.handleFF(self.FileObject)
if(self.window == None):
print "Disconnected: " + DisconMessage
else:
self.window.ChatPanel.Status.SetLabel("Disconnected: " + DisconMessage)
elif(self.window):
if(hasattr(self.window, 'ChatPanel')):
self.window.ChatPanel.Status.SetLabel("Disconnected: " + DisconMessage)
elif(hasattr(self.window, 'Status')):
self.window.Status.SetLabel("Disconnected: " + DisconMessage)
self.socket.close()
break

View File

@ -256,10 +256,10 @@ def handle18(FileObject):
val["id"] = struct.unpack('!h', FileObject.read(2))[0]
val["count"] = struct.unpack('!b', FileObject.read(1))[0]
val["damage"] = struct.unpack('!h', FileObject.read(2))[0]
if ty == 6:
val = []
for i in range(3):
val.append(struct.unpack('!i', FileObject.read(4))[0])
if ty == 6:
val = []
for i in range(3):
val.append(struct.unpack('!i', FileObject.read(4))[0])
metadata[index] = (ty, val)
x = struct.unpack('!b', FileObject.read(1))[0]
return {'EntityID' : EntityID,
@ -412,10 +412,10 @@ def handle28(FileObject):
val["id"] = struct.unpack('!h', FileObject.read(2))[0]
val["count"] = struct.unpack('!b', FileObject.read(1))[0]
val["damage"] = struct.unpack('!h', FileObject.read(2))[0]
if ty == 6:
val = []
for i in range(3):
val.append(struct.unpack('!i', FileObject.read(4))[0])
if ty == 6:
val = []
for i in range(3):
val.append(struct.unpack('!i', FileObject.read(4))[0])
metadata[index] = (ty, val)
x = struct.unpack('!b', FileObject.read(1))[0]
return {'EntityID' : EntityID,

View File

@ -11,12 +11,11 @@ import GUI
noGoooeees = False
try:
import wx
wx = wx
except ImportError:
print "wxPython not found, falling back to no gui version"
noGoooeees = True
#pydev error fix
wx = wx
class MinecraftLoginThread(threading.Thread):
def __init__(self, window, rotationthread, username, password):
@ -124,4 +123,8 @@ if __name__ == "__main__":
app.SetTopWindow(Login)
Login.Show()
app.MainLoop()
app2 = wx.PySimpleApp()
ChatPanel = GUI.ServerChatPanel(None, -1, "pyCraft")
app2.SetTopWindow(ChatPanel)
ChatPanel.Show()
app2.MainLoop()