static bool LoadFile( char const* pFileName, CUtlBuffer& buf ) { FileHandle_t fp; // load the model if( (fp = g_pFileSystem->Open(pFileName, "rb" )) == NULL) return false; // Get the file size int size = g_pFileSystem->Size( fp ); if (size == 0) { g_pFileSystem->Close( fp ); return false; } buf.EnsureCapacity( size ); g_pFileSystem->Read( buf.PeekPut(), size, fp ); g_pFileSystem->Close( fp ); buf.SeekPut( CUtlBuffer::SEEK_HEAD, size ); buf.SeekGet( CUtlBuffer::SEEK_HEAD, 0 ); return true; }
//----------------------------------------------------------------------------- // NOTE: This is an exact copy of code in BaseFileSystem.cpp which // has to be here because they want to call // the implementation of Open/Size/Read/Write in CBaseVMPIFileSystem //----------------------------------------------------------------------------- bool CBaseVMPIFileSystem::ReadFile( const char *pFileName, const char *pPath, CUtlBuffer &buf, int nMaxBytes, int nStartingByte, FSAllocFunc_t pfnAlloc ) { const char *pReadFlags = "rb"; if ( buf.IsText() && !buf.ContainsCRLF() ) { pReadFlags = "rt"; } FileHandle_t fp = Open( pFileName, buf.IsText() ? "rt" : "rb", pPath ); if ( !fp ) return false; int nBytesToRead = Size( fp ); if ( nMaxBytes > 0 ) { nBytesToRead = min( nMaxBytes, nBytesToRead ); } buf.EnsureCapacity( nBytesToRead + buf.TellPut() ); if ( nStartingByte != 0 ) { Seek( fp, nStartingByte, FILESYSTEM_SEEK_HEAD ); } int nBytesRead = Read( buf.PeekPut(), nBytesToRead, fp ); buf.SeekPut( CUtlBuffer::SEEK_CURRENT, nBytesRead ); Close( fp ); return (nBytesRead != 0); }
bool CUDPSocket::RecvFrom( netadr_t& packet_from, CUtlBuffer& data ) { sockaddr from; int nFromLen = sizeof( from ); int nMaxBytesToRead = data.Size() - data.TellPut(); char *pBuffer = (char*)data.PeekPut(); int nBytesRead = recvfrom( m_Socket, pBuffer, nMaxBytesToRead, 0, &from, &nFromLen ); if ( nBytesRead == SOCKET_ERROR ) { int nError = WSAGetLastError(); if ( nError != WSAEWOULDBLOCK ) { Warning( "Socket error '%i'\n", nError ); } return false; } packet_from.SetFromSockadr( &from ); data.SeekPut( CUtlBuffer::SEEK_CURRENT, nBytesRead ); return true; }
void VMPI_DistributeLightData() { if ( !g_bUseMPI ) return; if ( g_bMPIMaster ) { const char *pVirtualFilename = "--plightdata--"; CUtlBuffer lightFaceData; // write out the light data lightFaceData.EnsureCapacity( pdlightdata->Count() + (numfaces * (MAXLIGHTMAPS+sizeof(int))) ); Q_memcpy( lightFaceData.PeekPut(), pdlightdata->Base(), pdlightdata->Count() ); lightFaceData.SeekPut( CUtlBuffer::SEEK_HEAD, pdlightdata->Count() ); // write out the relevant face info into the stream for ( int i = 0; i < numfaces; i++ ) { for ( int j = 0; j < MAXLIGHTMAPS; j++ ) { lightFaceData.PutChar(g_pFaces[i].styles[j]); } lightFaceData.PutInt(g_pFaces[i].lightofs); } VMPI_FileSystem_CreateVirtualFile( pVirtualFilename, lightFaceData.Base(), lightFaceData.TellMaxPut() ); char cPacketID[2] = { VMPI_VRAD_PACKET_ID, VMPI_SUBPACKETID_PLIGHTDATA_RESULTS }; VMPI_Send2Chunks( cPacketID, sizeof( cPacketID ), pVirtualFilename, strlen( pVirtualFilename ) + 1, VMPI_PERSISTENT ); } else { VMPI_SetCurrentStage( "VMPI_DistributeLightData" ); // Wait until we've received the filename from the master. while ( g_LightResultsFilename.Count() == 0 ) { VMPI_DispatchNextMessage(); } // Open FileHandle_t fp = g_pFileSystem->Open( g_LightResultsFilename.Base(), "rb", VMPI_VIRTUAL_FILES_PATH_ID ); if ( !fp ) Error( "Can't open '%s' to read lighting info.", g_LightResultsFilename.Base() ); int size = g_pFileSystem->Size( fp ); int faceSize = (numfaces*(MAXLIGHTMAPS+sizeof(int))); if ( size > faceSize ) { int lightSize = size - faceSize; CUtlBuffer faceData; pdlightdata->EnsureCount( lightSize ); faceData.EnsureCapacity( faceSize ); g_pFileSystem->Read( pdlightdata->Base(), lightSize, fp ); g_pFileSystem->Read( faceData.Base(), faceSize, fp ); g_pFileSystem->Close( fp ); faceData.SeekPut( CUtlBuffer::SEEK_HEAD, faceSize ); // write out the face data for ( int i = 0; i < numfaces; i++ ) { for ( int j = 0; j < MAXLIGHTMAPS; j++ ) { g_pFaces[i].styles[j] = faceData.GetChar(); } g_pFaces[i].lightofs = faceData.GetInt(); } } } }
//----------------------------------------------------------------------------- // FIXME: assumes that we don't need to do gamma correction. //----------------------------------------------------------------------------- bool WriteToBuffer( unsigned char *pImageData, CUtlBuffer &buffer, int width, int height, ImageFormat srcFormat, ImageFormat dstFormat ) { TGAHeader_t header; // Fix the dstFormat to match what actually is going to go into the file switch( dstFormat ) { case IMAGE_FORMAT_RGB888: dstFormat = IMAGE_FORMAT_BGR888; break; #if defined( _X360 ) case IMAGE_FORMAT_LINEAR_RGB888: dstFormat = IMAGE_FORMAT_LINEAR_BGR888; break; #endif case IMAGE_FORMAT_RGBA8888: dstFormat = IMAGE_FORMAT_BGRA8888; break; } header.id_length = 0; // comment length header.colormap_type = 0; // ??? switch( dstFormat ) { case IMAGE_FORMAT_BGR888: #if defined( _X360 ) case IMAGE_FORMAT_LINEAR_BGR888: #endif header.image_type = 2; // 24/32 bit uncompressed TGA header.pixel_size = 24; break; case IMAGE_FORMAT_BGRA8888: header.image_type = 2; // 24/32 bit uncompressed TGA header.pixel_size = 32; break; case IMAGE_FORMAT_I8: header.image_type = 1; // 8 bit uncompressed TGA header.pixel_size = 8; break; default: return false; break; } header.colormap_index = 0; header.colormap_length = 0; header.colormap_size = 0; header.x_origin = 0; header.y_origin = 0; header.width = ( unsigned short )width; header.height = ( unsigned short )height; header.attributes = 0x20; // Makes it so we don't have to vertically flip the image buffer.PutChar( header.id_length ); buffer.PutChar( header.colormap_type ); buffer.PutChar( header.image_type ); fputLittleShort( header.colormap_index, buffer ); fputLittleShort( header.colormap_length, buffer ); buffer.PutChar( header.colormap_size ); fputLittleShort( header.x_origin, buffer ); fputLittleShort( header.y_origin, buffer ); fputLittleShort( header.width, buffer ); fputLittleShort( header.height, buffer ); buffer.PutChar( header.pixel_size ); buffer.PutChar( header.attributes ); int nSizeInBytes = width * height * ImageLoader::SizeInBytes( dstFormat ); buffer.EnsureCapacity( buffer.TellPut() + nSizeInBytes ); unsigned char *pDst = (unsigned char*)buffer.PeekPut(); if ( !ImageLoader::ConvertImageFormat( pImageData, srcFormat, pDst, dstFormat, width, height ) ) return false; buffer.SeekPut( CUtlBuffer::SEEK_CURRENT, nSizeInBytes ); return true; }