Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve local includes #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ TSharedRef<FVirtualDestructor> FHLSLMaterialFunctionLibraryEditor::CreateWatcher
FString Text;
if (TryLoadFileToString(Text, Library.GetFilePath()))
{
for (const FHLSLMaterialParser::FInclude& Include : FHLSLMaterialParser::GetIncludes(Text))
const FString ShaderPath = FPaths::GetPath(Library.GetFilePath());
for (const FHLSLMaterialParser::FInclude& Include : FHLSLMaterialParser::GetIncludes(ShaderPath, Text))
{
if (!Include.DiskPath.IsEmpty())
{
Expand Down Expand Up @@ -99,7 +100,7 @@ void FHLSLMaterialFunctionLibraryEditor::Generate(UHLSLMaterialFunctionLibrary&

FString BaseHash;
TArray<FString> IncludeFilePaths;
for (const FHLSLMaterialParser::FInclude& Include : FHLSLMaterialParser::GetIncludes(Text))
for (const FHLSLMaterialParser::FInclude& Include : FHLSLMaterialParser::GetIncludes(FPaths::GetPath(FullPath), Text))
{
IncludeFilePaths.Add(Include.VirtualPath);

Expand Down
20 changes: 18 additions & 2 deletions Source/HLSLMaterialEditor/Private/HLSLMaterialParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "HLSLMaterialFunction.h"
#include "HLSLMaterialFunctionLibrary.h"
#include "HLSLMaterialMessages.h"
#include "HAL/FileManagerGeneric.h"
#include "Internationalization/Regex.h"

FString FHLSLMaterialParser::Parse(
Expand Down Expand Up @@ -256,17 +257,32 @@ FString FHLSLMaterialParser::Parse(
return {};
}

TArray<FHLSLMaterialParser::FInclude> FHLSLMaterialParser::GetIncludes(const FString& Text)
TArray<FHLSLMaterialParser::FInclude> FHLSLMaterialParser::GetIncludes(const FString& ShaderPath, const FString& Text)
{
TArray<FInclude> OutIncludes;

FRegexPattern RegexPattern(R"_((\A|\v)\s*#include "([^"]+)")_");
FRegexMatcher RegexMatcher(RegexPattern, Text);
while (RegexMatcher.FindNext())
{
const FString VirtualPath = RegexMatcher.GetCaptureGroup(2);
FString VirtualPath = RegexMatcher.GetCaptureGroup(2);

FString DiskPath = GetShaderSourceFilePath(VirtualPath);

if (DiskPath.IsEmpty())
{
// Try going other way around.
// Check if path used as VirtualPath is really local path relative to shader and if it is does it map to virtual path
const FString VirtualToDiskFilePath = FPaths::ConvertRelativePathToFull(ShaderPath, VirtualPath);
const FString VirtualToDiskPath = FFileManagerGeneric::DefaultConvertToRelativePath(*FPaths::GetPath(VirtualToDiskFilePath));
const FString* NewVirtualPath = AllShaderSourceDirectoryMappings().FindKey(VirtualToDiskPath);
if (NewVirtualPath)
{
DiskPath = VirtualToDiskFilePath;
VirtualPath = *NewVirtualPath / FPaths::GetCleanFilename(VirtualToDiskFilePath);
}
}

if (DiskPath.IsEmpty())
{
FHLSLMaterialMessages::ShowError(TEXT("Failed to map include %s"), *VirtualPath);
Expand Down
2 changes: 1 addition & 1 deletion Source/HLSLMaterialEditor/Private/HLSLMaterialParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class FHLSLMaterialParser
FString VirtualPath;
FString DiskPath;
};
static TArray<FInclude> GetIncludes(const FString& Text);
static TArray<FInclude> GetIncludes(const FString& ShaderPath, const FString& Text);
static TArray<FCustomDefine> GetDefines(const FString& Text);
};