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
{
    static readonly object galaxyAccountMutex=new();
    static GalaxyAccountBookmarks? galaxyAccountBookmarks;
    static ulong galaxyAccountSampleAt,galaxyAccountVersion;
    static GalaxyAccountColonisation? galaxyColonisation;
    static ulong galaxyColonisationVersion;
    static GalaxyAccountSocial? galaxyAccountSocial;
    static ulong galaxySocialVersion;
    static GalaxyPowerOptions? galaxyPowerOptions;
    static ulong galaxyPowerOptionsVersion;
    static GalaxyAccountThargoid? galaxyAccountThargoid;
    static ulong galaxyThargoidVersion;
    static GalaxyAccountCommunityGoals? galaxyCommunityGoals;
    static ulong galaxyCommunityGoalsVersion;
    static GalaxyAccountFleet? galaxyAccountFleet;
    static ulong galaxyFleetVersion;
    static GalaxyAccountMissions? galaxyAccountMissions;
    static ulong galaxyMissionsVersion;
    static GalaxyCommodityOptions? galaxyCommodityOptions;
    static ulong galaxyCommodityOptionsVersion;
    static bool SameGalaxyMissions(GalaxyAccountMissionItem[] left,GalaxyAccountMissionItem[] right)
        =>left.Length==right.Length&&left.Zip(right).All(pair=>pair.First.missionId==pair.Second.missionId&&pair.First.markerType==pair.Second.markerType&&pair.First.selectedSystemAddress==pair.Second.selectedSystemAddress&&pair.First.systemAddresses.SequenceEqual(pair.Second.systemAddresses));

    static void PollGalaxyAccount(Snapshot frame)
    {
        var now=GetTickCount64();
        if(now-galaxyAccountSampleAt<2000)return;
        galaxyAccountSampleAt=now;
        if(TryReadGalaxyAccountBookmarks(frame,out var observed))lock(galaxyAccountMutex)
        {
            if(galaxyAccountBookmarks is not {} old||old.entity!=observed.entity||!old.items.SequenceEqual(observed.items))galaxyAccountVersion++;
            galaxyAccountBookmarks=observed;
        }
        if(TryReadGalaxyAccountColonisation(frame,out var claims))lock(galaxyAccountMutex)
        {
            if(galaxyColonisation is not {} old||old.entity!=claims.entity||!old.items.SequenceEqual(claims.items))galaxyColonisationVersion++;
            galaxyColonisation=claims;
        }
        if(TryReadGalaxyAccountSocial(frame,out var social))lock(galaxyAccountMutex)
        {
            if(galaxyAccountSocial is not {} old||old.entity!=social.entity||old.friendsReady!=social.friendsReady||old.wingsReady!=social.wingsReady||!old.items.SequenceEqual(social.items))galaxySocialVersion++;
            galaxyAccountSocial=social;
        }
        if(TryReadGalaxyPowerOptions(frame,out var powers))lock(galaxyAccountMutex)
        {
            if(galaxyPowerOptions is not {} old||old.entity!=powers.entity||!old.items.SequenceEqual(powers.items))galaxyPowerOptionsVersion++;
            galaxyPowerOptions=powers;
        }
        if(TryReadGalaxyAccountThargoid(frame,out var thargoid))lock(galaxyAccountMutex)
        {
            if(galaxyAccountThargoid is not {} old||old.entity!=thargoid.entity||old.metadataObserved!=thargoid.metadataObserved||old.responseObserved!=thargoid.responseObserved||
               !old.states.SequenceEqual(thargoid.states)||!old.signals.SequenceEqual(thargoid.signals)||!old.recommended.SequenceEqual(thargoid.recommended))galaxyThargoidVersion++;
            galaxyAccountThargoid=thargoid;
        }
        if(TryReadGalaxyAccountCommunityGoals(frame,out var goals))lock(galaxyAccountMutex)
        {
            if(galaxyCommunityGoals is not {} old||old.entity!=goals.entity||!old.systemAddresses.SequenceEqual(goals.systemAddresses))galaxyCommunityGoalsVersion++;
            galaxyCommunityGoals=goals;
        }
        if(TryReadGalaxyAccountFleet(frame,out var fleet))lock(galaxyAccountMutex)
        {
            if(galaxyAccountFleet is not {} old||old.entity!=fleet.entity||old.currentShipId!=fleet.currentShipId||!old.items.SequenceEqual(fleet.items))galaxyFleetVersion++;
            galaxyAccountFleet=fleet;
        }
        if(TryReadGalaxyAccountMissions(frame,out var missions))lock(galaxyAccountMutex)
        {
            if(galaxyAccountMissions is not {} old||old.entity!=missions.entity||!SameGalaxyMissions(old.items,missions.items))galaxyMissionsVersion++;
            galaxyAccountMissions=missions;
        }
        if(TryReadGalaxyCommodityOptions(frame,out var commodities))lock(galaxyAccountMutex)
        {
            if(galaxyCommodityOptions is not {} old||old.entity!=commodities.entity||old.unresolvedCount!=commodities.unresolvedCount||!old.items.SequenceEqual(commodities.items))galaxyCommodityOptionsVersion++;
            galaxyCommodityOptions=commodities;
        }
        PollGalaxyCurrentTradeFlows(frame);
    }
    static JsonObject GalaxyAccountDescription(JsonElement request)
    {
        lock(galaxyAccountMutex)
        {
            var frame=Current();var snapshot=galaxyAccountBookmarks;var now=GetTickCount64();
            var available=inGame&&frame.valid&&snapshot is not null&&snapshot.entity==frame.entity&&now>=snapshot.tick&&now-snapshot.tick<=6000;
            if(request.U64("generation") is >0 and var expected&&expected!=galaxyAccountVersion)throw new InvalidOperationException("Galaxy account snapshot changed");
            var items=new JsonArray();var rows=available?snapshot!.items:[];
            var offset=request.U64("offset");var limit=Math.Min(request.U64("limit",16),16UL);
            var wireBytes=0;
            for(var i=offset;i<(ulong)rows.Length&&i-offset<limit;i++)
            {
                var b=rows[(int)i];var item=Obj(("uid",b.uid.ToString()),("systemAddress",b.systemAddress.ToString()),
                    ("bodyAddress",b.bodyAddress.ToString()),("marketId",b.marketId.ToString()),("name",b.name),("systemName",b.systemName),
                    ("bodyName",b.bodyName),("stationName",b.stationName),("icon",b.icon),("bodyType",b.bodyType),
                    ("cluster",b.cluster),("rings",b.rings),("favourite",b.favourite),("sourceKind",b.sourceKind));
                var bytes=System.Text.Encoding.UTF8.GetByteCount(item.ToJsonString());
                if(i>offset&&wireBytes+bytes>48000)break;
                wireBytes+=bytes;items.Add((JsonNode)item);
            }
            return Obj(("available",available),("generation",galaxyAccountVersion),("total",rows.Length),("items",items),
                ("complete",false),("source","original-bookmark-manager"),("entityId",available?snapshot!.entity.ToString():""),
                ("ageMilliseconds",available?now-snapshot!.tick:0));
        }
    }
    static JsonObject GalaxyColonisationDescription(JsonElement request)
    {
        lock(galaxyAccountMutex)
        {
            var frame=Current();var observed=galaxyColonisation;var now=GetTickCount64();
            var available=inGame&&frame.valid&&observed is not null&&observed.entity==frame.entity&&now>=observed.tick&&now-observed.tick<=6000;
            if(request.U64("generation") is >0 and var expected&&expected!=galaxyColonisationVersion)throw new InvalidOperationException("Galaxy colonisation snapshot changed");
            var rows=available?observed!.items:[];var items=new JsonArray();var offset=request.U64("offset");var limit=Math.Min(request.U64("limit",96),96UL);
            for(var i=offset;i<(ulong)rows.Length&&i-offset<limit;i++)
            {
                var c=rows[(int)i];items.Add((JsonNode)Obj(("systemAddress",c.systemAddress.ToString()),("commanderId",c.commanderId.ToString()),
                    ("status",c.status),("active",c.active),("statusUpdated",c.statusUpdated.ToString())));
            }
            return Obj(("available",available),("generation",galaxyColonisationVersion),("total",rows.Length),("items",items),("complete",false),
                ("source","original-colonisation-data-cache"),("entityId",available?observed!.entity.ToString():""));
        }
    }
    static JsonObject GalaxySocialDescription(JsonElement request)
    {
        lock(galaxyAccountMutex)
        {
            var frame=Current();var observed=galaxyAccountSocial;var now=GetTickCount64();
            var available=inGame&&frame.valid&&observed is not null&&observed.entity==frame.entity&&now>=observed.tick&&now-observed.tick<=6000;
            if(request.U64("generation") is >0 and var expected&&expected!=galaxySocialVersion)throw new InvalidOperationException("Galaxy social snapshot changed");
            var rows=available?observed!.items:[];var items=new JsonArray();var offset=request.U64("offset");var limit=Math.Min(request.U64("limit",96),96UL);
            for(var i=offset;i<(ulong)rows.Length&&i-offset<limit;i++)
            {
                var s=rows[(int)i];items.Add((JsonNode)Obj(("systemAddress",s.systemAddress.ToString()),("kind",s.kind),
                    ("status",s.status),("statusName",s.statusName),("locationCurrent",s.locationCurrent)));
            }
            return Obj(("available",available),("generation",galaxySocialVersion),("total",rows.Length),("items",items),("complete",false),
                ("source","original-friends-and-wing-components"),("entityId",available?observed!.entity.ToString():""),
                ("friendsReady",available&&observed!.friendsReady),("wingsReady",available&&observed!.wingsReady));
        }
    }
    static JsonObject GalaxyCommunityGoalsDescription(JsonElement request)
    {
        lock(galaxyAccountMutex)
        {
            var frame=Current();var observed=galaxyCommunityGoals;var now=GetTickCount64();
            var available=inGame&&frame.valid&&observed is not null&&observed.entity==frame.entity&&now>=observed.tick&&now-observed.tick<=6000;
            if(request.U64("generation") is >0 and var expected&&expected!=galaxyCommunityGoalsVersion)throw new InvalidOperationException("Galaxy community goals changed");
            var rows=available?observed!.systemAddresses:[];var items=new JsonArray();var offset=request.U64("offset");var limit=Math.Min(request.U64("limit",96),96UL);
            for(var i=offset;i<(ulong)rows.Length&&i-offset<limit;i++)items.Add((JsonNode)JsonValue.Create(rows[(int)i].ToString())!);
            return Obj(("available",available),("generation",galaxyCommunityGoalsVersion),("total",rows.Length),("items",items),("complete",false),
                ("source","original-community-goal-map-published-cache"),("entityId",available?observed!.entity.ToString():""));
        }
    }
    static JsonObject GalaxyFleetDescription(JsonElement request)
    {
        lock(galaxyAccountMutex)
        {
            var frame=Current();var observed=galaxyAccountFleet;var now=GetTickCount64();
            var available=inGame&&frame.valid&&observed is not null&&observed.entity==frame.entity&&now>=observed.tick&&now-observed.tick<=6000;
            if(request.U64("generation") is >0 and var expected&&expected!=galaxyFleetVersion)throw new InvalidOperationException("Galaxy fleet snapshot changed");
            var rows=available?observed!.items:[];var items=new JsonArray();var offset=request.U64("offset");var limit=Math.Min(request.U64("limit",96),96UL);
            for(var i=offset;i<(ulong)rows.Length&&i-offset<limit;i++)
            {
                var ship=rows[(int)i];items.Add((JsonNode)Obj(("id",ship.id),("shipId",ship.shipId),("systemAddress",ship.systemAddress.ToString())));
            }
            return Obj(("available",available),("generation",galaxyFleetVersion),("total",rows.Length),("items",items),("complete",false),
                ("currentShipId",available?observed!.currentShipId:null),("source","original-stored-fleet-component"),("entityId",available?observed!.entity.ToString():""));
        }
    }
    static JsonObject GalaxyMissionsDescription(JsonElement request)
    {
        lock(galaxyAccountMutex)
        {
            var frame=Current();var observed=galaxyAccountMissions;var now=GetTickCount64();
            var available=inGame&&frame.valid&&observed is not null&&observed.entity==frame.entity&&now>=observed.tick&&now-observed.tick<=6000;
            if(request.U64("generation") is >0 and var expected&&expected!=galaxyMissionsVersion)throw new InvalidOperationException("Galaxy mission snapshot changed");
            var rows=available?observed!.items:[];var items=new JsonArray();var offset=request.U64("offset");var limit=Math.Min(request.U64("limit",16),16UL);var wireBytes=0;
            for(var i=offset;i<(ulong)rows.Length&&i-offset<limit;i++)
            {
                var mission=rows[(int)i];var addresses=new JsonArray();foreach(var address in mission.systemAddresses)addresses.Add((JsonNode)JsonValue.Create(address.ToString())!);
                var item=Obj(("missionId",mission.missionId.ToString()),("systemAddresses",addresses),("selectedSystemAddress",mission.selectedSystemAddress?.ToString()),("markerType",mission.markerType));
                var bytes=System.Text.Encoding.UTF8.GetByteCount(item.ToJsonString());
                if(bytes>48000)throw new InvalidOperationException("Original mission address set exceeds the wire bound; not truncated");
                if(i>offset&&wireBytes+bytes>48000)break;wireBytes+=bytes;items.Add((JsonNode)item);
            }
            return Obj(("available",available),("generation",galaxyMissionsVersion),("total",rows.Length),("items",items),("complete",false),
                ("source","original-external-panel-mission-list"),("entityId",available?observed!.entity.ToString():""));
        }
    }
    static JsonObject GalaxyCommodityOptionsDescription(JsonElement request)
    {
        lock(galaxyAccountMutex)
        {
            var frame=Current();var observed=galaxyCommodityOptions;var now=GetTickCount64();
            var available=inGame&&frame.valid&&observed is not null&&observed.entity==frame.entity&&now>=observed.tick&&now-observed.tick<=6000;
            if(request.U64("generation") is >0 and var expected&&expected!=galaxyCommodityOptionsVersion)throw new InvalidOperationException("Galaxy commodity options changed");
            var rows=available?observed!.items:[];var items=new JsonArray();var offset=request.U64("offset");var limit=Math.Min(request.U64("limit",16),16UL);var wireBytes=0;
            for(var i=offset;i<(ulong)rows.Length&&i-offset<limit;i++)
            {
                var option=rows[(int)i];var item=Obj(("uid",option.uid),("name",option.name),("category",option.category),("kind",option.kind),("publisherEligible",option.publisherEligible));
                var bytes=System.Text.Encoding.UTF8.GetByteCount(item.ToJsonString());
                if(bytes>48000)throw new InvalidOperationException("Original commodity definition exceeds the wire bound");
                if(items.Count>0&&wireBytes+bytes>48000)break;wireBytes+=bytes;items.Add((JsonNode)item);
            }
            return Obj(("available",available),("generation",galaxyCommodityOptionsVersion),("total",rows.Length),("items",items),("complete",false),
                ("nextOffset",offset+(ulong)items.Count<(ulong)rows.Length?JsonValue.Create(offset+(ulong)items.Count):null),
                ("unresolvedCount",available?observed!.unresolvedCount:0),("source","original-commodity-enumeration-and-definitions"),("entityId",available?observed!.entity.ToString():""));
        }
    }
    static JsonObject GalaxyThargoidDescription(JsonElement request)
    {
        lock(galaxyAccountMutex)
        {
            var frame=Current();var observed=galaxyAccountThargoid;var now=GetTickCount64();
            var available=inGame&&frame.valid&&observed is not null&&observed.entity==frame.entity&&now>=observed.tick&&now-observed.tick<=6000;
            if(request.U64("generation") is >0 and var expected&&expected!=galaxyThargoidVersion)throw new InvalidOperationException("Galaxy Thargoid snapshot changed");
            var states=new JsonArray();var rows=new List<JsonObject>();
            if(available)
            {
                foreach(var s in observed!.states)states.Add((JsonNode)Obj(("state",s.state),("count",s.count)));
                foreach(var s in observed.signals)rows.Add(Obj(("kind","signal"),("id",s.id),("systemAddress",s.systemAddress.ToString()),("stronghold",s.stronghold),("affectedSystemsCount",s.affectedSystemsCount)));
                foreach(var r in observed.recommended)rows.Add(Obj(("kind","recommended"),("id",r.id),("state",r.state),("rank",r.rank),("systemAddress",r.systemAddress.ToString()),("progress",r.progress)));
            }
            var items=new JsonArray();var offset=request.U64("offset");var limit=Math.Min(request.U64("limit",96),96UL);
            for(var i=offset;i<(ulong)rows.Count&&i-offset<limit;i++)items.Add((JsonNode)rows[(int)i]);
            return Obj(("available",available),("generation",galaxyThargoidVersion),("total",rows.Count),("items",items),("states",states),("complete",false),
                ("metadataObserved",available&&observed!.metadataObserved),("responseObserved",available&&observed!.responseObserved),
                ("source","original-thargoid-panel-published-cache"),("entityId",available?observed!.entity.ToString():""));
        }
    }
    static JsonObject GalaxyPowerOptionsDescription(JsonElement request)
    {
        lock(galaxyAccountMutex)
        {
            var frame=Current();var observed=galaxyPowerOptions;var now=GetTickCount64();
            var available=inGame&&frame.valid&&observed is not null&&observed.entity==frame.entity&&now>=observed.tick&&now-observed.tick<=6000;
            if(request.U64("generation") is >0 and var expected&&expected!=galaxyPowerOptionsVersion)throw new InvalidOperationException("Galaxy power options changed");
            var rows=available?observed!.items:[];var items=new JsonArray();var offset=request.U64("offset");var limit=Math.Min(request.U64("limit",16),16UL);var wireBytes=0;
            for(var i=offset;i<(ulong)rows.Length&&i-offset<limit;i++)
            {
                var p=rows[(int)i];var item=Obj(("index",p.index),("uiPowerId",p.uiPowerId),("internalPowerId",p.internalPowerId),("name",p.name),
                    ("rawRed",p.rawRed),("rawGreen",p.rawGreen),("rawBlue",p.rawBlue),("headquartersSystemAddress",p.headquartersSystemAddress?.ToString()));
                var bytes=System.Text.Encoding.UTF8.GetByteCount(item.ToJsonString());if(i>offset&&wireBytes+bytes>48000)break;
                wireBytes+=bytes;items.Add((JsonNode)item);
            }
            return Obj(("available",available),("generation",galaxyPowerOptionsVersion),("total",rows.Length),("items",items),("optionsComplete",available),
                ("source","original-power-definition-table"),("entityId",available?observed!.entity.ToString():""));
        }
    }
}