Skip to content

Change how enums are created, fixes K0lb3/UnityPy#67 - #71

Closed
UserUnknownFactor wants to merge 1 commit into
K0lb3:masterfrom
UserUnknownFactor:fix_enums
Closed

UserUnknownFactor wants to merge 1 commit into
K0lb3:masterfrom
UserUnknownFactor:fix_enums

Conversation

@UserUnknownFactor

Copy link
Copy Markdown
Contributor

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.

@K0lb3

K0lb3 commented Oct 3, 2021 •

Copy link
Copy Markdown
Owner

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 value

I guess there are only three real solutions for the problem,

  1. What you plan to do, trying to let all comparisons with instances of the enum classes be done via eq,
  2. Extending the enum class within missing e.g. via extend_enum from aenum which would be quite hacky.
  3. Using a (custom) type that can handle unknown values but behaves like an enum otherwise.

@UserUnknownFactor

Copy link
Copy Markdown
Contributor Author

make sure that the field will definitely be of the specific enum type.

It can't always be strictly Enum unless we know all possible values beforehand. It can be only Enum-like anyway as this is meta-project that heavily depends on unknown parent's project behaviour. So it's better to have possibility of several unknown types.

@K0lb3

K0lb3 commented Oct 17, 2021 •

Copy link
Copy Markdown
Owner

I found a more or less clean solution for this issue,
that makes it possible to create new enum fields on the fly.

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.

@K0lb3 K0lb3 closed this in 612cce4 Oct 22, 2021
@UserUnknownFactor

Copy link
Copy Markdown
Contributor Author

That's fine too...

@K0lb3

K0lb3 commented Oct 25, 2021

Copy link
Copy Markdown
Owner

I don't intend to slight your work and idea.
I simply prefer this solution as it's easily reuseable for other enums while not affecting the remaining code at all.

@UserUnknownFactor
UserUnknownFactor deleted the fix_enums branch February 11, 2022 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants