128 lines
5.7 KiB
C++
128 lines
5.7 KiB
C++
#pragma once
|
|
|
|
#include "gateway/object_updates.hpp"
|
|
#include "gateway/legacy_stream.hpp"
|
|
#include "serializer.h"
|
|
#include <map>
|
|
#include <optional>
|
|
|
|
namespace gateway {
|
|
|
|
struct ChannelTiming {
|
|
uint32_t spell = 0, visual = 0, start = 0, duration = 0;
|
|
bool operator==(const ChannelTiming&) const = default;
|
|
};
|
|
|
|
class NativeSpellChannels {
|
|
struct Channel {
|
|
ChannelTiming timing, presented_timing;
|
|
uint64_t target = 0;
|
|
Guid128 presented_target{};
|
|
};
|
|
std::map<uint64_t, Channel> entries;
|
|
|
|
Channel& entry(uint64_t owner) {
|
|
// Same bound as the entity translator: 4096 remote identities plus self.
|
|
if (entries.size() >= 4097 && !entries.contains(owner)) throw Error("channel owner bound");
|
|
return entries[owner];
|
|
}
|
|
|
|
public:
|
|
bool empty() const { return entries.empty(); }
|
|
void clear() { entries.clear(); }
|
|
|
|
void forget(uint64_t guid) {
|
|
entries.erase(guid);
|
|
for (auto& [owner, channel] : entries)
|
|
if (channel.target == guid) channel.target = 0;
|
|
}
|
|
|
|
// All callers observe a real full unit create before sparse values. Omitted halves in that
|
|
// create mean zero. A nonzero target seen before its spell is retained across packets.
|
|
template <typename Visual>
|
|
void observe(const LegacyObjectUpdate& update, bool create, bool existing_self, Visual visual) {
|
|
constexpr uint16_t object = 0x14, spell = 0x90; // Classic UNIT_FIELD_CHANNEL_OBJECT, UNIT_CHANNEL_SPELL
|
|
const auto& fields = update.fields;
|
|
auto id = fields.find(spell), low = fields.find(object), high = fields.find(object + 1);
|
|
if (id != fields.end() && id->second > INT32_MAX) throw Error("invalid source channel spell");
|
|
if (!create && id == fields.end() && low == fields.end() && high == fields.end()) return;
|
|
if (!entries.contains(update.guid) && (id == fields.end() || !id->second) &&
|
|
(low == fields.end() || !low->second) && (high == fields.end() || !high->second)) return;
|
|
|
|
auto& channel = entry(update.guid);
|
|
if (create) {
|
|
if (!existing_self) channel = {};
|
|
channel.target = 0;
|
|
}
|
|
if (create || id != fields.end()) {
|
|
uint32_t next = id == fields.end() ? 0 : id->second;
|
|
if (!next) channel.target = 0;
|
|
if (channel.timing.spell != next) channel.timing = {next, next ? visual(next) : 0, 0, 0};
|
|
}
|
|
if (low != fields.end()) channel.target = (channel.target & 0xffffffff00000000ull) | low->second;
|
|
if (high != fields.end()) channel.target = (channel.target & 0xffffffffull) | (uint64_t(high->second) << 32);
|
|
}
|
|
|
|
// Tortoise sends these messages exclusively to the player caster. No GUID occurs in either body.
|
|
template <typename Visual, typename Codec>
|
|
LegacyPacket event(uint32_t opcode, std::span<const uint8_t> body, uint64_t self,
|
|
const Guid128& player, uint32_t now, Visual visual, Codec codec) {
|
|
Reader r(body);
|
|
|
|
if (opcode == 0x139) {
|
|
const uint32_t spell = r.le(4), duration = r.le(4);
|
|
r.end();
|
|
if (!spell || spell > INT32_MAX) throw Error("invalid source channel spell");
|
|
const uint32_t link = visual(spell);
|
|
Bytes input{4}; append(input, player);
|
|
le(input, spell, 4); le(input, link, 4); le(input, duration, 4);
|
|
auto packet = codec(GW_CODEC_SPELL_CHANNEL, input);
|
|
entry(self).timing = {spell, link, now, duration};
|
|
return {0x690022, std::move(packet)};
|
|
}
|
|
if (opcode != 0x13a) throw Error("unexpected source channel event");
|
|
uint32_t remaining = r.le(4); r.end();
|
|
Bytes input{5}; append(input, player); le(input, remaining, 4);
|
|
auto packet = codec(GW_CODEC_SPELL_CHANNEL, input);
|
|
if (!remaining) {
|
|
if (auto it = entries.find(self); it != entries.end()) {
|
|
it->second.timing = {};
|
|
it->second.target = 0;
|
|
}
|
|
}
|
|
// The original SendChannelUpdate changes public timing only on stop. A positive update
|
|
// carries time remaining exclusively in the direct packet. FailedBy is absent on source.
|
|
return {0x690023, std::move(packet)};
|
|
}
|
|
|
|
template <typename Bind, typename Codec, typename Emit>
|
|
void flush(uint16_t map, Bind bind, Codec codec, Emit emit) {
|
|
for (auto it = entries.begin(); it != entries.end();) {
|
|
auto& c = it->second;
|
|
if (auto owner = bind(it->first, true)) {
|
|
Guid128 target{};
|
|
if (c.timing.spell && c.target) target = bind(c.target, false).value_or(Guid128{});
|
|
const bool timing_changed = c.timing != c.presented_timing;
|
|
const bool target_changed = target != c.presented_target;
|
|
if (timing_changed || target_changed) {
|
|
Bytes input;
|
|
le(input, uint8_t(timing_changed) | (uint8_t(target_changed) << 1), 1);
|
|
append(input, *owner); le(input, map, 2);
|
|
le(input, c.timing.spell, 4); le(input, c.timing.visual, 4);
|
|
le(input, c.timing.start, 4); le(input, c.timing.duration, 4); append(input, target);
|
|
emit(0x5e0000, codec(GW_CODEC_SPELL_CHANNEL, input));
|
|
c.presented_timing = c.timing;
|
|
c.presented_target = target;
|
|
}
|
|
} else {
|
|
// A hidden owner will have a fresh, empty native UnitData on its next create.
|
|
c.presented_timing = {};
|
|
c.presented_target = {};
|
|
}
|
|
if (!c.timing.spell && !c.target) it = entries.erase(it);
|
|
else ++it;
|
|
}
|
|
}
|
|
};
|
|
}
|