Skip to content

Commit 706d569

Browse files
authored
Add filter functionality to extractor (K0lb3#133)
* Add filter functionality to extractor * Reduce indenting by using continue on failed object filter
1 parent 7c3e3ee commit 706d569

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

‎UnityPy/tools/extractor.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from UnityPy.enums.ClassIDType import ClassIDType
1919
from typing import Union, List, Dict
2020
from pathlib import Path
21+
from collections.abc import Callable
2122

2223

2324
def export_obj(
@@ -26,6 +27,7 @@ def export_obj(
2627
append_name: bool = False,
2728
append_path_id: bool = False,
2829
export_unknown_as_typetree: bool = False,
30+
asset_filter: Callable[[Object], bool] = None,
2931
) -> List[int]:
3032
"""Exports the given object to the given filepath.
3133
@@ -35,6 +37,7 @@ def export_obj(
3537
append_name (bool, optional): Decides if the obj name will be appended to the filepath. Defaults to False.
3638
append_path_id (bool, optional): Decides if the obj path id will be appended to the filepath. Defaults to False.
3739
export_unknown_as_typetree (bool, optional): If set, then unimplemented objects will be exported via their typetree or dumped as bin. Defaults to False.
40+
asset_filter (func(Object)->bool, optional): Determines whether to export an object. Defaults to all objects.
3841
3942
Returns:
4043
list: a list of exported object path_ids
@@ -50,6 +53,10 @@ def export_obj(
5053
# set filepath
5154
obj = obj.read()
5255

56+
# a filter that returned True during an earlier extract_assets check can return False now with more info from read()
57+
if asset_filter and not asset_filter(obj):
58+
return []
59+
5360
if append_name:
5461
fp = os.path.join(fp, obj.name if obj.name else obj.type.name)
5562

@@ -69,8 +76,9 @@ def extract_assets(
6976
ignore_first_container_dirs: int = 0,
7077
append_path_id: bool = False,
7178
export_unknown_as_typetree: bool = False,
79+
asset_filter: Callable[[Object], bool] = None,
7280
) -> List[int]:
73-
"""Extracts all assets from the given source.
81+
"""Extracts some or all assets from the given source.
7482
7583
Args:
7684
src (Union[Path, BytesIO, bytes, bytearray]): [description]
@@ -79,6 +87,7 @@ def extract_assets(
7987
ignore_first_container_dirs (int, optional): [description]. Defaults to 0.
8088
append_path_id (bool, optional): [description]. Defaults to False.
8189
export_unknown_as_typetree (bool, optional): [description]. Defaults to False.
90+
asset_filter (func(object)->bool, optional): Determines whether to export an object. Defaults to all objects.
8291
8392
Returns:
8493
List[int]: [description]
@@ -98,6 +107,9 @@ def defaulted_export_index(type: ClassIDType):
98107
if use_container:
99108
container = sorted(env.container, key=lambda x: defaulted_export_index(x[1].type))
100109
for obj_path, obj in container:
110+
# The filter here can only access metadata. The same filter may produce a different result later in extract_obj after obj.read()
111+
if asset_filter is not None and not asset_filter(obj):
112+
continue
101113
# the check of the various sub directories is required to avoid // in the path
102114
obj_dest = os.path.join(
103115
dst,
@@ -116,6 +128,8 @@ def defaulted_export_index(type: ClassIDType):
116128
else:
117129
objects = sorted(env.objects, key=lambda x: defaulted_export_index(x.type))
118130
for obj in objects:
131+
if asset_filter is not None and not asset_filter(obj):
132+
continue
119133
if obj.path_id not in exported:
120134
exported.extend(
121135
export_obj(

0 commit comments

Comments
 (0)