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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

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

internal static unsafe partial class Navigation
{
    private sealed class NormalBoostPulse
    {
        internal nuint binding,registry;internal ulong until,entity;internal bool pressed;
    }
    private static readonly object normalBoostLock=new();
    private static NormalBoostPulse? normalBoostPulse;
    private static ulong normalBoostInputs,normalBoostReleased,lastNormalBoostAt;
    private static bool NormalBoostAllowed(bool normal,bool healthy,bool takeover,bool duty,bool gear,bool scoop,double distance,double angle)
        =>normal&&healthy&&!takeover&&!duty&&!gear&&!scoop&&double.IsFinite(distance)&&distance>7500&&double.IsFinite(angle)&&angle is >=0 and <=5;
    private static bool TryNormalBoostBinding(in Snapshot frame,out nuint registry,out nuint binding)
    {
        registry=binding=0;
        var computer=LocalDockingComputer(frame);if(computer==0)return false;
        registry=Pointer(computer+0x230);
        return registry!=0&&registry==Pointer(frame.fsd+0x6F8)&&TryFindExistingPhysicalBinding(registry,"UseBoostJuice",out binding)
            &&Read<uint>(binding+0x1C)<1024;
    }
    private static bool PressNormalBoostCount(nuint binding)
    {
        if(Read<uint>(binding+0x1C)!=0)return false;
        Write(binding+0x24,(byte)(Read<byte>(binding+0x24)|1));Write(binding+0x1C,1u);return true;
    }
    private static void ReleaseNormalBoostCount(nuint binding)
    {
        var count=Read<uint>(binding+0x1C);
        if(count is >0 and <1024){Write(binding+0x1C,count-1);Write(binding+0x24,(byte)(Read<byte>(binding+0x24)|2));}
    }
    private static void EndNormalBoostPulse()
    {
        NormalBoostPulse? pulse;lock(normalBoostLock){pulse=normalBoostPulse;normalBoostPulse=null;}
        if(pulse is null)return;
        try
        {
            // Release our one contribution to the original binding's pressed count.
            // The held original reference keeps the binding alive across a scene transition.
            if(pulse.pressed&&FixtureBindingName(pulse.binding,"UseBoostJuice"))
            {
                ReleaseNormalBoostCount(pulse.binding);
            }
        }
        finally{var owned=pulse.binding;Call(image+0x204010,(ulong)(nuint)(&owned));normalBoostReleased++;}
    }
    private static void PollNormalBoostPulse(in Snapshot frame)
    {
        NormalBoostPulse? pulse;lock(normalBoostLock)pulse=normalBoostPulse;
        if(pulse is null)return;
        if(GetTickCount64()>=pulse.until||frame.entity!=pulse.entity||!frame.valid||!inGame||overridden||frame.healthEmergency
            ||SampleLocalFlight(frame,out var local,out _)&&local.distance<=7500)EndNormalBoostPulse();
    }
    private static bool RequestNormalBoost(in Snapshot frame)
    {
        var now=GetTickCount64();PollNormalBoostPulse(frame);
        lock(normalBoostLock)if(normalBoostPulse is not null)return true;
        if(lastNormalBoostAt!=0&&now-lastNormalBoostAt<5000)return true;
        if(!SampleLocalFlight(frame,out var local,out _)||!local.selected||!LocalDockingTargetKind(local.target.kind))return false;
        var aim=Subtract(local.destination.position,local.ship.position);var length=Math.Sqrt(Dot(aim,aim));
        var forwardLength=Math.Sqrt(Dot(local.ship.forward,local.ship.forward));
        var angle=length>0&&forwardLength>.9?Math.Acos(Math.Clamp(Dot(aim,local.ship.forward)/(length*forwardLength),-1,1))*180/Math.PI:double.NaN;
        var input=frame.inputRoot+0x60;
        if(!NormalBoostAllowed(!local.inSupercruise&&LocalSelectionFsdReady(frame.rawFsdState,fsdOwned),frame.healthValid&&frame.hullFraction>0&&!frame.healthEmergency,
            overridden,local.nativeDockingActive||local.nativeLaunchActive||local.dockingGranted,Read<byte>(input+0xA9)!=0,Read<byte>(input+0xBB)!=0,local.distance,angle)
            ||Pointer(input)!=image+0x53143F8||!TryNormalBoostBinding(frame,out var registry,out var existing))return false;
        RequirePhysicalInputSources(frame,now);
        if(Read<uint>(existing+0x1C)>0)return true;
        var scratch=stackalloc byte[47];var temporary=AlignServiceScratch(scratch);new Span<byte>(temporary,32).Clear();
        try
        {
            // The original input lookup retains this existing UseBoostJuice binding.
            // Original 2DF73E0 retains its gear/scoop/context gates and publishes the normal boost request.
            Call(image+0x8FEDC0,registry,(ulong)(nuint)temporary,image+0x532FDC8);
            var held=Pointer((nuint)temporary);
            if(held!=existing||!FixtureBindingName(held,"UseBoostJuice")||Read<uint>(held+0x1C)!=0)return false;
            var pulse=new NormalBoostPulse{binding=held,registry=registry,entity=frame.entity,until=now+100,pressed=true};
            if(!PressNormalBoostCount(held))return false;
            Write((nuint)temporary,(nuint)0);lock(normalBoostLock)normalBoostPulse=pulse;
            lastNormalBoostAt=now;normalBoostInputs++;return true;
        }
        finally{Call(image+0x204010,(ulong)(nuint)temporary);}
    }
    private static bool NormalBoostPolicySelfTest()
    {
        if(!NormalBoostAllowed(true,true,false,false,false,false,8000,0))return false;
        foreach(var distance in new[]{7500d,5000d,2000d,double.NaN})if(NormalBoostAllowed(true,true,false,false,false,false,distance,0))return false;
        return !NormalBoostAllowed(false,true,false,false,false,false,8000,0)
            &&!NormalBoostAllowed(true,true,true,false,false,false,8000,0)&&!NormalBoostAllowed(true,true,false,true,false,false,8000,0)
            &&!NormalBoostAllowed(true,true,false,false,true,false,8000,0)&&!NormalBoostAllowed(true,true,false,false,false,true,8000,0)
            &&!NormalBoostAllowed(true,true,false,false,false,false,8000,6)&&!NormalBoostAllowed(true,false,false,false,false,false,8000,0);
    }
    private static uint NormalBoostSelfTest()
    {
        uint passed=NormalBoostPolicySelfTest()?1u:0;
        byte* storage=stackalloc byte[48];new Span<byte>(storage,48).Clear();var binding=(nuint)storage;
        if(PressNormalBoostCount(binding)&&Read<uint>(binding+0x1C)==1&&(Read<byte>(binding+0x24)&1)!=0&&!PressNormalBoostCount(binding))
        {
            ReleaseNormalBoostCount(binding);
            if(Read<uint>(binding+0x1C)==0&&(Read<byte>(binding+0x24)&2)!=0)passed|=2;
        }
        Write(binding+0x1C,2u);ReleaseNormalBoostCount(binding);
        var retainedPhysical=Read<uint>(binding+0x1C)==1;
        Write(binding+0x1C,0u);ReleaseNormalBoostCount(binding);
        Write(binding+0x1C,uint.MaxValue);ReleaseNormalBoostCount(binding);
        if(retainedPhysical&&Read<uint>(binding+0x1C)==uint.MaxValue&&!PressNormalBoostCount(binding))passed|=4;
        return passed;
    }
#if PLUGIN_NAVIGATION
    [System.Runtime.InteropServices.UnmanagedCallersOnly(EntryPoint="XFENavigationNormalBoostSelfTest")]
    internal static uint ExportNormalBoostSelfTest()=>RunTest(NormalBoostSelfTest);
#endif
}