445 lines
20 KiB
C++
445 lines
20 KiB
C++
#include "entity_translation.hpp"
|
|
#include "gateway/object_updates.hpp"
|
|
#include "gateway/remote_speeds.hpp"
|
|
#include <bit>
|
|
#include <iostream>
|
|
#include <zlib.h>
|
|
using namespace gateway;
|
|
namespace {
|
|
unsigned checks = 0;
|
|
constexpr uint16_t realm = 70;
|
|
constexpr uint32_t map = 1;
|
|
constexpr uint64_t self = 42, peer = 5000,
|
|
npc = (0xf130ULL << 48) | (123ULL << 24) | 7;
|
|
void check(bool value, const char *message) {
|
|
++checks;
|
|
if (!value)
|
|
throw Error(message);
|
|
}
|
|
Bytes packed(uint64_t id) {
|
|
Bytes b;
|
|
le(b, id, 8);
|
|
return pack_guid(b);
|
|
}
|
|
LegacyPacket packet(uint64_t id, bool observer, uint32_t flags = 0) {
|
|
Bytes b = packed(id);
|
|
if (observer) {
|
|
le(b, flags, 4);
|
|
le(b, 200, 4);
|
|
for (float value : {11.f, 12.f, 13.f, .25f})
|
|
le(b, std::bit_cast<uint32_t>(value), 4);
|
|
le(b, 0, 4);
|
|
}
|
|
le(b, std::bit_cast<uint32_t>(3.5f), 4);
|
|
return {observer ? 0xcdu : 0x2feu, std::move(b)};
|
|
}
|
|
LegacyPacket compressed(const std::vector<LegacyPacket> &packets) {
|
|
Bytes records;
|
|
for (const auto &p : packets) {
|
|
check(p.body.size() + 2 <= 255, "compressed source fixture length");
|
|
le(records, p.body.size() + 2, 1);
|
|
le(records, p.opcode, 2);
|
|
append(records, p.body);
|
|
}
|
|
Bytes body;
|
|
le(body, records.size(), 4);
|
|
uLongf n = compressBound(records.size());
|
|
Bytes data(n);
|
|
check(compress2(data.data(), &n, records.data(), records.size(),
|
|
Z_BEST_SPEED) == Z_OK,
|
|
"compression fixture");
|
|
data.resize(n);
|
|
append(body, data);
|
|
return {0x2fb, std::move(body)};
|
|
}
|
|
LegacyPacket monster() {
|
|
Bytes b = packed(npc);
|
|
for (float f : {1.f, 2.f, 3.f})
|
|
le(b, std::bit_cast<uint32_t>(f), 4);
|
|
le(b, 17, 4);
|
|
le(b, 0, 1);
|
|
le(b, 0x100, 4);
|
|
le(b, 900, 4);
|
|
le(b, 1, 4);
|
|
for (float f : {9.f, 9.f, 0.f})
|
|
le(b, std::bit_cast<uint32_t>(f), 4);
|
|
return {0xdd, std::move(b)};
|
|
}
|
|
Character identity() {
|
|
Character c;
|
|
c.guid = self;
|
|
c.name = "Fixture";
|
|
c.race = 1;
|
|
c.cls = 1;
|
|
return c;
|
|
}
|
|
std::vector<LegacyPacket> run(NativeEntityTranslation &s, const LegacyPacket &p,
|
|
uint16_t r = realm, uint32_t m = map) {
|
|
return s.translate(p, identity(), r, 1, m,
|
|
[](uint32_t, const Bytes &) -> Bytes {
|
|
throw Error("speed route must not invoke create codec");
|
|
});
|
|
}
|
|
NativeEntityTranslation state() {
|
|
NativeEntityTranslation s;
|
|
s.position_realm = realm;
|
|
s.position_map = map;
|
|
for (auto id : {peer, npc}) {
|
|
auto &a = s.actors[id];
|
|
a.kind = id == peer ? 4 : 3;
|
|
a.eligible = a.sent = true;
|
|
a.generation = id;
|
|
a.presentation_realm = realm;
|
|
a.presentation_map = map;
|
|
a.modern = id == peer ? forever_player_guid(id, realm)
|
|
: forever_creature_guid(id, 123, realm, map);
|
|
}
|
|
auto &p = s.peer_players[peer];
|
|
p.generation = peer;
|
|
p.named = true;
|
|
return s;
|
|
}
|
|
void held(NativeEntityTranslation s, uint64_t id = peer) {
|
|
const auto before = s.unsupported;
|
|
check(run(s, packet(id, true)).empty(),
|
|
"unsupported identity emitted native speed");
|
|
check(s.unsupported == before + 1,
|
|
"held speed missing bounded unsupported accounting");
|
|
}
|
|
// Source moves and stops use transport-local coordinates. The state fixture
|
|
// models a presentation already established by the moving-object producer.
|
|
void transport_spline_cases() {
|
|
auto s = state();
|
|
constexpr uint64_t source_transport = (uint64_t(0x1fc0) << 48) | 31;
|
|
auto with_transport = [](LegacyPacket input, uint64_t transport) {
|
|
const auto id = packed(transport);
|
|
input.opcode = 0x2ae;
|
|
input.body.insert(input.body.begin() + packed(npc).size(), id.begin(), id.end());
|
|
return input;
|
|
};
|
|
auto source = with_transport(monster(), source_transport);
|
|
check(run(s, source).empty(), "missing moving transport presented");
|
|
Bytes native;
|
|
le(native, 0, 8);
|
|
le(native, (uint64_t(6) << 58) | (uint64_t(44) << 38), 8);
|
|
auto &actor = s.actors[source_transport];
|
|
actor.kind = 5;
|
|
actor.gameobject_type = 15;
|
|
actor.generation = 12;
|
|
actor.sent = actor.eligible = true;
|
|
actor.presentation_realm = realm;
|
|
actor.presentation_map = map;
|
|
std::copy(native.begin(), native.end(), actor.modern.begin());
|
|
const auto native_transport = pack_guid(native);
|
|
auto expected = [&](LegacyPacket same_path_on_land) {
|
|
auto land = run(s, same_path_on_land);
|
|
check(land.size() == 1, "land spline comparison fixture");
|
|
auto bytes = land.front().body;
|
|
const auto offset = pack_guid(s.actors[npc].modern).size() + 22;
|
|
check(bytes.at(offset) == 0 && bytes.at(offset + 1) == 0 &&
|
|
bytes.at(offset + 2) == 0xff, "land transport and seat defaults");
|
|
auto where = bytes.erase(bytes.begin() + offset, bytes.begin() + offset + 2);
|
|
bytes.insert(where, native_transport.begin(), native_transport.end());
|
|
return bytes;
|
|
};
|
|
auto expected_path = expected(monster());
|
|
auto path = run(s, source);
|
|
check(path.size() == 1 && path[0].opcode == 0x600002 &&
|
|
path[0].body == expected_path, "transport-local path and native binding");
|
|
auto from_envelope = run(s, compressed({source, packet(npc, false)}));
|
|
check(from_envelope.size() == 2 && from_envelope[0].body == expected_path &&
|
|
from_envelope[1].opcode == 0x600015, "ordered compressed transport path");
|
|
auto stop = monster();
|
|
stop.body.resize(packed(npc).size() + 17);
|
|
stop.body.back() = 1;
|
|
auto expected_stop = expected(stop);
|
|
check(s.source_positions.contains(npc), "land stop must provide world coordinates");
|
|
auto stopped = run(s, with_transport(stop, source_transport));
|
|
check(stopped.size() == 1 && stopped[0].body == expected_stop &&
|
|
!s.source_positions.contains(npc), "local stop leaked into world pose");
|
|
auto facing = monster();
|
|
const auto face = packed(npc).size() + 16;
|
|
facing.body[face] = 3;
|
|
Bytes peer_id;
|
|
le(peer_id, peer, 8);
|
|
facing.body.insert(facing.body.begin() + face + 1, peer_id.begin(), peer_id.end());
|
|
run(s, packet(peer, true));
|
|
check(run(s, with_transport(facing, source_transport)).empty(),
|
|
"world pose used as a transport-local facing target");
|
|
check(run(s, with_transport(monster(), npc)).empty(), "wrong source transport type");
|
|
for (unsigned i = 0; i < 7; ++i) {
|
|
auto invalid = s;
|
|
auto &a = invalid.actors[source_transport];
|
|
switch (i) {
|
|
case 0: a.sent = false; break;
|
|
case 1: a.eligible = false; break;
|
|
case 2: a.generation = 0; break;
|
|
case 3: ++a.presentation_map; break;
|
|
case 4: ++a.presentation_realm; break;
|
|
case 5: a.gameobject_type = 3; break;
|
|
case 6: a.modern = invalid.actors[npc].modern; break;
|
|
}
|
|
check(run(invalid, source).empty(), "invalid/stale transport presentation emitted");
|
|
}
|
|
}
|
|
} // namespace
|
|
int main() {
|
|
try {
|
|
transport_spline_cases();
|
|
auto s = state();
|
|
for (auto id : {peer, npc})
|
|
for (bool full : {true, false}) {
|
|
const auto input = packet(id, full);
|
|
auto expected = remote_speeds::forever_packet(
|
|
*remote_speeds::turtle_packet(input.opcode, input.body),
|
|
s.actors.at(id).modern, realm, map);
|
|
auto out = run(s, input);
|
|
check(out.size() == 1 && out[0].opcode == expected.opcode &&
|
|
out[0].body == expected.body,
|
|
"bound speed dispatch differs");
|
|
check(s.source_positions.at(id).xyz == std::array<float, 3>{11, 12, 13},
|
|
"source stationary observer position missing");
|
|
}
|
|
s = state();
|
|
check(run(s, packet(npc, false)).size() == 1 &&
|
|
!s.source_positions.contains(npc),
|
|
"spline speed invented source position");
|
|
run(s, packet(peer, true));
|
|
check(s.source_positions.contains(peer), "stationary pose fixture missing");
|
|
run(s, packet(peer, true, 1));
|
|
check(!s.source_positions.contains(peer),
|
|
"moving observer retained stale stationary position");
|
|
auto p = packet(peer, true);
|
|
p.body.pop_back();
|
|
check(run(s, p).empty(), "truncated source speed emitted");
|
|
check(run(s, packet(peer, true)).size() == 1,
|
|
"one unsupported speed killed later valid traffic");
|
|
held(state(), peer + 1);
|
|
held(state(), self);
|
|
s = state();
|
|
s.actors.at(peer).sent = false;
|
|
held(s);
|
|
s = state();
|
|
s.actors.at(peer).eligible = false;
|
|
held(s);
|
|
s = state();
|
|
s.actors.at(peer).generation = 0;
|
|
held(s);
|
|
s = state();
|
|
s.actors.at(peer).presentation_realm++;
|
|
held(s);
|
|
s = state();
|
|
s.actors.at(peer).presentation_map++;
|
|
held(s);
|
|
s = state();
|
|
s.actors.at(peer).modern = forever_player_guid(peer + 1, realm);
|
|
held(s);
|
|
s = state();
|
|
s.peer_players.erase(peer);
|
|
held(s);
|
|
s = state();
|
|
s.peer_players.at(peer).named = false;
|
|
held(s);
|
|
s = state();
|
|
s.peer_players.at(peer).blocked = true;
|
|
held(s);
|
|
s = state();
|
|
s.peer_players.at(peer).generation++;
|
|
held(s);
|
|
s = state();
|
|
s.actors.at(npc).kind = 5;
|
|
held(s, npc);
|
|
s = state();
|
|
s.actors.at(npc).modern = forever_creature_guid(npc, 123, realm, map + 1);
|
|
held(s, npc);
|
|
s = state();
|
|
run(s, packet(npc, true));
|
|
check(run(s, packet(npc, false), realm, map + 1).empty(),
|
|
"foreign map emitted stale actor speed");
|
|
check(s.source_positions.empty(), "map change retained source pose");
|
|
s = state();
|
|
run(s, packet(peer, true));
|
|
p = packet(peer, true);
|
|
p.body = Bytes{0xff};
|
|
check(run(s, p).empty() && s.source_positions.empty(),
|
|
"unparseable observer retained stale position");
|
|
s = state();
|
|
auto override = packet(npc, false);
|
|
auto restore = packet(npc, false);
|
|
override.body = packed(npc);
|
|
le(override.body, std::bit_cast<uint32_t>(24.f), 4);
|
|
auto expected_override = remote_speeds::forever_packet(
|
|
*remote_speeds::turtle_packet(override.opcode, override.body),
|
|
s.actors.at(npc).modern, realm, map);
|
|
auto expected_restore = remote_speeds::forever_packet(
|
|
*remote_speeds::turtle_packet(restore.opcode, restore.body),
|
|
s.actors.at(npc).modern, realm, map);
|
|
auto ordered = run(s, compressed({override, monster(), restore}));
|
|
check(ordered.size() == 3 && ordered[0].opcode == 0x600015 &&
|
|
ordered[1].opcode == 0x600002 && ordered[2].opcode == 0x600015,
|
|
"compressed source speed/spline/restore order lost");
|
|
check(ordered[0].body == expected_override.body &&
|
|
ordered[2].body == expected_restore.body,
|
|
"compressed speed values changed");
|
|
check(!s.source_positions.contains(npc),
|
|
"spline speed restore invented endpoint position");
|
|
auto nested = compressed({compressed({restore})});
|
|
check(run(s, nested).empty(), "nested compression unexpectedly admitted");
|
|
s=state();
|
|
const auto native_peer=pack_guid(s.actors.at(peer).modern);
|
|
for (auto [source,dest]:std::initializer_list<std::pair<uint32_t,uint32_t>>{
|
|
{0x304,0x60004c},{0x305,0x600051},{0x306,0x600052},{0x307,0x600053},{0x308,0x600054},
|
|
{0x309,0x600055},{0x30a,0x600056},{0x30b,0x600057},{0x30c,0x600058},{0x30d,0x600059},{0x30e,0x60005a},{0x31a,0x60004b}}) {
|
|
auto out=run(s,{source,packed(peer)});
|
|
check(out.size()==1 && out.front().opcode==dest && out.front().body==native_peer,"spline flag wire");
|
|
}
|
|
auto sequence=run(s,compressed({{0x30d,packed(npc)},monster(),{0x30e,packed(npc)}}));
|
|
check(sequence.size()==3 && sequence[0].opcode==0x600059 && sequence[1].opcode==0x600002 &&
|
|
sequence[2].opcode==0x60005a,"compressed walking bracket order");
|
|
auto observer_source=packet(peer,true);
|
|
observer_source.body.resize(observer_source.body.size()-4); // no speed in a mode snapshot
|
|
auto status=remote_speeds::forever_packet(*remote_speeds::turtle_packet(0xcd,packet(peer,true).body),
|
|
s.actors.at(peer).modern,realm,map).body;
|
|
status.resize(status.size()-4);
|
|
for(auto op:{0xecu,0xedu,0xf7u,0x2b0u,0x2b1u,0xc5u}){
|
|
auto out=run(s,{op,observer_source.body});
|
|
auto expected=status;
|
|
if(op==0xc5u)expected.resize(expected.size()+6);
|
|
check(out.size()==1 && out.front().opcode==(op==0xc5?0x60000fu:0x60000eu) &&
|
|
out.front().body==expected,"observer status/teleport wire");
|
|
}
|
|
// Direct observer teleport admission: all lifecycle requests contain the
|
|
// complete source movement body. Reuse the existing exact wire golden.
|
|
auto teleport=[](uint64_t id){auto p=packet(id,true);
|
|
p.opcode=0xc5;p.body.resize(p.body.size()-4);return p;};
|
|
auto teleport_exact=[&](NativeEntityTranslation& x){
|
|
auto expected=status;expected.resize(expected.size()+6);
|
|
auto out=run(x,teleport(peer));
|
|
check(out.size()==1 && out.front().opcode==0x60000f && out.front().body==expected,
|
|
"complete peer teleport exact existing wire golden");
|
|
check(x.source_positions.contains(peer),"valid teleport records peer pose");
|
|
};
|
|
for(int field=0;field<16;++field){
|
|
auto x=state();teleport_exact(x); // establish a pose before retirement
|
|
uint64_t id=peer;uint16_t target_realm=realm;uint32_t target_map=map;
|
|
auto& a=x.actors.at(peer);
|
|
if(field==0)a.sent=false;
|
|
if(field==1)a.eligible=false;
|
|
if(field==2)a.generation=0;
|
|
if(field==3)a.presentation_realm++;
|
|
if(field==4)a.presentation_map++;
|
|
if(field==5)a.modern=forever_player_guid(peer+1,realm);
|
|
if(field==6)x.peer_players.erase(peer);
|
|
if(field==7)x.peer_players.at(peer).named=false;
|
|
if(field==8)x.peer_players.at(peer).blocked=true;
|
|
if(field==9)x.peer_players.at(peer).generation++;
|
|
if(field==10)a.kind=5;
|
|
if(field==11)x.actors.erase(peer);
|
|
if(field==12)target_realm++;
|
|
if(field==13)target_map++;
|
|
if(field==14)id=self;
|
|
if(field==15)id=peer+1;
|
|
const auto before=x.unsupported;
|
|
check(run(x,teleport(id),target_realm,target_map).empty(),
|
|
"complete teleport outside current observer presentation admitted");
|
|
check(x.unsupported==before+1,"teleport rejection accounted once");
|
|
check(!x.source_positions.contains(id),"rejected teleport leaves no target pose");
|
|
auto ready=state();x.actors=ready.actors;x.peer_players=ready.peer_players;
|
|
teleport_exact(x); // rejection must not poison a valid current presentation
|
|
}
|
|
{auto x=state();x.actors.at(peer).generation++;
|
|
check(run(x,teleport(peer)).empty(),"teleport waits for recreated peer naming");
|
|
x.peer_players.at(peer).generation=x.actors.at(peer).generation;teleport_exact(x);}
|
|
// Malformed input is independently tested against a valid named peer.
|
|
{auto x=state();auto full=teleport(peer);
|
|
for(size_t n=0;n<full.body.size();++n){auto p=full;p.body.resize(n);
|
|
check(run(x,p).empty(),"truncated peer teleport rejected");}
|
|
auto tail=full;tail.body.push_back(0);
|
|
check(run(x,tail).empty(),"trailing peer teleport rejected");teleport_exact(x);}
|
|
auto knock=packet(peer,true,0x2000);
|
|
knock.body.resize(knock.body.size()-4);
|
|
for(float f:{-3.f,.6f,.8f,10.f, .6f,.8f,10.f,-3.f})
|
|
le(knock.body,std::bit_cast<uint32_t>(f),4);
|
|
auto knocked=run(s,{0xf1,knock.body});
|
|
check(knocked.size()==1 && knocked.front().opcode==0x600010,"observer knockback absent");
|
|
Reader jump(knocked.front().body);unpack_guid(jump,16);check(jump.le(8)==0x800,"jump flags");
|
|
jump.take(4+16+4+4+4+4+4);check(jump.le(1)==0x20 && jump.le(1)==0,"fall bit");
|
|
check(jump.le(4)==0 && jump.f32()==-3.f && jump.le(1)==0x80 && jump.f32()==.8f &&
|
|
jump.f32()==.6f && jump.f32()==10.f,"observer jump fields or order");jump.end();
|
|
knock.body.back()^=1;check(run(s,{0xf1,knock.body}).empty(),"unmatched source impulse admitted");
|
|
auto skip=packed(peer);le(skip,120,4);
|
|
auto skip_out=run(s,{0x319,skip});auto expected_skip=native_peer;le(expected_skip,120,4);
|
|
check(skip_out.size()==1 && skip_out.front().opcode==0x60004a && skip_out.front().body==expected_skip,
|
|
"observer skip time wire");
|
|
for(uint8_t on:{0,1}){
|
|
auto in=packed(peer);in.push_back(on);auto expected=native_peer;expected.push_back(on?0x80:0);
|
|
auto out=run(s,{0x159,in});check(out.size()==1 && out.front().opcode==0x4700f1 && out.front().body==expected,
|
|
"ordinary control toggle wire");
|
|
}
|
|
auto malformed=packed(peer);malformed.push_back(2);
|
|
check(run(s,{0x159,malformed}).empty(),"malformed allow control emitted");
|
|
check(run(s,{0x304,malformed}).empty(),"trailing spline flag data consumed");
|
|
check(run(s,{0x305,packed(peer+1)}).empty(),"unknown spline mover admitted");
|
|
// HandleMoveTimeSkippedOpcode broadcasts packed mover + uint32 lag,
|
|
// excluding the sending player. Keep admission cases fully framed so a
|
|
// parser rejection cannot masquerade as proof of observer eligibility.
|
|
auto skipped=[](uint64_t id,uint32_t elapsed=120){
|
|
auto body=packed(id);le(body,elapsed,4);return LegacyPacket{0x319,body};
|
|
};
|
|
auto skip_exact=[&](NativeEntityTranslation& x,uint32_t elapsed=120){
|
|
auto expected=pack_guid(x.actors.at(peer).modern);le(expected,elapsed,4);
|
|
auto out=run(x,skipped(peer,elapsed));
|
|
check(out.size()==1 && out.front().opcode==0x60004a && out.front().body==expected,
|
|
"complete peer time skip exact current binding and elapsed");
|
|
};
|
|
for(auto elapsed:{0u,120u,0xffffffffu}){auto x=state();skip_exact(x,elapsed);}
|
|
auto skip_held=[&](NativeEntityTranslation x,uint64_t id=peer,
|
|
uint16_t target_realm=realm,uint32_t target_map=map){
|
|
const auto before=x.unsupported;
|
|
check(run(x,skipped(id),target_realm,target_map).empty(),
|
|
"complete time skip outside current observer presentation admitted");
|
|
check(x.unsupported==before+1,"time skip rejection accounted once");
|
|
// Restore valid presentation after rejection without replacing the translator.
|
|
auto ready=state();x.actors=ready.actors;x.peer_players=ready.peer_players;
|
|
skip_exact(x);
|
|
};
|
|
skip_held(state(),self);skip_held(state(),peer+1);
|
|
for(int field=0;field<12;++field){
|
|
auto x=state();auto& a=x.actors.at(peer);
|
|
if(field==0)a.sent=false;
|
|
if(field==1)a.eligible=false;
|
|
if(field==2)a.generation=0;
|
|
if(field==3)a.presentation_realm++;
|
|
if(field==4)a.presentation_map++;
|
|
if(field==5)a.modern=forever_player_guid(peer+1,realm);
|
|
if(field==6)x.peer_players.erase(peer);
|
|
if(field==7)x.peer_players.at(peer).named=false;
|
|
if(field==8)x.peer_players.at(peer).blocked=true;
|
|
if(field==9)x.peer_players.at(peer).generation++;
|
|
if(field==10)a.kind=5;
|
|
if(field==11)x.actors.erase(peer);
|
|
skip_held(x);
|
|
}
|
|
skip_held(state(),peer,realm+1,map);
|
|
skip_held(state(),peer,realm,map+1);
|
|
// A recreated peer is held until the naming generation catches up.
|
|
{auto x=state();x.actors.at(peer).generation++;
|
|
check(run(x,skipped(peer)).empty(),"recreated peer with old naming generation held");
|
|
x.peer_players.at(peer).generation=x.actors.at(peer).generation;skip_exact(x);}
|
|
// Framing negatives use a valid peer, independently of the self policy.
|
|
{auto x=state();const auto full=skipped(peer);
|
|
for(size_t n=0;n<full.body.size();++n){auto p=full;p.body.resize(n);
|
|
check(run(x,p).empty(),"truncated peer time skip rejected");}
|
|
auto tail=full;tail.body.push_back(0);
|
|
check(run(x,tail).empty(),"trailing peer time skip rejected");skip_exact(x);}
|
|
|
|
// Real source-create/name/context initialization is covered by the existing
|
|
// native peer pipeline. These explicit state fixtures isolate admission.
|
|
std::cout << "Native remote speed binding: " << checks << " checks PASS\n";
|
|
return 0;
|
|
} catch (const std::exception &e) {
|
|
std::cerr << e.what() << '\n';
|
|
return 1;
|
|
}
|
|
}
|