Skip to content

Commit e7a6d33

Browse files
Try to resolve image paths by replacing backslashes or forward slashes in EmbedTexturesProcess (assimp#5844)
* Try to resolve image paths by replacing backslashes. * Some changes suggested by CI * Removed usage of <filesystem>. * Removing usage of C++20/C++23 features --------- Co-authored-by: Kim Kulling <[email protected]>
1 parent 9723b35 commit e7a6d33

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

code/PostProcessing/EmbedTexturesProcess.cpp

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4646
#include "ProcessHelper.h"
4747

4848
#include <fstream>
49+
#include <algorithm>
4950

5051
using namespace Assimp;
5152

@@ -91,25 +92,47 @@ void EmbedTexturesProcess::Execute(aiScene* pScene) {
9192
ASSIMP_LOG_INFO("EmbedTexturesProcess finished. Embedded ", embeddedTexturesCount, " textures." );
9293
}
9394

95+
std::string EmbedTexturesProcess::tryToFindValidPath(const std::string &imagePath) const
96+
{
97+
// Test path directly
98+
if (mIOHandler->Exists(imagePath)) {
99+
return imagePath;
100+
}
101+
ASSIMP_LOG_WARN("EmbedTexturesProcess: Cannot find image: ", imagePath, ". Will try to find it in root folder.");
102+
103+
// Test path in root path
104+
std::string testPath = mRootPath + imagePath;
105+
if (mIOHandler->Exists(testPath)) {
106+
return testPath;
107+
}
108+
109+
// Test path basename in root path
110+
testPath = mRootPath + imagePath.substr(imagePath.find_last_of("\\/") + 1u);
111+
if (mIOHandler->Exists(testPath)) {
112+
return testPath;
113+
}
114+
115+
// In unix systems, '\' is a valid file name character, but some files may use \ as a directory separator.
116+
// Try replacing '\' by '/'.
117+
if (mIOHandler->getOsSeparator() != '\\' && imagePath.find('\\') != std::string::npos) {
118+
ASSIMP_LOG_WARN("EmbedTexturesProcess: Cannot find image '", imagePath, "' in root folder. Will try replacing directory separators.");
119+
testPath = imagePath;
120+
std::replace(testPath.begin(), testPath.end(), '\\', mIOHandler->getOsSeparator());
121+
return tryToFindValidPath(testPath);
122+
}
123+
124+
ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", imagePath, ".");
125+
return {};
126+
}
127+
94128
bool EmbedTexturesProcess::addTexture(aiScene *pScene, const std::string &path) const {
95129
std::streampos imageSize = 0;
96-
std::string imagePath = path;
130+
std::string imagePath = tryToFindValidPath(path);
97131

98-
// Test path directly
99-
if (!mIOHandler->Exists(imagePath)) {
100-
ASSIMP_LOG_WARN("EmbedTexturesProcess: Cannot find image: ", imagePath, ". Will try to find it in root folder.");
101-
102-
// Test path in root path
103-
imagePath = mRootPath + path;
104-
if (!mIOHandler->Exists(imagePath)) {
105-
// Test path basename in root path
106-
imagePath = mRootPath + path.substr(path.find_last_of("\\/") + 1u);
107-
if (!mIOHandler->Exists(imagePath)) {
108-
ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, ".");
109-
return false;
110-
}
111-
}
132+
if (imagePath.empty()) {
133+
return false;
112134
}
135+
113136
IOStream* pFile = mIOHandler->Open(imagePath);
114137
if (pFile == nullptr) {
115138
ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, ".");

code/PostProcessing/EmbedTexturesProcess.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class ASSIMP_API EmbedTexturesProcess : public BaseProcess {
7777
virtual void Execute(aiScene* pScene) override;
7878

7979
private:
80+
// Try several ways to attempt to resolve the image path
81+
std::string tryToFindValidPath(const std::string &imagePath) const;
8082
// Resolve the path and add the file content to the scene as a texture.
8183
bool addTexture(aiScene *pScene, const std::string &path) const;
8284

0 commit comments

Comments
 (0)