"""Guarded native menu controls/help; Xbox labels follow configured core mapping."""
ORIGIN=0x25B000
HOOKS=[
 (0x7E88,'080100076600005e',0),
 (0x8330,'08010007660004dc',4),
 (0x843E,'080100076600fed6',8),
 (0x85E0,'4278b0304e75',12),
 (0x7E6A,'31fc0018bd264e75',16),
 (0xF7918,'4cdf7fff4e75',20),
 (0xF748E,'0801000766000faa',24),
 (0x11290,'61004c2e3038b03e',44),
 (0x7DAE,'4eb900012c04',52),
 (0xA39E,'4278cf2008f80001c2f6',72),
]
def install(data,assemble,patch,with_cosmetic_ui=False):
 code=assemble('menu_ui',ORIGIN)
 assert len(code)<0x1000 and len(code)%2==0
 for text in [b'A Back  B Select  Start Done',b'A Back  B Select  Start Back',b'A Back  B Select  Start Play',b'D-pad: Move selection']:
  item=b'\x00\x22'+text.ljust(32,b' ')
  assert code.count(item)==1 and code.find(item)%2==0, 'unaligned or malformed native footer text'
 main=b'\x00\x1cD-pad Change  B/Start Play'if with_cosmetic_ui else b'\x00\x22'+b'D-pad Change   B/Start Play'.ljust(32,b' ')
 assert code.count(main)==1 and code.find(main)%2==0,'unaligned or malformed main footer'
 if with_cosmetic_ui:
  # Five visible settings rows reserve a real3-row native footer inside224px.
  for at,old,new in [(0xF7728,5,4),(0xF774C,5,4),(0xF7758,20,21),(0xF7762,20,21),(0xF7C08,20,21),(0xF7B82,27,25),(0xF7BFC,25,23),(0xF80A2,25,23)]:
   patch(at,old.to_bytes(2,'big'),new.to_bytes(2,'big'))
 patch(ORIGIN,b'\xff'*len(code),code)
 for at,old,entry in HOOKS:
  old=bytes.fromhex(old);new=bytes.fromhex('4ef9')+(ORIGIN+entry).to_bytes(4,'big')+bytes.fromhex('4e71')*((len(old)-6)//2)
  patch(at,old,new)
