Skip to content

Commit

Permalink
Revert "Support loading worlds from zips"
Browse files Browse the repository at this point in the history
This reverts commit 8ad9710.
  • Loading branch information
dcvz committed Sep 4, 2024
1 parent 8ad9710 commit b4a47e5
Showing 1 changed file with 7 additions and 34 deletions.
41 changes: 7 additions & 34 deletions worlds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sys
import warnings
import zipimport
import zipfile
import time
import dataclasses
from typing import Dict, List, TypedDict, Optional
Expand Down Expand Up @@ -58,20 +57,10 @@ def resolved_path(self) -> str:
return self.path

def load(self) -> bool:
import traceback
traceback.print_stack()

try:
start = time.perf_counter()
if self.is_zip:
if ".zip" in self.resolved_path:
path_to_zip = self.resolved_path.split(".zip")[0] + ".zip/worlds"
importer = zipimport.zipimporter(path_to_zip)
# load with path that's everything after the zip file name
importer.load_module(self.path)
else:
importer = zipimport.zipimporter(self.resolved_path)

importer = zipimport.zipimporter(self.resolved_path)
if hasattr(importer, "find_spec"): # new in Python 3.10
spec = importer.find_spec(os.path.basename(self.path).rsplit(".", 1)[0])
assert spec, f"{self.path} is not a loadable module"
Expand Down Expand Up @@ -105,36 +94,20 @@ def load(self) -> bool:
failed_world_loads.append(os.path.basename(self.path).rsplit(".", 1)[0])
return False

# Function to load worlds from a folder or a zip file within that folder
def load_worlds_from_folder(folder: str, relative: bool):

# find potential world containers, currently folders and zip-importable .apworld's
world_sources: List[WorldSource] = []
for folder in (folder for folder in (user_folder, local_folder) if folder):
relative = folder == local_folder
for entry in os.scandir(folder):
# prevent loading of __pycache__ and allow _* for non-world folders, disable files/folders starting with "."
if not entry.name.startswith(("_", ".")):
file_name = entry.name if relative else os.path.join(folder, entry.name)
if entry.is_dir():
world_sources.append(WorldSource(file_name, relative=relative))
elif entry.is_file() and entry.name.endswith(".apworld"):
world_sources.append(WorldSource(file_name, is_zip=True, relative=relative))

# Function to load worlds when AP is running from within a zip archive
def load_worlds_from_zip(zip_file: str):
zip_file = zip_file.split(".zip")[0] + ".zip"
with zipfile.ZipFile(zip_file, 'r') as z:
for file_name in z.namelist():
if not file_name.startswith("worlds/"):
continue
file_name = file_name.split("worlds/")[1]
if not file_name.startswith(("_", ".")):
world_sources.append(WorldSource(file_name, is_zip=True, relative=True))

# find potential world containers, currently folders and zip-importable .apworld's
world_sources: List[WorldSource] = []
for folder in (folder for folder in (user_folder, local_folder) if folder):
if ".zip" in folder:
load_worlds_from_zip(folder)
else:
relative = folder == local_folder
load_worlds_from_folder(folder, relative)

# import all submodules to trigger AutoWorldRegister
world_sources.sort()
for world_source in world_sources:
Expand Down

0 comments on commit b4a47e5

Please sign in to comment.