namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
private const nuint DockingUpdate=0xE9A160;
private static nuint originalDockingUpdate;
private struct DockingZeroUpdate
{
public LocalActionRequest command;
public nuint computer,controller,fsd;
public uint market;
public float throttle;
}
private static DockingZeroUpdate dockingZeroUpdate;
private static ulong dockingZeroUpdateCalls,dockingZeroUpdateMatched,dockingZeroUpdateApplied;
private static bool DockingZeroUpdateMatches(in DockingZeroUpdate wanted,in Snapshot frame,in LocalSelection selection,
nuint computer,ulong now,ulong epoch,bool ownerActive,bool enabled,bool active,bool playerOverride)
=>enabled&&active&&ownerActive&&!playerOverride&&frame.valid&&frame.healthValid&&frame.hullFraction>0&&!frame.healthEmergency&&(frame.rawFsdState is 1 or 2)
&&now>=frame.tick&&now-frame.tick<=200&&wanted.command.expires>now
&&wanted.computer==computer&&computer!=0&&wanted.command.entity==frame.entity&&wanted.command.life==frame.life
&&wanted.command.inputRoot==frame.inputRoot&&wanted.command.world==frame.referenceWorld&&wanted.fsd==frame.fsd
&&wanted.command.sequence>0&&wanted.command.sequence<=frame.sequence
&&wanted.command.epoch==epoch&&selection.epoch==epoch&&wanted.command.selection==selection.generation
&&selection.entity==frame.entity&&selection.life==frame.life&&selection.inputRoot==frame.inputRoot&&selection.fsd==frame.fsd
&&wanted.market!=0&&selection.target.market==wanted.market;
// selection.expires is the selection acknowledgement's short TTL. Its
// identity and generation remain bound throughout the flight; the fresh
// action's own expires controls this input request's lifetime.
// The normal input refresh can replace a zero written at the end of FSD
// processing. Deliver the already authorised real throttle input at the
// original docking computer's consumption point, before its own update.
// No AI state, permission, reported telemetry, steering or translation is
// written here. The request expires with its original control frame.
private static void ApplyDockingZeroBeforeUpdate(nuint obj)
{
dockingZeroUpdateCalls++;
if(GetCurrentThreadId()!=owningThread)return;
if(Volatile.Read(ref dockingZeroUpdate.command.expires)<=GetTickCount64())return;
var frame=Current();DockingZeroUpdate wanted;LocalSelection selection;LocalFlightObservation observed;
lock(localLock){wanted=dockingZeroUpdate;selection=localSelection;observed=localObservation;}
if(!DockingZeroUpdateMatches(wanted,frame,selection,obj+0x58,GetTickCount64(),controllerEpoch,
clientOwner.Length!=0,Enabled(),inGame,overridden)||localConfirmed!=selection.generation||fsdOwned||!BoundFsd(frame,frame.fsd))return;
var computer=LocalDockingComputer(frame);if(computer!=wanted.computer||computer!=obj+0x58||!LocalDockingAvailable(computer))return;
dockingZeroUpdateMatched++;
if((byte)Call(image+0xED6620,computer)!=0)
{lock(localLock)dockingZeroUpdate.command.expires=0;return;}
var now=GetTickCount64();var receipt=dockingZeroInputReceipt;
if(!observed.valid||observed.entity!=frame.entity||observed.inSupercruise||observed.distance>7500
||now<observed.tick||now-observed.tick>200||!SameLocal(observed.target,selection.target))return;
if(receipt.entity!=frame.entity||receipt.life!=frame.life||receipt.market!=wanted.market||receipt.inputService!=Pointer(computer+0x230)
||now<receipt.tick||now-receipt.tick>=30000||Pointer(computer+0x240)!=wanted.controller)return;
var dock=LocalDocking(frame);if(dock==0||Read<uint>(dock+0x614)!=2||Read<uint>(dock+0x4C0)is not(3 or 4)||Read<byte>(dock+0x4E9)==255)return;
var station=Pointer(dock+0x4E0);if(station==0||Read<uint>(station+0xC0)!=8||Read<ulong>(station+0xB0)!=wanted.market)return;
if(!LocalRecordMatches(Pointer(frame.fsd+0x338),selection.target))return;
bool released;lock(leaseLock)released=lease.expires<=now&&fsdLease.expires<=now;
if(CommitOriginalDockingZeroAxis(frame.inputRoot,wanted.controller,wanted.throttle,true,released,out _))dockingZeroUpdateApplied++;
}
[UnmanagedCallersOnly]
private static ulong DockingUpdateHook(nuint obj,float dt)
{
try{ApplyDockingZeroBeforeUpdate(obj);}catch{}
try{return Call(originalDockingUpdate,(ulong)obj,F32(dt));}catch{return 0;}
}
}
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
private const nuint DockingUpdate=0xE9A160;
private static nuint originalDockingUpdate;
private struct DockingZeroUpdate
{
public LocalActionRequest command;
public nuint computer,controller,fsd;
public uint market;
public float throttle;
}
private static DockingZeroUpdate dockingZeroUpdate;
private static ulong dockingZeroUpdateCalls,dockingZeroUpdateMatched,dockingZeroUpdateApplied;
private static bool DockingZeroUpdateMatches(in DockingZeroUpdate wanted,in Snapshot frame,in LocalSelection selection,
nuint computer,ulong now,ulong epoch,bool ownerActive,bool enabled,bool active,bool playerOverride)
=>enabled&&active&&ownerActive&&!playerOverride&&frame.valid&&frame.healthValid&&frame.hullFraction>0&&!frame.healthEmergency&&(frame.rawFsdState is 1 or 2)
&&now>=frame.tick&&now-frame.tick<=200&&wanted.command.expires>now
&&wanted.computer==computer&&computer!=0&&wanted.command.entity==frame.entity&&wanted.command.life==frame.life
&&wanted.command.inputRoot==frame.inputRoot&&wanted.command.world==frame.referenceWorld&&wanted.fsd==frame.fsd
&&wanted.command.sequence>0&&wanted.command.sequence<=frame.sequence
&&wanted.command.epoch==epoch&&selection.epoch==epoch&&wanted.command.selection==selection.generation
&&selection.entity==frame.entity&&selection.life==frame.life&&selection.inputRoot==frame.inputRoot&&selection.fsd==frame.fsd
&&wanted.market!=0&&selection.target.market==wanted.market;
// selection.expires is the selection acknowledgement's short TTL. Its
// identity and generation remain bound throughout the flight; the fresh
// action's own expires controls this input request's lifetime.
// The normal input refresh can replace a zero written at the end of FSD
// processing. Deliver the already authorised real throttle input at the
// original docking computer's consumption point, before its own update.
// No AI state, permission, reported telemetry, steering or translation is
// written here. The request expires with its original control frame.
private static void ApplyDockingZeroBeforeUpdate(nuint obj)
{
dockingZeroUpdateCalls++;
if(GetCurrentThreadId()!=owningThread)return;
if(Volatile.Read(ref dockingZeroUpdate.command.expires)<=GetTickCount64())return;
var frame=Current();DockingZeroUpdate wanted;LocalSelection selection;LocalFlightObservation observed;
lock(localLock){wanted=dockingZeroUpdate;selection=localSelection;observed=localObservation;}
if(!DockingZeroUpdateMatches(wanted,frame,selection,obj+0x58,GetTickCount64(),controllerEpoch,
clientOwner.Length!=0,Enabled(),inGame,overridden)||localConfirmed!=selection.generation||fsdOwned||!BoundFsd(frame,frame.fsd))return;
var computer=LocalDockingComputer(frame);if(computer!=wanted.computer||computer!=obj+0x58||!LocalDockingAvailable(computer))return;
dockingZeroUpdateMatched++;
if((byte)Call(image+0xED6620,computer)!=0)
{lock(localLock)dockingZeroUpdate.command.expires=0;return;}
var now=GetTickCount64();var receipt=dockingZeroInputReceipt;
if(!observed.valid||observed.entity!=frame.entity||observed.inSupercruise||observed.distance>7500
||now<observed.tick||now-observed.tick>200||!SameLocal(observed.target,selection.target))return;
if(receipt.entity!=frame.entity||receipt.life!=frame.life||receipt.market!=wanted.market||receipt.inputService!=Pointer(computer+0x230)
||now<receipt.tick||now-receipt.tick>=30000||Pointer(computer+0x240)!=wanted.controller)return;
var dock=LocalDocking(frame);if(dock==0||Read<uint>(dock+0x614)!=2||Read<uint>(dock+0x4C0)is not(3 or 4)||Read<byte>(dock+0x4E9)==255)return;
var station=Pointer(dock+0x4E0);if(station==0||Read<uint>(station+0xC0)!=8||Read<ulong>(station+0xB0)!=wanted.market)return;
if(!LocalRecordMatches(Pointer(frame.fsd+0x338),selection.target))return;
bool released;lock(leaseLock)released=lease.expires<=now&&fsdLease.expires<=now;
if(CommitOriginalDockingZeroAxis(frame.inputRoot,wanted.controller,wanted.throttle,true,released,out _))dockingZeroUpdateApplied++;
}
[UnmanagedCallersOnly]
private static ulong DockingUpdateHook(nuint obj,float dt)
{
try{ApplyDockingZeroBeforeUpdate(obj);}catch{}
try{return Call(originalDockingUpdate,(ulong)obj,F32(dt));}catch{return 0;}
}
}