2013-07-28 17:49:50 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "AdapterLoaders.h"
|
|
|
|
|
|
|
|
#include "../JsonNode.h"
|
2013-11-08 23:36:26 +03:00
|
|
|
#include "Filesystem.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
|
|
|
|
CMappedFileLoader::CMappedFileLoader(const std::string & mountPoint, const JsonNode &config)
|
|
|
|
{
|
|
|
|
for(auto entry : config.Struct())
|
|
|
|
{
|
|
|
|
fileList[ResourceID(mountPoint + entry.first)] = ResourceID(mountPoint + entry.second.String());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CInputStream> CMappedFileLoader::load(const ResourceID & resourceName) const
|
|
|
|
{
|
|
|
|
return CResourceHandler::get()->load(fileList.at(resourceName));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CMappedFileLoader::existsResource(const ResourceID & resourceName) const
|
|
|
|
{
|
|
|
|
return fileList.count(resourceName) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CMappedFileLoader::getMountPoint() const
|
|
|
|
{
|
|
|
|
return ""; // does not have any meaning with this type of data source
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<std::string> CMappedFileLoader::getResourceName(const ResourceID & resourceName) const
|
|
|
|
{
|
|
|
|
return CResourceHandler::get()->getResourceName(fileList.at(resourceName));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_set<ResourceID> CMappedFileLoader::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
|
|
|
|
{
|
|
|
|
std::unordered_set<ResourceID> foundID;
|
|
|
|
|
|
|
|
for (auto & file : fileList)
|
|
|
|
{
|
|
|
|
if (filter(file.first))
|
|
|
|
foundID.insert(file.first);
|
|
|
|
}
|
|
|
|
return foundID;
|
|
|
|
}
|
|
|
|
|
2013-07-28 21:57:07 +03:00
|
|
|
CFilesystemList::CFilesystemList()
|
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
//loaders = new std::vector<std::unique_ptr<ISimpleResourceLoader> >;
|
2013-07-28 21:57:07 +03:00
|
|
|
}
|
|
|
|
|
2013-07-29 13:08:02 +03:00
|
|
|
CFilesystemList::~CFilesystemList()
|
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
//delete loaders;
|
2013-07-29 13:08:02 +03:00
|
|
|
}
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
std::unique_ptr<CInputStream> CFilesystemList::load(const ResourceID & resourceName) const
|
|
|
|
{
|
2014-05-21 12:02:20 +03:00
|
|
|
// load resource from last loader that have it (last overridden version)
|
2013-12-29 14:27:38 +03:00
|
|
|
for (auto & loader : boost::adaptors::reverse(loaders))
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
|
|
|
if (loader->existsResource(resourceName))
|
|
|
|
return loader->load(resourceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("Resource with name " + resourceName.getName() + " and type "
|
|
|
|
+ EResTypeHelper::getEResTypeAsString(resourceName.getType()) + " wasn't found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFilesystemList::existsResource(const ResourceID & resourceName) const
|
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
for (auto & loader : loaders)
|
2013-11-14 16:21:09 +03:00
|
|
|
if (loader->existsResource(resourceName))
|
|
|
|
return true;
|
|
|
|
return false;
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CFilesystemList::getMountPoint() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<std::string> CFilesystemList::getResourceName(const ResourceID & resourceName) const
|
|
|
|
{
|
|
|
|
if (existsResource(resourceName))
|
|
|
|
return getResourcesWithName(resourceName).back()->getResourceName(resourceName);
|
|
|
|
return boost::optional<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_set<ResourceID> CFilesystemList::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
|
|
|
|
{
|
|
|
|
std::unordered_set<ResourceID> ret;
|
|
|
|
|
2013-12-29 14:27:38 +03:00
|
|
|
for (auto & loader : loaders)
|
2013-07-28 17:49:50 +03:00
|
|
|
for (auto & entry : loader->getFilteredFiles(filter))
|
|
|
|
ret.insert(entry);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFilesystemList::createResource(std::string filename, bool update)
|
|
|
|
{
|
|
|
|
logGlobal->traceStream()<< "Creating " << filename;
|
2013-12-29 14:27:38 +03:00
|
|
|
for (auto & loader : boost::adaptors::reverse(loaders))
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
|
|
|
if (writeableLoaders.count(loader.get()) != 0 // writeable,
|
|
|
|
&& loader->createResource(filename, update)) // successfully created
|
|
|
|
{
|
|
|
|
// Check if resource was created successfully. Possible reasons for this to fail
|
|
|
|
// a) loader failed to create resource (e.g. read-only FS)
|
|
|
|
// b) in update mode, call with filename that does not exists
|
|
|
|
assert(load(ResourceID(filename)));
|
|
|
|
|
|
|
|
logGlobal->traceStream()<< "Resource created successfully";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logGlobal->traceStream()<< "Failed to create resource";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const ISimpleResourceLoader *> CFilesystemList::getResourcesWithName(const ResourceID & resourceName) const
|
|
|
|
{
|
|
|
|
std::vector<const ISimpleResourceLoader *> ret;
|
|
|
|
|
2013-12-29 14:27:38 +03:00
|
|
|
for (auto & loader : loaders)
|
2013-07-28 17:49:50 +03:00
|
|
|
boost::range::copy(loader->getResourcesWithName(resourceName), std::back_inserter(ret));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFilesystemList::addLoader(ISimpleResourceLoader * loader, bool writeable)
|
|
|
|
{
|
2013-12-29 14:27:38 +03:00
|
|
|
loaders.push_back(std::unique_ptr<ISimpleResourceLoader>(loader));
|
2013-07-28 17:49:50 +03:00
|
|
|
if (writeable)
|
|
|
|
writeableLoaders.insert(loader);
|
|
|
|
}
|