// CPythonChat / CWhisper and the "chat" module against AVA2 PythonChat.cpp / PythonChatModule.cpp. #include "python_runtime.h" #include "python_chat.h" #include "python_gameplay_modules.h" #include #include #include #include #include using namespace ava2; using Bytes=std::vector; void require(bool ok,const char* why) { if(!ok)throw std::runtime_error(why); } void u32(Bytes& b,uint32_t v) { for(unsigned i=0;i<4;++i)b.push_back(uint8_t(v>>(8*i))); } // One-row MIPX item_proto (literal LZO run), as tests/item_data_test.cpp builds it. void proto(const std::filesystem::path& p,uint32_t id,const std::string& name) { Bytes raw;u32(raw,id);raw.resize(172);std::copy(name.begin(),name.end(),raw.begin()+4);std::copy(name.begin(),name.end(),raw.begin()+29);raw[54]=1;raw[57]=1; Bytes compressed{uint8_t(17+raw.size())};compressed.insert(compressed.end(),raw.begin(),raw.end());compressed.insert(compressed.end(),{17,0,0}); Bytes file{'M','I','P','X'};u32(file,1);u32(file,172);u32(file,1);u32(file,20+compressed.size()); file.insert(file.end(),{'M','C','O','Z'});u32(file,0);u32(file,compressed.size());u32(file,raw.size());file.insert(file.end(),{'M','C','O','Z'}); file.insert(file.end(),compressed.begin(),compressed.end()); std::filesystem::create_directories(p.parent_path());std::ofstream out(p,std::ios::binary);out.write(reinterpret_cast(file.data()),file.size()); } std::vector showing(DWORD id) { std::vector out; for(const auto* line:CPythonChat::Instance().GetChatSet(id)->m_ShowingChatLineList) out.push_back(line->text); return out; } int main(int argc,char** argv) { if(argc!=3)return 2; auto root=std::filesystem::temp_directory_path()/("ava2-chat-"+std::to_string(getpid())); std::filesystem::create_directories(root); int rc=0; try { PythonRuntime python(argv[1],root); proto(root/"proto",0x13,"Sword"); require(LoadItemTable("proto"),"item proto fixture rejected"); auto& chat=CPythonChat::Instance(); chat.SetCurrentMillisecond(1000); python.execute( "import chat\n" "assert (chat.CHAT_TYPE_TALKING,chat.CHAT_TYPE_SHOUT,chat.CHAT_TYPE_DICE_INFO,chat.CHAT_TYPE_MISSION)==(0,6,10,14)\n" "assert (chat.WHISPER_TYPE_CHAT,chat.WHISPER_TYPE_GM,chat.WHISPER_TYPE_SYSTEM)==(0,5,255)\n" "assert (chat.BOARD_STATE_VIEW,chat.BOARD_STATE_EDIT,chat.BOARD_STATE_LOG)==(0,1,2)\n" "assert (chat.CHAT_SET_CHAT_WINDOW,chat.CHAT_SET_LOG_WINDOW)==(0,1)\n" "assert chat.GetLineCount(0)==0 and chat.GetLineCount(1)==0 and chat.GetLineCount(7)==0\n" "assert chat.GetLineStep(0)==15 and chat.GetLineStep(7)==0\n"); // __Initialize colours; SetChatColor packs 0xff RRGGBB. require(chat.GetChatColorValue(0)==0xffffffffu,"talking colour"); require(chat.GetChatColorValue(CPythonChat::CHAT_TYPE_WHISPER)==0xff4AE14Au,"whisper colour"); require(chat.GetChatColorValue(CPythonChat::CHAT_TYPE_DICE_INFO)==0xffcc00ccu,"dice colour"); require(chat.GetChatColorValue(CPythonChat::CHAT_TYPE_INFO)==DWORD(D3DXCOLOR(1.0f,0.785f,0.785f,1.0f)),"info colour"); require(chat.GetChatColorValue(99)==DWORD(D3DXCOLOR(0.0f,0.0f,1.0f,1.0f)),"out-of-range colour"); python.execute("chat.SetChatColor(chat.CHAT_TYPE_PARTY,1,2,3)\nchat.SetChatColor(15,1,2,3)\n"); require(chat.GetChatColorValue(CPythonChat::CHAT_TYPE_PARTY)==0xff010203u,"SetChatColor"); // Every line store receives the line; "#ebvs" lines are dropped. python.execute( "chat.AppendChat(chat.CHAT_TYPE_TALKING,'a : hello')\n" "chat.AppendChat(chat.CHAT_TYPE_INFO,'x #ebvs y')\n" "assert chat.GetLineCount(0)==1 and chat.GetLineCount(1)==1\n" "assert chat.GetVisibleLineCount(0)==1\n"); require(chat.GetChatLineDeque(0)->front()->text=="a : hello","line text"); require(chat.GetChatLineDeque(0)->front()->fAppendedTime==1.0f,"appended time is CTimer seconds"); // Item hyperlinks take this client's item name. python.execute("chat.AppendChat(chat.CHAT_TYPE_INFO,'|cfff1e6c0|Hitem:13:0:0:0:0|h[Old]|h|r and |Hitem:zz:0|h[q]|h')\n"); require(chat.GetChatLineDeque(0)->back()->text=="|cfff1e6c0|Hitem:13:0:0:0:0|h[Sword]|h|r and |Hitem:zz:0|h[q]|h","item link rename"); // SHOUT: "|lang|N|" / "|empire|N|" prefixes are stripped; a bad language becomes 1. python.execute( "chat.AppendChat(chat.CHAT_TYPE_SHOUT,'|lang|3||empire|2|Bob : hi')\n" "chat.AppendChat(chat.CHAT_TYPE_SHOUT,'|lang|99|Al : yo')\n" "chat.AppendChat(chat.CHAT_TYPE_SHOUT,'|lang|5')\n"); { const auto& lines=*chat.GetChatLineDeque(0); const auto* bob=lines[lines.size()-3];const auto* al=lines[lines.size()-2];const auto* bare=lines.back(); require(bob->text=="Bob : hi" && bob->countryIndex==3 && bob->empireIndex==2,"shout prefixes"); require(al->text=="Al : yo" && al->countryIndex==1 && al->empireIndex==0,"shout language clamp"); require(bare->text=="5" && bare->countryIndex==5,"shout without closing bar"); } python.execute( "assert chat.GetLineCount(0)==5,(chat.GetLineCount(0),chat.GetLineCount(1))\n" "chat.DisableCountryMode(0,3)\nassert chat.GetLineCount(0)==4 and chat.GetLineCount(1)==5,(chat.GetLineCount(0),chat.GetLineCount(1))\n" "chat.EnableCountryMode(0,3)\nchat.DisableCountryMode(0,99)\nassert chat.GetLineCount(0)==5\n" "chat.DisableChatMode(0,chat.CHAT_TYPE_SHOUT)\nassert chat.GetLineCount(0)==2\n" "chat.ToggleChatMode(0,chat.CHAT_TYPE_SHOUT)\nassert chat.GetLineCount(0)==5\n" "chat.ToggleChatMode(0,-1)\nchat.EnableChatMode(0,1000)\n"); // ArrangeShowingChat: (height+step)/step lines ending at SetEndPos. python.execute("chat.Clear()\nfor i in range(5):\n chat.AppendChat(chat.CHAT_TYPE_TALKING,'l%d'%i)\nchat.SetHeight(0,30)\nchat.ArrangeShowingChat(0)\n"); require(showing(0)==std::vector{"l2","l3","l4"},"end-position 1 window"); require(showing(1)==std::vector{"l4"},"zero-height window shows one line"); python.execute("chat.SetEndPos(0,0.0)\nassert chat.GetVisibleLineCount(0)==3\n"); require(showing(0)==std::vector{"l1","l2","l3"},"end-position 0 window"); python.execute("chat.SetEndPos(0,5.0)\nchat.SetStep(0,10)\nassert chat.GetLineStep(0)==10\nchat.ArrangeShowingChat(0)\nassert chat.GetVisibleLineCount(0)==3\n"); require(showing(0)==std::vector{"l2","l3","l4"},"step 10: iHeight starts at 12, so 12/22/32 fit under 40"); python.execute("chat.SetStep(0,0)\nchat.ArrangeShowingChat(0)\nassert chat.GetVisibleLineCount(0)==0\nchat.SetStep(0,15)\nchat.ArrangeShowingChat(0)\n"); // VIEW mode fades lines older than 5 s by a tenth per update; a new append re-arranges. chat.SetCurrentMillisecond(7000); python.execute("chat.SetPosition(0,10,500)\nchat.Update(0)\nchat.Render(0)\n"); require(std::abs(chat.GetChatLineDeque(0)->back()->GetColorRef(0).a-0.9f)<1e-4f,"view mode fade"); require(chat.GetChatLineDeque(0)->back()->GetColorRef(1).a==1.0f,"fade is per chat set"); python.execute("chat.SetBoardState(0,chat.BOARD_STATE_EDIT)\nchat.Update(0)\nchat.SetBoardState(0,chat.BOARD_STATE_LOG)\nchat.Update(0)\nchat.Close()\n"); require(chat.GetChatSet(0)->m_ShowingChatLineList.front()->fAppendedTime==0.0f,"Close zeroes appended time"); // 300-line cap per store. python.execute("for i in range(305):\n chat.AppendChat(chat.CHAT_TYPE_TALKING,'n%d'%i)\nassert chat.GetLineCount(0)==300\n"); require(chat.GetChatLineDeque(0)->front()->text=="n5","oldest lines dropped"); // AppendChatWithDelay waits for CTimer ms past the delay; Update on an existing set flushes // into every set (an unknown id returns before the flush). python.execute("chat.Clear()\nchat.AppendChatWithDelay(chat.CHAT_TYPE_NOTICE,'later',500)\nchat.Update(0)\nassert chat.GetLineCount(0)==0\n"); require(chat.GetWaitChatCount()==1,"delayed chat queued"); chat.SetCurrentMillisecond(7500);python.execute("chat.Update(0)\nassert chat.GetLineCount(0)==0\n"); chat.SetCurrentMillisecond(7501);python.execute("chat.Update(5)\nassert chat.GetLineCount(0)==0\nchat.Update(1)\nassert chat.GetLineCount(0)==1 and chat.GetLineCount(1)==1\n"); require(chat.GetWaitChatCount()==0,"delayed chat flushed"); // CreateChatSet copies set 0; ids are stored by BYTE, colours index by set id. python.execute( "assert chat.CreateChatSet(2)==2 and chat.GetLineCount(2)==1\n" "assert chat.CreateChatSet(256)==256 and chat.GetLineCount(256)==0\n" "chat.AppendChat(chat.CHAT_TYPE_TALKING,'more')\nassert chat.GetLineCount(2)==2\n" "chat.DeleteChatSet(2)\nassert chat.GetLineCount(2)==0 and chat.GetLineStep(2)==0\n"); // Ignore toggles; chat.IsIgnoreCharacter returns None as on Windows. python.execute("chat.IgnoreCharacter('troll')\nassert chat.IsIgnoreCharacter('troll') is None\n"); require(chat.IsIgnoreCharacter("troll"),"ignore set"); python.execute("chat.IgnoreCharacter('troll')\n"); require(!chat.IsIgnoreCharacter("troll"),"ignore toggled off"); // Whispers. python.execute( "chat.AppendWhisper(chat.WHISPER_TYPE_CHAT,'bob','hi')\n" "chat.AppendWhisper(chat.WHISPER_TYPE_GM,'bob','gm')\n" "chat.CreateWhisper('al')\nchat.CreateWhisper('al')\n"); require(chat.GetNewChatCount()==2 && chat.GetWhisperMap().size()==2,"whisper line count"); require(chat.GetWhisperMap().at("bob")->GetLines().size()==2,"whisper lines"); python.execute("chat.SetWhisperBoxSize('bob',200.0,90.0)\nchat.SetWhisperPosition('bob',0.5)\nchat.RenderWhisper('bob',1.0,2.0)\nchat.SetWhisperBoxSize('nobody',1.0,1.0)\n"); { const auto* bob=chat.GetWhisperMap().at("bob"); require(bob->GetWidth()==200.0f && bob->GetHeight()==90.0f && bob->GetCurPosition()==0.5f,"whisper box"); require(chat.GetNewChatCount()==0,"GetWhisper marks lines read"); } python.execute( "class Iface:\n def __init__(self):\n self.names=[]\n def MakeWhisperButton(self,n):\n self.names.append(n)\n" "f=Iface()\nchat.InitWhisper(f)\nassert f.names==['al','bob'],f.names\n" "chat.ClearWhisper('bob')\nchat.ClearWhisper('bob')\n"); require(chat.GetWhisperMap().size()==1,"ClearWhisper"); // GetLinkFromHyperlink. python.execute( "assert chat.GetLinkFromHyperlink('item:13:1:0:0:0')=='|cfff1e6c0|Hitem:13:1:0:0:0|h[Sword]|h|r'\n" "assert chat.GetLinkFromHyperlink('item:13:0:0:0:0:1:50:2:30')=='|cffffc700|Hitem:13:0:0:0:0:1:50:2:30|h[Sword]|h|r'\n" "assert chat.GetLinkFromHyperlink('item:13:0:0:0:0:1:50:2')=='|cffffc700|Hitem:13:0:0:0:0:1:50:2:0|h[Sword]|h|r'\n" "assert chat.GetLinkFromHyperlink('item:13:0:0:0:0:1')=='|cfff1e6c0|Hitem:13:0:0:0:0|h[Sword]|h|r'\n" "assert chat.GetLinkFromHyperlink('item:13:0:zz:0:0')=='|cfff1e6c0|Hitem:13:0:ffffffff:0:0|h[Sword]|h|r'\n" "assert chat.GetLinkFromHyperlink('item:14:0:0:0:0')==''\n" "assert chat.GetLinkFromHyperlink('item:13')==''\n" "assert chat.GetLinkFromHyperlink('nolink')==''\n" "assert chat.GetLinkFromHyperlink('pm:bob')==''\n"); // Bad arguments return None (Py_BuildException without a message). python.execute( "assert chat.AppendChat('x') is None and chat.GetLineCount() is None\n" "assert chat.CreateChatSet('a') is None and chat.SetEndPos(0,'z') is None\n"); python.execute("chat.Clear()\nassert chat.GetLineCount(0)==0 and chat.GetLineStep(1)==15\n"); require(chat.GetWhisperMap().empty(),"Clear destroys whispers"); chat.SetCurrentMillisecond(0); std::cout<<"AVA2 chat parity passed\n"; } catch(const std::exception& e) { std::cerr<