private static string ResolveIncludes( FileInfo _ShaderPath, string _ShaderSource )
{
// Remove double characters
_ShaderSource = _ShaderSource.Replace( "\r\n", "\n" );
int IncludeIndex = _ShaderSource.IndexOf( "#include", StringComparison.InvariantCultureIgnoreCase );
if ( IncludeIndex == -1 )
return _ShaderSource; // We're done!
int FileNameSearchStartIndex = IncludeIndex + "#include".Length;
int EOLIndex = FindEOL( _ShaderSource, FileNameSearchStartIndex );
if ( EOLIndex == -1 )
throw new ConverterException( "Failed to find end of line while attempting to replace #include", _ShaderSource, IncludeIndex );
// Retrieve start of either "Filename" or <Filename>
int IncludeFileNameStart = _ShaderSource.IndexOf( '"', FileNameSearchStartIndex, EOLIndex-FileNameSearchStartIndex );
if ( IncludeFileNameStart == -1 )
{
IncludeFileNameStart =_ShaderSource.IndexOf( '<', FileNameSearchStartIndex, EOLIndex-FileNameSearchStartIndex );
if ( IncludeFileNameStart == -1 )
throw new ConverterException( "Failed to find the start of the include path (looked for \" and <) while attempting to replace #include", _ShaderSource, IncludeIndex, EOLIndex );
}
// Retrieve end of either "Filename" or <Filename>
int IncludeFileNameEnd = _ShaderSource.IndexOf( '"', IncludeFileNameStart+1, EOLIndex-IncludeFileNameStart-1 );
if ( IncludeFileNameEnd == -1 )
{
IncludeFileNameEnd =_ShaderSource.IndexOf( '>', IncludeFileNameStart+1, EOLIndex-IncludeFileNameStart-1 );
if ( IncludeFileNameEnd == -1 )
throw new ConverterException( "Failed to find the end of the include path (looked for \" and >) while attempting to replace #include", _ShaderSource, IncludeIndex, EOLIndex );
}
// Isolate filename & resolve file
string IncludeFileName = _ShaderSource.Substring( IncludeFileNameStart+1, IncludeFileNameEnd-IncludeFileNameStart-1 );
string IncludeFullPath = Path.Combine( _ShaderPath.Directory.FullName, IncludeFileName );
FileInfo IncludeFile = new FileInfo( IncludeFullPath );
if ( !IncludeFile.Exists )
throw new ConverterException( "Failed to find include file \"" + IncludeFile.FullName + "\" while attempting to replace #include", _ShaderSource, IncludeIndex, EOLIndex );
// Replace
string IncludedSource;
using ( StreamReader S = IncludeFile.OpenText() )
IncludedSource = S.ReadToEnd();
string SourceStart = _ShaderSource.Substring( 0, IncludeIndex );
string SourceEnd = _ShaderSource.Substring( EOLIndex+1, _ShaderSource.Length-EOLIndex-1 );
_ShaderSource = SourceStart
+ "\r\n #region ======================= START INCLUDE " + IncludeFileName + " =======================\r\n"
+ IncludedSource
+ " #endregion // ======================= END INCLUDE " + IncludeFileName + " =======================\r\n\r\n"
+ SourceEnd;
// Recurse
return ResolveIncludes( _ShaderPath, _ShaderSource );
}