Skip to content

Commit 2dfe0bc

Browse files
committed
better handle nested struct tuples, fixed arrays of structs
fixes reward reroller
1 parent 0e5a053 commit 2dfe0bc

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

src/legacy_compat/unrealsdk/__init__.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
UObject,
4242
UObjectProperty,
4343
UProperty,
44-
UScriptStruct,
4544
UStrProperty,
4645
UStruct,
4746
UStructProperty,
@@ -298,28 +297,11 @@ def KeepAlive(obj: UObject, /) -> None:
298297
_default_func_call = BoundFunction.__call__
299298

300299

301-
def _create_struct_from_tuples(struct: UScriptStruct, value: tuple[Any, ...]) -> WrappedStruct:
302-
"""
303-
Recursively creates a wrapped struct from it's tuple equivalent.
304-
305-
Args:
306-
struct: The struct type to create:
307-
value: The tuple to create it with.
308-
Returns:
309-
The new struct.
310-
"""
311-
return WrappedStruct(
312-
struct,
313-
*(
314-
_create_struct_from_tuples(prop.Struct, inner_val) # pyright: ignore[reportUnknownArgumentType]
315-
if isinstance(prop, UStructProperty) and isinstance(inner_val, tuple)
316-
else inner_val
317-
for prop, inner_val in zip(struct._properties(), value, strict=False)
318-
),
319-
)
320-
321-
322-
def _convert_struct_tuple_if_required(prop: UProperty, value: Any) -> Any:
300+
def _convert_struct_tuple_if_required(
301+
prop: UProperty,
302+
value: Any,
303+
_ignore_array_dim: bool = False,
304+
) -> Any:
323305
"""
324306
Convert any tuple-based structs in the given value into Wrapped Structs.
325307
@@ -330,16 +312,29 @@ def _convert_struct_tuple_if_required(prop: UProperty, value: Any) -> Any:
330312
The possibly converted value.
331313
"""
332314

315+
# If it's a fixed array of structs, need to convert each inner value
316+
if not _ignore_array_dim and prop.ArrayDim > 1 and isinstance(prop, UStructProperty):
317+
return tuple(
318+
_convert_struct_tuple_if_required(prop, inner_val, _ignore_array_dim=True)
319+
for inner_val in value # type: ignore
320+
)
321+
333322
# If it's a struct being set as a tuple directly
334323
if isinstance(prop, UStructProperty) and isinstance(value, tuple):
335-
return _create_struct_from_tuples(prop.Struct, value) # pyright: ignore[reportUnknownArgumentType]
324+
return WrappedStruct(
325+
prop.Struct,
326+
*(
327+
_convert_struct_tuple_if_required(inner_prop, inner_val)
328+
for inner_prop, inner_val in zip(prop.Struct._properties(), value, strict=False) # type: ignore
329+
),
330+
)
336331

337332
# If it's an array of structs, need to convert each value
338333
if isinstance(prop, UArrayProperty) and isinstance(prop.Inner, UStructProperty):
339334
seq_value: Sequence[Any] = value
340335

341336
return tuple(
342-
_create_struct_from_tuples(prop.Inner.Struct, inner_val) # pyright: ignore[reportUnknownArgumentType]
337+
_convert_struct_tuple_if_required(prop.Inner, inner_val)
343338
if isinstance(inner_val, tuple)
344339
else inner_val
345340
for inner_val in seq_value

0 commit comments

Comments
 (0)