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_MINOR 50)
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_VERSION_COMMITHASH "")

View File

@ -37,7 +37,7 @@ namespace Misc::ResourceHelpers
TEST(MiscResourceHelpersCorrectResourcePath, shouldFallbackToGivenExtentionIfDoesNotExistInVfs)
{
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)
@ -48,7 +48,7 @@ namespace Misc::ResourceHelpers
{ wav, 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)
@ -57,13 +57,13 @@ namespace Misc::ResourceHelpers
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({
{ 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)
{
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)
@ -72,8 +72,8 @@ namespace Misc::ResourceHelpers
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({
{ a, nullptr },
});
EXPECT_EQ(correctResourcePath({ { "textures", "bookart" } }, "bookart/foo.a", vfs.get(), ".b"),
"textures\\foo.a");
EXPECT_EQ(
correctResourcePath({ { "textures", "bookart" } }, "bookart/foo.a", vfs.get(), ".b"), "textures/foo.a");
}
TEST(MiscResourceHelpersCorrectResourcePath, shouldChangeTopDirectoryAndChangeExtensionIfFallbackExistInVfs)
@ -82,33 +82,32 @@ namespace Misc::ResourceHelpers
const std::unique_ptr<const VFS::Manager> vfs = TestingOpenMW::createTestVFS({
{ b, nullptr },
});
EXPECT_EQ(correctResourcePath({ { "textures", "bookart" } }, "bookart/foo.a", vfs.get(), ".b"),
"textures\\foo.b");
EXPECT_EQ(
correctResourcePath({ { "textures", "bookart" } }, "bookart/foo.a", vfs.get(), ".b"), "textures/foo.b");
}
TEST(MiscResourceHelpersCorrectResourcePath, shouldLowerCase)
{
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)
{
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)
{
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, shouldConvertToBackSlash)
TEST(MiscResourceHelpersCorrectResourcePath, shouldConvertToForwardSlash)
{
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>
@ -118,7 +117,7 @@ namespace Misc::ResourceHelpers
TEST_P(MiscResourceHelpersCorrectResourcePathShouldRemoveExtraPrefix, shouldMatchExpected)
{
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 = {

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_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
std::replace(correctedPath.begin(), correctedPath.end(), '/', '\\');
auto bothSeparators = [](char a, char b) { return a == '\\' && b == '\\'; };
auto bothSeparators = [](char a, char b) { return a == VFS::Path::separator && b == VFS::Path::separator; };
correctedPath.erase(std::unique(correctedPath.begin(), correctedPath.end(), bothSeparators), correctedPath.end());
// Remove leading separator
if (!correctedPath.empty() && correctedPath[0] == '\\')
if (!correctedPath.empty() && correctedPath[0] == VFS::Path::separator)
correctedPath.erase(0, 1);
// Handle top level directory
@ -54,15 +53,15 @@ std::string Misc::ResourceHelpers::correctResourcePath(std::span<const std::stri
{
if (correctedPath.starts_with(potentialTopLevelDirectory)
&& correctedPath.size() > potentialTopLevelDirectory.size()
&& correctedPath[potentialTopLevelDirectory.size()] == '\\')
&& correctedPath[potentialTopLevelDirectory.size()] == VFS::Path::separator)
{
needsPrefix = false;
break;
}
else
{
std::string topLevelPrefix = std::string{ potentialTopLevelDirectory } + '\\';
size_t topLevelPos = correctedPath.find('\\' + topLevelPrefix);
std::string topLevelPrefix = std::string{ potentialTopLevelDirectory } + VFS::Path::separator;
size_t topLevelPos = correctedPath.find(VFS::Path::separator + topLevelPrefix);
if (topLevelPos != std::string::npos)
{
correctedPath.erase(0, topLevelPos + 1);
@ -72,7 +71,7 @@ std::string Misc::ResourceHelpers::correctResourcePath(std::span<const std::stri
}
}
if (needsPrefix)
correctedPath = std::string{ topLevelDirectories.front() } + '\\' + correctedPath;
correctedPath = std::string{ topLevelDirectories.front() } + VFS::Path::separator + 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
std::string fallback{ topLevelDirectories.front() };
fallback += '\\';
fallback += VFS::Path::separator;
fallback += Misc::getFileName(correctedPath);
if (vfs->exists(fallback))
@ -97,7 +96,7 @@ std::string Misc::ResourceHelpers::correctResourcePath(std::span<const std::stri
if (isExtChanged)
{
fallback = topLevelDirectories.front();
fallback += '\\';
fallback += VFS::Path::separator;
fallback += Misc::getFileName(origExt);
if (vfs->exists(fallback))
return fallback;