Light code cleanup

- Use game's filesystem in 'Mod_GetAllInstalledMaps'.
- Reorder parameters of 'AddFilesToList', and add option to override path separator.
This commit is contained in:
Kawe Mazidjatari 2023-07-15 16:24:01 +02:00
parent a692d122dd
commit 83d7f0b9f4
5 changed files with 39 additions and 32 deletions

View File

@ -8,6 +8,7 @@
#include "core/stdafx.h"
#include "tier0/memstd.h"
#include "tier0/jobthread.h"
#include "tier2/fileutils.h"
#include "engine/sys_dll2.h"
#include "engine/host_cmd.h"
#include "engine/cmodel_bsp.h"
@ -21,11 +22,15 @@
#endif // !DEDICATED
vector<string> g_InstalledMaps;
string s_svLevelName;
string s_LevelName;
std::regex s_ArchiveRegex{ R"([^_]*_(.*)(.bsp.pak000_dir).*)" };
bool s_bLevelResourceInitialized = false;
bool s_bBasePaksInitialized = false;
KeyValues* s_pLevelSetKV = nullptr;
//-----------------------------------------------------------------------------
// Purpose: checks if level has changed
// Input : *pszLevelName -
@ -33,7 +38,7 @@ KeyValues* s_pLevelSetKV = nullptr;
//-----------------------------------------------------------------------------
bool Mod_LevelHasChanged(const char* pszLevelName)
{
return (s_svLevelName.compare(pszLevelName) != 0);
return (s_LevelName.compare(pszLevelName) != 0);
}
//-----------------------------------------------------------------------------
@ -41,17 +46,17 @@ bool Mod_LevelHasChanged(const char* pszLevelName)
//-----------------------------------------------------------------------------
void Mod_GetAllInstalledMaps()
{
CUtlVector<CUtlString> fileList;
AddFilesToList(fileList, "vpk", "vpk", nullptr, '/');
std::lock_guard<std::mutex> l(g_InstalledMapsMutex);
g_InstalledMaps.clear(); // Clear current list.
fs::directory_iterator directoryIterator("vpk");
std::regex archiveRegex{ R"([^_]*_(.*)(.bsp.pak000_dir).*)" };
std::smatch regexMatches;
for (const fs::directory_entry& directoryEntry : directoryIterator)
std::cmatch regexMatches;
FOR_EACH_VEC(fileList, i)
{
std::string fileName = directoryEntry.path().u8string();
std::regex_search(fileName, regexMatches, archiveRegex);
const CUtlString& fileName = fileList[i];
std::regex_search(fileName.Get(), regexMatches, s_ArchiveRegex);
if (!regexMatches.empty())
{
@ -347,7 +352,7 @@ void Mod_ProcessPakQueue()
if (s_bBasePaksInitialized && !s_bLevelResourceInitialized)
{
Mod_PreloadLevelPaks(s_svLevelName.c_str());
Mod_PreloadLevelPaks(s_LevelName.c_str());
s_bLevelResourceInitialized = true;
}
*(_DWORD*)v15 = g_pakLoadApi->LoadAsync(v17, AlignedMemAlloc(), 4, 0);
@ -387,7 +392,7 @@ void Mod_LoadPakForMap(const char* pszLevelName)
if (Mod_LevelHasChanged(pszLevelName))
s_bLevelResourceInitialized = false;
s_svLevelName = pszLevelName;
s_LevelName = pszLevelName;
// Dedicated should not load loadscreens.
#ifndef DEDICATED

View File

@ -46,7 +46,7 @@ void CModSystem::Init()
modsystem_debug->SetValue(true);
CUtlVector<CUtlString> modFileList;
RecursiveFindFilesMatchingName(&modFileList,
RecursiveFindFilesMatchingName(modFileList,
MOD_BASE_DIRECTORY, MOD_SETTINGS_FILE, "PLATFORM", '/');
FOR_EACH_VEC(modFileList, i)

View File

@ -20,7 +20,7 @@ void CPluginSystem::PluginSystem_Init()
FileSystem()->CreateDirHierarchy("bin\\x64_retail\\plugins");
CUtlVector< CUtlString > pluginPaths;
AddFilesToList(pluginPaths, "bin\\x64_retail\\plugins", NULL, "dll");
AddFilesToList(pluginPaths, "bin\\x64_retail\\plugins", "dll");
for (int i = 0; i < pluginPaths.Count(); ++i)
{

View File

@ -24,13 +24,13 @@ void ComputeModFilename( const char *pContentFileName, char *pBuf, size_t nBufLe
void ComputeModContentFilename( const char *pGameFileName, char *pBuf, size_t nBufLen );
// Finds all files matching the a name within a directory and its sub directories. Output entries are paths to found files (relative to and including szStartDirectory).
void RecursiveFindFilesMatchingName( CUtlVector< CUtlString > *pOutFileList, const char* szStartDirectory, const char* szTargetFileName, const char *pPathID, char separator = CORRECT_PATH_SEPARATOR);
void RecursiveFindFilesMatchingName( CUtlVector< CUtlString > &fileList, const char* szStartDirectory, const char* szTargetFileName, const char *pPathID, char separator = CORRECT_PATH_SEPARATOR);
// Builds a list of all files under a directory with a particular extension.
void AddFilesToList( CUtlVector< CUtlString > &list, const char *pDirectory, const char *pPath = nullptr, const char *pExtension = nullptr );
void AddFilesToList( CUtlVector< CUtlString > &fileList, const char *pDirectory, const char *pExtension = nullptr, const char* pPathID = nullptr, char separator = CORRECT_PATH_SEPARATOR );
// Returns the search path as a list of paths.
void GetSearchPath( CUtlVector< CUtlString > &path, const char *pPathID );
void GetSearchPath( CUtlVector< CUtlString > &pathList, const char *pPathID );
// Given file name generate a full path using the following rules.
// 1. if its full path already return.

View File

@ -108,12 +108,13 @@ void ComputeModContentFilename( const char *pGameFileName, char *pBuf, size_t nB
//-----------------------------------------------------------------------------
// Purpose: Search start directory, recurse into sub directories collecting all files matching the target name.
// Input : *outFileList -
// Input : &fileList -
// *szStartDirectory -
// *szTargetFileName -
// *pathID -
// separator -
//-----------------------------------------------------------------------------
void RecursiveFindFilesMatchingName( CUtlVector< CUtlString > *pOutFileList, const char* szStartDirectory, const char* szTargetFileName, const char *pPathID, char separator )
void RecursiveFindFilesMatchingName( CUtlVector< CUtlString > &fileList, const char* szStartDirectory, const char* szTargetFileName, const char *pPathID, char separator )
{
char searchString[MAX_PATH];
Q_snprintf( searchString, sizeof( searchString ), "%s/*.*", szStartDirectory );
@ -127,14 +128,14 @@ void RecursiveFindFilesMatchingName( CUtlVector< CUtlString > *pOutFileList, con
{
char newSearchPath[MAX_PATH];
Q_snprintf( newSearchPath, sizeof( newSearchPath ), "%s/%s", szStartDirectory, curFile );
RecursiveFindFilesMatchingName( pOutFileList, newSearchPath, szTargetFileName, pPathID, separator);
RecursiveFindFilesMatchingName( fileList, newSearchPath, szTargetFileName, pPathID, separator);
}
else if ( V_StringMatchesPattern( curFile, szTargetFileName ) )
{
CUtlString outFile;
outFile.Format( "%s/%s", szStartDirectory, curFile );
V_FixSlashes( outFile.Get(), separator );
pOutFileList->AddToTail( outFile );
fileList.AddToTail( outFile );
}
curFile = FileSystem()->FindNext( handle );
@ -144,15 +145,16 @@ void RecursiveFindFilesMatchingName( CUtlVector< CUtlString > *pOutFileList, con
//-----------------------------------------------------------------------------
// Builds a list of all files under a directory with a particular extension.
// Input : &list -
// Input : &fileList -
// *pDirectory -
// *pPathID -
// *pExtension -
// *pPathID -
// separator -
//-----------------------------------------------------------------------------
void AddFilesToList( CUtlVector< CUtlString > &list, const char *pDirectory, const char *pPathID, const char *pExtension )
void AddFilesToList( CUtlVector< CUtlString > &fileList, const char *pDirectory, const char *pExtension, const char* pPathID, char separator )
{
char pSearchString[MAX_PATH];
Q_snprintf( pSearchString, MAX_PATH, "%s\\*", pDirectory );
Q_snprintf( pSearchString, MAX_PATH, "%s/*", pDirectory );
bool bIsAbsolute = V_IsAbsolutePath( pDirectory );
@ -165,7 +167,7 @@ void AddFilesToList( CUtlVector< CUtlString > &list, const char *pDirectory, con
for ( ; pFoundFile; pFoundFile = FileSystem()->FindNext( hFind ) )
{
char pChildPath[MAX_PATH];
Q_snprintf( pChildPath, MAX_PATH, "%s\\%s", pDirectory, pFoundFile );
Q_snprintf( pChildPath, MAX_PATH, "%s/%s", pDirectory, pFoundFile );
if ( FileSystem()->FindIsDirectory( hFind ) )
{
@ -192,8 +194,8 @@ void AddFilesToList( CUtlVector< CUtlString > &list, const char *pDirectory, con
}
V_strlower( pFullPath );
V_FixSlashes( pFullPath );
list.AddToTail( pFullPath );
V_FixSlashes( pFullPath, separator );
fileList.AddToTail( pFullPath );
}
FileSystem()->FindClose(hFind);
@ -201,16 +203,16 @@ void AddFilesToList( CUtlVector< CUtlString > &list, const char *pDirectory, con
int nCount = subDirs.Count();
for ( int i = 0; i < nCount; ++i )
{
AddFilesToList( list, subDirs[i], pPathID, pExtension );
AddFilesToList( fileList, subDirs[i], pExtension, pPathID, separator );
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns the search path as a list of paths.
// Input : &path -
// Input : &pathList -
// *pPathID -
//-----------------------------------------------------------------------------
void GetSearchPath(CUtlVector< CUtlString >& path, const char* pPathID)
void GetSearchPath(CUtlVector< CUtlString >& pathList, const char* pPathID)
{
int nMaxLen = FileSystem()->GetSearchPath(pPathID, false, NULL, 0);
char* pBuf = (char*)stackalloc(nMaxLen);
@ -220,10 +222,10 @@ void GetSearchPath(CUtlVector< CUtlString >& path, const char* pPathID)
while (NULL != (pSemi = strchr(pBuf, ';')))
{
*pSemi = 0;
path.AddToTail(pBuf);
pathList.AddToTail(pBuf);
pBuf = pSemi + 1;
}
path.AddToTail(pBuf);
pathList.AddToTail(pBuf);
}
//-----------------------------------------------------------------------------