1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-11-29 20:34:30 +00:00

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::PathContainer mDataDirs{ { std::filesystem::path{ OPENMW_DATA_DIR } } };
const Files::Collections mFileCollections{ mDataDirs }; const Files::Collections mFileCollections{ mDataDirs };
const std::string mContentFile = "template.omwgame"; 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) 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)) if (!archivePath.empty() && !isBSA(archivePath))
{ {
const Files::Collections fileCollections({ archivePath }); const Files::Collections fileCollections({ archivePath });
const Files::MultiDirCollection& bsaCol = fileCollections.getCollection(".bsa"); const Files::MultiDirCollection& bsaCol = fileCollections.getCollection("bsa");
const Files::MultiDirCollection& ba2Col = fileCollections.getCollection(".ba2"); const Files::MultiDirCollection& ba2Col = fileCollections.getCollection("ba2");
for (const Files::MultiDirCollection& collection : { bsaCol, ba2Col }) for (const Files::MultiDirCollection& collection : { bsaCol, ba2Col })
{ {
for (auto& file : collection) for (auto& file : collection)

View file

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

View file

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

View file

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

View file

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

View file

@ -22,7 +22,8 @@ namespace Files
{ {
const auto& path = dirIter.path(); 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; continue;
const auto filename = Files::pathToUnicodeString(path.filename()); const auto filename = Files::pathToUnicodeString(path.filename());
@ -41,7 +42,7 @@ namespace Files
{ {
// handle case folding // handle case folding
mFiles.erase(result->first); mFiles.erase(result->first);
mFiles.insert(std::make_pair(filename, path)); mFiles.emplace(filename, path);
} }
} }
} }

View file

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

View file

@ -26,7 +26,7 @@ namespace Translation
private: private:
typedef std::map<std::string, std::string, std::less<>> ContainerType; 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); const Files::Collections& dataFileCollections);
void loadDataFromStream(ContainerType& container, std::istream& stream); void loadDataFromStream(ContainerType& container, std::istream& stream);