2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CFilesystemLoader.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2012-07-22 18:02:13 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CFilesystemLoader.h"
|
|
|
|
|
|
|
|
#include "CFileInputStream.h"
|
2016-01-16 17:36:16 +02:00
|
|
|
#include "FileStream.h"
|
2012-07-22 18:02:13 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
namespace bfs = boost::filesystem;
|
|
|
|
|
|
|
|
CFilesystemLoader::CFilesystemLoader(std::string _mountPoint, bfs::path baseDirectory, size_t depth, bool initial):
|
2022-09-10 18:30:41 +02:00
|
|
|
baseDirectory(std::move(baseDirectory)),
|
|
|
|
mountPoint(std::move(_mountPoint)),
|
|
|
|
fileList(listFiles(mountPoint, depth, initial)),
|
|
|
|
recursiveDepth(depth)
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->trace("File system loaded, %d files found", fileList.size());
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
std::unique_ptr<CInputStream> CFilesystemLoader::load(const ResourceID & resourceName) const
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
2013-07-28 17:49:50 +03:00
|
|
|
assert(fileList.count(resourceName));
|
2017-08-30 09:19:54 +02:00
|
|
|
bfs::path file = baseDirectory / fileList.at(resourceName);
|
2017-08-27 02:59:09 +02:00
|
|
|
logGlobal->trace("loading %s", file.string());
|
2022-12-07 23:36:20 +02:00
|
|
|
return std::make_unique<CFileInputStream>(file);
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
bool CFilesystemLoader::existsResource(const ResourceID & resourceName) const
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
2013-07-28 17:49:50 +03:00
|
|
|
return fileList.count(resourceName);
|
|
|
|
}
|
2012-07-22 18:02:13 +03:00
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
std::string CFilesystemLoader::getMountPoint() const
|
|
|
|
{
|
|
|
|
return mountPoint;
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
std::optional<boost::filesystem::path> CFilesystemLoader::getResourceName(const ResourceID & resourceName) const
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
2013-07-28 17:49:50 +03:00
|
|
|
assert(existsResource(resourceName));
|
|
|
|
|
2016-01-16 17:36:16 +02:00
|
|
|
return baseDirectory / fileList.at(resourceName);
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2016-10-16 22:09:57 +02:00
|
|
|
void CFilesystemLoader::updateFilteredFiles(std::function<bool(const std::string &)> filter) const
|
|
|
|
{
|
|
|
|
if (filter(mountPoint))
|
|
|
|
{
|
2022-09-10 18:30:41 +02:00
|
|
|
fileList = listFiles(mountPoint, recursiveDepth, false);
|
2016-10-16 22:09:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
std::unordered_set<ResourceID> CFilesystemLoader::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
|
2012-08-01 15:02:54 +03:00
|
|
|
{
|
2013-07-28 17:49:50 +03:00
|
|
|
std::unordered_set<ResourceID> foundID;
|
|
|
|
|
|
|
|
for (auto & file : fileList)
|
|
|
|
{
|
|
|
|
if (filter(file.first))
|
|
|
|
foundID.insert(file.first);
|
2014-08-21 23:26:28 +03:00
|
|
|
}
|
|
|
|
return foundID;
|
2012-08-01 15:02:54 +03:00
|
|
|
}
|
2012-08-07 14:28:52 +03:00
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
bool CFilesystemLoader::createResource(std::string filename, bool update)
|
2012-08-07 14:28:52 +03:00
|
|
|
{
|
2013-07-28 17:49:50 +03:00
|
|
|
ResourceID resID(filename);
|
|
|
|
|
|
|
|
if (fileList.find(resID) != fileList.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!boost::iequals(mountPoint, filename.substr(0, mountPoint.size())))
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->trace("Can't create file: wrong mount point: %s", mountPoint);
|
2012-08-07 14:28:52 +03:00
|
|
|
return false;
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
filename = filename.substr(mountPoint.size());
|
2012-08-07 14:28:52 +03:00
|
|
|
|
2013-05-30 19:25:00 +03:00
|
|
|
if (!update)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2023-01-22 14:45:43 +02:00
|
|
|
if (!FileStream::createFile(baseDirectory / filename))
|
2013-07-28 17:49:50 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fileList[resID] = filename;
|
2012-08-07 14:28:52 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
std::unordered_map<ResourceID, bfs::path> CFilesystemLoader::listFiles(const std::string &mountPoint, size_t depth, bool initial) const
|
2012-08-07 14:28:52 +03:00
|
|
|
{
|
2014-08-21 23:26:28 +03:00
|
|
|
static const EResType::Type initArray[] = {
|
|
|
|
EResType::DIRECTORY,
|
|
|
|
EResType::TEXT,
|
|
|
|
EResType::ARCHIVE_LOD,
|
|
|
|
EResType::ARCHIVE_VID,
|
|
|
|
EResType::ARCHIVE_SND,
|
|
|
|
EResType::ARCHIVE_ZIP };
|
2023-04-11 00:29:36 +02:00
|
|
|
static const std::set<EResType::Type> initialTypes(initArray, initArray + std::size(initArray));
|
2012-09-17 20:25:54 +03:00
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
assert(bfs::is_directory(baseDirectory));
|
|
|
|
std::unordered_map<ResourceID, bfs::path> fileList;
|
2012-08-07 14:28:52 +03:00
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
std::vector<bfs::path> path; //vector holding relative path to our file
|
2012-08-07 14:28:52 +03:00
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
bfs::recursive_directory_iterator enddir;
|
2023-01-14 15:27:11 +02:00
|
|
|
#if BOOST_VERSION >= 107200 // 1.72
|
|
|
|
bfs::recursive_directory_iterator it(baseDirectory, bfs::directory_options::follow_directory_symlink);
|
|
|
|
#else
|
2014-08-21 23:26:28 +03:00
|
|
|
bfs::recursive_directory_iterator it(baseDirectory, bfs::symlink_option::recurse);
|
2023-01-14 15:27:11 +02:00
|
|
|
#endif
|
2012-08-07 14:28:52 +03:00
|
|
|
|
|
|
|
for(; it != enddir; ++it)
|
|
|
|
{
|
|
|
|
EResType::Type type;
|
2023-01-14 15:27:11 +02:00
|
|
|
#if BOOST_VERSION >= 107200
|
|
|
|
const auto currentDepth = it.depth();
|
|
|
|
#else
|
|
|
|
const auto currentDepth = it.level();
|
|
|
|
#endif
|
2012-08-07 14:28:52 +03:00
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
if (bfs::is_directory(it->status()))
|
2012-08-07 14:28:52 +03:00
|
|
|
{
|
2023-01-14 15:27:11 +02:00
|
|
|
path.resize(currentDepth + 1);
|
2014-08-21 23:26:28 +03:00
|
|
|
path.back() = it->path().filename();
|
2013-07-29 13:08:02 +03:00
|
|
|
// don't iterate into directory if depth limit reached
|
2023-01-14 15:27:11 +02:00
|
|
|
#if BOOST_VERSION >= 107200
|
|
|
|
it.disable_recursion_pending(depth <= currentDepth);
|
|
|
|
#else
|
|
|
|
it.no_push(depth <= currentDepth);
|
|
|
|
#endif
|
2012-08-07 14:28:52 +03:00
|
|
|
|
|
|
|
type = EResType::DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
2014-08-21 23:26:28 +03:00
|
|
|
type = EResTypeHelper::getTypeFromExtension(it->path().extension().string());
|
2012-08-07 14:28:52 +03:00
|
|
|
|
2012-09-17 20:25:54 +03:00
|
|
|
if (!initial || vstd::contains(initialTypes, type))
|
2012-08-07 14:28:52 +03:00
|
|
|
{
|
|
|
|
//reconstruct relative filename (not possible via boost AFAIK)
|
2014-08-21 23:26:28 +03:00
|
|
|
bfs::path filename;
|
2023-01-14 15:27:11 +02:00
|
|
|
const size_t iterations = std::min(static_cast<size_t>(currentDepth), path.size());
|
2014-08-21 23:26:28 +03:00
|
|
|
if (iterations)
|
|
|
|
{
|
|
|
|
filename = path.front();
|
|
|
|
for (size_t i = 1; i < iterations; ++i)
|
|
|
|
filename /= path[i];
|
|
|
|
filename /= it->path().filename();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
filename = it->path().filename();
|
|
|
|
|
|
|
|
std::string resName;
|
|
|
|
if (bfs::path::preferred_separator != '/')
|
|
|
|
{
|
|
|
|
// resource names are using UNIX slashes (/)
|
|
|
|
resName.reserve(resName.size() + filename.native().size());
|
|
|
|
resName = mountPoint;
|
|
|
|
for (const char c : filename.string())
|
|
|
|
if (c != bfs::path::preferred_separator)
|
|
|
|
resName.push_back(c);
|
|
|
|
else
|
|
|
|
resName.push_back('/');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
resName = mountPoint + filename.string();
|
|
|
|
|
|
|
|
fileList[ResourceID(resName, type)] = std::move(filename);
|
2012-08-07 14:28:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileList;
|
2013-06-29 16:05:48 +03:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|