|
5 | 5 | #include <vgui_controls/Controls.h> |
6 | 6 | #include <vgui/ISurface.h> |
7 | 7 | #include "vpklib/packedstore.h" |
| 8 | +#include "fmtstr.h" |
8 | 9 |
|
9 | 10 | // memdbgon must be the last include file in a .cpp file!!! |
10 | 11 | #include "tier0/memdbgon.h" |
@@ -84,59 +85,83 @@ void CRD_PNG_Texture::SetRotation( int iRotation ) |
84 | 85 | { |
85 | 86 | } |
86 | 87 |
|
87 | | -void CRD_PNG_Texture::CleanLocalCachedTextures( const char *szDirectory ) |
| 88 | +void CRD_PNG_Texture::CleanLocalCachedTextures(const char* szDirectory) |
88 | 89 | { |
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")) |
99 | 91 | return; |
100 | 92 |
|
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; |
103 | 95 |
|
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) |
116 | 97 | { |
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 |
121 | 109 | { |
122 | | - Msg( "Removing local cached file %s\n", szVerifyPath ); |
123 | | - g_pFullFileSystem->RemoveFile( szVerifyPath, "MOD" ); |
| 110 | + return; |
124 | 111 | } |
125 | 112 | } |
126 | | - g_pFullFileSystem->FindClose( hFind ); |
127 | 113 |
|
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 | + }; |
132 | 126 |
|
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) |
134 | 135 | { |
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); |
137 | 163 | } |
138 | 164 | } |
139 | | - g_pFullFileSystem->FindClose( hFind ); |
140 | 165 | } |
141 | 166 |
|
142 | 167 | bool CRD_PNG_Texture::Init( const char *szDirectory, uint32_t iHash, bool bForceLoadRemote ) |
|
0 commit comments