Use normalizeFilename in correctResourcePath

This commit is contained in:
elsid 2025-09-07 15:48:17 +02:00
parent 9c6407a1e3
commit a79e121df6
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40
3 changed files with 24 additions and 26 deletions

View File

@ -82,7 +82,7 @@ message(STATUS "Configuring OpenMW...")
set(OPENMW_VERSION_MAJOR 0) set(OPENMW_VERSION_MAJOR 0)
set(OPENMW_VERSION_MINOR 50) set(OPENMW_VERSION_MINOR 50)
set(OPENMW_VERSION_RELEASE 0) set(OPENMW_VERSION_RELEASE 0)
set(OPENMW_LUA_API_REVISION 95) set(OPENMW_LUA_API_REVISION 96)
set(OPENMW_POSTPROCESSING_API_REVISION 3) set(OPENMW_POSTPROCESSING_API_REVISION 3)
set(OPENMW_VERSION_COMMITHASH "") set(OPENMW_VERSION_COMMITHASH "")

View File

@ -37,7 +37,7 @@ namespace Misc::ResourceHelpers
TEST(MiscResourceHelpersCorrectResourcePath, shouldFallbackToGivenExtentionIfDoesNotExistInVfs) TEST(MiscResourceHelpersCorrectResourcePath, shouldFallbackToGivenExtentionIfDoesNotExistInVfs)
{ {
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({}); const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({});
EXPECT_EQ(correctResourcePath({ { "sound" } }, "sound/foo.wav", vfs.get(), ".mp3"), "sound\\foo.mp3"); EXPECT_EQ(correctResourcePath({ { "sound" } }, "sound/foo.wav", vfs.get(), ".mp3"), "sound/foo.mp3");
} }
TEST(MiscResourceHelpersCorrectResourcePath, shouldFallbackToGivenExtentionIfBothExistInVfs) TEST(MiscResourceHelpersCorrectResourcePath, shouldFallbackToGivenExtentionIfBothExistInVfs)
@ -48,7 +48,7 @@ namespace Misc::ResourceHelpers
{ wav, nullptr }, { wav, nullptr },
{ mp3, nullptr }, { mp3, nullptr },
}); });
EXPECT_EQ(correctResourcePath({ { "sound" } }, wav.value(), vfs.get(), ".mp3"), "sound\\foo.mp3"); EXPECT_EQ(correctResourcePath({ { "sound" } }, wav.value(), vfs.get(), ".mp3"), "sound/foo.mp3");
} }
TEST(MiscResourceHelpersCorrectResourcePath, shouldKeepExtentionIfExistInVfs) TEST(MiscResourceHelpersCorrectResourcePath, shouldKeepExtentionIfExistInVfs)
@ -57,13 +57,13 @@ namespace Misc::ResourceHelpers
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({ const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({
{ wav, nullptr }, { wav, nullptr },
}); });
EXPECT_EQ(correctResourcePath({ { "sound" } }, wav.value(), vfs.get(), ".mp3"), "sound\\foo.wav"); EXPECT_EQ(correctResourcePath({ { "sound" } }, wav.value(), vfs.get(), ".mp3"), "sound/foo.wav");
} }
TEST(MiscResourceHelpersCorrectResourcePath, shouldPrefixWithGivenTopDirectory) TEST(MiscResourceHelpersCorrectResourcePath, shouldPrefixWithGivenTopDirectory)
{ {
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({}); const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({});
EXPECT_EQ(correctResourcePath({ { "sound" } }, "foo.mp3", vfs.get(), ".mp3"), "sound\\foo.mp3"); EXPECT_EQ(correctResourcePath({ { "sound" } }, "foo.mp3", vfs.get(), ".mp3"), "sound/foo.mp3");
} }
TEST(MiscResourceHelpersCorrectResourcePath, shouldChangeTopDirectoryAndKeepExtensionIfOriginalExistInVfs) TEST(MiscResourceHelpersCorrectResourcePath, shouldChangeTopDirectoryAndKeepExtensionIfOriginalExistInVfs)
@ -72,8 +72,8 @@ namespace Misc::ResourceHelpers
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({ const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({
{ a, nullptr }, { a, nullptr },
}); });
EXPECT_EQ(correctResourcePath({ { "textures", "bookart" } }, "bookart/foo.a", vfs.get(), ".b"), EXPECT_EQ(
"textures\\foo.a"); correctResourcePath({ { "textures", "bookart" } }, "bookart/foo.a", vfs.get(), ".b"), "textures/foo.a");
} }
TEST(MiscResourceHelpersCorrectResourcePath, shouldChangeTopDirectoryAndChangeExtensionIfFallbackExistInVfs) TEST(MiscResourceHelpersCorrectResourcePath, shouldChangeTopDirectoryAndChangeExtensionIfFallbackExistInVfs)
@ -82,33 +82,32 @@ namespace Misc::ResourceHelpers
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({ const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({
{ b, nullptr }, { b, nullptr },
}); });
EXPECT_EQ(correctResourcePath({ { "textures", "bookart" } }, "bookart/foo.a", vfs.get(), ".b"), EXPECT_EQ(
"textures\\foo.b"); correctResourcePath({ { "textures", "bookart" } }, "bookart/foo.a", vfs.get(), ".b"), "textures/foo.b");
} }
TEST(MiscResourceHelpersCorrectResourcePath, shouldLowerCase) TEST(MiscResourceHelpersCorrectResourcePath, shouldLowerCase)
{ {
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({}); const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({});
EXPECT_EQ(correctResourcePath({ { "sound" } }, "SOUND\\Foo.MP3", vfs.get(), ".mp3"), "sound\\foo.mp3"); EXPECT_EQ(correctResourcePath({ { "sound" } }, "SOUND\\Foo.MP3", vfs.get(), ".mp3"), "sound/foo.mp3");
} }
TEST(MiscResourceHelpersCorrectResourcePath, shouldRemoveLeadingSlash) TEST(MiscResourceHelpersCorrectResourcePath, shouldRemoveLeadingSlash)
{ {
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({}); const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({});
EXPECT_EQ(correctResourcePath({ { "sound" } }, "\\SOUND\\Foo.MP3", vfs.get(), ".mp3"), "sound\\foo.mp3"); EXPECT_EQ(correctResourcePath({ { "sound" } }, "\\SOUND\\Foo.MP3", vfs.get(), ".mp3"), "sound/foo.mp3");
} }
TEST(MiscResourceHelpersCorrectResourcePath, shouldRemoveDuplicateSlashes) TEST(MiscResourceHelpersCorrectResourcePath, shouldRemoveDuplicateSlashes)
{ {
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({}); const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({});
EXPECT_EQ( EXPECT_EQ(correctResourcePath({ { "sound" } }, "\\\\SOUND\\\\Foo.MP3", vfs.get(), ".mp3"), "sound/foo.mp3");
correctResourcePath({ { "sound" } }, "\\\\SOUND\\\\Foo.MP3", vfs.get(), ".mp3"), "sound\\foo.mp3");
} }
TEST(MiscResourceHelpersCorrectResourcePath, shouldConvertToBackSlash) TEST(MiscResourceHelpersCorrectResourcePath, shouldConvertToForwardSlash)
{ {
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({}); const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({});
EXPECT_EQ(correctResourcePath({ { "sound" } }, "SOUND/Foo.MP3", vfs.get(), ".mp3"), "sound\\foo.mp3"); EXPECT_EQ(correctResourcePath({ { "sound" } }, "SOUND/Foo.MP3", vfs.get(), ".mp3"), "sound/foo.mp3");
} }
struct MiscResourceHelpersCorrectResourcePathShouldRemoveExtraPrefix : TestWithParam<std::string> struct MiscResourceHelpersCorrectResourcePathShouldRemoveExtraPrefix : TestWithParam<std::string>
@ -118,7 +117,7 @@ namespace Misc::ResourceHelpers
TEST_P(MiscResourceHelpersCorrectResourcePathShouldRemoveExtraPrefix, shouldMatchExpected) TEST_P(MiscResourceHelpersCorrectResourcePathShouldRemoveExtraPrefix, shouldMatchExpected)
{ {
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({}); const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({});
EXPECT_EQ(correctResourcePath({ { "sound" } }, GetParam(), vfs.get(), ".mp3"), "sound\\foo.mp3"); EXPECT_EQ(correctResourcePath({ { "sound" } }, GetParam(), vfs.get(), ".mp3"), "sound/foo.mp3");
} }
const std::vector<std::string> pathsWithPrefix = { const std::vector<std::string> pathsWithPrefix = {

View File

@ -37,15 +37,14 @@ bool Misc::ResourceHelpers::changeExtensionToDds(std::string& path)
std::string Misc::ResourceHelpers::correctResourcePath(std::span<const std::string_view> topLevelDirectories, std::string Misc::ResourceHelpers::correctResourcePath(std::span<const std::string_view> topLevelDirectories,
std::string_view resPath, const VFS::Manager* vfs, std::string_view ext) std::string_view resPath, const VFS::Manager* vfs, std::string_view ext)
{ {
std::string correctedPath = Misc::StringUtils::lowerCase(resPath); std::string correctedPath = VFS::Path::normalizeFilename(resPath);
// Flatten slashes // Flatten slashes
std::replace(correctedPath.begin(), correctedPath.end(), '/', '\\'); auto bothSeparators = [](char a, char b) { return a == VFS::Path::separator && b == VFS::Path::separator; };
auto bothSeparators = [](char a, char b) { return a == '\\' && b == '\\'; };
correctedPath.erase(std::unique(correctedPath.begin(), correctedPath.end(), bothSeparators), correctedPath.end()); correctedPath.erase(std::unique(correctedPath.begin(), correctedPath.end(), bothSeparators), correctedPath.end());
// Remove leading separator // Remove leading separator
if (!correctedPath.empty() && correctedPath[0] == '\\') if (!correctedPath.empty() && correctedPath[0] == VFS::Path::separator)
correctedPath.erase(0, 1); correctedPath.erase(0, 1);
// Handle top level directory // Handle top level directory
@ -54,15 +53,15 @@ std::string Misc::ResourceHelpers::correctResourcePath(std::span<const std::stri
{ {
if (correctedPath.starts_with(potentialTopLevelDirectory) if (correctedPath.starts_with(potentialTopLevelDirectory)
&& correctedPath.size() > potentialTopLevelDirectory.size() && correctedPath.size() > potentialTopLevelDirectory.size()
&& correctedPath[potentialTopLevelDirectory.size()] == '\\') && correctedPath[potentialTopLevelDirectory.size()] == VFS::Path::separator)
{ {
needsPrefix = false; needsPrefix = false;
break; break;
} }
else else
{ {
std::string topLevelPrefix = std::string{ potentialTopLevelDirectory } + '\\'; std::string topLevelPrefix = std::string{ potentialTopLevelDirectory } + VFS::Path::separator;
size_t topLevelPos = correctedPath.find('\\' + topLevelPrefix); size_t topLevelPos = correctedPath.find(VFS::Path::separator + topLevelPrefix);
if (topLevelPos != std::string::npos) if (topLevelPos != std::string::npos)
{ {
correctedPath.erase(0, topLevelPos + 1); correctedPath.erase(0, topLevelPos + 1);
@ -72,7 +71,7 @@ std::string Misc::ResourceHelpers::correctResourcePath(std::span<const std::stri
} }
} }
if (needsPrefix) if (needsPrefix)
correctedPath = std::string{ topLevelDirectories.front() } + '\\' + correctedPath; correctedPath = std::string{ topLevelDirectories.front() } + VFS::Path::separator + correctedPath;
std::string origExt = correctedPath; std::string origExt = correctedPath;
@ -88,7 +87,7 @@ std::string Misc::ResourceHelpers::correctResourcePath(std::span<const std::stri
// fall back to a resource in the top level directory if it exists // fall back to a resource in the top level directory if it exists
std::string fallback{ topLevelDirectories.front() }; std::string fallback{ topLevelDirectories.front() };
fallback += '\\'; fallback += VFS::Path::separator;
fallback += Misc::getFileName(correctedPath); fallback += Misc::getFileName(correctedPath);
if (vfs->exists(fallback)) if (vfs->exists(fallback))
@ -97,7 +96,7 @@ std::string Misc::ResourceHelpers::correctResourcePath(std::span<const std::stri
if (isExtChanged) if (isExtChanged)
{ {
fallback = topLevelDirectories.front(); fallback = topLevelDirectories.front();
fallback += '\\'; fallback += VFS::Path::separator;
fallback += Misc::getFileName(origExt); fallback += Misc::getFileName(origExt);
if (vfs->exists(fallback)) if (vfs->exists(fallback))
return fallback; return fallback;