Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few APIs from OPR #73

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 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,10 +285,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 @@ -305,6 +305,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 @@ -331,6 +340,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 @@ -393,7 +409,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 +423,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 +450,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