Files
WoW-Hybrid-gateway/tests/native_spell_channel_test.cpp

168 lines
9.4 KiB
C++

#include "optional_test_codec.hpp"
#include "world_translation.hpp"
#include "presentation_clock.hpp"
#include "gateway/cast_visual_profile.hpp"
#include <bit>
#include <dlfcn.h>
#include <iostream>
#include <source_location>
using namespace gateway;
namespace {
constexpr uint64_t self = 42, peer = 19;
constexpr uint16_t realm = 57;
constexpr uint32_t spell = 5143;
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok) throw Error("channel fixture line " + std::to_string(at.line()));
}
Bytes packed64(uint64_t guid) {
Bytes raw; le(raw, guid, 8);
return pack_guid(raw);
}
using Fields = std::map<uint16_t, uint32_t>;
LegacyPacket object(uint64_t guid, Fields fields, bool create = false) {
Bytes packet; le(packet, 1, 4); packet.push_back(0);
packet.push_back(create ? 2 : 0); append(packet, packed64(guid));
if (create) {
packet.push_back(4); packet.push_back(0x20); // PLAYER, living movement
le(packet, 0, 4); le(packet, 100, 4);
for (float v : {1.f, 2.f, 3.f, .25f}) le(packet, std::bit_cast<uint32_t>(v), 4);
le(packet, 0, 4);
for (float v : {2.5f, 7.f, 4.5f, 4.72f, 2.5f, 3.14f}) le(packet, std::bit_cast<uint32_t>(v), 4);
fields.insert({{0, uint32_t(guid)}, {2, 0x19}, {4, std::bit_cast<uint32_t>(1.f)},
{22, 100}, {28, 100}, {34, 5}, {35, 35}, {36, 0x101}, {131, 49}, {132, 49}});
}
std::vector<uint32_t> mask(fields.rbegin()->first / 32 + 1);
for (auto [index, unused] : fields) mask[index / 32] |= 1u << (index % 32);
packet.push_back(uint8_t(mask.size()));
for (uint32_t word : mask) le(packet, word, 4);
for (auto [unused, value] : fields) le(packet, value, 4);
return {0xa9, packet};
}
LegacyPacket name(uint64_t guid) {
Bytes body; le(body, guid, 8); cstring(body, "Peerfixture"); cstring(body, "");
le(body, 1, 4); le(body, 0, 4); le(body, 1, 4);
return {0x51, body};
}
void direct_packets(decltype(&gw_codec_serialize) serialize) {
NativeSpellChannels state;
const auto owner = forever_player_guid(self, realm);
auto codec = [&](uint32_t mode, const Bytes& input) {
Bytes output(64); uint32_t written = 0;
check(serialize(mode, input.data(), input.size(), 0, output.data(), output.size(), &written) == GW_CODEC_OK);
output.resize(written); return output;
};
auto wire = [&](const std::string& tail) { auto b = pack_guid(owner); append(b, unhex(tail)); return b; };
auto visual = [](uint32_t s) { check(s == spell); return 4321u; };
auto start = state.event(0x139, unhex("17140000b80b0000"), self, owner, 7000, visual, codec);
check(start.opcode == 0x690022 && start.body == wire("17140000e110000000000000b80b000000"));
auto update = state.event(0x13a, unhex("e2040000"), self, owner, 8000, visual, codec);
check(update.opcode == 0x690023 && update.body == wire("e20400000000"));
for (auto [op, bad] : std::vector<std::pair<uint32_t, Bytes>>{
{0x139, Bytes(7)}, {0x139, Bytes(9)}, {0x139, Bytes(8)}, {0x139, unhex("ffffffff01000000")},
{0x13a, Bytes(3)}, {0x13a, Bytes(5)}}) {
bool rejected = false;
try { state.event(op, bad, self, owner, 1, visual, codec); } catch (const Error&) { rejected = true; }
check(rejected);
}
}
Bytes values_block(const Guid128& owner, unsigned mask, const Guid128& target,
uint32_t id, uint32_t visual, uint32_t start, uint32_t duration) {
// Independent wire oracle from UnitData::WriteUpdate / WriteCompleteDynamicFieldUpdateMask.
Bytes values = unhex("0000032000000001");
uint32_t fields = 1 | ((mask & 1) ? 1u << 27 : 0u) | ((mask & 2) ? 1u << 5 : 0u);
for (int shift : {24, 16, 8, 0}) values.push_back(uint8_t(fields >> shift));
values.push_back(0);
if (mask & 2) {
if (target == Guid128{}) append(values, Bytes(4));
else { append(values, unhex("0000000180")); append(values, pack_guid(target)); }
}
if (mask & 1) {
le(values, id, 4); le(values, visual, 4); le(values, 0, 4); le(values, start, 4); le(values, duration, 4);
}
Bytes record{0}; append(record, pack_guid(owner)); le(record, values.size(), 4); append(record, values);
Bytes packet; le(packet, 1, 2); le(packet, 1, 4); packet.push_back(0x80);
le(packet, record.size(), 4); append(packet, record);
return packet;
}
bool has(const std::vector<LegacyPacket>& packets, uint32_t opcode, const Bytes& bytes) {
return std::any_of(packets.begin(), packets.end(), [&](auto& p){ return p.opcode == opcode && p.body == bytes; });
}
void world_packets(decltype(&gw_codec_serialize) codec) {
NativeWorldTranslation world{};
world.selected.guid = self; world.selected.cls = 8; world.selected.race = 1; world.realm = realm;
world.location = WorldLocation{1, {}}; world.self_created = true; world.entities.self_presentation_ready = true;
const auto self_guid = forever_player_guid(self, realm), peer_guid = forever_player_guid(peer, realm);
const auto visual = cast_visual_profile::link(spell);
const LegacyPacket start{0x139, unhex("17140000b80b0000")}, delay{0x13a, unhex("e2040000")}, stop{0x13a, Bytes(4)};
auto dispatch = [&](const LegacyPacket& source) {
auto bundle = world.translate(source, 0x39010039, 1, codec);
Reader r(bundle); auto count = r.le(4); check(count < 100);
std::vector<LegacyPacket> packets;
for (size_t i = 0; i < count; ++i) {
auto opcode = uint32_t(r.le(4)); packets.push_back({opcode, r.take(r.le(4))});
}
r.end(); return packets;
};
auto before = native_presentation_milliseconds();
auto begin = dispatch(start);
auto after = native_presentation_milliseconds();
check(begin.size() == 2 && begin[0].opcode == 0x690022 && begin[1].opcode == 0x5e0000);
Bytes expected = pack_guid(self_guid); le(expected, spell, 4); le(expected, visual, 4);
le(expected, 0, 4); le(expected, 3000, 4); expected.push_back(0); check(begin[0].body == expected);
auto& body = begin[1].body; check(body.size() >= 20); Reader timing(std::span<const uint8_t>(body).last(8));
auto tick = uint32_t(timing.le(4)); check(uint32_t(tick-before) <= uint32_t(after-before));
check(body == values_block(self_guid, 1, {}, spell, visual, tick, 3000));
auto delayed = dispatch(delay); check(delayed.size() == 1 && delayed[0].opcode == 0x690023);
// A low half after a full source create has an implicit zero high half. Hold until this peer is bound.
auto unresolved = dispatch(object(self, {{0x14, peer}}));
check(!has(unresolved, 0x5e0000, values_block(self_guid, 2, peer_guid, 0, 0, 0, 0)));
dispatch(object(peer, {{0x14, self}, {0x90, spell}}, true));
auto joined = dispatch(name(peer));
check(has(joined, 0x5e0000, values_block(self_guid, 2, peer_guid, 0, 0, 0, 0)));
check(has(joined, 0x5e0000, values_block(peer_guid, 3, self_guid, spell, visual, 0, 0)));
check(std::none_of(joined.begin(), joined.end(), [](auto& p) { return p.opcode == 0x690022; }));
const auto clear = values_block(self_guid, 3, {}, 0, 0, 0, 0);
check(has(dispatch(object(self, {{0x14, 0}, {0x90, 0}})), 0x5e0000, clear));
auto ended = dispatch(stop); expected = pack_guid(self_guid); append(expected, Bytes(6));
check(ended.size() == 1 && has(ended, 0x690023, expected)); // Public interrupt clear can precede self stop.
check(has(dispatch(object(peer, {{0x90, 0}})), 0x5e0000, values_block(peer_guid, 3, {}, 0, 0, 0, 0)));
dispatch(start);
check(has(dispatch(object(self, {{0x14, peer}})), 0x5e0000, values_block(self_guid, 2, peer_guid, 0, 0, 0, 0)));
Bytes removed; le(removed, peer, 8);
check(has(dispatch({0xaa, removed}), 0x5e0000, values_block(self_guid, 2, {}, 0, 0, 0, 0)));
dispatch(object(peer, {}, true));
check(!has(dispatch(name(peer)), 0x5e0000, values_block(self_guid, 2, peer_guid, 0, 0, 0, 0)));
// Exercise both non-Unit channel target types against real original high-GUID serialization.
constexpr uint64_t go = 0xf110000001000001ull, ground = 0xf100000000000001ull;
for (uint64_t raw : {go, ground}) {
const auto target = raw == go ? forever_game_object_guid(go, 1, realm, 1) :
forever_persistent_ground_guid(ground, spell, realm, 1);
auto& bound = world.entities.actors[raw];
bound.modern = target; bound.kind = raw == go ? 5 : 6; bound.sent = true;
bound.presentation_realm = realm; bound.presentation_map = 1;
check(has(dispatch(object(self, {{0x14, uint32_t(raw)}, {0x15, uint32_t(raw >> 32)}})),
0x5e0000, values_block(self_guid, 2, target, 0, 0, 0, 0)));
}
check(!world.entities.spell_channels.empty());
world.entities.set_action_scope(self, realm, 0); check(world.entities.spell_channels.empty());
world.self_created = false; check(dispatch(start).empty());
}
}
int main(int argc, char** argv) {
try {
check(argc == 2);
if (test_fixture::optional_codec_missing(argv[1])) return 77;
auto lib = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);
if (!lib) throw Error(dlerror());
auto codec = reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(lib, "gw_codec_serialize"));
check(codec != nullptr); direct_packets(codec); world_packets(codec); dlclose(lib);
std::cout << "Synthetic self channel events and self/peer UnitData match the original wire.\n";
} catch (const std::exception& e) { std::cerr << e.what() << '\n'; return 1; }
}