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

114 lines
5.2 KiB
C++

// Exercise the normal world translator with fabricated items, offers and actors.
#include "world_translation.hpp"
#include <cstring>
#include <iostream>
#include <stdexcept>
using namespace gateway;
static void require(bool condition, const char* reason) {
if (!condition) throw std::runtime_error(reason);
}
// Expose the values delivered to the native codec at its existing ABI.
static int projection(uint32_t mode, const uint8_t* in, uint32_t size, uint32_t,
uint8_t* out, uint32_t capacity, uint32_t* written) {
if (mode != GW_CODEC_TRADE_EVENT) return GW_CODEC_ARGUMENT;
*written = size;
if (capacity < size) return GW_CODEC_CAPACITY;
std::memcpy(out, in, size);
return GW_CODEC_OK;
}
static Bytes word(uint32_t value) { Bytes out; le(out, value, 4); return out; }
static Bytes trade_status(unsigned code, uint64_t player = 0) {
Bytes out = word(code);
if (code == 1) le(out, player, 8);
return out;
}
static Bytes offer(unsigned side, uint32_t gold) {
Bytes out{uint8_t(side)};
le(out, 7, 4); le(out, 7, 4); le(out, gold, 4); le(out, 0, 4);
for (unsigned slot = 0; slot != 7; ++slot) {
le(out, slot, 1);
out.resize(out.size() + 60, 0);
}
return out;
}
static Bytes single(const Bytes& bundle, uint32_t opcode) {
Reader r(bundle);
require(r.le(4) == 1 && r.le(4) == opcode, "one expected native packet");
auto result = r.take(r.le(4));
r.end();
return result;
}
int main() { try {
NativeWorldTranslation w{};
w.selected.guid = 10; w.realm = 70; w.self_created = true;
w.location = WorldLocation{1, {0, 0, 0, 0}};
const uint64_t first = 20, second = 30;
const auto status = [&](unsigned code, uint64_t player = 0) {
return w.translate({0x120, trade_status(code, player)}, 0x10046, 0, projection);
};
const auto present = [&](unsigned side, uint32_t gold) {
auto packet = single(w.translate({0x121,offer(side,gold)},0x10046,0,projection),0x470016);
Reader r(packet);
require(r.le(1)==1 && r.le(1)==side && r.le(4)==w.trade.id,"offer side and trade ID");
r.le(4);
auto revision = uint32_t(r.le(4));
require(r.le(8)==gold,"displayed offer amount");
return revision;
};
const auto accepted = [&](uint32_t revision) {
const auto p = w.gameplay(0x3e0004,word(revision));
require(p.opcode == 0x11a && p.body == word(0),"normal source accept");
w.gameplay_sent(0x3e0004,word(revision));
};
const auto stale = [&](uint32_t revision) {
try { w.gameplay(0x3e0004,word(revision)); }
catch (const NativeWorldTranslation::TradeOfferChanged&) { return; }
throw std::runtime_error("mismatched partner revision accepted");
};
const auto closed = [&](uint32_t revision) {
require(!w.trade.open,"closed trade expected");
try { w.gameplay(0x3e0004,word(revision)); }
catch (const Error&) { return; }
throw std::runtime_error("closed trade accepted");
};
closed(1);
status(1,first); status(2);
require(w.trade.partner==first && w.trade.open,"recipient proposal and open");
accepted(1); // Empty opening offer on both sides, as in native TradeData.
const auto first_offer=present(1,25);
require(first_offer!=1,"changed partner offer");
const auto own=present(0,50);
require(own!=first_offer,"distinct own and partner revisions");
accepted(first_offer); stale(own);
require(present(1,25)==first_offer,"unchanged partner snapshot keeps its revision");
const auto next=present(1,75);
require(next!=first_offer,"updated partner snapshot");
stale(first_offer);
w.trade.notify_changed=true; // Transport sets this only on TradeOfferChanged.
auto notice=single(w.local_presentation(0x10046,projection),0x470017);
require(notice[0]==0 && notice[1]==9 && !w.trade.notify_changed,"native STATE_CHANGED once");
accepted(next);
require(w.gameplay(0x3e0005,{}).opcode==0x11b,"normal unaccept");
w.gameplay_sent(0x3e0005,{});
status(7); accepted(next);
status(10); accepted(next); // Source keeps the interrupted trade on the distance check.
status(3); closed(next);
const auto peer=forever_player_guid(second,w.realm);
auto& actor=w.entities.actors[second]; actor.modern=peer; actor.sent=true;
auto target=pack_guid(peer);
auto request=w.gameplay(0x3e0000,target);
require(request.opcode==0x116 && !w.trade.partner,"initiate waits for successful send");
w.gameplay_sent(0x3e0000,target); status(2);
require(w.trade.partner==second && w.trade.open,"initiating player also tracks partner");
stale(next);
const auto other=present(1,75);
require(other!=next,"new session's presented revision");
require(w.gameplay(0x3e0006,{}).opcode==0x11c,"native cancel");
w.gameplay_sent(0x3e0006,{}); closed(other);
status(1,first);status(2);accepted(1);status(8);closed(1);
status(1,second);status(0);closed(1);
std::cout << "PASS incoming/outgoing trade, both offer sides, unchanged snapshots, normal accept/unaccept, stale feedback, cancellation and completion\n";
return 0;
} catch (const std::exception& e) { std::cerr<<e.what()<<'\n'; return 1; } }