90 lines
5.2 KiB
C++
90 lines
5.2 KiB
C++
#include "optional_test_codec.hpp"
|
|
#include "entity_translation.hpp"
|
|
#include "serializer.h"
|
|
#include "gateway/turtle_entity_profile.hpp"
|
|
#include <bit>
|
|
#include <cstdlib>
|
|
#include <dlfcn.h>
|
|
#include <source_location>
|
|
#include <vector>
|
|
using namespace gateway;
|
|
|
|
static void check(bool ok,const std::source_location& at=std::source_location::current()){
|
|
if(!ok)throw Error("spirit service visibility fixture at line "+std::to_string(at.line()));
|
|
}
|
|
static Bytes packed(uint64_t guid){Bytes b;le(b,guid,8);return pack_guid(b);}
|
|
static LegacyPacket creature(uint64_t guid,uint32_t entry,uint32_t npc_flags,uint8_t type=3){
|
|
std::map<uint16_t,uint32_t> fields{{3,entry},{4,std::bit_cast<uint32_t>(1.f)},{22,80},{28,80},
|
|
{34,1},{35,1},{36,1u<<8|1},{131,19723},{132,19723}};
|
|
if(npc_flags)fields[turtle_entity_fields::UNIT_NPC_FLAGS]=npc_flags;
|
|
Bytes b;le(b,1,4);le(b,0,1);le(b,3,1);append(b,packed(guid));le(b,type,1);le(b,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);
|
|
std::vector<uint32_t> mask(fields.rbegin()->first/32+1);
|
|
for(auto [index,value]:fields){(void)value;mask[index/32]|=1u<<(index%32);}
|
|
le(b,mask.size(),1);for(auto word:mask)le(b,word,4);
|
|
for(auto [index,value]:fields){(void)index;le(b,value,4);}return {0xa9,std::move(b)};
|
|
}
|
|
static LegacyPacket metadata(uint32_t entry){
|
|
Bytes b;le(b,entry,4);cstring(b,"Source NPC");for(unsigned i=0;i<3;++i)cstring(b,"");cstring(b,"Service");
|
|
for(auto value:{0u,7u,0u,0u,0u,0u,19723u})le(b,value,4);le(b,0,1);le(b,0,1);return {0x61,std::move(b)};
|
|
}
|
|
static LegacyPacket values(uint64_t guid,uint16_t field,uint32_t value){
|
|
Bytes b;le(b,1,4);le(b,0,1);le(b,0,1);append(b,packed(guid));
|
|
le(b,field/32+1,1);for(unsigned i=0;i<field/32;++i)le(b,0,4);le(b,1u<<(field%32),4);le(b,value,4);
|
|
return {0xa9,std::move(b)};
|
|
}
|
|
static LegacyPacket remove(uint64_t guid){
|
|
Bytes b;le(b,1,4);le(b,0,1);le(b,4,1);le(b,1,4);append(b,packed(guid));return {0xa9,std::move(b)};
|
|
}
|
|
static unsigned blocks(const std::vector<LegacyPacket>& packets,bool creates){
|
|
unsigned n=0;for(const auto& packet:packets)if(packet.opcode==0x5e0000){
|
|
Reader r(packet.body);r.le(2);auto count=r.le(4);
|
|
if(creates && count && packet.body.size()>11 && packet.body[11]==2)n+=count;
|
|
if(!creates && !count)n++;
|
|
}return n;
|
|
}
|
|
int main(){
|
|
if (test_fixture::optional_codec_missing(GW_ENTITY_CODEC)) return 77;
|
|
void* library = dlopen(GW_ENTITY_CODEC, RTLD_NOW | RTLD_LOCAL);
|
|
if (!library) {
|
|
std::cerr << dlerror() << '\n';
|
|
return 1;
|
|
}
|
|
auto encode=reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(library,"gw_codec_serialize"));check(encode!=nullptr);
|
|
NativeEntityTranslation::Codec codec=[&](uint32_t mode,const Bytes& input){
|
|
uint32_t n=0;check(encode(mode,input.data(),uint32_t(input.size()),0,nullptr,0,&n)==GW_CODEC_CAPACITY);
|
|
Bytes output(n);check(encode(mode,input.data(),uint32_t(input.size()),0,output.data(),n,&n)==GW_CODEC_OK);
|
|
output.resize(n);return output;
|
|
};
|
|
Character self{};self.guid=4509;self.name="Fixture";self.race=1;self.cls=1;self.gender=0;self.zone=12;
|
|
constexpr uint64_t healer=(0xf130ULL<<48)|(6491ULL<<24)|17;
|
|
constexpr uint64_t special=(0xf130ULL<<48)|(61303ULL<<24)|18;
|
|
NativeEntityTranslation state;
|
|
auto run=[&](const LegacyPacket& packet,bool ghost=false){return state.translate(packet,self,70,0x46010046,0,codec,ghost);};
|
|
run(creature(self.guid,1,0,4));
|
|
// The source GM mode may send 6491 while alive. It must stay out of the
|
|
// native client, whereas a named spirit NPC without service flags appears.
|
|
check(run(creature(healer,6491,0x21)).empty());
|
|
check(blocks(run(metadata(6491)),true)==0 && !state.actors.at(healer).sent);
|
|
check(run(creature(special,61303,1)).empty());
|
|
check(blocks(run(metadata(61303)),true)==1 && state.actors.at(special).sent);
|
|
check(run(values(healer,22,42)).empty());
|
|
check(state.actors.at(healer).pending_values.at(22)==42);
|
|
// A genuine self ghost transition can reveal the retained create without
|
|
// asking the source for a duplicate GM-visible create.
|
|
auto ghost=run(values(self.guid,turtle_entity_fields::PLAYER_FLAGS,16),true);
|
|
check(blocks(ghost,true)==1 && state.actors.at(healer).sent);
|
|
check(state.actors.at(healer).pending_values.empty());
|
|
auto alive=run(values(self.guid,turtle_entity_fields::PLAYER_FLAGS,0));
|
|
check(blocks(alive,false)==1 && !state.actors.at(healer).sent && state.actors.at(special).sent);
|
|
check(blocks(run(values(self.guid,turtle_entity_fields::PLAYER_FLAGS,16),true),true)==1);
|
|
check(blocks(run(values(self.guid,turtle_entity_fields::PLAYER_FLAGS,0)),false)==1);
|
|
// A source GM-off removal while alive discards the retained create. Later
|
|
// ghost state cannot resurrect an actor no longer in source visibility.
|
|
auto removed=run(remove(healer));check(blocks(removed,false)==0 && !state.actors.contains(healer));
|
|
check(blocks(run(values(self.guid,turtle_entity_fields::PLAYER_FLAGS,16),true),true)==0);
|
|
check(state.retained_create_bytes==0 && state.retained_create_refs.empty());
|
|
return 0;
|
|
}
|