Files
WoW-Hybrid-gateway/tests/native_pet_unlearn_mount_test.cpp

144 lines
12 KiB
C++

// Synthetic: pet untrain offer/confirm guards (current hunter pet, offering trainer, generations,
// replacement, close) and mount special animation (visible players only, at most 2 kits).
#include "world_translation.hpp"
#include "gateway/pet_presentation.hpp"
#include <algorithm>
#include <iostream>
using namespace gateway;
static void expect(bool ok,const char* what){if(!ok)throw Error(what);}
template<class F>static void rejects(F f,const char* what){bool r=false;try{f();}catch(const Error&){r=true;}expect(r,what);}
static std::vector<std::pair<uint32_t,Bytes>> unbundle(const Bytes& b){
std::vector<std::pair<uint32_t,Bytes>> out;if(b.empty())return out;Reader r(b);auto n=r.le(4);
for(uint64_t i=0;i<n;++i){auto op=uint32_t(r.le(4));auto s=r.le(4);auto x=r.take(s);out.emplace_back(op,Bytes(x.begin(),x.end()));}return out;}
static Guid128 g(uint64_t lo,uint64_t hi){Guid128 x{};Bytes b;le(b,lo,8);le(b,hi,8);std::copy(b.begin(),b.end(),x.begin());return x;}
static NativeEntityTranslation::Actor actor(uint8_t kind,Guid128 modern,uint64_t gen){NativeEntityTranslation::Actor a;a.kind=kind;a.modern=modern;a.sent=true;a.generation=gen;return a;}
static unsigned mount_lifecycle(){
unsigned count=0,failed=0;
auto check=[&](bool ok,const char* why){++count;if(!ok){++failed;std::cerr<<why<<'\n';}};
constexpr uint64_t peer=77;
auto base=[&]{NativeWorldTranslation s;s.realm=70;s.selected.guid=4509;s.self_created=true;
s.location=WorldLocation{0,{0,0,0,0}};auto a=actor(4,forever_player_guid(peer,70),1);
a.eligible=true;a.presentation_realm=70;a.presentation_map=0;s.entities.actors[peer]=a;
auto& p=s.entities.peer_players[peer];p.generation=1;p.named=true;p.identity.guid=peer;p.identity.name="Peer";return s;};
auto source=[](uint64_t who){Bytes b;le(b,who,8);return b;};
auto send=[&](NativeWorldTranslation& s,uint64_t who){return s.translate({0x172,source(who)},0x10046,0,nullptr);};
auto valid=base();auto payload=pack_guid(forever_player_guid(peer,70));le(payload,0,4);le(payload,0,4);
Bytes expected;le(expected,1,4);le(expected,0x47014c,4);le(expected,payload.size(),4);append(expected,payload);
check(send(valid,peer)==expected,"complete native mount-special defaults");
const Bytes empty(4,0);
for(uint64_t who:{uint64_t(0),uint64_t(4509),uint64_t(88),uint64_t(0xf130000000000101)})
check(send(valid,who)==empty,"mount-special non-peer identity admitted");
for(unsigned state=0;state<6;++state){auto s=base();
if(state==0)s.self_created=false;
if(state==1)s.location.reset();
if(state==2)s.logout_complete=true;
if(state==3)s.pending_teleport=NearTeleport{};
if(state==4)s.far_transfer=NativeWorldTranslation::FarTransfer{};
if(state==5)s.realm=0;
check(send(s,peer)==empty,"mount-special outside active world");
}
for(unsigned state=0;state<16;++state){auto s=base();auto& a=s.entities.actors.at(peer);
if(state==0)a.sent=false;
if(state==1)a.eligible=false;
if(state==2)a.generation=0;
if(state==3)a.modern={};
if(state==4)a.kind=3;
if(state==5)a.kind=5;
if(state==6)a.presentation_map=1;
if(state==7)a.presentation_realm=71;
if(state==8)s.entities.peer_players.at(peer).named=false;
if(state==9)s.entities.peer_players.at(peer).blocked=true;
if(state==10)s.entities.peer_players.erase(peer);
if(state==11)s.entities.peer_players.at(peer).generation++;
if(state==12)s.entities.peer_players.at(peer).identity.guid++;
if(state==13)s.entities.peer_players.at(peer).identity.name.clear();
if(state==14)a.modern=forever_player_guid(peer+1,70);
if(state==15)s.entities.peer_players.at(peer).generation=0;
check(send(s,peer)==empty,"mount-special stale or invalid presentation");
a=base().entities.actors.at(peer);s.entities.peer_players=base().entities.peer_players;check(send(s,peer)==expected,"mount-special binding recovery");
}
for(size_t n=0;n<8;++n){auto b=source(peer);b.resize(n);bool rejected=false;
try{(void)valid.translate({0x172,b},0x10046,0,nullptr);}catch(const Error&){rejected=true;}
check(rejected,"truncated mount-special accepted");
}
{auto b=source(peer);b.push_back(0);bool rejected=false;
try{(void)valid.translate({0x172,b},0x10046,0,nullptr);}catch(const Error&){rejected=true;}
check(rejected,"trailing mount-special accepted");}
check(send(valid,peer)==expected,"malformed mount-special poisoned next event");
(void)valid.translate({0xaa,source(peer)},0x10046,0,nullptr);
check(send(valid,peer)==empty,"mount-special after source destroy");
valid.entities.actors[peer]=base().entities.actors.at(peer);valid.entities.actors[peer].generation=2;
valid.entities.peer_players=base().entities.peer_players;valid.entities.peer_players.at(peer).generation=2;
check(send(valid,peer)==expected,"mount-special recreated peer");
(void)valid.translate({0x4d,{}},0x10046,0,nullptr);
check(send(valid,peer)==empty,"mount-special after source logout");
std::cout<<count<<" mount-special lifecycle checks; "<<failed<<" failures\n";return failed;
}
int main(){try{
NativeWorldTranslation w{};w.realm=70;w.selected.guid=4509;w.selected.race=3;w.selected.cls=3;w.selected.name="Hunter";
w.self_created=true;w.location=WorldLocation{0,{0,0,0,0}};w.virtual_realm_address=0x10046;
const uint64_t trainer=0xF130000000000101ull,pet=0xF140000005000202ull,other=0xF130000000000303ull;
const auto trainer_m=g(0x101,0x2000000000000001ull),pet_m=g(0x202,0x2000000000000002ull),other_m=g(0x303,0x2000000000000003ull);
w.entities.actors[trainer]=actor(3,trainer_m,1);w.entities.actors[pet]=actor(3,pet_m,1);
{auto& a=w.entities.actors.at(pet);a.presentation_realm=70;a.presentation_map=0;a.owned_stat_pet=true;
a.pending_values={{SourcePetOwner,4509},{uint16_t(SourcePetOwner+1),0},{SourcePetCreator,4509},{uint16_t(SourcePetCreator+1),0},{SourcePetNumber,5}};}w.entities.actors[other]=actor(3,other_m,1);
{auto& a=w.entities.actors.at(pet);a.eligible=true;}
auto bar=[&]{PetSpellbook book{};book.unit=pet;NativeWorldTranslation::PetBar b{book,70,0};b.generation=w.entities.actors.at(pet).generation;b.shown=true;w.pet_bar=b;
auto& c=w.entities.self_controlled[1];c.words={uint32_t(pet),uint32_t(pet>>32)};c.known=3;c.presented=pet_m;};
bar();
// A genuinely sent GossipSelectOption on the shown menu of npc arms the authority.
auto gossip=[&](uint64_t npc){GossipMenu m;m.guid=npc;w.gossip=m;w.gossip_actor_generation=w.entities.actors.at(npc).generation;w.gameplay_sent(0x3f002b,{});};
auto emitted=[](const std::vector<std::pair<uint32_t,Bytes>>& v){return std::any_of(v.begin(),v.end(),[](auto& p){return p.first==0x4700bd;});};
auto offer=[&](uint64_t p,uint32_t cost=500){Bytes b;le(b,p,8);le(b,cost,4);return unbundle(w.translate({0x2f1,b},0x10046,0,nullptr));};
auto confirm=[&](Guid128 master,uint8_t type=3){Bytes b=pack_guid(master);b.push_back(type);return b;};
auto accepts=[&](Guid128 m){try{return w.gameplay(0x3e00ca,confirm(m)).opcode==0x2f0;}catch(const Error&){return false;}};
// Offer for the current pet names the trainer as RespecMaster.
gossip(trainer);auto o=offer(pet);auto e=std::find_if(o.begin(),o.end(),[](auto& p){return p.first==0x4700bd;});expect(e!=o.end(),"offer emitted");
{Bytes want{3};le(want,500,4);append(want,pack_guid(trainer_m));le(want,0,4);expect(e->second==want,"type 3, cost, trainer master");}
expect(!w.last_gossip_npc,"gossip authority consumed");
rejects([&]{w.gameplay(0x3e00ca,confirm(other_m));},"wrong trainer");
rejects([&]{w.gameplay(0x3e00ca,confirm(trainer_m,0));},"talent type");
auto ok=w.gameplay(0x3e00ca,confirm(trainer_m));{Reader r(ok.body);expect(ok.opcode==0x2f0 && r.le(8)==pet,"unlearn current pet");r.end();}
w.gameplay_sent(0x3e00ca,confirm(trainer_m));expect(!accepts(trainer_m),"consumed once");
// No offer: stale gossip authority (already consumed), wrong pet, non-hunter, unshown bar.
{auto x=offer(pet);expect(!emitted(x),"consumed gossip is not reused");}
gossip(trainer);{auto x=offer(other);expect(!emitted(x),"wrong pet");}
{auto h=w;h.selected.cls=1;h.last_gossip_npc=NativeWorldTranslation::SpiritHealerOffer{trainer,1};Bytes b;le(b,pet,8);le(b,1,4);
auto x=unbundle(h.translate({0x2f1,b},0x10046,0,nullptr));expect(!emitted(x),"non-hunter");}
{auto h=w;h.last_gossip_npc=NativeWorldTranslation::SpiritHealerOffer{trainer,1};h.pet_bar->shown=false;Bytes b;le(b,pet,8);le(b,1,4);
auto x=unbundle(h.translate({0x2f1,b},0x10046,0,nullptr));expect(!emitted(x),"unshown pet bar");}
{auto h=w;h.last_gossip_npc=NativeWorldTranslation::SpiritHealerOffer{trainer,1};h.entities.self_controlled[1].known=0;Bytes b;le(b,pet,8);le(b,1,4);
auto x=unbundle(h.translate({0x2f1,b},0x10046,0,nullptr));expect(!emitted(x),"no current pet control");}
// A -> B: a genuine gossip menu from another NPC retires A's offer.
gossip(trainer);offer(pet);
{Bytes menu;le(menu,other,8);le(menu,1,4);le(menu,0,4);le(menu,0,4);w.translate({0x17d,menu},0x10046,0,nullptr);}
expect(!w.pet_unlearn_offer && !accepts(trainer_m),"switch to B retires A");
// A sent banker / gossip hello to another NPC retires A's offer and any armed authority.
gossip(trainer);offer(pet);w.gameplay_sent(0x3f004b,{});expect(!accepts(trainer_m),"banker interaction retires");
gossip(trainer);w.gameplay_sent(0x3f0029,{});{auto x=offer(pet);expect(!emitted(x),"armed authority retired by new interaction");}
// Explicit client close of the selected trainer before the Source offer: no offer afterwards.
gossip(trainer);(void)w.close_interaction(pack_guid(trainer_m));{auto x=offer(pet);expect(!emitted(x),"close before offer retires authority");}
gossip(trainer);(void)w.close_interaction(pack_guid(other_m));{auto x=offer(pet);expect(emitted(x),"other close keeps authority");}
// A menu without a sent selection does not arm the authority.
{Bytes menu;le(menu,trainer,8);le(menu,1,4);le(menu,0,4);le(menu,0,4);w.translate({0x17d,menu},0x10046,0,nullptr);auto x=offer(pet);expect(!emitted(x),"menu alone does not arm");}
// Closing a different NPC keeps the offer; closing the master retires it.
gossip(trainer);offer(pet);(void)w.close_interaction(pack_guid(other_m));expect(accepts(trainer_m),"other close keeps");
(void)w.close_interaction(pack_guid(trainer_m));expect(!accepts(trainer_m),"master close retires");
// Changed trainer generation, stale pet generation.
gossip(trainer);offer(pet);++w.entities.actors.at(trainer).generation;expect(!accepts(trainer_m),"trainer generation");
w.entities.actors.at(trainer).generation=1;
gossip(trainer);offer(pet);++w.entities.actors.at(pet).generation;expect(!accepts(trainer_m),"pet generation");
w.entities.actors.at(pet).generation=1;bar();
// Second offer replaces the first, including its cost.
gossip(trainer);offer(pet,500);gossip(trainer);{auto x=offer(pet,900);auto q=std::find_if(x.begin(),x.end(),[](auto& p){return p.first==0x4700bd;});
expect(q!=x.end(),"second offer");Reader r(q->second);r.le(1);expect(r.le(4)==900,"second cost");}
expect(accepts(trainer_m),"second offer live");
// Pet control loss then reacquire: the old offer does not resurrect.
w.entities.self_controlled[1].known=0;w.translate({0x1d1,Bytes(8,0)},0x10046,0,nullptr);expect(!w.pet_unlearn_offer,"swept on loss");
bar();expect(!accepts(trainer_m),"no resurrection");
expect(mount_lifecycle()==0,"mount-special lifecycle");
for(uint32_t n:{0u,1u,2u}){Bytes q;le(q,n,4);le(q,0,4);for(uint32_t i=0;i<n;++i)le(q,10+i,4);expect(w.gameplay(0x3e0156,q).opcode==0x171,"0..2 kits");}
{Bytes q;le(q,3,4);le(q,0,4);for(int i=0;i<3;++i)le(q,1,4);rejects([&]{w.gameplay(0x3e0156,q);},"3 kits rejected");}
std::cout<<"PASS pet unlearn and mount special guards\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}