"""Read bounded original code constants and vtables from the pinned PE copy."""
import json
import struct
from pathlib import Path
import pefile
image=Path('artifacts/ship-model-reference/client-copy/EliteDangerous64.exe')
pe=pefile.PE(str(image),fast_load=True)
base=pe.OPTIONAL_HEADER.ImageBase
tables={}
for name,rva,count in [('physical',0x552e718,20),('starclass',0x552e130,20),('government',0x552dde8,20),('economy',0x552db48,20)]:
tables[name]=[dict(slot=hex(index*8),rva=hex(struct.unpack('<Q',pe.get_data(rva+index*8,8))[0]-base)) for index in range(count)]
result=dict(image=str(image),vtables=tables,colourConversion=dict(gamma=struct.unpack('<f',pe.get_data(0x50ff9c0,4))[0],offset=struct.unpack('<f',pe.get_data(0x4e3203c,4))[0]),
spectralTypePalette=[dict(type=index,packedRgba=hex(struct.unpack('<I',pe.get_data(0x5539360+index*4,4))[0]),rgba=list(pe.get_data(0x5539360+index*4,4))) for index in range(64)],
physicalSizeConstants={hex(rva):struct.unpack('<f',pe.get_data(rva,4))[0] for rva in (0x506b228,0x4deaa50)})
result['enumerations']={}
result['enumerations']['stellarType']=[dict(value=index,name=pe.get_data(struct.unpack('<Q',pe.get_data(0x5e9dbc0+index*8,8))[0]-base,128).split(b'\0',1)[0].decode('ascii')) for index in range(48)]
result['enumerations']['luminosity']=[dict(value=index,name=pe.get_data(struct.unpack('<Q',pe.get_data(0x5e9dae0+index*8,8))[0]-base,128).split(b'\0',1)[0].decode('ascii')) for index in range(27)]
result['luminosityAccessor']=dict(rva='0x3c59990',tableRva='0x5e9dae0',classificationMask='0x7c00',classificationShift=10)
result['starClassGroups']=[dict(paletteType=value,types=members) for value,members in [(0,[0]),(1,[1]),(2,[2]),(3,[3]),(4,[4]),(5,[5]),(6,[6]),(7,[7]),(8,[8]),(11,[11]),(9,[9,10]),(18,list(range(17,25))),(12,list(range(12,17))),(25,list(range(25,40))),(40,list(range(40,44)))]]
for name,rva in [('government',0x5e9e320),('economy',0x5e9e330)]:
pointer,count,_=struct.unpack('<QII',pe.get_data(rva,16));rows=[]
for index in range(count):
value,text=struct.unpack('<I4xQ',pe.get_data(pointer-base+index*16,16))
rows.append(dict(value=value,name=pe.get_data(text-base,256).split(b'\0',1)[0].decode('ascii')))
result['enumerations'][name]=rows
out=Path('artifacts/galaxy-query-research/palette-resources/code-tables.json')
out.write_text(json.dumps(result,indent=2)+'\n',encoding='utf-8')
print(json.dumps(result,indent=2))
"""Read bounded original code constants and vtables from the pinned PE copy."""
import json
import struct
from pathlib import Path
import pefile
image=Path('artifacts/ship-model-reference/client-copy/EliteDangerous64.exe')
pe=pefile.PE(str(image),fast_load=True)
base=pe.OPTIONAL_HEADER.ImageBase
tables={}
for name,rva,count in [('physical',0x552e718,20),('starclass',0x552e130,20),('government',0x552dde8,20),('economy',0x552db48,20)]:
tables[name]=[dict(slot=hex(index*8),rva=hex(struct.unpack('<Q',pe.get_data(rva+index*8,8))[0]-base)) for index in range(count)]
result=dict(image=str(image),vtables=tables,colourConversion=dict(gamma=struct.unpack('<f',pe.get_data(0x50ff9c0,4))[0],offset=struct.unpack('<f',pe.get_data(0x4e3203c,4))[0]),
spectralTypePalette=[dict(type=index,packedRgba=hex(struct.unpack('<I',pe.get_data(0x5539360+index*4,4))[0]),rgba=list(pe.get_data(0x5539360+index*4,4))) for index in range(64)],
physicalSizeConstants={hex(rva):struct.unpack('<f',pe.get_data(rva,4))[0] for rva in (0x506b228,0x4deaa50)})
result['enumerations']={}
result['enumerations']['stellarType']=[dict(value=index,name=pe.get_data(struct.unpack('<Q',pe.get_data(0x5e9dbc0+index*8,8))[0]-base,128).split(b'\0',1)[0].decode('ascii')) for index in range(48)]
result['enumerations']['luminosity']=[dict(value=index,name=pe.get_data(struct.unpack('<Q',pe.get_data(0x5e9dae0+index*8,8))[0]-base,128).split(b'\0',1)[0].decode('ascii')) for index in range(27)]
result['luminosityAccessor']=dict(rva='0x3c59990',tableRva='0x5e9dae0',classificationMask='0x7c00',classificationShift=10)
result['starClassGroups']=[dict(paletteType=value,types=members) for value,members in [(0,[0]),(1,[1]),(2,[2]),(3,[3]),(4,[4]),(5,[5]),(6,[6]),(7,[7]),(8,[8]),(11,[11]),(9,[9,10]),(18,list(range(17,25))),(12,list(range(12,17))),(25,list(range(25,40))),(40,list(range(40,44)))]]
for name,rva in [('government',0x5e9e320),('economy',0x5e9e330)]:
pointer,count,_=struct.unpack('<QII',pe.get_data(rva,16));rows=[]
for index in range(count):
value,text=struct.unpack('<I4xQ',pe.get_data(pointer-base+index*16,16))
rows.append(dict(value=value,name=pe.get_data(text-base,256).split(b'\0',1)[0].decode('ascii')))
result['enumerations'][name]=rows
out=Path('artifacts/galaxy-query-research/palette-resources/code-tables.json')
out.write_text(json.dumps(result,indent=2)+'\n',encoding='utf-8')
print(json.dumps(result,indent=2))