Fix auto-importing materials and textures from model file with invalid path characters used in name

Fixes #208
This commit is contained in:
Wojtek Figat
2021-02-07 12:49:14 +01:00
parent 9ddb077e7c
commit 6e5a13111a
4 changed files with 73 additions and 2 deletions

View File

@@ -741,6 +741,59 @@ bool EditorUtilities::GenerateCertificate(const String& name, const String& outp
return false;
}
bool EditorUtilities::IsInvalidPathChar(Char c)
{
char illegalChars[] =
{
'?',
'\\',
'/',
'\"',
'<',
'>',
'|',
':',
'*',
'\u0001',
'\u0002',
'\u0003',
'\u0004',
'\u0005',
'\u0006',
'\a',
'\b',
'\t',
'\n',
'\v',
'\f',
'\r',
'\u000E',
'\u000F',
'\u0010',
'\u0011',
'\u0012',
'\u0013',
'\u0014',
'\u0015',
'\u0016',
'\u0017',
'\u0018',
'\u0019',
'\u001A',
'\u001B',
'\u001C',
'\u001D',
'\u001E',
'\u001F'
};
for (auto i : illegalChars)
{
if (c == i)
return true;
}
return false;
}
bool EditorUtilities::ReplaceInFiles(const String& folderPath, const Char* searchPattern, DirectorySearchOption searchOption, const String& findWhat, const String& replaceWith)
{
Array<String> files;

View File

@@ -42,6 +42,13 @@ public:
public:
/// <summary>
/// Determines whether the specified path character is invalid.
/// </summary>
/// <param name="c">The path character.</param>
/// <returns><c>true</c> if the given character cannot be used as a path because it is illegal character; otherwise, <c>false</c>.</returns>
static bool IsInvalidPathChar(Char c);
/// <summary>
/// Replaces the given text with other one in the files.
/// </summary>

View File

@@ -178,7 +178,7 @@ void CreateAssetContext::ApplyChanges()
// Move file
if (FileSystem::MoveFile(TargetAssetPath, OutputPath, true))
{
LOG(Warning, "Cannot move imported file to the destination path.");
LOG(Warning, "Cannot move imported file {0} to the destination path {1}.", OutputPath, TargetAssetPath);
_applyChangesResult = CreateAssetResult::CannotSaveFile;
return;
}

View File

@@ -15,7 +15,8 @@
#include "Engine/Tools/TextureTool/TextureTool.h"
#include "Engine/ContentImporters/AssetsImportingManager.h"
#include "Engine/ContentImporters/CreateMaterial.h"
#include "ThirdParty/meshoptimizer/meshoptimizer.h"
#include "Editor/Utilities/EditorUtilities.h"
#include <ThirdParty/meshoptimizer/meshoptimizer.h>
void RemoveNamespace(String& name)
{
@@ -486,6 +487,11 @@ bool ModelTool::ImportModel(const String& path, ModelData& meshData, Options opt
if (autoImportOutput.IsEmpty() || (data.Types & ImportDataTypes::Textures) == 0 || texture.FilePath.IsEmpty())
continue;
auto filename = StringUtils::GetFileNameWithoutExtension(texture.FilePath);
for (int32 j = filename.Length() - 1; j >= 0; j--)
{
if (EditorUtilities::IsInvalidPathChar(filename[j]))
filename[j] = ' ';
}
if (importedFileNames.Contains(filename))
{
int32 counter = 1;
@@ -526,6 +532,11 @@ bool ModelTool::ImportModel(const String& path, ModelData& meshData, Options opt
if (autoImportOutput.IsEmpty() || (data.Types & ImportDataTypes::Materials) == 0 || !material.UsesProperties())
continue;
auto filename = material.Name;
for (int32 j = filename.Length() - 1; j >= 0; j--)
{
if (EditorUtilities::IsInvalidPathChar(filename[j]))
filename[j] = ' ';
}
if (importedFileNames.Contains(filename))
{
int32 counter = 1;