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

144 lines
7.7 KiB
C++

#include "world_translation.hpp"
#include "gateway/source_combat_events.hpp"
#include <iostream>
using namespace gateway;
namespace {
constexpr uint64_t self = 42, peer = 19, npc = 0xf130000007000015ull;
constexpr uint32_t counterspell = 2139, frostbolt = 116, fireball = 133;
unsigned checks = 0;
void check(bool ok, const char* message) {
++checks;
if (!ok) throw Error(message);
}
Bytes source(uint64_t caster, uint32_t groups) {
Bytes id;
le(id,caster,8);
Bytes b = pack_guid(id);
le(b,counterspell,4); le(b,groups,4);
return b;
}
void group(Bytes& b, uint32_t effect, uint32_t targets) {
le(b,effect,4); le(b,targets,4);
}
void interrupt(Bytes& b, uint64_t victim, uint32_t interrupted) {
le(b,victim,8); le(b,interrupted,4);
}
std::vector<LegacyPacket> send(NativeWorldTranslation& w, const Bytes& body) {
auto wire = w.translate({0x24c,body},0x46010046,0,nullptr);
Reader r(wire);
std::vector<LegacyPacket> out;
auto count = r.le(4);
while (count--) {
auto op = uint32_t(r.le(4));
out.push_back({op,r.take(r.le(4))});
}
r.end();
return out;
}
Bytes expected(const Guid128& caster, const Guid128& victim, uint32_t interrupted) {
Bytes b = pack_guid(caster);
append(b,pack_guid(victim));
// Spell::SendSpellInterruptLog sets the interrupting spell before the interrupted spell.
le(b,counterspell,4); le(b,interrupted,4); le(b,0,1);
return b;
}
bool is_interrupt(const LegacyPacket& p, const Bytes& payload) {
return p.opcode == 0x69000d && p.body == payload;
}
}
int main() { try {
NativeWorldTranslation w;
w.selected.guid = self; w.self_created = true; w.realm = 70; w.location = WorldLocation{};
const Guid128 me = forever_player_guid(self,w.realm), other = forever_player_guid(peer,w.realm);
const Guid128 creature = forever_creature_guid(npc,7,w.realm,0);
auto bind = [&](uint64_t id, const Guid128& native, uint32_t kind) {
auto& a = w.entities.actors[id];
a.kind = kind; a.modern = native; a.sent = true; a.eligible = true; a.generation = 1;
a.presentation_realm = w.realm; a.presentation_map = 0;
};
bind(peer,other,4); bind(npc,creature,3);
auto single = source(self,1);
group(single,68,1); interrupt(single,npc,frostbolt);
auto out = send(w,single);
check(out.size()==1 && is_interrupt(out[0],expected(me,creature,frostbolt)),"self interrupt preserves both spells and the creature victim");
// Exercise the source's multi-effect framing independently of particular spell data.
auto mixed = source(npc,3);
group(mixed,24,1); le(mixed,742,4);
group(mixed,68,2); interrupt(mixed,self,frostbolt); interrupt(mixed,peer,fireball);
group(mixed,24,1); le(mixed,744,4);
out = send(w,mixed);
check(out.size()==3,"two native interrupt logs and one regular execution log");
check(is_interrupt(out[0],expected(creature,me,frostbolt)),"remote caster and first interrupted unit");
check(is_interrupt(out[1],expected(creature,other,fireball)),"second unit and its distinct interrupted spell");
Bytes regular = pack_guid(creature);
le(regular,counterspell,4); le(regular,1,4); le(regular,24,4);
for (unsigned n : {0,0,0,0,2,0}) le(regular,n,4);
le(regular,742,4); le(regular,744,4); le(regular,0,1);
check(out[2].opcode==0x690033 && out[2].body==regular,"ordinary effects retain native aggregate layout and order");
// The battleground position service clears a previously shown marker list when
// the current map is outside a battleground. Its normal world output comes last.
w.bg_positions_shown=true;
auto with_positions=send(w,mixed);
check(with_positions.size()==4 && is_interrupt(with_positions[0],out[0].body) &&
is_interrupt(with_positions[1],out[1].body) && with_positions[2].body==regular &&
with_positions[3].opcode==0x4d0006 && with_positions[3].body==Bytes(4,0) && !w.bg_positions_shown,
"ordinary pending position output is retained after a complete log group");
// Exercise the same final admission used by world.translate with small capacities.
// Put an ordinary world packet on each side of the log's original insertion point.
const std::vector<LegacyPacket> mandatory{{0x4d0025,{}},{0x4d0006,Bytes(4,0)}};
const source_combat_events::Bindings known{{self,me},{peer,other},{npc,creature}};
auto admitted=[&](const Bytes& input,size_t records,size_t bytes){
auto result=mandatory;
source_combat_events::insert_spell_execute(result,1,input,known,{},records,bytes);
return result;
};
auto only_mandatory=[&](const std::vector<LegacyPacket>& result){
return result.size()==2 && result[0].opcode==0x4d0025 && result[0].body.empty() &&
result[1].opcode==0x4d0006 && result[1].body==Bytes(4,0);
};
size_t exact_size=4+8+8+4; // Bundle count and both mandatory framed packets.
for(const auto& p:out)exact_size+=8+p.body.size();
auto exact=admitted(mixed,5,exact_size);
check(exact.size()==5 && exact[0].opcode==0x4d0025 && is_interrupt(exact[1],out[0].body) &&
is_interrupt(exact[2],out[1].body) && exact[3].opcode==0x690033 && exact[3].body==regular &&
exact[4].opcode==0x4d0006,"exact shared record and byte capacity preserves the insertion point");
check(only_mandatory(admitted(mixed,4,exact_size)),"one record short preserves both mandatory packets");
check(only_mandatory(admitted(mixed,5,exact_size-1)),"one framed byte short preserves both mandatory packets");
check(only_mandatory(admitted(mixed,2,exact_size)),"no record space remains after other output");
check(only_mandatory(admitted(mixed,5,24)),"no byte space remains after other output");
auto terminal=source(npc,2); group(terminal,68,2);
interrupt(terminal,self,frostbolt); interrupt(terminal,peer,fireball);
group(terminal,24,1); le(terminal,742,4);
check(only_mandatory(admitted(terminal,4,exact_size)),"trailing aggregate needs its own slot after both interrupts");
check(admitted(terminal,5,exact_size).size()==5,"trailing aggregate fits with the final slot");
for (size_t n=0; n<mixed.size(); ++n)
check(send(w,{mixed.begin(),mixed.begin()+n}).empty(),"truncated mixed frame must not leak partial output");
auto tail=mixed; tail.push_back(0);
check(send(w,tail).empty(),"mixed frame with trailing data");
auto invalid = [&](uint64_t victim, uint32_t spell) {
auto b = source(self,1); group(b,68,2); interrupt(b,peer,fireball); interrupt(b,victim,spell);
return send(w,b).empty();
};
check(invalid(271,fireball) && invalid(0,fireball),"unknown and empty victims");
check(invalid(npc,0) && invalid(npc,0x80000000u),"absent and out-of-range native spell IDs");
constexpr uint64_t gameobject = 0xf110000001000019ull;
bind(gameobject,forever_game_object_guid(gameobject,1,w.realm,0),5);
check(invalid(gameobject,fireball),"a visible game object cannot have an interrupted cast");
auto unsupported = source(self,2); group(unsupported,68,1); interrupt(unsupported,peer,fireball);
group(unsupported,113,1); le(unsupported,peer,8);
check(send(w,unsupported).empty(),"unmapped later group cannot leak an earlier log");
auto missing = source(271,1); group(missing,68,1); interrupt(missing,peer,fireball);
check(send(w,missing).empty(),"unknown interrupting caster");
++w.entities.actors[npc].presentation_map;
check(send(w,mixed).empty() && send(w,single).empty(),"stale caster or victim");
--w.entities.actors[npc].presentation_map;
w.self_created = false;
check(send(w,single).empty(),"source event before self presentation");
std::cout << checks << " spell-interrupt execution assertions passed\n";
} catch (const std::exception& e) { std::cerr << e.what() << '\n'; return 1; } }