networking

This commit is contained in:
2025-07-13 15:47:42 +03:00
parent f5b26be510
commit a9c28b8940
345 changed files with 142130 additions and 174 deletions

View File

@@ -7,7 +7,11 @@
class CBaseEntity;
class C_BaseEntity;
enum EPredictionMode {
PREDICTION_MODE_CREATED,
PREDICTION_MODE_DESTROYED,
PREDICTION_MODE_NONE,
};
//-----------------------------------------------------------------------------
// Base server entity class.
// It is updated every 1/tickrate (64) of a second. Does not require special
@@ -22,6 +26,7 @@ public:
virtual void Destroy( void ) = 0;
virtual void ReadParameter( const char *szName, const char *szValue );
virtual void Think( float fDelta ) = 0;
virtual void Sync( void *pData, uint32_t nDataSize ) = 0;
void SetPosition( vec3 position );
void SetRotationEuler( vec3 euler );
void SetRotationQuat( vec4 quaternion );
@@ -32,6 +37,9 @@ public:
mat3 m_matrix;
vec3 m_position;
vec3 m_scale;
uint64_t m_id;
EPredictionMode m_prediction;
};
@@ -46,7 +54,7 @@ public:
const char *m_szName;
const char *m_szClass;
EntityRegistryFn m_pfn;
ClientEntityRegistryFn m_pClientfn;
ClientEntityRegistryFn m_pClientfn = 0;
};
@@ -56,7 +64,7 @@ CBaseEntity *__entity_alloc_##name() \
{ \
return new class; \
}; \
GLOBAL_USED CEntityRegistry __entity_##name##_registry(#name, #class, __entity_alloc_##name); \
CEntityRegistry __entity_##name##_registry(#name, #class, __entity_alloc_##name); \
//-----------------------------------------------------------------------------
@@ -93,7 +101,7 @@ C_BaseEntity *__c_entity_alloc_##server() \
C_EntityRegistry __c_entity_##server##_registry(#server, __c_entity_alloc_##server); \
extern CUtlSelfReferencingVector<CBaseEntity*> g_entities;
extern CUtlVector<CBaseEntity*> g_entities;
extern CUtlVector<CEntityRegistry*> g_RegisteredEntities;

View File

@@ -32,6 +32,7 @@ public:
virtual void ReadParameter( const char *szName, const char *szValue ) override;
virtual void Destroy( void ) override;
virtual void Think( float fDelta ) override;
virtual void Sync( void *pData, uint32_t nDataSize ) override;
CUtlVector<Triangle_t> m_mesh;
Collider *m_collider;

View File

@@ -16,7 +16,7 @@ public:
static void Shutdown();
};
extern IIClient g_localClient;
extern IIClient *g_localClient;
extern CUtlVector<IIClient*> g_clients;
@@ -26,8 +26,10 @@ public:
static CBaseEntity *SpawnEntity( const char *szName );
static void InitEntity(CBaseEntity *pEntity);
static void DestroyEntity( CBaseEntity *pEntity );
static void ConnectClient( IIClient *pClient);
static void DisconnectClient( IIClient *pClient);
static void ConnectClient( IIClient *pClient );
static void DisconnectClient( IIClient *pClient );
static void DisconnectClient( uint64_t playerID );
static void DisconnectClientByHandle( uint32_t playerHandle );
};

1
public/fgui/button.h Normal file
View File

@@ -0,0 +1 @@
#include "widget.h"

View File

@@ -1,27 +1,43 @@
#ifndef FGUI_H
#define FGUI_H
#include "rendering.h"
#include "tier0/platform.h"
#include "tier1/utlstring.h"
abstract_class fgui
class CFGUI_Widget;
struct FGUI_Event_t
{
struct Color_t
{
float r;
float g;
float b;
};
/* every position is resized to some degree of 1280x720 */
struct Vector_t
{
float x;
float y;
};
static void Init( void );
static void Frame( void );
static void Rectangle( Vector_t posPx, Vector_t posFl, Vector_t sizePx, Vector_t sizeFl );
static void Text( const char *psz, uint32_t nSize, Vector_t pos, Vector_t color );
};
#endif
class CFont
{
public:
CUtlString szName;
ITexture *pTexture;
uint32_t glyphWidth;
uint32_t glyphHeight;
char cCharacterSet[256];
};
interface IFGUI
{
public:
static void Init( void );
static void Frame( void );
static void AppendWidget( CFGUI_Widget *pWidget );
static void DestroyWidget( CFGUI_Widget *pWidget );
static void SetRectColor( float r, float g, float b, float a );
static void DrawRect( int32_t iPosX, int32_t iPosY, uint32_t uSizeX, uint32_t uSizeY );
static CFont *LoadFont( CUtlString szFontPath );
static void SetTextFont( CFont *pFont );
static void SetTextPos( float x, float y );
static void SetTextColor( float r, float g, float b, float a );
static void DrawText( CUtlString psz );
};
#endif

26
public/fgui/label.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef FGUI_LABEL_H
#define FGUI_LABEL_H
#include "widget.h"
#include "tier1/utlstring.h"
#include "rendering.h"
class CFGUI_Label: public CFGUI_Widget
{
public:
CFGUI_Label();
~CFGUI_Label();
CUtlString m_szText;
float m_fLabelColor[3];
void SetFont( CUtlString font );
void SetLabel( CUtlString text );
void SetLabelSize( uint32_t nSize );
virtual void Event( FGUI_Event_t event ) override;
virtual void Draw() override;
uint32_t m_fGlyphSize[2];
};
#endif

28
public/fgui/widget.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef FGUI_WIDGET_H
#define FGUI_WIDGET_H
#include "tier0/platform.h"
#include "fgui.h"
class CFGUI_Widget
{
public:
CFGUI_Widget();
CFGUI_Widget(CFGUI_Widget *pParent);
~CFGUI_Widget();
void SetPosition( int32_t nX, int32_t nY );
void SetSize( uint32_t nX, uint32_t nY );
void SetParent( CFGUI_Widget *pParent );
virtual void Event( FGUI_Event_t event ) = 0;
virtual void Draw() = 0;
static void SetDefaultParent(CFGUI_Widget *pParent);
int32_t m_iPosition[2];
uint32_t m_iSize[2];
CFGUI_Widget *m_pParent;
};
#endif

View File

View File

@@ -16,9 +16,28 @@ interface IGameModeManager
public:
static void Init( void );
static void Frame( void );
static void StartGameMode(CGameMode *pGameMode);
static CGameMode *GetCurrentMode( void );
static void RestartCurrentMode( void );
static void StartGameMode( const char *szName );
static const char *GetCurrentGameMode( void );
static CGameMode *GetCurrentGameModeClass( void );
static void RestartCurrentGameMode( void );
};
typedef CGameMode*(*GameModeRegistryFn)();
class CGameModeRegistry
{
public:
CGameModeRegistry( const char *szName, GameModeRegistryFn pfn );
const char *m_szName;
GameModeRegistryFn m_pfn;
};
#define DECLARE_GAME_MODE( class, name ) \
CGameMode *__gamemode_alloc_##name() \
{ \
return new class; \
}; \
CGameModeRegistry __gamemode_##name##_registry(#name, __gamemode_alloc_##name); \
#endif

View File

@@ -1,11 +0,0 @@
#ifndef NET_H
#define NET_H
#include "tier0/platform.h"
interface INet
{
};
#endif

71
public/networking.h Normal file
View File

@@ -0,0 +1,71 @@
#ifndef NET_H
#define NET_H
#include "steam/steam_api.h"
#include "steam/isteamnetworkingsockets.h"
#include "tier0/platform.h"
#include "tier1/commandline.h"
abstract_class IIClient;
class CBasePlayer;
enum EPacketType
{
PACKET_TYPE_CREATE_ENTITTY = 0,
PACKET_TYPE_DESTROY_ENTITTY = 1,
PACKET_TYPE_ENTITY_UPDATE = 2,
// Player can only send this
PACKET_TYPE_PLAYER_MOVEMENT = 3,
PACKET_TYPE_PLAYER_JOIN = 4,
PACKET_TYPE_PLAYER_LEAVE = 5,
PACKET_TYPE_GAMEMODE_START = 6,
PACKET_TYPE_GAMEMODE_END = 7,
};
struct Packet_t
{
uint32_t type;
};
struct PacketPlayer_t: public Packet_t
{
uint64_t playerID;
uint32_t playerHandle;
};
struct PacketGameMode_t: public Packet_t
{
char szName[256];
};
enum EMessageMode
{
MESSAGE_MODE_UNRELIABLE,
MESSAGE_MODE_RELIABLE,
};
interface INetworking
{
public:
static void Init();
static void Deinit();
static void Frame();
static void SendData( void *pData, uint32_t nSize, IIClient *pClient, EMessageMode messageMode );
static void SendDataEverybody( void *pData, uint32_t nSize, EMessageMode messageMode );
static void SendDataEverybodyExcept( void *pData, uint32_t nSize, IIClient *pClient, EMessageMode messageMode );
static void SendDataEverybodyExcept( void *pData, uint32_t nSize, CBasePlayer *pClient, EMessageMode messageMode );
static void ProcessPacket( void *pData, uint32_t nSize, IIClient *pClient );
static void CreateServer();
static void GetServerInfo();
static void JoinServer( const char *szIP );
static void ClientConnectedCallback( SteamNetConnectionStatusChangedCallback_t *pCallback );
static bool IsServer();
static bool IsClient();
static bool IsConnected();
};
#endif

View File

@@ -13,6 +13,7 @@ public:
virtual void ReadParameter( const char *szName, const char *szValue ) override;
virtual void Destroy( void ) override;
virtual void Think( float fDelta ) override;
virtual void Sync( void *pData, uint32_t nDataSize ) override;
virtual void RoundEnd( void );
virtual void RoundStart( IIClient *pClient );

View File

@@ -27,7 +27,7 @@ enum EImageFormat
IMAGE_FORMAT_R32G32B32A32,
IMAGE_FORMAT_DEPTH,
IMAGE_FORMAT_RENDERING = IMAGE_FORMAT_R8G8B8A8,
IMAGE_FORMAT_WINDOW,
};
enum EImageUsage
@@ -240,6 +240,8 @@ abstract_class ITexture
{
public:
const char *szName;
uint32_t x;
uint32_t y;
uint32_t id;
};
@@ -352,6 +354,10 @@ public:
static IImage *GetOutputImage();
};
extern char g_bConfigNotify;
extern uint32_t g_nWindowWidth;
extern uint32_t g_nWindowHeight;
abstract_class IRenderingPipelineStep
{
public:

View File

@@ -2,6 +2,7 @@
#define SERVER_H
#include "tier0/platform.h"
#include "networking.h"
interface IServer
{
@@ -17,6 +18,7 @@ abstract_class IIClient
{
public:
uint64_t playerID;
uint32_t playerHandle;
CBasePlayer *pBasePlayer;
};

12
public/tier0/network.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef TIER0_NETWORK_H
#define TIER0_NETWORK_H
#include "platform.h"
#include "steam/isteamnetworkingsockets.h"
#include "arpa/inet.h"
PLATFORM_INTERFACE void Net_Init();
PLATFORM_INTERFACE void Net_Deinit();
#endif

View File

@@ -10,7 +10,7 @@ public:
static void CreateCommandLine( int argc, char **argv );
static bool CheckParam( const char *psz );
static char *ParamValue( const char* psz );
static char *ParamValue( const char* psz, const char *szDefaultValue = 0 );
static void AddParam( char *psz );
static void RemoveParam( char *psz );

View File

@@ -27,8 +27,11 @@ public:
void AppendAt( size_t nIndex, const T *data, size_t n );
void RemoveHead();
void RemoveHead( size_t n );
void RemoveTail();
void RemoveTail( size_t n );
void RemoveAt( size_t nIndex );
void RemoveAt( size_t nIndex, size_t n );
T *GetData( void );
size_t GetSize( void );
@@ -121,10 +124,17 @@ void CUtlVector<T>::AppendHead( const T &data )
template<typename T>
void CUtlVector<T>::AppendHead( const T *pData, size_t n )
{
size_t prevSize = m_data.GetSize();
m_data.Resize(prevSize+n, n);
for ( size_t i = 0; i < n; i++ )
size_t nOldSize = m_nSize;
m_data.Resize(m_nSize+n);
for (size_t i = nOldSize; i > 0; --i)
{
new (&m_data[i + n - 1]) T(m_data[i - 1]);
m_data[i - 1].~T();
}
for (size_t i = 0; i < n; ++i)
{
new (&m_data[i]) T(pData[i]);
}
m_nSize+=n;
}
@@ -152,23 +162,71 @@ void CUtlVector<T>::AppendAt( size_t nIndex, const T &data )
template<typename T>
void CUtlVector<T>::AppendAt( size_t nIndex, const T *pData, size_t n )
{
size_t nOldSize = m_nSize;
m_data.Resize(m_nSize+n);
for (size_t i = nOldSize; i > nIndex; --i)
{
new (&m_data[i + n - 1]) T(m_data[i - 1]);
m_data[i - 1].~T();
}
for (size_t i = 0; i < n; ++i)
{
new (&m_data[nIndex + i]) T(pData[i]);
}
m_nSize+=n;
}
template<typename T>
void CUtlVector<T>::RemoveHead()
{
m_nSize--;
RemoveHead( 1 );
}
template<typename T>
void CUtlVector<T>::RemoveHead( size_t n )
{
RemoveAt(0, n);
}
template<typename T>
void CUtlVector<T>::RemoveTail()
{
m_nSize--;
RemoveTail( 1 );
}
template<typename T>
void CUtlVector<T>::RemoveTail( size_t n )
{
if (n > m_nSize)
n = m_nSize;
m_data.Resize(m_nSize-n);
m_nSize -= n;
}
template<typename T>
void CUtlVector<T>::RemoveAt( size_t nIndex )
{
m_nSize--;
RemoveAt( nIndex, 1 );
}
template<typename T>
void CUtlVector<T>::RemoveAt( size_t nIndex, size_t n )
{
if ( nIndex >= m_nSize )
{
return;
}
if ( nIndex + n > m_nSize )
{
n = m_nSize - nIndex;
}
for ( size_t i = nIndex; i < nIndex + n; i++ )
{
m_data[i].~T();
}
size_t nElementsToMove = m_nSize - (nIndex + n);
for ( size_t i = 0; i < nElementsToMove; i++ )
{
new (&m_data[nIndex + i]) T(m_data[nIndex + n + i]);
m_data[nIndex + n + i].~T();
}
m_nSize -= n;
}
template<typename T>