183 lines
13 KiB
C++
183 lines
13 KiB
C++
#include "entity_translation.hpp"
|
|
#include "serializer.h"
|
|
#include "gateway/turtle_entity_profile.hpp"
|
|
#include "gateway/gameobject_queries.hpp"
|
|
#include <bit>
|
|
#include <dlfcn.h>
|
|
#include <filesystem>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <source_location>
|
|
#ifdef GW_GAMEOBJECT_AUTHORITY_PROFILE_HEADER
|
|
#include GW_GAMEOBJECT_AUTHORITY_PROFILE_HEADER
|
|
#endif
|
|
using namespace gateway;
|
|
static void expect(bool ok,const std::source_location& at=std::source_location::current()){
|
|
if(!ok)throw Error("stationary GO batch assertion line "+std::to_string(at.line()));
|
|
}
|
|
static Bytes packed(uint64_t guid){Bytes raw;le(raw,guid,8);return pack_guid(raw);}
|
|
static LegacyPacket update(uint64_t guid,const std::map<uint16_t,uint32_t>& fields,bool create=false){
|
|
Bytes b;le(b,1,4);le(b,0,1);le(b,create?3:0,1);append(b,packed(guid));
|
|
if(create){le(b,5,1);le(b,0x40,1);for(float f:{1.f,2.f,3.f,0.f})le(b,std::bit_cast<uint32_t>(f),4);}
|
|
std::vector<uint32_t> mask(fields.rbegin()->first/32+1);for(auto [i,v]:fields){(void)v;mask[i/32]|=1u<<(i%32);}
|
|
le(b,mask.size(),1);for(auto word:mask)le(b,word,4);for(auto [i,v]:fields){(void)i;le(b,v,4);}return {0xa9,b};
|
|
}
|
|
static Bytes block_payload(const Bytes& packet,bool create,const Guid128& expected){
|
|
Reader r(packet);expect(r.le(2)==0 && r.le(4)==1 && r.le(1)==0x80);auto blocks=r.take(r.le(4));r.end();Reader b(blocks);
|
|
expect(b.le(1)==(create?2:0));auto guid=unpack_guid(b,16);expect(std::equal(guid.begin(),guid.end(),expected.begin()));
|
|
if(create){expect(b.le(1)==8);b.take(3);b.le(4);b.take(16);b.le(8);}
|
|
auto values=b.take(b.le(4));b.end();return values;
|
|
}
|
|
static uint32_t mask28(Reader& r){auto bytes=r.take(4);uint32_t out=0;for(unsigned i=0;i<28;++i)out=(out<<1)|((bytes[i/8]>>(7-i%8))&1);return out;}
|
|
int main(int argc, char** argv){try{
|
|
if (argc != 2)
|
|
throw Error("expected the configured schema codec path");
|
|
if (!std::filesystem::exists(argv[1])) {
|
|
std::cerr << "Optional schema codec is missing at " << argv[1] << '\n';
|
|
return 77;
|
|
}
|
|
void* library = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);
|
|
if (!library)
|
|
throw Error(dlerror());
|
|
auto encode = reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(library, "gw_codec_serialize"));
|
|
if (!encode)
|
|
throw Error("schema codec lacks gw_codec_serialize");
|
|
const auto writer = [encode](uint32_t mode, const Bytes& body) {
|
|
uint32_t required = 0;
|
|
expect(encode(mode,body.data(),uint32_t(body.size()),0,nullptr,0,&required)==GW_CODEC_CAPACITY);
|
|
Bytes result(required);
|
|
expect(encode(mode,body.data(),uint32_t(body.size()),0,result.data(),uint32_t(result.size()),&required)==GW_CODEC_OK && result.size()==required);
|
|
return result;
|
|
};
|
|
constexpr uint32_t entry=9010;constexpr uint64_t guid=(uint64_t(0xf110)<<48)|(uint64_t(entry)<<24)|19;
|
|
Character self{};self.guid=4520;self.race=1;self.cls=1;self.gender=0;
|
|
const auto native=forever_game_object_guid(guid,entry,70,0);
|
|
for(auto type:{0u,1u,10u,23u,25u}){
|
|
NativeEntityTranslation entities;
|
|
std::map<uint16_t,uint32_t> fields{{3,entry},{4,std::bit_cast<uint32_t>(1.25f)},{8,310},{9,0x43},{13,std::bit_cast<uint32_t>(1.f)},{14,1},{21,type},{23,7}};
|
|
auto created=entities.translate(update(guid,fields,true),self,70,0x10046,0,writer);expect(created.size()==1 && created[0].opcode==0x5e0000);
|
|
auto create_bytes=block_payload(created[0].body,true,native);Reader values(create_bytes);values.take(5);expect(values.le(4)==entry);values.le(4);expect(values.le(4)==std::bit_cast<uint32_t>(1.25f));
|
|
expect(values.le(4)==310);values.take(24);unpack_guid(values,16);unpack_guid(values,16);
|
|
expect(values.le(4)==3); // Source TRIGGERED0x40 never becomes native AI_OBSTACLE.
|
|
values.le(4);values.take(16);values.le(4);values.le(4);expect(values.le(1)==1 && values.le(1)==type && values.le(1)==100 && values.le(4)==7);
|
|
auto changed=entities.translate(update(guid,{{9,0x10},{14,0},{23,0}}),self,70,0x10046,0,writer);
|
|
expect(changed.size()==1);auto sparse_bytes=block_payload(changed[0].body,false,native);Reader sparse(sparse_bytes);
|
|
expect(sparse.le(1)==0 && sparse.le(1)==0 && sparse.le(1)==3 && sparse.le(4)==(1u<<8)); // Native TypeID::GameObject8, Source type5 is different.
|
|
expect(mask28(sparse)==((1u<<0)|(1u<<12)|(1u<<17)|(1u<<20)));
|
|
expect(sparse.le(4)==0x10 && sparse.le(1)==0 && sparse.le(4)==0 && sparse.le(1)==0);sparse.end();
|
|
changed=entities.translate(update(guid,{{14,1}}),self,70,0x10046,0,writer);expect(changed.size()==1);
|
|
auto state_bytes=block_payload(changed[0].body,false,native);Reader state(state_bytes);state.take(7);expect(mask28(state)==((1u<<0)|(1u<<17)));expect(state.le(1)==1 && state.le(1)==0);state.end();
|
|
changed=entities.translate(update(guid,{{14,99}}),self,70,0x10046,0,writer);expect(changed.empty());
|
|
entities.actors.at(guid).sent=false;expect(entities.translate(update(guid,{{14,0}}),self,70,0x10046,0,writer).empty());
|
|
entities.actors.at(guid).sent=true;entities.actors.at(guid).presentation_map=1;expect(entities.translate(update(guid,{{14,0}}),self,70,0x10046,0,writer).empty());
|
|
}
|
|
// The source gives each viewer a different field 19 for the same conditional chest.
|
|
// The native chest's static INTERACT_COND stays set even for the eligible viewer.
|
|
{
|
|
constexpr uint32_t herb=2020023, active_visual=0x4024;
|
|
constexpr uint64_t spawn=(uint64_t(0xf110)<<48)|(uint64_t(herb)<<24)|5015236;
|
|
const auto chest_guid=forever_game_object_guid(spawn,herb,70,0);
|
|
auto create_fields=std::map<uint16_t,uint32_t>{{3,herb},{4,std::bit_cast<uint32_t>(1.f)},
|
|
{8,28255},{9,4},{13,std::bit_cast<uint32_t>(1.f)},{14,1},{21,3}};
|
|
auto create_flags=[&](NativeEntityTranslation& viewer,uint32_t expected){
|
|
const auto packets=viewer.translate(update(spawn,create_fields,true),self,70,0x10046,0,writer);
|
|
expect(packets.size()==1);
|
|
const auto bytes=block_payload(packets[0].body,true,chest_guid);
|
|
Reader data(bytes);
|
|
data.take(5);expect(data.le(4)==herb && data.le(4)==expected);
|
|
expect(data.le(4)==std::bit_cast<uint32_t>(1.f) && data.le(4)==28255);
|
|
data.take(24);unpack_guid(data,16);unpack_guid(data,16);
|
|
expect(data.le(4)==4);
|
|
};
|
|
NativeEntityTranslation missing,off_quest,on_quest;
|
|
create_flags(missing,0);
|
|
create_fields[19]=0;create_flags(off_quest,0);
|
|
create_fields[19]=1;create_flags(on_quest,active_visual);
|
|
|
|
auto check_sparse=[&](const LegacyPacket& packet,uint32_t expected,bool with_static){
|
|
const auto bytes=block_payload(packet.body,false,chest_guid);
|
|
Reader data(bytes);
|
|
expect(data.le(1)==0 && data.le(1)==0 && data.le(1)==3);
|
|
expect(data.le(4)==(with_static?0x101u:1u)); // ObjectData first, optional GameObjectData second.
|
|
expect(data.le(1)==0x50 && data.le(4)==expected); // ObjectData group and DynamicFlags field.
|
|
if(with_static){
|
|
expect(mask28(data)==((1u<<0)|(1u<<12)));
|
|
expect(data.le(4)==4 && data.le(1)==0);
|
|
}
|
|
data.end();
|
|
};
|
|
auto check_update=[&](NativeEntityTranslation& viewer,uint32_t source_word,uint32_t expected,bool with_static){
|
|
std::map<uint16_t,uint32_t> fields{{19,source_word}};
|
|
if(with_static)fields[9]=4;
|
|
const auto packets=viewer.translate(update(spawn,fields),self,70,0x10046,0,writer);
|
|
expect(packets.size()==1);
|
|
check_sparse(packets[0],expected,with_static);
|
|
};
|
|
check_update(off_quest,1,active_visual,true);
|
|
check_update(off_quest,0,0,false);
|
|
check_update(on_quest,0,0,false);
|
|
check_update(on_quest,0x12340000,0,false); // High-word values do not confer activation.
|
|
NativeEntityTranslation batched;
|
|
create_fields[19]=0;
|
|
auto fresh=update(spawn,create_fields,true), activation=update(spawn,{{19,1}});
|
|
Bytes batch;le(batch,2,4);le(batch,0,1);
|
|
append(batch,std::span(fresh.body).subspan(5));append(batch,std::span(activation.body).subspan(5));
|
|
const auto together=batched.translate({0xa9,batch},self,70,0x10046,0,writer);
|
|
expect(together.size()==2);
|
|
const auto initial=block_payload(together.front().body,true,chest_guid);
|
|
Reader initial_fields(initial);initial_fields.take(5);expect(initial_fields.le(4)==herb && initial_fields.le(4)==0);
|
|
check_sparse(together.back(),active_visual,false);
|
|
NativeEntityTranslation other;
|
|
create_fields[19]=1;
|
|
create_fields[21]=0;create_flags(other,0);
|
|
expect(other.translate(update(spawn,{{19,1}}),self,70,0x10046,0,writer).empty());
|
|
}
|
|
// #99: FISHINGNODE(17) bobbers are presented (f30a799c).
|
|
expect(turtle_static_game_object_type(17));
|
|
expect(turtle_static_game_object_type(18));
|
|
for(auto type:{11u,14u,15u,22u,24u,26u})expect(!turtle_static_game_object_type(type));
|
|
expect(turtle_static_game_object_flags(0xffffffff)==0x37);
|
|
// C52D1's actual Source door entry176576/display4413 omitted its zero
|
|
// TYPE_ID and STATE words. A mixed packet must retain this create along
|
|
// with explicit nonzero types, without weakening exact create counts.
|
|
{
|
|
constexpr uint64_t zero_door=17370386725221721750ULL;
|
|
NativeEntityTranslation batch;
|
|
auto door=update(zero_door,{{3,176576},{4,std::bit_cast<uint32_t>(1.f)},{8,4413},{13,std::bit_cast<uint32_t>(1.f)}},true);
|
|
auto chest=update(guid,{{3,entry},{4,std::bit_cast<uint32_t>(1.f)},{8,310},{13,std::bit_cast<uint32_t>(1.f)},{14,1},{21,3}},true);
|
|
auto quest_guid=guid+1;
|
|
auto quest=update(quest_guid,{{3,entry},{4,std::bit_cast<uint32_t>(1.f)},{8,310},{13,std::bit_cast<uint32_t>(1.f)},{14,1},{21,2}},true);
|
|
Bytes mixed;le(mixed,3,4);le(mixed,0,1);
|
|
for(const auto* packet:{&chest,&door,&quest})append(mixed,std::span(packet->body).subspan(5));
|
|
auto created=batch.translate({0xa9,mixed},self,70,0x10046,0,writer);
|
|
expect(created.size()==1);Reader count(created[0].body);expect(count.le(2)==0 && count.le(4)==3);
|
|
expect(batch.actors.at(zero_door).eligible && batch.actors.at(zero_door).sent && !batch.actors.at(zero_door).questgiver);
|
|
expect(batch.actors.at(quest_guid).questgiver);
|
|
const auto generation=batch.actors.at(quest_guid).generation;
|
|
// A sparse packet is not a full create: absent TYPE_ID must not turn
|
|
// the cached questgiver into a door or reset its generation/identity.
|
|
auto changed=batch.translate(update(quest_guid,{{14,0}}),self,70,0x10046,0,writer);
|
|
expect(changed.size()==1 && batch.actors.at(quest_guid).questgiver && batch.actors.at(quest_guid).generation==generation);
|
|
expect(!batch.actors.at(zero_door).questgiver && batch.actors.at(zero_door).sent);
|
|
}
|
|
// A values update followed by same-GUID recreation cannot update the new
|
|
// generation. The original Source packet establishes both event orders.
|
|
NativeEntityTranslation entities;
|
|
auto fields=std::map<uint16_t,uint32_t>{{3,entry},{4,std::bit_cast<uint32_t>(1.f)},{8,310},{13,std::bit_cast<uint32_t>(1.f)},{14,1},{21,0}};
|
|
entities.translate(update(guid,fields,true),self,70,0x10046,0,writer);
|
|
auto old=update(guid,{{14,0}}),fresh=update(guid,fields,true);Bytes mixed;le(mixed,2,4);le(mixed,0,1);append(mixed,std::span(old.body).subspan(5));append(mixed,std::span(fresh.body).subspan(5));
|
|
auto replaced=entities.translate({0xa9,mixed},self,70,0x10046,0,writer);expect(replaced.size()==2); // removal + fresh create; no old sparse state.
|
|
#ifdef GW_GAMEOBJECT_AUTHORITY_PROFILE_HEADER
|
|
std::map<unsigned,unsigned> held;unsigned ready=0,total=0;
|
|
for(const auto& row:gameobject_authority_profile::Rows){
|
|
++total;GameObjectTemplateReply source;source.found=true;source.entry=row.entry;source.type=row.type;source.display=row.display;source.names[0]=row.name;source.data=row.data;
|
|
auto authority=gameobject_authority_profile::lookup(source);expect(authority && gameobject_template_authority_matches(source,*authority));
|
|
GameObjectTemplateBinding b;b.entry=row.entry;b.source_guid=(uint64_t(0xf110)<<48)|(uint64_t(row.entry)<<24)|1;b.realm=70;b.request_generation=b.current_generation=1;b.native_guid=forever_game_object_guid(b.source_guid,b.entry,70,0);b.template_size=authority->template_size;b.native_display_id=authority->native_display;b.references=authority->references;
|
|
auto result=forever_gameobject_template(source,b);if(result.packet)++ready;else for(auto issue:result.held)++held[unsigned(issue.reason)];
|
|
}
|
|
std::cout<<"Conditional local template census: "<<ready<<"/"<<total<<" ready; hold reason counts";for(auto [kind,count]:held)std::cout<<' '<<kind<<':'<<count;std::cout<<'\n';
|
|
#endif
|
|
dlclose(library);
|
|
std::cout<<"Stationary GO create, original Classic28 sparse state/flags/artKit and generation lifetime PASS\n";return 0;
|
|
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}
|