static int CountBinaryBytes( CUtlBuffer &buf, int *pEndGet ) { // This counts the number of bytes in the uuencoded text int nStartGet = buf.TellGet(); buf.EatWhiteSpace(); *pEndGet = buf.TellGet(); int nByteCount = 0; while ( buf.IsValid() ) { char c1 = buf.GetChar(); char c2 = buf.GetChar(); bool bIsNum1 = ( c1 >= '0' ) && ( c1 <= '9' ); bool bIsNum2 = ( c2 >= '0' ) && ( c2 <= '9' ); bool bIsAlpha1 = (( c1 >= 'A' ) && ( c1 <= 'F' )) || (( c1 >= 'a' ) && ( c1 <= 'f' )); bool bIsAlpha2 = (( c2 >= 'A' ) && ( c2 <= 'F' )) || (( c2 >= 'a' ) && ( c2 <= 'f' )); if ( !(bIsNum1 || bIsAlpha1) || !(bIsNum2 || bIsAlpha2) ) break; buf.EatWhiteSpace(); *pEndGet = buf.TellGet(); ++nByteCount; } buf.SeekGet( CUtlBuffer::SEEK_HEAD, nStartGet ); return nByteCount; }
bool CRunTimeKeyValuesStringTable::ReadStringTable( int numStrings, CUtlBuffer& buf ) { Assert( m_Strings.Count() == 0 ); CUtlVector< int > offsets; offsets.EnsureCapacity( numStrings ); offsets.CopyArray( (int *)( buf.PeekGet() ), numStrings ); // Skip over data buf.SeekGet( CUtlBuffer::SEEK_HEAD, buf.TellGet() + numStrings * sizeof( int ) ); int stringSize = buf.GetInt(); // Read in the string table m_Strings.EnsureCapacity( numStrings ); int i; for ( i = 0 ; i < numStrings; ++i ) { m_Strings.AddToTail( (const char *)buf.PeekGet( offsets[ i ] ) ); } buf.SeekGet( CUtlBuffer::SEEK_HEAD, buf.TellGet() + stringSize ); return true; }
//----------------------------------------------------------------------------- // Is it a PSD file? //----------------------------------------------------------------------------- bool IsPSDFile( CUtlBuffer &buf ) { int nGet = buf.TellGet(); PSDHeader_t header; buf.Get( &header, sizeof(header) ); buf.SeekGet( CUtlBuffer::SEEK_HEAD, nGet ); if ( BigLong( header.m_nSignature ) != PSD_SIGNATURE ) return false; if ( BigShort( header.m_nVersion ) != 1 ) return false; return ( BigShort( header.m_nDepth ) == 8 ); }
bool Unserialize( CUtlBuffer &buf, CUtlBinaryBlock &dest ) { if ( !buf.IsText() ) { int nLen = buf.GetInt( ); dest.SetLength( nLen ); if ( dest.Length() != 0 ) { buf.Get( dest.Get(), dest.Length() ); } if ( nLen != dest.Length() ) { buf.SeekGet( CUtlBuffer::SEEK_CURRENT, nLen - dest.Length() ); return false; } return buf.IsValid(); } int nEndGet; int nByteCount = CountBinaryBytes( buf, &nEndGet ); if ( nByteCount < 0 ) return false; buf.EatWhiteSpace(); int nDest = 0; dest.SetLength( nByteCount ); while( buf.TellGet() < nEndGet ) { char c1 = buf.GetChar(); char c2 = buf.GetChar(); unsigned char b1 = HexCharToInt( c1 ); unsigned char b2 = HexCharToInt( c2 ); if ( b1 == 0xFF || b2 == 0xFF ) return false; dest[ nDest++ ] = b2 | ( b1 << 4 ); buf.EatWhiteSpace(); } return true; }
//----------------------------------------------------------------------------- // Get PSD file image resources //----------------------------------------------------------------------------- PSDImageResources PSDGetImageResources( CUtlBuffer &buf ) { int nGet = buf.TellGet(); // Header PSDHeader_t header; buf.Get( &header, sizeof( header ) ); // Then palette unsigned int numBytesPalette = BigLong( buf.GetUnsignedInt() ); buf.SeekGet( CUtlBuffer::SEEK_CURRENT, numBytesPalette ); // Then image resources unsigned int numBytesImgResources = BigLong( buf.GetUnsignedInt() ); PSDImageResources imgres( numBytesImgResources, ( unsigned char * ) buf.PeekGet() ); // Restore the seek buf.SeekGet( CUtlBuffer::SEEK_HEAD, nGet ); return imgres; }
//----------------------------------------------------------------------------- // Returns information about the PSD file //----------------------------------------------------------------------------- bool PSDGetInfo( CUtlBuffer &buf, int *pWidth, int *pHeight, ImageFormat *pImageFormat, float *pSourceGamma ) { int nGet = buf.TellGet(); PSDHeader_t header; buf.Get( &header, sizeof(header) ); buf.SeekGet( CUtlBuffer::SEEK_HEAD, nGet ); if ( BigLong( header.m_nSignature ) != PSD_SIGNATURE ) return false; if ( BigShort( header.m_nVersion ) != 1 ) return false; if ( BigShort( header.m_nDepth ) != 8 ) return false; *pWidth = BigLong( header.m_nColumns ); *pHeight = BigLong( header.m_nRows ); *pImageFormat = BigShort( header.m_nChannels ) == 3 ? IMAGE_FORMAT_RGB888 : IMAGE_FORMAT_RGBA8888; *pSourceGamma = ARTWORK_GAMMA; return true; }
bool CBaseGameStats::LoadFromFile( void ) { if ( filesystem->FileExists( gamestats->GetStatSaveFileName(), GAMESTATS_PATHID ) ) { char fullpath[ 512 ]; filesystem->RelativePathToFullPath( gamestats->GetStatSaveFileName(), GAMESTATS_PATHID, fullpath, sizeof( fullpath ) ); StatsLog( "Loading stats from '%s'\n", fullpath ); } CUtlBuffer buf; if ( filesystem->ReadFile( gamestats->GetStatSaveFileName(), GAMESTATS_PATHID, buf ) ) { bool bRetVal = true; int version = buf.GetShort(); if ( version > GAMESTATS_FILE_VERSION ) return false; //file is beyond our comprehension // Set global parse version CBGSDriver.m_iLoadedVersion = version; buf.Get( CBGSDriver.m_szLoadedUserID, 16 ); CBGSDriver.m_szLoadedUserID[ sizeof( CBGSDriver.m_szLoadedUserID ) - 1 ] = 0; if ( s_szPseudoUniqueID[ 0 ] != 0 ) { if ( Q_stricmp( CBGSDriver.m_szLoadedUserID, s_szPseudoUniqueID ) ) { //UserID changed, blow away log!!! filesystem->RemoveFile( gamestats->GetStatSaveFileName(), GAMESTATS_PATHID ); filesystem->RemoveFile( GAMESTATS_LOG_FILE, GAMESTATS_PATHID ); Warning( "Userid changed, clearing stats file\n" ); CBGSDriver.m_szLoadedUserID[0] = '\0'; CBGSDriver.m_iLoadedVersion = -1; gamestats->m_BasicStats.Clear(); gamestats->LoadingEvent_PlayerIDDifferentThanLoadedStats(); bRetVal = false; } if ( version <= GAMESTATS_FILE_VERSION_OLD5 ) { gamestats->m_BasicStats.Clear(); bRetVal = false; } else { // Peek ahead in buffer to see if we have the "no default stats" secret flag set. int iCheckForStandardStatsInFile = *( int * )buf.PeekGet(); bool bValid = true; if ( iCheckForStandardStatsInFile != GAMESTATS_STANDARD_NOT_SAVED ) { //the GAMESTATS_STANDARD_NOT_SAVED flag coincides with user completion time, rewind so the gamestats parser can grab it bValid = gamestats->m_BasicStats.ParseFromBuffer( buf, version ); } else { // skip over the flag buf.GetInt(); } if( !bValid ) { m_BasicStats.Clear(); } if( ( buf.TellPut() - buf.TellGet() ) != 0 ) //more data left, must be custom data { gamestats->LoadCustomDataFromBuffer( buf ); } } } return bRetVal; } else { filesystem->RemoveFile( GAMESTATS_LOG_FILE, GAMESTATS_PATHID ); } return false; }
//----------------------------------------------------------------------------- // Creates a collision model (based on the render geometry!) //----------------------------------------------------------------------------- void CVradStaticPropMgr::CreateCollisionModel( char const* pModelName ) { CUtlBuffer buf; CUtlBuffer bufvtx; CUtlBuffer bufphy; int i = m_StaticPropDict.AddToTail( ); m_StaticPropDict[i].m_pModel = 0; if (!LoadStudioModel( pModelName, buf )) { VectorCopy( vec3_origin, m_StaticPropDict[i].m_Mins ); VectorCopy( vec3_origin, m_StaticPropDict[i].m_Maxs ); return; } // if (!LoadStudioModelVtx( pModelName, bufvtx )) // return; studiohdr_t* pHdr = (studiohdr_t*)buf.Base(); // OptimizedModel::FileHeader_t* pVtxHdr = (OptimizedModel::FileHeader_t*)bufvtx.Base(); VectorCopy( pHdr->hull_min, m_StaticPropDict[i].m_Mins ); VectorCopy( pHdr->hull_max, m_StaticPropDict[i].m_Maxs ); if ( LoadStudioCollisionModel( pModelName, bufphy ) ) { phyheader_t header; bufphy.Get( &header, sizeof(header) ); vcollide_t *pCollide = &m_StaticPropDict[i].m_loadedModel; s_pPhysCollision->VCollideLoad( pCollide, header.solidCount, (const char *)bufphy.PeekGet(), bufphy.TellPut() - bufphy.TellGet() ); m_StaticPropDict[i].m_pModel = m_StaticPropDict[i].m_loadedModel.solids[0]; /* static int propNum = 0; char tmp[128]; sprintf( tmp, "staticprop%03d.txt", propNum ); DumpCollideToGlView( pCollide, tmp ); ++propNum; */ } else { // mark this as unused m_StaticPropDict[i].m_loadedModel.solidCount = 0; // CPhysCollide* pPhys = CreatePhysCollide( pHdr, pVtxHdr ); m_StaticPropDict[i].m_pModel = ComputeConvexHull( pHdr ); } }
//----------------------------------------------------------------------------- // Creates a collision model (based on the render geometry!) //----------------------------------------------------------------------------- void CVradStaticPropMgr::CreateCollisionModel( char const* pModelName ) { CUtlBuffer buf; CUtlBuffer bufvtx; CUtlBuffer bufphy; int i = m_StaticPropDict.AddToTail(); m_StaticPropDict[i].m_pModel = NULL; m_StaticPropDict[i].m_pStudioHdr = NULL; if ( !LoadStudioModel( pModelName, buf ) ) { VectorCopy( vec3_origin, m_StaticPropDict[i].m_Mins ); VectorCopy( vec3_origin, m_StaticPropDict[i].m_Maxs ); return; } studiohdr_t* pHdr = (studiohdr_t*)buf.Base(); // necessary for vertex access SetCurrentModel( pHdr ); VectorCopy( pHdr->hull_min, m_StaticPropDict[i].m_Mins ); VectorCopy( pHdr->hull_max, m_StaticPropDict[i].m_Maxs ); if ( LoadStudioCollisionModel( pModelName, bufphy ) ) { phyheader_t header; bufphy.Get( &header, sizeof(header) ); vcollide_t *pCollide = &m_StaticPropDict[i].m_loadedModel; s_pPhysCollision->VCollideLoad( pCollide, header.solidCount, (const char *)bufphy.PeekGet(), bufphy.TellPut() - bufphy.TellGet() ); m_StaticPropDict[i].m_pModel = m_StaticPropDict[i].m_loadedModel.solids[0]; /* static int propNum = 0; char tmp[128]; sprintf( tmp, "staticprop%03d.txt", propNum ); DumpCollideToGlView( pCollide, tmp ); ++propNum; */ } else { // mark this as unused m_StaticPropDict[i].m_loadedModel.solidCount = 0; // CPhysCollide* pPhys = CreatePhysCollide( pHdr, pVtxHdr ); m_StaticPropDict[i].m_pModel = ComputeConvexHull( pHdr ); } // clone it m_StaticPropDict[i].m_pStudioHdr = (studiohdr_t *)malloc( buf.Size() ); memcpy( m_StaticPropDict[i].m_pStudioHdr, (studiohdr_t*)buf.Base(), buf.Size() ); if ( !LoadVTXFile( pModelName, m_StaticPropDict[i].m_pStudioHdr, m_StaticPropDict[i].m_VtxBuf ) ) { // failed, leave state identified as disabled m_StaticPropDict[i].m_VtxBuf.Purge(); } }