From de31fc320c09f4c5a5e14c49913435773f9891a2 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Sun, 16 May 2021 01:16:51 +0200 Subject: [PATCH] allow webhost handling of APMC files --- MultiMystery.py | 10 +++++++--- WebHostLib/downloads.py | 17 +++++++++++++++++ WebHostLib/templates/hostRoom.html | 2 +- WebHostLib/templates/macros.html | 5 +++++ WebHostLib/upload.py | 11 ++++++++++- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/MultiMystery.py b/MultiMystery.py index e84b3bc3..b399d775 100644 --- a/MultiMystery.py +++ b/MultiMystery.py @@ -178,17 +178,18 @@ if __name__ == "__main__": remove_zipped_file(file) - def _handle_apmc_file(file: str): + def _handle_apmc_file(file: str): if zip_apmcs: pack_file(file) - if zip_apmcs == 2: + if zip_apmcs == 2: remove_zipped_file(file) with concurrent.futures.ThreadPoolExecutor() as pool: futures = [] + files = os.listdir(output_path) with zipfile.ZipFile(zipname, "w", compression=compression, compresslevel=9) as zf: - for file in os.listdir(output_path): + for file in files: if seed_name in file: if file.endswith(".sfc"): futures.append(pool.submit(_handle_sfc_file, file)) @@ -196,6 +197,9 @@ if __name__ == "__main__": futures.append(pool.submit(_handle_diff_file, file)) elif file.endswith(".apmc"): futures.append(pool.submit(_handle_apmc_file, file)) + # just handle like a diff file for now + elif file.endswith(".zip"): + futures.append(pool.submit(_handle_diff_file, file)) if zip_multidata and os.path.exists(os.path.join(output_path, multidataname)): pack_file(multidataname) diff --git a/WebHostLib/downloads.py b/WebHostLib/downloads.py index 6a3204a1..3ede991e 100644 --- a/WebHostLib/downloads.py +++ b/WebHostLib/downloads.py @@ -44,3 +44,20 @@ def download_raw_patch(seed_id, player_id: int): fname = f"P{patch.player_id}_{patch.player_name}_{app.jinja_env.filters['suuid'](seed_id)}.apbp" return send_file(patch_data, as_attachment=True, attachment_filename=fname) + +@app.route("/slot_file//") +def download_slot_file(seed_id, player_id: int): + seed = Seed.get(id=seed_id) + slot_data: Slot = select(patch for patch in seed.slots if + patch.player_id == player_id).first() + + if not slot_data: + return "Slot Data not found" + else: + import io + + if slot_data.game == "Minecraft": + fname = f"AP_{app.jinja_env.filters['suuid'](seed_id)}_P{slot_data.player_id}_{slot_data.player_name}.apmc" + else: + return "Game download not supported." + return send_file(io.BytesIO(slot_data.data), as_attachment=True, attachment_filename=fname) \ No newline at end of file diff --git a/WebHostLib/templates/hostRoom.html b/WebHostLib/templates/hostRoom.html index d8d62f30..24b7bbf1 100644 --- a/WebHostLib/templates/hostRoom.html +++ b/WebHostLib/templates/hostRoom.html @@ -21,7 +21,7 @@ you can simply refresh this page and the server will be started again.
{% if room.last_port %} You can connect to this room by using '/connect archipelago.gg:{{ room.last_port }}' - in the client.
{% endif %} + in the client.
{% endif %} {{ macros.list_patches_room(room) }} {% if room.owner == session["_id"] %}
diff --git a/WebHostLib/templates/macros.html b/WebHostLib/templates/macros.html index 0ad67085..553aef8b 100644 --- a/WebHostLib/templates/macros.html +++ b/WebHostLib/templates/macros.html @@ -10,8 +10,13 @@ {% if room.seed.slots %} {% endif %} diff --git a/WebHostLib/upload.py b/WebHostLib/upload.py index fd54eb80..fe37dc71 100644 --- a/WebHostLib/upload.py +++ b/WebHostLib/upload.py @@ -1,5 +1,7 @@ import zipfile import lzma +import json +import base64 import MultiServer from flask import request, flash, redirect, url_for, session, render_template @@ -43,11 +45,18 @@ def uploads(): yaml_data = parse_yaml(lzma.decompress(data).decode("utf-8-sig")) if yaml_data["version"] < 2: return "Old format cannot be uploaded (outdated .apbp)", 500 - metadata = yaml_data["meta"] slots.add(Slot(data=data, player_name=metadata["player_name"], player_id=metadata["player_id"], game="A Link to the Past")) + + elif file.filename.endswith(".apmc"): + data = zfile.open(file, "r").read() + metadata = json.loads(base64.b64decode(data).decode("utf-8")) + slots.add(Slot(data=data, player_name=metadata["player_name"], + player_id=metadata["player_id"], + game="Minecraft")) + elif file.filename.endswith(".txt"): spoiler = zfile.open(file, "r").read().decode("utf-8-sig") elif file.filename.endswith(".archipelago"):