diff --git a/.github/labeler.yml b/.github/labeler.yml index 2743104f41..d0aa61c8cf 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -21,7 +21,6 @@ - '!data/**' - '!.run/**' - '!.github/**' - - '!worlds_disabled/**' - '!worlds/**' - '!WebHost.py' - '!WebHostLib/**' diff --git a/.github/workflows/analyze-modified-files.yml b/.github/workflows/analyze-modified-files.yml index b59336fafe..6788abd30a 100644 --- a/.github/workflows/analyze-modified-files.yml +++ b/.github/workflows/analyze-modified-files.yml @@ -65,7 +65,7 @@ jobs: continue-on-error: false if: env.diff != '' && matrix.task == 'flake8' run: | - flake8 --count --select=E9,F63,F7,F82 --show-source --statistics ${{ env.diff }} + flake8 --count --select=E9,F63,F7,F82 --ignore F824 --show-source --statistics ${{ env.diff }} - name: "flake8: Lint modified files" continue-on-error: true diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2b450fe46e..d6b80965f0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,12 +21,17 @@ env: ENEMIZER_VERSION: 7.1 APPIMAGETOOL_VERSION: 13 +permissions: # permissions required for attestation + id-token: 'write' + attestations: 'write' + jobs: # build-release-macos: # LF volunteer - build-win: # RCs will still be built and signed by hand + build-win: # RCs and releases may still be built and signed by hand runs-on: windows-latest steps: + # - copy code below to release.yml - - uses: actions/checkout@v4 - name: Install python uses: actions/setup-python@v5 @@ -65,6 +70,18 @@ jobs: $contents = Get-ChildItem -Path setups/*.exe -Force -Recurse $SETUP_NAME=$contents[0].Name echo "SETUP_NAME=$SETUP_NAME" >> $Env:GITHUB_ENV + # - copy code above to release.yml - + - name: Attest Build + if: ${{ github.event_name == 'workflow_dispatch' }} + uses: actions/attest-build-provenance@v2 + with: + subject-path: | + build/exe.*/ArchipelagoLauncher.exe + build/exe.*/ArchipelagoLauncherDebug.exe + build/exe.*/ArchipelagoGenerate.exe + build/exe.*/ArchipelagoServer.exe + dist/${{ env.ZIP_NAME }} + setups/${{ env.SETUP_NAME }} - name: Check build loads expected worlds shell: bash run: | @@ -99,8 +116,8 @@ jobs: if-no-files-found: error retention-days: 7 # keep for 7 days, should be enough - build-ubuntu2004: - runs-on: ubuntu-20.04 + build-ubuntu2204: + runs-on: ubuntu-22.04 steps: # - copy code below to release.yml - - uses: actions/checkout@v4 @@ -142,6 +159,16 @@ jobs: echo "APPIMAGE_NAME=$APPIMAGE_NAME" >> $GITHUB_ENV echo "TAR_NAME=$TAR_NAME" >> $GITHUB_ENV # - copy code above to release.yml - + - name: Attest Build + if: ${{ github.event_name == 'workflow_dispatch' }} + uses: actions/attest-build-provenance@v2 + with: + subject-path: | + build/exe.*/ArchipelagoLauncher + build/exe.*/ArchipelagoGenerate + build/exe.*/ArchipelagoServer + dist/${{ env.APPIMAGE_NAME }}* + dist/${{ env.TAR_NAME }} - name: Build Again run: | source venv/bin/activate diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f12e8fb80c..a500f9a23b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,6 +11,11 @@ env: ENEMIZER_VERSION: 7.1 APPIMAGETOOL_VERSION: 13 +permissions: # permissions required for attestation + id-token: 'write' + attestations: 'write' + contents: 'write' # additionally required for release + jobs: create-release: runs-on: ubuntu-latest @@ -26,11 +31,79 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # build-release-windows: # this is done by hand because of signing # build-release-macos: # LF volunteer - build-release-ubuntu2004: - runs-on: ubuntu-20.04 + build-release-win: + runs-on: windows-latest + if: ${{ true }} # change to false to skip if release is built by hand + needs: create-release + steps: + - name: Set env + shell: bash + run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV + # - code below copied from build.yml - + - uses: actions/checkout@v4 + - name: Install python + uses: actions/setup-python@v5 + with: + python-version: '~3.12.7' + check-latest: true + - name: Download run-time dependencies + run: | + Invoke-WebRequest -Uri https://github.com/Ijwu/Enemizer/releases/download/${Env:ENEMIZER_VERSION}/win-x64.zip -OutFile enemizer.zip + Expand-Archive -Path enemizer.zip -DestinationPath EnemizerCLI -Force + choco install innosetup --version=6.2.2 --allow-downgrade + - name: Build + run: | + python -m pip install --upgrade pip + python setup.py build_exe --yes + if ( $? -eq $false ) { + Write-Error "setup.py failed!" + exit 1 + } + $NAME="$(ls build | Select-String -Pattern 'exe')".Split('.',2)[1] + $ZIP_NAME="Archipelago_$NAME.7z" + echo "$NAME -> $ZIP_NAME" + echo "ZIP_NAME=$ZIP_NAME" >> $Env:GITHUB_ENV + New-Item -Path dist -ItemType Directory -Force + cd build + Rename-Item "exe.$NAME" Archipelago + 7z a -mx=9 -mhe=on -ms "../dist/$ZIP_NAME" Archipelago + Rename-Item Archipelago "exe.$NAME" # inno_setup.iss expects the original name + - name: Build Setup + run: | + & "${env:ProgramFiles(x86)}\Inno Setup 6\iscc.exe" inno_setup.iss /DNO_SIGNTOOL + if ( $? -eq $false ) { + Write-Error "Building setup failed!" + exit 1 + } + $contents = Get-ChildItem -Path setups/*.exe -Force -Recurse + $SETUP_NAME=$contents[0].Name + echo "SETUP_NAME=$SETUP_NAME" >> $Env:GITHUB_ENV + # - code above copied from build.yml - + - name: Attest Build + uses: actions/attest-build-provenance@v2 + with: + subject-path: | + build/exe.*/ArchipelagoLauncher.exe + build/exe.*/ArchipelagoLauncherDebug.exe + build/exe.*/ArchipelagoGenerate.exe + build/exe.*/ArchipelagoServer.exe + setups/* + - name: Add to Release + uses: softprops/action-gh-release@975c1b265e11dd76618af1c374e7981f9a6ff44a + with: + draft: true # see above + prerelease: false + name: Archipelago ${{ env.RELEASE_VERSION }} + files: | + setups/* + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + build-release-ubuntu2204: + runs-on: ubuntu-22.04 + needs: create-release steps: - name: Set env run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV @@ -74,6 +147,14 @@ jobs: echo "APPIMAGE_NAME=$APPIMAGE_NAME" >> $GITHUB_ENV echo "TAR_NAME=$TAR_NAME" >> $GITHUB_ENV # - code above copied from build.yml - + - name: Attest Build + uses: actions/attest-build-provenance@v2 + with: + subject-path: | + build/exe.*/ArchipelagoLauncher + build/exe.*/ArchipelagoGenerate + build/exe.*/ArchipelagoServer + dist/* - name: Add to Release uses: softprops/action-gh-release@975c1b265e11dd76618af1c374e7981f9a6ff44a with: diff --git a/AHITClient.py b/AHITClient.py index 6ed7d7b49d..edcbbd842e 100644 --- a/AHITClient.py +++ b/AHITClient.py @@ -1,3 +1,4 @@ +import sys from worlds.ahit.Client import launch import Utils import ModuleUpdate @@ -5,4 +6,4 @@ ModuleUpdate.update() if __name__ == "__main__": Utils.init_logging("AHITClient", exception_logger="Client") - launch() + launch(*sys.argv[1:]) diff --git a/BaseClasses.py b/BaseClasses.py index 4db3917985..1de23bc1ea 100644 --- a/BaseClasses.py +++ b/BaseClasses.py @@ -9,8 +9,9 @@ from argparse import Namespace from collections import Counter, deque from collections.abc import Collection, MutableSequence from enum import IntEnum, IntFlag -from typing import (AbstractSet, Any, Callable, ClassVar, Dict, Iterable, Iterator, List, Mapping, NamedTuple, +from typing import (AbstractSet, Any, Callable, ClassVar, Dict, Iterable, Iterator, List, Literal, Mapping, NamedTuple, Optional, Protocol, Set, Tuple, Union, TYPE_CHECKING) +import dataclasses from typing_extensions import NotRequired, TypedDict @@ -54,12 +55,21 @@ class HasNameAndPlayer(Protocol): player: int +@dataclasses.dataclass +class PlandoItemBlock: + player: int + from_pool: bool + force: bool | Literal["silent"] + worlds: set[int] = dataclasses.field(default_factory=set) + items: list[str] = dataclasses.field(default_factory=list) + locations: list[str] = dataclasses.field(default_factory=list) + resolved_locations: list[Location] = dataclasses.field(default_factory=list) + count: dict[str, int] = dataclasses.field(default_factory=dict) + + class MultiWorld(): debug_types = False player_name: Dict[int, str] - plando_texts: List[Dict[str, str]] - plando_items: List[List[Dict[str, Any]]] - plando_connections: List worlds: Dict[int, "AutoWorld.World"] groups: Dict[int, Group] regions: RegionManager @@ -83,6 +93,8 @@ class MultiWorld(): start_location_hints: Dict[int, Options.StartLocationHints] item_links: Dict[int, Options.ItemLinks] + plando_item_blocks: Dict[int, List[PlandoItemBlock]] + game: Dict[int, str] random: random.Random @@ -160,13 +172,12 @@ class MultiWorld(): self.local_early_items = {player: {} for player in self.player_ids} self.indirect_connections = {} self.start_inventory_from_pool: Dict[int, Options.StartInventoryPool] = {} + self.plando_item_blocks = {} for player in range(1, players + 1): def set_player_attr(attr: str, val) -> None: self.__dict__.setdefault(attr, {})[player] = val - set_player_attr('plando_items', []) - set_player_attr('plando_texts', {}) - set_player_attr('plando_connections', []) + set_player_attr('plando_item_blocks', []) set_player_attr('game', "Archipelago") set_player_attr('completion_condition', lambda state: True) self.worlds = {} @@ -223,7 +234,7 @@ class MultiWorld(): AutoWorld.AutoWorldRegister.world_types[self.game[player]].options_dataclass.type_hints} for option_key in all_keys: option = Utils.DeprecateDict(f"Getting options from multiworld is now deprecated. " - f"Please use `self.options.{option_key}` instead.") + f"Please use `self.options.{option_key}` instead.", True) option.update(getattr(args, option_key, {})) setattr(self, option_key, option) @@ -427,7 +438,8 @@ class MultiWorld(): def get_location(self, location_name: str, player: int) -> Location: return self.regions.location_cache[player][location_name] - def get_all_state(self, use_cache: bool, allow_partial_entrances: bool = False) -> CollectionState: + def get_all_state(self, use_cache: bool, allow_partial_entrances: bool = False, + collect_pre_fill_items: bool = True, perform_sweep: bool = True) -> CollectionState: cached = getattr(self, "_all_state", None) if use_cache and cached: return cached.copy() @@ -436,11 +448,13 @@ class MultiWorld(): for item in self.itempool: self.worlds[item.player].collect(ret, item) - for player in self.player_ids: - subworld = self.worlds[player] - for item in subworld.get_pre_fill_items(): - subworld.collect(ret, item) - ret.sweep_for_advancements() + if collect_pre_fill_items: + for player in self.player_ids: + subworld = self.worlds[player] + for item in subworld.get_pre_fill_items(): + subworld.collect(ret, item) + if perform_sweep: + ret.sweep_for_advancements() if use_cache: self._all_state = ret @@ -545,7 +559,9 @@ class MultiWorld(): else: return all((self.has_beaten_game(state, p) for p in range(1, self.players + 1))) - def can_beat_game(self, starting_state: Optional[CollectionState] = None) -> bool: + def can_beat_game(self, + starting_state: Optional[CollectionState] = None, + locations: Optional[Iterable[Location]] = None) -> bool: if starting_state: if self.has_beaten_game(starting_state): return True @@ -554,7 +570,9 @@ class MultiWorld(): state = CollectionState(self) if self.has_beaten_game(state): return True - prog_locations = {location for location in self.get_locations() if location.item + + base_locations = self.get_locations() if locations is None else locations + prog_locations = {location for location in base_locations if location.item and location.item.advancement and location not in state.locations_checked} while prog_locations: @@ -616,7 +634,7 @@ class MultiWorld(): locations: Set[Location] = set() events: Set[Location] = set() for location in self.get_filled_locations(): - if type(location.item.code) is int: + if type(location.item.code) is int and type(location.address) is int: locations.add(location) else: events.add(location) @@ -723,6 +741,7 @@ class CollectionState(): additional_copy_functions: List[Callable[[CollectionState, CollectionState], CollectionState]] = [] def __init__(self, parent: MultiWorld, allow_partial_entrances: bool = False): + assert parent.worlds, "CollectionState created without worlds initialized in parent" self.prog_items = {player: Counter() for player in parent.get_all_ids()} self.multiworld = parent self.reachable_regions = {player: set() for player in parent.get_all_ids()} @@ -999,6 +1018,17 @@ class CollectionState(): return changed + def add_item(self, item: str, player: int, count: int = 1) -> None: + """ + Adds the item to state. + + :param item: The item to be added. + :param player: The player the item is for. + :param count: How many of the item to add. + """ + assert count > 0 + self.prog_items[player][item] += count + def remove(self, item: Item): changed = self.multiworld.worlds[item.player].remove(self, item) if changed: @@ -1007,6 +1037,33 @@ class CollectionState(): self.blocked_connections[item.player] = set() self.stale[item.player] = True + def remove_item(self, item: str, player: int, count: int = 1) -> None: + """ + Removes the item from state. + + :param item: The item to be removed. + :param player: The player the item is for. + :param count: How many of the item to remove. + """ + assert count > 0 + self.prog_items[player][item] -= count + if self.prog_items[player][item] < 1: + del (self.prog_items[player][item]) + + def set_item(self, item: str, player: int, count: int) -> None: + """ + Sets the item in state equal to the provided count. + + :param item: The item to modify. + :param player: The player the item is for. + :param count: How many of the item to now have. + """ + assert count >= 0 + if count == 0: + del (self.prog_items[player][item]) + else: + self.prog_items[player][item] = count + class EntranceType(IntEnum): ONE_WAY = 1 @@ -1022,9 +1079,6 @@ class Entrance: connected_region: Optional[Region] = None randomization_group: int randomization_type: EntranceType - # LttP specific, TODO: should make a LttPEntrance - addresses = None - target = None def __init__(self, player: int, name: str = "", parent: Optional[Region] = None, randomization_group: int = 0, randomization_type: EntranceType = EntranceType.ONE_WAY) -> None: @@ -1043,10 +1097,8 @@ class Entrance: return False - def connect(self, region: Region, addresses: Any = None, target: Any = None) -> None: + def connect(self, region: Region) -> None: self.connected_region = region - self.target = target - self.addresses = addresses region.entrances.append(self) def is_valid_source_transition(self, er_state: "ERPlacementState") -> bool: @@ -1203,6 +1255,48 @@ class Region: for location, address in locations.items(): self.locations.append(location_type(self.player, location, address, self)) + def add_event( + self, + location_name: str, + item_name: str | None = None, + rule: Callable[[CollectionState], bool] | None = None, + location_type: type[Location] | None = None, + item_type: type[Item] | None = None, + show_in_spoiler: bool = True, + ) -> Item: + """ + Adds an event location/item pair to the region. + + :param location_name: Name for the event location. + :param item_name: Name for the event item. If not provided, defaults to location_name. + :param rule: Callable to determine access for this event location within its region. + :param location_type: Location class to create the event location with. Defaults to BaseClasses.Location. + :param item_type: Item class to create the event item with. Defaults to BaseClasses.Item. + :param show_in_spoiler: Will be passed along to the created event Location's show_in_spoiler attribute. + :return: The created Event Item + """ + if location_type is None: + location_type = Location + + if item_name is None: + item_name = location_name + + if item_type is None: + item_type = Item + + event_location = location_type(self.player, location_name, None, self) + event_location.show_in_spoiler = show_in_spoiler + if rule is not None: + event_location.access_rule = rule + + event_item = item_type(item_name, ItemClassification.progression, None, self.player) + + event_location.place_locked_item(event_item) + + self.locations.append(event_location) + + return event_item + def connect(self, connecting_region: Region, name: Optional[str] = None, rule: Optional[Callable[[CollectionState], bool]] = None) -> Entrance: """ @@ -1513,21 +1607,19 @@ class Spoiler: # in the second phase, we cull each sphere such that the game is still beatable, # reducing each range of influence to the bare minimum required inside it - restore_later: Dict[Location, Item] = {} + required_locations = {location for sphere in collection_spheres for location in sphere} for num, sphere in reversed(tuple(enumerate(collection_spheres))): to_delete: Set[Location] = set() for location in sphere: - # we remove the item at location and check if game is still beatable + # we remove the location from required_locations to sweep from, and check if the game is still beatable logging.debug('Checking if %s (Player %d) is required to beat the game.', location.item.name, location.item.player) - old_item = location.item - location.item = None - if multiworld.can_beat_game(state_cache[num]): + required_locations.remove(location) + if multiworld.can_beat_game(state_cache[num], required_locations): to_delete.add(location) - restore_later[location] = old_item else: # still required, got to keep it around - location.item = old_item + required_locations.add(location) # cull entries in spheres for spoiler walkthrough at end sphere -= to_delete @@ -1544,7 +1636,7 @@ class Spoiler: logging.debug('Checking if %s (Player %d) is required to beat the game.', item.name, item.player) precollected_items.remove(item) multiworld.state.remove(item) - if not multiworld.can_beat_game(): + if not multiworld.can_beat_game(multiworld.state, required_locations): # Add the item back into `precollected_items` and collect it into `multiworld.state`. multiworld.push_precollected(item) else: @@ -1586,9 +1678,6 @@ class Spoiler: self.create_paths(state, collection_spheres) # repair the multiworld again - for location, item in restore_later.items(): - location.item = item - for item in removed_precollected: multiworld.push_precollected(item) diff --git a/CommonClient.py b/CommonClient.py index b622fb939b..3a5f51aeee 100644 --- a/CommonClient.py +++ b/CommonClient.py @@ -196,25 +196,11 @@ class CommonContext: self.lookup_type: typing.Literal["item", "location"] = lookup_type self._unknown_item: typing.Callable[[int], str] = lambda key: f"Unknown {lookup_type} (ID: {key})" self._archipelago_lookup: typing.Dict[int, str] = {} - self._flat_store: typing.Dict[int, str] = Utils.KeyedDefaultDict(self._unknown_item) self._game_store: typing.Dict[str, typing.ChainMap[int, str]] = collections.defaultdict( lambda: collections.ChainMap(self._archipelago_lookup, Utils.KeyedDefaultDict(self._unknown_item))) - self.warned: bool = False # noinspection PyTypeChecker def __getitem__(self, key: str) -> typing.Mapping[int, str]: - # TODO: In a future version (0.6.0?) this should be simplified by removing implicit id lookups support. - if isinstance(key, int): - if not self.warned: - # Use warnings instead of logger to avoid deprecation message from appearing on user side. - self.warned = True - warnings.warn(f"Implicit name lookup by id only is deprecated and only supported to maintain " - f"backwards compatibility for now. If multiple games share the same id for a " - f"{self.lookup_type}, name could be incorrect. Please use " - f"`{self.lookup_type}_names.lookup_in_game()` or " - f"`{self.lookup_type}_names.lookup_in_slot()` instead.") - return self._flat_store[key] # type: ignore - return self._game_store[key] def __len__(self) -> int: @@ -254,7 +240,6 @@ class CommonContext: id_to_name_lookup_table = Utils.KeyedDefaultDict(self._unknown_item) id_to_name_lookup_table.update({code: name for name, code in name_to_id_lookup_table.items()}) self._game_store[game] = collections.ChainMap(self._archipelago_lookup, id_to_name_lookup_table) - self._flat_store.update(id_to_name_lookup_table) # Only needed for legacy lookup method. if game == "Archipelago": # Keep track of the Archipelago data package separately so if it gets updated in a custom datapackage, # it updates in all chain maps automatically. @@ -281,38 +266,71 @@ class CommonContext: last_death_link: float = time.time() # last send/received death link on AP layer # remaining type info - slot_info: typing.Dict[int, NetworkSlot] - server_address: typing.Optional[str] - password: typing.Optional[str] - hint_cost: typing.Optional[int] - hint_points: typing.Optional[int] - player_names: typing.Dict[int, str] + slot_info: dict[int, NetworkSlot] + """Slot Info from the server for the current connection""" + server_address: str | None + """Autoconnect address provided by the ctx constructor""" + password: str | None + """Password used for Connecting, expected by server_auth""" + hint_cost: int | None + """Current Hint Cost per Hint from the server""" + hint_points: int | None + """Current avaliable Hint Points from the server""" + player_names: dict[int, str] + """Current lookup of slot number to player display name from server (includes aliases)""" finished_game: bool + """ + Bool to signal that status should be updated to Goal after reconnecting + to be used to ensure that a StatusUpdate packet does not get lost when disconnected + """ ready: bool - team: typing.Optional[int] - slot: typing.Optional[int] - auth: typing.Optional[str] - seed_name: typing.Optional[str] + """Bool to keep track of state for the /ready command""" + team: int | None + """Team number of currently connected slot""" + slot: int | None + """Slot number of currently connected slot""" + auth: str | None + """Name used in Connect packet""" + seed_name: str | None + """Seed name that will be validated on opening a socket if present""" # locations - locations_checked: typing.Set[int] # local state - locations_scouted: typing.Set[int] - items_received: typing.List[NetworkItem] - missing_locations: typing.Set[int] # server state - checked_locations: typing.Set[int] # server state - server_locations: typing.Set[int] # all locations the server knows of, missing_location | checked_locations - locations_info: typing.Dict[int, NetworkItem] + locations_checked: set[int] + """ + Local container of location ids checked to signal that LocationChecks should be resent after reconnecting + to be used to ensure that a LocationChecks packet does not get lost when disconnected + """ + locations_scouted: set[int] + """ + Local container of location ids scouted to signal that LocationScouts should be resent after reconnecting + to be used to ensure that a LocationScouts packet does not get lost when disconnected + """ + items_received: list[NetworkItem] + """List of NetworkItems recieved from the server""" + missing_locations: set[int] + """Container of Locations that are unchecked per server state""" + checked_locations: set[int] + """Container of Locations that are checked per server state""" + server_locations: set[int] + """Container of Locations that exist per server state; a combination between missing and checked locations""" + locations_info: dict[int, NetworkItem] + """Dict of location id: NetworkItem info from LocationScouts request""" # data storage - stored_data: typing.Dict[str, typing.Any] - stored_data_notification_keys: typing.Set[str] + stored_data: dict[str, typing.Any] + """ + Data Storage values by key that were retrieved from the server + any keys subscribed to with SetNotify will be kept up to date + """ + stored_data_notification_keys: set[str] + """Current container of watched Data Storage keys, managed by ctx.set_notify""" # internals - # current message box through kvui _messagebox: typing.Optional["kvui.MessageBox"] = None - # message box reporting a loss of connection + """Current message box through kvui""" _messagebox_connection_loss: typing.Optional["kvui.MessageBox"] = None + """Message box reporting a loss of connection""" def __init__(self, server_address: typing.Optional[str] = None, password: typing.Optional[str] = None) -> None: # server state @@ -356,7 +374,6 @@ class CommonContext: self.item_names = self.NameLookupDict(self, "item") self.location_names = self.NameLookupDict(self, "location") - self.versions = {} self.checksums = {} self.jsontotextparser = JSONtoTextParser(self) @@ -571,7 +588,6 @@ class CommonContext: # DataPackage async def prepare_data_package(self, relevant_games: typing.Set[str], - remote_date_package_versions: typing.Dict[str, int], remote_data_package_checksums: typing.Dict[str, str]): """Validate that all data is present for the current multiworld. Download, assimilate and cache missing data from the server.""" @@ -580,33 +596,26 @@ class CommonContext: needed_updates: typing.Set[str] = set() for game in relevant_games: - if game not in remote_date_package_versions and game not in remote_data_package_checksums: + if game not in remote_data_package_checksums: continue - remote_version: int = remote_date_package_versions.get(game, 0) remote_checksum: typing.Optional[str] = remote_data_package_checksums.get(game) - if remote_version == 0 and not remote_checksum: # custom data package and no checksum for this game + if not remote_checksum: # custom data package and no checksum for this game needed_updates.add(game) continue - cached_version: int = self.versions.get(game, 0) cached_checksum: typing.Optional[str] = self.checksums.get(game) # no action required if cached version is new enough - if (not remote_checksum and (remote_version > cached_version or remote_version == 0)) \ - or remote_checksum != cached_checksum: - local_version: int = network_data_package["games"].get(game, {}).get("version", 0) + if remote_checksum != cached_checksum: local_checksum: typing.Optional[str] = network_data_package["games"].get(game, {}).get("checksum") - if ((remote_checksum or remote_version <= local_version and remote_version != 0) - and remote_checksum == local_checksum): + if remote_checksum == local_checksum: self.update_game(network_data_package["games"][game], game) else: cached_game = Utils.load_data_package_for_checksum(game, remote_checksum) - cache_version: int = cached_game.get("version", 0) cache_checksum: typing.Optional[str] = cached_game.get("checksum") # download remote version if cache is not new enough - if (not remote_checksum and (remote_version > cache_version or remote_version == 0)) \ - or remote_checksum != cache_checksum: + if remote_checksum != cache_checksum: needed_updates.add(game) else: self.update_game(cached_game, game) @@ -616,7 +625,6 @@ class CommonContext: def update_game(self, game_package: dict, game: str): self.item_names.update_game(game, game_package["item_name_to_id"]) self.location_names.update_game(game, game_package["location_name_to_id"]) - self.versions[game] = game_package.get("version", 0) self.checksums[game] = game_package.get("checksum") def update_data_package(self, data_package: dict): @@ -887,9 +895,8 @@ async def process_server_cmd(ctx: CommonContext, args: dict): logger.info(' %s (Player %d)' % (network_player.alias, network_player.slot)) # update data package - data_package_versions = args.get("datapackage_versions", {}) data_package_checksums = args.get("datapackage_checksums", {}) - await ctx.prepare_data_package(set(args["games"]), data_package_versions, data_package_checksums) + await ctx.prepare_data_package(set(args["games"]), data_package_checksums) await ctx.server_auth(args['password']) diff --git a/FF1Client.py b/FF1Client.py deleted file mode 100644 index 748a95b72c..0000000000 --- a/FF1Client.py +++ /dev/null @@ -1,267 +0,0 @@ -import asyncio -import copy -import json -import time -from asyncio import StreamReader, StreamWriter -from typing import List - - -import Utils -from Utils import async_start -from CommonClient import CommonContext, server_loop, gui_enabled, ClientCommandProcessor, logger, \ - get_base_parser - -SYSTEM_MESSAGE_ID = 0 - -CONNECTION_TIMING_OUT_STATUS = "Connection timing out. Please restart your emulator, then restart connector_ff1.lua" -CONNECTION_REFUSED_STATUS = "Connection Refused. Please start your emulator and make sure connector_ff1.lua is running" -CONNECTION_RESET_STATUS = "Connection was reset. Please restart your emulator, then restart connector_ff1.lua" -CONNECTION_TENTATIVE_STATUS = "Initial Connection Made" -CONNECTION_CONNECTED_STATUS = "Connected" -CONNECTION_INITIAL_STATUS = "Connection has not been initiated" - -DISPLAY_MSGS = True - - -class FF1CommandProcessor(ClientCommandProcessor): - def __init__(self, ctx: CommonContext): - super().__init__(ctx) - - def _cmd_nes(self): - """Check NES Connection State""" - if isinstance(self.ctx, FF1Context): - logger.info(f"NES Status: {self.ctx.nes_status}") - - def _cmd_toggle_msgs(self): - """Toggle displaying messages in EmuHawk""" - global DISPLAY_MSGS - DISPLAY_MSGS = not DISPLAY_MSGS - logger.info(f"Messages are now {'enabled' if DISPLAY_MSGS else 'disabled'}") - - -class FF1Context(CommonContext): - command_processor = FF1CommandProcessor - game = 'Final Fantasy' - items_handling = 0b111 # full remote - - def __init__(self, server_address, password): - super().__init__(server_address, password) - self.nes_streams: (StreamReader, StreamWriter) = None - self.nes_sync_task = None - self.messages = {} - self.locations_array = None - self.nes_status = CONNECTION_INITIAL_STATUS - self.awaiting_rom = False - self.display_msgs = True - - async def server_auth(self, password_requested: bool = False): - if password_requested and not self.password: - await super(FF1Context, self).server_auth(password_requested) - if not self.auth: - self.awaiting_rom = True - logger.info('Awaiting connection to NES to get Player information') - return - - await self.send_connect() - - def _set_message(self, msg: str, msg_id: int): - if DISPLAY_MSGS: - self.messages[time.time(), msg_id] = msg - - def on_package(self, cmd: str, args: dict): - if cmd == 'Connected': - async_start(parse_locations(self.locations_array, self, True)) - elif cmd == 'Print': - msg = args['text'] - if ': !' not in msg: - self._set_message(msg, SYSTEM_MESSAGE_ID) - - def on_print_json(self, args: dict): - if self.ui: - self.ui.print_json(copy.deepcopy(args["data"])) - else: - text = self.jsontotextparser(copy.deepcopy(args["data"])) - logger.info(text) - relevant = args.get("type", None) in {"Hint", "ItemSend"} - if relevant: - item = args["item"] - # goes to this world - if self.slot_concerns_self(args["receiving"]): - relevant = True - # found in this world - elif self.slot_concerns_self(item.player): - relevant = True - # not related - else: - relevant = False - if relevant: - item = args["item"] - msg = self.raw_text_parser(copy.deepcopy(args["data"])) - self._set_message(msg, item.item) - - def run_gui(self): - from kvui import GameManager - - class FF1Manager(GameManager): - logging_pairs = [ - ("Client", "Archipelago") - ] - base_title = "Archipelago Final Fantasy 1 Client" - - self.ui = FF1Manager(self) - self.ui_task = asyncio.create_task(self.ui.async_run(), name="UI") - - -def get_payload(ctx: FF1Context): - current_time = time.time() - return json.dumps( - { - "items": [item.item for item in ctx.items_received], - "messages": {f'{key[0]}:{key[1]}': value for key, value in ctx.messages.items() - if key[0] > current_time - 10} - } - ) - - -async def parse_locations(locations_array: List[int], ctx: FF1Context, force: bool): - if locations_array == ctx.locations_array and not force: - return - else: - # print("New values") - ctx.locations_array = locations_array - locations_checked = [] - if len(locations_array) > 0xFE and locations_array[0xFE] & 0x02 != 0 and not ctx.finished_game: - await ctx.send_msgs([ - {"cmd": "StatusUpdate", - "status": 30} - ]) - ctx.finished_game = True - for location in ctx.missing_locations: - # index will be - 0x100 or 0x200 - index = location - if location < 0x200: - # Location is a chest - index -= 0x100 - flag = 0x04 - else: - # Location is an NPC - index -= 0x200 - flag = 0x02 - - # print(f"Location: {ctx.location_names[location]}") - # print(f"Index: {str(hex(index))}") - # print(f"value: {locations_array[index] & flag != 0}") - if locations_array[index] & flag != 0: - locations_checked.append(location) - if locations_checked: - # print([ctx.location_names[location] for location in locations_checked]) - await ctx.send_msgs([ - {"cmd": "LocationChecks", - "locations": locations_checked} - ]) - - -async def nes_sync_task(ctx: FF1Context): - logger.info("Starting nes connector. Use /nes for status information") - while not ctx.exit_event.is_set(): - error_status = None - if ctx.nes_streams: - (reader, writer) = ctx.nes_streams - msg = get_payload(ctx).encode() - writer.write(msg) - writer.write(b'\n') - try: - await asyncio.wait_for(writer.drain(), timeout=1.5) - try: - # Data will return a dict with up to two fields: - # 1. A keepalive response of the Players Name (always) - # 2. An array representing the memory values of the locations area (if in game) - data = await asyncio.wait_for(reader.readline(), timeout=5) - data_decoded = json.loads(data.decode()) - # print(data_decoded) - if ctx.game is not None and 'locations' in data_decoded: - # Not just a keep alive ping, parse - async_start(parse_locations(data_decoded['locations'], ctx, False)) - if not ctx.auth: - ctx.auth = ''.join([chr(i) for i in data_decoded['playerName'] if i != 0]) - if ctx.auth == '': - logger.info("Invalid ROM detected. No player name built into the ROM. Please regenerate" - "the ROM using the same link but adding your slot name") - if ctx.awaiting_rom: - await ctx.server_auth(False) - except asyncio.TimeoutError: - logger.debug("Read Timed Out, Reconnecting") - error_status = CONNECTION_TIMING_OUT_STATUS - writer.close() - ctx.nes_streams = None - except ConnectionResetError as e: - logger.debug("Read failed due to Connection Lost, Reconnecting") - error_status = CONNECTION_RESET_STATUS - writer.close() - ctx.nes_streams = None - except TimeoutError: - logger.debug("Connection Timed Out, Reconnecting") - error_status = CONNECTION_TIMING_OUT_STATUS - writer.close() - ctx.nes_streams = None - except ConnectionResetError: - logger.debug("Connection Lost, Reconnecting") - error_status = CONNECTION_RESET_STATUS - writer.close() - ctx.nes_streams = None - if ctx.nes_status == CONNECTION_TENTATIVE_STATUS: - if not error_status: - logger.info("Successfully Connected to NES") - ctx.nes_status = CONNECTION_CONNECTED_STATUS - else: - ctx.nes_status = f"Was tentatively connected but error occured: {error_status}" - elif error_status: - ctx.nes_status = error_status - logger.info("Lost connection to nes and attempting to reconnect. Use /nes for status updates") - else: - try: - logger.debug("Attempting to connect to NES") - ctx.nes_streams = await asyncio.wait_for(asyncio.open_connection("localhost", 52980), timeout=10) - ctx.nes_status = CONNECTION_TENTATIVE_STATUS - except TimeoutError: - logger.debug("Connection Timed Out, Trying Again") - ctx.nes_status = CONNECTION_TIMING_OUT_STATUS - continue - except ConnectionRefusedError: - logger.debug("Connection Refused, Trying Again") - ctx.nes_status = CONNECTION_REFUSED_STATUS - continue - - -if __name__ == '__main__': - # Text Mode to use !hint and such with games that have no text entry - Utils.init_logging("FF1Client") - - options = Utils.get_options() - DISPLAY_MSGS = options["ffr_options"]["display_msgs"] - - async def main(args): - ctx = FF1Context(args.connect, args.password) - ctx.server_task = asyncio.create_task(server_loop(ctx), name="ServerLoop") - if gui_enabled: - ctx.run_gui() - ctx.run_cli() - ctx.nes_sync_task = asyncio.create_task(nes_sync_task(ctx), name="NES Sync") - - await ctx.exit_event.wait() - ctx.server_address = None - - await ctx.shutdown() - - if ctx.nes_sync_task: - await ctx.nes_sync_task - - - import colorama - - parser = get_base_parser() - args = parser.parse_args() - colorama.just_fix_windows_console() - - asyncio.run(main(args)) - colorama.deinit() diff --git a/FactorioClient.py b/FactorioClient.py deleted file mode 100644 index 070ca50326..0000000000 --- a/FactorioClient.py +++ /dev/null @@ -1,12 +0,0 @@ -from __future__ import annotations - -import ModuleUpdate -ModuleUpdate.update() - -from worlds.factorio.Client import check_stdin, launch -import Utils - -if __name__ == "__main__": - Utils.init_logging("FactorioClient", exception_logger="Client") - check_stdin() - launch() diff --git a/Fill.py b/Fill.py index fe39b74fbe..d0a42c07eb 100644 --- a/Fill.py +++ b/Fill.py @@ -4,7 +4,7 @@ import logging import typing from collections import Counter, deque -from BaseClasses import CollectionState, Item, Location, LocationProgressType, MultiWorld +from BaseClasses import CollectionState, Item, Location, LocationProgressType, MultiWorld, PlandoItemBlock from Options import Accessibility from worlds.AutoWorld import call_all @@ -75,9 +75,11 @@ def fill_restrictive(multiworld: MultiWorld, base_state: CollectionState, locati items_to_place.append(reachable_items[next_player].pop()) for item in items_to_place: - for p, pool_item in enumerate(item_pool): + # The items added into `reachable_items` are placed starting from the end of each deque in + # `reachable_items`, so the items being placed are more likely to be found towards the end of `item_pool`. + for p, pool_item in enumerate(reversed(item_pool), start=1): if pool_item is item: - item_pool.pop(p) + del item_pool[-p] break maximum_exploration_state = sweep_from_pool( @@ -98,7 +100,7 @@ def fill_restrictive(multiworld: MultiWorld, base_state: CollectionState, locati # if minimal accessibility, only check whether location is reachable if game not beatable if multiworld.worlds[item_to_place.player].options.accessibility == Accessibility.option_minimal: perform_access_check = not multiworld.has_beaten_game(maximum_exploration_state, - item_to_place.player) \ + item_to_place.player) \ if single_player_placement else not has_beaten_game else: perform_access_check = True @@ -136,32 +138,21 @@ def fill_restrictive(multiworld: MultiWorld, base_state: CollectionState, locati # to clean that up later, so there is a chance generation fails. if (not single_player_placement or location.player == item_to_place.player) \ and location.can_fill(swap_state, item_to_place, perform_access_check): + # Add this item to the existing placement, and + # add the old item to the back of the queue + spot_to_fill = placements.pop(i) - # Verify placing this item won't reduce available locations, which would be a useless swap. - prev_state = swap_state.copy() - prev_loc_count = len( - multiworld.get_reachable_locations(prev_state)) + swap_count += 1 + swapped_items[placed_item.player, placed_item.name, unsafe] = swap_count - swap_state.collect(item_to_place, True) - new_loc_count = len( - multiworld.get_reachable_locations(swap_state)) + reachable_items[placed_item.player].appendleft( + placed_item) + item_pool.append(placed_item) - if new_loc_count >= prev_loc_count: - # Add this item to the existing placement, and - # add the old item to the back of the queue - spot_to_fill = placements.pop(i) + # cleanup at the end to hopefully get better errors + cleanup_required = True - swap_count += 1 - swapped_items[placed_item.player, placed_item.name, unsafe] = swap_count - - reachable_items[placed_item.player].appendleft( - placed_item) - item_pool.append(placed_item) - - # cleanup at the end to hopefully get better errors - cleanup_required = True - - break + break # Item can't be placed here, restore original item location.item = placed_item @@ -240,7 +231,7 @@ def remaining_fill(multiworld: MultiWorld, unplaced_items: typing.List[Item] = [] placements: typing.List[Location] = [] swapped_items: typing.Counter[typing.Tuple[int, str]] = Counter() - total = min(len(itempool), len(locations)) + total = min(len(itempool), len(locations)) placed = 0 # Optimisation: Decide whether to do full location.can_fill check (respect excluded), or only check the item rule @@ -341,8 +332,10 @@ def fast_fill(multiworld: MultiWorld, def accessibility_corrections(multiworld: MultiWorld, state: CollectionState, locations, pool=[]): maximum_exploration_state = sweep_from_pool(state, pool) - minimal_players = {player for player in multiworld.player_ids if multiworld.worlds[player].options.accessibility == "minimal"} - unreachable_locations = [location for location in multiworld.get_locations() if location.player in minimal_players and + minimal_players = {player for player in multiworld.player_ids if + multiworld.worlds[player].options.accessibility == "minimal"} + unreachable_locations = [location for location in multiworld.get_locations() if + location.player in minimal_players and not location.can_reach(maximum_exploration_state)] for location in unreachable_locations: if (location.item is not None and location.item.advancement and location.address is not None and not @@ -363,7 +356,7 @@ def inaccessible_location_rules(multiworld: MultiWorld, state: CollectionState, unreachable_locations = [location for location in locations if not location.can_reach(maximum_exploration_state)] if unreachable_locations: def forbid_important_item_rule(item: Item): - return not ((item.classification & 0b0011) and multiworld.worlds[item.player].options.accessibility != 'minimal') + return not ((item.classification & 0b0011) and multiworld.worlds[item.player].options.accessibility != "minimal") for location in unreachable_locations: add_item_rule(location, forbid_important_item_rule) @@ -675,9 +668,9 @@ def balance_multiworld_progression(multiworld: MultiWorld) -> None: if multiworld.worlds[player].options.progression_balancing > 0 } if not balanceable_players: - logging.info('Skipping multiworld progression balancing.') + logging.info("Skipping multiworld progression balancing.") else: - logging.info(f'Balancing multiworld progression for {len(balanceable_players)} Players.') + logging.info(f"Balancing multiworld progression for {len(balanceable_players)} Players.") logging.debug(balanceable_players) state: CollectionState = CollectionState(multiworld) checked_locations: typing.Set[Location] = set() @@ -775,7 +768,7 @@ def balance_multiworld_progression(multiworld: MultiWorld) -> None: if player in threshold_percentages): break elif not balancing_sphere: - raise RuntimeError('Not all required items reachable. Something went terribly wrong here.') + raise RuntimeError("Not all required items reachable. Something went terribly wrong here.") # Gather a set of locations which we can swap items into unlocked_locations: typing.Dict[int, typing.Set[Location]] = collections.defaultdict(set) for l in unchecked_locations: @@ -791,8 +784,8 @@ def balance_multiworld_progression(multiworld: MultiWorld) -> None: testing = items_to_test.pop() reducing_state = state.copy() for location in itertools.chain(( - l for l in items_to_replace - if l.item.player == player + l for l in items_to_replace + if l.item.player == player ), items_to_test): reducing_state.collect(location.item, True, location) @@ -865,52 +858,30 @@ def swap_location_item(location_1: Location, location_2: Location, check_locked: location_2.item.location = location_2 -def distribute_planned(multiworld: MultiWorld) -> None: - def warn(warning: str, force: typing.Union[bool, str]) -> None: - if force in [True, 'fail', 'failure', 'none', False, 'warn', 'warning']: - logging.warning(f'{warning}') +def parse_planned_blocks(multiworld: MultiWorld) -> dict[int, list[PlandoItemBlock]]: + def warn(warning: str, force: bool | str) -> None: + if isinstance(force, bool): + logging.warning(f"{warning}") else: - logging.debug(f'{warning}') + logging.debug(f"{warning}") - def failed(warning: str, force: typing.Union[bool, str]) -> None: - if force in [True, 'fail', 'failure']: + def failed(warning: str, force: bool | str) -> None: + if force is True: raise Exception(warning) else: warn(warning, force) - swept_state = multiworld.state.copy() - swept_state.sweep_for_advancements() - reachable = frozenset(multiworld.get_reachable_locations(swept_state)) - early_locations: typing.Dict[int, typing.List[str]] = collections.defaultdict(list) - non_early_locations: typing.Dict[int, typing.List[str]] = collections.defaultdict(list) - for loc in multiworld.get_unfilled_locations(): - if loc in reachable: - early_locations[loc.player].append(loc.name) - else: # not reachable with swept state - non_early_locations[loc.player].append(loc.name) - world_name_lookup = multiworld.world_name_lookup - block_value = typing.Union[typing.List[str], typing.Dict[str, typing.Any], str] - plando_blocks: typing.List[typing.Dict[str, typing.Any]] = [] - player_ids = set(multiworld.player_ids) + plando_blocks: dict[int, list[PlandoItemBlock]] = dict() + player_ids: set[int] = set(multiworld.player_ids) for player in player_ids: - for block in multiworld.plando_items[player]: - block['player'] = player - if 'force' not in block: - block['force'] = 'silent' - if 'from_pool' not in block: - block['from_pool'] = True - elif not isinstance(block['from_pool'], bool): - from_pool_type = type(block['from_pool']) - raise Exception(f'Plando "from_pool" has to be boolean, not {from_pool_type} for player {player}.') - if 'world' not in block: - target_world = False - else: - target_world = block['world'] - + plando_blocks[player] = [] + for block in multiworld.worlds[player].options.plando_items: + new_block: PlandoItemBlock = PlandoItemBlock(player, block.from_pool, block.force) + target_world = block.world if target_world is False or multiworld.players == 1: # target own world - worlds: typing.Set[int] = {player} + worlds: set[int] = {player} elif target_world is True: # target any worlds besides own worlds = set(multiworld.player_ids) - {player} elif target_world is None: # target all worlds @@ -920,172 +891,197 @@ def distribute_planned(multiworld: MultiWorld) -> None: for listed_world in target_world: if listed_world not in world_name_lookup: failed(f"Cannot place item to {target_world}'s world as that world does not exist.", - block['force']) + block.force) continue worlds.add(world_name_lookup[listed_world]) elif type(target_world) == int: # target world by slot number if target_world not in range(1, multiworld.players + 1): failed( f"Cannot place item in world {target_world} as it is not in range of (1, {multiworld.players})", - block['force']) + block.force) continue worlds = {target_world} else: # target world by slot name if target_world not in world_name_lookup: failed(f"Cannot place item to {target_world}'s world as that world does not exist.", - block['force']) + block.force) continue worlds = {world_name_lookup[target_world]} - block['world'] = worlds + new_block.worlds = worlds - items: block_value = [] - if "items" in block: - items = block["items"] - if 'count' not in block: - block['count'] = False - elif "item" in block: - items = block["item"] - if 'count' not in block: - block['count'] = 1 - else: - failed("You must specify at least one item to place items with plando.", block['force']) - continue + items: list[str] | dict[str, typing.Any] = block.items if isinstance(items, dict): - item_list: typing.List[str] = [] + item_list: list[str] = [] for key, value in items.items(): if value is True: value = multiworld.itempool.count(multiworld.worlds[player].create_item(key)) item_list += [key] * value items = item_list - if isinstance(items, str): - items = [items] - block['items'] = items + new_block.items = items - locations: block_value = [] - if 'location' in block: - locations = block['location'] # just allow 'location' to keep old yamls compatible - elif 'locations' in block: - locations = block['locations'] + locations: list[str] = block.locations if isinstance(locations, str): locations = [locations] - if isinstance(locations, dict): - location_list = [] - for key, value in locations.items(): - location_list += [key] * value - locations = location_list + locations_from_groups: list[str] = [] + resolved_locations: list[Location] = [] + for target_player in worlds: + world_locations = multiworld.get_unfilled_locations(target_player) + for group in multiworld.worlds[target_player].location_name_groups: + if group in locations: + locations_from_groups.extend(multiworld.worlds[target_player].location_name_groups[group]) + resolved_locations.extend(location for location in world_locations + if location.name in [*locations, *locations_from_groups]) + new_block.locations = sorted(dict.fromkeys(locations)) + new_block.resolved_locations = sorted(set(resolved_locations)) + count = block.count + if not count: + count = len(new_block.items) + if isinstance(count, int): + count = {"min": count, "max": count} + if "min" not in count: + count["min"] = 0 + if "max" not in count: + count["max"] = len(new_block.items) + + new_block.count = count + plando_blocks[player].append(new_block) + + return plando_blocks + + +def resolve_early_locations_for_planned(multiworld: MultiWorld): + def warn(warning: str, force: bool | str) -> None: + if isinstance(force, bool): + logging.warning(f"{warning}") + else: + logging.debug(f"{warning}") + + def failed(warning: str, force: bool | str) -> None: + if force is True: + raise Exception(warning) + else: + warn(warning, force) + + swept_state = multiworld.state.copy() + swept_state.sweep_for_advancements() + reachable = frozenset(multiworld.get_reachable_locations(swept_state)) + early_locations: dict[int, list[Location]] = collections.defaultdict(list) + non_early_locations: dict[int, list[Location]] = collections.defaultdict(list) + for loc in multiworld.get_unfilled_locations(): + if loc in reachable: + early_locations[loc.player].append(loc) + else: # not reachable with swept state + non_early_locations[loc.player].append(loc) + + for player in multiworld.plando_item_blocks: + removed = [] + for block in multiworld.plando_item_blocks[player]: + locations = block.locations + resolved_locations = block.resolved_locations + worlds = block.worlds if "early_locations" in locations: - locations.remove("early_locations") for target_player in worlds: - locations += early_locations[target_player] + resolved_locations += early_locations[target_player] if "non_early_locations" in locations: - locations.remove("non_early_locations") for target_player in worlds: - locations += non_early_locations[target_player] + resolved_locations += non_early_locations[target_player] - block['locations'] = list(dict.fromkeys(locations)) + if block.count["max"] > len(block.items): + count = block.count["max"] + failed(f"Plando count {count} greater than items specified", block.force) + block.count["max"] = len(block.items) + if block.count["min"] > len(block.items): + block.count["min"] = len(block.items) + if block.count["max"] > len(block.resolved_locations) > 0: + count = block.count["max"] + failed(f"Plando count {count} greater than locations specified", block.force) + block.count["max"] = len(block.resolved_locations) + if block.count["min"] > len(block.resolved_locations): + block.count["min"] = len(block.resolved_locations) + block.count["target"] = multiworld.random.randint(block.count["min"], + block.count["max"]) - if not block['count']: - block['count'] = (min(len(block['items']), len(block['locations'])) if - len(block['locations']) > 0 else len(block['items'])) - if isinstance(block['count'], int): - block['count'] = {'min': block['count'], 'max': block['count']} - if 'min' not in block['count']: - block['count']['min'] = 0 - if 'max' not in block['count']: - block['count']['max'] = (min(len(block['items']), len(block['locations'])) if - len(block['locations']) > 0 else len(block['items'])) - if block['count']['max'] > len(block['items']): - count = block['count'] - failed(f"Plando count {count} greater than items specified", block['force']) - block['count'] = len(block['items']) - if block['count']['max'] > len(block['locations']) > 0: - count = block['count'] - failed(f"Plando count {count} greater than locations specified", block['force']) - block['count'] = len(block['locations']) - block['count']['target'] = multiworld.random.randint(block['count']['min'], block['count']['max']) + if not block.count["target"]: + removed.append(block) - if block['count']['target'] > 0: - plando_blocks.append(block) + for block in removed: + multiworld.plando_item_blocks[player].remove(block) + + +def distribute_planned_blocks(multiworld: MultiWorld, plando_blocks: list[PlandoItemBlock]): + def warn(warning: str, force: bool | str) -> None: + if isinstance(force, bool): + logging.warning(f"{warning}") + else: + logging.debug(f"{warning}") + + def failed(warning: str, force: bool | str) -> None: + if force is True: + raise Exception(warning) + else: + warn(warning, force) # shuffle, but then sort blocks by number of locations minus number of items, # so less-flexible blocks get priority multiworld.random.shuffle(plando_blocks) - plando_blocks.sort(key=lambda block: (len(block['locations']) - block['count']['target'] - if len(block['locations']) > 0 - else len(multiworld.get_unfilled_locations(player)) - block['count']['target'])) - + plando_blocks.sort(key=lambda block: (len(block.resolved_locations) - block.count["target"] + if len(block.resolved_locations) > 0 + else len(multiworld.get_unfilled_locations(block.player)) - + block.count["target"])) for placement in plando_blocks: - player = placement['player'] + player = placement.player try: - worlds = placement['world'] - locations = placement['locations'] - items = placement['items'] - maxcount = placement['count']['target'] - from_pool = placement['from_pool'] + worlds = placement.worlds + locations = placement.resolved_locations + items = placement.items + maxcount = placement.count["target"] + from_pool = placement.from_pool - candidates = list(multiworld.get_unfilled_locations_for_players(locations, sorted(worlds))) - multiworld.random.shuffle(candidates) - multiworld.random.shuffle(items) - count = 0 - err: typing.List[str] = [] - successful_pairs: typing.List[typing.Tuple[int, Item, Location]] = [] - claimed_indices: typing.Set[typing.Optional[int]] = set() - for item_name in items: - index_to_delete: typing.Optional[int] = None - if from_pool: - try: - # If from_pool, try to find an existing item with this name & player in the itempool and use it - index_to_delete, item = next( - (i, item) for i, item in enumerate(multiworld.itempool) - if item.player == player and item.name == item_name and i not in claimed_indices - ) - except StopIteration: - warn( - f"Could not remove {item_name} from pool for {multiworld.player_name[player]} as it's already missing from it.", - placement['force']) - item = multiworld.worlds[player].create_item(item_name) - else: - item = multiworld.worlds[player].create_item(item_name) - - for location in reversed(candidates): - if (location.address is None) == (item.code is None): # either both None or both not None - if not location.item: - if location.item_rule(item): - if location.can_fill(multiworld.state, item, False): - successful_pairs.append((index_to_delete, item, location)) - claimed_indices.add(index_to_delete) - candidates.remove(location) - count = count + 1 - break - else: - err.append(f"Can't place item at {location} due to fill condition not met.") - else: - err.append(f"{item_name} not allowed at {location}.") - else: - err.append(f"Cannot place {item_name} into already filled location {location}.") + item_candidates = [] + if from_pool: + instances = [item for item in multiworld.itempool if item.player == player and item.name in items] + for item in multiworld.random.sample(items, maxcount): + candidate = next((i for i in instances if i.name == item), None) + if candidate is None: + warn(f"Could not remove {item} from pool for {multiworld.player_name[player]} as " + f"it's already missing from it", placement.force) + candidate = multiworld.worlds[player].create_item(item) else: - err.append(f"Mismatch between {item_name} and {location}, only one is an event.") - - if count == maxcount: - break - if count < placement['count']['min']: - m = placement['count']['min'] - failed( - f"Plando block failed to place {m - count} of {m} item(s) for {multiworld.player_name[player]}, error(s): {' '.join(err)}", - placement['force']) - - # Sort indices in reverse so we can remove them one by one - successful_pairs = sorted(successful_pairs, key=lambda successful_pair: successful_pair[0] or 0, reverse=True) - - for (index, item, location) in successful_pairs: - multiworld.push_item(location, item, collect=False) - location.locked = True - logging.debug(f"Plando placed {item} at {location}") - if index is not None: # If this item is from_pool and was found in the pool, remove it. - multiworld.itempool.pop(index) + multiworld.itempool.remove(candidate) + instances.remove(candidate) + item_candidates.append(candidate) + else: + item_candidates = [multiworld.worlds[player].create_item(item) + for item in multiworld.random.sample(items, maxcount)] + if any(item.code is None for item in item_candidates) \ + and not all(item.code is None for item in item_candidates): + failed(f"Plando block for player {player} ({multiworld.player_name[player]}) contains both " + f"event items and non-event items. " + f"Event items: {[item for item in item_candidates if item.code is None]}, " + f"Non-event items: {[item for item in item_candidates if item.code is not None]}", + placement.force) + continue + else: + is_real = item_candidates[0].code is not None + candidates = [candidate for candidate in locations if candidate.item is None + and bool(candidate.address) == is_real] + multiworld.random.shuffle(candidates) + allstate = multiworld.get_all_state(False) + mincount = placement.count["min"] + allowed_margin = len(item_candidates) - mincount + fill_restrictive(multiworld, allstate, candidates, item_candidates, lock=True, + allow_partial=True, name="Plando Main Fill") + if len(item_candidates) > allowed_margin: + failed(f"Could not place {len(item_candidates)} " + f"of {mincount + allowed_margin} item(s) " + f"for {multiworld.player_name[player]}, " + f"remaining items: {item_candidates}", + placement.force) + if from_pool: + multiworld.itempool.extend([item for item in item_candidates if item.code is not None]) except Exception as e: raise Exception( f"Error running plando for player {player} ({multiworld.player_name[player]})") from e diff --git a/Generate.py b/Generate.py index 82386644e7..f9607e328b 100644 --- a/Generate.py +++ b/Generate.py @@ -10,8 +10,8 @@ import sys import urllib.parse import urllib.request from collections import Counter -from typing import Any, Dict, Tuple, Union from itertools import chain +from typing import Any import ModuleUpdate @@ -77,7 +77,7 @@ def get_seed_name(random_source) -> str: return f"{random_source.randint(0, pow(10, seeddigits) - 1)}".zfill(seeddigits) -def main(args=None) -> Tuple[argparse.Namespace, int]: +def main(args=None) -> tuple[argparse.Namespace, int]: # __name__ == "__main__" check so unittests that already imported worlds don't trip this. if __name__ == "__main__" and "worlds" in sys.modules: raise Exception("Worlds system should not be loaded before logging init.") @@ -95,7 +95,7 @@ def main(args=None) -> Tuple[argparse.Namespace, int]: logging.info("Race mode enabled. Using non-deterministic random source.") random.seed() # reset to time-based random source - weights_cache: Dict[str, Tuple[Any, ...]] = {} + weights_cache: dict[str, tuple[Any, ...]] = {} if args.weights_file_path and os.path.exists(args.weights_file_path): try: weights_cache[args.weights_file_path] = read_weights_yamls(args.weights_file_path) @@ -180,7 +180,7 @@ def main(args=None) -> Tuple[argparse.Namespace, int]: erargs.name = {} erargs.csv_output = args.csv_output - settings_cache: Dict[str, Tuple[argparse.Namespace, ...]] = \ + settings_cache: dict[str, tuple[argparse.Namespace, ...]] = \ {fname: (tuple(roll_settings(yaml, args.plando) for yaml in yamls) if args.sameoptions else None) for fname, yamls in weights_cache.items()} @@ -212,7 +212,7 @@ def main(args=None) -> Tuple[argparse.Namespace, int]: path = player_path_cache[player] if path: try: - settings: Tuple[argparse.Namespace, ...] = settings_cache[path] if settings_cache[path] else \ + settings: tuple[argparse.Namespace, ...] = settings_cache[path] if settings_cache[path] else \ tuple(roll_settings(yaml, args.plando) for yaml in weights_cache[path]) for settingsObject in settings: for k, v in vars(settingsObject).items(): @@ -224,10 +224,14 @@ def main(args=None) -> Tuple[argparse.Namespace, int]: except Exception as e: raise Exception(f"Error setting {k} to {v} for player {player}") from e - if path == args.weights_file_path: # if name came from the weights file, just use base player name - erargs.name[player] = f"Player{player}" - elif player not in erargs.name: # if name was not specified, generate it from filename - erargs.name[player] = os.path.splitext(os.path.split(path)[-1])[0] + # name was not specified + if player not in erargs.name: + if path == args.weights_file_path: + # weights file, so we need to make the name unique + erargs.name[player] = f"Player{player}" + else: + # use the filename + erargs.name[player] = os.path.splitext(os.path.split(path)[-1])[0] erargs.name[player] = handle_name(erargs.name[player], player, name_counter) player += 1 @@ -242,7 +246,7 @@ def main(args=None) -> Tuple[argparse.Namespace, int]: return erargs, seed -def read_weights_yamls(path) -> Tuple[Any, ...]: +def read_weights_yamls(path) -> tuple[Any, ...]: try: if urllib.parse.urlparse(path).scheme in ('https', 'file'): yaml = str(urllib.request.urlopen(path).read(), "utf-8-sig") @@ -252,7 +256,20 @@ def read_weights_yamls(path) -> Tuple[Any, ...]: except Exception as e: raise Exception(f"Failed to read weights ({path})") from e - return tuple(parse_yamls(yaml)) + from yaml.error import MarkedYAMLError + try: + return tuple(parse_yamls(yaml)) + except MarkedYAMLError as ex: + if ex.problem_mark: + lines = yaml.splitlines() + if ex.context_mark: + relevant_lines = "\n".join(lines[ex.context_mark.line:ex.problem_mark.line+1]) + else: + relevant_lines = lines[ex.problem_mark.line] + error_line = " " * ex.problem_mark.column + "^" + raise Exception(f"{ex.context} {ex.problem} on line {ex.problem_mark.line}:" + f"\n{relevant_lines}\n{error_line}") + raise ex def interpret_on_off(value) -> bool: @@ -321,12 +338,6 @@ def handle_name(name: str, player: int, name_counter: Counter): return new_name -def roll_percentage(percentage: Union[int, float]) -> bool: - """Roll a percentage chance. - percentage is expected to be in range [0, 100]""" - return random.random() < (float(percentage) / 100) - - def update_weights(weights: dict, new_weights: dict, update_type: str, name: str) -> dict: logging.debug(f'Applying {new_weights}') cleaned_weights = {} @@ -371,7 +382,7 @@ def update_weights(weights: dict, new_weights: dict, update_type: str, name: str return weights -def roll_meta_option(option_key, game: str, category_dict: Dict) -> Any: +def roll_meta_option(option_key, game: str, category_dict: dict) -> Any: from worlds import AutoWorldRegister if not game: @@ -392,7 +403,7 @@ def roll_linked_options(weights: dict) -> dict: if "name" not in option_set: raise ValueError("One of your linked options does not have a name.") try: - if roll_percentage(option_set["percentage"]): + if Options.roll_percentage(option_set["percentage"]): logging.debug(f"Linked option {option_set['name']} triggered.") new_options = option_set["options"] for category_name, category_options in new_options.items(): @@ -425,7 +436,7 @@ def roll_triggers(weights: dict, triggers: list, valid_keys: set) -> dict: trigger_result = get_choice("option_result", option_set) result = get_choice(key, currently_targeted_weights) currently_targeted_weights[key] = result - if result == trigger_result and roll_percentage(get_choice("percentage", option_set, 100)): + if result == trigger_result and Options.roll_percentage(get_choice("percentage", option_set, 100)): for category_name, category_options in option_set["options"].items(): currently_targeted_weights = weights if category_name: @@ -456,6 +467,14 @@ def handle_option(ret: argparse.Namespace, game_weights: dict, option_key: str, def roll_settings(weights: dict, plando_options: PlandoOptions = PlandoOptions.bosses): + """ + Roll options from specified weights, usually originating from a .yaml options file. + + Important note: + The same weights dict is shared between all slots using the same yaml (e.g. generic weights file for filler slots). + This means it should never be modified without making a deepcopy first. + """ + from worlds import AutoWorldRegister if "linked_options" in weights: @@ -521,10 +540,6 @@ def roll_settings(weights: dict, plando_options: PlandoOptions = PlandoOptions.b handle_option(ret, game_weights, option_key, option, plando_options) valid_keys.add(option_key) - # TODO remove plando_items after moving it to the options system - valid_keys.add("plando_items") - if PlandoOptions.items in plando_options: - ret.plando_items = copy.deepcopy(game_weights.get("plando_items", [])) if ret.game == "A Link to the Past": # TODO there are still more LTTP options not on the options system valid_keys |= {"sprite_pool", "sprite", "random_sprite_on_event"} diff --git a/Launcher.py b/Launcher.py index 609c109470..88e2070e9c 100644 --- a/Launcher.py +++ b/Launcher.py @@ -1,16 +1,14 @@ """ Archipelago Launcher -* if run with APBP as argument, launch corresponding client. -* if run with executable as argument, run it passing argv[2:] as arguments -* if run without arguments, open launcher GUI +* If run with a patch file as argument, launch corresponding client with the patch file as an argument. +* If run with component name as argument, run it passing argv[2:] as arguments. +* If run without arguments or unknown arguments, open launcher GUI. -Scroll down to components= to add components to the launcher as well as setup.py +Additional components can be added to worlds.LauncherComponents.components. """ -import os import argparse -import itertools import logging import multiprocessing import shlex @@ -18,9 +16,10 @@ import subprocess import sys import urllib.parse import webbrowser +from collections.abc import Callable, Sequence from os.path import isfile from shutil import which -from typing import Callable, Optional, Sequence, Tuple, Union, Any +from typing import Any if __name__ == "__main__": import ModuleUpdate @@ -86,12 +85,16 @@ def browse_files(): def open_folder(folder_path): if is_linux: exe = which('xdg-open') or which('gnome-open') or which('kde-open') - subprocess.Popen([exe, folder_path]) elif is_macos: exe = which("open") - subprocess.Popen([exe, folder_path]) else: webbrowser.open(folder_path) + return + + if exe: + subprocess.Popen([exe, folder_path]) + else: + logging.warning(f"No file browser available to open {folder_path}") def update_settings(): @@ -101,66 +104,51 @@ def update_settings(): components.extend([ # Functions - Component("Open host.yaml", func=open_host_yaml), - Component("Open Patch", func=open_patch), - Component("Generate Template Options", func=generate_yamls), - Component("Archipelago Website", func=lambda: webbrowser.open("https://archipelago.gg/")), - Component("Discord Server", icon="discord", func=lambda: webbrowser.open("https://discord.gg/8Z65BR2")), + Component("Open host.yaml", func=open_host_yaml, + description="Open the host.yaml file to change settings for generation, games, and more."), + Component("Open Patch", func=open_patch, + description="Open a patch file, downloaded from the room page or provided by the host."), + Component("Generate Template Options", func=generate_yamls, + description="Generate template YAMLs for currently installed games."), + Component("Archipelago Website", func=lambda: webbrowser.open("https://archipelago.gg/"), + description="Open archipelago.gg in your browser."), + Component("Discord Server", icon="discord", func=lambda: webbrowser.open("https://discord.gg/8Z65BR2"), + description="Join the Discord server to play public multiworlds, report issues, or just chat!"), Component("Unrated/18+ Discord Server", icon="discord", - func=lambda: webbrowser.open("https://discord.gg/fqvNCCRsu4")), - Component("Browse Files", func=browse_files), + func=lambda: webbrowser.open("https://discord.gg/fqvNCCRsu4"), + description="Find unrated and 18+ games in the After Dark Discord server."), + Component("Browse Files", func=browse_files, + description="Open the Archipelago installation folder in your file browser."), ]) -def handle_uri(path: str, launch_args: Tuple[str, ...]) -> None: +def handle_uri(path: str) -> tuple[list[Component], Component]: url = urllib.parse.urlparse(path) queries = urllib.parse.parse_qs(url.query) - launch_args = (path, *launch_args) - client_component = [] + client_components = [] text_client_component = None - if "game" in queries: - game = queries["game"][0] - else: # TODO around 0.6.0 - this is for pre this change webhost uri's - game = "Archipelago" + game = queries["game"][0] for component in components: if component.supports_uri and component.game_name == game: - client_component.append(component) + client_components.append(component) elif component.display_name == "Text Client": text_client_component = component - - from kvui import MDButton, MDButtonText - from kivymd.uix.dialog import MDDialog, MDDialogHeadlineText, MDDialogContentContainer, MDDialogSupportingText - from kivymd.uix.divider import MDDivider - - if client_component is None: - run_component(text_client_component, *launch_args) - return - else: - popup_text = MDDialogSupportingText(text="Select client to open and connect with.") - component_buttons = [MDDivider()] - for component in [text_client_component, *client_component]: - component_buttons.append(MDButton( - MDButtonText(text=component.display_name), - on_release=lambda *args, comp=component: run_component(comp, *launch_args), - style="text" - )) - component_buttons.append(MDDivider()) - - MDDialog( - # Headline - MDDialogHeadlineText(text="Connect to Multiworld"), - # Text - popup_text, - # Content - MDDialogContentContainer( - *component_buttons, - orientation="vertical" - ), - - ).open() + return client_components, text_client_component -def identify(path: Union[None, str]) -> Tuple[Union[None, str], Union[None, Component]]: +def build_uri_popup(component_list: list[Component], launch_args: tuple[str, ...]) -> None: + from kvui import ButtonsPrompt + component_options = { + component.display_name: component for component in component_list + } + popup = ButtonsPrompt("Connect to Multiworld", + "Select client to open and connect with.", + lambda component_name: run_component(component_options[component_name], *launch_args), + *component_options.keys()) + popup.open() + + +def identify(path: None | str) -> tuple[None | str, None | Component]: if path is None: return None, None for component in components: @@ -171,7 +159,7 @@ def identify(path: Union[None, str]) -> Tuple[Union[None, str], Union[None, Comp return None, None -def get_exe(component: Union[str, Component]) -> Optional[Sequence[str]]: +def get_exe(component: str | Component) -> Sequence[str] | None: if isinstance(component, str): name = component component = None @@ -224,19 +212,19 @@ def create_shortcut(button: Any, component: Component) -> None: button.menu.dismiss() -refresh_components: Optional[Callable[[], None]] = None +refresh_components: Callable[[], None] | None = None -def run_gui(path: str, args: Any) -> None: - from kvui import (ThemedApp, MDFloatLayout, MDGridLayout, MDButton, MDLabel, MDButtonText, ScrollBox, ApAsyncImage) +def run_gui(launch_components: list[Component], args: Any) -> None: + from kvui import (ThemedApp, MDFloatLayout, MDGridLayout, ScrollBox) from kivy.properties import ObjectProperty from kivy.core.window import Window from kivy.metrics import dp - from kivymd.uix.button import MDIconButton + from kivymd.uix.button import MDIconButton, MDButton from kivymd.uix.card import MDCard from kivymd.uix.menu import MDDropdownMenu - from kivymd.uix.relativelayout import MDRelativeLayout from kivymd.uix.snackbar import MDSnackbar, MDSnackbarText + from kivymd.uix.textfield import MDTextField from kivy.lang.builder import Builder @@ -250,22 +238,22 @@ def run_gui(path: str, args: Any) -> None: self.image = image_path super().__init__(args, kwargs) - class Launcher(ThemedApp): base_title: str = "Archipelago Launcher" top_screen: MDFloatLayout = ObjectProperty(None) navigation: MDGridLayout = ObjectProperty(None) grid: MDGridLayout = ObjectProperty(None) button_layout: ScrollBox = ObjectProperty(None) + search_box: MDTextField = ObjectProperty(None) cards: list[LauncherCard] current_filter: Sequence[str | Type] | None - def __init__(self, ctx=None, path=None, args=None): + def __init__(self, ctx=None, components=None, args=None): self.title = self.base_title + " " + Utils.__version__ self.ctx = ctx self.icon = r"data/icon.png" self.favorites = [] - self.launch_uri = path + self.launch_components = components self.launch_args = args self.cards = [] self.current_filter = (Type.CLIENT, Type.TOOL, Type.ADJUSTER, Type.MISC) @@ -337,14 +325,34 @@ def run_gui(path: str, args: Any) -> None: for card in cards: self.button_layout.layout.add_widget(card) - def filter_clients(self, caller): + top = self.button_layout.children[0].y + self.button_layout.children[0].height \ + - self.button_layout.height + scroll_percent = self.button_layout.convert_distance_to_scroll(0, top) + self.button_layout.scroll_y = max(0, min(1, scroll_percent[1])) + + def filter_clients_by_type(self, caller: MDButton): self._refresh_components(caller.type) + self.search_box.text = "" + + def filter_clients_by_name(self, caller: MDTextField, name: str) -> None: + if len(name) == 0: + self._refresh_components(self.current_filter) + return + + sub_matches = [ + card for card in self.cards + if name.lower() in card.component.display_name.lower() and card.component.type != Type.HIDDEN + ] + self.button_layout.layout.clear_widgets() + for card in sub_matches: + self.button_layout.layout.add_widget(card) def build(self): self.top_screen = Builder.load_file(Utils.local_path("data/launcher.kv")) self.grid = self.top_screen.ids.grid self.navigation = self.top_screen.ids.navigation self.button_layout = self.top_screen.ids.button_layout + self.search_box = self.top_screen.ids.search_box self.set_colors() self.top_screen.md_bg_color = self.theme_cls.backgroundColor @@ -352,18 +360,24 @@ def run_gui(path: str, args: Any) -> None: refresh_components = self._refresh_components Window.bind(on_drop_file=self._on_drop_file) + Window.bind(on_keyboard=self._on_keyboard) for component in components: self.cards.append(self.build_card(component)) self._refresh_components(self.current_filter) + # Uncomment to re-enable the Kivy console/live editor + # Ctrl-E to enable it, make sure numlock/capslock is disabled + # from kivy.modules.console import create_console + # create_console(Window, self.top_screen) + return self.top_screen def on_start(self): - if self.launch_uri: - handle_uri(self.launch_uri, self.launch_args) - self.launch_uri = None + if self.launch_components: + build_uri_popup(self.launch_components, self.launch_args) + self.launch_components = None self.launch_args = None @staticmethod @@ -381,7 +395,16 @@ def run_gui(path: str, args: Any) -> None: if file and component: run_component(component, file) else: - logging.warning(f"unable to identify component for {file}") + logging.warning(f"unable to identify component for {filename}") + + def _on_keyboard(self, window: Window, key: int, scancode: int, codepoint: str, modifier: list[str]): + # Activate search as soon as we start typing, no matter if we are focused on the search box or not. + # Focus first, then capture the first character we type, otherwise it gets swallowed and lost. + # Limit text input to ASCII non-control characters (space bar to tilde). + if not self.search_box.focus: + self.search_box.focus = True + if key in range(32, 126): + self.search_box.text += codepoint def _stop(self, *largs): # ran into what appears to be https://groups.google.com/g/kivy-users/c/saWDLoYCSZ4 with PyCharm. @@ -395,7 +418,7 @@ def run_gui(path: str, args: Any) -> None: for filter in self.current_filter)) super().on_stop() - Launcher(path=path, args=args).run() + Launcher(components=launch_components, args=args).run() # avoiding Launcher reference leak # and don't try to do something with widgets after window closed @@ -414,7 +437,7 @@ def run_component(component: Component, *args): logging.warning(f"Component {component} does not appear to be executable.") -def main(args: Optional[Union[argparse.Namespace, dict]] = None): +def main(args: argparse.Namespace | dict | None = None): if isinstance(args, argparse.Namespace): args = {k: v for k, v in args._get_kwargs()} elif not args: @@ -422,7 +445,15 @@ def main(args: Optional[Union[argparse.Namespace, dict]] = None): path = args.get("Patch|Game|Component|url", None) if path is not None: - if not path.startswith("archipelago://"): + if path.startswith("archipelago://"): + args["args"] = (path, *args.get("args", ())) + # add the url arg to the passthrough args + components, text_client_component = handle_uri(path) + if not components: + args["component"] = text_client_component + else: + args['launch_components'] = [text_client_component, *components] + else: file, component = identify(path) if file: args['file'] = file @@ -438,7 +469,7 @@ def main(args: Optional[Union[argparse.Namespace, dict]] = None): elif "component" in args: run_component(args["component"], *args["args"]) elif not args["update_settings"]: - run_gui(path, args.get("args", ())) + run_gui(args.get("launch_components", None), args.get("args", ())) if __name__ == '__main__': diff --git a/LinksAwakeningClient.py b/LinksAwakeningClient.py index bdfaa74625..14aaa415f1 100644 --- a/LinksAwakeningClient.py +++ b/LinksAwakeningClient.py @@ -26,13 +26,14 @@ import typing from CommonClient import (CommonContext, get_base_parser, gui_enabled, logger, server_loop) from NetUtils import ClientStatus +from worlds.ladx import LinksAwakeningWorld from worlds.ladx.Common import BASE_ID as LABaseID from worlds.ladx.GpsTracker import GpsTracker from worlds.ladx.TrackerConsts import storage_key from worlds.ladx.ItemTracker import ItemTracker from worlds.ladx.LADXR.checkMetadata import checkMetadataTable from worlds.ladx.Locations import get_locations_to_id, meta_to_name -from worlds.ladx.Tracker import LocationTracker, MagpieBridge +from worlds.ladx.Tracker import LocationTracker, MagpieBridge, Check class GameboyException(Exception): @@ -51,22 +52,6 @@ class BadRetroArchResponse(GameboyException): pass -def magpie_logo(): - from kivy.uix.image import CoreImage - binary_data = """ -iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXN -SR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA -7DAcdvqGQAAADGSURBVDhPhVLBEcIwDHOYhjHCBuXHj2OTbAL8+ -MEGZIxOQ1CinOOk0Op0bmo7tlXXeR9FJMYDLOD9mwcLjQK7+hSZ -wgcWMZJOAGeGKtChNHFL0j+FZD3jSCuo0w7l03wDrWdg00C4/aW -eDEYNenuzPOfPspBnxf0kssE80vN0L8361j10P03DK4x6FHabuV -ear8fHme+b17rwSjbAXeUMLb+EVTV2QHm46MWQanmnydA98KsVS -XkV+qFpGQXrLhT/fqraQeQLuplpNH5g+WkAAAAASUVORK5CYII=""" - binary_data = base64.b64decode(binary_data) - data = io.BytesIO(binary_data) - return CoreImage(data, ext="png").texture - - class LAClientConstants: # Connector version VERSION = 0x01 @@ -529,7 +514,9 @@ class LinksAwakeningContext(CommonContext): def run_gui(self) -> None: import webbrowser - from kvui import GameManager, ImageButton + from kvui import GameManager + from kivy.metrics import dp + from kivymd.uix.button import MDButton, MDButtonText class LADXManager(GameManager): logging_pairs = [ @@ -542,8 +529,10 @@ class LinksAwakeningContext(CommonContext): b = super().build() if self.ctx.magpie_enabled: - button = ImageButton(texture=magpie_logo(), fit_mode="cover", image_size=(32, 32), size_hint_x=None, - on_press=lambda _: webbrowser.open('https://magpietracker.us/?enable_autotracker=1')) + button = MDButton(MDButtonText(text="Open Tracker"), style="filled", size=(dp(100), dp(70)), radius=5, + size_hint_x=None, size_hint_y=None, pos_hint={"center_y": 0.55}, + on_press=lambda _: webbrowser.open('https://magpietracker.us/?enable_autotracker=1')) + button.height = self.server_connect_bar.height self.connect_layout.add_widget(button) return b @@ -637,6 +626,11 @@ class LinksAwakeningContext(CommonContext): "password": self.password, }) + # We can process linked items on already-checked checks now that we have slot_data + if self.client.tracker: + checked_checks = set(self.client.tracker.all_checks) - set(self.client.tracker.remaining_checks) + self.add_linked_items(checked_checks) + # TODO - use watcher_event if cmd == "ReceivedItems": for index, item in enumerate(args["items"], start=args["index"]): @@ -652,6 +646,13 @@ class LinksAwakeningContext(CommonContext): sync_msg = [{'cmd': 'Sync'}] await self.send_msgs(sync_msg) + def add_linked_items(self, checks: typing.List[Check]): + for check in checks: + if check.value and check.linkedItem: + linkedItem = check.linkedItem + if 'condition' not in linkedItem or (self.slot_data and linkedItem['condition'](self.slot_data)): + self.client.item_tracker.setExtraItem(check.linkedItem['item'], check.linkedItem['qty']) + item_id_lookup = get_locations_to_id() async def run_game_loop(self): @@ -660,11 +661,7 @@ class LinksAwakeningContext(CommonContext): checkMetadataTable[check.id])] for check in ladxr_checks] self.new_checks(checks, [check.id for check in ladxr_checks]) - for check in ladxr_checks: - if check.value and check.linkedItem: - linkedItem = check.linkedItem - if 'condition' not in linkedItem or linkedItem['condition'](self.slot_data): - self.client.item_tracker.setExtraItem(check.linkedItem['item'], check.linkedItem['qty']) + self.add_linked_items(ladxr_checks) async def victory(): await self.send_victory() @@ -741,8 +738,8 @@ class LinksAwakeningContext(CommonContext): await asyncio.sleep(1.0) def run_game(romfile: str) -> None: - auto_start = typing.cast(typing.Union[bool, str], - Utils.get_options()["ladx_options"].get("rom_start", True)) + auto_start = LinksAwakeningWorld.settings.rom_start + if auto_start is True: import webbrowser webbrowser.open(romfile) diff --git a/Main.py b/Main.py index 528db10c64..442c2ff404 100644 --- a/Main.py +++ b/Main.py @@ -7,14 +7,13 @@ import tempfile import time import zipfile import zlib -from typing import Dict, List, Optional, Set, Tuple, Union import worlds -from BaseClasses import CollectionState, Item, Location, LocationProgressType, MultiWorld, Region -from Fill import FillError, balance_multiworld_progression, distribute_items_restrictive, distribute_planned, \ - flood_items +from BaseClasses import CollectionState, Item, Location, LocationProgressType, MultiWorld +from Fill import FillError, balance_multiworld_progression, distribute_items_restrictive, flood_items, \ + parse_planned_blocks, distribute_planned_blocks, resolve_early_locations_for_planned from Options import StartInventoryPool -from Utils import __version__, output_path, version_tuple, get_settings +from Utils import __version__, output_path, version_tuple from settings import get_settings from worlds import AutoWorld from worlds.generic.Rules import exclusion_rules, locality_rules @@ -22,7 +21,7 @@ from worlds.generic.Rules import exclusion_rules, locality_rules __all__ = ["main"] -def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = None): +def main(args, seed=None, baked_server_options: dict[str, object] | None = None): if not baked_server_options: baked_server_options = get_settings().server_options.as_dict() assert isinstance(baked_server_options, dict) @@ -37,9 +36,6 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No logger = logging.getLogger() multiworld.set_seed(seed, args.race, str(args.outputname) if args.outputname else None) multiworld.plando_options = args.plando_options - multiworld.plando_items = args.plando_items.copy() - multiworld.plando_texts = args.plando_texts.copy() - multiworld.plando_connections = args.plando_connections.copy() multiworld.game = args.game.copy() multiworld.player_name = args.name.copy() multiworld.sprite = args.sprite.copy() @@ -56,29 +52,15 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No logger.info(f"Found {len(AutoWorld.AutoWorldRegister.world_types)} World Types:") longest_name = max(len(text) for text in AutoWorld.AutoWorldRegister.world_types) - max_item = 0 - max_location = 0 - for cls in AutoWorld.AutoWorldRegister.world_types.values(): - if cls.item_id_to_name: - max_item = max(max_item, max(cls.item_id_to_name)) - max_location = max(max_location, max(cls.location_id_to_name)) - - item_digits = len(str(max_item)) - location_digits = len(str(max_location)) item_count = len(str(max(len(cls.item_names) for cls in AutoWorld.AutoWorldRegister.world_types.values()))) location_count = len(str(max(len(cls.location_names) for cls in AutoWorld.AutoWorldRegister.world_types.values()))) - del max_item, max_location for name, cls in AutoWorld.AutoWorldRegister.world_types.items(): if not cls.hidden and len(cls.item_names) > 0: - logger.info(f" {name:{longest_name}}: {len(cls.item_names):{item_count}} " - f"Items (IDs: {min(cls.item_id_to_name):{item_digits}} - " - f"{max(cls.item_id_to_name):{item_digits}}) | " - f"{len(cls.location_names):{location_count}} " - f"Locations (IDs: {min(cls.location_id_to_name):{location_digits}} - " - f"{max(cls.location_id_to_name):{location_digits}})") + logger.info(f" {name:{longest_name}}: Items: {len(cls.item_names):{item_count}} | " + f"Locations: {len(cls.location_names):{location_count}}") - del item_digits, location_digits, item_count, location_count + del item_count, location_count # This assertion method should not be necessary to run if we are not outputting any multidata. if not args.skip_output and not args.spoiler_only: @@ -149,13 +131,15 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No multiworld.worlds[1].options.non_local_items.value = set() multiworld.worlds[1].options.local_items.value = set() + multiworld.plando_item_blocks = parse_planned_blocks(multiworld) + AutoWorld.call_all(multiworld, "connect_entrances") AutoWorld.call_all(multiworld, "generate_basic") # remove starting inventory from pool items. # Because some worlds don't actually create items during create_items this has to be as late as possible. fallback_inventory = StartInventoryPool({}) - depletion_pool: Dict[int, Dict[str, int]] = { + depletion_pool: dict[int, dict[str, int]] = { player: getattr(multiworld.worlds[player].options, "start_inventory_from_pool", fallback_inventory).value.copy() for player in multiworld.player_ids } @@ -164,7 +148,7 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No } if target_per_player: - new_itempool: List[Item] = [] + new_itempool: list[Item] = [] # Make new itempool with start_inventory_from_pool items removed for item in multiworld.itempool: @@ -193,8 +177,9 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No multiworld._all_state = None logger.info("Running Item Plando.") - - distribute_planned(multiworld) + resolve_early_locations_for_planned(multiworld) + distribute_planned_blocks(multiworld, [x for player in multiworld.plando_item_blocks + for x in multiworld.plando_item_blocks[player]]) logger.info('Running Pre Main Fill.') @@ -247,7 +232,7 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No pool.submit(AutoWorld.call_single, multiworld, "generate_output", player, temp_dir)) # collect ER hint info - er_hint_data: Dict[int, Dict[int, str]] = {} + er_hint_data: dict[int, dict[int, str]] = {} AutoWorld.call_all(multiworld, 'extend_hint_information', er_hint_data) def write_multidata(): @@ -288,7 +273,7 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No for player in multiworld.groups[location.item.player]["players"]: precollected_hints[player].add(hint) - locations_data: Dict[int, Dict[int, Tuple[int, int, int]]] = {player: {} for player in multiworld.player_ids} + locations_data: dict[int, dict[int, tuple[int, int, int]]] = {player: {} for player in multiworld.player_ids} for location in multiworld.get_filled_locations(): if type(location.address) == int: assert location.item.code is not None, "item code None should be event, " \ @@ -315,13 +300,14 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No game_world.game: worlds.network_data_package["games"][game_world.game] for game_world in multiworld.worlds.values() } + data_package["Archipelago"] = worlds.network_data_package["games"]["Archipelago"] - checks_in_area: Dict[int, Dict[str, Union[int, List[int]]]] = {} + checks_in_area: dict[int, dict[str, int | list[int]]] = {} # get spheres -> filter address==None -> skip empty - spheres: List[Dict[int, Set[int]]] = [] + spheres: list[dict[int, set[int]]] = [] for sphere in multiworld.get_sendable_spheres(): - current_sphere: Dict[int, Set[int]] = collections.defaultdict(set) + current_sphere: dict[int, set[int]] = collections.defaultdict(set) for sphere_location in sphere: current_sphere[sphere_location.player].add(sphere_location.address) diff --git a/MinecraftClient.py b/MinecraftClient.py index 93385ec538..3047dc540e 100644 --- a/MinecraftClient.py +++ b/MinecraftClient.py @@ -14,6 +14,7 @@ import requests import Utils from Utils import is_windows +from settings import get_settings atexit.register(input, "Press enter to exit.") @@ -147,9 +148,11 @@ def find_jdk(version: str) -> str: if os.path.isfile(jdk_exe): return jdk_exe else: - jdk_exe = shutil.which(options["minecraft_options"].get("java", "java")) + jdk_exe = shutil.which(options.java) if not jdk_exe: - raise Exception("Could not find Java. Is Java installed on the system?") + jdk_exe = shutil.which("java") # try to fall back to system java + if not jdk_exe: + raise Exception("Could not find Java. Is Java installed on the system?") return jdk_exe @@ -285,8 +288,8 @@ if __name__ == '__main__': # Change to executable's working directory os.chdir(os.path.abspath(os.path.dirname(sys.argv[0]))) - options = Utils.get_options() - channel = args.channel or options["minecraft_options"]["release_channel"] + options = get_settings().minecraft_options + channel = args.channel or options.release_channel apmc_data = None data_version = args.data_version or None @@ -299,8 +302,8 @@ if __name__ == '__main__': versions = get_minecraft_versions(data_version, channel) - forge_dir = options["minecraft_options"]["forge_directory"] - max_heap = options["minecraft_options"]["max_heap_size"] + forge_dir = options.forge_directory + max_heap = options.max_heap_size forge_version = args.forge or versions["forge"] java_version = args.java or versions["java"] mod_url = versions["url"] diff --git a/MultiServer.py b/MultiServer.py index 05e93e678d..f12f327c3f 100644 --- a/MultiServer.py +++ b/MultiServer.py @@ -46,7 +46,8 @@ from NetUtils import Endpoint, ClientStatus, NetworkItem, decode, encode, Networ SlotType, LocationStore, Hint, HintStatus from BaseClasses import ItemClassification -min_client_version = Version(0, 1, 6) + +min_client_version = Version(0, 5, 0) colorama.just_fix_windows_console() @@ -457,8 +458,12 @@ class Context: self.generator_version = Version(*decoded_obj["version"]) clients_ver = decoded_obj["minimum_versions"].get("clients", {}) self.minimum_client_versions = {} + if self.generator_version < Version(0, 6, 2): + min_version = Version(0, 1, 6) + else: + min_version = min_client_version for player, version in clients_ver.items(): - self.minimum_client_versions[player] = max(Version(*version), min_client_version) + self.minimum_client_versions[player] = max(Version(*version), min_version) self.slot_info = decoded_obj["slot_info"] self.games = {slot: slot_info.game for slot, slot_info in self.slot_info.items()} @@ -1825,7 +1830,7 @@ async def process_client_cmd(ctx: Context, client: Client, args: dict): ctx.clients[team][slot].append(client) client.version = args['version'] client.tags = args['tags'] - client.no_locations = "TextOnly" in client.tags or "Tracker" in client.tags + client.no_locations = bool(client.tags & _non_game_messages.keys()) # set NoText for old PopTracker clients that predate the tag to save traffic client.no_text = "NoText" in client.tags or ("PopTracker" in client.tags and client.version < (0, 5, 1)) connected_packet = { @@ -1899,7 +1904,7 @@ async def process_client_cmd(ctx: Context, client: Client, args: dict): old_tags = client.tags client.tags = args["tags"] if set(old_tags) != set(client.tags): - client.no_locations = 'TextOnly' in client.tags or 'Tracker' in client.tags + client.no_locations = bool(client.tags & _non_game_messages.keys()) client.no_text = "NoText" in client.tags or ( "PopTracker" in client.tags and client.version < (0, 5, 1) ) @@ -1982,14 +1987,21 @@ async def process_client_cmd(ctx: Context, client: Client, args: dict): new_hint = new_hint.re_prioritize(ctx, status) if hint == new_hint: return - ctx.replace_hint(client.team, hint.finding_player, hint, new_hint) - ctx.replace_hint(client.team, hint.receiving_player, hint, new_hint) + + concerning_slots = ctx.slot_set(hint.receiving_player) | {hint.finding_player} + for slot in concerning_slots: + ctx.replace_hint(client.team, slot, hint, new_hint) ctx.save() - ctx.on_changed_hints(client.team, hint.finding_player) - ctx.on_changed_hints(client.team, hint.receiving_player) - + for slot in concerning_slots: + ctx.on_changed_hints(client.team, slot) + elif cmd == 'StatusUpdate': - update_client_status(ctx, client, args["status"]) + if client.no_locations and args["status"] == ClientStatus.CLIENT_GOAL: + await ctx.send_msgs(client, [{'cmd': 'InvalidPacket', "type": "cmd", + "text": "Trackers can't register Goal Complete", + "original_cmd": cmd}]) + else: + update_client_status(ctx, client, args["status"]) elif cmd == 'Say': if "text" not in args or type(args["text"]) is not str or not args["text"].isprintable(): @@ -2416,8 +2428,10 @@ async def console(ctx: Context): def parse_args() -> argparse.Namespace: + from settings import get_settings + parser = argparse.ArgumentParser() - defaults = Utils.get_settings()["server_options"].as_dict() + defaults = get_settings().server_options.as_dict() parser.add_argument('multidata', nargs="?", default=defaults["multidata"]) parser.add_argument('--host', default=defaults["host"]) parser.add_argument('--port', default=defaults["port"], type=int) diff --git a/OoTClient.py b/OoTClient.py index 6a87b9e722..571300ed36 100644 --- a/OoTClient.py +++ b/OoTClient.py @@ -12,6 +12,7 @@ from CommonClient import CommonContext, server_loop, gui_enabled, \ import Utils from Utils import async_start from worlds import network_data_package +from worlds.oot import OOTWorld from worlds.oot.Rom import Rom, compress_rom_file from worlds.oot.N64Patch import apply_patch_file from worlds.oot.Utils import data_path @@ -280,7 +281,7 @@ async def n64_sync_task(ctx: OoTContext): async def run_game(romfile): - auto_start = Utils.get_options()["oot_options"].get("rom_start", True) + auto_start = OOTWorld.settings.rom_start if auto_start is True: import webbrowser webbrowser.open(romfile) @@ -295,7 +296,7 @@ async def patch_and_run_game(apz5_file): decomp_path = base_name + '-decomp.z64' comp_path = base_name + '.z64' # Load vanilla ROM, patch file, compress ROM - rom_file_name = Utils.get_options()["oot_options"]["rom_file"] + rom_file_name = OOTWorld.settings.rom_file rom = Rom(rom_file_name) sub_file = None diff --git a/Options.py b/Options.py index 95b9b468c6..3d08c5f003 100644 --- a/Options.py +++ b/Options.py @@ -1,6 +1,7 @@ from __future__ import annotations import abc +import collections import functools import logging import math @@ -23,6 +24,12 @@ if typing.TYPE_CHECKING: import pathlib +def roll_percentage(percentage: int | float) -> bool: + """Roll a percentage chance. + percentage is expected to be in range [0, 100]""" + return random.random() < (float(percentage) / 100) + + class OptionError(ValueError): pass @@ -866,15 +873,49 @@ class OptionDict(Option[typing.Dict[str, typing.Any]], VerifyKeys, typing.Mappin def __len__(self) -> int: return self.value.__len__() + # __getitem__ fallback fails for Counters, so we define this explicitly + def __contains__(self, item) -> bool: + return item in self.value -class ItemDict(OptionDict): + +class OptionCounter(OptionDict): + min: int | None = None + max: int | None = None + + def __init__(self, value: dict[str, int]) -> None: + super(OptionCounter, self).__init__(collections.Counter(value)) + + def verify(self, world: type[World], player_name: str, plando_options: PlandoOptions) -> None: + super(OptionCounter, self).verify(world, player_name, plando_options) + + range_errors = [] + + if self.max is not None: + range_errors += [ + f"\"{key}: {value}\" is higher than maximum allowed value {self.max}." + for key, value in self.value.items() if value > self.max + ] + + if self.min is not None: + range_errors += [ + f"\"{key}: {value}\" is lower than minimum allowed value {self.min}." + for key, value in self.value.items() if value < self.min + ] + + if range_errors: + range_errors = [f"For option {getattr(self, 'display_name', self)}:"] + range_errors + raise OptionError("\n".join(range_errors)) + + +class ItemDict(OptionCounter): verify_item_name = True - def __init__(self, value: typing.Dict[str, int]): - if any(item_count is None for item_count in value.values()): - raise Exception("Items must have counts associated with them. Please provide positive integer values in the format \"item\": count .") - if any(item_count < 1 for item_count in value.values()): - raise Exception("Cannot have non-positive item counts.") + min = 0 + + def __init__(self, value: dict[str, int]) -> None: + # Backwards compatibility: Cull 0s to make "in" checks behave the same as when this wasn't a OptionCounter + value = {item_name: amount for item_name, amount in value.items() if amount != 0} + super(ItemDict, self).__init__(value) @@ -984,7 +1025,7 @@ class PlandoTexts(Option[typing.List[PlandoText]], VerifyKeys): if isinstance(data, typing.Iterable): for text in data: if isinstance(text, typing.Mapping): - if random.random() < float(text.get("percentage", 100)/100): + if roll_percentage(text.get("percentage", 100)): at = text.get("at", None) if at is not None: if isinstance(at, dict): @@ -1010,7 +1051,7 @@ class PlandoTexts(Option[typing.List[PlandoText]], VerifyKeys): else: raise OptionError("\"at\" must be a valid string or weighted list of strings!") elif isinstance(text, PlandoText): - if random.random() < float(text.percentage/100): + if roll_percentage(text.percentage): texts.append(text) else: raise Exception(f"Cannot create plando text from non-dictionary type, got {type(text)}") @@ -1134,7 +1175,7 @@ class PlandoConnections(Option[typing.List[PlandoConnection]], metaclass=Connect for connection in data: if isinstance(connection, typing.Mapping): percentage = connection.get("percentage", 100) - if random.random() < float(percentage / 100): + if roll_percentage(percentage): entrance = connection.get("entrance", None) if is_iterable_except_str(entrance): entrance = random.choice(sorted(entrance)) @@ -1152,7 +1193,7 @@ class PlandoConnections(Option[typing.List[PlandoConnection]], metaclass=Connect percentage )) elif isinstance(connection, PlandoConnection): - if random.random() < float(connection.percentage / 100): + if roll_percentage(connection.percentage): value.append(connection) else: raise Exception(f"Cannot create connection from non-Dict type, got {type(connection)}.") @@ -1257,42 +1298,47 @@ class CommonOptions(metaclass=OptionsMetaProperty): progression_balancing: ProgressionBalancing accessibility: Accessibility - def as_dict(self, - *option_names: str, - casing: typing.Literal["snake", "camel", "pascal", "kebab"] = "snake", - toggles_as_bools: bool = False) -> typing.Dict[str, typing.Any]: + def as_dict( + self, + *option_names: str, + casing: typing.Literal["snake", "camel", "pascal", "kebab"] = "snake", + toggles_as_bools: bool = False, + ) -> dict[str, typing.Any]: """ Returns a dictionary of [str, Option.value] - :param option_names: names of the options to return - :param casing: case of the keys to return. Supports `snake`, `camel`, `pascal`, `kebab` - :param toggles_as_bools: whether toggle options should be output as bools instead of strings + :param option_names: Names of the options to get the values of. + :param casing: Casing of the keys to return. Supports `snake`, `camel`, `pascal`, `kebab`. + :param toggles_as_bools: Whether toggle options should be returned as bools instead of ints. + + :return: A dictionary of each option name to the value of its Option. If the option is an OptionSet, the value + will be returned as a sorted list. """ assert option_names, "options.as_dict() was used without any option names." option_results = {} for option_name in option_names: - if option_name in type(self).type_hints: - if casing == "snake": - display_name = option_name - elif casing == "camel": - split_name = [name.title() for name in option_name.split("_")] - split_name[0] = split_name[0].lower() - display_name = "".join(split_name) - elif casing == "pascal": - display_name = "".join([name.title() for name in option_name.split("_")]) - elif casing == "kebab": - display_name = option_name.replace("_", "-") - else: - raise ValueError(f"{casing} is invalid casing for as_dict. " - "Valid names are 'snake', 'camel', 'pascal', 'kebab'.") - value = getattr(self, option_name).value - if isinstance(value, set): - value = sorted(value) - elif toggles_as_bools and issubclass(type(self).type_hints[option_name], Toggle): - value = bool(value) - option_results[display_name] = value - else: + if option_name not in type(self).type_hints: raise ValueError(f"{option_name} not found in {tuple(type(self).type_hints)}") + + if casing == "snake": + display_name = option_name + elif casing == "camel": + split_name = [name.title() for name in option_name.split("_")] + split_name[0] = split_name[0].lower() + display_name = "".join(split_name) + elif casing == "pascal": + display_name = "".join([name.title() for name in option_name.split("_")]) + elif casing == "kebab": + display_name = option_name.replace("_", "-") + else: + raise ValueError(f"{casing} is invalid casing for as_dict. " + "Valid names are 'snake', 'camel', 'pascal', 'kebab'.") + value = getattr(self, option_name).value + if isinstance(value, set): + value = sorted(value) + elif toggles_as_bools and issubclass(type(self).type_hints[option_name], Toggle): + value = bool(value) + option_results[display_name] = value return option_results @@ -1313,6 +1359,7 @@ class StartInventory(ItemDict): verify_item_name = True display_name = "Start Inventory" rich_text_doc = True + max = 10000 class StartInventoryPool(StartInventory): @@ -1428,6 +1475,131 @@ class ItemLinks(OptionList): link["item_pool"] = list(pool) +@dataclass(frozen=True) +class PlandoItem: + items: list[str] | dict[str, typing.Any] + locations: list[str] + world: int | str | bool | None | typing.Iterable[str] | set[int] = False + from_pool: bool = True + force: bool | typing.Literal["silent"] = "silent" + count: int | bool | dict[str, int] = False + percentage: int = 100 + + +class PlandoItems(Option[typing.List[PlandoItem]]): + """Generic items plando.""" + default = () + supports_weighting = False + display_name = "Plando Items" + + def __init__(self, value: typing.Iterable[PlandoItem]) -> None: + self.value = list(deepcopy(value)) + super().__init__() + + @classmethod + def from_any(cls, data: typing.Any) -> Option[typing.List[PlandoItem]]: + if not isinstance(data, typing.Iterable): + raise OptionError(f"Cannot create plando items from non-Iterable type, got {type(data)}") + + value: typing.List[PlandoItem] = [] + for item in data: + if isinstance(item, typing.Mapping): + percentage = item.get("percentage", 100) + if not isinstance(percentage, int): + raise OptionError(f"Plando `percentage` has to be int, not {type(percentage)}.") + if not (0 <= percentage <= 100): + raise OptionError(f"Plando `percentage` has to be between 0 and 100 (inclusive) not {percentage}.") + if roll_percentage(percentage): + count = item.get("count", False) + items = item.get("items", []) + if not items: + items = item.get("item", None) # explicitly throw an error here if not present + if not items: + raise OptionError("You must specify at least one item to place items with plando.") + count = 1 + if isinstance(items, str): + items = [items] + elif not isinstance(items, (dict, list)): + raise OptionError(f"Plando 'items' has to be string, list, or " + f"dictionary, not {type(items)}") + locations = item.get("locations", []) + if not locations: + locations = item.get("location", ["Everywhere"]) + if locations: + count = 1 + if isinstance(locations, str): + locations = [locations] + if not isinstance(locations, list): + raise OptionError(f"Plando `location` has to be string or list, not {type(locations)}") + world = item.get("world", False) + from_pool = item.get("from_pool", True) + force = item.get("force", "silent") + if not isinstance(from_pool, bool): + raise OptionError(f"Plando 'from_pool' has to be true or false, not {from_pool!r}.") + if not (isinstance(force, bool) or force == "silent"): + raise OptionError(f"Plando `force` has to be true or false or `silent`, not {force!r}.") + value.append(PlandoItem(items, locations, world, from_pool, force, count, percentage)) + elif isinstance(item, PlandoItem): + if roll_percentage(item.percentage): + value.append(item) + else: + raise OptionError(f"Cannot create plando item from non-Dict type, got {type(item)}.") + return cls(value) + + def verify(self, world: typing.Type[World], player_name: str, plando_options: "PlandoOptions") -> None: + if not self.value: + return + from BaseClasses import PlandoOptions + if not (PlandoOptions.items & plando_options): + # plando is disabled but plando options were given so overwrite the options + self.value = [] + logging.warning(f"The plando items module is turned off, " + f"so items for {player_name} will be ignored.") + else: + # filter down item groups + for plando in self.value: + # confirm a valid count + if isinstance(plando.count, dict): + if "min" in plando.count and "max" in plando.count: + if plando.count["min"] > plando.count["max"]: + raise OptionError("Plando cannot have count `min` greater than `max`.") + items_copy = plando.items.copy() + if isinstance(plando.items, dict): + for item in items_copy: + if item in world.item_name_groups: + value = plando.items.pop(item) + group = world.item_name_groups[item] + filtered_items = sorted(group.difference(list(plando.items.keys()))) + if not filtered_items: + raise OptionError(f"Plando `items` contains the group \"{item}\" " + f"and every item in it. This is not allowed.") + if value is True: + for key in filtered_items: + plando.items[key] = True + else: + for key in random.choices(filtered_items, k=value): + plando.items[key] = plando.items.get(key, 0) + 1 + else: + assert isinstance(plando.items, list) # pycharm can't figure out the hinting without the hint + for item in items_copy: + if item in world.item_name_groups: + plando.items.remove(item) + plando.items.extend(sorted(world.item_name_groups[item])) + + @classmethod + def get_option_name(cls, value: list[PlandoItem]) -> str: + return ", ".join(["(%s: %s)" % (item.items, item.locations) for item in value]) #TODO: see what a better way to display would be + + def __getitem__(self, index: typing.SupportsIndex) -> PlandoItem: + return self.value.__getitem__(index) + + def __iter__(self) -> typing.Iterator[PlandoItem]: + yield from self.value + + def __len__(self) -> int: + return len(self.value) + + class Removed(FreeText): """This Option has been Removed.""" rich_text_doc = True @@ -1450,6 +1622,7 @@ class PerGameCommonOptions(CommonOptions): exclude_locations: ExcludeLocations priority_locations: PriorityLocations item_links: ItemLinks + plando_items: PlandoItems @dataclass @@ -1503,6 +1676,7 @@ def get_option_groups(world: typing.Type[World], visibility_level: Visibility = def generate_yaml_templates(target_folder: typing.Union[str, "pathlib.Path"], generate_hidden: bool = True) -> None: import os + from inspect import cleandoc import yaml from jinja2 import Template @@ -1541,19 +1715,21 @@ def generate_yaml_templates(target_folder: typing.Union[str, "pathlib.Path"], ge # yaml dump may add end of document marker and newlines. return yaml.dump(scalar).replace("...\n", "").strip() + with open(local_path("data", "options.yaml")) as f: + file_data = f.read() + template = Template(file_data) + for game_name, world in AutoWorldRegister.world_types.items(): if not world.hidden or generate_hidden: option_groups = get_option_groups(world) - with open(local_path("data", "options.yaml")) as f: - file_data = f.read() - res = Template(file_data).render( + + res = template.render( option_groups=option_groups, __version__=__version__, game=game_name, yaml_dump=yaml_dump_scalar, dictify_range=dictify_range, + cleandoc=cleandoc, ) - del file_data - with open(os.path.join(target_folder, get_file_safe_name(game_name) + ".yaml"), "w", encoding="utf-8-sig") as f: f.write(res) diff --git a/README.md b/README.md index 5e14ef5de3..9ce6caf0cf 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ Currently, the following games are supported: * Factorio * Minecraft * Subnautica -* Slay the Spire * Risk of Rain 2 * The Legend of Zelda: Ocarina of Time * Timespinner @@ -63,7 +62,6 @@ Currently, the following games are supported: * TUNIC * Kirby's Dream Land 3 * Celeste 64 -* Zork Grand Inquisitor * Castlevania 64 * A Short Hike * Yoshi's Island @@ -82,6 +80,9 @@ Currently, the following games are supported: * Inscryption * Civilization VI * The Legend of Zelda: The Wind Waker +* Jak and Daxter: The Precursor Legacy +* Super Mario Land 2: 6 Golden Coins +* shapez For setup and instructions check out our [tutorials page](https://archipelago.gg/tutorial/). Downloads can be found at [Releases](https://github.com/ArchipelagoMW/Archipelago/releases), including compiled diff --git a/Utils.py b/Utils.py index 202b8da178..b38809ba1b 100644 --- a/Utils.py +++ b/Utils.py @@ -114,6 +114,8 @@ def cache_self1(function: typing.Callable[[S, T], RetType]) -> typing.Callable[[ cache[arg] = res return res + wrap.__defaults__ = function.__defaults__ + return wrap @@ -137,8 +139,11 @@ def local_path(*path: str) -> str: local_path.cached_path = os.path.dirname(os.path.abspath(sys.argv[0])) else: import __main__ - if hasattr(__main__, "__file__") and os.path.isfile(__main__.__file__): + if globals().get("__file__") and os.path.isfile(__file__): # we are running in a normal Python environment + local_path.cached_path = os.path.dirname(os.path.abspath(__file__)) + elif hasattr(__main__, "__file__") and os.path.isfile(__main__.__file__): + # we are running in a normal Python environment, but AP was imported weirdly local_path.cached_path = os.path.dirname(os.path.abspath(__main__.__file__)) else: # pray @@ -427,6 +432,9 @@ class RestrictedUnpickler(pickle.Unpickler): def find_class(self, module: str, name: str) -> type: if module == "builtins" and name in safe_builtins: return getattr(builtins, name) + # used by OptionCounter + if module == "collections" and name == "Counter": + return collections.Counter # used by MultiServer -> savegame/multidata if module == "NetUtils" and name in {"NetworkItem", "ClientStatus", "Hint", "SlotType", "NetworkSlot", "HintStatus"}: @@ -532,6 +540,8 @@ def init_logging(name: str, loglevel: typing.Union[str, int] = logging.INFO, if add_timestamp: stream_handler.setFormatter(formatter) root_logger.addHandler(stream_handler) + if hasattr(sys.stdout, "reconfigure"): + sys.stdout.reconfigure(encoding="utf-8", errors="replace") # Relay unhandled exceptions to logger. if not getattr(sys.excepthook, "_wrapped", False): # skip if already modified @@ -630,6 +640,8 @@ def get_fuzzy_results(input_word: str, word_list: typing.Collection[str], limit: import jellyfish def get_fuzzy_ratio(word1: str, word2: str) -> float: + if word1 == word2: + return 1.01 return (1 - jellyfish.damerau_levenshtein_distance(word1.lower(), word2.lower()) / max(len(word1), len(word2))) @@ -650,8 +662,10 @@ def get_intended_text(input_text: str, possible_answers) -> typing.Tuple[str, bo picks = get_fuzzy_results(input_text, possible_answers, limit=2) if len(picks) > 1: dif = picks[0][1] - picks[1][1] - if picks[0][1] == 100: + if picks[0][1] == 101: return picks[0][0], True, "Perfect Match" + elif picks[0][1] == 100: + return picks[0][0], True, "Case Insensitive Perfect Match" elif picks[0][1] < 75: return picks[0][0], False, f"Didn't find something that closely matches '{input_text}', " \ f"did you mean '{picks[0][0]}'? ({picks[0][1]}% sure)" diff --git a/WebHostLib/__init__.py b/WebHostLib/__init__.py index 9c713419c9..934cc2498d 100644 --- a/WebHostLib/__init__.py +++ b/WebHostLib/__init__.py @@ -80,10 +80,8 @@ def register(): """Import submodules, triggering their registering on flask routing. Note: initializes worlds subsystem.""" # has automatic patch integration - import worlds.AutoWorld import worlds.Files - app.jinja_env.filters['supports_apdeltapatch'] = lambda game_name: \ - game_name in worlds.Files.AutoPatchRegister.patch_types + app.jinja_env.filters['is_applayercontainer'] = worlds.Files.is_ap_player_container from WebHostLib.customserver import run_server_process # to trigger app routing picking up on it diff --git a/WebHostLib/api/user.py b/WebHostLib/api/user.py index 0ddb6fe83e..2524cc40a6 100644 --- a/WebHostLib/api/user.py +++ b/WebHostLib/api/user.py @@ -28,6 +28,6 @@ def get_seeds(): response.append({ "seed_id": seed.id, "creation_time": seed.creation_time, - "players": get_players(seed.slots), + "players": get_players(seed), }) return jsonify(response) diff --git a/WebHostLib/autolauncher.py b/WebHostLib/autolauncher.py index 8ba093e014..b330146277 100644 --- a/WebHostLib/autolauncher.py +++ b/WebHostLib/autolauncher.py @@ -9,7 +9,7 @@ from threading import Event, Thread from typing import Any from uuid import UUID -from pony.orm import db_session, select, commit +from pony.orm import db_session, select, commit, PrimaryKey from Utils import restricted_loads from .locker import Locker, AlreadyRunningException @@ -36,12 +36,21 @@ def handle_generation_failure(result: BaseException): logging.exception(e) +def _mp_gen_game(gen_options: dict, meta: dict[str, Any] | None = None, owner=None, sid=None) -> PrimaryKey | None: + from setproctitle import setproctitle + + setproctitle(f"Generator ({sid})") + res = gen_game(gen_options, meta=meta, owner=owner, sid=sid) + setproctitle(f"Generator (idle)") + return res + + def launch_generator(pool: multiprocessing.pool.Pool, generation: Generation): try: meta = json.loads(generation.meta) options = restricted_loads(generation.options) logging.info(f"Generating {generation.id} for {len(options)} players") - pool.apply_async(gen_game, (options,), + pool.apply_async(_mp_gen_game, (options,), {"meta": meta, "sid": generation.id, "owner": generation.owner}, @@ -55,6 +64,10 @@ def launch_generator(pool: multiprocessing.pool.Pool, generation: Generation): def init_generator(config: dict[str, Any]) -> None: + from setproctitle import setproctitle + + setproctitle("Generator (idle)") + try: import resource except ModuleNotFoundError: diff --git a/WebHostLib/customserver.py b/WebHostLib/customserver.py index 301a386834..2ebb40d673 100644 --- a/WebHostLib/customserver.py +++ b/WebHostLib/customserver.py @@ -227,6 +227,9 @@ def set_up_logging(room_id) -> logging.Logger: def run_server_process(name: str, ponyconfig: dict, static_server_data: dict, cert_file: typing.Optional[str], cert_key_file: typing.Optional[str], host: str, rooms_to_run: multiprocessing.Queue, rooms_shutting_down: multiprocessing.Queue): + from setproctitle import setproctitle + + setproctitle(name) Utils.init_logging(name) try: import resource diff --git a/WebHostLib/options.py b/WebHostLib/options.py index 711762ee5f..38489cee3c 100644 --- a/WebHostLib/options.py +++ b/WebHostLib/options.py @@ -108,7 +108,7 @@ def option_presets(game: str) -> Response: f"Expected {option.special_range_names.keys()} or {option.range_start}-{option.range_end}." presets[preset_name][preset_option_name] = option.value - elif isinstance(option, (Options.Range, Options.OptionSet, Options.OptionList, Options.ItemDict)): + elif isinstance(option, (Options.Range, Options.OptionSet, Options.OptionList, Options.OptionCounter)): presets[preset_name][preset_option_name] = option.value elif isinstance(preset_option, str): # Ensure the option value is valid for Choice and Toggle options @@ -222,7 +222,7 @@ def generate_yaml(game: str): for key, val in options.copy().items(): key_parts = key.rsplit("||", 2) - # Detect and build ItemDict options from their name pattern + # Detect and build OptionCounter options from their name pattern if key_parts[-1] == "qty": if key_parts[0] not in options: options[key_parts[0]] = {} diff --git a/WebHostLib/requirements.txt b/WebHostLib/requirements.txt index 190409d9a2..4e6bf25df0 100644 --- a/WebHostLib/requirements.txt +++ b/WebHostLib/requirements.txt @@ -1,4 +1,4 @@ -flask>=3.1.0 +flask>=3.1.1 werkzeug>=3.1.3 pony>=0.7.19 waitress>=3.0.2 @@ -9,3 +9,4 @@ bokeh>=3.6.3 markupsafe>=3.0.2 Markdown>=3.7 mdx-breakless-lists>=1.0.1 +setproctitle>=1.3.5 diff --git a/WebHostLib/static/assets/gameInfo.js b/WebHostLib/static/assets/gameInfo.js index 1d6d136135..797c9f6448 100644 --- a/WebHostLib/static/assets/gameInfo.js +++ b/WebHostLib/static/assets/gameInfo.js @@ -23,7 +23,6 @@ window.addEventListener('load', () => { showdown.setOption('strikethrough', true); showdown.setOption('literalMidWordUnderscores', true); gameInfo.innerHTML += (new showdown.Converter()).makeHtml(results); - adjustHeaderWidth(); // Reset the id of all header divs to something nicer for (const header of document.querySelectorAll('h1, h2, h3, h4, h5, h6')) { diff --git a/WebHostLib/static/assets/hostGame.js b/WebHostLib/static/assets/hostGame.js index db1ab1ddde..01a8da06e5 100644 --- a/WebHostLib/static/assets/hostGame.js +++ b/WebHostLib/static/assets/hostGame.js @@ -6,6 +6,4 @@ window.addEventListener('load', () => { document.getElementById('file-input').addEventListener('change', () => { document.getElementById('host-game-form').submit(); }); - - adjustFooterHeight(); }); diff --git a/WebHostLib/static/assets/styleController.js b/WebHostLib/static/assets/styleController.js deleted file mode 100644 index 924e86ee26..0000000000 --- a/WebHostLib/static/assets/styleController.js +++ /dev/null @@ -1,47 +0,0 @@ -const adjustFooterHeight = () => { - // If there is no footer on this page, do nothing - const footer = document.getElementById('island-footer'); - if (!footer) { return; } - - // If the body is taller than the window, also do nothing - if (document.body.offsetHeight > window.innerHeight) { - footer.style.marginTop = '0'; - return; - } - - // Add a margin-top to the footer to position it at the bottom of the screen - const sibling = footer.previousElementSibling; - const margin = (window.innerHeight - sibling.offsetTop - sibling.offsetHeight - footer.offsetHeight); - if (margin < 1) { - footer.style.marginTop = '0'; - return; - } - footer.style.marginTop = `${margin}px`; -}; - -const adjustHeaderWidth = () => { - // If there is no header, do nothing - const header = document.getElementById('base-header'); - if (!header) { return; } - - const tempDiv = document.createElement('div'); - tempDiv.style.width = '100px'; - tempDiv.style.height = '100px'; - tempDiv.style.overflow = 'scroll'; - tempDiv.style.position = 'absolute'; - tempDiv.style.top = '-500px'; - document.body.appendChild(tempDiv); - const scrollbarWidth = tempDiv.offsetWidth - tempDiv.clientWidth; - document.body.removeChild(tempDiv); - - const documentRoot = document.compatMode === 'BackCompat' ? document.body : document.documentElement; - const margin = (documentRoot.scrollHeight > documentRoot.clientHeight) ? 0-scrollbarWidth : 0; - document.getElementById('base-header-right').style.marginRight = `${margin}px`; -}; - -window.addEventListener('load', () => { - window.addEventListener('resize', adjustFooterHeight); - window.addEventListener('resize', adjustHeaderWidth); - adjustFooterHeight(); - adjustHeaderWidth(); -}); diff --git a/WebHostLib/static/assets/tutorial.js b/WebHostLib/static/assets/tutorial.js index d527966005..c9022719fb 100644 --- a/WebHostLib/static/assets/tutorial.js +++ b/WebHostLib/static/assets/tutorial.js @@ -25,7 +25,6 @@ window.addEventListener('load', () => { showdown.setOption('literalMidWordUnderscores', true); showdown.setOption('disableForced4SpacesIndentedSublists', true); tutorialWrapper.innerHTML += (new showdown.Converter()).makeHtml(results); - adjustHeaderWidth(); const title = document.querySelector('h1') if (title) { diff --git a/WebHostLib/static/styles/globalStyles.css b/WebHostLib/static/styles/globalStyles.css index 1a0144830e..adcee6581b 100644 --- a/WebHostLib/static/styles/globalStyles.css +++ b/WebHostLib/static/styles/globalStyles.css @@ -36,6 +36,13 @@ html{ body{ margin: 0; + display: flex; + flex-direction: column; + min-height: calc(100vh - 110px); +} + +main { + flex-grow: 1; } a{ diff --git a/WebHostLib/templates/404.html b/WebHostLib/templates/404.html index 9d567510ee..6c91fed4ac 100644 --- a/WebHostLib/templates/404.html +++ b/WebHostLib/templates/404.html @@ -1,5 +1,6 @@ {% extends 'pageWrapper.html' %} {% import "macros.html" as macros %} +{% set show_footer = True %} {% block head %}