218 lines
10 KiB
C++
218 lines
10 KiB
C++
#include "world_translation.hpp"
|
|
#include <algorithm>
|
|
#include <bit>
|
|
#include <iostream>
|
|
|
|
using namespace gateway;
|
|
|
|
namespace {
|
|
void check(bool value, const char* reason) {
|
|
if (!value) throw Error(reason);
|
|
}
|
|
template<class F> void rejected(F perform) {
|
|
try { perform(); } catch (const Error&) { return; }
|
|
throw Error("expected NPC text request rejection");
|
|
}
|
|
struct Option {
|
|
float weight = 0;
|
|
std::string male = "Greetings $N", female = "Greetings $N";
|
|
uint32_t language = 0;
|
|
std::array<uint32_t, 6> emote{};
|
|
};
|
|
Bytes original(uint32_t id, const std::array<Option, 8>& opts = {}) {
|
|
Bytes b;
|
|
le(b, id, 4);
|
|
for (const auto& o : opts) {
|
|
le(b, std::bit_cast<uint32_t>(o.weight), 4);
|
|
cstring(b, o.male); cstring(b, o.female);
|
|
le(b, o.language, 4);
|
|
for (auto v : o.emote) le(b, v, 4);
|
|
}
|
|
return b;
|
|
}
|
|
Bytes native(uint32_t id, const std::array<Option, 8>& opts, const std::array<uint32_t, 8>& ids) {
|
|
Bytes b;
|
|
const bool present = std::any_of(ids.begin(), ids.end(), [](uint32_t v){ return v != 0; });
|
|
le(b, id, 4); le(b, present ? 0x80 : 0, 1); le(b, present ? 64 : 0, 4);
|
|
if (present) {
|
|
for (const auto& o : opts) le(b, std::bit_cast<uint32_t>(o.weight), 4);
|
|
for (auto v : ids) le(b, v, 4);
|
|
}
|
|
return b;
|
|
}
|
|
constexpr uint64_t first = 0xf13001638c000001, second = 0xf13001638d000002;
|
|
struct Fixture {
|
|
NativeWorldTranslation w{};
|
|
Fixture() {
|
|
w.selected.guid = 3; w.self_created = true; w.realm = 70;
|
|
w.location = WorldLocation{1, {}};
|
|
for (auto id : {first, second}) {
|
|
auto& actor = w.entities.actors[id];
|
|
actor.kind = 3; actor.generation = 1; actor.sent = true;
|
|
actor.modern = forever_creature_guid(id, uint32_t(id >> 24) & 0xffffff, 70, 1);
|
|
}
|
|
}
|
|
Bytes run(uint32_t op, const Bytes& data) { return w.translate({op, data}, 0x46010046, 0, nullptr); }
|
|
static Bytes single(const Bytes& b, uint32_t opcode) {
|
|
Reader r(b);
|
|
check(r.le(4) == 1 && r.le(4) == opcode, "wrong native packet count/opcode");
|
|
auto result = r.take(r.le(4)); r.end(); return result;
|
|
}
|
|
void open(uint64_t by, uint32_t id) {
|
|
Bytes input; le(input, by, 8); le(input, id, 4); input.resize(20);
|
|
const auto result = run(0x17d, input);
|
|
if (!id) {
|
|
presented(result, by, 0, 0);
|
|
return;
|
|
}
|
|
check(result == Bytes(4) && w.requests.size()==1, "menu waits for its original NPC text");
|
|
const auto& request=w.requests.front();
|
|
Bytes expected; le(expected,id,4); le(expected,by,8);
|
|
check(request.opcode==0x17f && request.body==expected, "bound source greeting request");
|
|
w.requests.pop_front();
|
|
}
|
|
void presented(const Bytes& result, uint64_t by, uint32_t id, uint32_t greeting) {
|
|
const auto menu = single(result, 0x670018);
|
|
Reader r(menu);
|
|
check(unpack_guid(r, 16) == Bytes(w.entities.actors.at(by).modern.begin(),
|
|
w.entities.actors.at(by).modern.end()), "menu GUID");
|
|
check(r.le(4) == id && r.le(8) == 0 && r.le(8) == 0, "menu fields");
|
|
check(r.le(1) == (greeting ? 0x40 : 0), "BroadcastTextID presence");
|
|
if (greeting) check(r.le(4) == greeting, "selected BroadcastTextID");
|
|
r.end();
|
|
}
|
|
Bytes query(uint64_t by, uint32_t id) {
|
|
Bytes b; le(b, id, 4); append(b, pack_guid(w.entities.actors.at(by).modern)); return b;
|
|
}
|
|
void sent(uint64_t by, uint32_t id) {
|
|
auto in = query(by, id);
|
|
auto request = w.gameplay(npc_text::Query, in);
|
|
Bytes expected; le(expected, id, 4); le(expected, by, 8);
|
|
check(request.opcode == 0x17f && request.body == expected, "original request order/binding");
|
|
w.gameplay_sent(npc_text::Query, in);
|
|
}
|
|
};
|
|
}
|
|
|
|
int main() try {
|
|
Fixture f;
|
|
std::array<Option, 8> opts{};
|
|
opts[0] = {1, "CUSTOM_MERCHANT_TRIGGER", "CUSTOM_MERCHANT_TRIGGER"};
|
|
const auto vendor = original(910210, opts);
|
|
const std::array<uint32_t, 8> vendor_ids{910210};
|
|
check(f.run(0x180, vendor) == Bytes(4), "unsolicited reply");
|
|
f.open(first, 910210);
|
|
f.presented(f.run(0x180,vendor),first,910210,910210);
|
|
auto wrong = f.query(second, 910210);
|
|
rejected([&]{ f.w.gameplay(npc_text::Query, wrong); });
|
|
auto valid = f.query(first, 910210);
|
|
for (size_t n = 0; n < valid.size(); ++n)
|
|
rejected([&]{ f.w.gameplay(npc_text::Query, std::span(valid).first(n)); });
|
|
auto extra = valid; extra.push_back(0);
|
|
rejected([&]{ f.w.gameplay(npc_text::Query, extra); });
|
|
auto wrong_id = f.query(first, 910211);
|
|
rejected([&]{ f.w.gameplay(npc_text::Query, wrong_id); });
|
|
f.w.gameplay(npc_text::Query, valid); // Sizing alone cannot create a pending query.
|
|
check(f.run(0x180, vendor) == Bytes(4), "sizing created pending request");
|
|
f.sent(first, 910210);
|
|
check(f.w.npc_text_queries.awaiting(910210), "repeat is not coalesced");
|
|
check(f.single(f.run(0x180, vendor), npc_text::Response) == native(910210, opts, vendor_ids),
|
|
"ordinary custom merchant response");
|
|
check(f.run(0x180, vendor) == Bytes(4), "duplicate response");
|
|
|
|
// The old NPC and the new NPC can advertise the same text ID. Source has no GUID in the reply.
|
|
f.sent(first, 910210); f.open(second, 910210); f.sent(second, 910210);
|
|
check(f.run(0x180, vendor) == Bytes(4), "late old response crossed NPCs");
|
|
f.presented(f.run(0x180,vendor),second,910210,910210);
|
|
f.sent(second, 910210);
|
|
auto hello = pack_guid(f.w.entities.actors.at(first).modern);
|
|
check(f.w.gameplay(0x3f0029, hello).opcode == 0x17b, "ordinary new NPC hello");
|
|
f.w.gameplay_sent(0x3f0029, hello);
|
|
check(f.run(0x180, vendor) == Bytes(4), "old reply appeared while next menu was pending");
|
|
f.open(second, 910210);
|
|
f.sent(second, 910210); f.run(0x17e, {});
|
|
check(f.run(0x180, vendor) == Bytes(4), "reply after close");
|
|
f.open(second, 910210); f.sent(second, 910210); ++f.w.entities.actors[second].generation;
|
|
check(f.run(0x180, vendor) == Bytes(4), "despawned actor generation");
|
|
rejected([&]{ f.w.gameplay(npc_text::Query, f.query(second, 910210)); });
|
|
|
|
f.open(first, 820);
|
|
auto vendor_target=pack_guid(f.w.entities.actors.at(first).modern);
|
|
f.w.gameplay_sent(0x3f0038,vendor_target);
|
|
check(f.run(0x180,original(820))==Bytes(4),"service switch retired an unresolved greeting");
|
|
|
|
f.open(first, 820);
|
|
const std::string welcome = "Welcome to my Inn, weary traveler. What can I do for you?";
|
|
opts={}; opts[0]={100,welcome,welcome,7};
|
|
f.presented(f.run(0x180,original(820,opts)),first,820,2821);
|
|
check(f.run(0x180,original(820,opts))==Bytes(4),"late duplicate cannot reroll or reopen menu");
|
|
f.open(first, 0xffffff);
|
|
f.presented(f.run(0x180,original(0xffffff)),first,0xffffff,16777200);
|
|
f.open(first, 564);
|
|
auto truncated_menu=original(564); truncated_menu.pop_back();
|
|
f.presented(f.run(0x180,truncated_menu),first,564,0);
|
|
f.sent(first,564);
|
|
const std::string classic = "Greetings, $c. Are you here to learn of the hidden arts?";
|
|
opts = {}; opts[0] = {1, classic, classic, 1};
|
|
check(f.single(f.run(0x180, original(564, opts)), npc_text::Response) ==
|
|
native(564, opts, {2529}), "ordinary vanilla greeting");
|
|
opts[0].weight = 0.25f;
|
|
opts[5] = {0.75f, "What can you tell me about the bear - Ursangous?\n",
|
|
"What can you tell me about the bear - Ursangous?", 0, {0, 6}};
|
|
check(npc_text::native_reply(original(564, opts)) == native(564, opts, {2529, 0, 0, 0, 0, 8229}),
|
|
"weighted options, language, emote, multiline and gender");
|
|
opts[0].weight = 0; opts[5].weight = 0;
|
|
check(npc_text::native_reply(original(564, opts)) == native(564, opts, {2529, 0, 0, 0, 0, 8229}),
|
|
"zero-weight genuine options retained");
|
|
opts[5].emote[0] = 1;
|
|
check(npc_text::native_reply(original(564, opts)) == native(564, {}, {}),
|
|
"missing complete wire signature must not substitute a different emote");
|
|
std::array<Option, 8> fallback{};
|
|
fallback[0].weight = 1;
|
|
f.sent(first, 564);
|
|
check(f.single(f.run(0x180, original(564)), npc_text::Response) ==
|
|
native(564, fallback, {16777200}), "eight original placeholders expose one selectable greeting");
|
|
check(npc_text::native_reply(original(564), [](const npc_text::Signature&) { return 0u; }) ==
|
|
native(564, {}, {}), "missing fallback BroadcastText must remain denied");
|
|
check(npc_text::native_reply(original(0)) == native(0, {}, {}), "invalid zero text id stays denied");
|
|
|
|
opts = {};
|
|
opts[3] = {0, "", ""};
|
|
check(npc_text::native_reply(original(564, opts)) == native(564, {}, {}),
|
|
"seven placeholders and an empty option do not qualify");
|
|
opts.fill({0, "", ""});
|
|
check(npc_text::native_reply(original(564, opts)) == native(564, {}, {}), "empty response stays denied");
|
|
opts = {};
|
|
opts[3].weight = -0.0f;
|
|
check(npc_text::native_reply(original(564, opts)) == native(564, {}, {}),
|
|
"negative zero differs from the original fallback");
|
|
opts[3].weight = 0.25f;
|
|
check(npc_text::native_reply(original(564, opts)) == native(564, opts, {0,0,0,16777200}),
|
|
"authored weighted greeting keeps its position and probability");
|
|
opts[3].weight = 0;
|
|
opts[3].language = 7;
|
|
check(npc_text::native_reply(original(564, opts)) == native(564, {}, {}),
|
|
"a different language is not the fallback");
|
|
opts[3].language = 0;
|
|
opts[3].emote[3] = 7;
|
|
check(npc_text::native_reply(original(564, opts)) == native(564, {}, {}),
|
|
"an emote is not the fallback");
|
|
opts[3].emote[3] = 0;
|
|
opts[3].female.clear();
|
|
check(npc_text::native_reply(original(564, opts)) == native(564, {}, {}),
|
|
"one matching gender string is insufficient");
|
|
f.sent(first, 564);
|
|
auto truncated = original(564, opts); truncated.pop_back();
|
|
check(f.run(0x180, truncated) == Bytes(4), "malformed optional source reply");
|
|
f.sent(first, 564); auto trailing = original(564); trailing.push_back(0);
|
|
check(f.run(0x180, trailing) == Bytes(4), "trailing source bytes");
|
|
opts[0].weight = std::bit_cast<float>(0x7fc00000u);
|
|
rejected([&]{ npc_text::native_reply(original(564, opts)); });
|
|
f.open(first, 0);
|
|
rejected([&]{ f.w.gameplay(npc_text::Query, f.query(first, 0)); });
|
|
std::cout << "Native source-backed greeting requests, replies and menu lifetimes passed.\n";
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::cerr << e.what() << '\n'; return 1;
|
|
}
|