using System.Text.Json;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
private static bool CommerceProfileValues(JsonElement response,uint capacity,out ulong credits,out int count)
{
credits=0;count=0;
if(capacity>int.MaxValue||response.ValueKind!=JsonValueKind.Object||response.TryGetProperty("error",out _)
||!response.TryGetProperty("credits",out var balance)||balance.ValueKind!=JsonValueKind.Number||!balance.TryGetUInt64(out credits)||credits>=100000000000000UL
||!response.TryGetProperty("inventory",out var inventory)||inventory.ValueKind!=JsonValueKind.Array)return false;
foreach(var row in inventory.EnumerateArray())
{
if(row.ValueKind!=JsonValueKind.Object||!row.TryGetProperty("commodity",out var name)||name.ValueKind!=JsonValueKind.String||string.IsNullOrWhiteSpace(name.GetString())
||!row.TryGetProperty("qty",out var quantity)||quantity.ValueKind!=JsonValueKind.Number||!quantity.TryGetInt32(out var amount)||amount<0||amount>capacity-count)return false;
count+=amount;
}
return true;
}
private static bool ApplyCommerceProfile(in Snapshot frame,ref CommerceReadRequest work,byte* body,nuint length)
{
if(!work.refreshClient||work.kind!=0||GetCurrentThreadId()!=owningThread||!frame.valid||!frame.healthValid||frame.healthEmergency||frame.hullFraction<=0
||GetTickCount64()-frame.tick>200||frame.entity!=work.entity||frame.life!=work.life||controllerEpoch!=work.epoch||!CommerceDocked(frame,work.market))return false;
var before=ReadCommerceShipInventory(frame);
if(!before.verified||!work.profileValidated||work.serverCargo>before.capacity)return false;
var credits=work.serverCredits;var count=work.serverCargo;
if(GameRoute(frame,out var pilot)==0)return false;
var cargo=RegisteredComponent(Pointer(frame.life+0xC8)-0x30,Read<uint>(image+0x5F2F25C));
var wallet=RegisteredComponent(Pointer(pilot+0xC8)-0x30,Read<uint>(image+0x5F59D90));
if(cargo==0||Pointer(cargo)!=image+0x530B858||Pointer(Pointer(cargo)+0x140)!=image+0x2A686B0
||wallet==0||Pointer(wallet)!=image+0x5295868||Pointer(Pointer(wallet)+0xA0)!=image+0x2591C70)return false;
var text=OriginalServiceText(body,length);if(text==0)return false;
try
{
// Apply this original server snapshot through the game's own
// inventory and wallet callbacks. This submits no purchase/sale.
Call(image+0x2A686B0,cargo,(ulong)(nuint)(&text),1,1,1,0);
Call(image+0x2591C70,wallet,(ulong)(nuint)(&text),0,0,0);
var after=ReadCommerceShipInventory(frame);
work.profileApplied=after.verified&&after.count==count&&after.credits==credits;
return work.profileApplied;
}
finally{Call(image+0x204010,(ulong)(nuint)(&text));}
}
private static bool CommerceProfileFixture(string body,uint capacity,bool expected,ulong credits=0,int count=0)
{
using var document=JsonDocument.Parse(body);
var valid=CommerceProfileValues(document.RootElement,capacity,out var actualCredits,out var actualCount);
return valid==expected&&(!valid||actualCredits==credits&&actualCount==count);
}
private static bool CommerceProfileSelfTest()=>
CommerceProfileFixture("{\"credits\":900,\"inventory\":[{\"commodity\":\"uranium\",\"qty\":192}]}",192,true,900,192)
&&CommerceProfileFixture("{\"credits\":1000,\"inventory\":[]}",192,true,1000,0)
&&CommerceProfileFixture("{\"credits\":900,\"inventory\":[{\"commodity\":\"uranium\",\"qty\":192},{\"commodity\":\"scrap\",\"qty\":1}]}",192,false)
&&CommerceProfileFixture("{\"credits\":900,\"inventory\":[{\"commodity\":\"uranium\",\"qty\":-1}]}",192,false)
&&CommerceProfileFixture("{\"credits\":900,\"inventory\":[{\"commodity\":\"uranium\",\"qty\":1.5}]}",192,false)
&&CommerceProfileFixture("{\"error\":\"rejected\",\"credits\":900,\"inventory\":[]}",192,false)
&&CommerceProfileFixture("{\"credits\":-1,\"inventory\":[]}",192,false)
&&CommerceProfileFixture("{\"credits\":900}",192,false);
}
using System.Text.Json;
namespace XFEEDToolBox.Plugin;
internal static unsafe partial class Navigation
{
private static bool CommerceProfileValues(JsonElement response,uint capacity,out ulong credits,out int count)
{
credits=0;count=0;
if(capacity>int.MaxValue||response.ValueKind!=JsonValueKind.Object||response.TryGetProperty("error",out _)
||!response.TryGetProperty("credits",out var balance)||balance.ValueKind!=JsonValueKind.Number||!balance.TryGetUInt64(out credits)||credits>=100000000000000UL
||!response.TryGetProperty("inventory",out var inventory)||inventory.ValueKind!=JsonValueKind.Array)return false;
foreach(var row in inventory.EnumerateArray())
{
if(row.ValueKind!=JsonValueKind.Object||!row.TryGetProperty("commodity",out var name)||name.ValueKind!=JsonValueKind.String||string.IsNullOrWhiteSpace(name.GetString())
||!row.TryGetProperty("qty",out var quantity)||quantity.ValueKind!=JsonValueKind.Number||!quantity.TryGetInt32(out var amount)||amount<0||amount>capacity-count)return false;
count+=amount;
}
return true;
}
private static bool ApplyCommerceProfile(in Snapshot frame,ref CommerceReadRequest work,byte* body,nuint length)
{
if(!work.refreshClient||work.kind!=0||GetCurrentThreadId()!=owningThread||!frame.valid||!frame.healthValid||frame.healthEmergency||frame.hullFraction<=0
||GetTickCount64()-frame.tick>200||frame.entity!=work.entity||frame.life!=work.life||controllerEpoch!=work.epoch||!CommerceDocked(frame,work.market))return false;
var before=ReadCommerceShipInventory(frame);
if(!before.verified||!work.profileValidated||work.serverCargo>before.capacity)return false;
var credits=work.serverCredits;var count=work.serverCargo;
if(GameRoute(frame,out var pilot)==0)return false;
var cargo=RegisteredComponent(Pointer(frame.life+0xC8)-0x30,Read<uint>(image+0x5F2F25C));
var wallet=RegisteredComponent(Pointer(pilot+0xC8)-0x30,Read<uint>(image+0x5F59D90));
if(cargo==0||Pointer(cargo)!=image+0x530B858||Pointer(Pointer(cargo)+0x140)!=image+0x2A686B0
||wallet==0||Pointer(wallet)!=image+0x5295868||Pointer(Pointer(wallet)+0xA0)!=image+0x2591C70)return false;
var text=OriginalServiceText(body,length);if(text==0)return false;
try
{
// Apply this original server snapshot through the game's own
// inventory and wallet callbacks. This submits no purchase/sale.
Call(image+0x2A686B0,cargo,(ulong)(nuint)(&text),1,1,1,0);
Call(image+0x2591C70,wallet,(ulong)(nuint)(&text),0,0,0);
var after=ReadCommerceShipInventory(frame);
work.profileApplied=after.verified&&after.count==count&&after.credits==credits;
return work.profileApplied;
}
finally{Call(image+0x204010,(ulong)(nuint)(&text));}
}
private static bool CommerceProfileFixture(string body,uint capacity,bool expected,ulong credits=0,int count=0)
{
using var document=JsonDocument.Parse(body);
var valid=CommerceProfileValues(document.RootElement,capacity,out var actualCredits,out var actualCount);
return valid==expected&&(!valid||actualCredits==credits&&actualCount==count);
}
private static bool CommerceProfileSelfTest()=>
CommerceProfileFixture("{\"credits\":900,\"inventory\":[{\"commodity\":\"uranium\",\"qty\":192}]}",192,true,900,192)
&&CommerceProfileFixture("{\"credits\":1000,\"inventory\":[]}",192,true,1000,0)
&&CommerceProfileFixture("{\"credits\":900,\"inventory\":[{\"commodity\":\"uranium\",\"qty\":192},{\"commodity\":\"scrap\",\"qty\":1}]}",192,false)
&&CommerceProfileFixture("{\"credits\":900,\"inventory\":[{\"commodity\":\"uranium\",\"qty\":-1}]}",192,false)
&&CommerceProfileFixture("{\"credits\":900,\"inventory\":[{\"commodity\":\"uranium\",\"qty\":1.5}]}",192,false)
&&CommerceProfileFixture("{\"error\":\"rejected\",\"credits\":900,\"inventory\":[]}",192,false)
&&CommerceProfileFixture("{\"credits\":-1,\"inventory\":[]}",192,false)
&&CommerceProfileFixture("{\"credits\":900}",192,false);
}