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

299 lines
18 KiB
C++

#include "world_translation.hpp"
#include "gateway/transport_periods.hpp"
#include <algorithm>
#include <array>
#include <bit>
#include <chrono>
#include <cmath>
#include <dlfcn.h>
#include <filesystem>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace gateway;
namespace {
using Clock = std::chrono::steady_clock;
using Pose = std::array<float,4>;
constexpr uint32_t address = 0x46010046, period = 16667;
constexpr uint16_t realm = 70;
constexpr uint64_t player = 4528;
constexpr Pose upper{1633.3f,233.931f,62.6749f,0};
constexpr Pose lower{1669.39f,307.177f,-62.064f,0};
constexpr Pose exterior{1737.11f,239.345f,62.6784f,0};
struct Spawn { uint32_t entry,counter,display; Pose pos; float qz,qw; };
// Independent values from the nine Tortoise gameobject rows and their display templates.
constexpr std::array<Spawn,9> spawns{{
{20649,44892,455,{1596.15f,291.8f,14.6822f,-1.57952f},-.710186f,.704014f},
{20650,44976,462,{1596.04f,282.738f,55.3952f,-1.57952f},-.710186f,.704014f},
{20651,44893,462,{1596.21f,302.401f,-40.6645f,1.56207f},.704015f,.710185f},
{20652,44962,455,{1595.26f,188.644f,-40.7835f,1.56207f},.704015f,.710185f},
{20653,44891,462,{1595.38f,197.706f,55.3952f,1.56207f},.704015f,.710185f},
{20654,44954,462,{1595.21f,178.693f,-40.5223f,-1.57952f},-.710186f,.704014f},
{20655,44901,455,{1544.24f,240.773f,-40.7835f,-.008727f},-.004363f,.99999f},
{20656,44890,462,{1553.3f,240.656f,55.3952f,-.008727f},-.004363f,.99999f},
{20657,44909,462,{1533.88f,240.826f,-32.3473f,3.13287f},.99999f,.004363f},
}};
void check(bool ok,const std::string& message){if(!ok)throw Error(message);}
uint32_t bits(float f){return std::bit_cast<uint32_t>(f);}
uint64_t source_id(const Spawn& row,uint16_t high=0xf110,uint32_t extra=0){
return (uint64_t(high)<<48)|(uint64_t(row.entry)<<24)|(row.counter+extra);
}
Guid128 guid(const Bytes& bytes){Guid128 result{};std::copy(bytes.begin(),bytes.end(),result.begin());return result;}
uint32_t milliseconds(){return uint32_t(std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now().time_since_epoch()).count());}
LegacyPacket received(std::span<const Spawn> rows,uint16_t high=0xf110,uint32_t extra=0){
Bytes packet;le(packet,rows.size(),4);le(packet,0,1);
for(const auto& r:rows){
auto id=source_id(r,high,extra);
Bytes raw;le(raw,id,8);
le(packet,3,1);append(packet,pack_guid(raw));le(packet,5,1);
// Transport movement block has zero XYZO and a transport progress word; the world location is in fields 15..18.
le(packet,0x42,1);for(unsigned n=0;n<4;++n)le(packet,0,4);le(packet,0,4);
std::map<unsigned,uint32_t> fields{{3,r.entry},{4,bits(1.f)},{8,r.display},{9,0x28},
{12,bits(r.qz)},{13,bits(r.qw)},{14,1},{21,11},{24,100}};
for(unsigned n=0;n<4;++n)fields[15+n]=bits(r.pos[n]);
uint32_t mask=0;for(const auto& [field,value]:fields)mask|=1u<<field;
le(packet,1,1);le(packet,mask,4);for(const auto& [field,value]:fields)le(packet,value,4);
}
return {0xa9,std::move(packet)};
}
NativeWorldTranslation world(uint32_t map=0) {
NativeWorldTranslation w{};
w.selected.guid=player;w.selected.name="Fixture";w.selected.race=1;w.selected.cls=1;w.selected.level=60;
w.realm=realm;w.location=WorldLocation{map,upper};w.self_created=true;
w.entities.position_realm=realm;w.entities.position_map=map;
w.entities.self_position_generation=1;w.entities.self_presentation_ready=true;
return w;
}
void move_to(NativeWorldTranslation& w,Pose pose){
constexpr uint32_t move_stop=0x420002;
auto body=forever_ground_movement(GroundMovement{player,0,250,pose},realm);
auto source=w.player_movement(move_stop,body,std::vector<Character>{w.selected});
w.movement_sent(move_stop,body,source);
check(w.entities.source_positions.contains(player),"normal owned movement failed to establish current position");
}
struct Batch {
std::map<uint32_t,Bytes> creates; // Full blocks, with validated timestamp and derived fractional progress normalized.
std::vector<Guid128> removed;
};
struct Observation {
uint32_t started,ended;
Bytes bundle;
};
template<typename Fn> Observation observe(Fn&& fn){
auto before=milliseconds();auto body=fn();return {before,milliseconds(),std::move(body)};
}
Batch parse(const Observation& call,uint32_t extra_counter=0,const Bytes* self_packet=nullptr){
Batch out;Reader bundle(call.bundle);auto messages=bundle.le(4);
while(messages--){
auto opcode=bundle.le(4);auto body=bundle.take(bundle.le(4));if(opcode!=0x5e0000 || (self_packet && *self_packet==body))continue;
Reader packet(body);const auto map=packet.le(2),count=packet.le(4),flags=packet.le(1);
if(flags==0xc0){
const auto destroyed=packet.le(2),total=packet.le(4);check(destroyed<=total,"removal counts");
for(unsigned i=0;i<total;++i)out.removed.push_back(guid(unpack_guid(packet,16)));
}else check(flags==0x80,"unexpected object update flags");
auto blocks=packet.take(packet.le(4));packet.end();Reader r(blocks);
for(unsigned i=0;i<count;++i){
auto start=blocks.size()-r.left();auto kind=r.le(1);auto object=guid(unpack_guid(r,16));
if(kind==0){r.take(r.le(4));continue;}
check(kind==2 && r.le(1)==8,"expected native gameobject create");
check(r.take(3)==Bytes({0x81,0x4c,0}) && r.le(4)==0,"stationary + rotation + server time + gameobject flags");
Pose pos;for(float& f:pos)f=r.f32();
auto stamp_at=blocks.size()-r.left();uint32_t stamp=r.le(4);uint64_t rot=r.le(8);
check(uint32_t(stamp-call.started)<=uint32_t(call.ended-call.started),"transport clock outside the serializer's monotonic interval");
check(r.le(4)==0 && r.le(1)==0x20,"missing gameobject path progress bit");
auto phase_at=blocks.size()-r.left();auto progress=r.le(4);
check(progress==bits(float(stamp%period)/float(period)),"transport phase differs from full shared clock and 16667-ms period");
auto values=r.take(r.le(4));Reader fields(values);
check(fields.take(5)==Bytes({0,3,206,255,1}),"object/gameobject visibility");
auto entry=uint32_t(fields.le(4));auto row=std::find_if(spawns.begin(),spawns.end(),[entry](auto& s){return s.entry==entry;});
check(row!=spawns.end(),"unexpected create outside the nine qualified entries");
check(turtle_transport_period(entry)==period,"period mismatch");
check(object==forever_game_object_guid(source_id(*row,0xf110,extra_counter),entry,realm,uint32_t(map)),"native GUID/entry/counter");
Pose expected=row->pos;expected[3]=forever_facing(expected[3]);check(pos==expected,"GO XYZ/facing disagrees with spawn");
auto length=std::sqrt(row->qz*row->qz+row->qw*row->qw);
check(rot==(uint32_t(int32_t((row->qz/length)*1048576))&0x1fffffu),"normalized packed spawn rotation");
fields.le(4);check(fields.le(4)==bits(1.f) && fields.le(4)==row->display,"scale/display");
fields.take(24);unpack_guid(fields,16);unpack_guid(fields,16);
auto go_flags=fields.le(4);check((go_flags&0x08)!=0,"missing transport flag");fields.le(4);
check(fields.take(16)==Bytes({0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,63}),"parent rotation");
fields.le(4);fields.le(4);
check(fields.le(1)==24 && fields.le(1)==11 && fields.le(1)==100,"state/type/health");
auto normalized=Bytes(blocks.begin()+start,blocks.begin()+(blocks.size()-r.left()));
for(auto offset:{stamp_at-start,phase_at-start})std::fill_n(normalized.begin()+offset,4,0);
check(out.creates.emplace(entry,std::move(normalized)).second,"duplicate create within output");
}
r.end();
}
bundle.end();return out;
}
using Codec=decltype(&gw_codec_serialize);
Batch local(NativeWorldTranslation& w,Codec codec){return parse(observe([&]{return w.local_presentation(address,codec);}));}
Batch source(NativeWorldTranslation& w,Codec codec,const LegacyPacket& p,uint32_t extra=0){
return parse(observe([&]{return w.translate(p,address,0,codec);}),extra);
}
void settle(NativeWorldTranslation& w){
check(w.undercity_settle.has_value(),"Undercity entry did not arm the settle deadline");
w.undercity_settle=Clock::now()-std::chrono::milliseconds(1);
check(w.local_presentation_pending(),"idle native session would never service this deadline");
}
void enter(NativeWorldTranslation& w,Codec codec,Pose where=upper){
move_to(w,where);
auto first=local(w,codec);
check(first.creates.empty() && first.removed.empty(),"transport appeared before world settle");
check(w.undercity_settle.has_value(),"missing settle after entry");
}
Bytes query(const Spawn& row,uint32_t counter_offset=0){
Bytes body;le(body,row.entry,4);
append(body,pack_guid(forever_game_object_guid(source_id(row,0xf110,counter_offset),row.entry,realm,0)));
return body;
}
void cold_templates(NativeWorldTranslation& w,Codec codec){
auto wrong=query(spawns.front());
wrong[0]++;
check(!w.queue_undercity_template(wrong),"mismatched query entry and GUID");
check(!w.queue_undercity_template(query(spawns.front(),23)),"unknown counter obtained a synthetic template");
const auto request_count=w.requests.size();
for(const auto& row:spawns)check(w.queue_undercity_template(query(row)),"missing visible cold-cache template");
check(w.local_presentation_pending(),"native template queue was unreachable");
auto raw=w.local_presentation(address,codec);
Reader packets(raw);auto total=packets.le(4);
std::set<uint32_t> returned;
for(unsigned n=0;n<total;++n){
auto op=packets.le(4);auto body=packets.take(packets.le(4));
if(op!=0x4b0007)continue;
Reader reply(body);uint32_t entry=reply.le(4);
auto it=std::find_if(spawns.begin(),spawns.end(),[&](auto& row){return row.entry==entry;});
check(it!=spawns.end() && returned.insert(entry).second,"unexpected or duplicate cold template");
check(guid(unpack_guid(reply,16))==forever_game_object_guid(source_id(*it),entry,realm,0),"wrong template native GUID");
check(reply.le(1)==0x80,"negative cold response");
auto values=reply.take(reply.le(4));reply.end();Reader stats(values);
check(stats.le(4)==11 && stats.le(4)==it->display,"wrong type/display in cold template");
constexpr std::array<const char*,3> names{"Undervator","upperLdoor","lowerLdoor"};
check(stats.cstring()==names[(entry-20649)%3],"cold template name");
for(unsigned i=0;i<6;++i)check(stats.cstring().empty(),"unexpected secondary template string");
for(unsigned i=0;i<35;++i)check(stats.le(4)==0,"transport definition gained a stop or secondary property");
check(stats.le(4)==bits(1.f) && stats.le(1)==0 && stats.le(4)==0 && stats.le(4)==0,"template size/list/tuning/level");
stats.end();
}
packets.end();check(returned.size()==9,"incomplete native template reply list");
check(w.requests.size()==request_count && w.gameobject_queries.empty(),"synthetic template sent a Source query");
}
LegacyPacket self_create(){
const std::map<uint16_t,uint32_t> fields{{22,80},{28,80},{34,60},{35,1},{36,0x01000101},{718,98},{719,0x012c012c}};
Bytes b,id;le(b,1,4);le(b,0,1);le(b,3,1);le(id,player,8);append(b,pack_guid(id));
le(b,4,1);le(b,0x21,1);le(b,0,4);le(b,100,4);
for(float f:upper)le(b,bits(f),4);
le(b,0,4);for(float f:{2.5f,7.f,4.5f,4.72f,2.5f,3.14f})le(b,bits(f),4);
std::array<uint32_t,23> mask{};
for(auto [field,value]:fields)mask[field/32]|=1u<<(field%32);
le(b,mask.size(),1);for(auto word:mask)le(b,word,4);
for(auto [field,value]:fields)le(b,value,4);
return {0xa9,std::move(b)};
}
Codec actual;
Bytes self_output;
int recording_codec(uint32_t mode,const uint8_t* input,uint32_t size,uint32_t flags,uint8_t* output,uint32_t capacity,uint32_t* written){
auto result=actual(mode,input,size,flags,output,capacity,written);
if(mode==GW_CODEC_SELF && result==GW_CODEC_OK && output)self_output.assign(output,output+*written);
return result;
}
void before_self(Codec codec,const Batch& real){
auto w=world();w.self_created=false;w.entities.self_presentation_ready=false;w.entities.self_position_generation=0;
auto initial=source(w,codec,received(std::span(spawns).first(5),0xf120));
check(initial.creates.empty() && !w.undercity_settle,"pre-self create leaked before own player");
Bytes removed;le(removed,source_id(spawns[4],0xf120),8);
check(source(w,codec,{0xaa,removed}).creates.empty(),"early destroy yielded a new create");
actual=codec;self_output.clear();
auto activation=observe([&]{return w.translate(self_create(),address,0,recording_codec);});
check(w.self_created && !self_output.empty(),"normal pre-self test did not publish own player with the actual codec");
auto presented=parse(activation,0,&self_output);
check(presented.creates.size()==4,"pre-self genuine survivors were not replayed exactly once");
settle(w);auto missing=local(w,codec);
check(missing.creates.size()==4,"expected four never-seen transports, emitted "+std::to_string(missing.creates.size())+
", real-seen="+w.entities.undercity_seen.to_string()+", filled="+w.entities.undercity_filled.to_string());
for(unsigned i=0;i<spawns.size();++i){
auto entry=spawns[i].entry;
if(i==4){
check(!presented.creates.contains(entry) && !missing.creates.contains(entry),"genuine pre-self removal was resurrected");
continue;
}
const auto& batch=i<4?presented:missing;
check(batch.creates.at(entry)==real.creates.at(entry),"pre-self or synthetic replay differed from genuine wire");
}
check(local(w,codec).creates.empty(),"retry resurrected the pre-self removal");
}
void run(Codec codec){
auto full=world();enter(full,codec);
auto real=source(full,codec,received(spawns));check(real.creates.size()==9,"supplied Source stream did not create nine");
before_self(codec,real);
if(full.undercity_settle)full.undercity_settle=Clock::now()-std::chrono::milliseconds(1);
check(!full.undercity_due(),"fully supplied stream scheduled synthetic work");
check(local(full,codec).creates.empty(),"supplied stream duplicated a real transport");
auto gap=world();enter(gap,codec);settle(gap);
auto request_count=gap.requests.size();
auto nine=local(gap,codec);check(nine.creates==real.creates && nine.removed.empty(),"omitted stream differs from genuine type-11 data");
check(local(gap,codec).creates.empty(),"idle retry duplicated synthetic creates");
check(gap.requests.size()==request_count && gap.entities.actors.empty(),"synthetic create inserted a Source actor or query");
cold_templates(gap,codec);
auto late=source(gap,codec,received(std::span(spawns).first(1),0xf120));
check(late.creates.empty() && late.removed.empty(),"late genuine alternate high-type GUID flickered canonical object");
check(!gap.queue_undercity_template(query(spawns.front())),"late genuine object retained synthetic template authority");
auto live_query=gap.gameplay(0x3e0144,query(spawns.front()));
Bytes expected_query;le(expected_query,spawns.front().entry,4);le(expected_query,source_id(spawns.front(),0xf120),8);
check(live_query.opcode==0x5e && live_query.body==expected_query,"adopted real query lost its Source GUID");
auto variant=source(gap,codec,received(std::span(spawns).subspan(1,1),0xf120,23),23);
auto stale=forever_game_object_guid(source_id(spawns[1]),spawns[1].entry,realm,0);
check(variant.creates.contains(spawns[1].entry) && variant.removed==std::vector<Guid128>{stale},
"different real counter did not replace only its provisional object");
check(local(gap,codec).creates.empty(),"late genuine creates triggered another fill");
auto partial=world();enter(partial,codec,lower);
auto four=source(partial,codec,received(std::span(spawns).first(4)));
check(four.creates.size()==4,"partial authoritative stream");
settle(partial);auto five=local(partial,codec);
check(five.creates.size()==5,"partial stream should fill exactly five entries");
for(const auto& [entry,bytes]:five.creates){
check(!four.creates.contains(entry) && real.creates.at(entry)==bytes,"partial creates differ from real path");
}
move_to(partial,exterior);auto clear=local(partial,codec);
std::set<Guid128> retired(clear.removed.begin(),clear.removed.end()),expected;
for(size_t i=4;i<spawns.size();++i)expected.insert(forever_game_object_guid(source_id(spawns[i]),spawns[i].entry,realm,0));
check(retired==expected && clear.removed.size()==5 && clear.creates.empty(),"region exit must retire just the five supplied by the gateway");
check(!partial.queue_undercity_template(query(spawns[4])),"exited region served a cold template");
enter(partial,codec,lower);settle(partial);check(local(partial,codec).creates==five.creates,"reentry must resettle and preserve real observations");
auto unknown=world();check(local(unknown,codec).creates.empty() && !unknown.undercity_settle,"location/roster XYZ alone armed a fallback");
for(uint32_t map:{0u,1u}){
auto outside=world(map);move_to(outside,map?upper:exterior);
check(local(outside,codec).creates.empty() && !outside.undercity_settle,"outside UC region or wrong map armed a fallback");
}
auto logout=source(partial,codec,{0x4d,{}});
check(logout.creates.empty() && !partial.undercity_settle,"logout retained transport deadline");
check(local(partial,codec).creates.empty(),"logged-out session created transports");
}
}
int main(int argc,char** argv){
try{
check(argc==2,"expected a reviewed 70334 schema codec path");
if(!std::filesystem::is_regular_file(argv[1])){std::cerr<<"Missing schema codec: "<<argv[1]<<'\n';return 77;}
void* lib=dlopen(argv[1],RTLD_NOW|RTLD_LOCAL);if(!lib)throw Error(dlerror());
auto codec=reinterpret_cast<Codec>(dlsym(lib,"gw_codec_serialize"));check(codec,"schema codec lacks gw_codec_serialize");
run(codec);dlclose(lib);
std::cout<<"PASS Undercity elevators and doors: 9 missing, 0 supplied, 5 partial; type-11 bytes, phase and scope/lifetime\n";
return 0;
}catch(const std::exception& ex){std::cerr<<ex.what()<<'\n';return 1;}
}