DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Snapshot.h (8915B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 #ifndef __SNAPSHOT_H__
     29 #define __SNAPSHOT_H__
     30 
     31 #include "snapshot_jobs.h"
     32 
     33 extern idCVar net_verboseSnapshot;
     34 #define NET_VERBOSESNAPSHOT_PRINT	if ( net_verboseSnapshot.GetInteger() > 0 ) idLib::Printf
     35 #define NET_VERBOSESNAPSHOT_PRINT_LEVEL( X, Y )  if ( net_verboseSnapshot.GetInteger() >= ( X ) ) idLib::Printf( Y )
     36 
     37 /*
     38 A snapshot contains a list of objects and their states
     39 */
     40 class idSnapShot {
     41 public:
     42 	idSnapShot();
     43 	idSnapShot( const idSnapShot & other );
     44 	~idSnapShot();
     45 
     46 	void operator=( const idSnapShot & other );
     47 
     48 	// clears the snapshot
     49 	void Clear();
     50 
     51 	int  GetTime() const { return time; }
     52 	void SetTime( int t ) { time = t; }
     53 
     54 	int  GetRecvTime() const { return recvTime; }
     55 	void SetRecvTime( int t ) { recvTime = t; }
     56 
     57 	// Loads only sequence and baseSequence values from the compressed stream
     58 	static void PeekDeltaSequence( const char * deltaMem, int deltaSize, int & sequence, int & baseSequence );
     59 
     60 	// Reads a new object state packet, which is assumed to be delta compressed against this snapshot
     61 	bool ReadDeltaForJob( const char * deltaMem, int deltaSize, int visIndex, idSnapShot * templateStates );
     62 	bool ReadDelta( idFile * file, int visIndex );
     63 
     64 	// Writes an object state packet which is delta compressed against the old snapshot
     65 	struct objectBuffer_t {
     66 		objectBuffer_t() : data( NULL ), size( 0 ) { }
     67 		objectBuffer_t( int s ) : data( NULL ), size( s ) { Alloc( s ); }
     68 		objectBuffer_t( const objectBuffer_t & o ) : data( NULL ), size( 0 ) { *this = o; }
     69 		~objectBuffer_t() { _Release(); }
     70 		void Alloc( int size );
     71 		int NumRefs() { return data == NULL ? 0 : data[size]; }
     72 		objectSize_t Size() const { return size; }
     73 		byte * Ptr() { return data == NULL ? NULL : data ; }
     74 		byte & operator[]( int i ) { return data[i]; }
     75 		void operator=( const objectBuffer_t & other );
     76 
     77 		// (not making private because of idSnapshot)
     78 		void _AddRef();
     79 		void _Release();
     80 	private:
     81 		byte *			data;
     82 		objectSize_t	size;		
     83 	};
     84 
     85 	struct objectState_t {
     86 		objectState_t() : 
     87 			objectNum( 0 ),
     88 			visMask( MAX_UNSIGNED_TYPE( uint32 ) ),
     89 			stale( false ),
     90 			deleted( false ),
     91 			changedCount( 0 ),
     92 			createdFromTemplate( false ),
     93 			
     94 			expectedSequence( 0 )
     95 			{ }
     96 		void Print( const char * name );
     97 
     98 		uint16			objectNum;
     99 		objectBuffer_t	buffer;
    100 		uint32			visMask;
    101 		bool			stale;			// easy way for clients to check if ss obj is stale. Probably temp till client side of vismask system is more fleshed out
    102 		bool			deleted;
    103 		int				changedCount;	// Incremented each time the state changed
    104 		int				expectedSequence;
    105 		bool			createdFromTemplate;
    106 	};
    107 
    108 	struct submitDeltaJobsInfo_t {
    109 		objParms_t *		objParms;				// Start of object parms
    110 		int					maxObjParms;			// Max parms (which will dictate how many objects can be processed)
    111 		uint8 *				objMemory;				// Memory that objects were written out to
    112 		objHeader_t *		headers;				// Memory for headers
    113 		int					maxHeaders;
    114 		int					maxObjMemory;			// Max memory (which will dictate when syncs need to occur)
    115 		lzwParm_t *			lzwParms;				// Start of lzw parms
    116 		int					maxDeltaParms;			// Max lzw parms (which will dictate how many syncs we can have)
    117 		
    118 		idSnapShot *		oldSnap;				// snap we are comparing this snap to (to produce a delta)
    119 		int					visIndex; 
    120 		int					baseSequence;
    121 
    122 		idSnapShot *		templateStates;			// states for new snapObj that arent in old states
    123 		
    124 		lzwInOutData_t *	lzwInOutData;
    125 	};
    126 
    127 	void SubmitWriteDeltaToJobs( const submitDeltaJobsInfo_t & submitDeltaJobInfo );
    128 
    129 	bool WriteDelta( idSnapShot & old, int visIndex, idFile * file, int maxLength, int optimalLength = 0 );
    130 
    131 	// Adds an object to the state, overwrites any existing object with the same number
    132 	objectState_t * S_AddObject( int objectNum, uint32 visMask, const idBitMsg & msg, const char * tag = NULL ) { return S_AddObject( objectNum, visMask, msg.GetReadData(), msg.GetSize(), tag ); }
    133 	objectState_t * S_AddObject( int objectNum, uint32 visMask, const byte * buffer, int size, const char * tag = NULL ) { return S_AddObject( objectNum, visMask, (const char *)buffer, size, tag ); }
    134 	objectState_t * S_AddObject( int objectNum, uint32 visMask, const char * buffer, int size, const char * tag = NULL );
    135 	bool CopyObject( const idSnapShot & oldss, int objectNum, bool forceStale = false );
    136 	int CompareObject( const idSnapShot * oldss, int objectNum, int start=0, int end=0, int oldStart=0 );
    137 
    138 	// returns the number of objects in this snapshot
    139 	int NumObjects() const { return objectStates.Num(); }
    140 
    141 	// Returns the object number of the specified object, also fills the bitmsg
    142 	int GetObjectMsgByIndex( int i, idBitMsg & msg, bool ignoreIfStale = false ) const;
    143 
    144 	// returns true if the object was found in the snapshot
    145 	bool GetObjectMsgByID( int objectNum, idBitMsg & msg, bool ignoreIfStale = false ) { return GetObjectMsgByIndex( FindObjectIndexByID( objectNum ), msg, ignoreIfStale ) == objectNum; }
    146 
    147 	// returns the object index or -1 if it's not found
    148 	int FindObjectIndexByID( int objectNum ) const;
    149 	
    150 	// returns the object by id, or NULL if not found
    151 	objectState_t *	FindObjectByID( int objectNum ) const;
    152 
    153 	// Returns whether or not an object is stale
    154 	bool ObjectIsStaleByIndex( int i ) const;
    155 
    156 	int ObjectChangedCountByIndex( int i ) const;
    157 
    158 	// clears the empty states from the snapshot snapshot
    159 	void CleanupEmptyStates();
    160 
    161 	void PrintReport();
    162 
    163 	void UpdateExpectedSeq( int newSeq );
    164 
    165 	void			ApplyToExistingState( int objId, idBitMsg & msg );
    166 	objectState_t *	GetTemplateState( int objNum, idSnapShot * templateStates, objectState_t * newState = NULL );
    167 
    168 	void	RemoveObject( int objId );
    169 
    170 private:
    171 
    172 	idList< objectState_t *, TAG_IDLIB_LIST_SNAPSHOT>							objectStates;
    173 	idBlockAlloc< objectState_t, 16, TAG_NETWORKING >	allocatedObjs;
    174 
    175 	int													time;
    176 	int													recvTime;
    177 
    178 	int				BinarySearch( int objectNum ) const;
    179 	objectState_t &	FindOrCreateObjectByID( int objectNum );					// objIndex is optional parm for returning the index of the obj
    180 
    181 	void			SubmitObjectJob(	const submitDeltaJobsInfo_t &	submitDeltaJobsInfo,		// Struct containing parameters originally passed in to SubmitWriteDeltaToJobs
    182 										objectState_t *					newState,					// New obj state (can be NULL, which means deleted)
    183 										objectState_t *					oldState,					// Old obj state (can be NULL, which means new)
    184 										objParms_t *&					baseObjParm,				// Starting obj parm of current stream
    185 										objParms_t *&					curObjParm,					// Current obj parm of current stream
    186 										objHeader_t *&					curHeader,					// Current header dest
    187 										uint8 *&						curObjDest,					// Current write pos of current obj
    188 										lzwParm_t *&					curlzwParm  );				// Current delta parm for next lzw job
    189 	void SubmitLZWJob( 
    190 		const submitDeltaJobsInfo_t &	writeDeltaInfo,		// Struct containing parameters originally passed in to SubmitWriteDeltaToJobs
    191 		objParms_t *&					baseObjParm,		// Pointer to the first obj parm for the current stream
    192 		objParms_t *&					curObjParm,			// Current obj parm
    193 		lzwParm_t *&					curlzwParm,			// Current delta parm
    194 		bool							saveDictionary		// If true, this is the first of several calls which will be appended
    195 	);		
    196 	
    197 	void WriteObject( idFile * file, int visIndex, objectState_t * newState, objectState_t * oldState, int & lastobjectNum );
    198 	void FreeObjectState( int index );
    199 };
    200 
    201 #endif // __SNAPSHOT_H__