Skip to content

Commit e63d977

Browse files
committed
Add filename escaping
1 parent 523d6f0 commit e63d977

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

include/nbl/asset/utils/IShaderCompiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
257257
}
258258
}
259259

260+
static std::string escapeFilename(std::string&& code);
261+
260262
static void disableAllDirectivesExceptIncludes(std::string& _code);
261263

262264
static void reenableDirectives(std::string& _code);

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,9 @@ static tcpp::IInputStream* getInputStreamInclude(
102102
IShaderCompiler::disableAllDirectivesExceptIncludes(res_str);
103103
res_str = IShaderCompiler::encloseWithinExtraInclGuards(std::move(res_str), maxInclCnt, name.string().c_str());
104104
res_str = res_str + "\n" +
105-
IShaderCompiler::PREPROC_DIRECTIVE_DISABLER + "line " + std::to_string(lineGoBackTo) + " \"" + includeStack.back().second + "\"\n";
105+
IShaderCompiler::PREPROC_DIRECTIVE_DISABLER + "line " + std::to_string(lineGoBackTo).c_str() + " \"" + includeStack.back().second.c_str() + "\"\n";
106106

107-
// avoid warnings about improperly escaping
108-
std::string identifier = name.string().c_str();
109-
std::replace(identifier.begin(), identifier.end(), '\\', '/');
110-
111-
includeStack.push_back(std::pair<uint32_t, std::string>(lineGoBackTo, identifier));
107+
includeStack.push_back(std::pair<uint32_t, std::string>(lineGoBackTo, IShaderCompiler::escapeFilename(name.string())));
112108

113109
return new tcpp::StringInputStream(std::move(res_str));
114110
}

src/nbl/asset/utils/IShaderCompiler.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
using namespace nbl;
1313
using namespace nbl::asset;
1414

15+
std::string IShaderCompiler::escapeFilename(std::string&& code)
16+
{
17+
std::string dest;
18+
for (char c : code)
19+
{
20+
if (c == '\\')
21+
dest.append("\\" "\\");
22+
else
23+
dest.push_back(c);
24+
}
25+
return dest;
26+
}
27+
1528
//all "#", except those in "#include"/"#version"/"#pragma shader_stage(...)", replaced with `PREPROC_DIRECTIVE_DISABLER`
1629
void IShaderCompiler::disableAllDirectivesExceptIncludes(std::string& _code)
1730
{
@@ -56,10 +69,7 @@ std::string IShaderCompiler::encloseWithinExtraInclGuards(std::string&& _code, u
5669
return undefs;
5770
};
5871

59-
// avoid warnings about improperly escaping
60-
std::string identifier = _identifier;
61-
std::replace(identifier.begin(), identifier.end(), '\\', '/');
62-
72+
std::string identifier = IShaderCompiler::escapeFilename(_identifier);
6373
return
6474
genDefs() +
6575
"\n"

0 commit comments

Comments
 (0)