spxtr / p3

Super Smash Bros. Melee AI interface for Dolphin.
GNU General Public License v3.0
44 stars 8 forks source link

p3 crashes if any address is not within enum boundaries #8

Closed jabdownsmash closed 8 years ago

jabdownsmash commented 8 years ago

For global addresses, this will likely not be an issue, but this is an issue for player[i].action_state and will be for any addresses that chase pointers. If a game ends, the pointer isn't guaranteed to be pointing at char data anymore, and p3 ends up trying to parse some random value from the memory into the action_state fields, and the ActionState enum conversion fails.

For action_state specifically, a solution would be to ignore these addresses from Dolphin if menu isn't Game, but implementing a solution like that might not work for all addresses. A more general but less predictable solution would be to set the field to the default value when the enum conversion error is caught.

spxtr commented 8 years ago

Ah right, this was an issue lurking in the back of my mind that I didn't want to acknowledge. Are you going to work on a fix? It should hopefully be a simple check. I'd be fine with resetting to the default.

jabdownsmash commented 8 years ago

Enums are pretty limited in python it seems. The simplest fix looks like this:

def handle(value):
    transformed = (struct.unpack('>i', value)[0] >> shift) & mask
    wrapped = transformed
    if wrapper is not None:
        try:
            wrapper(transformed)
        except:
            wrapper = default
    setattr(obj, name, wrapped)

Little weird to handle it that way, but it makes sense to revert to default if the wrapper throws an error. An alternative would be to use a customized class instead of Enum and pass in a validation function to the handlers.

spxtr commented 8 years ago

I think the simple fix is fine, but make sure to do except ValueError:, update the comment saying that the wrapper should raise ValueError in case the input is bad, and add a test :)