#if PLUGIN_SESSION_RECOVERY using System.Text; namespace XFEEDToolBox.Plugin; internal static unsafe partial class Navigation { // The returned token is an original intrusive-ref object, NOT a binding // wrapper. Its release ABI is 77A6A0(slot), never 204010(bindingSlot). internal sealed class RecoveryFlightContextBinding { internal RecoveryResetBinding ship=null!; internal nuint entry,active; internal bool reproduction; } internal sealed record RecoveryFlightContextLayers(nuint entry,nuint data,ulong count,nuint vm,byte active, ulong[] occupancy,bool ownMember,ulong foreignCount) { internal bool Empty=>occupancy.All(value=>value==0); internal bool OnlyOwn=>ownMember&&occupancy.Length>0&&occupancy[0]==1&&foreignCount==0; } private static bool FlightContextHeap(nuint value)=>value>=0x10000&&value<0x7fffffff0000&&(value&7)==0&&!InImage(value); internal static bool FlightContextNoSeedThrottle(float[] values)=>values.Length==4 &&values.Take(3).All(value=>float.IsFinite(value)&&Math.Abs(value)<=.001f) // Original 25E9360 clears the optional controller override to -100. // This is kept as a raw released marker, never reported as throttle 0. &&float.IsFinite(values[3])&&(Math.Abs(values[3])<=.001f||values[3]==-100f); private static bool FlightContextName(nuint value) { if(!FlightContextHeap(value)||Read(value)!=15)return false; var name=Platform.ReadBytes(value+0x14,15); return Encoding.ASCII.GetString(name).Equals("Bindings_Flight",StringComparison.OrdinalIgnoreCase); } private static nuint FlightContextEntry(nuint registry,nuint expected=0) { if(!FlightContextHeap(registry)||Pointer(registry)!=image+0x512FD90||Pointer(Pointer(registry)+0x20)!=image+0x8FA080 ||Pointer(Pointer(registry)+0x10)!=image+0x8ED50C)throw new InvalidOperationException("Original Flight registry ABI differs"); var buckets=Pointer(registry+0x78);var count=Read(registry+0x80); if(!FlightContextHeap(buckets)||count is 0 or >4096)throw new InvalidOperationException("Original Flight registry map bound differs"); var seen=new HashSet();nuint match=0; for(nuint i=0;i<(nuint)count;i++) { var head=buckets+i*8;var item=Pointer(head); while(item=buckets+(nuint)count*8) { if(!FlightContextHeap(item)||!seen.Add(item)||seen.Count>4096)throw new InvalidOperationException("Original Flight registry chain differs"); if(FlightContextName(Pointer(item+8))){if(match!=0)throw new InvalidOperationException("Original Flight profile is ambiguous");match=item;} item=Pointer(item); } } if(match==0||expected!=0&&match!=expected||Pointer(registry+0x78)!=buckets||Read(registry+0x80)!=count) throw new InvalidOperationException("Original Flight registry changed"); return match; } internal static RecoveryFlightContextBinding BindRecoveryFlightContext(ulong entity,ulong world,JsonElement target,double hull,double? shield,nuint node,bool reproduction) { var ship=BindRecoveryReset(entity,world,target,hull,shield,node); var active=Pointer(node+0x90); if(!FixtureBindingName(active,"Bindings_Flight_Active")||Pointer(active+0x10)!=ship.registry+0xB8) throw new InvalidOperationException("Original current-source Flight Active binding differs"); return new(){ship=ship,entry=FlightContextEntry(ship.registry),active=active,reproduction=reproduction}; } internal static RecoveryFlightContextLayers ReadRecoveryFlightContextLayers(RecoveryFlightContextBinding proof,nuint token) { var ship=proof.ship;var registry=ship.registry;var entry=FlightContextEntry(registry,proof.entry); var header=Platform.ReadBytes(entry+0x10,0x30);var count=Read(entry+0x28);var data=Pointer(entry+0x30);var vm=Pointer(entry+0x38); if(count>256||count>0&&!FlightContextHeap(data)||vm!=0&&!FlightContextHeap(vm))throw new InvalidOperationException("Original Flight layer array exceeds its bound"); var raw=count==0?[]:Platform.ReadBytes(data,checked((int)count*32));var occupancy=new ulong[count];var member=false;ulong foreign=0; for(nuint i=0;i<(nuint)count;i++) { var set=data+i*32;var used=Read(set+0x18);occupancy[i]=used; if(used>4096)throw new InvalidOperationException("Original Flight layer occupancy exceeds its bound"); if(used==0)continue; // 9091E0 inserts the exact raw token into this original hash set. var buckets=Pointer(set);var capacity=Read(set+8); if(!FlightContextHeap(buckets)||capacity is 0 or >8192)throw new InvalidOperationException("Original Flight token set bound differs"); var seen=new HashSet();ulong matches=0; for(nuint b=0;b<(nuint)capacity;b++) { var head=buckets+b*8;var item=Pointer(head); while(item!=head) { if(!FlightContextHeap(item)||!seen.Add(item)||seen.Count>4096)throw new InvalidOperationException("Original Flight token set chain differs"); var value=Pointer(item+8);if(token!=0&&i==0&&value==token)matches++;else foreign++; item=Pointer(item); } } if((ulong)seen.Count!=used||matches>1||Pointer(set)!=buckets||Read(set+8)!=capacity||Read(set+0x18)!=used) throw new InvalidOperationException("Original Flight token membership changed"); member|=matches==1; } var active=Read(proof.active+0x24); if(Pointer(proof.ship.node+0x90)!=proof.active||!FixtureBindingName(proof.active,"Bindings_Flight_Active")||Pointer(proof.active+0x10)!=registry+0xB8 ||!header.AsSpan().SequenceEqual(Platform.ReadBytes(entry+0x10,header.Length)) ||raw.Length!=0&&!raw.AsSpan().SequenceEqual(Platform.ReadBytes(data,raw.Length))||active!=Read(proof.active+0x24)) throw new InvalidOperationException("Original Flight layer metadata changed during verification"); return new(entry,data,count,vm,active,occupancy,member,foreign); } internal static void CheckRecoveryFlightContextFrame(RecoveryFlightContextBinding proof,bool initial,bool seed=false) { var baseline=proof.ship.baseline;var current=RecoveryPlayer(baseline.entity); if(current.life!=baseline.life||current.fsd!=baseline.fsd||current.physics!=baseline.physics||current.inputRoot!=baseline.inputRoot ||!Sample(current.physics,out var frame)||frame.entity!=baseline.entity||frame.life!=baseline.life||frame.referenceWorld!=baseline.referenceWorld ||!frame.healthValid||frame.hullFraction<.95||HealthDamageEmergency(baseline,frame)||frame.rawFsdState is not(1 or 2)||!BoundFsd(frame,frame.fsd)) throw new InvalidOperationException("Flight scope current main ship, normal space or health changed"); var ship=proof.ship;var controller=Pointer(frame.inputRoot+0x2F8);var inventory=ReadCommerceShipInventory(frame); if(!inventory.verified||inventory.count!=0||inventory.capacity!=192||LocalDockingComputer(frame)!=ship.computer||Pointer(ship.computer+0x240)!=controller ||Pointer(frame.fsd+0x6F8)!=ship.registry||Pointer(ship.computer+0x230)!=ship.registry||Pointer(ship.node)!=image+0x512E330 ||Pointer(ship.node+0x40)!=ship.registry||Pointer(ship.node+0x78)!=ship.source||Pointer(ship.computer+0x260)!=ship.source||!RecoverySubscriber(ship)) throw new InvalidOperationException("Flight scope original empty main ship input ownership changed"); if(!SampleLocalFlight(frame,out var local,out var stage))throw new InvalidOperationException("Flight scope original local telemetry unavailable: "+stage); var target=ship.target; if(!local.selected||local.target.key!=target.GetProperty("key").GetInt32()||local.target.body.ToString()!=target.GetProperty("bodyAddress").GetString() ||local.target.kind!=target.GetProperty("kind").GetUInt32()||local.target.market!=target.GetProperty("marketId").GetUInt32() ||LocalIdentityName(local.target)!=target.GetProperty("name").GetString()||local.target.market==0||local.inSupercruise ||local.nativeDockingActive||local.nativeLaunchActive||local.nativeLaunchRequested||local.cruiseAssistEnabled||local.cruiseAssistEngaged ||!double.IsFinite(local.distance)||local.distance>7500||!double.IsFinite(local.speed)||local.speed>(initial||seed?15:100)) throw new InvalidOperationException("Flight scope original target, duty, SCA or bounded geometry changed"); if(proof.reproduction) { var outward=Subtract(local.ship.position,local.destination.position);var length=Math.Sqrt(Dot(outward,outward)); if(local.distance<4500||local.distance>7200||length<=0||Dot(outward,local.ship.forward)/length<.98||!local.dockingVerified||!local.dockingGranted) throw new InvalidOperationException("Flight reproduction requires original ATC and must face away from its original station"); } else if(local.dockingState!=5||local.speed>.1)throw new InvalidOperationException("No-seed Flight admission probe must remain actually docked"); var values=new[]{Read(ship.node+0xD0),Read(ship.source+0x18),Read(frame.inputRoot+0x70),Read(controller+0x680)}; if(!proof.reproduction) {if(!FlightContextNoSeedThrottle(values))throw new InvalidOperationException("No-seed Flight scope requires zero actual/source/accumulator and zero or originally released override");} else if(values.Take(3).Any(value=>!float.IsFinite(value)||Math.Abs(value)>1.001f) ||!float.IsFinite(values[3])||Math.Abs(values[3])>1.001f&&values[3]!=-100f ||initial&&values.Take(3).Any(value=>Math.Abs(value)>.001f))throw new InvalidOperationException("Flight scope original throttle is not eligible"); } internal static bool RecoveryFlightContextTokenValid(RecoveryFlightContextBinding proof,nuint token) =>FlightContextHeap(token)&&Pointer(token)==image+0x512CF40&&Read(token+8)>0&&Read(token+8)<=32 &&Pointer(token+0x10)==proof.ship.registry-0x58&&Pointer(token+0x18)==proof.entry+0x10&&Read(token+0x20)==0; internal static float[] ReadRecoveryFlightContextThrottle(RecoveryFlightContextBinding proof) { var ship=proof.ship;var controller=Pointer(ship.baseline.inputRoot+0x2F8); if(Pointer(ship.node+0x78)!=ship.source||Pointer(ship.computer+0x240)!=controller||LocalPlayerIdentity.ReadLife(ship.baseline.inputRoot,image,imageEnd)!=ship.baseline.life) throw new InvalidOperationException("Original Flight scope throttle identity changed"); var values=new[]{Read(ship.node+0xD0),Read(ship.source+0x18),Read(ship.baseline.inputRoot+0x70),Read(controller+0x680)}; if(values.Any(value=>!float.IsFinite(value)))throw new InvalidOperationException("Original Flight scope throttle sample is non-finite");return values; } internal static void CreateRecoveryFlightContext(RecoveryFlightContextBinding proof,ref nuint owned) { if(owned!=0)throw new InvalidOperationException("Original Flight output slot is already owned"); CheckRecoveryFlightContextFrame(proof,true);var layers=ReadRecoveryFlightContextLayers(proof,0); if(!layers.Empty||layers.active!=0||layers.vm!=0)throw new InvalidOperationException("Every original Flight layer must be empty before the lowest scope is created"); if(!Platform.ReadBytes(image+0x8FA080,22).AsSpan().SequenceEqual(new byte[]{0x48,0x89,0x5C,0x24,0x10,0x48,0x89,0x6C,0x24,0x18,0x48,0x89,0x74,0x24,0x20,0x57,0x41,0x56,0x41,0x57,0x48,0x83})) throw new InvalidOperationException("Original Flight context constructor signature differs"); var name=Encoding.ASCII.GetBytes("Bindings_Flight\0"); fixed(byte* text=name) fixed(nuint* slot=&owned) { // Win64: RCX registry, RDX address of SDK-owned slot, R8 char*, // R9 priority=0. RAX is that same output-slot address. var returned=Call(image+0x8FA080,(ulong)proof.ship.registry,(ulong)(nuint)slot,(ulong)(nuint)text,0); if(returned!=(ulong)(nuint)slot)throw new InvalidOperationException("Original Flight constructor returned a different output slot"); } } internal static void ReleaseRecoveryFlightContext(RecoveryFlightContextBinding proof,ref nuint owned,Action dispatching) { if(owned==0)return; if(!RecoveryFlightContextTokenValid(proof,owned)||!ReadRecoveryFlightContextLayers(proof,owned).ownMember) throw new InvalidOperationException("Original Flight token lifetime or exact set membership differs; no stale destructor will be called"); if(!Platform.ReadBytes(image+0x77A6A0,46).AsSpan().SequenceEqual(new byte[]{0x40,0x53,0x48,0x83,0xEC,0x20,0x48,0x8B,0xD9,0x48,0x8B,0x09,0x48,0x85,0xC9,0x74,0x17,0x83,0x41,0x08,0xFF,0x75,0x0A,0x48,0x8B,0x01,0xBA,0x01,0,0,0,0xFF,0x10,0x48,0xC7,0x03,0,0,0,0,0x48,0x83,0xC4,0x20,0x5B,0xC3})) throw new InvalidOperationException("Original typed Flight release signature differs"); fixed(nuint* slot=&owned) { dispatching(); Call(image+0x77A6A0,(ulong)(nuint)slot); } if(owned!=0)throw new InvalidOperationException("Original typed Flight release did not clear its owned slot"); } } #endif