From eec9c216481748616b9530ad02b88580ec01ff3f Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sat, 18 Feb 2023 00:03:30 +0100 Subject: [PATCH] Implement SVC_ServerTick properly * Message handler is now part of the CNetMessage base class. * SVC_ServerTick has been implemented, and is ready for use. --- r5dev/common/netmessages.h | 262 ++++++++++++++++++++++--------------- r5dev/public/inetmessage.h | 10 +- 2 files changed, 164 insertions(+), 108 deletions(-) diff --git a/r5dev/common/netmessages.h b/r5dev/common/netmessages.h index cebac38c..1fdd703e 100644 --- a/r5dev/common/netmessages.h +++ b/r5dev/common/netmessages.h @@ -20,109 +20,8 @@ // Forward declarations //------------------------------------------------------------------------------------- class CNetChan; - -class CNetMessage : public INetMessage -{ -public: - int m_nGroup; - bool m_bReliable; - CNetChan* m_NetChannel; -}; - -/////////////////////////////////////////////////////////////////////////////////////// -// server messages: -/////////////////////////////////////////////////////////////////////////////////////// -class SVC_Print : public CNetMessage -{ -public: - virtual ~SVC_Print() {}; - - virtual void SetNetChannel(INetChannel* netchan) = 0; - virtual void SetReliable(bool state) = 0; - - virtual bool Process(void) = 0; bool ProcessImpl(void); - - virtual bool ReadFromBuffer(bf_read& buffer) = 0; - virtual bool WriteToBuffer(bf_write& buffer) = 0; - - virtual bool IsReliable(void) const = 0; - - virtual int GetGroup(void) const = 0; - virtual int GetType(void) const = 0; - virtual const char* GetName(void) const = 0; - virtual INetChannel* GetNetChannel(void) const = 0; - virtual const char* ToString(void) const = 0; - virtual size_t GetSize(void) const = 0; - - IServerMessageHandler* m_pMessageHandler; - const void* m_pData; - const char* m_szText; -private: - char m_szTextBuffer[2048]; -}; - -class SVC_UserMessage : public CNetMessage -{ -public: - virtual ~SVC_UserMessage() {}; - - virtual void SetNetChannel(INetChannel* netchan) = 0; - virtual void SetReliable(bool state) = 0; - - virtual bool Process(void) = 0; bool ProcessImpl(void); - - virtual bool ReadFromBuffer(bf_read& buffer) = 0; - virtual bool WriteToBuffer(bf_write& buffer) = 0; - - virtual bool IsReliable(void) const = 0; - - virtual int GetGroup(void) const = 0; - virtual int GetType(void) const = 0; - virtual const char* GetName(void) const = 0; - virtual INetChannel* GetNetChannel(void) const = 0; - virtual const char* ToString(void) const = 0; - virtual size_t GetSize(void) const = 0; - - IServerMessageHandler* m_pMessageHandler; - int m_nMsgType; - int m_nLength; // data length in bits - bf_read m_DataIn; - bf_write m_DataOut; -}; - -class SVC_ServerTick : public CNetMessage -{ -public: - virtual ~SVC_ServerTick() {}; - - virtual void SetNetChannel(INetChannel* netchan) = 0; - virtual void SetReliable(bool state) = 0; - - virtual bool Process(void) = 0; - - virtual bool ReadFromBuffer(bf_read& buffer) = 0; - virtual bool WriteToBuffer(bf_write& buffer) = 0; - - virtual bool IsReliable(void) const = 0; - - virtual int GetGroup(void) const = 0; - virtual int GetType(void) const = 0; - virtual const char* GetName(void) const = 0; - virtual INetChannel* GetNetChannel(void) const = 0; - virtual const char* ToString(void) const = 0; - virtual size_t GetSize(void) const = 0; - - IServerMessageHandler* m_pMessageHandler; - nettick_t m_NetTick; -}; - -struct NET_StringCmd : CNetMessage -{ - INetMessageHandler* m_pMessageHandler; - const char* cmd; - char buffer[1024]; -}; - +class SVC_Print; +class SVC_UserMessage; //------------------------------------------------------------------------- // MM_HEARTBEAT @@ -141,6 +40,161 @@ inline void* g_pSVC_Print_VFTable = nullptr; inline auto SVC_UserMessage_Process = CMemory().RCast(); inline void* g_pSVC_UserMessage_VFTable = nullptr; +//------------------------------------------------------------------------- +// SVC_ServerTick +//------------------------------------------------------------------------- +inline void* g_pSVC_ServerTick_VFTable = nullptr; + +//------------------------------------------------------------------------- +// Enumeration of the INetMessage class +//------------------------------------------------------------------------- +enum NetMessageVtbl +{ + Destructor = 0, + SetNetChannel, + SetReliable, + Process, + ReadFromBuffer, + WriteToBuffer, + IsReliable, + GetGroup, + GetType, + GetName, + GetNetChannel, + ToString, + GetSize +}; + +/////////////////////////////////////////////////////////////////////////////////////// +class CNetMessage : public INetMessage +{ +public: + int m_nGroup; + bool m_bReliable; + CNetChan* m_NetChannel; + INetMessageHandler* m_pMessageHandler; +}; + +/////////////////////////////////////////////////////////////////////////////////////// +// server messages: +/////////////////////////////////////////////////////////////////////////////////////// +class SVC_Print : public CNetMessage +{ +public: + virtual ~SVC_Print() {}; + + virtual void SetNetChannel(CNetChan* netchan) = 0; + virtual void SetReliable(bool state) = 0; + + virtual bool Process(void) = 0; bool ProcessImpl(void); + + virtual bool ReadFromBuffer(bf_read* buffer) = 0; + virtual bool WriteToBuffer(bf_write* buffer) = 0; + + virtual bool IsReliable(void) const = 0; + + virtual int GetGroup(void) const = 0; + virtual int GetType(void) const = 0; + virtual const char* GetName(void) const = 0; + virtual CNetChan* GetNetChannel(void) const = 0; + virtual const char* ToString(void) const = 0; + virtual size_t GetSize(void) const = 0; + + const void* m_pData; + const char* m_szText; +private: + char m_szTextBuffer[2048]; +}; + +class SVC_UserMessage : public CNetMessage +{ +public: + virtual ~SVC_UserMessage() {}; + + virtual void SetNetChannel(CNetChan* netchan) = 0; + virtual void SetReliable(bool state) = 0; + + virtual bool Process(void) = 0; bool ProcessImpl(void); + + virtual bool ReadFromBuffer(bf_read* buffer) = 0; + virtual bool WriteToBuffer(bf_write* buffer) = 0; + + virtual bool IsReliable(void) const = 0; + + virtual int GetGroup(void) const = 0; + virtual int GetType(void) const = 0; + virtual const char* GetName(void) const = 0; + virtual CNetChan* GetNetChannel(void) const = 0; + virtual const char* ToString(void) const = 0; + virtual size_t GetSize(void) const = 0; + + int m_nMsgType; + int m_nLength; // data length in bits + bf_read m_DataIn; + bf_write m_DataOut; +}; + +class SVC_ServerTick : public CNetMessage +{ +public: + SVC_ServerTick() = default; + SVC_ServerTick(int tick, float hostFrametime, float hostFrametime_stddeviation, uint8_t cpuPercent) + { + void** pVFTable = reinterpret_cast(this); + *pVFTable = g_pSVC_ServerTick_VFTable; + + m_bReliable = false; + m_NetChannel = nullptr; + m_NetTick.m_nServerTick = tick; + m_NetTick.m_nClientTick = tick; + + m_NetTick.m_flHostFrameTime = hostFrametime; + m_NetTick.m_flHostFrameTimeStdDeviation = hostFrametime_stddeviation; + m_NetTick.m_nServerCPU = cpuPercent; + }; + + virtual ~SVC_ServerTick() {}; + + virtual void SetNetChannel(CNetChan* netchan) { m_NetChannel = netchan; } + virtual void SetReliable(bool state) { m_bReliable = state; }; + + virtual bool Process(void) + { + return CallVFunc(NetMessageVtbl::Process, this); + }; + virtual bool ReadFromBuffer(bf_read* buffer) + { + return CallVFunc(NetMessageVtbl::ReadFromBuffer, this, buffer); + } + virtual bool WriteToBuffer(bf_write* buffer) + { + return CallVFunc(NetMessageVtbl::WriteToBuffer, this, buffer); + } + + virtual bool IsReliable(void) const { return m_bReliable; }; + + virtual int GetGroup(void) const { return m_nGroup; }; + virtual int GetType(void) const { return 22; }; + virtual const char* GetName(void) const { return "svc_ServerTick"; }; + virtual CNetChan* GetNetChannel(void) const { return m_NetChannel; }; + virtual const char* ToString(void) const + { + static char szBuf[4096]; + V_snprintf(szBuf, sizeof(szBuf), "%s: server tick %i", this->GetName(), m_NetTick.m_nServerTick); + + return szBuf; + }; + virtual size_t GetSize(void) const { return sizeof(SVC_ServerTick); }; + + nettick_t m_NetTick; +}; + +struct NET_StringCmd : CNetMessage +{ + const char* cmd; + char buffer[1024]; +}; + /////////////////////////////////////////////////////////////////////////////// class V_NetMessages : public IDetour { @@ -148,6 +202,7 @@ class V_NetMessages : public IDetour { LogConAdr("SVC_Print::`vftable'", reinterpret_cast(g_pSVC_Print_VFTable)); LogConAdr("SVC_UserMessage::`vftable'", reinterpret_cast(g_pSVC_UserMessage_VFTable)); + LogConAdr("SVC_ServerTick::`vftable'", reinterpret_cast(g_pSVC_ServerTick_VFTable)); LogFunAdr("MM_Heartbeat::ToString", MM_Heartbeat__ToString.GetPtr()); } virtual void GetFun(void) const @@ -161,6 +216,7 @@ class V_NetMessages : public IDetour // We get the actual address of the vftable here, not the class instance. g_pSVC_Print_VFTable = g_GameDll.GetVirtualMethodTable(".?AVSVC_Print@@"); g_pSVC_UserMessage_VFTable = g_GameDll.GetVirtualMethodTable(".?AVSVC_UserMessage@@"); + g_pSVC_ServerTick_VFTable = g_GameDll.GetVirtualMethodTable(".?AVSVC_ServerTick@@"); } virtual void Attach(void) const; virtual void Detach(void) const; diff --git a/r5dev/public/inetmessage.h b/r5dev/public/inetmessage.h index 62bdf687..f6ddecfb 100644 --- a/r5dev/public/inetmessage.h +++ b/r5dev/public/inetmessage.h @@ -12,7 +12,7 @@ class INetMsgHandler; class INetMessage; -class INetChannel; +class CNetChan; // typedef bool (INetMsgHandler::*PROCESSFUNCPTR)(INetMessage*); // #define CASTPROCPTR( fn ) static_cast (fn) @@ -25,20 +25,20 @@ public: // Use these to setup who can hear whose voice. // Pass in client indices (which are their ent indices - 1). - virtual void SetNetChannel(INetChannel * netchan) = 0; // netchannel this message is from/for + virtual void SetNetChannel(CNetChan* netchan) = 0; // netchannel this message is from/for virtual void SetReliable( bool state ) = 0; // set to true if it's a reliable message virtual bool Process( void ) = 0; // calls the recently set handler to process this message - virtual bool ReadFromBuffer( bf_read &buffer ) = 0; // returns true if parsing was OK - virtual bool WriteToBuffer( bf_write &buffer ) = 0; // returns true if writing was OK + virtual bool ReadFromBuffer( bf_read *buffer ) = 0; // returns true if parsing was OK + virtual bool WriteToBuffer( bf_write *buffer ) = 0; // returns true if writing was OK virtual bool IsReliable( void ) const = 0; // true, if message needs reliable handling virtual int GetGroup( void ) const = 0; // returns net message group of this message virtual int GetType( void ) const = 0; // returns module specific header tag eg svc_serverinfo virtual const char *GetName( void ) const = 0; // returns network message name, eg "svc_serverinfo" - virtual INetChannel *GetNetChannel( void ) const = 0; + virtual CNetChan *GetNetChannel( void ) const = 0; virtual const char *ToString( void ) const = 0; // returns a human readable string about message content virtual size_t GetSize( void ) const = 0; // returns net message size of this message };