1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-09 21:42:13 +00:00
OpenMW/components/vfs/filesystemarchive.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
2.3 KiB
C++
Raw Normal View History

#include "filesystemarchive.hpp"
#include <filesystem>
2023-05-31 21:11:03 +00:00
#include "pathutil.hpp"
2018-08-14 15:42:41 +00:00
#include <components/debug/debuglog.hpp>
#include <components/files/constrainedfilestream.hpp>
#include <components/files/conversion.hpp>
2018-08-14 15:42:41 +00:00
namespace VFS
{
FileSystemArchive::FileSystemArchive(const std::filesystem::path& path)
: mBuiltIndex(false)
, mPath(path)
{
}
2023-12-17 14:20:48 +00:00
void FileSystemArchive::listResources(FileMap& out)
{
if (!mBuiltIndex)
{
const auto str = mPath.u8string();
size_t prefix = str.size();
if (!mPath.empty() && str[prefix - 1] != '\\' && str[prefix - 1] != '/')
++prefix;
for (const auto& i : std::filesystem::recursive_directory_iterator(mPath))
{
if (std::filesystem::is_directory(i))
continue;
const auto& path = i.path();
2023-05-31 21:11:03 +00:00
const std::string proper = Files::pathToUnicodeString(path);
FileSystemArchiveFile file(path);
2023-05-31 21:11:03 +00:00
std::string searchable = Path::normalizeFilename(std::string_view{ proper }.substr(prefix));
2023-05-31 21:11:03 +00:00
const auto inserted = mIndex.emplace(searchable, file);
if (!inserted.second)
Log(Debug::Warning)
<< "Warning: found duplicate file for '" << proper
<< "', please check your file system for two files with the same name in different cases.";
else
out[inserted.first->first] = &inserted.first->second;
}
mBuiltIndex = true;
}
else
{
2023-12-17 14:20:48 +00:00
for (auto& [k, v] : mIndex)
out[k] = &v;
}
}
2023-12-17 14:20:48 +00:00
bool FileSystemArchive::contains(std::string_view file) const
2020-12-29 20:45:59 +00:00
{
2021-05-01 21:21:21 +00:00
return mIndex.find(file) != mIndex.end();
2020-12-29 20:45:59 +00:00
}
std::string FileSystemArchive::getDescription() const
{
return "DIR: " + Files::pathToUnicodeString(mPath);
2020-12-29 20:45:59 +00:00
}
// ----------------------------------------------------------------------------------
FileSystemArchiveFile::FileSystemArchiveFile(const std::filesystem::path& path)
: mPath(path)
{
}
Files::IStreamPtr FileSystemArchiveFile::open()
{
return Files::openConstrainedFileStream(mPath);
}
}