static bool SrcPyPathIsInGameFolder( const char *pPath ) { #if 0 if( SrcPySystem()->IsPathProtected() ) { // Verify the file is in the gamefolder char searchPaths[MAX_PATH]; filesystem->GetSearchPath( "MOD", true, searchPaths, sizeof( searchPaths ) ); V_StripTrailingSlash( searchPaths ); if( V_IsAbsolutePath( pPath ) ) { if( V_strnicmp( pPath, searchPaths, V_strlen( searchPaths ) ) != 0 ) return false; } else { char pFullPath[MAX_PATH]; char moddir[MAX_PATH]; filesystem->RelativePathToFullPath( ".", "MOD", moddir, sizeof( moddir ) ); V_MakeAbsolutePath( pFullPath, sizeof( pFullPath ), pPath, moddir ); if( V_strnicmp( pFullPath, searchPaths, V_strlen(searchPaths) ) != 0 ) return false; } } #endif // 0 return true; }
//----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- boost::python::list PyFS_ListDir( const char *pPath, const char *pPathID, const char *pWildCard, bool appendslashdir ) { if( !pPath || !pWildCard ) return boost::python::list(); const char *pFileName; char wildcard[MAX_PATH]; FileFindHandle_t fh; boost::python::list result; // Construct the wildcard V_strncpy( wildcard, pPath, sizeof( wildcard ) ); if( V_strlen( wildcard ) == 0 ) V_strncpy( wildcard, "./", sizeof( wildcard ) ); if( appendslashdir && filesystem->IsDirectory( pPath ) ) V_AppendSlash( wildcard, sizeof( wildcard ) ); V_strcat( wildcard, pWildCard, sizeof( wildcard ) ); pFileName = filesystem->FindFirstEx( wildcard, pPathID, &fh ); while( pFileName ) { result.append( boost::python::object( boost::python::handle<>( PyUnicode_DecodeUTF8( pFileName, V_strlen( pFileName ), "ignore" ) ) ) ); pFileName = filesystem->FindNext( fh ); } filesystem->FindClose( fh ); return result; }
CUtlString CUtlString::Replace( const char *pszFrom, const char *pszTo ) const { Assert( pszTo ); // Can be 0 length, but not null Assert( pszFrom && *pszFrom ); // Must be valid and have one character. const char *pos = V_strstr( String(), pszFrom ); if ( !pos ) { return *this; } const char *pFirstFound = pos; // count number of search string int nSearchCount = 0; int nSearchLength = V_strlen( pszFrom ); while ( pos ) { nSearchCount++; int nSrcOffset = ( pos - String() ) + nSearchLength; pos = V_strstr( String() + nSrcOffset, pszFrom ); } // allocate the new string int nReplaceLength = V_strlen( pszTo ); int nAllocOffset = nSearchCount * ( nReplaceLength - nSearchLength ); size_t srcLength = Length(); CUtlString strDest; size_t destLength = srcLength + nAllocOffset; strDest.SetLength( destLength ); // find and replace the search string pos = pFirstFound; int nDestOffset = 0; int nSrcOffset = 0; while ( pos ) { // Found an instance int nCurrentSearchOffset = pos - String(); int nCopyLength = nCurrentSearchOffset - nSrcOffset; V_strncpy( strDest.GetForModify() + nDestOffset, String() + nSrcOffset, nCopyLength + 1 ); nDestOffset += nCopyLength; V_strncpy( strDest.GetForModify() + nDestOffset, pszTo, nReplaceLength + 1 ); nDestOffset += nReplaceLength; nSrcOffset = nCurrentSearchOffset + nSearchLength; pos = V_strstr( String() + nSrcOffset, pszFrom ); } // making sure that the left over string from the source is the same size as the left over dest buffer Assert( destLength - nDestOffset == srcLength - nSrcOffset ); if ( destLength - nDestOffset > 0 ) { V_strncpy( strDest.GetForModify() + nDestOffset, String() + nSrcOffset, destLength - nDestOffset + 1 ); } return strDest; }
void BuildPingHeader( CUtlVector<char> &data, char packetID, int iState ) { // Figure out the computer's name. char computerName[128]; DWORD computerNameLen = sizeof( computerName ); GetComputerName( computerName, &computerNameLen ); // Ping back at them. data.AddToTail( VMPI_PROTOCOL_VERSION ); data.AddToTail( packetID ); data.AddToTail( (char)iState ); DWORD liveTime = GetTickCount() - g_AppStartTime; data.AddMultipleToTail( sizeof( liveTime ), (char*)&liveTime ); data.AddMultipleToTail( sizeof( g_SocketPort ), (char*)&g_SocketPort ); data.AddMultipleToTail( strlen( computerName ) + 1, computerName ); if ( g_hRunningProcess ) data.AddMultipleToTail( strlen( g_CurMasterName ) + 1, g_CurMasterName ); else data.AddMultipleToTail( 1, "" ); // Write in how long the worker app has been running. DWORD appRunTime = 0; if ( g_hRunningProcess ) appRunTime = GetTickCount() - g_CreateProcessTime; data.AddMultipleToTail( sizeof( appRunTime ), (char*)&appRunTime ); // Finally, write the password. if ( g_pPassword ) data.AddMultipleToTail( strlen( g_pPassword ) + 1, g_pPassword ); else data.AddToTail( 0 ); data.AddMultipleToTail( V_strlen( g_VersionString ) + 1, g_VersionString ); int processorPercentage, memoryUsageMegabytes; GetRunningProcessStats( processorPercentage, memoryUsageMegabytes ); // Write processor percentage. data.AddToTail( (char)processorPercentage ); // Write the EXE name. data.AddMultipleToTail( V_strlen( g_RunningProcess_ExeName ) + 1, g_RunningProcess_ExeName ); // Write memory usage. short memUsageShort = (short)memoryUsageMegabytes; data.AddMultipleToTail( sizeof( memUsageShort ), (const char*)&memUsageShort ); // Write the map name. data.AddMultipleToTail( V_strlen( g_RunningProcess_MapName ) + 1, g_RunningProcess_MapName ); }
bool VideoPlayerPanel::SetVideo( const char *pVideoFile ) { ClearVideo(); // clearing the video? if ( pVideoFile == NULL || pVideoFile[0] == 0x00 ) { return true; } // create the material m_VideoMaterial = g_pVideo->CreateVideoMaterial( "VideoPlayerMaterial", pVideoFile, "GAME", VideoPlaybackFlags::DEFAULT_MATERIAL_OPTIONS, VideoSystem::DETERMINE_FROM_FILE_EXTENSION, true ); if ( m_VideoMaterial == NULL ) { return false; } // save filename int sLen = V_strlen( pVideoFile ) + 1; m_VideoFileName = new char[ sLen ]; V_strncpy( m_VideoFileName, pVideoFile, sLen ); // compute Playback dimensions int nWidth, nHeight; m_VideoMaterial->GetVideoImageSize( &nWidth, &nHeight ); m_VideoMaterial->GetVideoTexCoordRange( &m_flU, &m_flV ); float flFrameRatio = ( (float) GetWide() / (float) GetTall() ); float flVideoRatio = ( (float) nWidth / (float) nHeight ); if ( flVideoRatio > flFrameRatio ) { m_nPlaybackWidth = GetWide(); m_nPlaybackHeight = ( GetWide() / flVideoRatio ); m_letterBox = 1; } else if ( flVideoRatio < flFrameRatio ) { m_nPlaybackWidth = ( GetTall() * flVideoRatio ); m_nPlaybackHeight = GetTall(); m_letterBox = 2; } else { m_nPlaybackWidth = GetWide(); m_nPlaybackHeight = GetTall(); m_letterBox = 0; } m_pMaterial = m_VideoMaterial->GetMaterial(); m_VideoDuration = m_VideoMaterial->GetVideoDuration(); m_VideoLoaded = true; return true; }
//----------------------------------------------------------------------------- // Purpose: // Input : *path - // *extension - // pathStringLength - //----------------------------------------------------------------------------- void V_DefaultExtension( char *path, const char *extension, int pathStringLength ) { Assert( path ); Assert( pathStringLength >= 1 ); Assert( extension ); Assert( extension[0] == '.' ); char *src; // if path doesn't have a .EXT, append extension // (extension should include the .) src = path + V_strlen(path) - 1; while ( !PATHSEPARATOR( *src ) && ( src > path ) ) { if (*src == '.') { // it has an extension return; } src--; } // Concatenate the desired extension V_strncat( path, extension, pathStringLength, COPY_ALL_CHARACTERS ); }
//----------------------------------------------------------------------------- // Purpose: // Input : *in - // *out - // outSize - //----------------------------------------------------------------------------- void V_StripExtension( const char *in, char *out, int outSize ) { // Find the last dot. If it's followed by a dot or a slash, then it's part of a // directory specifier like ../../somedir/./blah. // scan backward for '.' int end = V_strlen( in ) - 1; while ( end > 0 && in[end] != '.' && !PATHSEPARATOR( in[end] ) ) { --end; } if (end > 0 && !PATHSEPARATOR( in[end] ) && end < outSize) { int nChars = min( end, outSize-1 ); if ( out != in ) { memcpy( out, in, nChars ); } out[nChars] = 0; } else { // nothing found if ( out != in ) { V_strncpy( out, in, outSize ); } } }
//----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void PyFS_WriteFile( const char *filepath, const char *pathid, const char *content ) { if( !filepath ) { PyErr_SetString(PyExc_IOError, "No filepath specified" ); throw boost::python::error_already_set(); } if( !content ) { PyErr_SetString(PyExc_IOError, "Content cannot be empty" ); throw boost::python::error_already_set(); } if( !SrcPyPathIsInGameFolder( filepath ) ) { PyErr_SetString(PyExc_IOError, "filesystem module only allows paths in the game folder" ); throw boost::python::error_already_set(); } CUtlBuffer buf( 0, 0, CUtlBuffer::TEXT_BUFFER ); buf.Put( content, V_strlen( content ) ); if( !filesystem->WriteFile( filepath, pathid, buf ) ) { PyErr_SetString(PyExc_IOError, "Failed to write file" ); throw boost::python::error_already_set(); } }
char* CopyString( const char *pStr ) { int len = V_strlen( pStr ) + 1; char *pRet = new char[len]; V_strncpy( pRet, pStr, len ); return pRet; }
void CTextureSystem::OnFileChange( const char *pFilename, int context, CTextureSystem::EFileType eFileType ) { // It requires the forward slashes later... char fixedSlashes[MAX_PATH]; V_StrSubst( pFilename, "\\", "/", fixedSlashes, sizeof( fixedSlashes ) ); // Get rid of the extension. if ( V_strlen( fixedSlashes ) < 5 ) { Assert( false ); return; } fixedSlashes[ V_strlen( fixedSlashes ) - 4 ] = 0; // Handle it based on what type of file we've got. if ( eFileType == k_eFileTypeVMT ) { IEditorTexture *pTex = FindActiveTexture( fixedSlashes, NULL, FALSE ); if ( pTex ) { pTex->Reload( true ); } else { EnumMaterial( fixedSlashes, context ); IEditorTexture *pTex = FindActiveTexture( fixedSlashes, NULL, FALSE ); if ( pTex ) { GetMainWnd()->m_TextureBar.NotifyNewMaterial( pTex ); GetMainWnd()->GetFaceEditSheet()->NotifyNewMaterial( pTex ); } } } else if ( eFileType == k_eFileTypeVTF ) { // Whether a VTF was added, removed, or modified, we do the same thing.. refresh it and any materials that reference it. ITexture *pTexture = materials->FindTexture( fixedSlashes, TEXTURE_GROUP_UNACCOUNTED, false ); if ( pTexture ) { pTexture->Download( NULL ); ReloadMaterialsUsingTexture( pTexture ); } } }
char *_V_strrchr(const char* file, int line, const char *s, char c) { AssertValidStringPtr( s ); int len = V_strlen(s); s += len; while (len--) if (*--s == c) return (char *)s; return 0; }
boost::python::object C_PlayerResource::PyGetPlayerName( int index ) { const char *pPlayerName = GetPlayerName( index ); return boost::python::object( boost::python::handle<>( PyUnicode_DecodeUTF8( pPlayerName, V_strlen( pPlayerName ), "ignore" ) ) ); }
//----------------------------------------------------------------------------- // Purpose: Extracts the base name of a file (no path, no extension, assumes '/' or '\' as path separator) // Input : *in - // *out - // maxlen - //----------------------------------------------------------------------------- void V_FileBase( const char *in, char *out, int maxlen ) { Assert( maxlen >= 1 ); Assert( in ); Assert( out ); if ( !in || !in[ 0 ] ) { *out = 0; return; } int len, start, end; len = V_strlen( in ); // scan backward for '.' end = len - 1; while ( end&& in[end] != '.' && !PATHSEPARATOR( in[end] ) ) { end--; } if ( in[end] != '.' ) // no '.', copy to end { end = len-1; } else { end--; // Found ',', copy to left of '.' } // Scan backward for '/' start = len-1; while ( start >= 0 && !PATHSEPARATOR( in[start] ) ) { start--; } if ( start < 0 || !PATHSEPARATOR( in[start] ) ) { start = 0; } else { start++; } // Length of new sting len = end - start + 1; int maxcopy = min( len + 1, maxlen ); // Copy partial string V_strncpy( out, &in[start], maxcopy ); }
void V_AppendSlash( char *pStr, int strSize ) { int len = V_strlen( pStr ); if ( len > 0 && !PATHSEPARATOR(pStr[len-1]) ) { if ( len+1 >= strSize ) Error( "V_AppendSlash: ran out of space on %s.", pStr ); pStr[len] = CORRECT_PATH_SEPARATOR; pStr[len+1] = 0; } }
//----------------------------------------------------------------------------- // Purpose: // Input : *ppath - //----------------------------------------------------------------------------- void V_StripTrailingSlash( char *ppath ) { Assert( ppath ); int len = V_strlen( ppath ); if ( len > 0 ) { if ( PATHSEPARATOR( ppath[ len - 1 ] ) ) { ppath[ len - 1 ] = 0; } } }
//----------------------------------------------------------------------------- // Purpose: Builds all navigation meshes //----------------------------------------------------------------------------- bool CRecastMgr::Build( bool loadDefaultMeshes ) { double fStartTime = Plat_FloatTime(); // Load map mesh if( !LoadMapMesh() ) { Warning("CRecastMesh::Build: failed to load map data!\n"); return false; } // Insert all meshes first if( loadDefaultMeshes ) { InitDefaultMeshes(); } CUtlVector<CRecastMesh *> meshesToBuild; for ( int i = m_Meshes.First(); i != m_Meshes.InvalidIndex(); i = m_Meshes.Next(i ) ) { if( IsMeshBuildDisabled( m_Meshes[i]->GetName() ) ) continue; meshesToBuild.AddToTail( m_Meshes[i] ); } // Create meshes if( recast_build_threaded.GetBool() ) { // Build threaded CParallelProcessor<CRecastMesh *, CFuncJobItemProcessor<CRecastMesh *>, 2 > processor; processor.m_ItemProcessor.Init( &ThreadedBuildMesh, &PreThreadedBuildMesh, &PostThreadedBuildMesh ); processor.Run( meshesToBuild.Base(), meshesToBuild.Count(), 1, recast_build_numthreads.GetInt(), g_pThreadPool ); } else { if( V_strlen( recast_build_single.GetString() ) > 0 ) { BuildMesh( m_pMapMesh, recast_build_single.GetString() ); } else { for( int i = 0; i < meshesToBuild.Count(); i++ ) { BuildMesh( m_pMapMesh, meshesToBuild[i]->GetName() ); } } } m_bLoaded = true; DevMsg( "CRecastMgr: Finished generating %d meshes in %f seconds\n", m_Meshes.Count(), Plat_FloatTime() - fStartTime ); return true; }
void CUtlString::Append( const char *pchAddition, int nChars ) { nChars = Min<int>( nChars, V_strlen( pchAddition ) ); const int lhsLength( Length() ); const int rhsLength( nChars ); const int requestedLength( lhsLength + rhsLength ); AllocMemory( requestedLength ); const int allocatedLength( requestedLength ); const int copyLength( allocatedLength - lhsLength < rhsLength ? allocatedLength - lhsLength : rhsLength ); memcpy( GetForModify() + lhsLength, pchAddition, copyLength ); m_pString[ allocatedLength ] = '\0'; }
void SetPassword( const char *pPassword ) { delete [] g_pPassword; if ( pPassword ) { int len = V_strlen( pPassword ) + 1; g_pPassword = new char[len]; V_strncpy( g_pPassword, pPassword, len ); } else { g_pPassword = NULL; } }
bool V_AppendSlash( char *pStr, int strSize ) { int len = V_strlen( pStr ); if ( len > 0 && !PATHSEPARATOR(pStr[len-1]) ) { if ( len+1 >= strSize ) { ptERROR( "V_AppendSlash: ran out of space on %s.", pStr ); return false; } pStr[len] = CORRECT_PATH_SEPARATOR; pStr[len+1] = 0; } return true; }
void SendPatchCommandToUIs( DWORD dwInstallerProcessId ) { Msg( "SendPatchCommandToUIs\n "); CUtlVector<char> data; data.AddToTail( VMPI_SERVICE_UI_PROTOCOL_VERSION ); data.AddToTail( VMPI_SERVICE_TO_UI_PATCHING ); // This arg tells the UI whether to exit after running the command or not. data.AddToTail( 1 ); // First argument is the working directory, which is the cache path in this case. data.AddMultipleToTail( V_strlen( g_FileCachePath ) + 1, g_FileCachePath ); // Second argument is the command line. char waitAndRestartExe[MAX_PATH], serviceUIExe[MAX_PATH], commandLine[1024 * 8]; V_ComposeFileName( g_FileCachePath, "WaitAndRestart.exe", waitAndRestartExe, sizeof( waitAndRestartExe ) ); V_ComposeFileName( g_BaseAppPath, "vmpi_service_ui.exe", serviceUIExe, sizeof( serviceUIExe ) ); // We're running the UI from the same directory this exe is in. char strSeconds[64]; V_snprintf( strSeconds, sizeof( strSeconds ), "*%lu", dwInstallerProcessId ); // IMPORTANT to use BuildCommandLineFromArgs here because it'll handle slashes and quotes correctly. // If we don't do that, the command often won't work. CUtlVector<char*> args; args.AddToTail( waitAndRestartExe ); args.AddToTail( strSeconds ); args.AddToTail( g_BaseAppPath ); args.AddToTail( serviceUIExe ); BuildCommandLineFromArgs( args, commandLine, sizeof( commandLine ) ); data.AddMultipleToTail( V_strlen( commandLine ) + 1, commandLine ); if ( g_pConnMgr ) { g_pConnMgr->SendPacket( -1, data.Base(), data.Count() ); Sleep( 1000 ); // Make sure this packet goes out. } }
//----------------------------------------------------------------------------- // Purpose: This function fixes cases of filenames like materials\\blah.vmt or somepath\otherpath\\ and removes the extra double slash. //----------------------------------------------------------------------------- void V_FixDoubleSlashes( char *pStr ) { int len = V_strlen( pStr ); for ( int i=1; i < len-1; i++ ) { if ( (pStr[i] == '/' || pStr[i] == '\\') && (pStr[i+1] == '/' || pStr[i+1] == '\\') ) { // This means there's a double slash somewhere past the start of the filename. That // can happen in Hammer if they use a material in the root directory. You'll get a filename // that looks like 'materials\\blah.vmt' V_memmove( &pStr[i], &pStr[i+1], len - i ); --len; } } }
//----------------------------------------------------------------------------- // Purpose: Remove final filename from string // Input : *path - // Output : void V_StripFilename //----------------------------------------------------------------------------- void V_StripFilename (char *path) { int length; length = V_strlen( path )-1; if ( length <= 0 ) return; while ( length > 0 && !PATHSEPARATOR( path[length] ) ) { length--; } path[ length ] = 0; }
void V_IsolateFirstDir (char *path) { int lengthMax; int pos = 0; lengthMax = V_strlen( path )-1; if ( lengthMax <= 0 ) return; while ( pos <= lengthMax && !PATHSEPARATOR( path[pos] ) ) { pos++; } path[ pos ] = 0; }
//----------------------------------------------------------------------------- // Purpose: Copies string using local new/delete operators // Input : *from - // Output : char //----------------------------------------------------------------------------- char *ConCommandBase::CopyString( const char *from ) { int len; char *to; len = V_strlen( from ); if ( len <= 0 ) { to = new char[1]; to[0] = 0; } else { to = new char[len+1]; Q_strncpy( to, from, len+1 ); } return to; }
char *V_StripFirstDir (char *path) { int len_max; len_max = V_strlen( path )-1; if ( len_max <= 0 ) return NULL; int pos = 0; while ( pos <= len_max && !PATHSEPARATOR( path[pos] ) ) { pos++; } if ( PATHSEPARATOR( path[pos] ) && pos < len_max ) pos++; //path[ pos ] = 0; return &path[ pos ]; }
//----------------------------------------------------------------------------- // Purpose: Strip off the last directory from dirName // Input : *dirName - // maxlen - // Output : Returns true on success, false on failure. //----------------------------------------------------------------------------- bool V_StripLastDir( char *dirName, int maxlen ) { if( dirName[0] == 0 || !V_stricmp( dirName, "./" ) || !V_stricmp( dirName, ".\\" ) ) return false; int len = V_strlen( dirName ); Assert( len < maxlen ); // skip trailing slash if ( PATHSEPARATOR( dirName[len-1] ) ) { len--; } while ( len > 0 ) { if ( PATHSEPARATOR( dirName[len-1] ) ) { dirName[len] = 0; V_FixSlashes( dirName, CORRECT_PATH_SEPARATOR ); return true; } len--; } // Allow it to return an empty string and true. This can happen if something like "tf2/" is passed in. // The correct behavior is to strip off the last directory ("tf2") and return true. if( len == 0 ) { V_snprintf( dirName, maxlen, ".%c", CORRECT_PATH_SEPARATOR ); return true; } return true; }
//----------------------------------------------------------------------------- // Normalizes a float string in place. // // (removes leading zeros, trailing zeros after the decimal point, and the decimal point itself where possible) //----------------------------------------------------------------------------- void V_normalizeFloatString( char* pFloat ) { // If we have a decimal point, remove trailing zeroes: if( strchr( pFloat,'.' ) ) { int len = V_strlen(pFloat); while( len > 1 && pFloat[len - 1] == '0' ) { pFloat[len - 1] = '\0'; len--; } if( len > 1 && pFloat[ len - 1 ] == '.' ) { pFloat[len - 1] = '\0'; len--; } } // TODO: Strip leading zeros }
//----------------------------------------------------------------------------- // Purpose: // Input : *in - // numchars - // *out - // maxoutputbytes - //----------------------------------------------------------------------------- void S_littleendianhextobinary( char const *in, int numchars, byte *out, int maxoutputbytes ) { int len = V_strlen( in ); numchars = MIN( len, numchars ); // Make sure it's even numchars = ( numchars ) & ~0x1; // Must be an even # of input characters (two chars per output byte) Assert( numchars >= 2 ); memset( out, 0x00, maxoutputbytes ); byte *p; int i; p = out; for ( i = numchars-2; ( i >= 0 ) && ( ( p - out ) < maxoutputbytes ); i-=2, p++ ) { *p = ( S_nibble( in[i] ) << 4 ) | S_nibble( in[i+1] ); } }
//----------------------------------------------------------------------------- // Purpose: // Input : *in - // numchars - // *out - // maxoutputbytes - //----------------------------------------------------------------------------- void V_hextobinary( char const *in, int numchars, byte *out, int maxoutputbytes ) { int len = V_strlen( in ); numchars = min( len, numchars ); // Make sure it's even numchars = ( numchars ) & ~0x1; // Must be an even # of input characters (two chars per output byte) Assert( numchars >= 2 ); memset( out, 0x00, maxoutputbytes ); byte *p; int i; p = out; for ( i = 0; ( i < numchars ) && ( ( p - out ) < maxoutputbytes ); i+=2, p++ ) { *p = ( V_nibble( in[i] ) << 4 ) | V_nibble( in[i+1] ); } }
//----------------------------------------------------------------------------- // Purpose: // Input : *path - // *dest - // destSize - // Output : void V_ExtractFilePath //----------------------------------------------------------------------------- bool V_ExtractFilePath (const char *path, char *dest, int destSize ) { Assert( destSize >= 1 ); if ( destSize < 1 ) { return false; } // Last char int len = V_strlen(path); const char *src = path + (len ? len-1 : 0); // back up until a \ or the start while ( src != path && !PATHSEPARATOR( *(src-1) ) ) { src--; } int copysize = min( src - path, destSize - 1 ); memcpy( dest, path, copysize ); dest[copysize] = 0; return copysize != 0 ? true : false; }