UserCmd strings: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (added debugging code)
mNo edit summary
Line 6: Line 6:
====WriteUsercmd====
====WriteUsercmd====
At the end:
At the end:
  if(g_pLocalPlayer&&g_pLocalPlayer->UserDataChanged())
byte count = g_pLocalPlayer->GetUserDataCount();
  if(g_pLocalPlayer&&count)
  {
  {
  buf->WriteOneBit( 1 );
  buf->WriteOneBit( 1 );
int count = g_pLocalPlayer->GetUserDataCount();
const char *pData;
  buf->WriteByte(count);
  buf->WriteByte(count);
  for(int x=0;x<count;x++)
  for(byte x=0;x<count;x++)
  {
  {
  buf->WriteString(g_pLocalPlayer->GetUserData(x));
  buf->WriteString(g_pLocalPlayer->GetUserData(x));
  }
  }
  g_pLocalPlayer->ClearUserData();
  g_pLocalPlayer->ClearUserData();
  }
  }
Line 51: Line 53:


==Implementation==
==Implementation==
===Client===
===C_BasePlayer===
*<code>int GetUserDataCount(void)</code> returns the number of data strings stored in a char array stored on the player.
*<code>CUtlLinkedList<const char *, byte> m_pUserData</code> stores all of the data to send.
*<code>const char *GetUserData(int index)</code> returns the data stored in the array of user data stored on the player.
*<code>void AddUserData(const char *pNewData)</code> adds data to send.
*<code>void ClearUserData(void)</code> clears the client-side char*[] and sets the boolean so they don't get sent twice.
*<code>byte GetUserDataCount(void)</code> returns <code>m_pUserData.Count()</code>.
*<code>bool UserDataChanged(void)</code> returns the boolean modified by ClearUserData.
*<code>const char *GetUserData(byte index)</code> returns <code>m_pUserData.Element(index)</code>.
===Server===
*<code>void ClearUserData(void)</code> calls <code>m_pUserData.PurgeAndDeleteElements()</code>. See [[CloneString]] to make sure you don't accidentally get any null pointers.
*<code>void EvaluateUserData(const char *pData)</code> evaluates what to do with the commands stored in pData.
===CBasePlayer===
*<code>void EvaluateUserData(const char *pData)</code> evaluates what to do with the command stored in pData.
[[Category:Programming]][[Category:Tutorials]]
[[Category:Programming]][[Category:Tutorials]]

Revision as of 18:58, 17 June 2006

Warning.pngWarning:These changes are only theoretical and haven't been tested whatsoever.

Introduction

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.

Changes

UserCmd.cpp

WriteUsercmd

At the end:

	byte count = g_pLocalPlayer->GetUserDataCount();
	if(g_pLocalPlayer&&count)
	{
		buf->WriteOneBit( 1 );

		buf->WriteByte(count);

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

		g_pLocalPlayer->ClearUserData();
	}
	else
	{
		buf->WriteOneBit( 0 );
	}

ReadUsercmd

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

At the end:

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

UserCmd.h

#include "baseplayer_shared.h"
void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from, CBasePlayer *pPlayer );

in_main.cpp

ReadUsercmd( buf, to, from, NULL );

Changes to gameinterface.cpp

ReadUsercmd( buf, to, from, pPlayer );

Implementation

C_BasePlayer

  • CUtlLinkedList<const char *, byte> m_pUserData stores all of the data to send.
  • void AddUserData(const char *pNewData) adds data to send.
  • byte GetUserDataCount(void) returns m_pUserData.Count().
  • const char *GetUserData(byte index) returns m_pUserData.Element(index).
  • void ClearUserData(void) calls m_pUserData.PurgeAndDeleteElements(). See CloneString to make sure you don't accidentally get any null pointers.

CBasePlayer

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