Use pathhelpers to populate Collections

This commit is contained in:
Evil Eye 2025-09-01 17:57:44 +02:00
parent 2105e98d0a
commit 14e1ec6d87
9 changed files with 33 additions and 34 deletions

View File

@ -42,7 +42,7 @@ namespace
const Files::PathContainer mDataDirs{ { std::filesystem::path{ OPENMW_DATA_DIR } } };
const Files::Collections mFileCollections{ mDataDirs };
const std::string mContentFile = "template.omwgame";
const std::filesystem::path mContentFilePath = mFileCollections.getCollection(".omwgame").getPath(mContentFile);
const std::filesystem::path mContentFilePath = mFileCollections.getCollection("omwgame").getPath(mContentFile);
};
TEST_F(ESM3ReadersCacheWithContentFile, shouldKeepOpenReleasedOpenReader)

View File

@ -190,8 +190,8 @@ void readVFS(std::unique_ptr<VFS::Archive>&& archive, const std::filesystem::pat
if (!archivePath.empty() && !isBSA(archivePath))
{
const Files::Collections fileCollections({ archivePath });
const Files::MultiDirCollection& bsaCol = fileCollections.getCollection(".bsa");
const Files::MultiDirCollection& ba2Col = fileCollections.getCollection(".ba2");
const Files::MultiDirCollection& bsaCol = fileCollections.getCollection("bsa");
const Files::MultiDirCollection& ba2Col = fileCollections.getCollection("ba2");
for (const Files::MultiDirCollection& collection : { bsaCol, ba2Col })
{
for (auto& file : collection)

View File

@ -32,6 +32,7 @@
#include <components/misc/constants.hpp>
#include <components/misc/convert.hpp>
#include <components/misc/mathutil.hpp>
#include <components/misc/pathhelpers.hpp>
#include <components/misc/resourcehelpers.hpp>
#include <components/misc/rng.hpp>
@ -2885,9 +2886,7 @@ namespace MWWorld
int idx = 0;
for (const std::string& file : content)
{
const auto filename = Files::pathFromUnicodeString(file);
const Files::MultiDirCollection& col
= fileCollections.getCollection(Files::pathToUnicodeString(filename.extension()));
const Files::MultiDirCollection& col = fileCollections.getCollection(Misc::getFileExtension(file));
if (col.doesExist(file))
{
gameContentLoader.load(col.getPath(file), idx, listener);

View File

@ -18,6 +18,7 @@
#include <components/files/conversion.hpp>
#include <components/files/multidircollection.hpp>
#include <components/loadinglistener/loadinglistener.hpp>
#include <components/misc/pathhelpers.hpp>
#include <components/misc/resourcehelpers.hpp>
#include <components/misc/strings/lower.hpp>
@ -211,21 +212,20 @@ namespace EsmLoader
{
ShallowContent result;
const std::set<std::string> supportedFormats{
".esm",
".esp",
".omwgame",
".omwaddon",
".project",
const std::set<std::string_view, Misc::StringUtils::CiComp> supportedFormats{
"esm",
"esp",
"omwgame",
"omwaddon",
"project",
};
for (std::size_t i = 0; i < contentFiles.size(); ++i)
{
const std::string& file = contentFiles[i];
const std::string extension
= Misc::StringUtils::lowerCase(Files::pathToUnicodeString(std::filesystem::path(file).extension()));
const std::string_view extension = Misc::getFileExtension(file);
if (supportedFormats.find(extension) == supportedFormats.end())
if (!supportedFormats.contains(extension))
{
Log(Debug::Warning) << "Skipping unsupported content file: " << file;
continue;

View File

@ -2,7 +2,6 @@
#include "conversion.hpp"
#include <components/misc/strings/algorithm.hpp>
#include <components/misc/strings/lower.hpp>
namespace Files
{

View File

@ -15,8 +15,7 @@ namespace Files
///< Directories are listed with increasing priority.
Collections(const Files::PathContainer& directories);
///< Return a file collection for the given extension. Extension must contain the
/// leading dot
///< Return a file collection for the given extension.
const MultiDirCollection& getCollection(std::string_view extension) const;
std::filesystem::path getPath(std::string_view file) const;

View File

@ -22,7 +22,8 @@ namespace Files
{
const auto& path = dirIter.path();
if (!Misc::StringUtils::ciEqual(extension, Files::pathToUnicodeString(path.extension())))
std::string ext = Files::pathToUnicodeString(path.extension());
if (ext.size() != extension.size() + 1 || !Misc::StringUtils::ciEndsWith(ext, extension))
continue;
const auto filename = Files::pathToUnicodeString(path.filename());
@ -41,7 +42,7 @@ namespace Files
{
// handle case folding
mFiles.erase(result->first);
mFiles.insert(std::make_pair(filename, path));
mFiles.emplace(filename, path);
}
}
}

View File

@ -2,6 +2,8 @@
#include <fstream>
#include <components/misc/pathhelpers.hpp>
namespace Translation
{
Storage::Storage()
@ -11,25 +13,24 @@ namespace Translation
void Storage::loadTranslationData(const Files::Collections& dataFileCollections, std::string_view esmFileName)
{
std::string esmNameNoExtension(Misc::StringUtils::lowerCase(esmFileName));
// changing the extension
size_t dotPos = esmNameNoExtension.rfind('.');
if (dotPos != std::string::npos)
esmNameNoExtension.resize(dotPos);
std::string_view esmNameNoExtension = Misc::stemFile(esmFileName);
loadData(mCellNamesTranslations, esmNameNoExtension, ".cel", dataFileCollections);
loadData(mPhraseForms, esmNameNoExtension, ".top", dataFileCollections);
loadData(mTopicIDs, esmNameNoExtension, ".mrk", dataFileCollections);
loadData(mCellNamesTranslations, esmNameNoExtension, "cel", dataFileCollections);
loadData(mPhraseForms, esmNameNoExtension, "top", dataFileCollections);
loadData(mTopicIDs, esmNameNoExtension, "mrk", dataFileCollections);
}
void Storage::loadData(ContainerType& container, const std::string& fileNameNoExtension,
const std::string& extension, const Files::Collections& dataFileCollections)
void Storage::loadData(ContainerType& container, std::string_view fileNameNoExtension, std::string_view extension,
const Files::Collections& dataFileCollections)
{
std::string fileName = fileNameNoExtension + extension;
std::string fileName(fileNameNoExtension);
fileName += '.';
fileName += extension;
if (dataFileCollections.getCollection(extension).doesExist(fileName))
const Files::MultiDirCollection& collection = dataFileCollections.getCollection(extension);
if (collection.doesExist(fileName))
{
std::ifstream stream(dataFileCollections.getCollection(extension).getPath(fileName).c_str());
std::ifstream stream(collection.getPath(fileName));
if (!stream.is_open())
throw std::runtime_error("failed to open translation file: " + fileName);

View File

@ -26,7 +26,7 @@ namespace Translation
private:
typedef std::map<std::string, std::string, std::less<>> ContainerType;
void loadData(ContainerType& container, const std::string& fileNameNoExtension, const std::string& extension,
void loadData(ContainerType& container, std::string_view fileNameNoExtension, std::string_view extension,
const Files::Collections& dataFileCollections);
void loadDataFromStream(ContainerType& container, std::istream& stream);