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"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
CFilesystemLoader::CFilesystemLoader(std::string _mountPoint, boost::filesystem::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));
|
2023-07-28 14:51:14 +02:00
|
|
|
boost::filesystem::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-08-06 21:39:39 +02:00
|
|
|
// create folders if not exists
|
|
|
|
boost::filesystem::path p((baseDirectory / filename).c_str());
|
|
|
|
boost::filesystem::create_directories(p.parent_path());
|
|
|
|
|
2023-07-24 18:42:19 +02:00
|
|
|
// create file, if not exists
|
2023-08-04 14:19:09 +02:00
|
|
|
std::ofstream file((baseDirectory / filename).c_str(), std::ofstream::binary);
|
2023-07-24 18:42:19 +02:00
|
|
|
|
|
|
|
if (!file.is_open())
|
2013-07-28 17:49:50 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fileList[resID] = filename;
|
2012-08-07 14:28:52 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
std::unordered_map<ResourceID, boost::filesystem::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
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
assert(boost::filesystem::is_directory(baseDirectory));
|
|
|
|
std::unordered_map<ResourceID, boost::filesystem::path> fileList;
|
2012-08-07 14:28:52 +03:00
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
std::vector<boost::filesystem::path> path; //vector holding relative path to our file
|
2012-08-07 14:28:52 +03:00
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
boost::filesystem::recursive_directory_iterator enddir;
|
2023-01-14 15:27:11 +02:00
|
|
|
#if BOOST_VERSION >= 107200 // 1.72
|
2023-07-28 14:51:14 +02:00
|
|
|
boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::directory_options::follow_directory_symlink);
|
2023-01-14 15:27:11 +02:00
|
|
|
#else
|
2023-07-28 14:51:14 +02:00
|
|
|
boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::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
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
if (boost::filesystem::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)
|
2023-07-28 14:51:14 +02:00
|
|
|
boost::filesystem::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;
|
2023-07-28 14:51:14 +02:00
|
|
|
if (boost::filesystem::path::preferred_separator != '/')
|
2014-08-21 23:26:28 +03:00
|
|
|
{
|
|
|
|
// resource names are using UNIX slashes (/)
|
|
|
|
resName.reserve(resName.size() + filename.native().size());
|
|
|
|
resName = mountPoint;
|
|
|
|
for (const char c : filename.string())
|
2023-07-28 14:51:14 +02:00
|
|
|
if (c != boost::filesystem::path::preferred_separator)
|
2014-08-21 23:26:28 +03:00
|
|
|
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
|