2015-03-17 20:59:39 +00:00
|
|
|
#include "manager.hpp"
|
|
|
|
|
2022-08-02 22:00:54 +00:00
|
|
|
#include <algorithm>
|
2024-01-16 22:53:55 +00:00
|
|
|
#include <cassert>
|
2015-03-17 20:59:39 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2022-07-02 22:02:29 +00:00
|
|
|
#include <components/files/conversion.hpp>
|
2022-08-02 22:00:54 +00:00
|
|
|
#include <components/misc/strings/lower.hpp>
|
2024-01-15 23:30:41 +00:00
|
|
|
#include <components/vfs/recursivedirectoryiterator.hpp>
|
2015-12-07 20:58:30 +00:00
|
|
|
|
2015-03-17 20:59:39 +00:00
|
|
|
#include "archive.hpp"
|
2024-01-15 23:30:41 +00:00
|
|
|
#include "file.hpp"
|
2023-05-31 21:11:03 +00:00
|
|
|
#include "pathutil.hpp"
|
2024-01-15 23:30:41 +00:00
|
|
|
#include "recursivedirectoryiterator.hpp"
|
2015-03-17 20:59:39 +00:00
|
|
|
|
|
|
|
namespace VFS
|
|
|
|
{
|
2024-01-15 23:30:41 +00:00
|
|
|
Manager::Manager() = default;
|
|
|
|
|
|
|
|
Manager::~Manager() = default;
|
|
|
|
|
2017-08-22 02:31:19 +00:00
|
|
|
void Manager::reset()
|
|
|
|
{
|
|
|
|
mIndex.clear();
|
2015-03-17 20:59:39 +00:00
|
|
|
mArchives.clear();
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:14:20 +00:00
|
|
|
void Manager::addArchive(std::unique_ptr<Archive>&& archive)
|
2015-03-17 20:59:39 +00:00
|
|
|
{
|
2022-07-16 10:14:20 +00:00
|
|
|
mArchives.push_back(std::move(archive));
|
2015-03-17 20:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::buildIndex()
|
|
|
|
{
|
|
|
|
mIndex.clear();
|
|
|
|
|
2022-07-16 10:14:20 +00:00
|
|
|
for (const auto& archive : mArchives)
|
2023-05-31 21:11:03 +00:00
|
|
|
archive->listResources(mIndex);
|
2015-03-17 20:59:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 22:55:42 +00:00
|
|
|
Files::IStreamPtr Manager::get(const Path::Normalized& name) const
|
2015-03-17 20:59:39 +00:00
|
|
|
{
|
2024-01-16 22:55:42 +00:00
|
|
|
return getNormalized(name);
|
2015-03-26 17:02:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 14:20:48 +00:00
|
|
|
Files::IStreamPtr Manager::getNormalized(std::string_view normalizedName) const
|
2015-03-26 17:02:51 +00:00
|
|
|
{
|
2024-01-16 22:53:55 +00:00
|
|
|
assert(Path::isNormalized(normalizedName));
|
2023-12-17 14:20:48 +00:00
|
|
|
const auto found = mIndex.find(normalizedName);
|
2015-03-17 20:59:39 +00:00
|
|
|
if (found == mIndex.end())
|
2023-12-17 14:20:48 +00:00
|
|
|
throw std::runtime_error("Resource '" + std::string(normalizedName) + "' not found");
|
2015-03-17 20:59:39 +00:00
|
|
|
return found->second->open();
|
|
|
|
}
|
|
|
|
|
2024-01-16 22:55:42 +00:00
|
|
|
bool Manager::exists(const Path::Normalized& name) const
|
2015-03-17 20:59:39 +00:00
|
|
|
{
|
2024-01-16 22:55:42 +00:00
|
|
|
return mIndex.find(name) != mIndex.end();
|
2015-03-26 17:02:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 22:55:42 +00:00
|
|
|
std::string Manager::getArchive(const Path::Normalized& name) const
|
2020-12-29 20:45:59 +00:00
|
|
|
{
|
2020-12-30 09:35:51 +00:00
|
|
|
for (auto it = mArchives.rbegin(); it != mArchives.rend(); ++it)
|
2020-12-29 20:45:59 +00:00
|
|
|
{
|
2024-01-16 22:55:42 +00:00
|
|
|
if ((*it)->contains(name))
|
2020-12-30 09:35:51 +00:00
|
|
|
return (*it)->getDescription();
|
2020-12-29 20:45:59 +00:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2021-09-06 19:56:32 +00:00
|
|
|
|
2022-06-19 11:28:33 +00:00
|
|
|
std::filesystem::path Manager::getAbsoluteFileName(const std::filesystem::path& name) const
|
2022-05-14 01:58:00 +00:00
|
|
|
{
|
2022-07-02 22:02:29 +00:00
|
|
|
std::string normalized = Files::pathToUnicodeString(name);
|
2023-05-31 21:11:03 +00:00
|
|
|
Path::normalizeFilenameInPlace(normalized);
|
2022-05-14 01:58:00 +00:00
|
|
|
|
2022-06-19 11:28:33 +00:00
|
|
|
const auto found = mIndex.find(normalized);
|
2022-05-14 01:58:00 +00:00
|
|
|
if (found == mIndex.end())
|
|
|
|
throw std::runtime_error("Resource '" + normalized + "' not found");
|
2022-07-02 22:02:29 +00:00
|
|
|
return found->second->getPath();
|
2022-05-14 01:58:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 23:30:41 +00:00
|
|
|
RecursiveDirectoryRange Manager::getRecursiveDirectoryIterator(std::string_view path) const
|
2021-09-23 16:47:59 +00:00
|
|
|
{
|
|
|
|
if (path.empty())
|
|
|
|
return { mIndex.begin(), mIndex.end() };
|
2023-05-31 21:11:03 +00:00
|
|
|
std::string normalized = Path::normalizeFilename(path);
|
2021-09-23 16:47:59 +00:00
|
|
|
const auto it = mIndex.lower_bound(normalized);
|
2024-01-15 21:26:56 +00:00
|
|
|
if (it == mIndex.end() || !it->first.view().starts_with(normalized))
|
2021-09-23 16:47:59 +00:00
|
|
|
return { it, it };
|
|
|
|
++normalized.back();
|
|
|
|
return { it, mIndex.lower_bound(normalized) };
|
2021-09-06 19:56:32 +00:00
|
|
|
}
|
2015-03-17 20:59:39 +00:00
|
|
|
}
|