Core: Detect and account for apworlds being downloaded with a (1) in their name (#4144)

* Core: Detect and account for apworlds being downloaded with a (1) in their name

* Reword comment

* Always use internal module name

* Requested changes from black-silver
This commit is contained in:
Katelyn Gigante 2024-11-15 07:51:05 +11:00 committed by GitHub
parent eac3e3c29e
commit 8f60a4a259
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 3 deletions

View File

@ -100,10 +100,16 @@ def _install_apworld(apworld_src: str = "") -> Optional[Tuple[pathlib.Path, path
apworld_path = pathlib.Path(apworld_src)
module_name = pathlib.Path(apworld_path.name).stem
try:
import zipfile
zipfile.ZipFile(apworld_path).open(module_name + "/__init__.py")
zip = zipfile.ZipFile(apworld_path)
directories = [f.filename.strip('/') for f in zip.filelist if f.CRC == 0 and f.file_size == 0 and f.filename.count('/') == 1]
if len(directories) == 1 and directories[0] in apworld_path.stem:
module_name = directories[0]
apworld_name = module_name + ".apworld"
else:
raise Exception("APWorld appears to be invalid or damaged. (expected a single directory)")
zip.open(module_name + "/__init__.py")
except ValueError as e:
raise Exception("Archive appears invalid or damaged.") from e
except KeyError as e:
@ -122,7 +128,7 @@ def _install_apworld(apworld_src: str = "") -> Optional[Tuple[pathlib.Path, path
# TODO: run generic test suite over the apworld.
# TODO: have some kind of version system to tell from metadata if the apworld should be compatible.
target = pathlib.Path(worlds.user_folder) / apworld_path.name
target = pathlib.Path(worlds.user_folder) / apworld_name
import shutil
shutil.copyfile(apworld_path, target)