diff options
-rw-r--r-- | game/misc.rpy | 291 | ||||
-rw-r--r-- | game/screens.rpy | 3 |
2 files changed, 231 insertions, 63 deletions
diff --git a/game/misc.rpy b/game/misc.rpy index cfc8981..6a088d4 100644 --- a/game/misc.rpy +++ b/game/misc.rpy @@ -175,69 +175,6 @@ label msgbox_label(msgcode): call screen msgbox(msgcode) return -label clear_all: - python: - rl=False - - # Your version is no longer valid - if (HOST != persistent.host): - print("Host changed from %s to %s" % (str(HOST), persistent.host)) - persistent.version=1 - rl=True - # TODO: Clear cache - - # Update host and delete password - HOST=persistent.host - PORT=persistent.port - renpy.notify("Deleting password: %s" % persistent.password) - persistent.password=None - - if debug or config.developer: - "Spheres" "Techinical info:\n\nServer: [HOST]:[PORT]" - - if rl: - "Spheres" "Server data purged, we will now relaunch the game." - $renpy.quit(relaunch=True) - else: - "Spheres" "You have logged out successfully." - $ renpy.full_restart() - # ??? - jump start - -label clear_cache: - python: - import os - - # Your version is no longer valid - if persistent.host != "localhost": - persistent.version=1 - - # Find path to cached files - #if renpy.android: - root=get_path("") - #else: - # root=get_path("extra/") - - # Remove cached files - for file in os.listdir(root): - if file.endswith('.png') or file.endswith('.mp3') or file.endswith('.jpg') or file.endswith('.webp') or file.endswith('.ogg'): - try: - os.remove(get_path(root+file)) - stdout("[CC] Removing %s" % get_path(root+file)) - except: - stdout("[CC] Removing %s... FAILED" % get_path(root+file)) - os.remove(root+file) - stdout("[CC] Removing %s" % (root+file)) - continue - - # Erase file memory - persistent.allfiles=[] - allfiles=[] - - "Spheres" "Cache deleted. Space was freed!" - $renpy.full_restart() - jump start - image spinner: "gfx/spinner.png" rotate 30.0 @@ -350,3 +287,231 @@ Make sure to note down the password or have provided a valid email, or you'll lo $ renpy.full_restart() return +label clear_all: + python: + rl=False + + # Your version is no longer valid + if (HOST != persistent.host): + print("Host changed from %s to %s" % (str(HOST), persistent.host)) + persistent.version=1 + rl=True + # TODO: Clear cache + + # Update host and delete password + HOST=persistent.host + PORT=persistent.port + renpy.notify("Deleting password: %s" % persistent.password) + persistent.password=None + + if debug or config.developer: + "Spheres" "Techinical info:\n\nServer: [HOST]:[PORT]" + + if rl: + "Spheres" "Server data purged, we will now relaunch the game." + $renpy.quit(relaunch=True) + else: + "Spheres" "You have logged out successfully." + $ renpy.full_restart() + # ??? + jump start + +label clear_cache: + python: + import os + + # Your version is no longer valid + if persistent.host != "localhost": + persistent.version=1 + + # Find path to cached files + #if renpy.android: + root=get_path("") + #else: + # root=get_path("extra/") + + # Remove cached files + for file in os.listdir(root): + if file.endswith('.png') or file.endswith('.mp3') or file.endswith('.jpg') or file.endswith('.webp') or file.endswith('.ogg'): + try: + os.remove(root+file) + stdout("[CC] Removing %s" % (root+file)) + except: + stdout("[CC] Removing %s... FAILED" % root+file) + os.remove(get_path(root+file)) + stdout("[CC] Removing %s" % get_path(root+file)) + continue + + # Erase file memory + persistent.allfiles=[] + allfiles=[] + + "Spheres" "Cache deleted. Space was freed!" + $renpy.full_restart() + jump start + + +################################################################################# +# Everything below this line is part of Mana Launcher. +# Copyright (C) 2021 Jesusalva <jesusalva@tmw2.org> +# +# Distributed under the MIT license. +################################################################################# +screen loading(): + zorder 100 + + fixed: + frame: + xfill True + yfill True + background Frame("gfx/bg/forest_sunset.png", 0, 0) + bar: + value progress + range max_value + xalign 0.5 + yalign 0.45 + xmaximum 0.75 + label "{color=#fff}[statusmsg]{/color}": + xalign 0.5 + yalign 0.55 + +init python: + import hashlib, zipfile + def md5(string): + return hashlib.md5(string.encode()).hexdigest() + + def md5sum(f): + md5=hashlib.md5() + fp=open(f, "rb") + ct=fp.read() + md5.update(ct) + rt=copy.copy(md5.hexdigest()) + fp.close() + del ct + return rt + + def dall_fetch(): + global pat, statusmsg, progress, max_value, status + try: + print("Fetching http://%s/assets%s ..." % (persistent.host, pat)) + r = requests.get("http://%s/assets%s" % (persistent.host, pat), stream=True) + if r.status_code / 100 != 2: + raise Exception("Aborted with status %d" % r.status_code) + + ## Download the file, slowly as it may be... + max_value = int(r.headers.get('content-length', 0)) + print "Blocks: %d" % max_value + block_size = 1024 #1 Kibibyte + statusmsg = "Downloading the zip file..." + with open(get_path("allinone.zip"), 'wb') as f: + for data in r.iter_content(block_size): + f.write(data) + progress += 1 + print "Finished OK" + status = 1 + except: + traceback.print_exc() + print("Download All: Not successful!") + statusmsg = "Download failed.\n\nClick anywhere to continue." % r.status_code + status = -1 + return + + def dall_save(): + global statusmsg, progress, max_value, status + try: + statusmsg = "Extracting..."; progress = 0 + with zipfile.ZipFile(get_path("allinone.zip"), 'r') as zip_ref: + max_value=len(zip_ref.namelist()) + print "Blocks: %d" % max_value + for f in zip_ref.namelist(): + try: + print("Extracting %s..." % f) + monday = "extra_"+f + if "." in f: + pass + elif f.startswith("sfx"): + monday += ".mp3" + else: + monday += ".webp" + addr=get_path(monday) + ## Work-around to save as + with open(addr, 'wb') as zf: + zf.write(zip_ref.read(f)) + ## ...Better always save path in full + path=((f, addr)) + persistent.allfiles.append(path) + except: + traceback.print_exc() + print("[ERROR] Skipping %s..." % f) + progress+=1 + print "Finished OK" + status = 1 + except: + traceback.print_exc() + print("Extraction error!") + statusmsg = "Extraction error.\n\nClick anywhere to continue." % r.status_code + status = -1 + return + +label download_all: + $ statusmsg = "Preparing to download..." + $ progress = 0 + $ max_value = 1000 + $ status = 0 + $ pat = "" # Download + $ md5 = "" # MD5 Hash + show screen loading with dissolve + python: + ## Where the zip file is located? + try: + x = requests.get("http://%s/assets/fetch.txt" % persistent.host, timeout=8.0) + assert x.status_code == 200, "Status Code Error" + print "%s" % str(x.text) + pat = x.text.split("#")[0] + md5 = x.text.split("#")[1].replace("\n", "") + except: + # Oh noes - something went *terribly* wrong + traceback.print_exc() + x=requests.Response() + x.status_code=403 + + ## Did we manage to get the location? + if x.status_code / 100 != 2: + statusmsg = "Error downloading!\nResource unavailable for the selected host.\n\nClick anywhere to continue."; renpy.pause(); renpy.full_restart(); renpy.jump("start") + + ## Now we know what to download + # Must be done in thread, or Ren'Py will be blocked + renpy.invoke_in_thread(dall_fetch) + while not status: + sdelay() + + if status < 0: + renpy.pause(); renpy.full_restart(); renpy.jump("start") + + status = 0 + + ## Verify if download was successful... + statusmsg = "Integrity Check..."; + md4 = md5sum(get_path("allinone.zip")) + if (md4 != md5): + statusmsg = "{color=#f00}Download failed.{/color}\n%s != %s\nClick anywhere to retry." % (md4, md5); renpy.jump("download_all") + else: + print("Integrity check passed: %s" % md4) + + ## We downloaded, but we need to extract it + # FIXME: Must be done in thread, or Ren'Py will be blocked + renpy.invoke_in_thread(dall_save) + while not status: + sdelay() + + if status < 0: + renpy.pause(); renpy.full_restart(); renpy.jump("start") + + status = 0 + + statusmsg = "Complete.\nClick anywhere to continue." + renpy.pause() + + renpy.full_restart() + jump start + diff --git a/game/screens.rpy b/game/screens.rpy index 937a6b6..d4e89a6 100644 --- a/game/screens.rpy +++ b/game/screens.rpy @@ -777,6 +777,9 @@ screen preferences(): null height 60 textbutton _("Clear Cache"): action Jump("clear_cache") + null height 30 + textbutton _("Download All Files"): + action Jump("download_all") vbox: |