Skip to content

Commit e80ec08

Browse files
committed
Allow backslashes, extra slashes in shader/image names
Remove initial slashes or multiple consecutive slashes and convert \ to / when looking up shaders or images. Useful for Smokin' Guns assets.
1 parent cb39538 commit e80ec08

4 files changed

Lines changed: 36 additions & 7 deletions

File tree

src/common/FileSystem.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,27 @@ std::string Build(Str::StringRef base, Str::StringRef path)
477477
return out;
478478
}
479479

480+
std::string NormalizeSlashes(Str::StringRef path)
481+
{
482+
std::string out;
483+
out.reserve(path.size());
484+
bool lastSlash = true;
485+
486+
for (char c : path) {
487+
if (c == '/' || c == '\\') {
488+
if (!lastSlash) {
489+
lastSlash = true;
490+
out.push_back('/');
491+
}
492+
} else {
493+
out.push_back( c );
494+
lastSlash = false;
495+
}
496+
}
497+
498+
return out;
499+
}
500+
480501
std::string DirName(Str::StringRef path)
481502
{
482503
if (path.empty())

src/common/FileSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ namespace Path {
177177
// Build a path from components
178178
std::string Build(Str::StringRef base, Str::StringRef path);
179179

180+
// Replace \ with /
181+
// Remove multiple consecutive slashes
182+
// Remove initial slashes
183+
std::string NormalizeSlashes(Str::StringRef path);
184+
180185
// Get the directory portion of a path:
181186
// a/b/c => a/b
182187
// a/b/ => a

src/engine/renderer/tr_image.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,19 +1818,21 @@ Finds or loads the given image.
18181818
Returns nullptr if it fails, not a default image.
18191819
==============
18201820
*/
1821-
image_t *R_FindImageFile( const char *imageName, imageParams_t &imageParams )
1821+
image_t *R_FindImageFile( const char *imageName0, imageParams_t &imageParams )
18221822
{
1823-
if ( !imageName )
1823+
if ( !imageName0 )
18241824
{
18251825
return nullptr;
18261826
}
18271827

1828-
unsigned hash = GenerateImageHashValue( imageName );
1828+
std::string imageName = FS::Path::NormalizeSlashes( imageName0 );
1829+
1830+
unsigned hash = GenerateImageHashValue( imageName.c_str() );
18291831

18301832
// See if the image is already loaded.
18311833
for ( image_t *image = r_imageHashTable[ hash ]; image; image = image->next )
18321834
{
1833-
if ( !Q_strnicmp( imageName, image->name, sizeof( image->name ) ) )
1835+
if ( Str::IsIEqual( imageName, image->name ) )
18341836
{
18351837
if ( imageParams == image->initialParams || r_allowImageParamMismatch.Get() )
18361838
{
@@ -1889,7 +1891,7 @@ image_t *R_FindImageFile( const char *imageName, imageParams_t &imageParams )
18891891
byte *pic[ MAX_TEXTURE_MIPS * MAX_TEXTURE_LAYERS ];
18901892
pic[ 0 ] = nullptr;
18911893

1892-
R_LoadImage( imageName, pic, &width, &height, &numLayers, &numMips, &imageParams.bits );
1894+
R_LoadImage( imageName.c_str(), pic, &width, &height, &numLayers, &numMips, &imageParams.bits);
18931895

18941896
if ( *pic )
18951897
{
@@ -1909,7 +1911,7 @@ image_t *R_FindImageFile( const char *imageName, imageParams_t &imageParams )
19091911
R_ProcessLightmap( *pic, width, height, imageParams.bits );
19101912
}
19111913

1912-
image_t *image = R_CreateImage( imageName, (const byte **)pic, width, height, numMips, imageParams );
1914+
image_t *image = R_CreateImage( imageName.c_str(), (const byte**)pic, width, height, numMips, imageParams);
19131915
image->initialParams = initialParams;
19141916

19151917
Z_Free( *pic );

src/engine/renderer/tr_shader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6237,7 +6237,8 @@ shader_t *R_FindShader( const char *name, int flags )
62376237
return tr.defaultShader;
62386238
}
62396239

6240-
COM_StripExtension3( name, strippedName, sizeof( strippedName ) );
6240+
COM_StripExtension3( FS::Path::NormalizeSlashes( name ).c_str(),
6241+
strippedName, sizeof( strippedName ) );
62416242

62426243
hash = generateHashValue( strippedName, FILE_HASH_SIZE );
62436244

0 commit comments

Comments
 (0)