using System.Numerics;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
internal sealed record GalaxyPowerStatusFact(int rawState,int stateIndex,int? ownerIndex,int? ownerInternalPowerId,
uint unoccupiedPowerFlags,uint contestPowerFlags,bool? generalAdmissible);
// Original3B34860 and3B27100 copy these metadata fields. No original
// lookup/update function is invoked, and unoccupied systems get no bubble.
static bool CopyGalaxyPowerStatus(GalaxyPowerTerritoryContext context,ulong address,out GalaxyPowerStatusFact? result)
{
result=null;var source=context.activity;
if(address==0||address>=0x80000000000000UL||context.options.Length is <1 or >128||context.options.Length!=source.optionCount)return false;
try
{
if(!GalaxyPowerActivitySource(source.service,source.expectedImage,source.owner)||
!GalaxyPowerActivityFind(source.service+0x3C0,address,-1,0x180,16,out var node))return false;
if(node==0)return true;
var metadata=node+0x10;var state=Read<int>(metadata+0xC);var rawOwner=Read<ulong>(metadata);
var unoccupiedFlags=Read<uint>(metadata+0x28);var contestFlags=Read<uint>(metadata+0x4C);
if(state<0||state>65535||state!=0&&rawOwner>=(ulong)context.options.Length)return false;
var special=state is 3 or 4?Read<byte>(metadata+0x11):(byte)0;if(special>1)return false;
var index=state switch{0=>BitOperations.PopCount(contestFlags)>1?3:contestFlags!=0?2:unoccupiedFlags!=0?1:0,1=>4,2=>5,3 or 4=>special!=0?7:6,_=>0};
int? ownerIndex=null,actualId=null;
if(state!=0)
{var option=context.options[(int)rawOwner];if(option.index!=(int)rawOwner||option.internalPowerId<0)return false;ownerIndex=(int)rawOwner;actualId=option.internalPowerId;}
if(!GalaxyPowerTerritoryAdmissible(context.metadata,source.expectedImage,address,out var allowed))return false;
if(!GalaxyPowerActivitySource(source.service,source.expectedImage,source.owner)||Read<ulong>(node)!=address||Read<ulong>(metadata)!=rawOwner||
Read<int>(metadata+0xC)!=state||Read<uint>(metadata+0x28)!=unoccupiedFlags||Read<uint>(metadata+0x4C)!=contestFlags||
state is 3 or 4&&Read<byte>(metadata+0x11)!=special)return false;
result=new(state,index,ownerIndex,actualId,unoccupiedFlags,contestFlags,allowed);return true;
}
catch{return false;}
}
static bool TryReadGalaxyPowerStatus(GalaxyPowerTerritoryContext context,ulong address,out GalaxyPowerStatusFact? result)
{
result=null;var source=context.activity;
if(!Enabled()||!inGame||source.thread==0||source.thread!=owningThread||GetCurrentThreadId()!=source.thread||
GetTickCount64()-source.tick>500||source.expectedImage!=image)return false;
var frame=Current();
if(!frame.valid||frame.entity!=source.entity||(frame.referenceWorld&0x007FFFFFFFFFFFFFUL)!=source.currentSystem)return false;
return CopyGalaxyPowerStatus(context,address,out result);
}
}
using System.Numerics;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
internal sealed record GalaxyPowerStatusFact(int rawState,int stateIndex,int? ownerIndex,int? ownerInternalPowerId,
uint unoccupiedPowerFlags,uint contestPowerFlags,bool? generalAdmissible);
// Original3B34860 and3B27100 copy these metadata fields. No original
// lookup/update function is invoked, and unoccupied systems get no bubble.
static bool CopyGalaxyPowerStatus(GalaxyPowerTerritoryContext context,ulong address,out GalaxyPowerStatusFact? result)
{
result=null;var source=context.activity;
if(address==0||address>=0x80000000000000UL||context.options.Length is <1 or >128||context.options.Length!=source.optionCount)return false;
try
{
if(!GalaxyPowerActivitySource(source.service,source.expectedImage,source.owner)||
!GalaxyPowerActivityFind(source.service+0x3C0,address,-1,0x180,16,out var node))return false;
if(node==0)return true;
var metadata=node+0x10;var state=Read<int>(metadata+0xC);var rawOwner=Read<ulong>(metadata);
var unoccupiedFlags=Read<uint>(metadata+0x28);var contestFlags=Read<uint>(metadata+0x4C);
if(state<0||state>65535||state!=0&&rawOwner>=(ulong)context.options.Length)return false;
var special=state is 3 or 4?Read<byte>(metadata+0x11):(byte)0;if(special>1)return false;
var index=state switch{0=>BitOperations.PopCount(contestFlags)>1?3:contestFlags!=0?2:unoccupiedFlags!=0?1:0,1=>4,2=>5,3 or 4=>special!=0?7:6,_=>0};
int? ownerIndex=null,actualId=null;
if(state!=0)
{var option=context.options[(int)rawOwner];if(option.index!=(int)rawOwner||option.internalPowerId<0)return false;ownerIndex=(int)rawOwner;actualId=option.internalPowerId;}
if(!GalaxyPowerTerritoryAdmissible(context.metadata,source.expectedImage,address,out var allowed))return false;
if(!GalaxyPowerActivitySource(source.service,source.expectedImage,source.owner)||Read<ulong>(node)!=address||Read<ulong>(metadata)!=rawOwner||
Read<int>(metadata+0xC)!=state||Read<uint>(metadata+0x28)!=unoccupiedFlags||Read<uint>(metadata+0x4C)!=contestFlags||
state is 3 or 4&&Read<byte>(metadata+0x11)!=special)return false;
result=new(state,index,ownerIndex,actualId,unoccupiedFlags,contestFlags,allowed);return true;
}
catch{return false;}
}
static bool TryReadGalaxyPowerStatus(GalaxyPowerTerritoryContext context,ulong address,out GalaxyPowerStatusFact? result)
{
result=null;var source=context.activity;
if(!Enabled()||!inGame||source.thread==0||source.thread!=owningThread||GetCurrentThreadId()!=source.thread||
GetTickCount64()-source.tick>500||source.expectedImage!=image)return false;
var frame=Current();
if(!frame.valid||frame.entity!=source.entity||(frame.referenceWorld&0x007FFFFFFFFFFFFFUL)!=source.currentSystem)return false;
return CopyGalaxyPowerStatus(context,address,out result);
}
}