1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 04:53:52 +00:00
openmw/apps/opencs/model/world/resources.cpp

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

102 lines
2.5 KiB
C++
Raw Normal View History

2014-07-04 10:46:57 +00:00
#include "resources.hpp"
#include <algorithm>
2014-07-04 10:46:57 +00:00
#include <sstream>
#include <stdexcept>
#include <string_view>
2014-07-04 10:46:57 +00:00
#include <components/vfs/manager.hpp>
#include <components/misc/strings/lower.hpp>
2014-07-04 10:46:57 +00:00
CSMWorld::Resources::Resources(
const VFS::Manager* vfs, const std::string& baseDirectory, UniversalId::Type type, const char* const* extensions)
2014-07-07 13:20:05 +00:00
: mBaseDirectory(baseDirectory)
, mType(type)
2014-07-04 10:46:57 +00:00
{
2017-08-19 07:43:31 +00:00
recreate(vfs, extensions);
}
void CSMWorld::Resources::recreate(const VFS::Manager* vfs, const char* const* extensions)
{
mFiles.clear();
mIndex.clear();
2021-05-02 06:43:44 +00:00
size_t baseSize = mBaseDirectory.size();
2014-07-04 10:46:57 +00:00
for (const auto& filepath : vfs->getRecursiveDirectoryIterator(""))
2014-07-04 10:46:57 +00:00
{
if (filepath.size() < baseSize + 1 || filepath.substr(0, baseSize) != mBaseDirectory
|| (filepath[baseSize] != '/' && filepath[baseSize] != '\\'))
2014-07-04 10:46:57 +00:00
continue;
if (extensions)
2014-07-04 10:46:57 +00:00
{
2016-10-15 16:34:54 +00:00
std::string::size_type extensionIndex = filepath.find_last_of('.');
2016-10-15 16:34:54 +00:00
if (extensionIndex == std::string::npos)
continue;
2016-10-15 16:34:54 +00:00
std::string extension = filepath.substr(extensionIndex + 1);
int i = 0;
for (; extensions[i]; ++i)
if (extensions[i] == extension)
break;
if (!extensions[i])
continue;
2014-07-04 10:46:57 +00:00
}
std::string file = filepath.substr(baseSize + 1);
mFiles.push_back(file);
std::replace(file.begin(), file.end(), '\\', '/');
mIndex.insert(std::make_pair(Misc::StringUtils::lowerCase(file), static_cast<int>(mFiles.size()) - 1));
2014-07-04 10:46:57 +00:00
}
}
int CSMWorld::Resources::getSize() const
{
2021-05-02 06:43:44 +00:00
return static_cast<int>(mFiles.size());
2014-07-04 10:46:57 +00:00
}
std::string CSMWorld::Resources::getId(int index) const
{
return mFiles.at(index);
}
int CSMWorld::Resources::getIndex(const std::string& id) const
{
int index = searchId(id);
if (index == -1)
{
std::ostringstream stream;
stream << "Invalid resource: " << mBaseDirectory << '/' << id;
2019-01-07 13:47:39 +00:00
throw std::runtime_error(stream.str());
2014-07-04 10:46:57 +00:00
}
return index;
}
int CSMWorld::Resources::searchId(std::string_view id) const
2014-07-04 10:46:57 +00:00
{
std::string id2 = Misc::StringUtils::lowerCase(id);
std::replace(id2.begin(), id2.end(), '\\', '/');
2014-07-04 10:46:57 +00:00
std::map<std::string, int>::const_iterator iter = mIndex.find(id2);
if (iter == mIndex.end())
return -1;
return iter->second;
2014-07-07 13:20:05 +00:00
}
CSMWorld::UniversalId::Type CSMWorld::Resources::getType() const
{
return mType;
}