Repository navigation
Change how enums are created, fixes K0lb3/UnityPy#67 - #71
UserUnknownFactor wants to merge 1 commit into
Conversation
|
It is indeed a better solution than the current system that definitely causes problems once multiple instances with different unknown values pop up. The general idea of what I intended to do with it was to make sure that the field will definitely be of the specific enum type. I think that your implementation would break that idea and return an int instead if the number is not within the enum class. def makeBuildTarget(_bt):
if isinstance(_bt, BuildTarget):
return _bt
elif BuildTarget.has_value(_bt):
return BuildTarget(_bt)
return _bt
```
An alternate solution for that part would have been
```python
class X(IntEnum):
@classmethod
def _missing_(cls, value):
return valueI guess there are only three real solutions for the problem,
|
It can't always be strictly |
|
I found a more or less clean solution for this issue, from enum import IntEnum
# based on https://stackoverflow.com/a/57179436
class ExtendableEnum(IntEnum):
@classmethod
def _missing_(cls, value):
if isinstance(value, int):
pseudo_member = cls._value2member_map_.get(value, None)
if pseudo_member is None:
new_member = int.__new__(cls, value)
# I expect a name attribute to hold a string, hence str(value)
# However, new_member._name_ = value works, too
new_member._name_ = f"Unknown ({value})"
new_member._value_ = value
pseudo_member = cls._value2member_map_.setdefault(value, new_member)
return pseudo_member
return None # will raise the ValueError in Enum.__new__BuildTarget and ClassIDType can simply inherit this class and everything should work as intended while keeping an Enum interface. |
|
That's fine too... |
|
I don't intend to slight your work and idea. |
Is it viable this way? It's better if the value stores original data if there is no corresponding enum field so we can later write it back as is.