Yanis4224

evalActorParams

Jun 15th, 2022
1,606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. def getShiftFromMask(mask):
  2.     """Returns the shift value from the mask"""
  3.     # get the shift by subtracting the length of the mask
  4.     # converted in binary on 16 bits (since the mask can be on 16 bits) with
  5.     # that length but with the rightmost zeros stripped
  6.     return int(f"{len(f'{mask:016b}') - len(f'{mask:016b}'.rstrip('0'))}", base=10)
  7.  
  8. def evalActorParams(parameters):
  9.     """Returns the parameters as an integer"""
  10.     def _getMaskAndValue(part):
  11.         """Returns the mask and the value of the param part"""
  12.  
  13.         # if there's a mask
  14.         # the value of it and the value of the param
  15.         # if there's not a mask get the value from the part
  16.         if (part.find('&') != -1):
  17.             # get the value
  18.             match = re.finditer(r"& ([0-9]+([a-zA-Z0-9]+)+)", part)
  19.  
  20.             # remove unwanted elements and
  21.             # return the mask as an integer
  22.             # and the value as a string
  23.             for elem in match:
  24.                 strMask = elem.group(0)[2:]
  25.                 mask = int(strMask, base=16)
  26.                 value = part.replace('(','').replace(')','')[:-(len(strMask) + 3)]
  27.                 return mask, value
  28.         return 0xFFFF, None
  29.  
  30.     def _getParams(parameters):
  31.         """Returns the evaluated parameter value"""
  32.         params = 0
  33.  
  34.         # split the string into parts and
  35.         # process every elements of the list
  36.         for part in parameters.split(' | '):
  37.             # get shift, mask and the value
  38.             mask, value = _getMaskAndValue(part)
  39.             shift = getShiftFromMask(mask)
  40.  
  41.             # default the value to the current part
  42.             if value is None:
  43.                 value = part
  44.  
  45.             # remove unwanted characters
  46.             value = value.replace('(','').replace(')','')
  47.             if value.find("<<") != -1:
  48.                 value = value.replace(" << ",'')[:-len(f"{shift}")]
  49.            
  50.             # apply the shift and the mask
  51.             # and do a OR on the ``params`` variable
  52.             # with the value
  53.             params |= ((int(value, base=16) << shift) & mask)
  54.        
  55.         # finally, return the parameter value
  56.         return params
  57.  
  58.     return _getParams(parameters)
  59.  
Advertisement
Add Comment
Please, Sign In to add comment