diff --git a/gameview.py b/gameview.py index afb24ab..66baed6 100644 --- a/gameview.py +++ b/gameview.py @@ -5,6 +5,8 @@ from ConfigParser import ConfigParser from pybass_constants import * from PyQt4 import QtGui, QtCore from functools import partial +from ctypes import create_string_buffer +from urllib2 import Request, urlopen import AOsocket import images @@ -1097,6 +1099,9 @@ class gui(QtGui.QWidget): self.wtcesfx = 0 self.guiltysfx = 0 self.notguiltysfx = 0 + + self.stream = 0 + self.download_thread = None def resetOffsets(self): self.pairoffset.setValue(0) @@ -2495,6 +2500,12 @@ class gui(QtGui.QWidget): if audio.handleisactive(self.music): audio.stophandle(self.music) audio.freehandle(self.music) + if self.stream: + del self.stream + + if self.download_thread: + self.download_thread.terminate() # Live dangerously + self.download_thread = None if exists(AOpath + 'sounds/music/' + mus): self.music = audio.loadhandle(False, AOpath + 'sounds/music/' + mus, 0, 0, BASS_SAMPLE_LOOP) @@ -2503,12 +2514,13 @@ class gui(QtGui.QWidget): elif ini.read_ini_bool("AO2XP.ini", "General", "download music", True): if mus.lower().startswith("http"): - self.music = audio.loadURLhandle(mus, 0, BASS_STREAM_BLOCK | BASS_SAMPLE_LOOP) - print "Trying to play", mus.lower() + #self.music = audio.loadURLhandle(mus, 0, BASS_STREAM_BLOCK | BASS_SAMPLE_LOOP) + self.music = audio.loadURLhandle(mus, 0, BASS_SAMPLE_LOOP) + print "[audio] Trying to play", mus.lower() else: for bucket in buckets: if not bucket: continue - print "music stream:", bucket+'base/sounds/music/' + mus.lower() + print "[audio] Music stream:", bucket+'base/sounds/music/' + mus.lower() self.music = audio.loadURLhandle(bucket+'base/sounds/music/' + mus.lower(), 0, BASS_STREAM_BLOCK | BASS_SAMPLE_LOOP) if self.music: break @@ -2516,13 +2528,26 @@ class gui(QtGui.QWidget): audio.sethandleattr(self.music, BASS_ATTRIB_VOL, self.musicslider.value() / 100.0) audio.playhandle(self.music, True) else: - print "Couldn't play music. Error", audio.getbasserror() + print "[audio] Couldn't play music. Error", audio.getbasserror() + # Here comes the evil HTTPS hack for XP systems + if mus.lower().startswith("https"): + print "[audio] Trying to download music with urllib2" + self.download_thread = DownloadThread(self, mus) + self.download_thread.finished_signal.connect(self.playDownloadedMusic) + self.download_thread.start() def stopMusic(self): if self.music: if audio.handleisactive(self.music): audio.stophandle(self.music) audio.freehandle(self.music) + + def playDownloadedMusic(self, file_length): + # Part of the evil HTTPS music download hack for XP systems + print "[audio] Done downloading; trying to play..." + self.music = audio.loadhandle(True, self.stream, 0, file_length, BASS_SAMPLE_LOOP) + audio.sethandleattr(self.music, BASS_ATTRIB_VOL, self.musicslider.value() / 100.0) + audio.playhandle(self.music, False) def startGame(self, tcp, playerlist, charlist, musiclist, background, evidence, areas, features=[], oocjoin=[], hplist=[], webAO_bucket=""): self.willDisconnect = False @@ -3077,3 +3102,50 @@ class TCP_Thread(QtCore.QThread): del network[0] #print "(PU) id: %s, type: %d, data: %s" % (network[0], int(network[1]), network[2]) self.updatePlayerList.emit(network[0], 1, int(network[1]), network[2].decode('utf-8')) + +class DownloadThread(QtCore.QThread): + # Part of the evil HTTPS music download hack for XP systems + finished_signal = QtCore.pyqtSignal(int) + + def __init__(self, caller, url): + super(DownloadThread, self).__init__() + self.caller = caller + self.url = url + + def run(self): + self.download(self.url) + + def download(self, url): + headers = { + 'User-Agent': 'AO2XP', + 'Accept': 'audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3, */*', + 'Accept-Encoding': 'gzip, deflate', + 'Accept-Language': 'en-US,en;q=0.9', + 'Connection': 'keep-alive', + 'Upgrade-Insecure-Requests': '1' + } + + request = Request(url, headers=headers) + request.get_method = lambda: 'HEAD' + response = urlopen(request) + file_length = int(response.headers.get('Content-Length', 0)) + + if file_length > 0: + request = Request(url, headers=headers) + response = urlopen(request) + stream = "" + + bytes_downloaded = 0 + buffer_size = 8192 + + while bytes_downloaded < file_length: + chunk = response.read(buffer_size) + if not chunk: + break + stream += chunk + bytes_downloaded += len(chunk) + #progress = int((bytes_downloaded / float(file_length)) * 100) + #print "[debug] progress: %d%%" % progress + + self.caller.stream = create_string_buffer(stream) + self.finished_signal.emit(file_length) \ No newline at end of file