Skip to content

Commit 23c289c

Browse files
authored
Fixes: CRD_PNG_Texture::CleanLocalCachedTextures
1 parent fae989c commit 23c289c

1 file changed

Lines changed: 65 additions & 40 deletions

File tree

src/game/client/swarm/rd_png_texture.cpp

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <vgui_controls/Controls.h>
66
#include <vgui/ISurface.h>
77
#include "vpklib/packedstore.h"
8+
#include "fmtstr.h"
89

910
// memdbgon must be the last include file in a .cpp file!!!
1011
#include "tier0/memdbgon.h"
@@ -84,59 +85,83 @@ void CRD_PNG_Texture::SetRotation( int iRotation )
8485
{
8586
}
8687

87-
void CRD_PNG_Texture::CleanLocalCachedTextures( const char *szDirectory )
88+
void CRD_PNG_Texture::CleanLocalCachedTextures(const char* szDirectory)
8889
{
89-
// Delete textures that are in the local cache folder but also in pak01_vpk.dir.
90-
//
91-
// This can happen if an item is added to the schema and seen in-game before the
92-
// update that adds the pre-cached texture to the game's files is released.
93-
//
94-
// We don't check mods (so replacing an item icon in an addon doesn't affect this)
95-
// (The "MOD" path below refers to a Source Engine game's raw files, not addons.)
96-
97-
// Don't do this if we are loading the VPK with lower priority than loose files.
98-
if ( CommandLine()->FindParm( "-override_vpk" ) )
90+
if (CommandLine()->FindParm("-override_vpk"))
9991
return;
10092

101-
static CPackedStore pak01{ "pak01", g_pFullFileSystem };
102-
static CPackedStore pak02{ "pak02", g_pFullFileSystem };
93+
static char szGameRootPath[MAX_PATH];
94+
static bool bPathInitialized = false;
10395

104-
// Add the materials/ prefix here so we match the directory naming scheme in Init.
105-
char szFullDirectory[MAX_PATH];
106-
V_snprintf( szFullDirectory, sizeof( szFullDirectory ), "materials/%s", szDirectory );
107-
108-
// Do a separate pass for materials and for textures just in case the user already
109-
// deleted one or something went wrong during generation.
110-
char szMaterialWildcard[MAX_PATH], szTextureWildcard[MAX_PATH];
111-
V_snprintf( szMaterialWildcard, sizeof( szMaterialWildcard ), "%s/*.vmt", szFullDirectory );
112-
V_snprintf( szTextureWildcard, sizeof( szTextureWildcard ), "%s/*.vtf", szFullDirectory );
113-
114-
FileFindHandle_t hFind = FILESYSTEM_INVALID_FIND_HANDLE;
115-
for ( const char *szName = g_pFullFileSystem->FindFirstEx( szMaterialWildcard, "MOD", &hFind ); szName; szName = g_pFullFileSystem->FindNext( hFind ) )
96+
if (!bPathInitialized)
11697
{
117-
char szVerifyPath[MAX_PATH];
118-
V_snprintf( szVerifyPath, sizeof( szVerifyPath ), "%s/%s", szFullDirectory, szName );
119-
120-
if ( pak01.OpenFile( szVerifyPath ) )
98+
const char* szGameInfoPath = "gameinfo.txt";
99+
if (g_pFullFileSystem->FileExists(szGameInfoPath, "MOD"))
100+
{
101+
char szFullPath[MAX_PATH];
102+
g_pFullFileSystem->RelativePathToFullPath(szGameInfoPath, "MOD", szFullPath, sizeof(szFullPath));
103+
V_ExtractFilePath(szFullPath, szGameRootPath, sizeof(szGameRootPath));
104+
V_FixSlashes(szGameRootPath);
105+
V_AppendSlash(szGameRootPath, sizeof(szGameRootPath));
106+
bPathInitialized = true;
107+
}
108+
else
121109
{
122-
Msg( "Removing local cached file %s\n", szVerifyPath );
123-
g_pFullFileSystem->RemoveFile( szVerifyPath, "MOD" );
110+
return;
124111
}
125112
}
126-
g_pFullFileSystem->FindClose( hFind );
127113

128-
for ( const char *szName = g_pFullFileSystem->FindFirstEx( szTextureWildcard, "MOD", &hFind ); szName; szName = g_pFullFileSystem->FindNext( hFind ) )
129-
{
130-
char szVerifyPath[MAX_PATH];
131-
V_snprintf( szVerifyPath, sizeof( szVerifyPath ), "%s/%s", szFullDirectory, szName );
114+
static CPackedStore s_pak01(CFmtStr("%spak01", szGameRootPath), g_pFullFileSystem);
115+
static CPackedStore s_pak02(CFmtStr("%spak02", szGameRootPath), g_pFullFileSystem);
116+
117+
struct CleanupRule {
118+
std::initializer_list<const char*> extensions;
119+
std::initializer_list<CPackedStore*> targetPaks;
120+
};
121+
122+
const std::initializer_list<CleanupRule> cleanupRules = {
123+
{ {".vtf"}, {&s_pak02} },
124+
{ {".vmt"}, {&s_pak01} },
125+
};
132126

133-
if ( pak02.OpenFile( szVerifyPath ) )
127+
char szBasePath[MAX_PATH];
128+
V_ComposeFileName("materials", szDirectory, szBasePath, sizeof(szBasePath));
129+
V_FixSlashes(szBasePath);
130+
V_AppendSlash(szBasePath, sizeof(szBasePath));
131+
132+
for (const auto& rule : cleanupRules)
133+
{
134+
for (const char* szExtension : rule.extensions)
134135
{
135-
Msg( "Removing local cached file %s\n", szVerifyPath );
136-
g_pFullFileSystem->RemoveFile( szVerifyPath, "MOD" );
136+
char szWildcard[MAX_PATH];
137+
V_ComposeFileName(szBasePath, CFmtStr("*%s", szExtension), szWildcard, sizeof(szWildcard));
138+
139+
FileFindHandle_t hFind;
140+
for (const char* szFilename = g_pFullFileSystem->FindFirstEx(szWildcard, "MOD", &hFind);
141+
szFilename;
142+
szFilename = g_pFullFileSystem->FindNext(hFind))
143+
{
144+
char szVerifyPath[MAX_PATH];
145+
V_ComposeFileName(szBasePath, szFilename, szVerifyPath, sizeof(szVerifyPath));
146+
147+
for (CPackedStore* pPak : rule.targetPaks)
148+
{
149+
if (pPak->OpenFile(szVerifyPath))
150+
{
151+
char szFullPath[MAX_PATH];
152+
if (g_pFullFileSystem->RelativePathToFullPath(szVerifyPath, "MOD", szFullPath, sizeof(szFullPath)) &&
153+
g_pFullFileSystem->FileExists(szFullPath))
154+
{
155+
Msg("[Cleanup] Removing: %s\n", szFullPath);
156+
g_pFullFileSystem->RemoveFile(szFullPath);
157+
break;
158+
}
159+
}
160+
}
161+
}
162+
g_pFullFileSystem->FindClose(hFind);
137163
}
138164
}
139-
g_pFullFileSystem->FindClose( hFind );
140165
}
141166

142167
bool CRD_PNG_Texture::Init( const char *szDirectory, uint32_t iHash, bool bForceLoadRemote )

0 commit comments

Comments
 (0)