123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /*
- * 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
- *
- */
- #include "StdInc.h"
- #include "CFilesystemLoader.h"
- #include "CFileInputStream.h"
- #include "../ExceptionsCommon.h"
- #include "../texts/TextOperations.h"
- VCMI_LIB_NAMESPACE_BEGIN
- CFilesystemLoader::CFilesystemLoader(std::string _mountPoint, boost::filesystem::path baseDirectory, size_t depth, bool initial):
- baseDirectory(std::move(baseDirectory)),
- mountPoint(std::move(_mountPoint)),
- recursiveDepth(depth)
- {
- try {
- fileList = listFiles(mountPoint, depth, initial);
- }
- catch (const boost::filesystem::filesystem_error & e) {
- throw DataLoadingException("Failed to load content of '" + baseDirectory.string() + "'. Reason: " + e.what());
- }
- logGlobal->trace("File system loaded, %d files found", fileList.size());
- }
- std::unique_ptr<CInputStream> CFilesystemLoader::load(const ResourcePath & resourceName) const
- {
- assert(fileList.count(resourceName));
- boost::filesystem::path file = baseDirectory / fileList.at(resourceName);
- logGlobal->trace("loading %s", file.string());
- return std::make_unique<CFileInputStream>(file);
- }
- bool CFilesystemLoader::existsResource(const ResourcePath & resourceName) const
- {
- return fileList.count(resourceName);
- }
- std::string CFilesystemLoader::getMountPoint() const
- {
- return mountPoint;
- }
- std::optional<boost::filesystem::path> CFilesystemLoader::getResourceName(const ResourcePath & resourceName) const
- {
- assert(existsResource(resourceName));
- return baseDirectory / fileList.at(resourceName);
- }
- void CFilesystemLoader::updateFilteredFiles(std::function<bool(const std::string &)> filter) const
- {
- if (filter(mountPoint))
- {
- fileList = listFiles(mountPoint, recursiveDepth, false);
- }
- }
- std::unordered_set<ResourcePath> CFilesystemLoader::getFilteredFiles(std::function<bool(const ResourcePath &)> filter) const
- {
- std::unordered_set<ResourcePath> foundID;
- for (auto & file : fileList)
- {
- if (filter(file.first))
- foundID.insert(file.first);
- }
- return foundID;
- }
- bool CFilesystemLoader::createResource(const std::string & requestedFilename, bool update)
- {
- std::string filename = requestedFilename;
- ResourcePath resID(filename);
- if (fileList.find(resID) != fileList.end())
- return true;
- if (!boost::iequals(mountPoint, filename.substr(0, mountPoint.size())))
- {
- logGlobal->trace("Can't create file: wrong mount point: %s", mountPoint);
- return false;
- }
- filename = filename.substr(mountPoint.size());
- boost::filesystem::path filePath = TextOperations::Utf8TofilesystemPath(filename);
- if (!update)
- {
- // create folders if not exists
- boost::filesystem::path p((baseDirectory / filePath).c_str());
- boost::filesystem::create_directories(p.parent_path());
- // create file, if not exists
- std::ofstream file((baseDirectory / filePath).c_str(), std::ofstream::binary);
- if (!file.is_open())
- return false;
- }
- fileList[resID] = filePath;
- return true;
- }
- std::unordered_map<ResourcePath, boost::filesystem::path> CFilesystemLoader::listFiles(const std::string &mountPoint, size_t depth, bool initial) const
- {
- static const EResType initArray[] = {
- EResType::DIRECTORY,
- EResType::JSON,
- EResType::ARCHIVE_LOD,
- EResType::ARCHIVE_VID,
- EResType::ARCHIVE_SND,
- EResType::ARCHIVE_ZIP };
- static const std::set<EResType> initialTypes(initArray, initArray + std::size(initArray));
- std::unordered_map<ResourcePath, boost::filesystem::path> fileList;
- if(!boost::filesystem::is_directory(baseDirectory))
- return fileList;
- std::vector<boost::filesystem::path> path; //vector holding relative path to our file
- boost::filesystem::recursive_directory_iterator enddir;
- #if BOOST_VERSION >= 107200 // 1.72
- boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::directory_options::follow_directory_symlink);
- #else
- boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::symlink_option::recurse);
- #endif
- for(; it != enddir; ++it)
- {
- EResType type;
- #if BOOST_VERSION >= 107200
- const auto currentDepth = it.depth();
- #else
- const auto currentDepth = it.level();
- #endif
- if (boost::filesystem::is_directory(it->status()))
- {
- path.resize(currentDepth + 1);
- path.back() = it->path().filename();
- // don't iterate into directory if depth limit reached
- #if BOOST_VERSION >= 107200
- it.disable_recursion_pending(depth <= currentDepth);
- #else
- it.no_push(depth <= currentDepth);
- #endif
- type = EResType::DIRECTORY;
- }
- else
- type = EResTypeHelper::getTypeFromExtension(it->path().extension().string());
- if (!initial || vstd::contains(initialTypes, type))
- {
- //reconstruct relative filename (not possible via boost AFAIK)
- boost::filesystem::path filename;
- const size_t iterations = std::min(static_cast<size_t>(currentDepth), path.size());
- 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;
- std::string filenameUtf8 = TextOperations::filesystemPathToUtf8(filename);
- if (boost::filesystem::path::preferred_separator != '/')
- {
- // resource names are using UNIX slashes (/)
- resName.reserve(resName.size() + filename.native().size());
- resName = mountPoint;
- for (const char c : filenameUtf8)
- if (c != boost::filesystem::path::preferred_separator)
- resName.push_back(c);
- else
- resName.push_back('/');
- }
- else
- resName = mountPoint + filenameUtf8;
- fileList[ResourcePath(resName, type)] = std::move(filename);
- }
- }
- return fileList;
- }
- std::string CFilesystemLoader::getFullFileURI(const ResourcePath& resourceName) const
- {
- auto filePath = getResourceName(resourceName);
- auto path = boost::filesystem::canonical(*filePath);
- return TextOperations::filesystemPathToUtf8(path);
- }
- std::time_t CFilesystemLoader::getLastWriteTime(const ResourcePath& resourceName) const
- {
- auto resourcePath = getResourceName(resourceName);
- return boost::filesystem::last_write_time(*resourcePath);
- }
- VCMI_LIB_NAMESPACE_END
|