2023-05-06 21:56:27 +02:00
|
|
|
|
//===== Copyright <20> 2005-2005, Valve Corporation, All rights reserved. ======//
|
|
|
|
|
//
|
|
|
|
|
// Purpose: Helper methods + classes for file access.
|
|
|
|
|
//
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
#include "tier1/strtools.h"
|
|
|
|
|
#include "tier1/utlbuffer.h"
|
2023-05-10 00:05:38 +02:00
|
|
|
|
#include "tier2/fileutils.h"
|
|
|
|
|
#include "filesystem/filesystem.h"
|
2023-05-06 21:56:27 +02:00
|
|
|
|
|
|
|
|
|
// NOTE: This has to be the last file included!
|
|
|
|
|
#include "tier0/memdbgon.h"
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: Builds a directory which is a subdirectory of the current mod.
|
|
|
|
|
// Input : *pSubDir -
|
|
|
|
|
// *pBuf -
|
|
|
|
|
// nBufLen -
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void GetModSubdirectory(const char* pSubDir, char* pBuf, size_t nBufLen)
|
|
|
|
|
{
|
|
|
|
|
// Compute starting directory.
|
|
|
|
|
Assert(FileSystem()->GetSearchPath( "MOD", false, NULL, 0) < nBufLen );
|
|
|
|
|
FileSystem()->GetSearchPath( "MOD", false, pBuf, int( nBufLen ) );
|
|
|
|
|
char* pSemi = strchr( pBuf, ';' );
|
|
|
|
|
if ( pSemi )
|
|
|
|
|
{
|
|
|
|
|
*pSemi = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
V_StripTrailingSlash( pBuf );
|
|
|
|
|
size_t currentLength = strlen( pBuf );
|
|
|
|
|
|
|
|
|
|
if ( pSubDir )
|
|
|
|
|
{
|
|
|
|
|
Q_strncat( pBuf, "\\", nBufLen-currentLength );
|
|
|
|
|
currentLength += 1; // Account for the added '\\' character.
|
|
|
|
|
Q_strncat( pBuf, pSubDir, nBufLen-currentLength );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
V_FixSlashes( pBuf );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: Builds a directory which is a subdirectory of the current mod's *content*.
|
|
|
|
|
// Input : *pSubDir -
|
|
|
|
|
// *pBuf -
|
|
|
|
|
// nBufLen -
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void GetModContentSubdirectory( const char *pSubDir, char *pBuf, size_t nBufLen )
|
|
|
|
|
{
|
|
|
|
|
char pTemp[ MAX_PATH ];
|
|
|
|
|
GetModSubdirectory( pSubDir, pTemp, sizeof(pTemp) );
|
|
|
|
|
ComputeModContentFilename( pTemp, pBuf, nBufLen );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: Generates a filename under the 'game' subdirectory given a subdirectory of 'content'.
|
|
|
|
|
// Input : *pContentFileName -
|
|
|
|
|
// *pBuf -
|
|
|
|
|
// nBufLen -
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void ComputeModFilename( const char *pContentFileName, char *pBuf, size_t nBufLen )
|
|
|
|
|
{
|
|
|
|
|
char pRelativePath[ MAX_PATH ];
|
|
|
|
|
if ( !FileSystem()->FullPathToRelativePath(pContentFileName, pRelativePath, sizeof(pRelativePath)))
|
|
|
|
|
{
|
|
|
|
|
Q_strncpy( pBuf, pContentFileName, nBufLen );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char pGameRoot[ MAX_PATH ];
|
|
|
|
|
FileSystem()->GetSearchPath("GAME", false, pGameRoot, sizeof(pGameRoot));
|
|
|
|
|
char *pSemi = strchr( pGameRoot, ';' );
|
|
|
|
|
if ( pSemi )
|
|
|
|
|
{
|
|
|
|
|
*pSemi = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
V_ComposeFileName( pGameRoot, pRelativePath, pBuf, nBufLen );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: Generates a filename under the 'content' subdirectory given a subdirectory of 'game'.
|
|
|
|
|
// Input : *pGameFileName -
|
|
|
|
|
// *pBuf -
|
|
|
|
|
// nBufLen -
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void ComputeModContentFilename( const char *pGameFileName, char *pBuf, size_t nBufLen )
|
|
|
|
|
{
|
|
|
|
|
char pRelativePath[ MAX_PATH ];
|
|
|
|
|
if ( !FileSystem()->FullPathToRelativePath( pGameFileName, pRelativePath, sizeof(pRelativePath) ) )
|
|
|
|
|
{
|
|
|
|
|
Q_strncpy( pBuf, pGameFileName, nBufLen );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char pContentRoot[ MAX_PATH ];
|
|
|
|
|
FileSystem()->GetSearchPath( "CONTENT", false, pContentRoot, sizeof(pContentRoot) );
|
|
|
|
|
char *pSemi = strchr( pContentRoot, ';' );
|
|
|
|
|
if ( pSemi )
|
|
|
|
|
{
|
|
|
|
|
*pSemi = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
V_ComposeFileName( pContentRoot, pRelativePath, pBuf, nBufLen );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: Search start directory, recurse into sub directories collecting all files matching the target name.
|
2023-07-15 16:24:01 +02:00
|
|
|
|
// Input : &fileList -
|
2023-05-06 21:56:27 +02:00
|
|
|
|
// *szStartDirectory -
|
|
|
|
|
// *szTargetFileName -
|
|
|
|
|
// *pathID -
|
2023-07-15 16:24:01 +02:00
|
|
|
|
// separator -
|
2023-05-06 21:56:27 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-07-15 16:24:01 +02:00
|
|
|
|
void RecursiveFindFilesMatchingName( CUtlVector< CUtlString > &fileList, const char* szStartDirectory, const char* szTargetFileName, const char *pPathID, char separator )
|
2023-05-06 21:56:27 +02:00
|
|
|
|
{
|
|
|
|
|
char searchString[MAX_PATH];
|
|
|
|
|
Q_snprintf( searchString, sizeof( searchString ), "%s/*.*", szStartDirectory );
|
|
|
|
|
V_FixSlashes( searchString );
|
|
|
|
|
|
|
|
|
|
FileFindHandle_t handle;
|
|
|
|
|
const char* curFile = FileSystem()->FindFirstEx( searchString, pPathID, &handle );
|
|
|
|
|
while ( curFile )
|
|
|
|
|
{
|
|
|
|
|
if ( *curFile != '.' && FileSystem()->FindIsDirectory( handle ) )
|
|
|
|
|
{
|
|
|
|
|
char newSearchPath[MAX_PATH];
|
|
|
|
|
Q_snprintf( newSearchPath, sizeof( newSearchPath ), "%s/%s", szStartDirectory, curFile );
|
2023-07-15 16:24:01 +02:00
|
|
|
|
RecursiveFindFilesMatchingName( fileList, newSearchPath, szTargetFileName, pPathID, separator);
|
2023-05-06 21:56:27 +02:00
|
|
|
|
}
|
|
|
|
|
else if ( V_StringMatchesPattern( curFile, szTargetFileName ) )
|
|
|
|
|
{
|
|
|
|
|
CUtlString outFile;
|
|
|
|
|
outFile.Format( "%s/%s", szStartDirectory, curFile );
|
2023-07-09 20:05:47 +02:00
|
|
|
|
V_FixSlashes( outFile.Get(), separator );
|
2023-07-15 16:24:01 +02:00
|
|
|
|
fileList.AddToTail( outFile );
|
2023-05-06 21:56:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curFile = FileSystem()->FindNext( handle );
|
|
|
|
|
}
|
|
|
|
|
FileSystem()->FindClose( handle );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Builds a list of all files under a directory with a particular extension.
|
2023-07-15 16:24:01 +02:00
|
|
|
|
// Input : &fileList -
|
2023-05-06 21:56:27 +02:00
|
|
|
|
// *pDirectory -
|
|
|
|
|
// *pExtension -
|
2023-07-15 16:24:01 +02:00
|
|
|
|
// *pPathID -
|
|
|
|
|
// separator -
|
2023-05-06 21:56:27 +02:00
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-07-15 16:24:01 +02:00
|
|
|
|
void AddFilesToList( CUtlVector< CUtlString > &fileList, const char *pDirectory, const char *pExtension, const char* pPathID, char separator )
|
2023-05-06 21:56:27 +02:00
|
|
|
|
{
|
|
|
|
|
char pSearchString[MAX_PATH];
|
2023-07-15 16:24:01 +02:00
|
|
|
|
Q_snprintf( pSearchString, MAX_PATH, "%s/*", pDirectory );
|
2023-05-06 21:56:27 +02:00
|
|
|
|
|
|
|
|
|
bool bIsAbsolute = V_IsAbsolutePath( pDirectory );
|
|
|
|
|
|
|
|
|
|
// get the list of files.
|
|
|
|
|
FileFindHandle_t hFind;
|
|
|
|
|
const char *pFoundFile = FileSystem()->FindFirstEx( pSearchString, pPathID, &hFind );
|
|
|
|
|
|
|
|
|
|
// add all the items.
|
|
|
|
|
CUtlVector< CUtlString > subDirs;
|
|
|
|
|
for ( ; pFoundFile; pFoundFile = FileSystem()->FindNext( hFind ) )
|
|
|
|
|
{
|
|
|
|
|
char pChildPath[MAX_PATH];
|
2023-07-15 16:24:01 +02:00
|
|
|
|
Q_snprintf( pChildPath, MAX_PATH, "%s/%s", pDirectory, pFoundFile );
|
2023-05-06 21:56:27 +02:00
|
|
|
|
|
|
|
|
|
if ( FileSystem()->FindIsDirectory( hFind ) )
|
|
|
|
|
{
|
|
|
|
|
if ( Q_strnicmp( pFoundFile, ".", 2 ) && Q_strnicmp( pFoundFile, "..", 3 ) )
|
|
|
|
|
{
|
|
|
|
|
subDirs.AddToTail( pChildPath );
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check the extension matches.
|
2023-05-08 16:12:37 +01:00
|
|
|
|
if ( pExtension && Q_stricmp( V_GetFileExtension( pFoundFile ), pExtension ) )
|
2023-05-06 21:56:27 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
char pFullPathBuf[MAX_PATH];
|
|
|
|
|
char *pFullPath = pFullPathBuf;
|
|
|
|
|
if ( !bIsAbsolute )
|
|
|
|
|
{
|
|
|
|
|
FileSystem()->RelativePathToFullPath( pChildPath, pPathID, pFullPathBuf, sizeof(pFullPathBuf) );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pFullPath = pChildPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
V_strlower( pFullPath );
|
2023-07-15 16:24:01 +02:00
|
|
|
|
V_FixSlashes( pFullPath, separator );
|
|
|
|
|
fileList.AddToTail( pFullPath );
|
2023-05-06 21:56:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileSystem()->FindClose(hFind);
|
|
|
|
|
|
|
|
|
|
int nCount = subDirs.Count();
|
|
|
|
|
for ( int i = 0; i < nCount; ++i )
|
|
|
|
|
{
|
2023-07-15 16:24:01 +02:00
|
|
|
|
AddFilesToList( fileList, subDirs[i], pExtension, pPathID, separator );
|
2023-05-06 21:56:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: Returns the search path as a list of paths.
|
2023-07-15 16:24:01 +02:00
|
|
|
|
// Input : &pathList -
|
2023-05-06 21:56:27 +02:00
|
|
|
|
// *pPathID -
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-07-15 16:24:01 +02:00
|
|
|
|
void GetSearchPath(CUtlVector< CUtlString >& pathList, const char* pPathID)
|
2023-05-06 21:56:27 +02:00
|
|
|
|
{
|
|
|
|
|
int nMaxLen = FileSystem()->GetSearchPath(pPathID, false, NULL, 0);
|
|
|
|
|
char* pBuf = (char*)stackalloc(nMaxLen);
|
|
|
|
|
FileSystem()->GetSearchPath(pPathID, false, pBuf, nMaxLen);
|
|
|
|
|
|
|
|
|
|
char* pSemi;
|
|
|
|
|
while (NULL != (pSemi = strchr(pBuf, ';')))
|
|
|
|
|
{
|
|
|
|
|
*pSemi = 0;
|
2023-07-15 16:24:01 +02:00
|
|
|
|
pathList.AddToTail(pBuf);
|
2023-05-06 21:56:27 +02:00
|
|
|
|
pBuf = pSemi + 1;
|
|
|
|
|
}
|
2023-07-15 16:24:01 +02:00
|
|
|
|
pathList.AddToTail(pBuf);
|
2023-05-06 21:56:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: Given file name in the current dir generate a full path to it.
|
|
|
|
|
// Input : *pFileName -
|
|
|
|
|
// *pPathID -
|
|
|
|
|
// *pBuf -
|
|
|
|
|
// nBufLen -
|
|
|
|
|
// Output: True on success, false otherwise.
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
bool GenerateFullPath(const char* pFileName, char const* pPathID, char* pBuf, size_t nBufLen)
|
|
|
|
|
{
|
|
|
|
|
if (V_IsAbsolutePath(pFileName))
|
|
|
|
|
{
|
|
|
|
|
V_strncpy(pBuf, pFileName, nBufLen);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* pFullPath = FileSystem()->RelativePathToFullPath(pFileName, pPathID, pBuf, int(nBufLen));
|
|
|
|
|
if (pFullPath && V_IsAbsolutePath(pFullPath))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
char pDir[MAX_PATH];
|
|
|
|
|
if (!FileSystem()->GetCurrentDirectory(pDir, sizeof(pDir)))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
V_ComposeFileName(pDir, pFileName, pBuf, nBufLen);
|
|
|
|
|
V_RemoveDotSlashes(pBuf);
|
|
|
|
|
return true;
|
|
|
|
|
}
|