Difference between revisions of "UserCmd strings"

From Valve Developer Community
Jump to: navigation, search
m (cleanup)
(fixed some issues with includes and the shared nature of usercmd.cpp)
Line 6: Line 6:
  
 
===UserCmd.cpp===
 
===UserCmd.cpp===
 +
#include baseplayer_shared.h before memdbgon.h
 +
 
Note how WriteUsercmd only writes data if there is a delta (change). Notice how the count is tested, the data is then sent, and all of the saved data is then purged. This means only delta data will be sent.
 
Note how WriteUsercmd only writes data if there is a delta (change). Notice how the count is tested, the data is then sent, and all of the saved data is then purged. This means only delta data will be sent.
  
Line 11: Line 13:
 
At the end:
 
At the end:
  
  C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
+
#ifdef CLIENT_DLL
 +
  CBasePlayer *pLocalPlayer = CBasePlayer::GetLocalPlayer();
 
  byte count = pLocalPlayer->GetUserDataCount();
 
  byte count = pLocalPlayer->GetUserDataCount();
 
  if(count)
 
  if(count)
Line 30: Line 33:
 
  buf->WriteOneBit( 0 );
 
  buf->WriteOneBit( 0 );
 
  }
 
  }
 +
#endif
  
 
====ReadUsercmd====
 
====ReadUsercmd====
Line 55: Line 59:
 
====UserCmd.h====
 
====UserCmd.h====
  
  #include "baseplayer_shared.h"
+
//Forward declarations.
 +
#ifdef CLIENT_DLL
 +
class C_BasePlayer;
 +
#else
 +
class CBasePlayer;
 +
  #endif
  
 
  void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from<font color="green">, CBasePlayer *pPlayer = NULL</font> );
 
  void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from<font color="green">, CBasePlayer *pPlayer = NULL</font> );

Revision as of 15:15, 14 July 2009

This modification allows a mod to send messages from the client to the server without using commands. This allows for a more secure method of communication from the client to the server. A possible implementation would be with VGUI Screens.

Note:This method should only be used on multiplayer servers, where cheating has a negative effect on gameplay.

Changes

UserCmd.cpp

  1. include baseplayer_shared.h before memdbgon.h

Note how WriteUsercmd only writes data if there is a delta (change). Notice how the count is tested, the data is then sent, and all of the saved data is then purged. This means only delta data will be sent.

WriteUsercmd

At the end:

  1. ifdef CLIENT_DLL
	CBasePlayer *pLocalPlayer = CBasePlayer::GetLocalPlayer();
	byte count = pLocalPlayer->GetUserDataCount();
	if(count)
	{
		buf->WriteOneBit( 1 );

		buf->WriteByte(count);

		for(byte x=0;x<count;x++)
		{
			buf->WriteString(pLocalPlayer->GetUserData(x));
		}

		pLocalPlayer->ClearUserData();
	}
	else
	{
		buf->WriteOneBit( 0 );
	}
  1. endif

ReadUsercmd

void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from, CBasePlayer *pPlayer /*= NULL*/ )

At the end:

	if( buf->ReadOneBit() )
	{
		int count = buf->ReadByte();
		for(int x=0;x<count;x++)
		{
			bool bOverflow = false;
			char *cmd = buf->ReadAndAllocateString(&bOverflow);
			Assert(!bOverflow);
#ifndef CLIENT_DLL
			Assert(pPlayer);
			pPlayer->EvaluateUserData(cmd);
#endif
			delete cmd;
		}
	}

UserCmd.h

//Forward declarations.
#ifdef CLIENT_DLL 
	class C_BasePlayer;
#else
	class CBasePlayer;
#endif
void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from, CBasePlayer *pPlayer = NULL );

Changes to gameinterface.cpp

ReadUsercmd( buf, to, from, pPlayer );

Implementation

C_BasePlayer

  • protected CUtlLinkedList<const char *, byte> m_UserData stores all of the data to send (The player should never have to send over 255 strings for 1 usercmd).
  • public void AddUserData(const char *pNewData) adds data to send.
  • public byte GetUserDataCount(void) returns m_UserData.Count().
  • public const char *GetUserData(byte index) returns m_UserData.Element(index).
  • public void ClearUserData(void) calls m_UserData.PurgeAndDeleteElements(). See strdup to make sure you don't accidentally get any null pointers.

CBasePlayer

  • public void EvaluateUserData(const char *pData) evaluates what to do with the command stored in pData.