2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "AdapterLoaders.h"
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
#include "Filesystem.h"
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "../json/JsonNode.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
CMappedFileLoader::CMappedFileLoader(const std::string & mountPoint, const JsonNode &config)
|
|
|
|
{
|
|
|
|
for(auto entry : config.Struct())
|
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
//fileList[ResourcePath(mountPoint + entry.first)] = ResourcePath(mountPoint + entry.second.String());
|
|
|
|
fileList.emplace(ResourcePath(mountPoint + entry.first), ResourcePath(mountPoint + entry.second.String()));
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::unique_ptr<CInputStream> CMappedFileLoader::load(const ResourcePath & resourceName) const
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
|
|
|
return CResourceHandler::get()->load(fileList.at(resourceName));
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
bool CMappedFileLoader::existsResource(const ResourcePath & resourceName) const
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
|
|
|
return fileList.count(resourceName) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CMappedFileLoader::getMountPoint() const
|
|
|
|
{
|
|
|
|
return ""; // does not have any meaning with this type of data source
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::optional<boost::filesystem::path> CMappedFileLoader::getResourceName(const ResourcePath & resourceName) const
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
|
|
|
return CResourceHandler::get()->getResourceName(fileList.at(resourceName));
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::unordered_set<ResourcePath> CMappedFileLoader::getFilteredFiles(std::function<bool(const ResourcePath &)> filter) const
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
std::unordered_set<ResourcePath> foundID;
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2023-02-15 00:09:07 +02:00
|
|
|
for(const auto & file : fileList)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
|
|
|
if (filter(file.first))
|
|
|
|
foundID.insert(file.first);
|
|
|
|
}
|
|
|
|
return foundID;
|
|
|
|
}
|
|
|
|
|
2013-07-28 21:57:07 +03:00
|
|
|
CFilesystemList::CFilesystemList()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-29 13:08:02 +03:00
|
|
|
CFilesystemList::~CFilesystemList()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::unique_ptr<CInputStream> CFilesystemList::load(const ResourcePath & resourceName) const
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2024-08-19 23:18:14 +02:00
|
|
|
if(resourceName.getOriginalResource())
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2024-08-19 23:18:14 +02:00
|
|
|
for(const auto & loader : loaders)
|
|
|
|
if (loader->existsResource(resourceName))
|
|
|
|
return loader->load(resourceName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// load resource from last loader that have it (last overridden version)
|
|
|
|
for(const auto & loader : boost::adaptors::reverse(loaders))
|
|
|
|
if (loader->existsResource(resourceName))
|
|
|
|
return loader->load(resourceName);
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("Resource with name " + resourceName.getName() + " and type "
|
|
|
|
+ EResTypeHelper::getEResTypeAsString(resourceName.getType()) + " wasn't found.");
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
bool CFilesystemList::existsResource(const ResourcePath & resourceName) const
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2023-02-15 00:09:07 +02:00
|
|
|
for(const 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 "";
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::optional<boost::filesystem::path> CFilesystemList::getResourceName(const ResourcePath & resourceName) const
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
|
|
|
if (existsResource(resourceName))
|
|
|
|
return getResourcesWithName(resourceName).back()->getResourceName(resourceName);
|
2023-04-16 19:42:56 +02:00
|
|
|
return std::optional<boost::filesystem::path>();
|
2013-07-28 17:49:50 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::set<boost::filesystem::path> CFilesystemList::getResourceNames(const ResourcePath & resourceName) const
|
2016-01-27 10:38:35 +02:00
|
|
|
{
|
2016-01-27 18:32:59 +02:00
|
|
|
std::set<boost::filesystem::path> paths;
|
2016-01-27 10:38:35 +02:00
|
|
|
for(auto& loader : getResourcesWithName(resourceName))
|
|
|
|
{
|
|
|
|
auto rn = loader->getResourceName(resourceName);
|
|
|
|
if(rn)
|
|
|
|
{
|
2016-01-27 22:08:08 +02:00
|
|
|
paths.insert(rn->string());
|
2016-01-27 10:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-19 12:52:02 +02:00
|
|
|
return paths;
|
2016-01-27 10:38:35 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::unordered_set<ResourcePath> CFilesystemList::getFilteredFiles(std::function<bool(const ResourcePath &)> filter) const
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
std::unordered_set<ResourcePath> ret;
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2023-02-15 00:09:07 +02:00
|
|
|
for(const auto & loader : loaders)
|
|
|
|
for(const auto & entry : loader->getFilteredFiles(filter))
|
2013-07-28 17:49:50 +03:00
|
|
|
ret.insert(entry);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-09-04 15:16:00 +02:00
|
|
|
bool CFilesystemList::createResource(const std::string & filename, bool update)
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
2023-09-04 15:16:00 +02:00
|
|
|
logGlobal->trace("Creating %s", 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
|
2023-08-23 14:07:50 +02:00
|
|
|
assert(load(ResourcePath(filename)));
|
2013-07-28 17:49:50 +03:00
|
|
|
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->trace("Resource created successfully");
|
2013-07-28 17:49:50 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->trace("Failed to create resource");
|
2013-07-28 17:49:50 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::vector<const ISimpleResourceLoader *> CFilesystemList::getResourcesWithName(const ResourcePath & resourceName) const
|
2013-07-28 17:49:50 +03:00
|
|
|
{
|
|
|
|
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));
|
2013-07-28 17:49:50 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
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;
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|