2014-07-04 12:46:57 +02:00
|
|
|
#include "resources.hpp"
|
|
|
|
|
2014-12-03 15:31:00 +01:00
|
|
|
#include <algorithm>
|
2014-07-04 12:46:57 +02:00
|
|
|
#include <sstream>
|
2022-10-19 19:02:00 +02:00
|
|
|
#include <stddef.h>
|
2014-07-04 12:46:57 +02:00
|
|
|
#include <stdexcept>
|
2021-09-04 18:07:23 +02:00
|
|
|
#include <string_view>
|
2022-10-19 19:02:00 +02:00
|
|
|
#include <utility>
|
2014-07-04 12:46:57 +02:00
|
|
|
|
2022-10-19 19:02:00 +02:00
|
|
|
#include <apps/opencs/model/world/universalid.hpp>
|
2015-03-19 17:49:41 +01:00
|
|
|
|
2022-08-03 00:00:54 +02:00
|
|
|
#include <components/misc/strings/lower.hpp>
|
2022-10-19 19:02:00 +02:00
|
|
|
#include <components/vfs/manager.hpp>
|
2014-07-04 12:46:57 +02:00
|
|
|
|
2015-03-19 17:49:41 +01:00
|
|
|
CSMWorld::Resources::Resources(
|
|
|
|
const VFS::Manager* vfs, const std::string& baseDirectory, UniversalId::Type type, const char* const* extensions)
|
2014-07-07 15:20:05 +02:00
|
|
|
: mBaseDirectory(baseDirectory)
|
|
|
|
, mType(type)
|
2014-07-04 12:46:57 +02:00
|
|
|
{
|
2017-08-19 03:43:31 -04:00
|
|
|
recreate(vfs, extensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::Resources::recreate(const VFS::Manager* vfs, const char* const* extensions)
|
|
|
|
{
|
|
|
|
mFiles.clear();
|
|
|
|
mIndex.clear();
|
|
|
|
|
2021-05-02 10:43:44 +04:00
|
|
|
size_t baseSize = mBaseDirectory.size();
|
2014-07-04 12:46:57 +02:00
|
|
|
|
2021-09-06 21:56:32 +02:00
|
|
|
for (const auto& filepath : vfs->getRecursiveDirectoryIterator(""))
|
2014-07-04 12:46:57 +02:00
|
|
|
{
|
2015-03-19 17:49:41 +01:00
|
|
|
if (filepath.size() < baseSize + 1 || filepath.substr(0, baseSize) != mBaseDirectory
|
|
|
|
|| (filepath[baseSize] != '/' && filepath[baseSize] != '\\'))
|
2014-07-04 12:46:57 +02:00
|
|
|
continue;
|
|
|
|
|
2015-03-19 17:49:41 +01:00
|
|
|
if (extensions)
|
2014-07-04 12:46:57 +02:00
|
|
|
{
|
2016-10-16 01:34:54 +09:00
|
|
|
std::string::size_type extensionIndex = filepath.find_last_of('.');
|
2014-07-07 11:34:24 +02:00
|
|
|
|
2016-10-16 01:34:54 +09:00
|
|
|
if (extensionIndex == std::string::npos)
|
2015-03-19 17:49:41 +01:00
|
|
|
continue;
|
2014-07-07 11:34:24 +02:00
|
|
|
|
2016-10-16 01:34:54 +09:00
|
|
|
std::string extension = filepath.substr(extensionIndex + 1);
|
2014-07-07 11:34:24 +02:00
|
|
|
|
2015-03-19 17:49:41 +01:00
|
|
|
int i = 0;
|
2014-07-07 11:34:24 +02:00
|
|
|
|
2015-03-19 17:49:41 +01:00
|
|
|
for (; extensions[i]; ++i)
|
|
|
|
if (extensions[i] == extension)
|
|
|
|
break;
|
2014-07-07 11:34:24 +02:00
|
|
|
|
2015-03-19 17:49:41 +01:00
|
|
|
if (!extensions[i])
|
|
|
|
continue;
|
2014-07-04 12:46:57 +02:00
|
|
|
}
|
2015-03-19 17:49:41 +01: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 12:46:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSMWorld::Resources::getSize() const
|
|
|
|
{
|
2021-05-02 10:43:44 +04:00
|
|
|
return static_cast<int>(mFiles.size());
|
2014-07-04 12:46:57 +02: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 17:47:39 +04:00
|
|
|
throw std::runtime_error(stream.str());
|
2014-07-04 12:46:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2021-09-04 18:07:23 +02:00
|
|
|
int CSMWorld::Resources::searchId(std::string_view id) const
|
2014-07-04 12:46:57 +02:00
|
|
|
{
|
|
|
|
std::string id2 = Misc::StringUtils::lowerCase(id);
|
|
|
|
|
2014-12-03 15:31:00 +01:00
|
|
|
std::replace(id2.begin(), id2.end(), '\\', '/');
|
|
|
|
|
2014-07-04 12:46:57 +02:00
|
|
|
std::map<std::string, int>::const_iterator iter = mIndex.find(id2);
|
|
|
|
|
|
|
|
if (iter == mIndex.end())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return iter->second;
|
2014-07-07 15:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CSMWorld::UniversalId::Type CSMWorld::Resources::getType() const
|
|
|
|
{
|
|
|
|
return mType;
|
2015-03-19 17:21:15 +01:00
|
|
|
}
|