68 lines
4.8 KiB
Python
68 lines
4.8 KiB
Python
"""Compile the production chat monitor: provenance, /AI routing, JSON and rotation."""
|
|
from pathlib import Path
|
|
import subprocess,tempfile,json
|
|
root=Path(__file__).resolve().parents[2]
|
|
with tempfile.TemporaryDirectory()as d:
|
|
p=Path(d)
|
|
stubs={
|
|
'Common.h':'#pragma once\n#include <cstdint>\nusing uint32=uint32_t;using uint64=uint64_t;\n#define TC_GAME_API\n',
|
|
'ObjectGuid.h':'''#pragma once
|
|
#include "Common.h"
|
|
struct ObjectGuid{uint64 value=0;ObjectGuid()=default;ObjectGuid(uint64 n):value(n){}bool IsPlayer()const{return value>0&&value<10000;}uint64 GetRawValue()const{return value;}unsigned GetCounter()const{return unsigned(value);}bool operator==(ObjectGuid const&v)const{return value==v.value;}};
|
|
''',
|
|
'WorldSession.h':'#pragma once\nstruct WorldSession{unsigned GetAccountId(){return 17;}bool IsClientless(){return false;}};\n',
|
|
'Player.h':'''#pragma once
|
|
#include "ObjectGuid.h"
|
|
#include "WorldSession.h"
|
|
#include <string>
|
|
struct Player{WorldSession session;WorldSession*GetSession(){return &session;}std::string GetName(){return "Tester";}ObjectGuid GetGUID(){return ObjectGuid(123);}ObjectGuid GetTarget(){return ObjectGuid(456);}};
|
|
''',
|
|
'ObjectAccessor.h':'#pragma once\n#include "Player.h"\nnamespace ObjectAccessor{inline Player p;inline Player*FindConnectedPlayer(ObjectGuid g){return g.value==123?&p:nullptr;}}\n',
|
|
'Config.h':'#pragma once\n#include <string>\nstruct Config{bool enabled=true;std::string path;bool GetBoolDefault(char const*,bool){return enabled;}std::string GetStringDefault(char const*,char const*){return path;}};inline Config cfg;inline auto sConfigMgr=&cfg;\n',
|
|
'SharedDefines.h':'#pragma once\nconstexpr unsigned LANG_ADDON=0xFFFFFFFFu,CHAT_MSG_WHISPER_INFORM=9;\n',
|
|
'Log.h':'#pragma once\n#define TC_LOG_ERROR(...) ((void)0)\n'}
|
|
for name,body in stubs.items():(p/name).write_text(body)
|
|
code=r'''
|
|
#include "ClientlessChatMonitor.h"
|
|
#include "Config.h"
|
|
#include "Player.h"
|
|
#include "SharedDefines.h"
|
|
#include <cassert>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <thread>
|
|
using namespace ClientlessChatMonitor;
|
|
int main(int,char**argv){cfg.path=argv[1];
|
|
assert(DirectedToAI("/AI check")&&DirectedToAI("/ai\tcheck")&&DirectedToAI("/AI"));
|
|
assert(!DirectedToAI("/AIM check")&&!DirectedToAI("hello /AI")&&!DirectedToAI(" /AI"));
|
|
Record(1,7,ObjectGuid(123),ObjectGuid{},"Grüße \"test\"\\\n","","","");
|
|
Record(7,7,ObjectGuid(123),ObjectGuid(123),"/AI check","","","");
|
|
Record(7,7,ObjectGuid(123),ObjectGuid(123),"/AI check","","",""); // duplicate packet builder
|
|
Record(CHAT_MSG_WHISPER_INFORM,7,ObjectGuid(123),ObjectGuid(123),"/AI check","","","");
|
|
Record(12,0,ObjectGuid(100001),ObjectGuid{},"/AI NPC text","NPC","","");
|
|
Record(1,LANG_ADDON,ObjectGuid(123),ObjectGuid{},"/AI addon","","","");
|
|
auto all=Read();assert(all.size()==4 && all[0].SenderName=="Tester" && all[0].Account==17 && !all[0].Clientless);
|
|
auto ai=Read(0,true);assert(ai.size()==1 && ai[0].ReceiverName=="Tester" && ai[0].DirectedToAI);
|
|
assert(Read(ai[0].Sequence).size()==2);
|
|
cfg.enabled=false;Record(1,7,ObjectGuid(123),ObjectGuid{},"ignored","","","");assert(Read().size()==4);cfg.enabled=true;
|
|
std::filesystem::copy_file(std::filesystem::path(cfg.path)/"monitor/chat.jsonl",std::filesystem::path(cfg.path)/"monitor/chat-before.jsonl");
|
|
Player actor;Command(&actor,".debug combat",true,{"Combat refs: 0"});
|
|
// Bounded in-memory ring; persistence remains available after old entries expire.
|
|
for(unsigned i=0;i<520;++i)Record(1,7,ObjectGuid(123),ObjectGuid{},"bounded-"+std::to_string(i),"","","");
|
|
assert(Read().size()==512);
|
|
// Exercise real file rotation with a sparse oversized file.
|
|
auto path=std::filesystem::path(cfg.path)/"monitor/chat.jsonl";
|
|
{std::ofstream out(path,std::ios::binary|std::ios::trunc);out.seekp(16*1024*1024);out.put('x');}
|
|
Record(1,7,ObjectGuid(123),ObjectGuid{},"rotated","","","");assert(std::filesystem::exists(path.string()+".1"));
|
|
}
|
|
'''
|
|
(p/'test.cpp').write_text(code)
|
|
subprocess.run(['g++','-std=c++17','-fsanitize=address,undefined','-pthread','-I'+str(p),'-I'+str(root/'src/server/game/World'),str(root/'src/server/game/World/ClientlessChatMonitor.cpp'),str(p/'test.cpp'),'-o',str(p/'test')],check=True)
|
|
subprocess.run([str(p/'test'),str(p/'data')],check=True)
|
|
monitor=p/'data/monitor'
|
|
before=[json.loads(x)for x in (monitor/'chat-before.jsonl').read_text().splitlines()];assert before[0]['text']=='Grüße "test"\\\n'
|
|
ai=[json.loads(x)for x in (monitor/'ai.jsonl').read_text().splitlines()];assert len(ai)==1 and ai[0]['ai'] and ai[0]['account']==17
|
|
commands=[json.loads(x)for x in (monitor/'commands.jsonl').read_text().splitlines()];assert commands[0]['command']=='.debug combat' and commands[0]['selection']=='456'
|
|
last=json.loads((monitor/'chat.jsonl').read_text());assert last['text']=='rotated'
|
|
print('Production chat monitor: /AI provenance, echo filtering, JSON, bounded cursor ring and file rotation passed.')
|