Skip to content

Commit

Permalink
Merge pull request #73 from randovania/feature/apis-from-opr
Browse files Browse the repository at this point in the history
Add a few APIs from OPR
  • Loading branch information
henriquegemignani authored Jul 30, 2023
2 parents 2c98129 + 9c9e087 commit ff72426
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/retro_data_structures/asset_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,11 @@ def register_custom_asset_name(self, name: str, asset_id: AssetId):
def get_custom_asset(self, name: str) -> AssetId | None:
return self._custom_asset_ids.get(name)

def add_new_asset(self, name: str | uuid.UUID, new_data: Resource,
in_paks: typing.Iterable[str]):
def add_new_asset(self, name: str, new_data: Resource,
in_paks: typing.Iterable[str] = ()) -> AssetId:
"""
Adds an asset that doesn't already exist.
:return: Asset id of the new asset.
"""
asset_id = self._resolve_asset_id(name)

Expand All @@ -309,6 +310,15 @@ def add_new_asset(self, name: str | uuid.UUID, new_data: Resource,
for pak_name in in_paks:
self.ensure_present(pak_name, asset_id)

return asset_id

def duplicate_asset(self, asset_id: AssetId, new_name: str) -> AssetId:
"""
Creates a new asset named `new_name` with the contents of `asset_id`
:return: Asset id of the new asset.
"""
return self.add_new_asset(new_name, self.get_parsed_asset(asset_id), ())

def replace_asset(self, asset_id: NameOrAssetId, new_data: Resource):
"""
Replaces an existing asset.
Expand All @@ -335,6 +345,13 @@ def replace_asset(self, asset_id: NameOrAssetId, new_data: Resource):

return asset_id

def add_or_replace_custom_asset(self, name: str, new_data: Resource) -> AssetId:
"""Adds a new asset named `name`, or replaces an existing one if it already exists."""
if self.does_asset_exists(name):
return self.replace_asset(name, new_data)
else:
return self.add_new_asset(name, new_data)

def delete_asset(self, asset_id: NameOrAssetId):
original_name = asset_id
asset_id = self._resolve_asset_id(asset_id)
Expand Down Expand Up @@ -397,7 +414,7 @@ def _get_dependencies_for_asset(self, asset_id: NameOrAssetId, must_exist: bool,
deps: tuple[Dependency, ...] = ()

if asset_id in dep_cache:
logger.debug(f"Fetching cached asset {asset_id:#8x}...")
# logger.debug(f"Fetching cached asset {asset_id:#8x}...")
deps = dep_cache[asset_id]
else:
if dependency_cheating.should_cheat_asset(asset_type):
Expand All @@ -411,7 +428,7 @@ def _get_dependencies_for_asset(self, asset_id: NameOrAssetId, must_exist: bool,
else:
logger.warning(f"Potential missing assets for {asset_type} {asset_id}")

logger.debug(f"Adding {asset_id:#8x} deps to cache...")
# logger.debug(f"Adding {asset_id:#8x} deps to cache...")
dep_cache[asset_id] = deps

yield from deps
Expand All @@ -438,7 +455,7 @@ def get_dependencies_for_ancs(self, asset_id: NameOrAssetId, char_index: int | N
return

if char_index in self._cached_ancs_per_char_dependencies[asset_id]:
logger.debug(f"Fetching cached asset {asset_id:#8x}...")
# logger.debug(f"Fetching cached asset {asset_id:#8x}...")
deps = self._cached_ancs_per_char_dependencies[asset_id][char_index]
else:
deps = list(self.target_game.special_ancs_dependencies(asset_id))
Expand Down

0 comments on commit ff72426

Please sign in to comment.