Skip to content

Commit

Permalink
Add a few APIs from OPR
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquegemignani committed Jul 30, 2023
1 parent c06831d commit 5bb1ccf
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/retro_data_structures/asset_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import logging
import typing
import uuid
from collections import defaultdict
from collections.abc import Iterator
from pathlib import Path
Expand Down Expand Up @@ -286,8 +285,8 @@ 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] = ()):
"""
Adds an asset that doesn't already exist.
"""
Expand All @@ -305,6 +304,13 @@ 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)

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), ())

Check warning on line 312 in src/retro_data_structures/asset_manager.py

View check run for this annotation

Codecov / codecov/patch

src/retro_data_structures/asset_manager.py#L312

Added line #L312 was not covered by tests

def replace_asset(self, asset_id: NameOrAssetId, new_data: Resource):
"""
Replaces an existing asset.
Expand All @@ -331,6 +337,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)

Check warning on line 343 in src/retro_data_structures/asset_manager.py

View check run for this annotation

Codecov / codecov/patch

src/retro_data_structures/asset_manager.py#L342-L343

Added lines #L342 - L343 were not covered by tests
else:
return self.add_new_asset(name, new_data)

Check warning on line 345 in src/retro_data_structures/asset_manager.py

View check run for this annotation

Codecov / codecov/patch

src/retro_data_structures/asset_manager.py#L345

Added line #L345 was not covered by tests

def delete_asset(self, asset_id: NameOrAssetId):
original_name = asset_id
asset_id = self._resolve_asset_id(asset_id)
Expand Down Expand Up @@ -393,7 +406,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 @@ -407,7 +420,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 @@ -434,7 +447,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:
from retro_data_structures.formats.ancs import Ancs
Expand Down

0 comments on commit 5bb1ccf

Please sign in to comment.