Files
2026-11-23 03:28:32 +02:00

152 lines
8.8 KiB
C++

#include "party_translation.hpp"
#include "serializer.h"
#include "gateway/source_team.hpp"
#include <algorithm>
#include <atomic>
using namespace gateway;
namespace {
std::atomic<uint32_t> party_epoch{0};
std::optional<uint8_t> faction(uint8_t race){
const auto team = source_team_for_race(race);
if (!team) return std::nullopt;
// PartyUpdate uses FactionGroup (Alliance 3, Horde 5).
return *team == SourceTeam::Alliance ? 3 : 5;
}
bool source_class(uint8_t cls){return cls==1||cls==2||cls==3||cls==4||cls==5||cls==7||cls==8||cls==9||cls==11;}
}
std::optional<LegacyPacket> NativePartyTranslation::take(const TurtleGroupList& group,
const Character& self,const std::map<uint64_t,Character>& named,uint16_t realm,
const std::function<Bytes(uint32_t,const Bytes&)>& codec){
if(!pending)return {};
if(!realm || realm>8191 || !self.guid || self.guid>UINT32_MAX || !codec){++held;return {};}
if(group.explicit_solo && !shown){counter=0;queried.clear();members.clear();ready_check={};pending=false;return {};}
auto identity=[&](uint64_t id)->const Character*{
if(id==self.guid)return &self;
auto found=named.find(id);return found==named.end()?nullptr:&found->second;
};
if(!group.explicit_solo){
if(group.leader!=self.guid && std::none_of(group.members.begin(),group.members.end(),
[&](const auto& member){return member.guid==group.leader;})){++held;return {};}
auto leader=identity(group.leader);
if(!leader || !faction(leader->race)){++held;return {};}
auto valid=[&](const Character* c){return c && c->guid && c->guid<=UINT32_MAX && source_class(c->cls) && faction(c->race);};
if(!valid(&self) || self.name.empty() || self.name.size()>48){++held;return {};}
for(const auto& member:group.members)if(member.guid==self.guid || !valid(identity(member.guid))){++held;return {};}
if(group.loot && group.loot->master>UINT32_MAX){++held;return {};}
}
if(!counter){
auto previous=party_epoch.load();
do{if(previous>=0x7ffff){++held;return {};}}
while(!party_epoch.compare_exchange_weak(previous,previous+1));
const auto epoch=previous+1;
counter=(uint64_t(realm)<<51)|(self.guid<<19)|epoch;
}
if(sequence==UINT32_MAX){++held;return {};}
Bytes wire;le(wire,realm,2);le(wire,counter,8);le(wire,sequence+1,4);
le(wire,group.explicit_solo?1:0,1);le(wire,group.type==TurtleGroupType::raid?1:0,1);
le(wire,group.explicit_solo?0:*faction(identity(group.leader)->race),1);
le(wire,group.explicit_solo?0:group.leader,8);
le(wire,group.explicit_solo?0:group.members.size()+1,1);
if(!group.explicit_solo){
auto member=[&](uint64_t guid,const std::string& name,uint8_t flags,bool connected){
const auto* c=identity(guid);le(wire,guid,8);cstring(wire,name);
le(wire,c->cls,1);le(wire,flags&7,1);le(wire,(flags&0x80)?1:0,1);
le(wire,connected?1:0,1);le(wire,*faction(c->race),1);
};
member(self.guid,self.name,group.own_flags,true);
for(const auto& m:group.members)member(m.guid,m.name,m.flags,(m.status&1)!=0);
}
le(wire,!group.explicit_solo && group.loot?1:0,1);
if(!group.explicit_solo && group.loot){le(wire,uint8_t(group.loot->method),1);le(wire,group.loot->master,8);le(wire,group.loot->threshold,1);}
auto body=codec(GW_CODEC_PARTY_UPDATE,wire);++sequence;shown=!group.explicit_solo;pending=false;
if(group.explicit_solo){counter=0;queried.clear();members.clear();ready_check={};}
// SMSG_PARTY_UPDATE: TC 0x450097 is NOT shifted. The 70291+ main-group insertion is at index 0x98
// (advocaite/TrinityCore d02645dd ClassicOpcodes.cpp, official 70338 capture); 0x470098 was a
// different message, so the stock client never applied our roster.
return LegacyPacket{0x470097,std::move(body)};
}
// Original code-cache writers 0x8b7c10/0x8b78d0. The final required byte (voice-provider
// selector) is absent in the SDK reader.
Bytes source_party_invite_request(std::span<const uint8_t> body,uint32_t address){
Reader r(body);auto party=r.le(1);if(party!=0 && party!=0x80)throw Error("party invite optional padding");
auto a=r.le(1),b=r.le(1),c=r.le(1);
auto n=(a<<1)|(b>>7),realm=((b&127)<<2)|(c>>6);
if((c&63) || !n || n>48 || realm)throw Error("party invite unsupported name/realm");
auto target_address=r.le(4);if(target_address && target_address!=address)throw Error("party invite foreign realm");
if(unpack_guid(r,16)!=Bytes(16))throw Error("party invite GUID not name-qualified");
auto chars=r.take(n);std::string name(chars.begin(),chars.end());
if(!std::all_of(name.begin(),name.end(),[](unsigned char x){return (x>='A'&&x<='Z')||(x>='a'&&x<='z');}))throw Error("party invite invalid Source name");
// The stock writer (C_PartyInfo.InviteUnit) always appends the voice-provider selector u8
// (raw state 0->1, 1->2, else 0; Codex 2026-11-21 static writer trace), then PartyIndex
// when its bit is set. Tortoise has no voice chat and CMSG_GROUP_INVITE carries only the
// name, so the selector is validated and not forwarded; PartyIndex must be home (0).
if(r.left()!=(party?2u:1u))throw Error("party invite tail length");
if(r.le(1)>2)throw Error("party invite voice provider selector bound");
if(party && r.le(1)!=0)throw Error("party invite non-home category");
r.end();Bytes out;cstring(out,name);return out;
}
bool source_party_invite_response(std::span<const uint8_t> body){
Reader r(body);auto bits=r.le(1);if(bits&31)throw Error("party response padding");
if((bits&0x80) && r.le(1)!=0)throw Error("party response category");
if((bits&0x20) && r.le(1)!=0)throw Error("party response role");
r.end();return bits&0x40;
}
uint64_t apply_party_member_stats(std::map<uint64_t,PartyMemberState>& members,std::span<const uint8_t> body){
// Source WorldSession::BuildPartyMemberStatsPacket field order and widths.
Reader r(body);auto packed=unpack_guid(r,8);uint64_t guid=0;
for(size_t i=0;i<8;++i)guid|=uint64_t(packed[i])<<(8*i);
auto mask=uint32_t(r.le(4));
if(!guid || guid>UINT32_MAX || (mask>>21))throw Error("party member stats identity/mask bound");
if(!members.contains(guid) && members.size()>=40)throw Error("party member cache bound");
auto& m=members[guid];
auto auras=[&](std::array<uint16_t,48>& slots,bool negative){
uint64_t bits=negative?uint64_t(r.le(2))<<32:r.le(4);
for(unsigned i=negative?32:0;i<(negative?48u:32u);++i)if(bits&(uint64_t(1)<<i))slots[i]=uint16_t(r.le(2));
};
if(mask&0x1)m.status=uint8_t(r.le(1));
if(mask&0x2)m.health=uint16_t(r.le(2));
if(mask&0x4)m.max_health=uint16_t(r.le(2));
if(mask&0x8)m.power_type=uint8_t(r.le(1));
if(mask&0x10)m.power=uint16_t(r.le(2));
if(mask&0x20)m.max_power=uint16_t(r.le(2));
if(mask&0x40)m.level=uint16_t(r.le(2));
if(mask&0x80)m.zone=uint16_t(r.le(2));
if(mask&0x100){m.x=uint16_t(r.le(2));m.y=uint16_t(r.le(2));}
if(mask&0x200)auras(m.auras,false);
if(mask&0x400)auras(m.auras,true);
if(mask&0x800){m.pet=r.le(8);if(!m.pet){m.pet_name.clear();m.pet_model=m.pet_health=m.pet_max_health=0;m.pet_auras={};}}
if(mask&0x1000)m.pet_name=r.cstring(48);
if(mask&0x2000)m.pet_model=uint16_t(r.le(2));
if(mask&0x4000)m.pet_health=uint16_t(r.le(2));
if(mask&0x8000)m.pet_max_health=uint16_t(r.le(2));
if(mask&0x10000)m.pet_power_type=uint8_t(r.le(1));
if(mask&0x20000)(void)r.le(2); // 70334 PetStats has no power fields.
if(mask&0x40000)(void)r.le(2);
if(mask&0x80000)auras(m.pet_auras,false);
if(mask&0x100000)auras(m.pet_auras,true);
r.end();
// An offline member keeps no live values (source FULL reply with STATUS only).
if((mask&0x1) && !(m.status&1)){auto status=m.status;m=PartyMemberState{};m.status=status;}
return guid;
}
Bytes party_member_state_input(const PartyMemberState& m,const Guid128& member,const Guid128* pet){
Bytes out;le(out,9,1);append(out,Bytes(member.begin(),member.end()));
le(out,m.level,2);le(out,m.status,1);le(out,m.health,4);le(out,m.max_health,4);
// Source power types 0-3 equal the native ones; happiness (4) is pet-only.
le(out,m.power_type<=3?m.power_type:0,1);le(out,m.power,2);le(out,m.max_power,2);
le(out,m.zone,2);le(out,m.x,2);le(out,m.y,2);
auto auras=[&](const std::array<uint16_t,48>& slots){
uint8_t n=0;for(auto s:slots)n+=s!=0;le(out,n,1);
// Native AuraFlags: POSITIVE 0x02 for source slots 0-31, NEGATIVE 0x10 for 32-47.
for(unsigned i=0;i<48;++i)if(slots[i]){le(out,slots[i],4);le(out,i<32?0x02:0x10,2);}
};
auras(m.auras);
const bool with_pet=m.pet && pet;
le(out,with_pet?1:0,1);
if(with_pet){append(out,Bytes(pet->begin(),pet->end()));cstring(out,m.pet_name);le(out,m.pet_model,2);
le(out,m.pet_health,4);le(out,m.pet_max_health,4);auras(m.pet_auras);}
return out;
}