707 lines
46 KiB
C++
707 lines
46 KiB
C++
#include <cstdlib>
|
|
#include <cstdarg>
|
|
#include <cstdio>
|
|
#include "diagnostics_journal.hpp"
|
|
#include "inventory_translation.hpp"
|
|
#include "object_update_layout_guard.hpp"
|
|
#include "gateway/unit_presentation.hpp"
|
|
#include "serializer.h"
|
|
#include "gateway/turtle_entity_profile.hpp"
|
|
#include "gateway/gameplay.hpp"
|
|
#include "gateway/inventory_result_profile.hpp"
|
|
#include "gateway/keyring_slots.hpp"
|
|
#ifndef GW_ITEM_RENDER_PROFILE_HEADER
|
|
#define GW_ITEM_RENDER_PROFILE_HEADER "gateway/item_render_profile.hpp"
|
|
#endif
|
|
#include GW_ITEM_RENDER_PROFILE_HEADER
|
|
#include <algorithm>
|
|
namespace{
|
|
static void gw_inv_log(const char* fmt,...){ // appended to GW_NATIVE_STARTUP_LOG (bank-sort error hunt)
|
|
const char* path=std::getenv("GW_NATIVE_STARTUP_LOG");if(!path || !*path)return;
|
|
FILE* file=std::fopen(path,"a");if(!file)return;
|
|
va_list args;va_start(args,fmt);std::vfprintf(file,fmt,args);va_end(args);std::fclose(file);
|
|
}
|
|
}
|
|
namespace F=gateway::turtle_entity_fields;
|
|
using namespace gateway;
|
|
Bytes NativeInventoryTranslation::snapshot() const{
|
|
Bytes b;for(auto v:{2u,1344u,uint32_t(items.size()),uint32_t(std::min<size_t>(items.size(),32)),uint32_t(items.size()>32),value_updates,creates,removals})le(b,v,4);
|
|
unsigned n=0;for(const auto& [guid,u]:items){if(n++==32)break;auto f=[&](unsigned i){auto p=u.fields.find(i);return p==u.fields.end()?0u:p->second;};
|
|
le(b,guid,8);le(b,uint64_t(f(F::ITEM_FIELD_CONTAINED))|(uint64_t(f(F::ITEM_FIELD_CONTAINED+1))<<32),8);
|
|
for(auto index:{F::OBJECT_FIELD_ENTRY,F::ITEM_FIELD_STACK_COUNT,F::ITEM_FIELD_DURABILITY,F::ITEM_FIELD_MAXDURABILITY,F::ITEM_FIELD_ENCHANTMENT+3,F::ITEM_FIELD_ENCHANTMENT+4})le(b,f(index),4);
|
|
}
|
|
b.resize(1312);
|
|
for(auto v:{uint32_t(templates.size()),uint32_t(queries.size()),uint32_t(requests.size()),profile_replies,profile_missing,last_profile,cooldown_packets,cooldown_modifier_blocks})le(b,v,4);
|
|
return b;
|
|
}
|
|
// Source Player::ApplyForAllItems(true): roots0..68, keyring81..112,
|
|
// and children of equipped inventory/bank bags. Buyback roots69..80 excluded.
|
|
std::vector<std::pair<uint64_t,uint32_t>> NativeInventoryTranslation::carried_stacks(uint64_t self, uint32_t entry) const {
|
|
auto word=[](const auto& fields, unsigned i) {auto it=fields.find(i);return it==fields.end()?0u:it->second;};
|
|
auto pair=[&](const auto& fields, unsigned i) {return uint64_t(word(fields,i))|(uint64_t(word(fields,i+1))<<32);};
|
|
std::vector<std::pair<uint64_t,uint32_t>> out;
|
|
auto take=[&](uint64_t guid,uint64_t container) {
|
|
if(!guid)return;
|
|
auto it=items.find(guid);if(it==items.end())return;
|
|
const auto& fields=it->second.fields;
|
|
if(pair(fields,F::ITEM_FIELD_OWNER)!=self || pair(fields,F::ITEM_FIELD_CONTAINED)!=container)return;
|
|
if(word(fields,F::OBJECT_FIELD_ENTRY)!=entry)return;
|
|
out.push_back({guid,std::max(1u,word(fields,F::ITEM_FIELD_STACK_COUNT))});
|
|
};
|
|
for(unsigned slot=23;slot<39;++slot)take(pair(player_fields,F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot),self);
|
|
for(unsigned slot=19;slot<23;++slot) {
|
|
auto id=pair(player_fields,F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot);auto bag=items.find(id);
|
|
if(!id || bag==items.end() || bag->second.type!=2)continue;
|
|
auto size=std::min(36u,word(bag->second.fields,F::CONTAINER_FIELD_NUM_SLOTS));
|
|
for(unsigned j=0;j<size;++j)take(pair(bag->second.fields,F::CONTAINER_FIELD_SLOT_1+2*j),id);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::map<uint32_t, uint64_t> NativeInventoryTranslation::quest_item_counts(uint64_t self) const {
|
|
constexpr unsigned keyring_first = F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1 + 24;
|
|
auto word=[](const auto& fields, unsigned i) {auto it=fields.find(i);return it==fields.end()?0u:it->second;};
|
|
auto pair=[&](const auto& fields, unsigned i) {return uint64_t(word(fields,i))|(uint64_t(word(fields,i+1))<<32);};
|
|
std::map<uint32_t,uint64_t> counts;std::set<uint64_t> seen;
|
|
auto count=[&](uint64_t guid,uint64_t container) {
|
|
if(!guid || seen.contains(guid))return;
|
|
auto it=items.find(guid);if(it==items.end())return;
|
|
const auto& fields=it->second.fields;
|
|
if(pair(fields,F::ITEM_FIELD_OWNER)!=self || pair(fields,F::ITEM_FIELD_CONTAINED)!=container)return;
|
|
auto entry=word(fields,F::OBJECT_FIELD_ENTRY);if(!entry)return;
|
|
seen.insert(guid);counts[entry]+=word(fields,F::ITEM_FIELD_STACK_COUNT);
|
|
};
|
|
for(unsigned i=F::PLAYER_FIELD_INV_SLOT_HEAD;i<=F::PLAYER_FIELD_BANKBAG_SLOT_END;i+=2)count(pair(player_fields,i),self);
|
|
for(unsigned i=keyring_first;i<keyring_first+64;i+=2)count(pair(quest_keyring_fields,i),self);
|
|
for(unsigned slot=19;slot<69;++slot)if(slot<23 || slot>=63) {
|
|
auto id=pair(player_fields,F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot);auto bag=items.find(id);
|
|
if(!id || bag==items.end() || bag->second.type!=2 || !seen.contains(id))continue;
|
|
auto size=word(bag->second.fields,F::CONTAINER_FIELD_NUM_SLOTS);
|
|
if(size>36)throw Error("quest item container size bound");
|
|
for(unsigned j=0;j<size;++j)count(pair(bag->second.fields,F::CONTAINER_FIELD_SLOT_1+2*j),id);
|
|
}
|
|
return counts;
|
|
}
|
|
NativeInventoryPosition NativeInventoryTranslation::position(uint8_t bag,uint8_t slot) const{
|
|
auto field=[](const auto& fields,unsigned index){auto it=fields.find(index);return it==fields.end()?0u:it->second;};
|
|
auto pair=[&](const auto& fields,unsigned index){return uint64_t(field(fields,index))|(uint64_t(field(fields,index+1))<<32);};
|
|
auto root=[&](uint8_t legacy){
|
|
if(keyring_slots::source(legacy))return pair(player_fields,F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1+24+2*(legacy-keyring_slots::SourceFirst));
|
|
return pair(player_fields,legacy<23?F::PLAYER_FIELD_INV_SLOT_HEAD+2*legacy:
|
|
legacy<39?F::PLAYER_FIELD_PACK_SLOT_1+2*(legacy-23):legacy<63?F::PLAYER_FIELD_BANK_SLOT_1+2*(legacy-39):
|
|
F::PLAYER_FIELD_BANKBAG_SLOT_1+2*(legacy-63));};
|
|
NativeInventoryPosition out{255,0,0};
|
|
if(bag==255){
|
|
if(slot<19)out.slot=slot;
|
|
else if(slot>=30 && slot<34)out.slot=slot-30+19;
|
|
else if(slot>=35 && slot<51)out.slot=slot-35+23;
|
|
else if(slot>=64 && slot<70)out.slot=slot-64+63;
|
|
else if(keyring_slots::native(slot))out.slot=uint8_t(keyring_slots::to_source(slot));
|
|
else throw Error("inventory position outside mapped root slots");
|
|
out.item=root(out.slot);
|
|
}else if(bag==63){
|
|
if(slot>=24)throw Error("inventory position outside source bank root");
|
|
out.slot=39+slot;out.item=root(out.slot);
|
|
}else{
|
|
// Player::GetBagByPos uses inventory bag slots30..33 in Forever;
|
|
// Turtle uses19..22. Slots inside an owned container are zero-based.
|
|
if(bag>=30 && bag<34)out.bag=bag-30+19;
|
|
else if(bag>=64 && bag<70)out.bag=bag-64+63;
|
|
else throw Error("inventory container outside mapped equipped bags");
|
|
out.slot=slot;auto id=root(out.bag);auto container=items.find(id);
|
|
if(container==items.end() || container->second.type!=2)throw Error("inventory container not bound");
|
|
auto slots=field(container->second.fields,F::CONTAINER_FIELD_NUM_SLOTS);
|
|
if(slots>36 || slot>=slots)throw Error("inventory container slot bound");
|
|
out.item=pair(container->second.fields,F::CONTAINER_FIELD_SLOT_1+2*slot);
|
|
if(out.item){auto child=items.find(out.item);if(child==items.end() || pair(child->second.fields,F::ITEM_FIELD_CONTAINED)!=id)throw Error("inventory child/container identity mismatch");}
|
|
}
|
|
if(out.item && !items.contains(out.item))throw Error("inventory item is not owned");
|
|
return out;
|
|
}
|
|
namespace {
|
|
Guid128 owned_item_guid(uint64_t source) {
|
|
Guid128 modern{};
|
|
for(unsigned i=0;i<4;++i)modern[i]=uint8_t(source>>(8*i));
|
|
modern[15]=3u<<2; // HighGuid::Item, low 32 bits of the Source GUID.
|
|
return modern;
|
|
}
|
|
}
|
|
const NativeInventoryTranslation::OwnedBindings& NativeInventoryTranslation::owned_bindings() const & {
|
|
// The projection depends only on the keys. Compare the live key set so
|
|
// staged creates, same-size replacements and complete resets are covered.
|
|
if(items.size()!=owned_binding_cache_.size() ||
|
|
!std::equal(items.begin(),items.end(),owned_binding_cache_.begin(),
|
|
[](const auto& item,const auto& binding){return item.first==binding.first;})) {
|
|
OwnedBindings current;
|
|
for(const auto& [id,record]:items)current.emplace_hint(current.end(),id,owned_item_guid(id));
|
|
owned_binding_cache_.swap(current);
|
|
}
|
|
return owned_binding_cache_;
|
|
}
|
|
std::map<uint64_t,NativeInventoryTranslation::QuestStarter> NativeInventoryTranslation::quest_starters(uint64_t self) const{
|
|
std::map<uint64_t,QuestStarter> result;if(!self)return result;
|
|
auto word=[](const auto& fields,unsigned index){auto i=fields.find(index);return i==fields.end()?0u:i->second;};
|
|
auto pair=[&](const auto& fields,unsigned index){return uint64_t(word(fields,index))|(uint64_t(word(fields,index+1))<<32);};
|
|
auto add=[&](uint64_t id,uint64_t container){
|
|
auto item=items.find(id);auto generation=item_generations.find(id);
|
|
if(item==items.end()||item->second.type!=1||generation==item_generations.end()||!generation->second)return;
|
|
const auto& fields=item->second.fields;
|
|
if(pair(fields,F::ITEM_FIELD_OWNER)!=self||pair(fields,F::ITEM_FIELD_CONTAINED)!=container||!word(fields,F::ITEM_FIELD_STACK_COUNT))return;
|
|
auto entry=word(fields,F::OBJECT_FIELD_ENTRY);auto meta=templates.find(entry);
|
|
if(meta==templates.end()||!meta->second.found||meta->second.entry!=entry||!meta->second.start_quest||meta->second.start_quest>0x1fffffff)return;
|
|
result.emplace(id,QuestStarter{owned_item_guid(id),generation->second,meta->second.start_quest});
|
|
};
|
|
// Equipped/carried items and the four equipped inventory bags only. Bank,
|
|
// buyback, keyring and merely cached owned records are not usable givers.
|
|
for(unsigned slot=0;slot<39;++slot){
|
|
auto field=slot<23?F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot:F::PLAYER_FIELD_PACK_SLOT_1+2*(slot-23);
|
|
add(pair(player_fields,field),self);
|
|
}
|
|
for(unsigned slot=19;slot<23;++slot){
|
|
auto id=pair(player_fields,F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot);auto bag=items.find(id);
|
|
if(bag==items.end()||bag->second.type!=2||pair(bag->second.fields,F::ITEM_FIELD_OWNER)!=self||pair(bag->second.fields,F::ITEM_FIELD_CONTAINED)!=self)continue;
|
|
auto size=word(bag->second.fields,F::CONTAINER_FIELD_NUM_SLOTS);if(size>36)throw Error("quest starter container size bound");
|
|
for(unsigned j=0;j<size;++j)add(pair(bag->second.fields,F::CONTAINER_FIELD_SLOT_1+2*j),id);
|
|
}
|
|
return result;
|
|
}
|
|
uint8_t NativeInventoryTranslation::buyback_slot(uint32_t modern_slot) const{
|
|
if(modern_slot<72 || modern_slot>=84)throw Error("buyback outside pinned native slots");
|
|
auto index=F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1+2*(modern_slot-72);
|
|
auto field=[&](unsigned i){auto it=player_fields.find(i);return it==player_fields.end()?0u:it->second;};
|
|
uint64_t id=uint64_t(field(index))|(uint64_t(field(index+1))<<32);
|
|
if(!id || !items.contains(id))throw Error("buyback item outside authenticated owned state");
|
|
return uint8_t(69+modern_slot-72);
|
|
}
|
|
LegacyPacket NativeInventoryTranslation::use_item(std::span<const uint8_t> body,const std::map<uint64_t,Guid128>& actors,bool gameobject_target) const{
|
|
auto request=forever_item_use(body);
|
|
if((request.spell.target_flags&0x800) && !gameobject_target)throw Error("item use gameobject target not qualified");
|
|
auto from=position(request.bag,request.slot);
|
|
if(!from.item)throw Error("item use source is empty");
|
|
auto known=actors;
|
|
for(const auto& [id,guid]:owned_bindings())known[id]=guid;
|
|
if(request.item!=known.at(from.item))throw Error("item use source GUID/slot mismatch");
|
|
return {0xab,turtle_item_use(request,from.bag,from.slot,known)};
|
|
}
|
|
LegacyPacket NativeInventoryTranslation::gameplay(uint32_t opcode,std::span<const uint8_t> body) const{
|
|
if(opcode==0x3e016c){
|
|
// Pinned70334 DestroyItem::Read is Count32, ContainerId8, SlotNum8.
|
|
// No ItemGUID is transmitted: resolve the position through authenticated
|
|
// owned inventory, including source/container identity, before sending.
|
|
Reader r(body);auto quantity=r.le(4);auto bag=r.le(1),slot=r.le(1);r.end();
|
|
auto from=position(bag,slot);
|
|
if(!from.item)throw Error("empty inventory destroy source");
|
|
auto item=items.find(from.item);auto stack=item->second.fields.find(F::ITEM_FIELD_STACK_COUNT);
|
|
if(stack==item->second.fields.end() || !stack->second || quantity>255 || quantity>stack->second)
|
|
throw Error("destroy outside authenticated source stack/count bounds");
|
|
// Both source and native handlers use zero for the whole stack.
|
|
// Source reads three additional bytes but never consumes their values.
|
|
// Leave server unequip/indestructible/progression checks authoritative;
|
|
// the owned cache changes only after its real response/update arrives.
|
|
return {0x111,Bytes{from.bag,from.slot,uint8_t(quantity),0,0,0}};
|
|
}
|
|
|
|
if(opcode==0x3e0213 || opcode==0x3e0214){
|
|
// OpenItem {u8 Slot, u8 PackSlot} -> Tortoise 0xAC {bag, slot}; ReadItem {u8 PackSlot, u8 Slot} -> 0xAD.
|
|
Reader r(body);uint8_t bag,slot;
|
|
if(opcode==0x3e0213){slot=uint8_t(r.le(1));bag=uint8_t(r.le(1));}else{bag=uint8_t(r.le(1));slot=uint8_t(r.le(1));}
|
|
r.end();auto from=position(bag,slot);
|
|
if(!from.item)throw Error("open/read item source is empty");
|
|
return {opcode==0x3e0213?0xacu:0xadu,Bytes{from.bag,from.slot}};
|
|
}
|
|
if(opcode==0x3f0105){
|
|
// Actual70334 CMSG_SET_AMMO carries an entry, not an inventory GUID
|
|
// or a physical equipment move. The stack remains in its source bag.
|
|
Reader r(body);auto entry=uint32_t(r.le(4));r.end();
|
|
if(entry && std::none_of(items.begin(),items.end(),[&](const auto& row){
|
|
auto it=row.second.fields.find(F::OBJECT_FIELD_ENTRY);
|
|
return it!=row.second.fields.end() && it->second==entry;
|
|
}))throw Error("ammunition selection outside authenticated owned inventory");
|
|
Bytes out;le(out,entry,4);return {0x268,std::move(out)};
|
|
}
|
|
if(opcode!=0x410002 && opcode!=0x410003 && opcode!=0x410007 && opcode!=0x410004 && opcode!=0x410008 && opcode!=0x410006)throw Error("unsupported inventory request");
|
|
Reader r(body);auto header=r.le(1),count=header>>6;
|
|
if(header&63)throw Error("inventory request metadata padding");
|
|
// Source InvUpdate's bounded advisory metadata does not authorize items;
|
|
// actual positions below must resolve through authenticated owned state.
|
|
r.take(2*count);
|
|
if(opcode==0x410002 || opcode==0x410003){
|
|
// AutoBankItem has the additional BankType selector (Character=0).
|
|
if(opcode==0x410003 && r.le(1)!=0)throw Error("unsupported bank type");
|
|
const auto bag=uint8_t(r.le(1));
|
|
const auto slot=uint8_t(r.le(1));
|
|
r.end();
|
|
const auto from=position(bag,slot);
|
|
const bool carried=from.bag==255 ? from.slot>=23 && from.slot<39 : from.bag>=19 && from.bag<23;
|
|
const bool banked=from.bag==255 ? from.slot>=39 && from.slot<63 : from.bag>=63 && from.bag<69;
|
|
if(!from.item || (!carried && !(opcode==0x410002 && banked)))
|
|
throw Error("character bank transfer outside owned storage");
|
|
// Source AutoStoreBankItem chooses the direction from the source position.
|
|
return {opcode==0x410003?0x283u:0x282u,Bytes{from.bag,from.slot}};
|
|
}
|
|
if(opcode==0x410004){auto bag=r.le(1);auto slot=r.le(1);r.end();auto from=position(bag,slot);if(!from.item)throw Error("empty auto-equip source");return {0x10a,Bytes{from.bag,from.slot}};}
|
|
NativeInventoryPosition from{},to{};
|
|
if(opcode==0x410007){auto destination=r.le(1);auto source=r.le(1);from=position(255,source);to=position(255,destination);r.end();}
|
|
else if(opcode==0x410006){auto to_bag=r.le(1),from_bag=r.le(1),to_slot=r.le(1),from_slot=r.le(1);r.end();from=position(from_bag,from_slot);to=position(to_bag,to_slot);}
|
|
else{
|
|
auto from_bag=r.le(1),from_slot=r.le(1),to_bag=r.le(1),to_slot=r.le(1),quantity=r.le(4);r.end();from=position(from_bag,from_slot);to=position(to_bag,to_slot);
|
|
if(!quantity || quantity>255 || !from.item || (from.bag==to.bag && from.slot==to.slot))throw Error("split outside Turtle position/quantity bounds");
|
|
return {0x10e,Bytes{from.bag,from.slot,to.bag,to.slot,uint8_t(quantity)}};
|
|
}
|
|
if((from.bag==to.bag && from.slot==to.slot) || (!from.item && !to.item))throw Error("empty/same inventory swap");
|
|
if(opcode==0x410007)return {0x10d,Bytes{from.slot,to.slot}};
|
|
return {0x10c,Bytes{to.bag,to.slot,from.bag,from.slot}};
|
|
}
|
|
std::vector<LegacyPacket> NativeInventoryTranslation::sort_plan(bool bank) const{
|
|
auto field=[](const auto& fields,unsigned index){auto it=fields.find(index);return it==fields.end()?0u:it->second;};
|
|
auto pair=[&](const auto& fields,unsigned index){return uint64_t(field(fields,index))|(uint64_t(field(fields,index+1))<<32);};
|
|
struct Cell{uint8_t bag,slot;uint64_t guid=0;uint32_t entry=0,count=0;};
|
|
std::vector<Cell> cells;
|
|
auto add_root=[&](uint8_t first,uint8_t last){
|
|
for(unsigned legacy=first;legacy<last;++legacy){
|
|
const unsigned index=legacy<39?F::PLAYER_FIELD_PACK_SLOT_1+2*(legacy-23):F::PLAYER_FIELD_BANK_SLOT_1+2*(legacy-39);
|
|
cells.push_back({255,uint8_t(legacy),pair(player_fields,index)});
|
|
}
|
|
};
|
|
auto add_bags=[&](uint8_t first,uint8_t last){
|
|
for(unsigned bag=first;bag<last;++bag){
|
|
const unsigned index=bag<23?F::PLAYER_FIELD_INV_SLOT_HEAD+2*bag:F::PLAYER_FIELD_BANKBAG_SLOT_1+2*(bag-63);
|
|
const auto id=pair(player_fields,index);if(!id)continue;
|
|
auto container=items.find(id);
|
|
// #93 (owner: bag cleanup does nothing, bank cleanup works): say why a bag stays out of the plan.
|
|
if(container==items.end() || container->second.type!=2){gw_inv_log("GW_INV sort-bag bag=%u skip=%s\n",bag,container==items.end()?"unbound":"not-container");continue;}
|
|
auto tpl=templates.find(field(container->second.fields,F::OBJECT_FIELD_ENTRY));
|
|
if(tpl==templates.end() || tpl->second.bag_family){gw_inv_log("GW_INV sort-bag bag=%u entry=%u skip=%s\n",bag,field(container->second.fields,F::OBJECT_FIELD_ENTRY),tpl==templates.end()?"no-template":"special-family");continue;} // unknown or special-family bag stays as it is
|
|
const auto slots=field(container->second.fields,F::CONTAINER_FIELD_NUM_SLOTS);
|
|
gw_inv_log("GW_INV sort-bag bag=%u entry=%u slots=%u\n",bag,field(container->second.fields,F::OBJECT_FIELD_ENTRY),slots);
|
|
if(slots>36)throw Error("sort container slot bound");
|
|
for(unsigned slot=0;slot<slots;++slot)
|
|
cells.push_back({uint8_t(bag),uint8_t(slot),pair(container->second.fields,F::CONTAINER_FIELD_SLOT_1+2*slot)});
|
|
}
|
|
};
|
|
if(bank){add_root(39,63);add_bags(63,69);}else{add_root(23,39);add_bags(19,23);}
|
|
for(auto& c:cells){
|
|
if(!c.guid)continue;
|
|
auto it=items.find(c.guid);
|
|
if(it==items.end())throw Error("sort cell item is not owned");
|
|
c.entry=field(it->second.fields,F::OBJECT_FIELD_ENTRY);
|
|
c.count=std::max(1u,field(it->second.fields,F::ITEM_FIELD_STACK_COUNT));
|
|
}
|
|
auto max_stack=[&](uint32_t entry){auto t=templates.find(entry);return t==templates.end()?1u:std::max(1u,t->second.stack);};
|
|
std::vector<LegacyPacket> out;
|
|
auto swap=[&](size_t from,size_t to){
|
|
if(out.size()>=512)throw Error("sort plan bound");
|
|
out.push_back({0x10c,Bytes{cells[to].bag,cells[to].slot,cells[from].bag,cells[from].slot}});
|
|
};
|
|
// Phase 1: Turtle Player::SwapItem merges a stack dropped onto a partial stack of the
|
|
// same entry (overflow stays in the source). Merge until each entry has <=1 partial.
|
|
for(size_t i=0;i<cells.size();++i){
|
|
auto& dst=cells[i];if(!dst.entry)continue;const auto max=max_stack(dst.entry);
|
|
for(size_t j=i+1;j<cells.size() && dst.count<max;++j){
|
|
auto& src=cells[j];if(src.entry!=dst.entry || src.count>=max)continue;
|
|
swap(j,i);const auto total=dst.count+src.count;
|
|
if(total<=max){dst.count=total;src=Cell{src.bag,src.slot};}
|
|
else{dst.count=max;src.count=total-max;}
|
|
}
|
|
}
|
|
// Phase 2: stable order by class, quality, level, name. Stackables compare by value
|
|
// (entry,count): a swap onto the same entry's partial stack exchanges counts.
|
|
auto rank=[](uint32_t cls){static constexpr uint8_t order[16]={0,4,1,9,2,7,5,8,11,10,15,6,12,13,14,3};return cls<16?order[cls]:16u;};
|
|
std::vector<Cell> want;for(const auto& c:cells)if(c.entry)want.push_back(c);
|
|
std::stable_sort(want.begin(),want.end(),[&](const Cell& a,const Cell& b){
|
|
auto ta=templates.find(a.entry),tb=templates.find(b.entry);
|
|
const bool ka=ta!=templates.end(),kb=tb!=templates.end();
|
|
if(ka!=kb)return ka;
|
|
if(ka){
|
|
const auto& x=ta->second;const auto& y=tb->second;
|
|
if(rank(x.cls)!=rank(y.cls))return rank(x.cls)<rank(y.cls);
|
|
if(x.subclass!=y.subclass)return x.subclass<y.subclass;
|
|
if(x.quality!=y.quality)return x.quality>y.quality;
|
|
if(x.item_level!=y.item_level)return x.item_level>y.item_level;
|
|
if(x.name!=y.name)return x.name<y.name;
|
|
}
|
|
if(a.entry!=b.entry)return a.entry<b.entry;
|
|
return a.count>b.count;
|
|
});
|
|
auto same=[&](const Cell& a,const Cell& b){
|
|
if(a.entry!=b.entry)return false;
|
|
if(!a.entry)return true;
|
|
return max_stack(a.entry)>1?a.count==b.count:a.guid==b.guid;
|
|
};
|
|
for(size_t i=0;i<cells.size();++i){
|
|
const Cell target=i<want.size()?want[i]:Cell{};
|
|
if(same(cells[i],target))continue;
|
|
size_t j=i+1;while(j<cells.size() && !same(cells[j],target))++j;
|
|
if(j==cells.size())throw Error("sort plan lost an item");
|
|
swap(j,i);
|
|
std::swap(cells[i].guid,cells[j].guid);std::swap(cells[i].entry,cells[j].entry);std::swap(cells[i].count,cells[j].count);
|
|
}
|
|
return out;
|
|
}
|
|
const ItemTemplateReply* NativeInventoryTranslation::peer_template(uint32_t entry) const{
|
|
if(auto row=templates.find(entry);row!=templates.end())return &row->second;
|
|
auto peer=peer_templates.find(entry);
|
|
return peer==peer_templates.end()?nullptr:&peer->second;
|
|
}
|
|
void NativeInventoryTranslation::request_owned_template(uint32_t entry){
|
|
if(templates.contains(entry) || (queries.contains(entry) && !peer_template_queries.contains(entry)))return;
|
|
const auto pending_owned=queries.size()-peer_template_queries.size();
|
|
if(templates.size()+pending_owned>=4096)throw Error("native item metadata bound");
|
|
if(auto cached=peer_templates.extract(entry);!cached.empty()){
|
|
std::erase(peer_template_order,entry);
|
|
templates.insert(std::move(cached));
|
|
return;
|
|
}
|
|
// A real owned item or SELF transmog now needs the full outstanding reply.
|
|
if(peer_template_queries.erase(entry))return;
|
|
auto query=turtle_item_query(entry);
|
|
if(requests.size()>=4096){
|
|
// Only an unsent peer request may yield its queue slot to a genuine owned query.
|
|
uint32_t displaced=0;
|
|
auto queued=std::find_if(requests.begin(),requests.end(),[&](const LegacyPacket& request){
|
|
if(request.opcode!=0x56 || request.body.size()!=12)return false;
|
|
Reader read(request.body);displaced=uint32_t(read.le(4));
|
|
return peer_template_queries.contains(displaced);
|
|
});
|
|
if(queued==requests.end())throw Error("native item query queue bound");
|
|
requests.erase(queued);queries.erase(displaced);peer_template_queries.erase(displaced);
|
|
peer_queries_deferred=true;
|
|
}
|
|
requests.push_back({0x56,std::move(query)});
|
|
queries.insert(entry);
|
|
}
|
|
void NativeInventoryTranslation::request_peer_template(uint32_t entry){
|
|
if(!entry || entry>0x7fffffff || peer_template(entry) || queries.contains(entry))return;
|
|
if(queries.size()>=64 || requests.size()>=4096){peer_queries_deferred=true;return;}
|
|
requests.push_back({0x56,turtle_item_query(entry)});
|
|
queries.insert(entry);peer_template_queries.insert(entry);
|
|
}
|
|
bool NativeInventoryTranslation::equipped_entry(uint32_t entry) const{
|
|
if(!entry)return false;
|
|
for(unsigned slot=0;slot<19;++slot){
|
|
// A transmog appearance is the visible entry, not the owned item.
|
|
const auto visible=player_fields.find(uint16_t(F::PLAYER_VISIBLE_ITEM_1_0+12*slot));
|
|
if(visible!=player_fields.end() && visible->second==entry)return true;
|
|
const auto lo=player_fields.find(F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot),hi=player_fields.find(F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot+1);
|
|
if(lo==player_fields.end())continue;
|
|
const uint64_t id=uint64_t(lo->second)|(hi==player_fields.end()?0:uint64_t(hi->second)<<32);
|
|
const auto item=items.find(id);if(item==items.end())continue;
|
|
const auto e=item->second.fields.find(F::OBJECT_FIELD_ENTRY);
|
|
if(e!=item->second.fields.end() && e->second==entry)return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool NativeInventoryTranslation::cache_pending_template_reply(std::span<const uint8_t> body,bool* peer_requested){
|
|
auto reply=turtle_item_template(body);
|
|
gw_diagnostics::journal().definition(0x58,reply.entry,reply.found,body);
|
|
if(!queries.erase(reply.entry))return false;
|
|
const bool from_peer=peer_template_queries.erase(reply.entry)!=0;
|
|
if(peer_requested)*peer_requested=from_peer;
|
|
++profile_replies;last_profile=reply.entry;if(!reply.found)++profile_missing;
|
|
if(from_peer){
|
|
if(peer_template_order.size()==PeerTemplateCapacity){
|
|
peer_templates.erase(peer_template_order.front());
|
|
peer_template_order.pop_front();
|
|
}
|
|
peer_template_order.push_back(reply.entry);
|
|
peer_templates.emplace(reply.entry,std::move(reply));
|
|
}else templates.insert_or_assign(reply.entry,std::move(reply));
|
|
return true;
|
|
}
|
|
std::vector<LegacyPacket> NativeInventoryTranslation::translate(const LegacyPacket& packet,uint64_t self,uint16_t realm,uint32_t map,bool initial,const NativeEntityTranslation::Codec& codec,bool project_staged_initial_items){
|
|
std::vector<LegacyPacket> out;std::vector<LegacyObjectUpdate> changed;std::set<uint64_t> new_owned_items;
|
|
auto visible_source_display=[&](uint32_t entry){
|
|
// Both callers have proved the corresponding SELF inventory slot. Preserve a cached
|
|
// display on the first projection, before the ordinary transmog query runs below.
|
|
if(peer_templates.contains(entry))request_owned_template(entry);
|
|
if(auto source_template=templates.find(entry);source_template!=templates.end())
|
|
return source_template->second.found && source_template->second.inventory_type==8?
|
|
source_template->second.display:0u;
|
|
return race_slot_hide_profile::source_display(entry);
|
|
};
|
|
if(packet.opcode==0x112){
|
|
// Player::SendEquipError: reason8, optional level32 before two raw
|
|
// item GUIDs and bag8. Forever writer puts level after packed GUIDs.
|
|
Reader r(packet.body);auto legacy=r.le(1);uint32_t level=0;uint64_t first=0,second=0;uint8_t bag=0;
|
|
if(legacy){if(legacy==1)level=uint32_t(r.le(4));first=r.le(8);second=r.le(8);bag=uint8_t(r.le(1));}r.end();
|
|
auto reason=inventory_results::forever(uint8_t(legacy));
|
|
gw_inv_log("GW_INV error legacy=%u native=%d bag=%u first=%llx second=%llx\n",unsigned(legacy),int(reason),unsigned(bag),(unsigned long long)first,(unsigned long long)second);
|
|
if(reason<0)return out;
|
|
Bytes body;le(body,uint32_t(reason),4);
|
|
for(auto id:{first,second}){
|
|
if(id && !items.contains(id))return out;
|
|
Guid128 modern{};if(id){Bytes raw;le(raw,id&0xffffffff,8);le(raw,uint64_t(3)<<58,8);std::copy(raw.begin(),raw.end(),modern.begin());}
|
|
append(body,pack_guid(modern));
|
|
}
|
|
le(body,bag,1);if(reason==1)le(body,level,4);
|
|
// Classic server group68 is shifted twice for build70334 ->6A.
|
|
out.push_back({0x6a0005,std::move(body)});return out;
|
|
}
|
|
if(packet.opcode==0x58){
|
|
bool from_peer=false;
|
|
if(!cache_pending_template_reply(packet.body,&from_peer))return out;
|
|
if(from_peer)return out;
|
|
// A real query reply can complete an equipped transmog appearance after
|
|
// initial SELF. Re-evaluate the cached slots; never create an owned item.
|
|
// Only an equipped or visible (transmog) entry affects them. Bag and bank replies at login
|
|
// otherwise republish every container slot once per owned item.
|
|
if(!player_fields.empty() && equipped_entry(last_profile)){LegacyObjectUpdate update;update.guid=self;update.type=4;update.fields=player_fields;changed.push_back(std::move(update));}
|
|
}
|
|
if(packet.opcode==0x1eb){
|
|
// Turtle ItemHandler::SendItemEnchantTimeUpdate: item64, slot32,
|
|
// seconds32, owner64. Forever ItemPackets::ItemEnchantTimeUpdate
|
|
// reverses slot/time and uses packed128 identities. This separate
|
|
// timer message is required even when ItemData.Enchantment is set.
|
|
Reader r(packet.body);auto item=r.le(8),slot=r.le(4),seconds=r.le(4),owner=r.le(8);r.end();
|
|
if(slot>=7)throw Error("Turtle enchantment timer slot bound");
|
|
if(owner!=self || !items.contains(item))return out;
|
|
Guid128 modern{};Bytes raw;le(raw,item&0xffffffff,8);le(raw,uint64_t(3)<<58,8);std::copy(raw.begin(),raw.end(),modern.begin());
|
|
auto body=pack_guid(modern);le(body,seconds,4);le(body,slot,4);append(body,pack_guid(forever_player_guid(self,realm)));
|
|
out.push_back({0x470200,std::move(body)});return out;
|
|
}
|
|
auto forget=[&](uint64_t guid,bool destroyed){
|
|
if(!items.erase(guid))return;
|
|
item_generations.erase(guid);
|
|
applied_appearances.erase(guid);
|
|
++removals;
|
|
std::erase_if(changed,[guid](const auto& u){return u.guid==guid;});
|
|
Guid128 id{};Bytes raw;le(raw,guid&0xffffffff,8);le(raw,uint64_t(3)<<58,8);std::copy(raw.begin(),raw.end(),id.begin());
|
|
std::vector<Guid128> ids{id},empty;
|
|
out.push_back({0x5e0000,forever_object_removal(uint16_t(map),destroyed?ids:empty,destroyed?empty:ids)});
|
|
};
|
|
if(packet.opcode==0xaa){Reader r(packet.body);auto guid=r.le(8);r.end();forget(guid,true);}
|
|
if(packet.opcode==0xa9 || packet.opcode==0x1f6){
|
|
for(const auto& u:turtle_object_updates(packet.opcode,packet.body)){
|
|
if(u.kind==4){
|
|
for(auto guid:u.removed){
|
|
if(guid==self){
|
|
player_fields.erase(F::PLAYER_FLAGS);
|
|
player_fields.erase(F::UNIT_FIELD_BYTES_0);
|
|
for(auto slot:VisibilitySlots)for(unsigned word=0;word<12;++word)
|
|
player_fields.erase(F::PLAYER_VISIBLE_ITEM_1_0+12*slot+word);
|
|
}
|
|
forget(guid,false);
|
|
}
|
|
continue;
|
|
}
|
|
if(u.guid==self && (u.kind==0 || u.type==4)){
|
|
bool touched=false,buyback=false;
|
|
uint32_t keyring_touched=0;
|
|
const bool create=(u.kind==2 || u.kind==3) && u.type==4;
|
|
if(create){
|
|
// A genuine owned Source create replaces the whole player
|
|
// record; omitted zero words must not retain cached slot,
|
|
// bank, buyback, ammo or visibility state from an earlier
|
|
// world. A partial values update retains omitted words.
|
|
player_fields.clear();
|
|
quest_keyring_fields.clear();
|
|
// A genuine full Source create makes every omitted keyring
|
|
// word zero. Emit those clears as one32-slot native reset.
|
|
for(unsigned i=0;i<keyring_slots::Count*2;++i)
|
|
player_fields[F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1+24+i]=0;
|
|
touched=true;
|
|
player_fields[F::PLAYER_FLAGS]=0;
|
|
for(auto slot:VisibilitySlots)for(unsigned word=0;word<12;++word)
|
|
player_fields[F::PLAYER_VISIBLE_ITEM_1_0+12*slot+word]=0;
|
|
}
|
|
if(auto flags=u.fields.find(F::PLAYER_FLAGS);flags!=u.fields.end()){
|
|
auto old=player_fields.find(F::PLAYER_FLAGS);
|
|
touched|=old!=player_fields.end() &&
|
|
((old->second^flags->second)&(SourceHideHelm|SourceHideCloak));
|
|
player_fields[F::PLAYER_FLAGS]=flags->second;
|
|
}
|
|
// Cache authenticated slot pairs and explicit zero clears. Buyback
|
|
// survives logout on this Turtle server, so initialize it even
|
|
// when the main SELF create has already supplied carried slots.
|
|
for(auto [index,value]:u.fields){
|
|
bool bb=(index>=F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1 && index<F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1+24) ||
|
|
(index>=F::PLAYER_FIELD_BUYBACK_PRICE_1 && index<F::PLAYER_FIELD_BUYBACK_PRICE_1+12) ||
|
|
(index>=F::PLAYER_FIELD_BUYBACK_TIMESTAMP_1 && index<F::PLAYER_FIELD_BUYBACK_TIMESTAMP_1+12);
|
|
const bool keyring=index>=F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1+24 && index<F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1+88;
|
|
if(keyring){quest_keyring_fields[index]=value;keyring_touched|=uint32_t(1)<<((index-F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1-24)/2);}
|
|
if(bb || keyring || index==F::UNIT_FIELD_BYTES_0 || index==F::PLAYER_BYTES_2 || index==F::PLAYER_AMMO_ID || (index>=F::PLAYER_VISIBLE_ITEM_1_CREATOR && index<=F::PLAYER_FIELD_BANKBAG_SLOT_END)){player_fields[index]=value;touched=true;buyback|=bb;}
|
|
}
|
|
if(touched && (!initial || buyback || project_staged_initial_items)){
|
|
LegacyObjectUpdate update;update.guid=self;update.type=4;update.fields=player_fields;
|
|
// The codec needs both cached halves of a touched GUID, but
|
|
// unrelated player deltas must not manufacture keyring events.
|
|
if(!create)for(unsigned i=0;i<keyring_slots::Count;++i)if(!(keyring_touched&(uint32_t(1)<<i))){
|
|
auto first=F::PLAYER_FIELD_VENDORBUYBACK_SLOT_1+24+2*i;
|
|
update.fields.erase(first);update.fields.erase(first+1);
|
|
}
|
|
changed.push_back(std::move(update));
|
|
}
|
|
continue;
|
|
}
|
|
if((u.kind==2 || u.kind==3) && (u.type==1 || u.type==2)){
|
|
auto field=[&](unsigned index){auto it=u.fields.find(index);return it==u.fields.end()?0u:it->second;};
|
|
if((u.guid>>48)!=0x4000)continue;
|
|
if((uint64_t(field(F::ITEM_FIELD_OWNER))|(uint64_t(field(F::ITEM_FIELD_OWNER+1))<<32))!=self){forget(u.guid,true);continue;}
|
|
if(items.size()>=4096 && !items.contains(u.guid))throw Error("native owned-item bound");
|
|
const bool already_known=items.contains(u.guid);
|
|
if(already_known)forget(u.guid,true);
|
|
if(item_generation==UINT64_MAX)throw Error("owned item generation overflow");
|
|
item_generations[u.guid]=++item_generation;
|
|
items[u.guid]=u;auto record=u;
|
|
if(!initial && !already_known && u.type==1)new_owned_items.insert(u.guid);
|
|
auto entry=field(F::OBJECT_FIELD_ENTRY);
|
|
request_owned_template(entry);
|
|
++creates;
|
|
// Initial SELF already emitted type1 creates. Send their full
|
|
// values once to fill enchant/creator state, never duplicate creates.
|
|
if(initial && u.type==1)record.kind=0;
|
|
changed.push_back(std::move(record));continue;
|
|
}
|
|
if(u.kind==0){
|
|
auto item=items.find(u.guid);if(item==items.end())continue;
|
|
const auto prior_entry=item->second.fields.find(F::OBJECT_FIELD_ENTRY);
|
|
const uint32_t old_entry=prior_entry==item->second.fields.end()?0:prior_entry->second;
|
|
++value_updates;
|
|
for(auto [index,value]:u.fields){if(index>=F::CONTAINER_END)throw Error("owned-item field bound");item->second.fields[index]=value;}
|
|
auto field=[&](unsigned index){auto it=item->second.fields.find(index);return it==item->second.fields.end()?0u:it->second;};
|
|
if((uint64_t(field(F::ITEM_FIELD_OWNER))|(uint64_t(field(F::ITEM_FIELD_OWNER+1))<<32))!=self){forget(u.guid,true);continue;}
|
|
if(field(F::OBJECT_FIELD_ENTRY)!=old_entry){
|
|
if(item_generation==UINT64_MAX)throw Error("owned item generation overflow");
|
|
item_generations[u.guid]=++item_generation;
|
|
}
|
|
auto record=item->second;record.kind=0;changed.push_back(std::move(record));
|
|
}
|
|
}
|
|
}
|
|
// A real template can complete during the same authenticated far transfer.
|
|
// SELF is now present, so re-evaluate its cached equipment once; do not
|
|
// synthesize or publish a player update while SELF is absent.
|
|
if(initial && deferred_template_projection){
|
|
if(!player_fields.empty() && std::none_of(changed.begin(),changed.end(),[&](const auto& u){return u.guid==self && u.type==4;})){
|
|
LegacyObjectUpdate update;update.guid=self;update.type=4;update.fields=player_fields;changed.push_back(std::move(update));
|
|
}
|
|
deferred_template_projection=false;
|
|
}
|
|
// A real item create can arrive in a later Source packet than SELF.
|
|
// SELF's serializer had no item create from which to fill VisibleItems;
|
|
// refresh the cached player fields once the owned equipped item exists.
|
|
// If this packet already contains a player projection, it is serialized
|
|
// after the complete packet has populated the owned-item cache.
|
|
if(!new_owned_items.empty() && std::none_of(changed.begin(),changed.end(),[&](const auto& u){return u.guid==self && u.type==4;})){
|
|
auto word=[&](unsigned index){auto it=player_fields.find(index);return it==player_fields.end()?0u:it->second;};
|
|
for(unsigned slot=0;slot<19;++slot){
|
|
auto root=F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot;
|
|
auto guid=uint64_t(word(root))|(uint64_t(word(root+1))<<32);
|
|
if(word(F::PLAYER_VISIBLE_ITEM_1_0+12*slot) && new_owned_items.contains(guid) && items.contains(guid)){
|
|
LegacyObjectUpdate update;update.guid=self;update.type=4;update.fields=player_fields;changed.push_back(std::move(update));break;
|
|
}
|
|
}
|
|
}
|
|
// Initial SELF cannot know an uncatalogued generated transmog entry's
|
|
// display. If its genuine template was already cached, apply the hidden
|
|
// look after SELF now; no later template response is guaranteed to arrive.
|
|
if(initial && std::none_of(changed.begin(),changed.end(),[&](const auto& u){return u.guid==self && u.type==4;})){
|
|
auto word=[&](unsigned index){auto p=player_fields.find(index);return p==player_fields.end()?0u:p->second;};
|
|
const auto visible=word(F::PLAYER_VISIBLE_ITEM_1_0+12*7),identity=word(F::UNIT_FIELD_BYTES_0);
|
|
const auto root=F::PLAYER_FIELD_INV_SLOT_HEAD+2*7;
|
|
const auto owned=uint64_t(word(root))|(uint64_t(word(root+1))<<32);
|
|
if(visible && !race_slot_hide_profile::source_display(visible) && items.contains(owned) &&
|
|
race_slot_hide_profile::hidden(uint8_t(identity),uint8_t(identity>>16),7,visible_source_display(visible))){
|
|
LegacyObjectUpdate update;update.guid=self;update.type=4;update.fields=player_fields;changed.push_back(std::move(update));
|
|
}
|
|
}
|
|
if(!changed.empty()){
|
|
Bytes input;le(input,self,8);le(input,realm,2);le(input,map,4);le(input,changed.size(),4);
|
|
for(auto u:changed){
|
|
if(u.guid==self && u.type==4){
|
|
// Source generated transmog templates are presentation only.
|
|
// Resolve their actual authenticated query display/type through
|
|
// the existing native content batch. Owned ItemData, slot GUIDs,
|
|
// tooltips and outgoing gameplay retain the real equipped item.
|
|
for(unsigned slot=0;slot<19;++slot){
|
|
auto visible=uint16_t(F::PLAYER_VISIBLE_ITEM_1_0+12*slot);
|
|
auto field=u.fields.find(visible);if(field==u.fields.end())continue;
|
|
// Source zero explicitly clears the appearance, even if the
|
|
// owned item or its slot binding has not changed yet.
|
|
if(!field->second)continue;
|
|
auto root=F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot;
|
|
auto word=[&](unsigned index){auto p=player_fields.find(index);return p==player_fields.end()?0u:p->second;};
|
|
auto id=uint64_t(word(root))|(uint64_t(word(root+1))<<32);
|
|
if(!id){field->second=0;continue;}
|
|
auto owned=items.find(id);
|
|
if(owned==items.end()){u.fields.erase(field);continue;}
|
|
auto entry=owned->second.fields.find(F::OBJECT_FIELD_ENTRY);
|
|
if(entry==owned->second.fields.end() || !entry->second){u.fields.erase(field);continue;}
|
|
auto appearance=field->second;
|
|
auto visible_display=visible_source_display(appearance);
|
|
field->second=entry->second;
|
|
if(appearance && appearance!=entry->second){
|
|
request_owned_template(appearance);
|
|
auto query=templates.find(appearance);
|
|
if(query!=templates.end() && query->second.found){
|
|
auto base=templates.find(entry->second);
|
|
if(base!=templates.end() && base->second.found &&
|
|
base->second.cls==query->second.cls &&
|
|
base->second.inventory_type==query->second.inventory_type){
|
|
if(const auto* render=item_render_profile::find(query->second.display,query->second.inventory_type))
|
|
field->second=render->render_item;
|
|
}
|
|
}
|
|
}
|
|
// Change only this outgoing VisibleItems projection. The
|
|
// real item, Source fields, instance transmog and stats
|
|
// remain authoritative and unchanged.
|
|
const auto identity=word(F::UNIT_FIELD_BYTES_0);
|
|
field->second=forever_visible_item_id(0,slot,field->second,uint8_t(identity),
|
|
uint8_t(identity>>16),visible_display);
|
|
}
|
|
}
|
|
le(input,u.guid,8);le(input,u.type,1);le(input,u.kind,1);le(input,u.fields.size(),2);
|
|
for(auto [index,value]:u.fields){le(input,index,2);le(input,value,4);}
|
|
}
|
|
auto result=codec(GW_CODEC_INVENTORY,input);Reader r(result);r.le(2);
|
|
if(r.le(4))out.push_back({0x5e0000,std::move(result)});
|
|
}
|
|
// Item instance appearance metadata is separate from player VisibleItems.
|
|
// Resolve only genuine source transmog templates returned to this owned
|
|
// session. Reuse published native display adapters; never change real items.
|
|
std::map<uint64_t,uint32_t> desired;std::map<uint64_t,uint8_t> desired_slots;
|
|
auto word=[&](unsigned field){auto p=player_fields.find(field);return p==player_fields.end()?0u:p->second;};
|
|
for(unsigned slot=0;slot<19;++slot){
|
|
auto root=F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot;
|
|
auto id=uint64_t(word(root))|(uint64_t(word(root+1))<<32);
|
|
auto owned=items.find(id);if(owned==items.end() || owned->second.type!=1)continue;
|
|
auto entry=owned->second.fields.find(F::OBJECT_FIELD_ENTRY);if(entry==owned->second.fields.end() || !entry->second)continue;
|
|
auto visible=word(F::PLAYER_VISIBLE_ITEM_1_0+12*slot);
|
|
if(!visible || visible==entry->second)continue;
|
|
// Initial SELF does not always emit a second player projection. If
|
|
// its owned item template was already cached, no later base-item reply
|
|
// is guaranteed to revisit the slot, so request the real appearance
|
|
// template as soon as both the visible entry and owned item are known.
|
|
request_owned_template(visible);
|
|
auto query=templates.find(visible),base=templates.find(entry->second);
|
|
if(query==templates.end() || base==templates.end() || !query->second.found || !base->second.found ||
|
|
base->second.cls!=query->second.cls || base->second.inventory_type!=query->second.inventory_type)continue;
|
|
// The visible entry is the transmog source item. Prefer its own native
|
|
// appearance (correct "Transmogrified to" name); a same-display donor
|
|
// only when the client has no appearance for the source itself.
|
|
auto appearance=item_render_profile::modified_appearance(visible);
|
|
if(!appearance)
|
|
if(const auto* render=item_render_profile::find(query->second.display,query->second.inventory_type))
|
|
appearance=item_render_profile::modified_appearance(render->render_item);
|
|
if(appearance){desired[id]=appearance;desired_slots[id]=uint8_t(slot);}
|
|
}
|
|
std::map<uint64_t,uint32_t> delta;
|
|
for(auto [id,appearance]:desired)if(!applied_appearances.contains(id) || applied_appearances.at(id)!=appearance)delta[id]=appearance;
|
|
for(auto [id,appearance]:applied_appearances)if(!desired.contains(id) && items.contains(id))delta[id]=0;
|
|
std::set<uint8_t> slots;
|
|
for(auto [id,slot]:desired_slots){(void)id;slots.insert(slot);}
|
|
std::map<uint8_t,bool> flags;
|
|
for(auto slot:slots)if(!applied_transmog_slots.contains(slot))flags[slot]=true;
|
|
for(auto slot:applied_transmog_slots)if(!slots.contains(slot))flags[slot]=false;
|
|
if(!delta.empty() || !flags.empty()){
|
|
Bytes input;le(input,uint16_t(map),2);le(input,realm,2);le(input,self,8);le(input,delta.size(),2);
|
|
for(auto [id,appearance]:delta){le(input,id,8);le(input,appearance,4);}
|
|
le(input,flags.size(),1);
|
|
for(auto [slot,active]:flags){le(input,slot,1);le(input,active,1);}
|
|
out.push_back({0x5e0000,codec(GW_CODEC_ITEM_APPEARANCE,input)});
|
|
applied_appearances=std::move(desired);applied_transmog_slots=std::move(slots);
|
|
}
|
|
return out;
|
|
}
|