networking i guess

This commit is contained in:
2026-02-28 21:07:44 +02:00
parent e83f7cd448
commit 03c560c2b7
68 changed files with 1348 additions and 121 deletions

View File

@@ -10,6 +10,7 @@
#ifndef DATAMAP_H
#define DATAMAP_H
#include "stddef.h"
#include "gameclass.h"
enum fieldtype_t {
FIELD_VOID = 0,
@@ -57,12 +58,6 @@ struct datamap_t
int m_iNumFields;
};
#define DECLARE_CLASS_NOBASE( className ) \
typedef className ThisClass;
#define DECLARE_CLASS( className, baseName ) \
typedef baseName BaseClass; \
typedef className ThisClass;
#define DECLARE_DATADESC() \
datamap_t *GetBaseMap(); \
virtual datamap_t *GetDataMap() override; \
@@ -103,6 +98,7 @@ struct datamap_t
#define _class_offsetof( class, var ) ((size_t)&(((class*)0)->var))
#define _FIELD( name, fieldtype, count, flags, mapname, tolerance) { #name, mapname, fieldtype, _class_offsetof(ThisClass, name), count, sizeof(((ThisClass*)0)->name), flags },
#define DEFINE_FIELD( name, fieldtype ) _FIELD( name, fieldtype, 1, FTYPEDESC_KEY, 0, 0)
#define DEFINE_KEYFIELD( name, fieldtype, mapname ) _FIELD( name, fieldtype, 1, FTYPEDESC_KEY, mapname, 0)
#define FTYPEDESC_KEY 0x0004

26
game/shared/engine.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef ENGINE_H
#define ENGINE_H
#include "tier2/ifilesystem.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/igamewindow.h"
#include "networkbase.h"
struct EngineConsts_t
{
bool m_bIsDedicated;
bool m_bIsSteam;
INetworkBase *(LaunchLocalBridge)(uint16_t uPort);
INetworkBase *(LaunchServer)(uint16_t uPort);
INetworkBase *(ConnectLocalBridge)(uint16_t uPort);
INetworkBase *(ConnectSteamServer)(uint64_t uServer, uint16_t uPort);
};
extern IFileSystem *filesystem;
extern IRenderContext *g_pRenderContext;
extern IGameWindow *g_pMainWindow;
extern EngineConsts_t *g_pEngineConstants;
extern INetworkBase *g_pServerBridge;
extern INetworkBase *g_pClientBridge;
#endif

9
game/shared/gameclass.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef GAMECLASS_H
#define GAMECLASS_H
#define DECLARE_CLASS_NOBASE( className ) \
typedef className ThisClass;
#define DECLARE_CLASS( className, baseName ) \
typedef baseName BaseClass; \
typedef className ThisClass;
#endif

118
game/shared/netmap.h Normal file
View File

@@ -0,0 +1,118 @@
#ifndef NETMAP_H
#define NETMAP_H
#include "stddef.h"
#include "stdint.h"
#include "datamap.h"
struct netfield_t
{
const char *m_szName;
fieldtype_t m_eType;
size_t m_uOffset;
size_t m_uSize;
};
struct netmap_t
{
struct netmap_t *m_pBase;
netfield_t *m_pFields;
uint32_t m_uFieldCount;
};
#define NetPropInt(name) { #name, FIELD_INT, _class_offsetof(ThisClass, name), sizeof(name)}
#define NetPropFloat(name) { #name, FIELD_FLOAT, _class_offsetof(ThisClass, name), sizeof(name)}
#define NetPropFloat3(name) \
NetPropFloat(name.x), \
NetPropFloat(name.y), \
NetPropFloat(name.z)
#define NetPropQuaternion(name) \
NetPropFloat(name.x), \
NetPropFloat(name.y), \
NetPropFloat(name.z), \
NetPropFloat(name.w) \
#define __DECLARE_NETCLASS_NOBASE() \
netmap_t *GetBaseSendMap(); \
virtual netmap_t *GetSendMap(); \
virtual netmap_t SendMapInit(); \
netmap_t *GetBaseRecvMap(); \
virtual netmap_t *GetRecvMap(); \
virtual netmap_t RecvMapInit(); \
#define __DECLARE_NETCLASS() \
netmap_t *GetBaseSendMap(); \
virtual netmap_t *GetSendMap() override; \
virtual netmap_t SendMapInit() override; \
netmap_t *GetBaseRecvMap(); \
virtual netmap_t *GetRecvMap() override; \
virtual netmap_t RecvMapInit() override; \
#define DECLARE_SERVERCLASS_NOBASE() \
__DECLARE_NETCLASS_NOBASE() \
#define DECLARE_CLIENTCLASS_NOBASE() \
__DECLARE_NETCLASS_NOBASE() \
#define DECLARE_SERVERCLASS() \
__DECLARE_NETCLASS() \
#define DECLARE_CLIENTCLASS() \
__DECLARE_NETCLASS() \
#define IMPLEMENT_SEND_DT_INTERNAL(className) \
netmap_t *className::GetSendMap() { static netmap_t s_DataMap = ThisClass::SendMapInit(); return &s_DataMap; } \
netmap_t className::SendMapInit() { \
netmap_t map; \
map.m_pBase = ThisClass::GetBaseSendMap(); \
static netfield_t dataDesc[] \
{\
#define IMPLEMENT_SEND_DT_NOBASE(className) \
netmap_t *className::GetBaseSendMap() { return NULL; } \
IMPLEMENT_SEND_DT_INTERNAL(className)
#define IMPLEMENT_SEND_DT(className) \
netmap_t *className::GetBaseSendMap() { return BaseClass::GetSendMap(); } \
IMPLEMENT_SEND_DT_INTERNAL(className)
#define IMPLEMENT_RECV_DT_INTERNAL(className) \
netmap_t *className::GetRecvMap() { static netmap_t s_DataMap = ThisClass::RecvMapInit(); return &s_DataMap; } \
netmap_t className::RecvMapInit() { \
netmap_t map; \
map.m_pBase = ThisClass::GetBaseRecvMap(); \
static netfield_t dataDesc[] \
{\
#define IMPLEMENT_RECV_DT_NOBASE(className) \
netmap_t *className::GetBaseRecvMap() { return NULL; } \
IMPLEMENT_RECV_DT_INTERNAL(className)
#define IMPLEMENT_RECV_DT(className) \
netmap_t *className::GetBaseRecvMap() { return BaseClass::GetRecvMap(); } \
IMPLEMENT_RECV_DT_INTERNAL(className)
#define END_NET_DT_INTERNAL() \
}; \
map.m_uFieldCount = sizeof(dataDesc)/sizeof(netfield_t); \
map.m_pFields = dataDesc; \
return map; \
}
#define END_SEND_DT() \
END_NET_DT_INTERNAL()
#define END_RECV_DT() \
END_NET_DT_INTERNAL()
#define IMPLEMENT_EMPTY_SEND_DT_NOBASE(className) \
IMPLEMENT_SEND_DT_NOBASE(className) \
END_RECV_DT()
#define IMPLEMENT_EMPTY_SEND_DT(className) \
IMPLEMENT_SEND_DT(className) \
END_RECV_DT()
#define IMPLEMENT_EMPTY_RECV_DT_NOBASE(className) \
IMPLEMENT_RECV_DT_NOBASE(className) \
END_RECV_DT()
#define IMPLEMENT_EMPTY_RECV_DT(className) \
IMPLEMENT_RECV_DT(className) \
END_RECV_DT()
#endif

76
game/shared/netprotocol.h Normal file
View File

@@ -0,0 +1,76 @@
#ifndef NET_PROTOCOL_H
#define NET_PROTOCOL_H
#include "stdint.h"
#include <arpa/inet.h>
class CNetworkUInt32
{
public:
CNetworkUInt32() : m_uValue(0) {}
CNetworkUInt32( uint32_t uValue ) : m_uValue(htonl(uValue)) {}
operator uint32_t() const {
return htonl(m_uValue);
}
uint32_t m_uValue;
};
enum EMessageType: uint32_t
{
MESSAGE_PLAYER_JOINED,
MESSAGE_PLAYER_JOINED_CALLBACK,
MESSAGE_PLAYER_LEFT,
MESSAGE_ENTITY_CLASS_SYNC,
MESSAGE_ENTITY_DATA_SYNC,
};
struct PlayerJoined_t
{
EMessageType m_eType;
int8_t m_szPlayerName[256];
};
struct PlayerJoinedCallback_t
{
EMessageType m_eType;
CNetworkUInt32 m_uPlayerIndex;
};
struct PlayerLeft_t
{
EMessageType m_eType;
CNetworkUInt32 m_uPlayerIndex;
};
struct EntityClass_t
{
EMessageType m_eType;
CNetworkUInt32 m_uIndex;
int8_t m_szEntityName[256];
};
struct EntityDataSync_t
{
EMessageType m_eType;
CNetworkUInt32 m_uIndex;
CNetworkUInt32 m_uCount;
};
struct EntityDataSyncValue_t
{
CNetworkUInt32 m_uVariableIndex;
CNetworkUInt32 m_uVariableSize;
};
union PlayerPacket_t
{
EMessageType m_eType;
PlayerJoined_t m_playerJoined;
PlayerJoinedCallback_t m_playerJoinedCallback;
PlayerLeft_t m_playerLeft;
EntityClass_t m_entityClass;
EntityDataSync_t m_entityData;
};
#endif