225 lines
16 KiB
C++
225 lines
16 KiB
C++
#include "optional_test_codec.hpp"
|
|
#include "world_translation.hpp"
|
|
#include "persistent_ground_fixture.hpp"
|
|
#include "consecration_cast_fixture.hpp"
|
|
#include "gateway/gameplay.hpp"
|
|
#include "serializer.h"
|
|
#include <algorithm>
|
|
#include <bit>
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
#include <dlfcn.h>
|
|
#include <functional>
|
|
#include <iostream>
|
|
#include <limits>
|
|
using namespace gateway;
|
|
namespace {
|
|
unsigned checks=0;
|
|
void check(bool value,const char* message){++checks;if(!value)throw Error(message);}
|
|
void rejects(const std::function<void()>& op){++checks;try{op();}catch(const Error&){return;}throw Error("expected location rejection");}
|
|
void f32(Bytes& out,float value){le(out,std::bit_cast<uint32_t>(value),4);}
|
|
Bytes source_guid(uint64_t id){Bytes out;le(out,id,8);return pack_guid(out);}
|
|
Guid128 identity(uint64_t low,uint64_t high){Bytes b;le(b,low,8);le(b,high,8);Guid128 out{};std::copy(b.begin(),b.end(),out.begin());return out;}
|
|
constexpr uint64_t owner=4520,enemy=(0xf130ULL<<48)|(1563ULL<<24)|2195,item=(0x4000ULL<<48)|77;
|
|
const SpellLocation src{{-14677.5f,497.89f,1.76032f}},dst{{-14677.5f,477.714f,3.68688f}};
|
|
Bytes source_event(uint16_t opcode,uint16_t targets,uint32_t spell){
|
|
Bytes out=source_guid(owner);append(out,source_guid(owner));le(out,spell,4);le(out,opcode==0x131?2:0x100,2);
|
|
if(opcode==0x131)le(out,500,4);else{le(out,1,1);le(out,enemy,8);le(out,0,1);}
|
|
le(out,targets,2);if(targets&2)append(out,source_guid(enemy));if(targets&16)append(out,source_guid(item));
|
|
if(targets&0x20)for(float v:src.position)f32(out,v);
|
|
if(targets&0x40)for(float v:dst.position)f32(out,v);
|
|
return out;
|
|
}
|
|
Bytes target_wire(const TurtleSpellCast& event,const Guid128& enemy_id,const Guid128& item_id){
|
|
// Independent named70334 SpellTargetData writer contract: Flags/3GUIDs,
|
|
// 12MSBbits (housing/src/dst/orient/map/name7), then typed location(s).
|
|
Bytes out;le(out,event.targets,4);append(out,pack_guid(event.target_unit?enemy_id:Guid128{}));
|
|
append(out,pack_guid(event.target_item?item_id:Guid128{}));le(out,0,2);
|
|
le(out,(event.source_location?0x40:0)|(event.destination_location?0x20:0),1);le(out,0,1);
|
|
for(const auto* location:{&event.source_location,&event.destination_location})if(*location){
|
|
le(out,0,2);for(float v:(*location)->position)f32(out,v); // Empty transport + absolute XYZ.
|
|
}
|
|
return out;
|
|
}
|
|
Bytes codec_input(const TurtleSpellCast& event,const Guid128& self,const Guid128& enemy_id,const Guid128& item_id,const Guid128& cast,uint8_t kind){
|
|
Bytes out{kind};append(out,self);append(out,self);append(out,cast);
|
|
le(out,event.spell,4);le(out,0,4);le(out,0,4);le(out,kind?12345:event.time,4);le(out,event.targets,4);
|
|
append(out,event.target_unit?enemy_id:Guid128{});append(out,event.target_item?item_id:Guid128{});
|
|
le(out,event.hits.size(),1);for(auto id:event.hits){check(id==enemy,"fixture hit identity");append(out,enemy_id);}
|
|
le(out,event.misses.size(),1);for(const auto& miss:event.misses){check(miss.guid==enemy,"fixture miss identity");append(out,enemy_id);le(out,miss.reason,1);le(out,miss.reflect,1);}
|
|
append(out,spell_cast_location_payload(event));return out;
|
|
}
|
|
Bytes serialize(decltype(&gw_codec_serialize) codec,const Bytes& input){
|
|
uint32_t count=0;check(codec(GW_CODEC_SPELL_CAST,input.data(),input.size(),0,nullptr,0,&count)==GW_CODEC_CAPACITY && count<=GW_CODEC_BOUND,"codec sizing");
|
|
Bytes out(count);check(codec(GW_CODEC_SPELL_CAST,input.data(),input.size(),0,out.data(),out.size(),&count)==GW_CODEC_OK && count==out.size(),"codec serialization");return out;
|
|
}
|
|
void target_at_native_offset(const Bytes& body,const Bytes& target){
|
|
// Original SpellCastData writer: four packed128 identities, SpellID,
|
|
// two visual IDs, flags/flagsEx/flagsEx2/time, then SpellTargetData.
|
|
Reader reader(body);
|
|
for(unsigned i=0;i<4;++i){auto mask=uint16_t(reader.le(2));reader.take(std::popcount(mask));}
|
|
reader.take(28);
|
|
check(reader.take(target.size())==target,"native target location framing/coordinates differ at original writer offset");
|
|
}
|
|
std::vector<LegacyPacket> packets(const Bytes& body){Reader r(body);auto n=r.le(4);std::vector<LegacyPacket> out;for(unsigned i=0;i<n;++i){auto op=uint32_t(r.le(4));auto size=r.le(4);out.push_back({op,r.take(size)});}r.end();return out;}
|
|
|
|
Bytes consecration(decltype(&gw_codec_serialize) codec,uint32_t account,uint32_t player,bool logout_with_ground){
|
|
using consecration_fixture::spell;
|
|
using consecration_fixture::visual;
|
|
NativeWorldTranslation world;
|
|
world.realm=70;
|
|
world.internal_source_account=account;
|
|
world.selected.guid=player;
|
|
world.selected.name="Fixture";
|
|
world.selected.race=1;
|
|
world.selected.cls=2;
|
|
world.selected.level=20;
|
|
const auto map=logout_with_ground?1u:0u;
|
|
const auto& position=logout_with_ground?dst.position:src.position;
|
|
Bytes location;le(location,map,4);
|
|
for(float coordinate:position)f32(location,coordinate);
|
|
f32(location,0);
|
|
world.translate({0x236,location},0x46010046,0,codec);
|
|
Bytes known{0};le(known,1,2);le(known,spell,2);le(known,0,2);le(known,0,2);
|
|
world.translate({0x12a,known},0x46010046,0,codec);
|
|
Bytes self;
|
|
le(self,1,4);le(self,0,1);le(self,3,1);append(self,source_guid(player));le(self,4,1);le(self,0x21,1);
|
|
le(self,0,4);le(self,100,4);append(self,std::span(location).subspan(4));le(self,0,4);
|
|
for(float speed:{2.5f,7.f,4.5f,4.72f,2.5f,3.14f})f32(self,speed);
|
|
// Sparse level, faction and human paladin identity fields (34, 35, 36).
|
|
le(self,2,1);le(self,0,4);le(self,0x1c,4);le(self,20,4);le(self,1,4);le(self,0x0201,4);
|
|
world.translate({0xa9,self},0x46010046,0,codec);
|
|
check(world.self_created && world.entities.self_presentation_ready,"selected paladin SELF create missing");
|
|
|
|
const auto cast=identity(23,(uint64_t(47)<<58)|(uint64_t(70)<<42)|(uint64_t(spell)<<6)|3);
|
|
auto request=consecration_fixture::request(cast);
|
|
auto outgoing=world.gameplay(0x3e0176,request);
|
|
check(outgoing.opcode==0x12e && outgoing.body==Bytes{0xcd,0x67,0,0,0,0},"self-cast rank or Source target layout changed");
|
|
world.gameplay_sent(0x3e0176,request);
|
|
|
|
// Tortoise sends START before FillTargetMap. For effect 27 with implicit
|
|
// targets 18/16 it sets DEST to the caster and adds no units to the hit list.
|
|
auto prefix=source_guid(player);
|
|
append(prefix,source_guid(player));le(prefix,spell,4);
|
|
auto start=prefix;
|
|
le(start,2,2);le(start,0,4);le(start,0,2);
|
|
auto started=packets(world.translate({0x131,start},0x46010046,0,codec));
|
|
const auto prepare=std::find_if(started.begin(),started.end(),[](const auto& p){return p.opcode==0x69002b;});
|
|
Bytes prepared=pack_guid(cast);append(prepared,pack_guid(cast));
|
|
check(prepare!=started.end() && prepare->body==prepared,"instant cast prepare identity changed");
|
|
const auto begin=std::find_if(started.begin(),started.end(),[](const auto& p){return p.opcode==0x69002d;});
|
|
check(begin!=started.end() && world.pending_cast && world.cast_prepared,"self-target START lost");
|
|
TurtleSpellCast expected;
|
|
target_at_native_offset(begin->body,target_wire(expected,{},{}));
|
|
|
|
auto go=prefix;
|
|
le(go,0x100,2);le(go,0,1);le(go,0,1);le(go,0x40,2);
|
|
for(float coordinate:position)f32(go,coordinate);
|
|
auto went=packets(world.translate({0x132,go},0x46010046,0,codec));
|
|
const auto end=std::find_if(went.begin(),went.end(),[](const auto& p){return p.opcode==0x69002c;});
|
|
check(end!=went.end() && !world.pending_cast && !world.cast_prepared,"caster-centered GO failed to finish");
|
|
expected.targets=0x40;
|
|
expected.destination_location=SpellLocation{position};
|
|
target_at_native_offset(end->body,target_wire(expected,{},{}));
|
|
|
|
const auto ground=ground_fixture::source;
|
|
const auto create=ground_fixture::update(ground,true,ground_fixture::consecration_fields(player),world.location->position);
|
|
std::vector<LegacyPacket> created;
|
|
try { created=packets(world.translate(create,0x46010046,0,codec)); }
|
|
catch(const Error& error){throw Error(std::string("Consecration ground CREATE failed (")+error.what()+")");}
|
|
check(created.size()==1 && created[0].opcode==0x5e0000,"missing native ground-object update");
|
|
const auto ground_id=forever_persistent_ground_guid(ground,spell,world.realm,world.location->map);
|
|
Reader packet(created[0].body);
|
|
check(packet.le(2)==map && packet.le(4)==1 && packet.le(1)==0x80,"native ground-object update header");
|
|
auto block=packet.take(packet.le(4));packet.end();
|
|
Reader create_block(block);
|
|
check(create_block.le(1)==2 && unpack_guid(create_block,16)==Bytes(ground_id.begin(),ground_id.end())
|
|
&& create_block.le(1)==9,"wrong ground object created");
|
|
create_block.take(7); // Stationary movement flags and pause times.
|
|
for(float coordinate:world.location->position)check(create_block.f32()==coordinate,"wrong native ground position");
|
|
auto values=create_block.take(create_block.le(4));create_block.end();
|
|
Reader fields(values);
|
|
fields.take(17); // Fragment list and ObjectData.
|
|
const auto caster=forever_player_guid(player,world.realm);
|
|
check(unpack_guid(fields,16)==Bytes(caster.begin(),caster.end()),"ground caster belongs to another world");
|
|
check(fields.le(1)==1 && fields.le(4)==visual && fields.le(4)==0 && fields.le(4)==spell && fields.f32()==8.f,
|
|
"ground type, visual, spell or radius changed");
|
|
check(fields.le(4)==world.entities.persistent_ground.at(ground).cast_time,"ground timestamp differs from create");
|
|
fields.end();
|
|
Bytes comparable=created[0].body;comparable.resize(comparable.size()-4); // Per-cast timestamp.
|
|
if(logout_with_ground){
|
|
world.translate({0x4d,{}},0x46010046,0,codec);
|
|
check(world.logout_complete && !world.self_created,"prior world did not acknowledge logout");
|
|
return comparable;
|
|
}
|
|
|
|
Bytes destroy;le(destroy,ground,8);
|
|
const auto removed=packets(world.translate({0xaa,destroy},0x46010046,0,codec));
|
|
check(removed.size()==1 && removed[0].opcode==0x5e0000
|
|
&& removed[0].body==forever_object_removal(0,std::span(&ground_id,1),{}),"wrong ground object removed");
|
|
check(!world.entities.actors.contains(ground) && !world.entities.persistent_ground.contains(ground),"stale ground object");
|
|
const auto stand=world.gameplay(0x3e0036,Bytes{0});
|
|
check(stand.opcode==0x101 && stand.body==Bytes(4,0),"gameplay failed after ground removal");
|
|
return comparable;
|
|
}
|
|
}
|
|
int main(){try{
|
|
const char* library=std::getenv("GW_TEST_CODEC_PATH");
|
|
if (test_fixture::optional_codec_missing(library)) return 77;
|
|
void* handle=dlopen(library,RTLD_NOW|RTLD_LOCAL);if(!handle)throw Error("matched codec cannot load");
|
|
auto codec=reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(handle,"gw_codec_serialize"));if(!codec)throw Error("codec symbol missing");
|
|
const auto direct=consecration(codec,513,4528,false);
|
|
// Retire a different account's world with its ground object still active, then
|
|
// reuse its Source counter on the paladin in the same process and codec.
|
|
consecration(codec,1001,42,true);
|
|
check(consecration(codec,513,4528,false)==direct,"paladin ground bytes changed on second account entry");
|
|
const auto self=forever_player_guid(owner,70),enemy_id=forever_creature_guid(enemy,1563,70,0),item_id=identity(77,uint64_t(3)<<58);
|
|
for(uint16_t opcode:{0x131,0x132})for(uint16_t flags:{0u,2u,16u,0x20u,0x40u,0x60u,0x22u,0x52u,0x72u}){
|
|
auto body=source_event(opcode,flags,20549);auto parsed=turtle_spell_cast(opcode,body);check(parsed.has_value(),"supported source event held");
|
|
const auto& event=*parsed;check(event.targets==flags && bool(event.source_location)==bool(flags&0x20) && bool(event.destination_location)==bool(flags&0x40),"source flags/location presence changed");
|
|
if(event.source_location)check(event.source_location->position==src.position,"source XYZ changed");
|
|
if(event.destination_location)check(event.destination_location->position==dst.position,"destination XYZ changed");
|
|
const auto cast=identity(17,(uint64_t(47)<<58)|(uint64_t(70)<<42)|(uint64_t(event.spell)<<6)|3);
|
|
auto input=codec_input(event,self,enemy_id,item_id,cast,opcode==0x132);
|
|
target_at_native_offset(serialize(codec,input),target_wire(event,enemy_id,item_id));
|
|
if(flags==0x20 || flags==0x40 || flags==0x60){
|
|
for(size_t size=0;size<body.size();++size)rejects([&]{turtle_spell_cast(opcode,std::span(body).first(size));});
|
|
auto extra=body;extra.push_back(0);rejects([&]{turtle_spell_cast(opcode,extra);});
|
|
for(float invalid:{std::numeric_limits<float>::quiet_NaN(),std::numeric_limits<float>::infinity(),100001.f,-100001.f}){
|
|
auto malformed=body;auto offset=malformed.size()-12;auto bits=std::bit_cast<uint32_t>(invalid);for(unsigned i=0;i<4;++i)malformed[offset+i]=uint8_t(bits>>(8*i));
|
|
rejects([&]{turtle_spell_cast(opcode,malformed);});
|
|
auto bad=input;for(unsigned i=0;i<4;++i)bad[bad.size()-12+i]=uint8_t(bits>>(8*i));uint32_t count=0;
|
|
check(codec(GW_CODEC_SPELL_CAST,bad.data(),bad.size(),0,nullptr,0,&count)==GW_CODEC_PACKET,"codec accepted nonfinite/out-of-bound location");
|
|
}
|
|
auto short_input=input;short_input.pop_back();uint32_t count=0;
|
|
check(codec(GW_CODEC_SPELL_CAST,short_input.data(),short_input.size(),0,nullptr,0,&count)==GW_CODEC_PACKET,"codec accepted truncated XYZ");
|
|
}
|
|
}
|
|
for(uint16_t opcode:{0x131,0x132}){
|
|
check(!turtle_spell_cast(opcode,source_event(opcode,0x80,20549)),"unsupported source target mask accepted");
|
|
auto projectile=source_event(opcode,0x40,20549);auto offset=source_guid(owner).size()*2+4;projectile[offset]|=0x20;
|
|
check(!turtle_spell_cast(opcode,projectile),"unsupported projectile cast accepted");
|
|
}
|
|
TurtleSpellCast mismatch;mismatch.targets=0x20;rejects([&]{spell_cast_location_payload(mismatch);});
|
|
mismatch.targets=0;mismatch.source_location=src;rejects([&]{spell_cast_location_payload(mismatch);});
|
|
mismatch.targets=0x40;mismatch.source_location.reset();rejects([&]{spell_cast_location_payload(mismatch);});
|
|
mismatch.targets=0;mismatch.destination_location=dst;rejects([&]{spell_cast_location_payload(mismatch);});
|
|
// Actual source-target families: server-generated SRC for both shout/clap,
|
|
// DST for WarStomp. Real START retains pending identity; matching GO retires it.
|
|
for(auto [spell,flags]:{std::pair{1161u,0x20u},{11581u,0x20u},{20549u,0x40u}}){
|
|
NativeWorldTranslation world;world.selected.guid=owner;world.selected.race=6;world.selected.cls=1;world.selected.level=60;world.realm=70;world.self_created=true;world.entities.self_presentation_ready=true;
|
|
auto& actor=world.entities.actors[enemy];actor.kind=3;actor.entry=1563;actor.sent=true;actor.modern=enemy_id;actor.generation=1;
|
|
const auto cast=identity(23,(uint64_t(47)<<58)|(uint64_t(70)<<42)|(uint64_t(spell)<<6)|3);
|
|
ForeverSpellRequest request;request.spell=spell;request.cast=cast;world.pending_cast=NativeWorldTranslation::PendingCast{request,self};
|
|
auto start_body=source_event(0x131,flags,spell);auto start=packets(world.translate({0x131,start_body},0x46010046,0,codec));
|
|
check(world.pending_cast.has_value() && world.cast_prepared,"source START lost pending identity");
|
|
auto started=std::find_if(start.begin(),start.end(),[](const auto& p){return p.opcode==0x69002d;});check(started!=start.end(),"source-location START dropped");
|
|
target_at_native_offset(started->body,target_wire(*turtle_spell_cast(0x131,start_body),enemy_id,item_id));
|
|
auto go_body=source_event(0x132,flags,spell);auto go=packets(world.translate({0x132,go_body},0x46010046,0,codec));
|
|
check(!world.pending_cast && !world.cast_prepared,"source-location GO left stuck pending cast");
|
|
auto went=std::find_if(go.begin(),go.end(),[](const auto& p){return p.opcode==0x69002c;});check(went!=go.end(),"source-location GO dropped");
|
|
target_at_native_offset(went->body,target_wire(*turtle_spell_cast(0x132,go_body),enemy_id,item_id));
|
|
}
|
|
dlclose(handle);std::cout<<checks<<" source/destination cast checks passed, including Consecration cast, ground creation and removal, and subsequent gameplay.\n";return 0;
|
|
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}
|