using System.Runtime.InteropServices; using System.Text; using System.Text.Json; using System.Text.Json.Nodes; using static XFEEDToolBox.Plugin.JsonWire; namespace XFEEDToolBox.Plugin; internal static unsafe partial class Navigation { // All COM methods use their documented D3D11 vtable slot through the guarded ABI boundary. internal static class SkyCapture { [StructLayout(LayoutKind.Sequential)] struct ShaderResourceDescription {public uint format,dimension,mostDetailedMip,mipLevels,unused0,unused1;} [StructLayout(LayoutKind.Sequential)] struct TextureDescription {public uint width,height,mipLevels,arraySize,format,sampleCount,sampleQuality,usage,bindFlags,cpuAccessFlags,miscFlags;} [StructLayout(LayoutKind.Sequential)] struct MappedResource {public nuint data;public uint rowPitch,depthPitch;} static nuint original,hookTarget; static volatile bool active; static bool primaryOnly; static readonly object mutex=new(); static string directory=""; static readonly List seen=[]; static JsonArray files=new(); static ulong until; [ThreadStatic]static bool observing; static nuint Method(nuint instance,uint slot)=>Pointer(Pointer(instance)+slot*8); static void Release(nuint instance){if(instance!=0)Call(Method(instance,2),instance);} static bool Failed(ulong result)=>unchecked((int)result)<0; static uint RowPitch(uint format,uint width,out uint rows) { uint block=format switch{71 or 72=>8,77 or 78 or 95 or 96 or 98 or 99=>16,_=>0}; if(block!=0){rows=(width+3)/4;return ((width+3)/4)*block;} rows=width;return format switch{2=>width*16,10=>width*8,26 or 67 or 28 or 29 or 87 or 91=>width*4,_=>0}; } internal static bool LayoutSelfTest() { if(sizeof(ShaderResourceDescription)!=24||sizeof(TextureDescription)!=44||sizeof(MappedResource)!=16)return false; foreach(var format in new uint[]{71,72})if(RowPitch(format,5,out var rows)!=16||rows!=2)return false; foreach(var format in new uint[]{77,78,95,96,98,99})if(RowPitch(format,5,out var rows)!=32||rows!=2)return false; if(RowPitch(2,256,out var rgbaRows)!=4096||rgbaRows!=256||RowPitch(10,256,out _)!=2048)return false; foreach(var format in new uint[]{26,67,28,29,87,91})if(RowPitch(format,256,out var rows)!=1024||rows!=256)return false; return RowPitch(0,256,out _)==0; } static void Copy(nuint context,nuint view,uint slot) { ShaderResourceDescription srv=default;Call(Method(view,8),view,(nuint)(&srv)); if(srv.dimension!=9)return; // D3D11_SRV_DIMENSION_TEXTURECUBE nuint resource=0,source=0,device=0,copy=0; try { Call(Method(view,7),view,(nuint)(&resource));if(resource==0)return; var textureId=new Guid("6f15aaf2-d208-4e89-9ab4-489535d34f9c"); if(Failed(Call(Method(resource,0),resource,(nuint)(&textureId),(nuint)(&source)))||source==0)return; var identity=source;if(seen.Contains(identity))return; TextureDescription description=default;Call(Method(source,10),source,(nuint)(&description)); if((description.miscFlags&4)==0||description.arraySize!=6||description.width<128||description.width!=description.height||description.mipLevels==0)return; if(primaryOnly&&(slot!=0||srv.format!=26||description.width!=256||srv.mostDetailedMip!=0))return; var mip=srv.mostDetailedMip;if(mip>=description.mipLevels||mip>=32)return; var width=description.width>>(int)mip; while(width>1024&&mip+1>(int)mip;} if(width<16||width>2048)return; var pitch=RowPitch(srv.format,width,out var rows);if(pitch==0||(ulong)pitch*rows*6>128*1024*1024)return; Call(Method(context,3),context,(nuint)(&device));if(device==0)return; var staging=description;staging.width=staging.height=width;staging.mipLevels=1;staging.usage=3;staging.bindFlags=0;staging.cpuAccessFlags=0x20000;staging.miscFlags=0; if(Failed(Call(Method(device,5),device,(nuint)(&staging),0,(nuint)(©)))||copy==0)return; for(uint face=0;face<6;face++)Call(Method(context,46),context,copy,face,0,0,0,source,mip+face*description.mipLevels,0); var name="cube-"+files.Count+".dds";var path=Path.Combine(directory,name);var temporary=path+".partial"; using(var stream=new FileStream(temporary,FileMode.Create,FileAccess.Write,FileShare.None)) using(var output=new BinaryWriter(stream,Encoding.UTF8,leaveOpen:false)) { var header=new uint[37];header[0]=0x20534444;header[1]=124;header[2]=0x100F;header[3]=header[4]=width;header[5]=pitch;header[7]=1; header[19]=32;header[20]=4;header[21]=0x30315844;header[27]=0x1008;header[28]=0xFE00; header[32]=srv.format;header[33]=3;header[34]=4;header[35]=1; foreach(var word in header)output.Write(word); for(uint face=0;face<6;face++) { MappedResource mapped=default;if(Failed(Call(Method(context,14),context,copy,face,1,0,(nuint)(&mapped))))return; try { if(mapped.data==0||mapped.rowPitch128||observing)return; try { var frame=Current();if(frame.controlApplied||!Monitor.TryEnter(mutex))return; observing=true; try { if(GetTickCount64()>until||files.Count>=12){active=false;return;} for(uint i=0;iGetTickCount64())throw new InvalidOperationException("Stop automatic flight before reading skybox resources"); if(hookTarget==0) { nuint device=0,context=0;uint feature=0,level=0xB000; try { if(D3D11CreateDevice(0,1,0,0,&level,1,7,&device,&feature,&context)<0||context==0)throw new InvalidOperationException("D3D11 resource observer unavailable"); var target=Method(context,8); if(Platform.HookCreate(target,(nuint)(delegate* unmanaged)&Set,out original)!=0) throw new InvalidOperationException("D3D11 resource observer could not be installed"); if(Platform.HookEnable(target)!=0){Platform.HookRemove(target);original=0;throw new InvalidOperationException("D3D11 resource observer could not be installed");} hookTarget=target; } finally{try{Release(context);}finally{Release(device);}} } var local=Environment.GetEnvironmentVariable("LOCALAPPDATA");if(string.IsNullOrEmpty(local)||local.Length>=32768)throw new InvalidOperationException("Local game display cache unavailable"); directory=Path.Combine(local,"XFEstudio","XFEEDToolBox","galaxy","capture-"+GetTickCount64()); Directory.CreateDirectory(directory);files=new();seen.Clear();primaryOnly=op=="galaxy.skybox.refresh";until=GetTickCount64()+60000;active=true; } return Obj(("active",active),("directory",directory),("candidates",files.DeepClone()),("screenCapture",false)); } } internal static void Stop() { active=false; lock(mutex)if(hookTarget!=0){Platform.HookDisable(hookTarget);Platform.HookRemove(hookTarget);hookTarget=0;original=0;} } } }