From 170d7ba53ad883db95fe7736cdc154fef7b8cb14 Mon Sep 17 00:00:00 2001 From: galaxyhaxz Date: Wed, 5 Sep 2018 14:06:58 -0500 Subject: [PATCH] Fix timed messages in multiplayer (#196) --- Source/multi.cpp | 2 +- Source/tmsg.cpp | 48 +++++++++++++++++++++++++----------------------- Source/tmsg.h | 4 ++-- Support/TODO.md | 1 - structs.h | 16 ++++++++++------ 5 files changed, 38 insertions(+), 33 deletions(-) diff --git a/Source/multi.cpp b/Source/multi.cpp index 94fb25243..81debc69b 100644 --- a/Source/multi.cpp +++ b/Source/multi.cpp @@ -652,7 +652,7 @@ void __cdecl multi_process_tmsgs() while ( 1 ) { - v0 = tmsg_get(&pkt.hdr.px, 512); + v0 = tmsg_get((unsigned char *)&pkt, 512); if ( !v0 ) break; multi_handle_all_packets(myplr, &pkt, v0); diff --git a/Source/tmsg.cpp b/Source/tmsg.cpp index bcacbf630..f15e0cbd7 100644 --- a/Source/tmsg.cpp +++ b/Source/tmsg.cpp @@ -4,72 +4,74 @@ TMsg *sgpTimedMsgHead; -int __fastcall tmsg_get(unsigned char *pbMsg, char bLen) +int __fastcall tmsg_get(unsigned char *pbMsg, unsigned int dwMaxLen) { unsigned char *v2; // ebx DWORD v3; // eax TMsg *v4; // esi - size_t dwMaxLen; // edi + size_t v6; // edi v2 = pbMsg; if ( !sgpTimedMsgHead ) return 0; v3 = GetTickCount(); v4 = sgpTimedMsgHead; - if ( (signed int)(*(_DWORD *)&sgpTimedMsgHead[1] - v3) >= 0 ) + if ( (signed int)(sgpTimedMsgHead->hdr.dwTime - v3) >= 0 ) return 0; - //sgpTimedMsgHead = (TMsg *)*sgpTimedMsgHead; /* fix */ - dwMaxLen = (unsigned char)v4[2].hdr.next; - memcpy(v2, (char *)&v4[2] + 1, dwMaxLen); + sgpTimedMsgHead = sgpTimedMsgHead->hdr.pNext; + v6 = v4->hdr.bLen; + memcpy(v2, v4->body, v6); mem_free_dbg(v4); - return dwMaxLen; + return v6; } -void __fastcall tmsg_add(unsigned char *pbMsg, char bLen) +void __fastcall tmsg_add(unsigned char *pbMsg, unsigned char bLen) { - char v2; // bl + unsigned char v2; // bl unsigned char *v3; // ebp size_t v4; // edi TMsg *v5; // eax TMsg *v6; // esi + DWORD v7; // eax TMsg *v8; // ecx TMsg **v9; // eax v2 = bLen; v3 = pbMsg; - v4 = (unsigned char)bLen; - v5 = (TMsg *)DiabloAllocPtr((unsigned char)bLen + 12); + v4 = bLen; + v5 = (TMsg *)DiabloAllocPtr(bLen + 12); v6 = v5; - // *v5 = 0; /* fix */ - v6[2].hdr.next = v2; - v6[1].hdr.next = GetTickCount() + 500; - memcpy((char *)&v6[2] + 1, v3, v4); + v5->hdr.pNext = 0; + v7 = GetTickCount(); + v6->hdr.bLen = v2; + v6->hdr.dwTime = v7 + 500; + memcpy(v6->body, v3, v4); v8 = sgpTimedMsgHead; v9 = &sgpTimedMsgHead; - /*while ( v8 ) + while ( v8 ) { - v9 = (TMsg **)v8; - v8 = (TMsg *)*v8; - } fix */ + v9 = &v8->hdr.pNext; + v8 = v8->hdr.pNext; + } *v9 = v6; } void __cdecl tmsg_cleanup() { TMsg *v0; // eax - //TMsg *v1; // esi + TMsg *v1; // esi v0 = sgpTimedMsgHead; if ( sgpTimedMsgHead ) { - /* do + do { - v1 = (TMsg *)*v0; + v1 = v0->hdr.pNext; sgpTimedMsgHead = 0; mem_free_dbg(v0); v0 = v1; sgpTimedMsgHead = v1; } - while ( v1 ); fix */ + while ( v1 ); } } diff --git a/Source/tmsg.h b/Source/tmsg.h index 74a9365f8..0396702c2 100644 --- a/Source/tmsg.h +++ b/Source/tmsg.h @@ -4,8 +4,8 @@ extern TMsg *sgpTimedMsgHead; -int __fastcall tmsg_get(unsigned char *pbMsg, char bLen); -void __fastcall tmsg_add(unsigned char *pbMsg, char bLen); +int __fastcall tmsg_get(unsigned char *pbMsg, unsigned int dwMaxLen); +void __fastcall tmsg_add(unsigned char *pbMsg, unsigned char bLen); void __cdecl tmsg_cleanup(); #endif /* __TMSG_H__ */ diff --git a/Support/TODO.md b/Support/TODO.md index d0657ec25..310a3f7b6 100644 --- a/Support/TODO.md +++ b/Support/TODO.md @@ -9,7 +9,6 @@ Serious bugs (crash/fault) Minor bugs (noticeable but can be avoided) - Lighting of objects/items in dungeon is slightly lighter than it should be -- Timed messages are broken and have been disabled `tmsg.cpp` - Server commands are broken and have been disabled `msgcmd.cpp` Code issues (incorrect code that still works) diff --git a/structs.h b/structs.h index ec34e0a64..21a070040 100644 --- a/structs.h +++ b/structs.h @@ -1499,18 +1499,22 @@ struct SHA1Context // tmsg ////////////////////////////////////////////////// -struct TMsgHeader +#pragma pack(push, 1) +struct TMsg; + +struct TMsgHdr { - char next; - char start_tc; - char len; + TMsg *pNext; + unsigned int dwTime; + unsigned char bLen; }; struct TMsg { - TMsgHeader hdr; - char data; + TMsgHdr hdr; + unsigned char body[3]; }; +#pragma pack(pop) ////////////////////////////////////////////////// // mpqapi