1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00
vcmi/lib/filesystem/AdapterLoaders.cpp

184 lines
5.0 KiB
C++
Raw Normal View History

/*
* AdapterLoaders.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 "AdapterLoaders.h"
#include "../JsonNode.h"
#include "Filesystem.h"
VCMI_LIB_NAMESPACE_BEGIN
CMappedFileLoader::CMappedFileLoader(const std::string & mountPoint, const JsonNode &config)
{
for(auto entry : config.Struct())
{
2016-01-29 16:16:14 +02:00
//fileList[ResourceID(mountPoint + entry.first)] = ResourceID(mountPoint + entry.second.String());
fileList.emplace(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
}
2023-04-16 19:42:56 +02:00
std::optional<boost::filesystem::path> 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;
2023-02-15 00:09:07 +02:00
for(const auto & file : fileList)
{
if (filter(file.first))
foundID.insert(file.first);
}
return foundID;
}
CFilesystemList::CFilesystemList()
{
}
CFilesystemList::~CFilesystemList()
{
}
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)
2023-02-15 00:09:07 +02:00
for(const auto & loader : boost::adaptors::reverse(loaders))
{
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
{
2023-02-15 00:09:07 +02:00
for(const auto & loader : loaders)
if (loader->existsResource(resourceName))
return true;
return false;
}
std::string CFilesystemList::getMountPoint() const
{
return "";
}
2023-04-16 19:42:56 +02:00
std::optional<boost::filesystem::path> CFilesystemList::getResourceName(const ResourceID & resourceName) const
{
if (existsResource(resourceName))
return getResourcesWithName(resourceName).back()->getResourceName(resourceName);
2023-04-16 19:42:56 +02:00
return std::optional<boost::filesystem::path>();
}
2016-01-27 18:32:59 +02:00
std::set<boost::filesystem::path> CFilesystemList::getResourceNames(const ResourceID & resourceName) const
{
2016-01-27 18:32:59 +02:00
std::set<boost::filesystem::path> paths;
for(auto& loader : getResourcesWithName(resourceName))
{
auto rn = loader->getResourceName(resourceName);
if(rn)
{
paths.insert(rn->string());
}
}
return paths;
}
2016-10-16 22:09:57 +02:00
void CFilesystemList::updateFilteredFiles(std::function<bool(const std::string &)> filter) const
{
2023-02-15 00:09:07 +02:00
for(const auto & loader : loaders)
2016-10-16 22:09:57 +02:00
loader->updateFilteredFiles(filter);
}
std::unordered_set<ResourceID> CFilesystemList::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
{
std::unordered_set<ResourceID> ret;
2023-02-15 00:09:07 +02:00
for(const auto & loader : loaders)
for(const auto & entry : loader->getFilteredFiles(filter))
ret.insert(entry);
return ret;
}
bool CFilesystemList::createResource(std::string filename, bool update)
{
logGlobal->trace("Creating %s", filename);
for (auto & loader : boost::adaptors::reverse(loaders))
{
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)));
2017-08-10 18:39:27 +02:00
logGlobal->trace("Resource created successfully");
return true;
}
}
2017-08-10 18:39:27 +02:00
logGlobal->trace("Failed to create resource");
return false;
}
std::vector<const ISimpleResourceLoader *> CFilesystemList::getResourcesWithName(const ResourceID & resourceName) const
{
std::vector<const ISimpleResourceLoader *> ret;
2023-02-15 00:09:07 +02:00
for(const auto & loader : loaders)
2022-08-15 11:18:52 +02:00
boost::range::copy(loader->getResourcesWithName(resourceName), std::back_inserter(ret));
return ret;
}
void CFilesystemList::addLoader(ISimpleResourceLoader * loader, bool writeable)
{
loaders.push_back(std::unique_ptr<ISimpleResourceLoader>(loader));
if (writeable)
writeableLoaders.insert(loader);
}
2022-09-17 13:04:01 +02:00
bool CFilesystemList::removeLoader(ISimpleResourceLoader * loader)
{
for(auto loaderIterator = loaders.begin(); loaderIterator != loaders.end(); ++loaderIterator)
{
if(loaderIterator->get() == loader)
{
loaders.erase(loaderIterator);
writeableLoaders.erase(loader);
return true;
}
}
return false;
}
VCMI_LIB_NAMESPACE_END