Files
WoW-Hybrid-gateway/tests/native_group_events_test.cpp
2026-11-23 03:28:32 +02:00

128 lines
9.0 KiB
C++

// Source group/raid notifications and native group requests (GW_CODEC_GROUP_EVENT).
#include "world_translation.hpp"
#include "gateway/group.hpp"
#include <algorithm>
#include <dlfcn.h>
#include <filesystem>
#include <iostream>
#include <source_location>
#include <vector>
using namespace gateway;
static void expect(bool v, const std::source_location at = std::source_location::current()) {
if (!v) throw Error("Group event assertion at line " + std::to_string(at.line()));
}
static constexpr uint64_t self_id = 100, peer_id = 101;
static constexpr uint16_t realm_id = 70;
struct Out { uint32_t opcode; Bytes body; };
static std::vector<Out> packets(const Bytes& bundle) {
Reader r(bundle); auto n = r.le(4); std::vector<Out> out;
for (uint64_t i = 0; i < n; ++i) { auto op = uint32_t(r.le(4)); auto size = r.le(4); out.push_back({op, r.take(size_t(size))}); }
r.end(); return out;
}
static bool contains(const Bytes& b, std::string_view text) { return std::search(b.begin(), b.end(), text.begin(), text.end()) != b.end(); }
static Bytes roster() {
Bytes body{1, 0}; le(body, 1, 4); cstring(body, "SyntheticPeer"); le(body, peer_id, 8);
le(body, 1, 1); le(body, 0, 1); le(body, self_id, 8); le(body, 0, 1); le(body, 0, 8); le(body, 2, 1); le(body, 0, 1);
return body;
}
static Bytes packed(uint64_t guid) { Bytes raw; le(raw, guid, 8); return pack_guid(raw); }
static Bytes native_packed(uint64_t guid) { auto g = forever_player_guid(guid, realm_id); return pack_guid(Bytes(g.begin(), g.end())); }
// Source BuildPartyMemberStatsPacket with the given fields.
static Bytes stats(uint32_t mask, uint16_t hp, uint16_t level, uint16_t aura) {
Bytes b = packed(peer_id); le(b, mask, 4);
if (mask & 0x1) le(b, 1, 1);
if (mask & 0x2) le(b, hp, 2);
if (mask & 0x4) le(b, 1000, 2);
if (mask & 0x8) le(b, 0, 1);
if (mask & 0x10) le(b, 500, 2);
if (mask & 0x20) le(b, 800, 2);
if (mask & 0x40) le(b, level, 2);
if (mask & 0x80) le(b, 12, 2);
if (mask & 0x100) { le(b, uint16_t(-8913), 2); le(b, uint16_t(-120), 2); }
if (mask & 0x200) { le(b, 1, 4); le(b, aura, 2); }
return b;
}
int main(int argc, char** argv) { try {
if (argc != 2) throw Error("codec path required");
if (!std::filesystem::exists(argv[1])) { std::cout << "Source codec unavailable\n"; return 77; }
void* lib = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL); if (!lib) throw Error("codec unavailable");
auto codec = reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(lib, "gw_codec_serialize")); expect(codec);
NativeWorldTranslation world{}; world.selected.guid = self_id; world.selected.name = "SyntheticSelf";
world.selected.race = 10; world.selected.cls = 8; world.realm = realm_id; world.self_created = true;
world.location = WorldLocation{1, {0, 0, 0, 0}};
auto tr = [&](NativeWorldTranslation& w, uint32_t op, const Bytes& body) { return packets(w.translate({op, body}, 0x10046, 0, codec)); };
tr(world, 0x7d, roster());
Bytes name; le(name, peer_id, 8); cstring(name, "SyntheticPeer"); cstring(name, ""); le(name, 9, 4); le(name, 0, 4); le(name, 1, 4);
tr(world, 0x51, name);
expect(world.party.shown && world.party.counter);
// The full raid icon list is requested once when the party first appears.
expect(std::count_if(world.requests.begin(), world.requests.end(), [](const auto& p) { return p.opcode == 0x321 && p.body == Bytes{0xff}; }) == 1);
// Member stats: FULL then delta equals one FULL carrying the merged state.
constexpr uint32_t full = 0x3ff;
auto base = world;
auto a = tr(world, 0x2f2, stats(full, 400, 60, 1243));
expect(a.size() == 1 && a[0].opcode == 0x470204);
auto b = tr(world, 0x7e, stats(0x2, 650, 0, 0));
auto merged = tr(base, 0x2f2, stats(full, 650, 60, 1243));
expect(b.size() == 1 && b[0].opcode == 0x470204 && b[0].body == merged[0].body && b[0].body != a[0].body);
// A non-member is never published.
auto stranger = world; auto foreign = stats(0x2, 1, 0, 0); foreign[1] = 0x77;
expect(tr(stranger, 0x7e, foreign).empty());
auto leader = [&] { Bytes n; cstring(n, "SyntheticPeer"); return tr(world, 0x79, n); }();
expect(leader.size() == 1 && leader[0].opcode == 0x4700d7 && contains(leader[0].body, "SyntheticPeer"));
auto decline = [&] { Bytes n; cstring(n, "Nobody"); return tr(world, 0x74, n); }();
expect(decline.size() == 1 && decline[0].opcode == 0x47023c);
auto result = [&](uint32_t code) { Bytes n; le(n, 0, 4); cstring(n, "Nobody"); le(n, code, 4); return tr(world, 0x7f, n); };
expect(result(3).size() == 1 && result(3)[0].opcode == 0x470241 && result(10).empty());
auto ping = [&] { Bytes p; le(p, peer_id, 8); le(p, 0x41200000, 4); le(p, 0x41a00000, 4); return tr(world, 0x1d5, p); }();
expect(ping.size() == 1 && ping[0].opcode == 0x47017b);
auto single = [&] { Bytes p{0, 3}; le(p, peer_id, 8); return tr(world, 0x321, p); }();
expect(single.size() == 1 && single[0].opcode == 0x4700d9);
auto all = [&] { Bytes p{1}; le(p, 0, 1); le(p, peer_id, 8); le(p, 7, 1); le(p, 0xF130000000001234ull, 8); return tr(world, 0x321, p); }();
expect(all.size() == 1 && all[0].opcode == 0x4700d8); // The unbound creature entry is dropped, the player kept.
auto start = tr(world, 0x322, {});
expect(start.size() == 1 && start[0].opcode == 0x47009a && world.party.ready_check.active);
auto answer = [&] { Bytes p; le(p, peer_id, 8); le(p, 1, 1); return tr(world, 0x322, p); }();
expect(answer.size() == 2 && answer[0].opcode == 0x47009b && answer[1].opcode == 0x47009c && !world.party.ready_check.active);
auto only = [&] { Bytes p; le(p, 60000, 4); le(p, 1, 4); return tr(world, 0x286, p); }();
expect(only.size() == 1 && only[0].opcode == 0x47025a);
// Native requests to Source.
Bytes mp{0}; le(mp, 0x41200000, 4); le(mp, 0x41a00000, 4);
auto mping = world.gameplay(0x440077, mp); expect(mping.opcode == 0x1d5 && mping.body.size() == 8);
Bytes rt{0}; append(rt, native_packed(peer_id)); le(rt, 2, 1);
auto target = world.gameplay(0x44007c, rt); Reader tr_(target.body);
expect(target.opcode == 0x321 && tr_.le(1) == 2 && tr_.le(8) == peer_id); tr_.end();
const Bytes icon_req{0, 0, 0, 0xff}; // no PartyIndex, empty packed GUID, Symbol -1
auto icons = world.gameplay(0x44007c, icon_req); expect(icons.opcode == 0x321 && icons.body == Bytes{0xff});
expect(world.gameplay(0x44005e, Bytes{0}).opcode == 0x322);
auto ready = world.gameplay(0x44005f, Bytes{0x40}); expect(ready.opcode == 0x322 && ready.body == Bytes{1});
auto not_ready = world.gameplay(0x44005f, Bytes{0x00}); expect(not_ready.body == Bytes{0});
Bytes sl{0}; append(sl, native_packed(peer_id));
auto set_leader = world.gameplay(0x440076, sl); Reader slr(set_leader.body);
expect(set_leader.opcode == 0x78 && slr.le(8) == peer_id); slr.end();
bool outsider = false; try { Bytes x{0}; append(x, native_packed(555)); (void)world.gameplay(0x440076, x); } catch (const Error&) { outsider = true; }
expect(outsider);
// Raid management: convert, kick, subgroup move/swap by Source name, assistant.
expect(world.gameplay(0x44007a, Bytes{0x80}).opcode == 0x28e);
bool to_party = false; try { (void)world.gameplay(0x44007a, Bytes{0}); } catch (const Error&) { to_party = true; } expect(to_party);
{ auto reset = world.gameplay(0x440094, Bytes{}); expect(reset.opcode == 0x31d && reset.body.empty()); // Reset all instances
bool padded = false; try { (void)world.gameplay(0x440094, Bytes{0}); } catch (const Error&) { padded = true; } expect(padded); }
Bytes kick{0x00, 0x40}; append(kick, native_packed(peer_id)); kick.push_back('x');
auto k = world.gameplay(0x440073, kick); Reader kr(k.body); expect(k.opcode == 0x76 && kr.le(8) == peer_id); kr.end();
Bytes move = native_packed(peer_id); move.push_back(3); move.push_back(0);
auto mv = world.gameplay(0x440078, move); expect(mv.opcode == 0x27e && contains(mv.body, "SyntheticPeer") && mv.body.back() == 3);
Bytes swap{0}; append(swap, native_packed(peer_id)); append(swap, native_packed(self_id));
auto sw = world.gameplay(0x440079, swap); expect(sw.opcode == 0x280 && contains(sw.body, "SyntheticPeer") && contains(sw.body, world.selected.name));
Bytes assist{0x40}; append(assist, native_packed(peer_id));
auto as = world.gameplay(0x44007b, assist); Reader ar(as.body); expect(as.opcode == 0x28f && ar.le(8) == peer_id && ar.le(1) == 1); ar.end();
bool unknown_target = false; try { Bytes x{0x40}; append(x, native_packed(555)); (void)world.gameplay(0x44007b, x); } catch (const Error&) { unknown_target = true; } expect(unknown_target);
world.requests.clear();
Bytes rs{0}; le(rs, 2, 4); append(rs, native_packed(peer_id)); append(rs, native_packed(555));
expect(world.party_stats_request(rs) == 1 && world.requests.size() == 1 && world.requests.front().opcode == 0x27f);
std::cout << "PASS member stats merge, leader, decline, command result, ping, raid targets, ready check, raid-only and six native requests\n";
return 0;
} catch (const std::exception& e) { std::cerr << e.what() << '\n'; return 1; } }