149 lines
9.5 KiB
C++
149 lines
9.5 KiB
C++
#include "optional_test_codec.hpp"
|
|
#include "world_translation.hpp"
|
|
#include "serializer.h"
|
|
#include <algorithm>
|
|
#include <bit>
|
|
#include <dlfcn.h>
|
|
#include <iostream>
|
|
using namespace gateway;
|
|
namespace {
|
|
void check(bool ok,const char* why){if(!ok)throw Error(why);}
|
|
LegacyPacket flags(uint32_t hp,uint32_t value){
|
|
Bytes b;le(b,1,4);append(b,Bytes{0,0});Bytes raw;le(raw,4518,8);append(b,pack_guid(raw));
|
|
le(b,6,1);le(b,1u<<22,4);for(unsigned i=0;i<4;++i)le(b,0,4);le(b,1u<<30,4);
|
|
le(b,hp,4);le(b,value,4);return {0xa9,b};
|
|
}
|
|
Bytes spell_id(uint32_t spell){
|
|
Bytes bytes;le(bytes,spell,4);return bytes;
|
|
}
|
|
LegacyPacket self_res_update(uint64_t guid, bool create, std::optional<uint32_t> offer){
|
|
constexpr uint16_t field=1224; // Tortoise PLAYER_SELF_RES_SPELL
|
|
Bytes body;le(body,1,4);le(body,0,1);le(body,create?3:0,1);
|
|
Bytes raw;le(raw,guid,8);append(body,pack_guid(raw));
|
|
std::map<uint16_t,uint32_t> fields;
|
|
if(create){
|
|
le(body,4,1);le(body,0x21,1);le(body,0,4);le(body,100,4);
|
|
for(float f:{1.f,2.f,3.f,0.f})le(body,std::bit_cast<uint32_t>(f),4);
|
|
le(body,0,4);
|
|
for(float f:{2.5f,7.f,4.5f,4.72f,2.5f,3.14f})le(body,std::bit_cast<uint32_t>(f),4);
|
|
fields={{22,0},{28,80},{34,1},{35,1},{36,6u|(1u<<8)|(1u<<24)}};
|
|
}
|
|
if(offer)fields[field]=*offer;
|
|
check(!fields.empty(),"self-res fixture needs a Source field");
|
|
std::vector<uint32_t> mask(fields.rbegin()->first/32+1);
|
|
for(auto [index,value]:fields)mask[index/32]|=1u<<(index%32);
|
|
le(body,mask.size(),1);for(auto word:mask)le(body,word,4);
|
|
for(auto [index,value]:fields)le(body,value,4);
|
|
return {0xa9,std::move(body)};
|
|
}
|
|
LegacyPacket location(bool valid=true){
|
|
Bytes b{uint8_t(valid)};
|
|
if(valid){le(b,1,4);for(float f:{-6900.f,1200.f,2.f})le(b,std::bit_cast<uint32_t>(f),4);le(b,1,4);}
|
|
return {0x216,b};
|
|
}
|
|
LegacyPacket transfer(){
|
|
Bytes raw;le(raw,4518,8);auto b=pack_guid(raw);le(b,42,4);
|
|
append(b,turtle_ground_movement(GroundMovement{4518,0x10000000,100,{-6831.32f,891.437f,33.8663f,0}}));
|
|
return {0xc7,b};
|
|
}
|
|
std::vector<LegacyPacket> unpack(const Bytes& b){
|
|
Reader r(b);auto n=r.le(4);std::vector<LegacyPacket> out;
|
|
while(n--){auto op=uint32_t(r.le(4));out.push_back({op,r.take(r.le(4))});}r.end();return out;
|
|
}
|
|
NativeWorldTranslation world(){
|
|
NativeWorldTranslation w{};w.selected.guid=4518;w.selected.cls=1;w.selected.race=6;
|
|
w.realm=70;w.self_created=true;w.location=WorldLocation{1,{0,0,0,0}};return w;
|
|
}
|
|
}
|
|
int main(){try{
|
|
if (test_fixture::optional_codec_missing(GW_ENTITY_CODEC)) return 77;
|
|
auto library = dlopen(GW_ENTITY_CODEC, RTLD_NOW | RTLD_LOCAL);
|
|
if (!library) {
|
|
std::cerr << dlerror() << '\n';
|
|
return 1;
|
|
}
|
|
auto codec=reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(library,"gw_codec_serialize"));check(codec,"missing owned codec export");
|
|
auto run=[&](NativeWorldTranslation& w,const LegacyPacket& p){return unpack(w.translate(p,0x46010046,0,codec));};
|
|
auto count=[](const auto& rows){return std::count_if(rows.begin(),rows.end(),[](const auto& p){return p.opcode==0x47017f;});};
|
|
{
|
|
auto w=world();w.self_created=false;w.selected.name="Fixture";w.selected.zone=12;
|
|
constexpr uint32_t first=20608,second=20762,native=0x3f00d6;
|
|
auto prepare=[&](uint32_t spell){return w.gameplay(native,spell_id(spell));};
|
|
auto denied=[&](uint32_t spell){
|
|
try{(void)prepare(spell);}catch(const Error&){return true;}
|
|
return false;
|
|
};
|
|
auto expect_offer=[&](uint32_t spell){
|
|
auto packet=prepare(spell);check(packet.opcode==0x2b3 && packet.body.empty(),"self-res Source request");
|
|
};
|
|
auto object_updates=[](const auto& rows){
|
|
return std::count_if(rows.begin(),rows.end(),[](const auto& p){return p.opcode==0x5e0000;});
|
|
};
|
|
auto offers=[](const auto& rows,uint32_t spell){
|
|
// Original build 70334 ActivePlayerData::WriteUpdate for a sole SelfResSpells list.
|
|
Bytes values{0,4,0,64,0,4,0,0,0,uint8_t(spell?1:0)};
|
|
if(spell){values.push_back(0x80);le(values,spell,4);}
|
|
return std::count_if(rows.begin(),rows.end(),[&](const auto& row){
|
|
return row.opcode==0x5e0000 && row.body.size()>=values.size() &&
|
|
std::equal(values.rbegin(),values.rend(),row.body.rbegin());
|
|
});
|
|
};
|
|
check(denied(first),"self-res before own create");
|
|
auto born=run(w,self_res_update(w.selected.guid,true,first));
|
|
check(object_updates(born)==1,"initial self-res must use the single full native self");
|
|
expect_offer(first);
|
|
w.pending_teleport.emplace();check(denied(first),"self-res allowed during pending near teleport");w.pending_teleport.reset();
|
|
w.far_transfer.emplace();check(denied(first),"self-res allowed during far transfer");w.far_transfer.reset();
|
|
run(w,self_res_update(w.selected.guid+1,false,second));
|
|
check(denied(second),"peer changed selected spell");expect_offer(first);
|
|
auto replaced=run(w,self_res_update(w.selected.guid,false,second));
|
|
check(offers(replaced,second)==1 && denied(first),"selected spell replacement");expect_offer(second);
|
|
expect_offer(second); // a sizing retry has not sent the request
|
|
w.gameplay_sent(native,spell_id(second));
|
|
check(denied(second),"same offer spent twice before Source clear");
|
|
run(w,self_res_update(w.selected.guid,false,second));check(denied(second),"unchanged option rearmed after sending");
|
|
auto cleared=run(w,self_res_update(w.selected.guid,true,{}));
|
|
check(offers(cleared,0)==1 && denied(second),"full omitted spell must clear native list");
|
|
run(w,self_res_update(w.selected.guid,true,first));expect_offer(first);
|
|
auto no_change=run(w,self_res_update(w.selected.guid,true,first));
|
|
check(!offers(no_change,first),"unchanged Source full republished the offer");
|
|
auto sparse_clear=run(w,self_res_update(w.selected.guid,false,0));
|
|
check(offers(sparse_clear,0)==1 && denied(first),"sparse zero did not retire the offer");
|
|
run(w,self_res_update(w.selected.guid,false,second));expect_offer(second);
|
|
Bytes guid;le(guid,w.selected.guid,8);run(w,{0xaa,guid});
|
|
check(denied(second),"destroy retained selected option");
|
|
auto stale=run(w,self_res_update(w.selected.guid,false,second));
|
|
check(!offers(stale,second) && denied(second),"forgotten self resurrected via an isolated sparse update");
|
|
auto recreated=run(w,self_res_update(w.selected.guid,true,first));
|
|
check(offers(recreated,first)==1,"returning self did not publish the new offer");expect_offer(first);
|
|
run(w,{0x4d,{}});check(denied(first),"logout retained an actionable offer");
|
|
std::cout<<"Self-res initial, replacement, retry, sent, omitted/explicit clear, returning self and logout PASS.\n";
|
|
}
|
|
std::array<unsigned,3> order{0,1,2};
|
|
do{
|
|
auto w=world();run(w,flags(0,0));
|
|
check(w.source_health==0 && w.source_ghost==false,"source death not retained");
|
|
w.gameplay(0x3f00cb,Bytes{0});check(!w.release.requested,"sizing/preparation starts release");
|
|
w.gameplay_sent(0x3f00cb,Bytes{0});check(w.release.requested,"genuine sent request not retained");
|
|
std::array<LegacyPacket,3> events{flags(1,16),location(),transfer()};
|
|
std::vector<LegacyPacket> result;
|
|
for(unsigned i=0;i<3;++i){auto rows=run(w,events[order[i]]);check(count(rows)==(i==2),"graveyard before all source witnesses or missing final event");if(i==2)result=std::move(rows);}
|
|
auto event=std::find_if(result.begin(),result.end(),[](const auto& p){return p.opcode==0x47017f;});
|
|
Reader r(event->body);check(r.le(4)==1,"graveyard map differs");
|
|
for(float f:{-6831.32f,891.437f,33.8663f})check(r.f32()==f,"corpse/body position substituted for real graveyard transfer");r.end();
|
|
check(!w.release.requested && !w.release.transfer,"release not one-shot");
|
|
check(count(run(w,location()))==0,"duplicate corpse reply republishes graveyard");
|
|
auto revived=run(w,flags(100,0));check(count(revived)==1,"real resurrection does not clear graveyard");
|
|
auto clear=std::find_if(revived.begin(),revived.end(),[](const auto& p){return p.opcode==0x47017f;});
|
|
check(clear->body==Bytes({255,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0}),"clear framing differs");
|
|
check(count(run(w,flags(100,0)))==0,"repeat living snapshot restarts lifecycle");
|
|
}while(std::next_permutation(order.begin(),order.end()));
|
|
{auto w=world();run(w,flags(1,16));check(count(run(w,location()))==0 && count(run(w,transfer()))==0,"initial ghost login invents graveyard");}
|
|
{auto w=world();run(w,flags(100,0));w.gameplay_sent(0x3f00cb,Bytes{0});check(!w.release.requested,"living request starts release");}
|
|
{auto w=world();run(w,flags(0,0));w.gameplay_sent(0x3f00cb,Bytes{0});run(w,location(false));check(!w.release.requested,"absent corpse leaves reusable release");}
|
|
{auto w=world();run(w,flags(0,0));w.gameplay_sent(0x3f00cb,Bytes{0});run(w,flags(100,0));check(!w.release.requested,"rejected release outlives real living state");}
|
|
{auto w=world();run(w,flags(0,0));w.gameplay_sent(0x3f00cb,Bytes{0});run(w,{0x4d,{}});check(!w.release.requested && !w.source_health,"logout retains release generation");}
|
|
std::cout<<"Source-bound graveyard initialization: six witness orderings, original writer coordinates, one-shot lifetime, real resurrection clear and negative ghost/login/corpse/logout cases PASS. No actual-client prompt claim.\n";
|
|
return 0;
|
|
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}
|