"""Retain the now-closed original Dust input/domain/default-effect boundaries.

No source angle is changed and no production renderer is enabled by this tool.
"""
from pathlib import Path
import hashlib,json,struct
import pefile

root=Path('artifacts/galaxy-query-research/power-outer-sources')
image=Path('artifacts/ship-model-reference/client-copy/EliteDangerous64.exe')
raw=image.read_bytes();sha=hashlib.sha256(raw).hexdigest()
if sha!='6891f70a021fa9f3a0e00458c284f48d1226e68ae7fc02ad48081aed47857e93':raise ValueError('Pinned Dust image changed')
pe=pefile.PE(data=raw,fast_load=True)
if struct.unpack('<I',pe.get_data(0x4E2E500+38*4,4))[0]!=10:raise ValueError('Original Half4 input format changed')
if struct.unpack('<f',pe.get_data(0x4E32690,4))[0]!=6.2831854820251465:raise ValueError('Original angle range changed')
links=json.loads((root/'authored-models/effects/original-effect-shader-links.json').read_text())
effect=next(e for e in links['effects'] if e['effect']=='DistantMilkyWayDust_Forward')
materials=json.loads((root/'material/original-materials.json').read_text())
material=next(m for m in materials['materials'] if m['name']=='milkywaydust')
properties={p['name']:p.get('floatValues',p.get('integerValue')) for p in material['parameters']}
if properties['pDustColour']!=[0.,0.,0.]:raise ValueError('Original authored Dust tint changed')
paths=[root/'background/solo-39faee0.c',root/'background/solo-3b11860.c',root/'background/dust-input/solo-3c10dc0.c',
    root/'background/dust-input/solo-3c0d9d0.c',root/'decompiled/solo-3a21bc0.c',root/'decompiled/solo-3a469d0.c',
    root/'decompiled/solo-3ab44e0.c',root/'background/effect-loader/solo-4fa720.c']
result=dict(image=str(image),imageSha256=sha,gameProcessAccess=False,gameCalls=0,
    shaders=effect,input=dict(builderRva='0x3C0D9D0',quadConstructor='0x39FAEE0',draw='0x3A1E980',stride=56,
        attributes=[dict(name='POSITION0',offset=0,sourceFormat=1,dxgiFormat=2),
            dict(name='POSITION1',offset=16,sourceFormat=38,dxgiFormat=10),
            dict(name='POSITION2',offset=24,sourceFormat=1,dxgiFormat=2),
            dict(name='TEXCOORD3',offset=40,sourceFormat=1,dxgiFormat=2)],
        staticQuadBuilder='0x3C10DC0',rawDataUploadedWithoutTrigonometricConversion=True),
    producer=dict(jobRva='0x3A469D0',rotationField=20,rotationRange=[0,6.2831854820251465],
        rendererRva='0x3A21BC0',rotationCopiedToHalf4Xyz=True,firstLayerFlagInHalf4W=True,
        layerDefaultFunction='0x3B11860',layerAlphaSizeDefaults=[[struct.unpack('<f',struct.pack('<I',v))[0] for v in pair]
            for pair in [(0x402A2D0E,0x42CF21CB),(0x411D374C,0x415B9DB2),(0x409B22D1,0x41858937)]]),
    actualMaterialDefaults=properties,
    frameBlendOverride=dict(rva='0x3AB44E0',publisherField='0x8E0',runtimeHandleField='0x950',targetQuadField='0x2E0',
        operation=0,source=0,destination=2,rgb='Zero/SourceColor/Add',alpha='Zero/SourceAlpha/Add'),
    defaultRgbEffect='Read*Write=1 and source pDustColour=0 give transmission1: RGB identity',
    sourceFunctions=[dict(path=str(path),sha256=hashlib.sha256(path.read_bytes()).hexdigest()) for path in paths],
    warpProbe='artifacts/galaxy-dust-shader-validation/report.json',
    unclosed=['runtime nonzero pDustColour override, if any','complete multi-layer frame near/far overlap parameters',
        'current client/live GPU result (all evidence is pinned offline)'],
    productionDustEnabled=False,fullRestored=False)
(root/'background/original-dust-evidence.json').write_text(json.dumps(result,indent=2)+'\n',encoding='utf-8')
print(json.dumps(dict(path=str(root/'background/original-dust-evidence.json'),sourceFunctions=len(paths),inputStride=56,
    half4DxgiFormat=10,defaultRgbEffect='identity',productionDustEnabled=False)))
