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

462 lines
28 KiB
C++

#include "optional_test_codec.hpp"
#include "entity_translation.hpp"
#include "world_translation.hpp"
#include <algorithm>
#include "serializer.h"
#include "gateway/chat.hpp"
#include "gateway/object_updates.hpp"
#include "gateway/turtle_entity_profile.hpp"
#include "gateway/world.hpp"
#include <bit>
#include <cstdlib>
#include <dlfcn.h>
#include <iostream>
#include <source_location>
#include <zlib.h>
using namespace gateway;
static void expect(bool value,std::source_location at=std::source_location::current()){
if(!value)throw Error("peer pipeline fixture assertion at source line"+std::to_string(at.line()));
}
static Bytes packed(uint64_t guid){Bytes b;le(b,guid,8);return pack_guid(b);}
static LegacyPacket source_create(uint32_t entry,uint64_t guid,uint8_t cls=1,uint8_t kind=3,const std::map<uint16_t,uint32_t>& extra={},std::optional<uint64_t> melee={}){
std::map<uint16_t,uint32_t> f{{3,entry},{4,std::bit_cast<uint32_t>(1.f)},{22,80},{28,80},{34,1},{35,1},{36,uint32_t(cls)<<8|1},{131,19723},{132,19723}};
if(kind==5)f[21]=11; // Unsupported transport: must remain visible in diagnostics, not be serialized as a static GO.
for(auto [index,value]:extra)f[index]=value;
Bytes b;le(b,1,4);le(b,0,1);le(b,3,1);append(b,packed(guid));le(b,kind,1);le(b,melee?0x24:0x20,1);le(b,0,4);le(b,100,4);
for(float v:{1.f,2.f,3.f,0.f})le(b,std::bit_cast<uint32_t>(v),4);le(b,0,4);
for(float v:{2.5f,7.f,4.5f,4.72f,2.5f,3.14f})le(b,std::bit_cast<uint32_t>(v),4);
if(melee)append(b,packed(*melee));
std::vector<uint32_t> mask(f.rbegin()->first/32+1);for(auto [index,value]:f){(void)value;mask[index/32]|=uint32_t(1)<<(index%32);}
le(b,mask.size(),1);for(auto word:mask)le(b,word,4);for(auto [index,value]:f){(void)index;le(b,value,4);}return {0xa9,b};
}
static LegacyPacket peer(uint64_t guid){
return source_create(0,guid,1,4,{{36,0x101},{131,49},{132,49}});
}
static LegacyPacket name(uint64_t guid,std::string_view value="Peer"){
Bytes b;le(b,guid,8);cstring(b,std::string(value));cstring(b,"");
le(b,1,4);le(b,0,4);le(b,1,4);return {0x51,b};
}
static LegacyPacket value(uint64_t guid,uint16_t field,uint32_t v){
Bytes b;le(b,1,4);le(b,0,1);le(b,0,1);append(b,packed(guid));
auto words=field/32+1;le(b,words,1);
for(unsigned i=0;i<words;++i)le(b,i==field/32?uint32_t(1)<<(field%32):0,4);
le(b,v,4);return {0xa9,b};
}
static LegacyPacket remove(uint64_t guid){Bytes b;le(b,guid,8);return {0xaa,b};}
static LegacyPacket motion(uint64_t guid,uint16_t opcode){
Bytes b=packed(guid);le(b,opcode==0xc3?0x100u:0u,4);le(b,123,4);
for(float f:{1.f,2.f,3.f,0.f})le(b,std::bit_cast<uint32_t>(f),4);
le(b,0,4);return {opcode,b};
}
static LegacyPacket compressed(const LegacyPacket& packet){
Bytes records;le(records,packet.body.size()+2,1);le(records,packet.opcode,2);append(records,packet.body);
uLongf length=compressBound(records.size());Bytes z(length);
expect(compress2(z.data(),&length,records.data(),records.size(),Z_DEFAULT_COMPRESSION)==Z_OK);z.resize(length);
Bytes b;le(b,records.size(),4);append(b,z);return {0x2fb,b};
}
static LegacyPacket source_tail_fixture(uint64_t guid,uint32_t flags,bool bad_pitch=false){
auto packet=peer(guid);Reader r(packet.body);r.le(4);r.le(1);r.le(1);
auto mask=r.le(1);for(unsigned i=0;i<8;++i)if(mask&(1u<<i))r.le(1);
r.le(1);r.le(1);auto flag_offset=packet.body.size()-r.left();
r.le(4);r.le(4);r.take(16);auto fall_offset=packet.body.size()-r.left();r.le(4);
Bytes out(packet.body.begin(),packet.body.begin()+flag_offset);le(out,flags,4);
append(out,Bytes(packet.body.begin()+flag_offset+4,packet.body.begin()+fall_offset));
if(flags&0x00200000)le(out,bad_pitch?0x7fc00000u:std::bit_cast<uint32_t>(0.25f),4);
le(out,499,4);
if(flags&0x2000)for(float f:{7.1f,0.8f,0.6f,4.2f})le(out,std::bit_cast<uint32_t>(f),4);
if(flags&0x04000000)le(out,std::bit_cast<uint32_t>(0.5f),4);
append(out,Bytes(packet.body.begin()+fall_offset+4,packet.body.end()));return {packet.opcode,out};
}
struct Spy{
uint32_t creates=0,updates=0,moves=0;uint64_t last_guid=0;
std::vector<uint32_t> health;
Bytes operator()(uint32_t mode,const Bytes& input){
Reader r(input);auto realm=r.le(2);expect(realm==70);
if(mode==GW_CODEC_PEER_MOVEMENT){
last_guid=r.le(8);auto opcode=r.le(2);
expect(NativeEntityTranslation::peer_movement_opcode(uint16_t(opcode)));expect(r.left()>0);
++moves;return Bytes{1};
}
expect(mode==GW_CODEC_PLAYERS || mode==GW_CODEC_PLAYER_VALUES);
expect(r.le(4)==0x46010046 && r.le(4)==1 && r.le(2)==1);
last_guid=r.le(8);expect(last_guid!=4509);expect(r.cstring(63)=="Peer");
auto count=r.le(2);std::map<uint16_t,uint32_t> fields;uint16_t last=0;
for(unsigned i=0;i<count;++i){auto field=uint16_t(r.le(2));expect(i==0 || field>last);last=field;
expect(fields.emplace(field,uint32_t(r.le(4))).second);}
auto opcode=uint16_t(r.le(2));auto updates_source=turtle_object_updates(opcode,r.take(r.left()));
bool found=false;
for(const auto& update:updates_source)if(update.guid==last_guid){
expect(mode==GW_CODEC_PLAYERS?(update.kind==2 || update.kind==3):update.kind==0);
for(auto [field,v]:update.fields)expect(fields.contains(field) && fields.at(field)==v);
found=true;
}
expect(found);if(fields.contains(22))health.push_back(fields.at(22));
if(mode==GW_CODEC_PLAYERS)++creates;else ++updates;
Bytes out;le(out,1,2);le(out,1,4);return out; // Caller contract only; real writer separately tested.
}
};
static Character self(){Character c{};c.guid=4509;c.name="Self";c.race=1;c.cls=1;return c;}
static std::vector<LegacyPacket> run(NativeEntityTranslation& state,const LegacyPacket& packet,Spy& spy){
// NativeWorldTranslation owns the observation boundary before entity routing.
// Reproduce that public caller contract; translate() does not self-observe.
if(packet.opcode==0xa9 || packet.opcode==0x1f6){
++state.source_packets;
state.observe_source_fields(turtle_object_updates(packet.opcode,packet.body),self().guid,uint16_t(packet.opcode));
}
return state.translate(packet,self(),70,0x46010046,1,[&](uint32_t mode,const Bytes& input){return spy(mode,input);});
}
// Exercise the real world dispatcher and C ABI adapter, not just entities.translate.
static Spy* world_spy=nullptr;
static Bytes world_spy_result;
static uint32_t world_spy_mode=0;
static Bytes world_spy_input;
static int world_codec(uint32_t mode,const uint8_t* input,uint32_t size,uint32_t flags,
uint8_t* output,uint32_t capacity,uint32_t* written){
expect(world_spy && written && flags==0);
Bytes request(input,input+size);
if(!output){world_spy_mode=mode;world_spy_input=request;world_spy_result=(*world_spy)(mode,request);
*written=uint32_t(world_spy_result.size());return GW_CODEC_CAPACITY;}
expect(mode==world_spy_mode && request==world_spy_input);
*written=uint32_t(world_spy_result.size());if(capacity<*written)return GW_CODEC_CAPACITY;
std::copy(world_spy_result.begin(),world_spy_result.end(),output);return GW_CODEC_OK;
}
static std::vector<LegacyPacket> world_packets(const Bytes& bytes){
Reader r(bytes);auto n=r.le(4);expect(n<=1024);std::vector<LegacyPacket> packets;
for(unsigned i=0;i<n;++i){auto opcode=uint32_t(r.le(4));auto size=r.le(4);packets.push_back({opcode,r.take(size)});}
r.end();return packets;
}
static void real_world_dispatch(){
NativeWorldTranslation world{};world.selected=self();world.realm=70;world.location=WorldLocation{1,{0,0,0,0}};world.self_created=true;
Spy spy;world_spy=&spy;uint64_t clock=100;constexpr uint64_t guid=9191;
auto dispatch=[&](const LegacyPacket& packet){return world_packets(world.translate(packet,0x46010046,clock++,&world_codec));};
expect(dispatch(peer(guid)).empty());auto queries=std::move(world.entities.requests);world.entities.requests.clear();
expect(queries.size()==1 && queries[0].opcode==0x50 && spy.creates==0);
// No chat request is pending: this name reply must still reach peer creation.
expect(!world.chat_name_queries.contains(guid));
auto created=dispatch(name(guid));
auto public_create=[](const auto& packets){return std::count_if(packets.begin(),packets.end(),[](const auto& p){return p.opcode==0x5e0000 && p.body==Bytes{1,0,1,0,0,0};})==1;};
// The real caller may also emit its legitimate initial public target clear.
expect(spy.creates==1 && public_create(created));
expect(world.entities.actors.at(guid).presentation_realm==70 && world.entities.actors.at(guid).presentation_map==1);
auto speed=motion(guid,0xb7);speed.opcode=0xcd;le(speed.body,std::bit_cast<uint32_t>(3.5f),4);
auto observer_speed=dispatch(speed);expect(observer_speed.size()==1 && observer_speed[0].opcode==0x600004);
speed.body=packed(guid);le(speed.body,std::bit_cast<uint32_t>(3.5f),4);
constexpr std::pair<uint16_t,uint32_t> spline_speeds[]={
{0x2fe,0x600015},{0x2ff,0x600016},{0x300,0x600017},
{0x301,0x60001d},{0x302,0x600018},{0x303,0x60001e}
};
for(auto [source_opcode,native_opcode]:spline_speeds){
speed.opcode=source_opcode;
auto observed=dispatch(speed);
expect(observed.size()==1 && observed.front().opcode==native_opcode);
}
const uint16_t regular[]={0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xc1,0xc2,0xc3,0xc9,0xca,0xcb,0xda,0xdb,0xee,0x2ca};
for(auto opcode:regular){auto before=spy.moves;auto moved=dispatch(motion(guid,opcode));
expect(spy.moves==before+1 && moved.size()==1 && moved[0].opcode==0x60000e);}
for(auto opcode:{uint16_t(0xdc),uint16_t(0xf3)}){auto before=spy.moves;bool rejected=false;
try{dispatch(motion(guid,opcode));}catch(const Error&){rejected=true;}
expect(spy.moves==before);if(opcode==0xf3)expect(rejected); // Existing controller parser rejects broadcast-shaped control body.
}
auto before=spy.moves;dispatch(motion(999999,0xb5));expect(spy.moves==before);
// A single source reply can independently satisfy both legitimate consumers.
constexpr uint64_t both=9192;dispatch(peer(both));world.entities.requests.clear();world.chat_name_queries.insert(both);
auto shared=dispatch(name(both));expect(spy.creates==2 && public_create(shared) && world.chat_players.contains(both));
auto changed=dispatch(value(guid,22,123));expect(spy.updates==1 && !changed.empty());
// Peer GuildGUID already reaches the native player writer. Its original
// metadata request must work without granting the local player membership.
dispatch(value(guid,turtle_entity_fields::PLAYER_GUILDID,42));
expect(world.guild_id==0 && world.player_guild(guid)==42);
auto query_body=[&](uint32_t guild,uint64_t player){
auto guild_guid=forever_guild_guid(guild,70);
Bytes result=pack_guid(Bytes(guild_guid.begin(),guild_guid.end()));
if(player){auto player_guid=forever_player_guid(player,70);append(result,pack_guid(Bytes(player_guid.begin(),player_guid.end())));}
else append(result,pack_guid(Bytes(16)));
return result;
};
auto request=world.gameplay(0x4400b6,query_body(42,guid));
Reader source_query(request.body);expect(request.opcode==0x54 && source_query.le(4)==42);source_query.end();
expect(world.gameplay(0x4400b6,query_body(42,0)).body==request.body);
auto rejects=[&](uint32_t guild,uint64_t player){try{world.gameplay(0x4400b6,query_body(guild,player));return false;}catch(const Error&){return true;}};
expect(rejects(99,guid) && rejects(42,999999));
expect(world.gameplay(0x4400b6,query_body(42,world.selected.guid)).body==request.body);
Bytes info;le(info,42,4);cstring(info,"NearbyGuild");
for(unsigned i=0;i<10;++i)cstring(info,i?"Member":"Master");for(unsigned i=0;i<5;++i)le(info,i+1,4);
auto response=dispatch({0x55,info});expect(response.size()==1 && response.front().opcode==0x53002c);
auto foreign=info;foreign[0]=99;expect(dispatch({0x55,foreign}).empty());
dispatch(value(guid,turtle_entity_fields::PLAYER_GUILDID,0));expect(rejects(42,guid) && dispatch({0x55,info}).empty());
dispatch(value(guid,turtle_entity_fields::PLAYER_GUILDID,42));
++world.entities.actors.at(guid).generation;expect(rejects(42,guid));--world.entities.actors.at(guid).generation;
world.entities.actors.at(guid).presentation_map=2;expect(rejects(42,guid));world.entities.actors.at(guid).presentation_map=1;
auto queued=world.requests.size();
dispatch(value(both,turtle_entity_fields::PLAYER_GUILDID,42));
expect(world.requests.size()==queued); // Shared guild metadata remains original-cache driven.
auto removed=dispatch(remove(guid));expect(!removed.empty());before=spy.moves;dispatch(motion(guid,0xb5));expect(spy.moves==before);
expect(rejects(42,guid)); // A removed peer cannot supply the native player identity.
expect(!dispatch({0x55,info}).empty()); // Another live member still establishes this public guild.
dispatch(remove(both));expect(dispatch({0x55,info}).empty() && rejects(42,0));
world_spy=nullptr;
}
// Drive the two real source observers with the same name reply and an original
// two-member raid. A completed peer query can precede or follow GROUP_LIST.
static void shared_party_identity(bool reply_first, decltype(&gw_codec_serialize) codec) {
constexpr uint64_t guid=9191;
NativeWorldTranslation world{};
world.selected=self();world.realm=70;world.location=WorldLocation{1,{}};world.self_created=true;
auto dispatch=[&](LegacyPacket p){return world_packets(world.translate(p,0x46010046,0,codec));};
auto count=[](const auto& events,uint32_t op){
return std::count_if(events.begin(),events.end(),[&](const auto& p){return p.opcode==op;});
};
Bytes roster{1,0};le(roster,1,4);cstring(roster,"Peer");le(roster,guid,8);
le(roster,1,1);le(roster,0,1);le(roster,guid,8); // Connected peer leads the raid.
le(roster,0,1);le(roster,0,8);le(roster,2,1);le(roster,0,1);
auto visible=source_create(0,guid,1,4,{{0,uint32_t(guid)},{2,0x19},{36,0x101},{131,49},{132,49}});
expect(dispatch(visible).empty());
expect(world.entities.requests.size()==1 && world.entities.requests.front().opcode==0x50);
Reader query(world.entities.requests.front().body);expect(query.le(8)==guid);query.end();
world.entities.requests.pop_front();
std::vector<LegacyPacket> events;
if(reply_first) {
auto first=dispatch(name(guid));
expect(count(first,0x5e0000)>=1 && world.entities.actors.at(guid).sent && count(first,0x470097)==0);
expect(world.entities.peer_players.at(guid).named);
events=dispatch({0x7d,roster});
} else {
auto first=dispatch({0x7d,roster});
expect(first.empty() && world.party.pending && !world.party.shown);
expect(count(world.requests,0x50)==0 && world.entities.peer_name_queries.contains(guid));
events=dispatch(name(guid));
expect(count(events,0x5e0000)>=1 && world.entities.actors.at(guid).sent);
}
if(count(events,0x470097)!=1 || count(world.requests,0x50) || !world.party.shown || world.party.pending)
throw Error(std::string(reply_first?"peer reply before GROUP_LIST":"peer reply after GROUP_LIST")+
": PartyUpdate="+std::to_string(count(events,0x470097))+
", extra source name queries="+std::to_string(count(world.requests,0x50))+
", held="+std::to_string(world.party.pending));
const auto packet=std::find_if(events.begin(),events.end(),[](const auto& p){return p.opcode==0x470097;});
Reader update(packet->body);
expect(update.le(2)==2 && update.le(1)==0 && update.le(1)==1 && update.le(4)==0);
(void)unpack_guid(update,16);expect(update.le(4)==1 && update.le(1)==1);
const auto leader=unpack_guid(update,16);
const auto expected=forever_player_guid(guid,world.realm);
expect(std::equal(leader.begin(),leader.end(),expected.begin()));
expect(update.le(1)==3);update.le(4);expect(update.le(8)==0 && update.le(4)==2);
expect(world.entities.peer_players.at(guid).named && world.entities.actors.at(guid).sent);
expect(world.entities.peer_name_queries.empty() && world.chat_name_queries.empty());
}
static void peer_melee_visibility(decltype(&gw_codec_serialize) codec) {
NativeWorldTranslation world{};
world.selected=self();
world.realm=70;
world.location=WorldLocation{1,{}};
world.self_created=true;
world.entities.self_presentation_ready=true;
auto dispatch=[&](const LegacyPacket& packet){
return world_packets(world.translate(packet,0x46010046,0,codec));
};
auto create=[](uint64_t guid,std::optional<uint64_t> victim={}) {
return source_create(0,guid,1,4,{{0,uint32_t(guid)},{2,0x19}},victim);
};
auto attack=[](uint64_t guid,uint64_t victim,bool stop=false) {
Bytes body;
if(stop){
append(body,packed(guid));append(body,packed(victim));le(body,0,4);
}else{
le(body,guid,8);le(body,victim,8);
}
return LegacyPacket{stop?0x144u:0x143u,std::move(body)};
};
auto movement_victim=[](uint64_t guid,uint64_t victim) {
Bytes body;
le(body,1,4);le(body,0,1);le(body,1,1);le(body,guid,8);
le(body,4,1);append(body,packed(victim));
return LegacyPacket{0xa9,std::move(body)};
};
auto quiet=[](const auto& events){
return std::none_of(events.begin(),events.end(),[](const auto& p){return p.opcode==0x4d0017 || p.opcode==0x4d0018;});
};
auto check=[&](const auto& events,uint64_t guid,uint64_t victim,bool stop,uint64_t created_peer) {
const auto op=stop?0x4d0018u:0x4d0017u;
auto at=std::find_if(events.begin(),events.end(),[&](const auto& p){return p.opcode==op;});
expect(at!=events.end() && std::count_if(events.begin(),events.end(),[&](const auto& p){
return p.opcode==0x4d0017 || p.opcode==0x4d0018;
})==1);
if(created_peer){
const auto expected=forever_player_guid(created_peer,70);
expect(std::any_of(events.begin(),at,[&](const auto& p){
if(p.opcode!=0x5e0000)return false;
Reader update(p.body);
if(update.le(2)!=1 || update.le(4)!=1 || update.le(1)!=0x80)return false;
auto size=update.le(4);
if(size!=update.left() || !size || update.le(1)!=2)return false;
auto actual=unpack_guid(update,16);
return std::equal(actual.begin(),actual.end(),expected.begin());
}));
}
Reader fields(at->body);
auto actor_guid=unpack_guid(fields,16),victim_guid=unpack_guid(fields,16);
auto expected_actor=forever_player_guid(guid,70);
auto expected_victim=victim?forever_player_guid(victim,70):Guid128{};
expect(std::equal(actor_guid.begin(),actor_guid.end(),expected_actor.begin()));
expect(std::equal(victim_guid.begin(),victim_guid.end(),expected_victim.begin()));
if(stop)expect(fields.le(1)==0);
fields.end();
};
constexpr uint64_t attacker=9191,victim=9192;
const uint64_t player=world.selected.guid;
expect(quiet(dispatch(create(attacker,player))));
check(dispatch(name(attacker)),attacker,player,false,attacker);
expect(quiet(dispatch(name(attacker))));
check(dispatch(attack(attacker,player,true)),attacker,player,true,0);
// A real event before the peer's name reply supersedes the create state.
expect(quiet(dispatch(create(attacker,player))));
expect(quiet(dispatch(attack(attacker,player,true))));
expect(quiet(dispatch(name(attacker))));
expect(quiet(dispatch(create(attacker))));
expect(quiet(dispatch(attack(attacker,player))));
check(dispatch(name(attacker)),attacker,player,false,attacker);
check(dispatch(attack(attacker,player,true)),attacker,player,true,0);
// The victim must also have completed publication. Retiring its GUID
// cancels that pending relationship even if a peer with that GUID returns.
expect(quiet(dispatch(create(victim))));
expect(quiet(dispatch(attack(attacker,victim))));
check(dispatch(name(victim)),attacker,victim,false,victim);
check(dispatch(attack(attacker,victim,true)),attacker,victim,true,0);
expect(quiet(dispatch(remove(victim))));
expect(quiet(dispatch(create(victim))));
expect(quiet(dispatch(attack(attacker,victim))));
expect(quiet(dispatch(remove(victim))));
expect(quiet(dispatch(create(victim))));
expect(quiet(dispatch(name(victim))));
expect(quiet(dispatch(name(victim))));
// Public movement-only updates can carry an explicit new victim or zero.
check(dispatch(movement_victim(attacker,player)),attacker,player,false,0);
expect(quiet(dispatch(movement_victim(attacker,player))));
check(dispatch(movement_victim(attacker,0)),attacker,0,true,0);
expect(quiet(dispatch(movement_victim(attacker,0))));
expect(quiet(dispatch(create(attacker,player))));
expect(quiet(dispatch(movement_victim(attacker,0))));
expect(quiet(dispatch(name(attacker))));
// A source update may create the peer and change its melee victim in one batch.
auto combined=create(attacker,player);
auto clear=movement_victim(attacker,0);
combined.body[0]=2;
combined.body.insert(combined.body.end(),clear.body.begin()+5,clear.body.end());
expect(quiet(dispatch(combined)));
expect(quiet(dispatch(name(attacker))));
// Ordinary target selection and broad combat flags carry no melee authority.
expect(quiet(dispatch(value(attacker,turtle_entity_fields::UNIT_FIELD_TARGET,uint32_t(player)))));
expect(quiet(dispatch(value(attacker,turtle_entity_fields::UNIT_FIELD_FLAGS,0x80000u))));
check(dispatch(attack(attacker,player)),attacker,player,false,0);
check(dispatch(attack(attacker,player,true)),attacker,player,true,0);
std::cout<<"PASS published and delayed peer melee, explicit stop, visibility, and target-only controls\n";
}
int main(int argc,char** argv){try{
if(argc==3 && (std::string_view(argv[1])=="--peer-reply-first" || std::string_view(argv[1])=="--roster-first" || std::string_view(argv[1])=="--peer-melee")){
if (test_fixture::optional_codec_missing(argv[2])) return 77;
void* lib = dlopen(argv[2], RTLD_NOW | RTLD_LOCAL);
if (!lib) {
std::cerr << dlerror() << '\n';
return 1;
}
auto codec=reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(lib,"gw_codec_serialize"));expect(codec);
if(std::string_view(argv[1])=="--peer-melee")peer_melee_visibility(codec);
else shared_party_identity(std::string_view(argv[1])=="--peer-reply-first",codec);
dlclose(lib);return 0;
}
real_world_dispatch();
{
// Source MovementInfo ordering, separate from the native codec spy.
for(uint32_t flags:{0x2000u,0x4000u,0x00200000u,0x00202000u,0x04000000u}){
auto packet=source_tail_fixture(5000,flags);
auto parsed=turtle_object_updates(packet.opcode,packet.body);
expect(parsed.size()==1 && parsed[0].movement && parsed[0].basic_movement);
const auto& ground=*parsed[0].movement;const auto& full=*parsed[0].basic_movement;
expect(ground.guid==5000 && ground.flags==flags && full.state.position==ground.position);
expect(full.fall_time==499 && full.has_fall);
expect(full.falling==bool(flags&0x2000) && full.falling_far==bool(flags&0x4000));
if(flags&0x2000)expect(full.has_direction && full.vertical_speed==7.1f &&
full.sin_angle==0.6f && full.cos_angle==0.8f && full.horizontal_speed==4.2f);
if(flags&0x00200000)expect(full.swimming && full.swim_pitch==0.25f);
if(flags&0x04000000)expect(full.has_spline_elevation && full.spline_elevation==0.5f);
}
bool rejected=false;auto invalid=source_tail_fixture(5000,0x00200000,true);
try{(void)turtle_object_updates(invalid.opcode,invalid.body);}catch(const Error&){rejected=true;}
expect(rejected);
}
{
NativeEntityTranslation state;Spy spy;
auto first=run(state,peer(5000),spy);expect(first.empty());
expect(state.actors.at(5000).kind==4 && !state.actors.at(5000).sent && spy.creates==0);
expect(state.requests.size()==1 && state.requests.front().opcode==0x50);
Reader query(state.requests.front().body);expect(query.le(8)==5000);query.end();state.requests.clear();
run(state,value(5000,22,77),spy);expect(spy.updates==0);
auto result=run(state,name(5000),spy);expect(result.size()==2);
expect(spy.creates==1 && spy.updates==1 && spy.health==std::vector<uint32_t>({80,77}));
expect(state.actors.at(5000).sent && state.actors.at(5000).eligible);
expect(state.actors.at(5000).modern==forever_player_guid(5000,70));
expect(state.peer_pending_bytes==0 && !state.peer_players.at(5000).create);
run(state,value(5000,22,70),spy);expect(spy.updates==2 && spy.health.back()==70);
for(uint16_t opcode:{0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,
0xbf,0xc0,0xc1,0xc2,0xc3,0xc9,0xca,0xcb,0xda,0xdb,0xee,0x2ca}){
auto movement=run(state,motion(5000,opcode),spy);expect(movement.size()==1 && movement[0].opcode==0x60000e);
}
expect(spy.moves==22);
for(uint16_t opcode:{0xdc,0xf3}){
expect(!NativeEntityTranslation::peer_movement_opcode(opcode));
auto held=run(state,motion(5000,opcode),spy);expect(held.empty() && spy.moves==22);
}
auto rejected=state.motion_rejected;auto zipped=run(state,compressed(motion(5000,0xb8)),spy);
expect(zipped.size()==1 && spy.moves==23 && state.motion_rejected==rejected);
run(state,motion(5001,0xb5),spy);expect(spy.moves==23);
auto before_equipment=spy.updates;
run(state,value(5000,turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0,47000),spy);
expect(spy.updates==before_equipment+1 && state.peer_players.at(5000).fields.at(turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0)==47000);
auto removal=run(state,remove(5000),spy);expect(removal.size()==1);
expect(!state.actors.contains(5000) && !state.peer_players.contains(5000));
run(state,motion(5000,0xb5),spy);expect(spy.moves==23);
}
{
NativeEntityTranslation state;Spy spy;
run(state,peer(5000),spy);auto old=state.peer_players.at(5000).generation;state.requests.clear();
run(state,remove(5000),spy);run(state,peer(5000),spy);
expect(state.peer_players.at(5000).generation!=old && state.requests.empty());
run(state,name(5000),spy);expect(spy.creates==0 && state.requests.size()==1);
run(state,name(5000),spy);expect(spy.creates==1);
auto recreated=run(state,peer(5000),spy);expect(recreated.size()==1 && !state.actors.at(5000).sent);
expect(state.requests.size()==2);run(state,name(5000),spy);expect(spy.creates==2);
}
{
NativeEntityTranslation state;Spy spy;
run(state,peer(5000),spy);run(state,motion(5000,0xbb),spy);expect(spy.moves==0);
auto result=run(state,name(5000),spy);expect(result.size()==2 && spy.moves==1);
expect(state.peer_players.at(5000).movement==std::nullopt);
run(state,peer(5001),spy);auto wrong=name(5001);wrong.body[wrong.body.size()-4]=2;
run(state,wrong,spy);expect(!state.actors.at(5001).sent && state.peer_players.at(5001).blocked);
expect(!NativeEntityTranslation::peer_movement_opcode(0xc5));
expect(!NativeEntityTranslation::peer_movement_opcode(0xdead));
}
{
NativeEntityTranslation state;Spy spy;
run(state,peer(5000),spy);run(state,name(5000),spy);
Bytes b;le(b,1,4);le(b,0,1);le(b,4,1);le(b,1,4);append(b,packed(5000));
auto removed=run(state,{0xa9,b},spy);expect(removed.size()==1);
expect(!state.actors.contains(5000) && !state.peer_players.contains(5000));
}
{
NativeEntityTranslation state;Spy spy;
run(state,peer(5000),spy);run(state,name(5000),spy);
auto observed=state.source_fields.size();run(state,value(5000,22,66),spy);
expect(state.source_fields.size()>observed);
expect(state.source_fields.back().legacy==5000 && state.source_fields.back().value==66);
auto snapshot=state.snapshot();Reader r(snapshot);expect(r.le(4)==3);r.le(4);
for(unsigned i=0;i<8;++i)r.le(4);expect(r.le(4)==1);
}
return EXIT_SUCCESS;
}catch(const std::exception& error){std::cerr<<error.what()<<'\n';return EXIT_FAILURE;}}