Files
WoW-Hybrid-gateway/native/mailbox_world_binding.hpp
2026-11-23 03:28:32 +02:00

59 lines
3.1 KiB
C++

#pragma once
#include "entity_translation.hpp"
#include "gateway/mailbox_translation.hpp"
#include <algorithm>
#include <map>
#include <optional>
#include <type_traits>
// Pure world binding for a client MailGetList. `templates` contains only
// Source type-19 query replies that passed native template publication.
namespace mailbox_world {
struct Context {
bool self_created=false,pending_teleport=false,far_transfer=false,logout_complete=false;
uint16_t realm=0;
uint32_t map=0;
};
struct Binding {
gateway::MailboxActor actor;
gateway::MailboxTemplateEvidence evidence;
};
// One template query per ENTRY serves every spawn of it (the client asks once per
// entry and later answers from gameobjectcache.wdb), so evidence is matched by entry.
// `cached` supplies reviewed type-19 evidence when this session saw no query at all.
template<class Cached=std::nullptr_t>
inline Binding bind(std::span<const uint8_t> request,
const std::map<uint64_t,NativeEntityTranslation::Actor>& actors,
const std::map<uint64_t,gateway::MailboxTemplateEvidence>& templates,
Context context,Cached&& cached=nullptr) {
gateway::Reader reader(request);
const auto raw=unpack_guid(reader,16);reader.end();
gateway::Guid128 guid{};std::copy(raw.begin(),raw.end(),guid.begin());
if(!context.self_created || context.pending_teleport || context.far_transfer ||
context.logout_complete || !context.realm)
throw gateway::Error("mailbox outside current source world");
for(const auto& [source,actor]:actors){
if(actor.modern!=guid)continue;
std::optional<gateway::GameObjectTemplateReply> reply;
if(auto own=templates.find(source);own!=templates.end() && own->second.generation==actor.generation)
reply=own->second.source;
else for(const auto& [other,evidence]:templates)
if(evidence.source.entry==actor.entry && evidence.map==context.map && evidence.realm==context.realm){reply=evidence.source;break;}
if constexpr(!std::is_same_v<std::decay_t<Cached>,std::nullptr_t>)if(!reply)reply=cached(actor.entry);
if(!reply)throw gateway::Error("mailbox missing published Source template");
if(actor.kind!=5 || !actor.sent || !actor.eligible || !actor.generation ||
actor.presentation_realm!=context.realm || actor.presentation_map!=context.map ||
actor.modern!=gateway::forever_game_object_guid(source,actor.entry,context.realm,context.map) ||
!reply->found || reply->type!=19 || reply->entry!=actor.entry)
throw gateway::Error("mailbox outside current published type-19 binding");
gateway::MailboxTemplateEvidence evidence{source,actor.generation,context.map,context.realm,*reply};
gateway::MailboxActor bound;
bound.source_guid=source;bound.generation=actor.generation;bound.entry=actor.entry;
bound.map=context.map;bound.realm=context.realm;bound.native_guid=actor.modern;
bound.sent=actor.sent;bound.eligible=actor.eligible;
return {bound,evidence};
}
throw gateway::Error("mailbox actor missing");
}
}