2012-07-22 18:02:13 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CFilesystemLoader.h"
|
|
|
|
|
|
|
|
#include "CFileInfo.h"
|
|
|
|
#include "CFileInputStream.h"
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
CFilesystemLoader::CFilesystemLoader(const std::string &mountPoint, const std::string & baseDirectory, size_t depth, bool initial):
|
2012-08-07 14:28:52 +03:00
|
|
|
baseDirectory(baseDirectory),
|
2013-07-28 17:49:50 +03:00
|
|
|
mountPoint(mountPoint),
|
|
|
|
fileList(listFiles(mountPoint, depth, initial))
|
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));
|
|
|
|
|
|
|
|
std::unique_ptr<CInputStream> stream(new CFileInputStream(baseDirectory + '/' + fileList.at(resourceName)));
|
2012-07-22 18:02:13 +03:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
boost::optional<std::string> 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));
|
|
|
|
|
|
|
|
return baseDirectory + '/' + fileList.at(resourceName);
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
2012-08-01 15:02:54 +03: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);
|
|
|
|
} 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())))
|
|
|
|
{
|
|
|
|
logGlobal->traceStream() << "Can't create file: wrong mount point: " << 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
|
|
|
{
|
2013-05-30 19:25:00 +03:00
|
|
|
std::ofstream newfile (baseDirectory + "/" + filename);
|
2013-07-28 17:49:50 +03:00
|
|
|
if (!newfile.good())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fileList[resID] = filename;
|
2012-08-07 14:28:52 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
std::unordered_map<ResourceID, std::string> CFilesystemLoader::listFiles(const std::string &mountPoint, size_t depth, bool initial) const
|
2012-08-07 14:28:52 +03:00
|
|
|
{
|
2012-09-17 20:25:54 +03:00
|
|
|
std::set<EResType::Type> initialTypes;
|
|
|
|
initialTypes.insert(EResType::DIRECTORY);
|
|
|
|
initialTypes.insert(EResType::TEXT);
|
|
|
|
initialTypes.insert(EResType::ARCHIVE_LOD);
|
|
|
|
initialTypes.insert(EResType::ARCHIVE_VID);
|
|
|
|
initialTypes.insert(EResType::ARCHIVE_SND);
|
|
|
|
|
2012-08-07 14:28:52 +03:00
|
|
|
assert(boost::filesystem::is_directory(baseDirectory));
|
2013-06-29 16:05:48 +03:00
|
|
|
std::unordered_map<ResourceID, std::string> fileList;
|
2012-08-07 14:28:52 +03:00
|
|
|
|
|
|
|
std::vector<std::string> path;//vector holding relative path to our file
|
|
|
|
|
|
|
|
boost::filesystem::recursive_directory_iterator enddir;
|
|
|
|
boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::symlink_option::recurse);
|
|
|
|
|
|
|
|
for(; it != enddir; ++it)
|
|
|
|
{
|
|
|
|
EResType::Type type;
|
|
|
|
|
|
|
|
if (boost::filesystem::is_directory(it->status()))
|
|
|
|
{
|
|
|
|
path.resize(it.level()+1);
|
|
|
|
path.back() = it->path().leaf().string();
|
2013-07-28 17:49:50 +03:00
|
|
|
// don't iterate into directory if depth limit reached OR into hidden directories (like .svn)
|
|
|
|
it.no_push(depth <= it.level() || it->path().leaf().string()[0] == '.');
|
2012-08-07 14:28:52 +03:00
|
|
|
|
|
|
|
type = EResType::DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
type = EResTypeHelper::getTypeFromExtension(boost::filesystem::extension(*it));
|
|
|
|
|
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)
|
|
|
|
std::string filename;
|
|
|
|
for (size_t i=0; i<it.level() && i<path.size(); i++)
|
|
|
|
filename += path[i] + '/';
|
|
|
|
filename += it->path().leaf().string();
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
fileList[ResourceID(mountPoint + filename, type)] = filename;
|
2012-08-07 14:28:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileList;
|
2013-06-29 16:05:48 +03:00
|
|
|
}
|