Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def getShiftFromMask(mask):
- """Returns the shift value from the mask"""
- # get the shift by subtracting the length of the mask
- # converted in binary on 16 bits (since the mask can be on 16 bits) with
- # that length but with the rightmost zeros stripped
- return int(f"{len(f'{mask:016b}') - len(f'{mask:016b}'.rstrip('0'))}", base=10)
- def evalActorParams(parameters):
- """Returns the parameters as an integer"""
- def _getMaskAndValue(part):
- """Returns the mask and the value of the param part"""
- # if there's a mask
- # the value of it and the value of the param
- # if there's not a mask get the value from the part
- if (part.find('&') != -1):
- # get the value
- match = re.finditer(r"& ([0-9]+([a-zA-Z0-9]+)+)", part)
- # remove unwanted elements and
- # return the mask as an integer
- # and the value as a string
- for elem in match:
- strMask = elem.group(0)[2:]
- mask = int(strMask, base=16)
- value = part.replace('(','').replace(')','')[:-(len(strMask) + 3)]
- return mask, value
- return 0xFFFF, None
- def _getParams(parameters):
- """Returns the evaluated parameter value"""
- params = 0
- # split the string into parts and
- # process every elements of the list
- for part in parameters.split(' | '):
- # get shift, mask and the value
- mask, value = _getMaskAndValue(part)
- shift = getShiftFromMask(mask)
- # default the value to the current part
- if value is None:
- value = part
- # remove unwanted characters
- value = value.replace('(','').replace(')','')
- if value.find("<<") != -1:
- value = value.replace(" << ",'')[:-len(f"{shift}")]
- # apply the shift and the mask
- # and do a OR on the ``params`` variable
- # with the value
- params |= ((int(value, base=16) << shift) & mask)
- # finally, return the parameter value
- return params
- return _getParams(parameters)
Advertisement
Add Comment
Please, Sign In to add comment