82 lines
5.8 KiB
Python
82 lines
5.8 KiB
Python
"""Run production target selection and outgoing native chat packet construction.
|
|
Decode the packets in the order used by the actual Cataclysm chat handler.
|
|
"""
|
|
from pathlib import Path
|
|
import subprocess,tempfile
|
|
root=Path(__file__).resolve().parents[2]
|
|
s=(root/'src/server/game/World/ClientlessTestBotMgr.cpp').read_text()
|
|
def fn(sig):
|
|
a=s.index(sig);b=s.index('{',a);e=b+1;n=1
|
|
while n:n+=(s[e]=='{')-(s[e]=='}');e+=1
|
|
return s[a:e]
|
|
code=r'''
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <map>
|
|
#include <stdexcept>
|
|
using uint16=uint16_t;using uint32=uint32_t;using uint64=uint64_t;
|
|
constexpr int CMSG_SET_SELECTION=1,CMSG_MESSAGECHAT_SAY=2,CMSG_MESSAGECHAT_YELL=3,CMSG_MESSAGECHAT_EMOTE=4,CMSG_MESSAGECHAT_PARTY=5,CMSG_MESSAGECHAT_RAID=6,CMSG_MESSAGECHAT_GUILD=7,CMSG_MESSAGECHAT_OFFICER=8,CMSG_MESSAGECHAT_WHISPER=9,CMSG_MESSAGECHAT_CHANNEL=10,CMSG_MESSAGECHAT_BATTLEGROUND=11,CMSG_MESSAGECHAT_RAID_WARNING=12;
|
|
constexpr int TEAM_ALLIANCE=0,LANG_COMMON=7,LANG_ORCISH=1;
|
|
struct ObjectGuid{uint64 value=0;ObjectGuid()=default;explicit ObjectGuid(uint64 x):value(x){}bool operator==(ObjectGuid const&x)const{return value==x.value;}};
|
|
struct WorldPacket{
|
|
int opcode;std::vector<unsigned char>b;unsigned bits=0;unsigned bitval=0;std::size_t read=0;unsigned rbit=8;unsigned rvalue=0;
|
|
WorldPacket(int op,unsigned):opcode(op){}
|
|
WorldPacket&operator<<(uint32 n){for(int i=0;i<4;++i)b.push_back((n>>(8*i))&255);return *this;}
|
|
WorldPacket&operator<<(ObjectGuid g){for(int i=0;i<8;++i)b.push_back((g.value>>(8*i))&255);return *this;}
|
|
void WriteBits(uint64 n,unsigned count){while(count){--count;bitval=(bitval<<1)|((n>>count)&1);if(++bits==8){b.push_back(bitval);bits=bitval=0;}}}
|
|
void FlushBits(){if(bits){b.push_back(bitval<<(8-bits));bits=bitval=0;}}
|
|
void WriteString(std::string const&s){b.insert(b.end(),s.begin(),s.end());}
|
|
void rpos(unsigned n){read=n;rbit=8;}
|
|
uint32 U32(){uint32 n=0;for(int i=0;i<4;++i)n|=uint32(b.at(read++))<<(8*i);return n;}
|
|
uint32 ReadBits(unsigned n){uint32 v=0;for(unsigned i=0;i<n;++i){if(rbit==8){rvalue=b.at(read++);rbit=0;}v=(v<<1)|((rvalue>>(7-rbit++))&1);}return v;}
|
|
std::string ReadString(unsigned n){std::string s(b.begin()+read,b.begin()+read+n);read+=n;return s;}
|
|
};
|
|
struct Map{}mapA,mapB;
|
|
struct Player;
|
|
struct Session{Player*owner=nullptr;unsigned selected=0,chats=0;std::string text,target;uint32 lang=0;
|
|
void HandleSetSelectionOpcode(WorldPacket&);void HandleMessagechatOpcode(WorldPacket&p){
|
|
++chats;target.clear();lang=p.opcode==CMSG_MESSAGECHAT_EMOTE?0:p.U32();
|
|
unsigned len=0,to=0;
|
|
if(p.opcode==CMSG_MESSAGECHAT_WHISPER){to=p.ReadBits(10);len=p.ReadBits(9);target=p.ReadString(to);text=p.ReadString(len);}
|
|
else if(p.opcode==CMSG_MESSAGECHAT_CHANNEL){len=p.ReadBits(9);to=p.ReadBits(10);text=p.ReadString(len);target=p.ReadString(to);}
|
|
else {len=p.ReadBits(9);text=p.ReadString(len);}
|
|
assert(p.read==p.b.size());
|
|
}
|
|
};
|
|
struct Player{Session session;ObjectGuid guid,selection;Map*map=&mapA;bool world=true;int team=0;
|
|
Player(uint64 g):guid(g){session.owner=this;}
|
|
ObjectGuid GetGUID(){return guid;}ObjectGuid GetTarget(){return selection;}bool IsInWorld(){return world;}Map*GetMap(){return map;}
|
|
Session*GetSession(){return &session;}int GetTeamId(){return team;}
|
|
};
|
|
void Session::HandleSetSelectionOpcode(WorldPacket&p){uint64 n=0;for(int i=0;i<8;++i)n|=uint64(p.b.at(i))<<(8*i);owner->selection=ObjectGuid(n);++selected;}
|
|
bool normalizePlayerName(std::string&n){return !n.empty();}
|
|
namespace ObjectAccessor{std::map<uint64,Player*>units;Player*FindConnectedPlayerByName(std::string const&n){return n=="Other"?units[149]:nullptr;}Player*GetUnit(Player&,ObjectGuid g){auto it=units.find(g.value);return it==units.end()?nullptr:it->second;}}
|
|
'''+fn('ObjectGuid SelectTarget(')+fn('void SendTestChat(')+r'''
|
|
int main(){Player actor(148),other(149);ObjectAccessor::units={{148,&actor},{149,&other}};
|
|
Player npc(0xF130000001000001ULL);ObjectAccessor::units[npc.guid.value]=&npc;
|
|
assert(SelectTarget(&actor,std::to_string(npc.guid.value)).value==npc.guid.value);assert(SelectTarget(&actor,"0xF130000001000001").value==npc.guid.value);actor.session.selected=0;
|
|
assert(SelectTarget(&actor,"Other").value==149&&actor.session.selected==1);
|
|
assert(SelectTarget(&actor,"self").value==148);assert(SelectTarget(&actor,"none").value==0);
|
|
assert(SelectTarget(&actor,"149").value==149);
|
|
other.map=&mapB;try{SelectTarget(&actor,"149");assert(false);}catch(std::runtime_error const&){}assert(actor.selection.value==149);other.map=&mapA;
|
|
try{SelectTarget(&actor,"999");assert(false);}catch(std::runtime_error const&){}
|
|
for(auto type:{"say","yell","emote","party","raid","guild","officer","battleground","raid_warning"}){
|
|
SendTestChat(&actor,type,"hello world","");assert(actor.session.text=="hello world"&&actor.session.target.empty());
|
|
}
|
|
SendTestChat(&actor,"whisper","/AI status?","Other");assert(actor.session.text=="/AI status?"&&actor.session.target=="Other");
|
|
SendTestChat(&actor,"channel","hello channel","General");assert(actor.session.text=="hello channel"&&actor.session.target=="General");
|
|
actor.team=1;SendTestChat(&actor,"say","Horde","");assert(actor.session.lang==LANG_ORCISH);
|
|
unsigned sent=actor.session.chats;
|
|
for(auto text:{std::string{},std::string(512,'x'),std::string("one\ntwo")})try{SendTestChat(&actor,"say",text,"");assert(false);}catch(std::runtime_error const&){}
|
|
try{SendTestChat(&actor,"whisper","hello","");assert(false);}catch(std::runtime_error const&){}
|
|
assert(actor.session.chats==sent);
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory()as d:
|
|
p=Path(d);(p/'test.cpp').write_text(code)
|
|
subprocess.run(['g++','-std=c++17','-fsanitize=address,undefined',str(p/'test.cpp'),'-o',str(p/'test')],check=True)
|
|
subprocess.run([str(p/'test')],check=True)
|
|
print('Native test selection and Cataclysm chat wire layout: target retention/clearing, map isolation, supported channels and validation passed.')
|