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-offsetoffset&&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=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=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=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=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-offset48000)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-offset48000)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(); 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=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-offsetoffset&&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():"")); } } }