Skip to content

Commit

Permalink
spearmint: Have CGame pass sizes for various structs
Browse files Browse the repository at this point in the history
Handle mismatched sizes. This allows extending the structs
without breaking backward/forward compatibility. Missing
fields are cleared.
  • Loading branch information
zturtleman committed Mar 2, 2014
1 parent 240c155 commit 91b3375
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 95 deletions.
33 changes: 27 additions & 6 deletions code/cgame/cg_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Suite 120, Rockville, Maryland 20850 USA.
// major 0 means each minor is an API break.
// major > 0 means each major is an API break and each minor extends API.
#define CG_API_MAJOR_VERSION 0
#define CG_API_MINOR_VERSION 17
#define CG_API_MINOR_VERSION 18


#define CMD_BACKUP 64
Expand All @@ -46,10 +46,6 @@ Suite 120, Rockville, Maryland 20850 USA.
// needs to be larger than PACKET_BACKUP


#ifdef CGAME
#define MAX_ENTITIES_IN_SNAPSHOT 256 * MAX_SPLITVIEW
#endif

// snapshots are a view of the server at a given time

// Snapshots are generated at regular time intervals by the server,
Expand All @@ -69,14 +65,39 @@ typedef struct {
int serverCommandSequence; // snapshot becomes current

int numEntities;
} vmSnapshot_t;

#ifdef CGAME
#define MAX_ENTITIES_IN_SNAPSHOT 256 * MAX_SPLITVIEW

typedef struct {
//
// Must exactly match vmSnapshot_t
//
int snapFlags; // SNAPFLAG_RATE_DELAYED, etc
int ping;

int serverTime; // server time the message is valid for (in msec)

byte areamask[MAX_SPLITVIEW][MAX_MAP_AREA_BYTES]; // portalarea visibility bits

int clientNums[MAX_SPLITVIEW];

int numServerCommands; // text based server commands to execute when this
int serverCommandSequence; // snapshot becomes current

int numEntities;

//
// CGame specific data
//
entityState_t entities[MAX_ENTITIES_IN_SNAPSHOT]; // all of the entities that need to be presented
// at the time of this snapshot

playerState_t pss[MAX_SPLITVIEW]; // complete information about the current players at this time
#endif

} snapshot_t;
#endif

typedef enum {
UIMENU_NONE,
Expand Down
61 changes: 34 additions & 27 deletions code/client/cl_cgame.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ extern qboolean getCameraInfo(int time, vec3_t *origin, vec3_t *angles);
CL_GetGameState
====================
*/
void CL_GetGameState( gameState_t *gs ) {
*gs = cl.gameState;
void CL_GetGameState( gameState_t *gs, int size ) {
Com_Memcpy2( gs, size, &cl.gameState, sizeof ( gameState_t ) );
}

/*
====================
CL_GetGlconfig
====================
*/
void CL_GetGlconfig( glconfig_t *glconfig ) {
*glconfig = cls.glconfig;
void CL_GetGlconfig( glconfig_t *glconfig, int size ) {
Com_Memcpy2( glconfig, size, &cls.glconfig, sizeof ( glconfig_t ) );
}

/*
Expand Down Expand Up @@ -134,12 +134,16 @@ void CL_GetViewAngles( int localPlayerNum, vec3_t angles ) {
CL_GetClientState
====================
*/
static void CL_GetClientState( uiClientState_t *state ) {
state->connectPacketCount = clc.connectPacketCount;
state->connState = clc.state;
Q_strncpyz( state->servername, clc.servername, sizeof( state->servername ) );
Q_strncpyz( state->updateInfoString, cls.updateInfoString, sizeof( state->updateInfoString ) );
Q_strncpyz( state->messageString, clc.serverMessage, sizeof( state->messageString ) );
static void CL_GetClientState( uiClientState_t *vmState, int vmSize ) {
uiClientState_t state;

state.connectPacketCount = clc.connectPacketCount;
state.connState = clc.state;
Q_strncpyz( state.servername, clc.servername, sizeof( state.servername ) );
Q_strncpyz( state.updateInfoString, cls.updateInfoString, sizeof( state.updateInfoString ) );
Q_strncpyz( state.messageString, clc.serverMessage, sizeof( state.messageString ) );

Com_Memcpy2( vmState, vmSize, &state, sizeof ( uiClientState_t ) );
}

/*
Expand Down Expand Up @@ -232,7 +236,8 @@ void CL_GetCurrentSnapshotNumber( int *snapshotNumber, int *serverTime ) {
CL_GetSnapshot
====================
*/
qboolean CL_GetSnapshot( int snapshotNumber, snapshot_t *snapshot, void *playerStates, void *entities, int maxEntitiesInSnapshot ) {
qboolean CL_GetSnapshot( int snapshotNumber, vmSnapshot_t *vmSnapshot, int vmSize, void *playerStates, void *entities, int maxEntitiesInSnapshot ) {
vmSnapshot_t snapshot;
sharedPlayerState_t *ps;
clSnapshot_t *clSnap;
int i, count;
Expand All @@ -259,20 +264,20 @@ qboolean CL_GetSnapshot( int snapshotNumber, snapshot_t *snapshot, void *playerS
}

// write the snapshot
snapshot->snapFlags = clSnap->snapFlags;
snapshot->serverCommandSequence = clSnap->serverCommandNum;
snapshot->ping = clSnap->ping;
snapshot->serverTime = clSnap->serverTime;
snapshot.snapFlags = clSnap->snapFlags;
snapshot.serverCommandSequence = clSnap->serverCommandNum;
snapshot.ping = clSnap->ping;
snapshot.serverTime = clSnap->serverTime;
for (i = 0; i < MAX_SPLITVIEW; i++) {
snapshot->clientNums[i] = clSnap->clientNums[i];
snapshot.clientNums[i] = clSnap->clientNums[i];

ps = (sharedPlayerState_t*)((byte*)playerStates + i * cl.cgamePlayerStateSize);
if ( clSnap->lcIndex[i] == -1 ) {
Com_Memset( snapshot->areamask[i], 0, sizeof( snapshot->areamask[0] ) );
Com_Memset( snapshot.areamask[i], 0, sizeof( snapshot.areamask[0] ) );
Com_Memset( ps, 0, cl.cgamePlayerStateSize );
ps->clientNum = -1;
} else {
Com_Memcpy( snapshot->areamask[i], clSnap->areamask[clSnap->lcIndex[i]], sizeof( snapshot->areamask[0] ) );
Com_Memcpy( snapshot.areamask[i], clSnap->areamask[clSnap->lcIndex[i]], sizeof( snapshot.areamask[0] ) );
Com_Memcpy( ps, DA_ElementPointer( clSnap->playerStates, clSnap->lcIndex[i] ), cl.cgamePlayerStateSize );
}
}
Expand All @@ -282,13 +287,15 @@ qboolean CL_GetSnapshot( int snapshotNumber, snapshot_t *snapshot, void *playerS
Com_DPrintf( "CL_GetSnapshot: truncated %i entities to %i\n", count, maxEntitiesInSnapshot );
count = maxEntitiesInSnapshot;
}
snapshot->numEntities = count;
snapshot.numEntities = count;
for ( i = 0 ; i < count ; i++ ) {
Com_Memcpy( (byte*)entities + i * cl.cgameEntityStateSize, CL_ParseEntityState( clSnap->parseEntitiesNum + i ), cl.cgameEntityStateSize );
}

// FIXME: configstring changes and server commands!!!

Com_Memcpy2( vmSnapshot, vmSize, &snapshot, sizeof ( vmSnapshot_t ) );

return qtrue;
}

Expand Down Expand Up @@ -1303,7 +1310,7 @@ intptr_t CL_CgameSystemCalls( intptr_t *args ) {
case CG_R_REGISTERSHADERNOMIP:
return re.RegisterShaderNoMip( VMA(1) );
case CG_R_REGISTERFONT:
re.RegisterFont( VMA(1), args[2], VMA(3));
re.RegisterFont( VMA(1), args[2], VMA(3), args[4]);
return 0;
case CG_R_ALLOCSKINSURFACE:
return re.AllocSkinSurface( VMA(1), args[2] );
Expand All @@ -1313,10 +1320,10 @@ intptr_t CL_CgameSystemCalls( intptr_t *args ) {
re.ClearScene();
return 0;
case CG_R_ADDREFENTITYTOSCENE:
re.AddRefEntityToScene( VMA(1), 0, NULL, 0 );
re.AddRefEntityToScene( VMA(1), args[2], 0, NULL, 0 );
return 0;
case CG_R_ADDPOLYREFENTITYTOSCENE:
re.AddRefEntityToScene( VMA(1), args[2], VMA(3), args[4] );
re.AddRefEntityToScene( VMA(1), args[2], args[3], VMA(4), args[5] );
return 0;
case CG_R_ADDPOLYTOSCENE:
re.AddPolyToScene( args[1], args[2], VMA(3), 1 );
Expand All @@ -1339,7 +1346,7 @@ intptr_t CL_CgameSystemCalls( intptr_t *args ) {
re.AddCoronaToScene( VMA(1), VMF(2), VMF(3), VMF(4), VMF(5), args[6], args[7] );
return 0;
case CG_R_RENDERSCENE:
re.RenderScene( VMA(1) );
re.RenderScene( VMA(1), args[2] );
return 0;
case CG_R_SETCOLOR:
re.SetColor( VMA(1) );
Expand Down Expand Up @@ -1375,7 +1382,7 @@ intptr_t CL_CgameSystemCalls( intptr_t *args ) {
CL_GetClipboardData( VMA(1), args[2] );
return 0;
case CG_GETGLCONFIG:
CL_GetGlconfig( VMA(1) );
CL_GetGlconfig( VMA(1), args[2] );
return 0;
case CG_GET_VOIP_TIME:
#ifdef USE_VOIP
Expand Down Expand Up @@ -1408,13 +1415,13 @@ intptr_t CL_CgameSystemCalls( intptr_t *args ) {
return 0;
#endif
case CG_GETGAMESTATE:
CL_GetGameState( VMA(1) );
CL_GetGameState( VMA(1), args[2] );
return 0;
case CG_GETCURRENTSNAPSHOTNUMBER:
CL_GetCurrentSnapshotNumber( VMA(1), VMA(2) );
return 0;
case CG_GETSNAPSHOT:
return CL_GetSnapshot( args[1], VMA(2), VMA(3), VMA(4), args[5] );
return CL_GetSnapshot( args[1], VMA(2), args[3], VMA(4), VMA(5), args[6] );
case CG_GETSERVERCOMMAND:
return CL_GetServerCommand( args[1] );
case CG_GETCURRENTCMDNUMBER:
Expand Down Expand Up @@ -1445,7 +1452,7 @@ intptr_t CL_CgameSystemCalls( intptr_t *args ) {
CL_GetViewAngles( args[1], VMA(2) );
return 0;
case CG_GETCLIENTSTATE:
CL_GetClientState( VMA(1) );
CL_GetClientState( VMA(1), args[2] );
return 0;
case CG_GETCONFIGSTRING:
return CL_GetConfigString( args[1], VMA(2), args[3] );
Expand Down
7 changes: 7 additions & 0 deletions code/qcommon/q_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ Suite 120, Rockville, Maryland 20850 USA.
// q_shared.c -- stateless support routines that are included in each code dll
#include "q_shared.h"

void Com_Memcpy2( void *dst, int dstSize, const void *src, int srcSize ) {
Com_Memcpy( dst, src, MIN( dstSize, srcSize ) );
if ( dstSize > srcSize ) {
Com_Memset( (byte*)dst+srcSize, 0, dstSize-srcSize );
}
}

float Com_Clamp( float min, float max, float value ) {
if ( value < min ) {
return min;
Expand Down
1 change: 1 addition & 0 deletions code/qcommon/q_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ void *Hunk_Alloc( int size, ha_pref preference );

#define Com_Memset memset
#define Com_Memcpy memcpy
void Com_Memcpy2( void *dst, int dstSize, const void *src, int srcSize );

#define CIN_system 1
#define CIN_loop 2
Expand Down
2 changes: 1 addition & 1 deletion code/renderercommon/tr_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ qhandle_t RE_RegisterShaderFromImage(const char *name, int lightmapIndex, image_
// font stuff
void R_InitFreeType( void );
void R_DoneFreeType( void );
void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font);
void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *vmFont, int vmFontBufSize);

/*
====================================================================
Expand Down
11 changes: 8 additions & 3 deletions code/renderercommon/tr_font.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,9 @@ static qboolean R_GetFont(const char *name, int pointSize, fontInfo_t *font) {
RE_RegisterFont
===============
*/
void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *vmFont, int vmFontBufSize) {
char strippedName[MAX_QPATH];
fontInfo_t font;

if (!fontName) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: called with empty name\n");
Expand All @@ -601,15 +602,19 @@ void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {

R_IssuePendingRenderCommands();

if ( R_GetFont( fontName, pointSize, font ) )
if ( R_GetFont( fontName, pointSize, &font ) ) {
Com_Memcpy2( vmFont, vmFontBufSize, &font, sizeof ( fontInfo_t ) );
return;
}

COM_StripExtension( fontName, strippedName, sizeof ( strippedName ) );

// If there is no extension, assume this is loading one of the legacy fonts
if( !Q_stricmpn( strippedName, fontName, strlen( fontName ) ) ) {
if ( R_GetFont( "fonts/fontImage", pointSize, font ) )
if ( R_GetFont( "fonts/fontImage", pointSize, &font ) ){
Com_Memcpy2( vmFont, vmFontBufSize, &font, sizeof ( fontInfo_t ) );
return;
}
}

#ifdef BUILD_FREETYPE
Expand Down
6 changes: 3 additions & 3 deletions code/renderercommon/tr_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ typedef struct {
// a scene is built up by calls to R_ClearScene and the various R_Add functions.
// Nothing is drawn until R_RenderScene is called.
void (*ClearScene)( void );
void (*AddRefEntityToScene)( const refEntity_t *re, int numVerts, const polyVert_t *verts, int numPolys );
void (*AddRefEntityToScene)( const refEntity_t *re, int bufsize, int numVerts, const polyVert_t *verts, int numPolys );
void (*AddPolyToScene)( qhandle_t hShader , int numVerts, const polyVert_t *verts, int numPolys );
void (*AddPolyBufferToScene)( polyBuffer_t* pPolyBuffer );
int (*LightForPoint)( vec3_t point, vec3_t ambientLight, vec3_t directedLight, vec3_t lightDir );
void (*AddLightToScene)( const vec3_t org, float radius, float intensity, float r, float g, float b );
void (*AddAdditiveLightToScene)( const vec3_t org, float radius, float intensity, float r, float g, float b );
void (*AddCoronaToScene)( const vec3_t org, float r, float g, float b, float scale, int id, qboolean visible );
void (*RenderScene)( const refdef_t *fd );
void (*RenderScene)( const refdef_t *fd, int bufsize );

void (*SetColor)( const float *rgba ); // NULL = 1,1,1,1
void (*SetClipRegion)( const float *region );
Expand Down Expand Up @@ -114,7 +114,7 @@ typedef struct {
#ifdef __USEA3D
void (*A3D_RenderGeometry) (void *pVoidA3D, void *pVoidGeom, void *pVoidMat, void *pVoidGeomStatus);
#endif
void (*RegisterFont)(const char *fontName, int pointSize, fontInfo_t *font);
void (*RegisterFont)(const char *fontName, int pointSize, fontInfo_t *font, int bufsize);
void (*RemapShader)(const char *oldShader, const char *newShader, const char *offsetTime);
qboolean (*GetEntityToken)( char *buffer, int size );
qboolean (*inPVS)( const vec3_t p1, const vec3_t p2 );
Expand Down
4 changes: 2 additions & 2 deletions code/renderergl1/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -1527,13 +1527,13 @@ void R_InitNextFrame( void );
qhandle_t RE_AddSkinToFrame( int numSurfaces, const qhandle_t *surfaces );

void RE_ClearScene( void );
void RE_AddRefEntityToScene( const refEntity_t *ent, int numVerts, const polyVert_t *verts, int numPolys );
void RE_AddRefEntityToScene( const refEntity_t *ent, int entBufSize, int numVerts, const polyVert_t *verts, int numPolys );
void RE_AddPolyToScene( qhandle_t hShader, int numVerts, const polyVert_t *verts, int numPolys );
void RE_AddPolyBufferToScene( polyBuffer_t* pPolyBuffer );
void RE_AddLightToScene( const vec3_t org, float radius, float intensity, float r, float g, float b );
void RE_AddAdditiveLightToScene( const vec3_t org, float radius, float intensity, float r, float g, float b );
void RE_AddCoronaToScene( const vec3_t org, float r, float g, float b, float scale, int id, qboolean visible );
void RE_RenderScene( const refdef_t *fd );
void RE_RenderScene( const refdef_t *fd, int fdSize );

/*
=============================================================
Expand Down
Loading

0 comments on commit 91b3375

Please sign in to comment.