XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
#if PLUGIN_SESSION_RECOVERY
namespace XFEEDToolBox.Plugin;

internal static unsafe partial class SessionRecoveryRuntime
{
    private static bool PersistentThrottleTrialFrame(ulong frame,ulong previous,bool restored,float command,float[] values)
        =>frame>previous&&restored&&command==0&&values.Length==4&&values.Take(3).All(value=>float.IsFinite(value)&&Math.Abs(value-.75f)<=.001f)
            &&float.IsFinite(values[3])&&(Math.Abs(values[3])<=1.001f||values[3]==-100);
    private static Navigation.RecoveryThrottlePulse BeginFlightThrottleTrial(nuint node,float dt)
    {
        FlightContextWork? wanted;lock(flightContextGate)wanted=flightContext;
        if(wanted?.proof is null||!wanted.reproduction||!wanted.seedRequested||wanted.seedAttempted||wanted.proof.ship.node!=node)return default;
        wanted.seedAttempted=true;
        try
        {
            if(!enabled||GetCurrentThreadId()!=expectedThread||!float.IsFinite(dt)||dt<=0||dt>.25f||wanted.token==0||wanted.releaseRequested
                ||wanted.failed||Utc()>=wanted.deadline||Utc()>=wanted.absoluteEnd||Utc()<wanted.bridgeAt||Utc()-wanted.bridgeAt>200
                ||!Navigation.ReadRecoveryFlightContextLayers(wanted.proof,wanted.token).OnlyOwn)throw new InvalidOperationException("Original seed lease or exact layer changed");
            var pulse=Navigation.BeginRecoveryThrottleTrial(wanted.proof);wanted.seedApplied=pulse.applied;wanted.seedOutstanding=pulse.applied;wanted.seedAt=Utc();
            wanted.phase="original-absolute-command-once";return pulse;
        }
        catch(Exception error){wanted.failed=true;wanted.releaseRequested=true;wanted.issue=error.ToString();wanted.phase="seed-refused-no-replay";return default;}
    }
    private static void EndFlightThrottleTrial(in Navigation.RecoveryThrottlePulse pulse)
    {
        if(!pulse.applied)return;FlightContextWork? wanted;lock(flightContextGate)wanted=flightContext;
        if(wanted?.proof is null||wanted.proof.ship.node!=pulse.node)return;
        wanted.seedRestored=Navigation.EndRecoveryThrottleTrial(pulse);
        if(!wanted.seedRestored){wanted.failed=true;wanted.releaseRequested=true;wanted.issue="Original one-frame absolute command was not restored";}
    }
    private static void ObserveFlightThrottleTrial(nuint node)
    {
        FlightContextWork? wanted;lock(flightContextGate)wanted=flightContext;
        if(wanted?.proof is null||!wanted.reproduction||!wanted.seedApplied||wanted.registryDestroyed||wanted.proof.ship.node!=node||GetCurrentThreadId()!=expectedThread)return;
        var values=Navigation.ReadRecoveryFlightContextThrottle(wanted.proof);var command=Navigation.RecoveryThrottleTrialCommand(wanted.proof);
        if(values.Take(3).All(value=>Math.Abs(value)<=.001f))
        {wanted.seedZeroObserved=true;wanted.seedOutstanding=false;return;}
        Root rootFrame;lock(gate)rootFrame=root;
        if(rootFrame.sequence==wanted.lastTrialFrame||!wanted.seedRestored||command!=0)return;
        var previous=wanted.lastTrialFrame;wanted.lastTrialFrame=rootFrame.sequence;
        if(PersistentThrottleTrialFrame(rootFrame.sequence,previous,wanted.seedRestored,command,values))
        {
            wanted.unseededFrames++;
            if(wanted.trialSamples.Length<5)wanted.trialSamples=[..wanted.trialSamples,Obj(("frameSequence",rootFrame.sequence),("observedAtUnixMilliseconds",Utc()),
                ("accumulator",values[0]),("source",values[1]),("actualThrottle",values[2]),("controllerOverride",values[3]),("absoluteCommand",command))];
            if(wanted.unseededFrames>=3)wanted.phase="persistent075-original-graph";
        }
        else if(wanted.unseededFrames>0&&wanted.token!=0)
        {wanted.failed=true;wanted.releaseRequested=true;wanted.issue="Original graph did not retain the fixed .75 trial";}
    }
    private static void CleanupFlightThrottleTrial(FlightContextWork wanted)
    {
        if(!wanted.seedOutstanding||wanted.seedCleanupCalled||wanted.proof is null||wanted.registryDestroyed)return;
        Root context;lock(gate)context=root;
        if(context.state!="InGame"||context.next!=context.state)return;
        var due=wanted.failed||Utc()>=wanted.deadline||Utc()>=wanted.absoluteEnd
            ||wanted.releaseRequested&&(!wanted.preserveSeed||Utc()>=wanted.seedCleanupDeadline);
        if(!due)return;
        try
        {
            var layers=Navigation.ReadRecoveryFlightContextLayers(wanted.proof,wanted.token);
            if(wanted.token!=0?!layers.OnlyOwn:!layers.Empty)
            {wanted.failed=true;wanted.issue="Original UI or foreign layer owns input; owned seed cleanup will not compete";return;}
            wanted.seedCleanupCalled=Navigation.CleanupRecoveryThrottleTrial(wanted.proof);
            wanted.failed=true;wanted.issue=wanted.seedCleanupCalled?"Owned .75 trial expired or was cancelled; original cleanup dispatched":
                "Owned .75 cleanup refused changed identity or original input; no user input was overwritten";
        }
        catch(Exception error){wanted.failed=true;wanted.issue="Owned seed cleanup failed: "+error;}
    }
    internal static uint ThrottleTrialSelfTest()
    {
        if(started||ImageHash(ModulePath())==GameHash)return 0;uint mask=0;float[] actual=[.75f,.75f,.75f,-100];
        if(PersistentThrottleTrialFrame(2,1,true,0,actual))mask|=1;
        if(!PersistentThrottleTrialFrame(1,1,true,0,actual)&&!PersistentThrottleTrialFrame(0,1,true,0,actual))mask|=2;
        if(!PersistentThrottleTrialFrame(2,1,false,0,actual)&&!PersistentThrottleTrialFrame(2,1,true,.75f,actual))mask|=4;
        if(!PersistentThrottleTrialFrame(2,1,true,0,[0,.75f,.75f,-100]))mask|=8;
        if(!PersistentThrottleTrialFrame(2,1,true,0,[.75f,0,.75f,-100]))mask|=16;
        if(!PersistentThrottleTrialFrame(2,1,true,0,[.75f,.75f,0,-100]))mask|=32;
        if(!PersistentThrottleTrialFrame(2,1,true,0,[.75f,.75f,float.NaN,-100])&&!PersistentThrottleTrialFrame(2,1,true,0,[.75f,.75f,.75f,float.NaN]))mask|=64;
        if(!PersistentThrottleTrialFrame(2,1,true,0,[.75f,.75f,.75f,-1.5f])&&PersistentThrottleTrialFrame(2,1,true,0,[.75f,.75f,.75f,0]))mask|=128;
        return mask;
    }
}
internal static class FlightThrottleTrialExports
{
    [UnmanagedCallersOnly(EntryPoint="XFEFlightThrottleTrialSelfTest")]
    internal static uint Test(){try{return SessionRecoveryRuntime.ThrottleTrialSelfTest();}catch{return 0;}}
}
#endif