From 128e37e06a7d0e5522e3f623903c795b888c6beb Mon Sep 17 00:00:00 2001 From: cuu Date: Sun, 15 Dec 2019 16:03:25 +0000 Subject: [PATCH] preview game screenshot --- Menu/GameShell/21_Warehouse/__init__.py | 159 ++++++++++++++++++++---- sys.py/UI/download.py | 56 +++++---- sys.py/appinstaller.py | 2 +- 3 files changed, 169 insertions(+), 48 deletions(-) diff --git a/Menu/GameShell/21_Warehouse/__init__.py b/Menu/GameShell/21_Warehouse/__init__.py index 587175f..b76fd97 100644 --- a/Menu/GameShell/21_Warehouse/__init__.py +++ b/Menu/GameShell/21_Warehouse/__init__.py @@ -27,6 +27,8 @@ from UI.info_page_selector import InfoPageSelector from UI.yes_cancel_confirm_page import YesCancelConfirmPage from UI.keyboard import Keyboard +from UI.download import Download + import config class RPCStack: @@ -54,8 +56,82 @@ class RPCStack: def Length(self): return len(self.stack) -class GameConfirmInstallPopupUpPage(Page): - pass +class ImageDownloadProcessPage(Page): + _FootMsg = ["Nav.","","","Back","Cancel"] + _DownloaderTimer = -1 + _Value = 0 + _URL = None + + _URLColor = MySkinManager.GiveColor('URL') + _TextColor = MySkinManager.GiveColor('Text') + _img = None + + def __init__(self): + Page.__init__(self) + self._Icons = {} + self._CanvasHWND = None + + def Init(self): + self._PosX = self._Index * self._Screen._Width + self._Width = self._Screen._Width + self._Height = self._Screen._Height + + self._CanvasHWND = self._Screen._CanvasHWND + + def OnLoadCb(self): + if self._URL is None: + return + self.ClearCanvas() + + print(self._URL ) + self._Downloader = Download(self._URL,"/tmp",None) + self._Downloader.start() + self._DownloaderTimer = gobject.timeout_add(300, self.GObjectUpdateProcessInterval) + + + def GObjectUpdateProcessInterval(self): + if self._Screen.CurPage() == self: + if self._Downloader._stop == True: + return False + + filename = self._Downloader.get_dest() + #print("dest ",filename) + if FileExists(filename): + try: + self._img = pygame.image.load(filename).convert_alpha() + self._Screen.Draw() + self._Screen.SwapAndShow() + except Exception as ex: + print(ex) + + return True + else: + return False + + + def KeyDown(self,event): + if IsKeyMenuOrB(event.key): + gobject.source_remove(self._DownloaderTimer) + self._DownloaderTimer = -1 + + if self._Downloader != None: + try: + self._Downloader.stop() + except: + print("user canceled ") + + self.ReturnToUpLevelPage() + self._Screen.Draw() + self._Screen.SwapAndShow() + self._URL = None + + def Draw(self): + self.ClearCanvas() + if self._img is not None: + self._CanvasHWND.blit(self._img,midRect(160, + 120, + pygame.Surface.get_width(self._img),pygame.Surface.get_height(self._img),Width,Height)) + class Aria2DownloadProcessPage(Page): _FootMsg = ["Nav.","","Pause","Back","Cancel"] @@ -251,7 +327,6 @@ class GameStoreListItem(InfoPageListItem): if self._Type == "source" or self._Type == "dir": self._Icons["ware"].NewCoord( 4, (self._Height - self._Icons["ware"]._Height)/2 ) - print(self._Height,self._Icons["ware"]._Height) self._Icons["ware"].DrawTopLeft() if self._Type == "launcher" or self._Type == "pico8": @@ -283,7 +358,7 @@ class GameStoreListItem(InfoPageListItem): class GameStorePage(Page): - _FootMsg = ["Nav","Update store","Up","Back","Select"] + _FootMsg = ["Nav","UpdateWare","Up","Back","Select"] _MyList = [] _ListFont12 = MyLangManager.TrFont("notosanscjk12") _ListFont15 = MyLangManager.TrFont("varela15") @@ -444,6 +519,45 @@ class GameStorePage(Page): self._Keyboard.SetPassword("github.com/clockworkpi/warehouse") self._Keyboard._Caller = self + self._PreviewPage = ImageDownloadProcessPage() + self._PreviewPage._Screen = self._Screen + self._PreviewPage._Name = "preview" + self._PreviewPage.Init() + + + def ResetHouse(self): + if self._PsIndex > len(self._MyList) -1: + return + cur_li = self._MyList[self._PsIndex] + if cur_li._Value["type"] == "source": + remote_file_url = cur_li._Value["file"] + menu_file = remote_file_url.split("raw.githubusercontent.com")[1] #assume master branch + local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),menu_file ) + print(local_menu_file) + try: + if os.path.exists(local_menu_file): + os.remove(local_menu_file) + if os.path.exists(local_menu_file+".aria2"): + os.remove(local_menu_file+".aria2") + except Exception as ex: + print(ex) + + self._Screen._MsgBox.SetText("Done") + self._Screen._MsgBox.Draw() + self._Screen.SwapAndShow() + + def PreviewGame(self): + if self._PsIndex > len(self._MyList) -1: + return + cur_li = self._MyList[self._PsIndex] + if cur_li._Value["type"] == "launcher" or cur_li._Value["type"] == "pico8" or cur_li._Value["type"] == "tic80": + print(cur_li._Value["shots"]) + self._PreviewPage._URL = cur_li._Value["shots"] + self._Screen.PushPage(self._PreviewPage) + self._Screen.Draw() + self._Screen.SwapAndShow() + + def RemoveGame(self): if self._PsIndex > len(self._MyList) -1: return @@ -473,7 +587,7 @@ class GameStorePage(Page): ret = c.execute("SELECT * FROM tasks WHERE gid='%s'" % gid ).fetchone() if ret != None: remote_file_url = ret["file"] - menu_file = remote_file_url.split("master")[1] + menu_file = remote_file_url.split("raw.githubusercontent.com")[1] local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),menu_file ) try: if os.path.exists(local_menu_file): @@ -503,7 +617,7 @@ class GameStorePage(Page): if cur_li._Value["type"] == "source" or cur_li._Value["type"] == "dir": remote_file_url = cur_li._Value["file"] - menu_file = remote_file_url.split("master")[1] #assume master branch + menu_file = remote_file_url.split("raw.githubusercontent.com")[1] #assume master branch local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),menu_file ) print(local_menu_file) if FileExists( local_menu_file ) == False: @@ -516,12 +630,13 @@ class GameStorePage(Page): self._Screen._MsgBox.SetText("Loading") self._Screen._MsgBox.Draw() self._Screen.SwapAndShow() - + pygame.time.delay(800) else: print(gid," url is downloading") self._Screen._MsgBox.SetText("Loading") self._Screen._MsgBox.Draw() self._Screen.SwapAndShow() + pygame.time.delay(400) else: #read the local_menu_file, push into stack,display menu @@ -533,7 +648,7 @@ class GameStorePage(Page): self.SyncList() self._Screen.Draw() - self._Screen.SwapAndShow() + self._Screen.SwapAndShow() elif cur_li._Value["type"] == "add_house": print("show keyboard to add ware house") @@ -543,7 +658,7 @@ class GameStorePage(Page): else: #download the game probably remote_file_url = cur_li._Value["file"] - menu_file = remote_file_url.split("master")[1] + menu_file = remote_file_url.split("raw.githubusercontent.com")[1] local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),menu_file ) if FileExists( local_menu_file ) == False: @@ -554,7 +669,7 @@ class GameStorePage(Page): print("stack length ",self._MyStack.Length()) if self._MyStack.Length() > 1:## not on the top list page try: - conn = sqlite3.connect("aria2tasks.db") + conn = sqlite3.connect(self._aria2_db) c = conn.cursor() c.execute("INSERT INTO tasks(gid,title,file,type,status,fav) VALUES ('"+gid+"','"+cur_li._Value["title"]+"','"+cur_li._Value["file"]+"','"+cur_li._Value["type"]+"','active','0')") @@ -594,7 +709,7 @@ class GameStorePage(Page): game_sh = "/home/cpi/apps/Menu/51_TIC-80/TIC-80.sh" self._Screen.RunEXE(game_sh) - def raw_github_com(self,_url): + def raw_github_com(self,_url):#eg: github.com/clockworkpi/warehouse if _url.startswith("github.com")== False: return False parts = _url.split("/") @@ -637,6 +752,7 @@ class GameStorePage(Page): c.execute(sql_insert) conn.commit() + self.SyncList() conn.close() except Exception as ex: print(ex) @@ -649,7 +765,7 @@ class GameStorePage(Page): print("OnLoadCb") if self._MyStack.Length() == 1: self._FootMsg[2] = "Remove" - self._FootMsg[1] = "Update store" + self._FootMsg[1] = "UpdateWare" else: self._FootMsg[2] = "Up" self._FootMsg[1] = "" @@ -660,7 +776,7 @@ class GameStorePage(Page): if self._MyStack.Length() == 1: self._FootMsg[2] = "Remove" - self._FootMsg[1] = "Update store" + self._FootMsg[1] = "UpdateWare" else: self._FootMsg[2] = "Up" self._FootMsg[1] = "" @@ -685,10 +801,10 @@ class GameStorePage(Page): if self._MyStack.Length() == 1: self._FootMsg[2] = "Remove" - self._FootMsg[1] = "Update store" + self._FootMsg[1] = "UpdateWare" else: self._FootMsg[2] = "Up" - self._FootMsg[1] = "" + self._FootMsg[1] = "Preview" self._Screen.Draw() self._Screen.SwapAndShow() @@ -707,10 +823,10 @@ class GameStorePage(Page): self._MyStack.Pop() if self._MyStack.Length() == 1: self._FootMsg[2] = "Remove" - self._FootMsg[1] = "Update store" + self._FootMsg[1] = "UpdateWare" else: self._FootMsg[2] = "Up" - self._FootMsg[1] = "" + self._FootMsg[1] = "Preview" self.SyncList() self._Screen.Draw() @@ -718,10 +834,9 @@ class GameStorePage(Page): if event.key == CurKeys["Y"]: if self._MyStack.Length() == 1: - self._Screen._MsgBox.SetText("Done") - self._Screen._MsgBox.Draw() - self._Screen.SwapAndShow() - + self.ResetHouse() + else: + self.PreviewGame() if event.key == CurKeys["Up"]: self.ScrollUp() @@ -775,7 +890,7 @@ class APIOBJ(object): def Init(self,main_screen): self._Page = GameStorePage() self._Page._Screen = main_screen - self._Page._Name ="Warehouse list" + self._Page._Name ="Warehouse" self._Page.Init() def API(self,main_screen): diff --git a/sys.py/UI/download.py b/sys.py/UI/download.py index cb97470..014373f 100644 --- a/sys.py/UI/download.py +++ b/sys.py/UI/download.py @@ -6,6 +6,7 @@ import urllib2 import hashlib from threading import Thread +from StringIO import StringIO class Download(Thread): _dst_path = "" @@ -24,7 +25,7 @@ class Download(Thread): self.downloaded = 0 self.progress = { 'downloaded': 0, 'total': 0, 'percent': 0,'stopped':False } - self.stop = False + self._stop = False self.filename = "" def isFinished(self): @@ -40,14 +41,15 @@ class Download(Thread): return self._errors def run(self): - c = pycurl.Curl() c.setopt(pycurl.URL, self.url) - c.setopt(pycurl.FOLLOWLOCATION, 1) - c.setopt(pycurl.MAXREDIRS, 5) - c.setopt(pycurl.NOBODY, 1) + c.setopt(pycurl.FOLLOWLOCATION, True) + c.setopt(pycurl.MAXREDIRS, 4) + #c.setopt(pycurl.NOBODY, 1) - c.setopt(pycurl.CONNECTTIMEOUT, 10) + c.setopt(c.VERBOSE, True) + + #c.setopt(pycurl.CONNECTTIMEOUT, 20) if self.useragent: c.setopt(pycurl.USERAGENT, self.useragent) @@ -55,39 +57,39 @@ class Download(Thread): # add cookies, if available if self.cookies: c.setopt(pycurl.COOKIE, self.cookies) - c.perform() - realurl = c.getinfo(pycurl.EFFECTIVE_URL) + #realurl = c.getinfo(pycurl.EFFECTIVE_URL) + realurl = self.url + print("realurl",realurl) self.filename = realurl.split("/")[-1].strip() - - c = pycurl.Curl() - c.setopt(pycurl.CONNECTTIMEOUT, 10) c.setopt(pycurl.URL, realurl) - c.setopt(pycurl.FOLLOWLOCATION, 0) + c.setopt(pycurl.FOLLOWLOCATION, True) c.setopt(pycurl.NOPROGRESS, False) c.setopt(pycurl.XFERINFOFUNCTION, self.getProgress) - if self.useragent: - c.setopt(pycurl.USERAGENT, self.useragent) + + c.setopt(pycurl.SSL_VERIFYPEER, False) + c.setopt(pycurl.SSL_VERIFYHOST, False) # configure pycurl output file if self.path == False: self.path = os.getcwd() filepath = os.path.join(self.path, self.filename) - + + if os.path.exists(filepath):## remove old file,restart download os.system("rm -rf " + filepath) - f = open(filepath, "wb") - else: - f = open(filepath, "wb") - c.setopt(pycurl.WRITEDATA, f) + buffer = StringIO() + c.setopt(pycurl.WRITEDATA, buffer) self._dst_path = filepath # add cookies, if available if self.cookies: c.setopt(pycurl.COOKIE, self.cookies) - + + self._stop = False + self.progress["stopped"] = False # download file try: c.perform() @@ -95,14 +97,18 @@ class Download(Thread): errno,errstr = error print("curl error: %s" % errstr) self._errors.append(errstr) - self.stop = True + self._stop = True self.progress["stopped"] = True + self.stop() finally: code = c.getinfo( c.RESPONSE_CODE ) c.close() self._is_finished = True - + + with open(filepath, mode='w') as f: + f.write(buffer.getvalue()) + if self.progress["percent"] < 100: self._is_successful = False else: @@ -127,7 +133,7 @@ class Download(Thread): self.progress['total'] = download_t + self.downloaded self.progress['percent'] = ( float(self.progress['downloaded']) / float(self.progress['total'])) * 100.0 self.progress["stopped"] = False - if self.stop: + if self._stop: self.progress["stopped"] = True return 1 @@ -151,11 +157,11 @@ class Download(Thread): return self.progress["percent"] def stop(self): - self.stop = True + self._stop = True def cancel(self): # sets the boolean to stop the thread. - self.stop = True + self._stop = True def main(): from optparse import OptionParser diff --git a/sys.py/appinstaller.py b/sys.py/appinstaller.py index 89c8e30..6f72c5d 100644 --- a/sys.py/appinstaller.py +++ b/sys.py/appinstaller.py @@ -36,7 +36,7 @@ def game_install_thread(gid): conn.close() remote_file_url = ret["file"] - menu_file = remote_file_url.split("master")[1] + menu_file = remote_file_url.split("raw.githubusercontent.com")[1] local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),menu_file ) if os.path.exists(local_menu_file) == True and "arm" in platform.machine():