Skip to content

Commit 086124e

Browse files
committed
strip fname suffixes in legacy compat
1 parent a6165de commit 086124e

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

src/legacy_compat/unrealsdk/__init__.py

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# ruff: noqa: N802, N803, D102, D103, N999
22

33
import inspect
4+
import re
45
from collections.abc import Callable, Iterator, Sequence
56
from contextlib import contextmanager
67
from dataclasses import dataclass
@@ -263,10 +264,13 @@ def KeepAlive(obj: UObject, /) -> None:
263264
- The legacy sdk returned None instead of throwing an attribute error when a field didn't exist.
264265
- The legacy sdk had interface properties return an FScriptInterface struct, but since you only ever
265266
accessed the object, the new sdk just returns it directly. We need to return the struct instead.
267+
- In the legacy sdk, name properties did not include the number when converted to a string, which we
268+
need to strip out.
266269
267270
UObject:
268271
- The `ObjectFlags` field on objects used to be split into the upper and lower 32 bits (B and A
269272
respectively), new sdk returns a single 64 bit int. Return a proxy object instead.
273+
- The `Name` field is also a name property, so needs have the suffix stripped as above.
270274
- In the legacy sdk the __repr__ of objects was just kind of wrong, but we need to replicate it.
271275
272276
BoundFunction:
@@ -340,6 +344,21 @@ def _convert_struct_tuple_if_required(prop: UProperty, value: Any) -> Any:
340344
return value
341345

342346

347+
_RE_NAME_SUFFIX = re.compile(r"^(.+)_\d+$")
348+
349+
350+
def _strip_name_property_suffix(name: str) -> str:
351+
"""
352+
Tries to strip the numeric suffix from a name property.
353+
354+
Args:
355+
name: The input name.
356+
Returns:
357+
The stripped name.
358+
"""
359+
return match.group(1) if (match := _RE_NAME_SUFFIX.match(name)) else name
360+
361+
343362
@wraps(UObject.__getattr__)
344363
def _uobject_getattr(self: UObject, name: str) -> Any:
345364
try:
@@ -349,10 +368,13 @@ def _uobject_getattr(self: UObject, name: str) -> Any:
349368

350369
value = self._get_field(prop)
351370

352-
if isinstance(prop, UInterfaceProperty):
353-
return FScriptInterface(value)
354-
355-
return value
371+
match prop:
372+
case UInterfaceProperty():
373+
return FScriptInterface(value)
374+
case UNameProperty():
375+
return _strip_name_property_suffix(value)
376+
case _:
377+
return value
356378

357379

358380
@dataclass
@@ -384,12 +406,16 @@ def __int__(self) -> int:
384406
return _default_object_getattribute(self._obj, "ObjectFlags")
385407

386408

387-
# Because we want to overwrite an exiting field, we have to use getattribute over getattr
409+
# Because we want to overwrite exiting fields, we have to use getattribute over getattr
388410
@wraps(UObject.__getattribute__)
389411
def _uobject_getattribute(self: UObject, name: str) -> Any:
390-
if name == "ObjectFlags":
391-
return _ObjectFlagsProxy(self)
392-
return _default_object_getattribute(self, name)
412+
match name:
413+
case "ObjectFlags":
414+
return _ObjectFlagsProxy(self)
415+
case "Name":
416+
return _strip_name_property_suffix(_default_object_getattribute(self, "Name"))
417+
case _:
418+
return _default_object_getattribute(self, name)
393419

394420

395421
@wraps(UObject.__setattr__)
@@ -422,10 +448,13 @@ def _struct_getattr(self: WrappedStruct, name: str) -> Any:
422448

423449
value = self._get_field(prop)
424450

425-
if isinstance(prop, UInterfaceProperty):
426-
return FScriptInterface(value)
427-
428-
return value
451+
match prop:
452+
case UInterfaceProperty():
453+
return FScriptInterface(value)
454+
case UNameProperty():
455+
return _strip_name_property_suffix(value)
456+
case _:
457+
return value
429458

430459

431460
@wraps(WrappedStruct.__setattr__)
@@ -443,14 +472,23 @@ def _struct_setattr(self: WrappedStruct, name: str, value: Any) -> None:
443472
def _array_getitem[T](self: WrappedArray[T], idx: int | slice) -> T | list[T]:
444473
value = _default_array_getitem(self, idx)
445474

446-
if not isinstance(self._type, UInterfaceProperty):
447-
return value
475+
match self._type:
476+
case UInterfaceProperty():
477+
if isinstance(idx, slice):
478+
val_seq: Sequence[T] = value # type: ignore
479+
return [FScriptInterface(x) for x in val_seq] # type: ignore
448480

449-
if isinstance(idx, slice):
450-
val_seq: Sequence[T] = value # type: ignore
451-
return [FScriptInterface(x) for x in val_seq] # type: ignore
481+
return FScriptInterface(value) # type: ignore
482+
case UNameProperty():
483+
if isinstance(idx, slice):
484+
val_seq: Sequence[T] = value # type: ignore
485+
return [_strip_name_property_suffix(x) for x in val_seq] # type: ignore
452486

453-
return FScriptInterface(value) # type: ignore
487+
return _strip_name_property_suffix(value) # type: ignore
488+
489+
return _strip_name_property_suffix(value)
490+
case _:
491+
return value
454492

455493

456494
@wraps(WrappedArray[Any].__setitem__)

0 commit comments

Comments
 (0)