Files
WoW-Hybrid-gateway/native/peer_player_translation.cpp

339 lines
20 KiB
C++

#include "entity_translation.hpp"
#include "inventory_translation.hpp"
#include "gateway/cast_visual_profile.hpp"
#include "gateway/peer_item_visual.hpp"
#include "gateway/peer_item_appearances.hpp"
#include "object_update_layout_guard.hpp"
#include "serializer.h"
#include "gateway/chat.hpp"
#include "gateway/object_updates.hpp"
#include "gateway/turtle_entity_profile.hpp"
#include "gateway/world.hpp"
#include "gateway/remote_speeds.hpp"
#include <algorithm>
#include <limits>
using namespace gateway;
namespace {
constexpr size_t PeerBytes=32u*1024u*1024u;
constexpr size_t PeerFields=4096,PeerDeferred=32;
void increment(uint32_t& value){if(value!=UINT32_MAX)++value;}
uint64_t source_guid(Reader& r){
uint64_t guid=0;auto mask=r.le(1);
for(unsigned i=0;i<8;++i)if(mask&(1u<<i))guid|=r.le(1)<<(8*i);
return guid;
}
uint32_t block_count(const Bytes& bytes){Reader r(bytes);r.le(2);return uint32_t(r.le(4));}
size_t stored_size(const LegacyPacket& packet,const std::map<uint16_t,uint32_t>& fields){
return packet.body.size()+fields.size()*6u;
}
using PeerLooks=std::vector<peer_item_visual::Template>;
uint32_t field_value(const std::map<uint16_t,uint32_t>& fields,unsigned index){
auto row=fields.find(index);return row==fields.end()?0u:row->second;
}
PeerLooks peer_looks(NativeEntityTranslation::PeerPlayer& peer,const std::map<uint16_t,uint32_t>& fields,NativeInventoryTranslation* inventory,
bool request=false,uint32_t only_entry=0){
PeerLooks result;
if(!inventory)return result;
auto flags=field_value(fields,turtle_entity_fields::PLAYER_FLAGS);
auto identity=field_value(fields,turtle_entity_fields::UNIT_FIELD_BYTES_0);
for(unsigned slot=0;slot<19;++slot){
if(!peer_item_visual::actor_slot(slot))continue;
const auto field=turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0+12*slot;
const auto item=field_value(fields,field),current=field_value(peer.fields,field);
auto& retained=peer.visible_looks[slot];
if(retained.item!=current)retained={};
if(!item || item>0x7fffffffu || (only_entry && item!=only_entry) ||
!forever_visible_item_id(flags,slot,item,uint8_t(identity),uint8_t(identity>>16),
race_slot_hide_profile::source_display(item)) ||
peer_item_appearances::has_native_look(item))continue;
auto look=retained;
if(const auto* source=inventory->peer_template(item)){
look={uint8_t(slot),item,0,0};
if(source->found && source->entry==item && peer_item_visual::matches(slot,source->inventory_type)){
look.display=source->display;look.inventory_type=uint8_t(source->inventory_type);
}
if(item==current)retained=look;
}else if(look.item!=item){
if(request)inventory->request_peer_template(item);
continue;
}
if(!look.display || (!race_slot_hide_profile::hidden(uint8_t(identity),uint8_t(identity>>16),slot,look.display) &&
!peer_item_appearances::render_item(look.display,look.inventory_type)))continue;
result.push_back(look);
}
return result;
}
Bytes peer_descriptor(uint16_t realm,uint32_t address,uint32_t map,
const NativeEntityTranslation::PeerPlayer& peer,
const std::map<uint16_t,uint32_t>& fields,const LegacyPacket* packet,const PeerLooks& looks){
if(fields.size()>PeerFields || peer.identity.name.empty() || peer.identity.name.size()>63)
throw Error("peer descriptor bounds");
Bytes input;le(input,realm,2);le(input,address,4);le(input,map,4);
le(input,1u | (looks.empty()?0u:peer_item_visual::TemplateSections) | (packet?0u:peer_item_visual::Refresh),2);
le(input,peer.identity.guid,8);cstring(input,peer.identity.name);le(input,fields.size(),2);
for(auto [field,value]:fields){le(input,field,2);le(input,value,4);}
if(!looks.empty()){
le(input,looks.size(),1);
for(const auto& look:looks){le(input,look.slot,1);le(input,look.item,4);le(input,look.display,4);le(input,look.inventory_type,1);}
}
if(packet){le(input,packet->opcode,2);append(input,packet->body);}
return input;
}
}
bool NativeEntityTranslation::peer_movement_opcode(uint16_t opcode){
// Pinned Tortoise Opcodes_1_12_1.h regular MovementInfo broadcasts only.
// Teleports/transport/forced movement and unknown packet families are not aliases.
switch(opcode){
case 0xb5:case 0xb6:case 0xb7:case 0xb8:case 0xb9:case 0xba: // forward/back/stop/strafe
case 0xbb:case 0xbc:case 0xbd:case 0xbe: // jump/turn
case 0xbf:case 0xc0:case 0xc1:case 0xc2:case 0xc3: // pitch/run/walk
case 0xc9:case 0xca:case 0xcb:case 0xda:case 0xdb:case 0xee: // land/swim/facing/heartbeat
case 0x2ca: // CMSG_MOVE_FALL_RESET, relayed to observers by HandleMovementOpcodes (#91 scan: 846 drops)
return true;
default:return false;
}
}
std::vector<LegacyPacket> NativeEntityTranslation::translate_peers(const LegacyPacket& packet,
const Character& self,uint16_t realm,uint32_t address,uint32_t map,const Codec& codec,NativeInventoryTranslation* peer_items){
std::vector<LegacyPacket> out;
auto emit=[&](uint32_t opcode,Bytes body){out.push_back({opcode,std::move(body)});};
auto release=[&](uint64_t guid){
auto found=peer_players.find(guid);if(found==peer_players.end())return;
peer_pending_bytes-=std::min(peer_pending_bytes,found->second.pending_bytes);
peer_players.erase(found);
// Keep an outstanding name query tied to its retired generation. A late
// answer must be consumed/discarded before querying a recreated actor.
};
auto query=[&](uint64_t guid,PeerPlayer& peer){
if(peer_name_queries.contains(guid))return;
if(peer_name_queries.size()>=4096 || requests.size()>=4096){increment(peer_held);return;}
Bytes body;le(body,guid,8);requests.push_back({0x50,std::move(body)});
peer_name_queries.emplace(guid,peer.generation);
};
auto target=[&](const LegacyObjectUpdate& update,bool create){
spell_channels.observe(update, create, false, cast_visual_profile::link);
constexpr auto first=turtle_entity_fields::UNIT_FIELD_TARGET;
if(!create && !update.fields.contains(first) && !update.fields.contains(first+1))return;
auto& value=targets[update.guid];
if(create){value=TargetState{};value.known=3;value.presented=Guid128{};}
if(auto f=update.fields.find(first);f!=update.fields.end()){value.words[0]=f->second;value.known|=1;}
if(auto f=update.fields.find(first+1);f!=update.fields.end()){value.words[1]=f->second;value.known|=2;}
if(!create || value.words[0] || value.words[1])target_dirty.insert(update.guid);
};
auto values=[&](PeerPlayer& peer,const std::map<uint16_t,uint32_t>& fields,const LegacyPacket& source){
auto result=codec(GW_CODEC_PLAYER_VALUES,peer_descriptor(realm,address,map,peer,fields,&source,peer_looks(peer,fields,peer_items)));
auto count=block_count(result);if(count>1)throw Error("peer value block count");
if(count)emit(0x5e0000,std::move(result));
};
auto movement=[&](uint64_t guid,const LegacyPacket& source){
Bytes input;le(input,realm,2);le(input,guid,8);le(input,source.opcode,2);append(input,source.body);
auto result=codec(GW_CODEC_PEER_MOVEMENT,input);
if(result.empty())throw Error("empty peer movement");
emit(remote_speeds::MovementOpcode,std::move(result));
};
if(packet.opcode==0x51){
try{
auto name=turtle_chat_player(packet.body);auto requested=peer_name_queries.find(name.guid);
if(requested!=peer_name_queries.end()){
auto expected=requested->second;peer_name_queries.erase(requested);
auto found=peer_players.find(name.guid);auto actor=actors.find(name.guid);
if(found!=peer_players.end() && actor!=actors.end() && actor->second.kind==4){
auto& peer=found->second;
if(peer.generation!=expected || actor->second.generation!=expected){query(name.guid,peer);}
else if(name.race!=peer.identity.race || name.cls!=peer.identity.cls || name.gender!=peer.identity.gender){
peer.blocked=true;increment(peer_rejected);
}else{peer.identity=name;peer.named=true;}
}
}
}catch(const Error&){increment(peer_rejected);}
}
if(packet.opcode==0xaa){
try{Reader r(packet.body);auto guid=r.le(8);release(guid);}catch(const Error&){increment(peer_rejected);}
}
if(packet.opcode==0xa9 || packet.opcode==0x1f6){
auto updates=turtle_object_updates(packet.opcode,packet.body);
std::set<uint64_t> changed;
std::shared_ptr<const LegacyPacket> original;
for(const auto& update:updates){
if(update.kind==4){for(auto guid:update.removed)release(guid);continue;}
if(update.guid==self.guid){
const bool create=(update.kind==2 || update.kind==3) && update.type==4;
if(create){self_look=PeerPlayer{};self_look.identity=self;self_look.fields=update.fields;self_look_dirty=true;}
else if(update.kind==0 && self_look.identity.guid==self.guid){
constexpr auto flags=turtle_entity_fields::PLAYER_FLAGS;
const auto before=field_value(self_look.fields,flags)&(SourceHideHelm|SourceHideCloak);
for(auto [field,value]:update.fields)self_look.fields[field]=value;
// Only a visible-entry change or a head/back visibility toggle
// changes the projected look; ordinary flag churn does not.
if((field_value(self_look.fields,flags)&(SourceHideHelm|SourceHideCloak))!=before ||
std::any_of(update.fields.begin(),update.fields.end(),[](const auto& row){
constexpr unsigned first=turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0;
return row.first>=first && row.first<first+19*12 && (row.first-first)%12==0;
}))self_look_dirty=true;
}
continue;
}
if((update.kind==2 || update.kind==3) && update.type==4){
increment(peer_received);release(update.guid);
if(!update.guid || update.guid>UINT32_MAX || actors.size()>=4096 || update.fields.size()>PeerFields || generation==UINT64_MAX){
increment(peer_rejected);continue;
}
auto& actor=actors[update.guid];actor=Actor{};actor.kind=4;
actor.generation=++generation;actor.presentation_realm=realm;actor.presentation_map=map;actor.modern=forever_player_guid(update.guid,realm);
auto& peer=peer_players[update.guid];peer.generation=actor.generation;
peer.identity.guid=update.guid;
auto word=update.fields.find(turtle_entity_fields::UNIT_FIELD_BYTES_0);
if(word==update.fields.end()){peer.blocked=true;increment(peer_held);continue;}
peer.identity.race=uint8_t(word->second);peer.identity.cls=uint8_t(word->second>>8);
peer.identity.gender=uint8_t(word->second>>16);actor.unit_class=peer.identity.cls;
actor.power_type=uint8_t(word->second>>24);
auto size=stored_size(packet,update.fields);
if(!peer.identity.race || !peer.identity.cls || peer.identity.gender>1 || size>PeerBytes-peer_pending_bytes){
peer.blocked=true;increment(peer_held);continue;
}
if(!original)original=std::make_shared<const LegacyPacket>(packet);
peer.create=original;peer.create_fields=update.fields;peer.fields=update.fields;
peer.pending_bytes=size;peer_pending_bytes+=size;target(update,true);query(update.guid,peer);
peer_looks(peer,peer.fields,peer_items,true);
if(update.melee_victim)actor.observe_movement_victim(update.guid,*update.melee_victim,out);
changed.insert(update.guid);continue;
}
if(update.kind==1){
auto found=peer_players.find(update.guid);
if(found!=peer_players.end()){
auto actor=actors.find(update.guid);
if(update.melee_victim && actor!=actors.end() && actor->second.kind==4 &&
actor->second.generation==found->second.generation && !found->second.blocked)
actor->second.observe_movement_victim(update.guid,*update.melee_victim,out);
increment(peer_held); // The movement itself still needs a native writer.
}
continue;
}
if(update.kind!=0)continue;
auto found=peer_players.find(update.guid);auto actor=actors.find(update.guid);
if(found==peer_players.end() || actor==actors.end() || actor->second.kind!=4)continue;
auto& peer=found->second;
if(peer.generation!=actor->second.generation || peer.blocked)continue;
if(!changed.insert(update.guid).second){peer.blocked=true;increment(peer_held);continue;}
for(auto [field,value]:update.fields)peer.fields[field]=value;
if(peer.fields.size()>PeerFields){peer.blocked=true;increment(peer_held);continue;}
target(update,false);
if(update.fields.contains(turtle_entity_fields::PLAYER_FLAGS) || update.fields.contains(turtle_entity_fields::UNIT_FIELD_BYTES_0) ||
std::any_of(update.fields.begin(),update.fields.end(),[](const auto& row){
constexpr unsigned first=turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0;
return row.first>=first && row.first<first+19*12 && (row.first-first)%12==0;
}))peer_looks(peer,peer.fields,peer_items,true);
if(actor->second.sent){
try{values(peer,peer.fields,packet);}catch(const Error&){increment(peer_held);}
}else{
auto size=stored_size(packet,peer.fields);
if(peer.deferred.size()>=PeerDeferred || size>PeerBytes-peer_pending_bytes){
peer.blocked=true;increment(peer_held);continue;
}
peer.deferred.push_back({packet,peer.fields});peer.pending_bytes+=size;peer_pending_bytes+=size;
}
}
}
std::vector<LegacyMovementRecord> motions;
if(peer_movement_opcode(packet.opcode))motions.push_back({uint16_t(packet.opcode),packet.body});
else if(packet.opcode==0x2fb){
try{motions=turtle_compressed_moves(packet.body);}catch(const Error&){increment(peer_rejected);}
}
for(const auto& motion:motions){
if(!peer_movement_opcode(motion.opcode))continue;
try{
Reader r(motion.body);auto guid=source_guid(r);if(guid==self.guid)continue;
auto found=peer_players.find(guid);auto actor=actors.find(guid);
if(found==peer_players.end() || actor==actors.end() || actor->second.kind!=4 ||
found->second.generation!=actor->second.generation){increment(peer_held);continue;}
if(motion.body.size()>4096){increment(peer_rejected);continue;}
LegacyPacket source{motion.opcode,motion.body};
if(actor->second.sent)movement(guid,source);
else found->second.movement=std::move(source);
}catch(const Error&){increment(peer_rejected);}
}
for(auto& [guid,peer]:peer_players){
auto actor=actors.find(guid);
if(peer.blocked || !peer.named || !peer.create || actor==actors.end() ||
actor->second.kind!=4 || actor->second.generation!=peer.generation || actor->second.sent)continue;
try{
auto result=codec(GW_CODEC_PLAYERS,peer_descriptor(realm,address,map,peer,peer.create_fields,peer.create.get(),peer_looks(peer,peer.create_fields,peer_items)));
if(block_count(result)!=1)throw Error("peer create block count");
emit(0x5e0000,std::move(result));actor->second.eligible=true;actor->second.sent=true;increment(peer_sent);
for(const auto& deferred:peer.deferred){
try{values(peer,deferred.fields,deferred.packet);}catch(const Error&){increment(peer_held);}
}
if(peer.movement){try{movement(guid,*peer.movement);}catch(const Error&){increment(peer_rejected);}}
peer_pending_bytes-=std::min(peer_pending_bytes,peer.pending_bytes);peer.pending_bytes=0;
peer.create.reset();peer.create_fields.clear();peer.deferred.clear();peer.movement.reset();
}catch(const Error&){peer.blocked=true;increment(peer_held);}
}
if(self_look_dirty){
self_look_dirty=false;
for(auto& item:self_look_refresh(realm,address,map,codec,peer_items,0,true))out.push_back(std::move(item));
}
return out;
}
std::vector<LegacyPacket> NativeEntityTranslation::self_look_refresh(uint16_t realm,uint32_t address,uint32_t map,
const Codec& codec,NativeInventoryTranslation* inventory,uint32_t only_entry,bool request){
std::vector<LegacyPacket> out;
if(!inventory || !self_look.identity.guid || self_look.identity.name.empty())return out;
(void)address;
auto looks=peer_looks(self_look,self_look.fields,inventory,request,only_entry);
if(looks.empty())return out;
// The active player accepts VisibleItems only in its own owner-form values
// update. The codec's self inventory writer emits exactly that from Source
// visible fields, so present the resolved donor item in place of the
// generated entry and keep the slot's enchantment words unchanged.
std::map<uint16_t,uint32_t> fields;
for(const auto& look:looks){
const auto donor=peer_item_appearances::render_item(look.display,look.inventory_type);
if(!donor)continue;
const uint16_t index=uint16_t(turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0+12*look.slot);
fields[index]=donor;
fields[uint16_t(index+1)]=field_value(self_look.fields,index+1u);
fields[uint16_t(index+2)]=field_value(self_look.fields,index+2u);
}
if(fields.empty())return out;
try{
Bytes input;le(input,self_look.identity.guid,8);le(input,realm,2);le(input,map,4);le(input,1,4);
le(input,self_look.identity.guid,8);le(input,4,1);le(input,0,1);le(input,fields.size(),2);
for(auto [index,value]:fields){le(input,index,2);le(input,value,4);}
auto block=codec(GW_CODEC_INVENTORY,input);
if(block_count(block)!=1)throw Error("self item refresh block count");
out.push_back({0x5e0000,std::move(block)});
}catch(const Error&){increment(peer_held);}
return out;
}
std::vector<LegacyPacket> NativeEntityTranslation::refresh_peer_item(uint32_t entry,uint16_t realm,uint32_t address,
uint32_t map,NativeInventoryTranslation& inventory,const Codec& codec){
std::vector<LegacyPacket> out;
const bool retry_queries=inventory.peer_queries_deferred;
inventory.peer_queries_deferred=false;
for(auto& [guid,peer]:peer_players){
auto actor=actors.find(guid);
if(peer.blocked || actor==actors.end() || actor->second.kind!=4 || actor->second.generation!=peer.generation ||
actor->second.presentation_realm!=realm || actor->second.presentation_map!=map)continue;
if(retry_queries)peer_looks(peer,peer.fields,&inventory,true);
auto looks=peer_looks(peer,peer.fields,&inventory,false,entry);
if(!peer.named || !actor->second.sent || looks.empty())continue;
try{
auto block=codec(GW_CODEC_PLAYER_VALUES,peer_descriptor(realm,address,map,peer,peer.fields,nullptr,looks));
if(block_count(block)!=1)throw Error("peer item refresh block count");
out.push_back({0x5e0000,std::move(block)});
}catch(const Error&){increment(peer_held);}
}
if(retry_queries)self_look_refresh(realm,address,map,codec,&inventory,0,true);
for(auto& item:self_look_refresh(realm,address,map,codec,&inventory,entry,false))out.push_back(std::move(item));
return out;
}