mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-09 01:06:07 +02:00
large filesystem update. Filesysytem is now fully functional, everything should work.
- completely replaced CLodHandler, removed bitmaph and spriteh - replaced CLodStream in favour of CCompressedStream (2 new files) - renamed CResourceLoaderFactory and ResourceIndetifier to shorter names NOTES: - campaign loading is currently broken. Will fix. - I am going to remove several unused files in several days (e.g. LodHandler)
This commit is contained in:
@ -2,36 +2,69 @@
|
||||
#include "CResourceLoader.h"
|
||||
#include "CFileInfo.h"
|
||||
#include "CLodArchiveLoader.h"
|
||||
#include "CFilesystemLoader.h"
|
||||
|
||||
CResourceLoader * CResourceLoaderFactory::resourceLoader = nullptr;
|
||||
//For filesystem initialization
|
||||
#include "../JsonNode.h"
|
||||
#include "../GameConstants.h"
|
||||
#include "../VCMIDirs.h"
|
||||
|
||||
ResourceIdentifier::ResourceIdentifier()
|
||||
//experimental support for ERA-style mods. Requires custom config in mod directory
|
||||
#define ENABLE_ERA_FILESYSTEM
|
||||
|
||||
CResourceLoader * CResourceHandler::resourceLoader = nullptr;
|
||||
CResourceLoader * CResourceHandler::initialLoader = nullptr;
|
||||
|
||||
ResourceID::ResourceID()
|
||||
:type(EResType::OTHER)
|
||||
{
|
||||
}
|
||||
|
||||
ResourceIdentifier::ResourceIdentifier(const std::string & name, EResType type) : name(name), type(type)
|
||||
ResourceID::ResourceID(const std::string & name)
|
||||
{
|
||||
boost::to_upper(this->name);
|
||||
CFileInfo info(name);
|
||||
setName(info.getStem());
|
||||
setType(info.getType());
|
||||
}
|
||||
|
||||
std::string ResourceIdentifier::getName() const
|
||||
ResourceID::ResourceID(const std::string & name, EResType::Type type)
|
||||
{
|
||||
setName(name);
|
||||
setType(type);
|
||||
}
|
||||
|
||||
ResourceID::ResourceID(const std::string & prefix, const std::string & name, EResType::Type type)
|
||||
{
|
||||
setName(name);
|
||||
this->name = prefix + this->name;
|
||||
|
||||
setType(type);
|
||||
}
|
||||
|
||||
std::string ResourceID::getName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
EResType ResourceIdentifier::getType() const
|
||||
EResType::Type ResourceID::getType() const
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
void ResourceIdentifier::setName(const std::string & name)
|
||||
void ResourceID::setName(const std::string & name)
|
||||
{
|
||||
this->name = name;
|
||||
|
||||
size_t dotPos = this->name.find_last_of("/.");
|
||||
|
||||
if(dotPos != std::string::npos && this->name[dotPos] == '.')
|
||||
this->name.erase(dotPos);
|
||||
|
||||
// strangely enough but this line takes 40-50% of filesystem loading time
|
||||
boost::to_upper(this->name);
|
||||
}
|
||||
|
||||
void ResourceIdentifier::setType(EResType type)
|
||||
void ResourceID::setType(EResType::Type type)
|
||||
{
|
||||
this->type = type;
|
||||
}
|
||||
@ -43,13 +76,13 @@ CResourceLoader::CResourceLoader()
|
||||
CResourceLoader::~CResourceLoader()
|
||||
{
|
||||
// Delete all loader objects
|
||||
BOOST_FOREACH ( ISimpleResourceLoader* it, loaders)
|
||||
BOOST_FOREACH ( auto & it, loaders)
|
||||
{
|
||||
delete it;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<CInputStream> CResourceLoader::load(const ResourceIdentifier & resourceIdent) const
|
||||
std::unique_ptr<CInputStream> CResourceLoader::load(const ResourceID & resourceIdent) const
|
||||
{
|
||||
auto resource = resources.find(resourceIdent);
|
||||
|
||||
@ -66,31 +99,60 @@ std::unique_ptr<CInputStream> CResourceLoader::load(const ResourceIdentifier & r
|
||||
return locator.getLoader()->load(locator.getResourceName());
|
||||
}
|
||||
|
||||
bool CResourceLoader::existsResource(const ResourceIdentifier & resourceIdent) const
|
||||
std::pair<std::unique_ptr<ui8[]>, ui64> CResourceLoader::loadData(const ResourceID & resourceIdent) const
|
||||
{
|
||||
auto stream = load(resourceIdent);
|
||||
std::unique_ptr<ui8[]> data(new ui8[stream->getSize()]);
|
||||
size_t readSize = stream->read(data.get(), stream->getSize());
|
||||
|
||||
assert(readSize == stream->getSize());
|
||||
return std::make_pair(std::move(data), stream->getSize());
|
||||
}
|
||||
|
||||
ResourceLocator CResourceLoader::getResource(const ResourceID & resourceIdent) const
|
||||
{
|
||||
auto resource = resources.find(resourceIdent);
|
||||
|
||||
if (resource == resources.end())
|
||||
return ResourceLocator(nullptr, "");
|
||||
return resource->second.back();
|
||||
}
|
||||
|
||||
std::string CResourceLoader::getResourceName(const ResourceID & resourceIdent) const
|
||||
{
|
||||
auto locator = getResource(resourceIdent);
|
||||
if (locator.getLoader())
|
||||
return locator.getLoader()->getOrigin() + '/' + locator.getResourceName();
|
||||
return "";
|
||||
}
|
||||
|
||||
bool CResourceLoader::existsResource(const ResourceID & resourceIdent) const
|
||||
{
|
||||
// Check if resource is registered
|
||||
return resources.find(resourceIdent) != resources.end();
|
||||
}
|
||||
|
||||
void CResourceLoader::addLoader(ISimpleResourceLoader * loader)
|
||||
void CResourceLoader::addLoader(std::string mountPoint, ISimpleResourceLoader * loader)
|
||||
{
|
||||
loaders.push_back(loader);
|
||||
loaders.insert(loader);
|
||||
|
||||
// Get entries and add them to the resources list
|
||||
const std::list<std::string> & entries = loader->getEntries();
|
||||
|
||||
boost::to_upper(mountPoint);
|
||||
|
||||
BOOST_FOREACH (const std::string & entry, entries)
|
||||
{
|
||||
CFileInfo file(entry);
|
||||
|
||||
// Create identifier and locator and add them to the resources list
|
||||
ResourceIdentifier ident(file.getStem(), file.getType());
|
||||
ResourceID ident(mountPoint, file.getStem(), file.getType());
|
||||
ResourceLocator locator(loader, entry);
|
||||
resources[ident].push_back(locator);
|
||||
}
|
||||
}
|
||||
|
||||
CResourceLoader * CResourceLoaderFactory::getInstance()
|
||||
CResourceLoader * CResourceHandler::get()
|
||||
{
|
||||
if(resourceLoader != nullptr)
|
||||
{
|
||||
@ -105,10 +167,10 @@ CResourceLoader * CResourceLoaderFactory::getInstance()
|
||||
}
|
||||
}
|
||||
|
||||
void CResourceLoaderFactory::setInstance(CResourceLoader * resourceLoader)
|
||||
{
|
||||
CResourceLoaderFactory::resourceLoader = resourceLoader;
|
||||
}
|
||||
//void CResourceLoaderFactory::setInstance(CResourceLoader * resourceLoader)
|
||||
//{
|
||||
// CResourceLoaderFactory::resourceLoader = resourceLoader;
|
||||
//}
|
||||
|
||||
ResourceLocator::ResourceLocator(ISimpleResourceLoader * loader, const std::string & resourceName)
|
||||
: loader(loader), resourceName(resourceName)
|
||||
@ -126,11 +188,11 @@ std::string ResourceLocator::getResourceName() const
|
||||
return resourceName;
|
||||
}
|
||||
|
||||
EResType EResTypeHelper::getTypeFromExtension(std::string extension)
|
||||
EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
|
||||
{
|
||||
boost::to_upper(extension);
|
||||
|
||||
static const std::map<std::string, EResType> stringToRes =
|
||||
static const std::map<std::string, EResType::Type> stringToRes =
|
||||
boost::assign::map_list_of
|
||||
(".TXT", EResType::TEXT)
|
||||
(".JSON", EResType::TEXT)
|
||||
@ -146,12 +208,14 @@ EResType EResTypeHelper::getTypeFromExtension(std::string extension)
|
||||
(".PNG", EResType::IMAGE)
|
||||
(".TGA", EResType::IMAGE)
|
||||
(".WAV", EResType::SOUND)
|
||||
(".82M", EResType::SOUND)
|
||||
(".SMK", EResType::VIDEO)
|
||||
(".BIK", EResType::VIDEO)
|
||||
(".MJPG", EResType::VIDEO)
|
||||
(".MP3", EResType::MUSIC)
|
||||
(".OGG", EResType::MUSIC)
|
||||
(".LOD", EResType::ARCHIVE)
|
||||
(".PAC", EResType::ARCHIVE)
|
||||
(".VID", EResType::ARCHIVE)
|
||||
(".SND", EResType::ARCHIVE)
|
||||
(".VCGM1", EResType::CLIENT_SAVEGAME)
|
||||
@ -164,13 +228,11 @@ EResType EResTypeHelper::getTypeFromExtension(std::string extension)
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
std::string EResTypeHelper::getEResTypeAsString(EResType type)
|
||||
std::string EResTypeHelper::getEResTypeAsString(EResType::Type type)
|
||||
{
|
||||
#define MAP_ENUM(value) (EResType::value, #value)
|
||||
|
||||
#define MAP_ENUM(value) (EResType::value, "value")
|
||||
|
||||
static const std::map<EResType, std::string> stringToRes = boost::assign::map_list_of
|
||||
MAP_ENUM(ANY)
|
||||
static const std::map<EResType::Type, std::string> stringToRes = boost::assign::map_list_of
|
||||
MAP_ENUM(TEXT)
|
||||
MAP_ENUM(ANIMATION)
|
||||
MAP_ENUM(MASK)
|
||||
@ -193,5 +255,80 @@ std::string EResTypeHelper::getEResTypeAsString(EResType type)
|
||||
assert(iter != stringToRes.end());
|
||||
|
||||
return iter->second;
|
||||
|
||||
}
|
||||
|
||||
void CResourceHandler::initialize()
|
||||
{
|
||||
//temporary filesystem that will be used to initialize main one.
|
||||
//used to solve several case-sensivity issues like Mp3 vs MP3
|
||||
initialLoader = new CResourceLoader;
|
||||
resourceLoader = new CResourceLoader;
|
||||
|
||||
auto rootDir = new CFilesystemLoader(GameConstants::DATA_DIR, 3);
|
||||
initialLoader->addLoader("GLOBAL/", rootDir);
|
||||
initialLoader->addLoader("ALL/", rootDir);
|
||||
|
||||
auto userDir = rootDir;
|
||||
|
||||
//add local directory to "ALL" but only if it differs from root dir (true for linux)
|
||||
if (GameConstants::DATA_DIR != GVCMIDirs.UserPath)
|
||||
{
|
||||
userDir = new CFilesystemLoader(GVCMIDirs.UserPath, 3);
|
||||
initialLoader->addLoader("ALL/", userDir);
|
||||
}
|
||||
|
||||
//create "LOCAL" dir with current userDir (may be same as rootDir)
|
||||
initialLoader->addLoader("LOCAL/", userDir);
|
||||
}
|
||||
|
||||
void CResourceHandler::loadFileSystem(const std::string fsConfigURI)
|
||||
{
|
||||
const JsonNode fsConfig(initialLoader->getResourceName(ResourceID(fsConfigURI, EResType::TEXT)));
|
||||
|
||||
BOOST_FOREACH(auto & mountPoint, fsConfig["filesystem"].Struct())
|
||||
{
|
||||
BOOST_FOREACH(auto & entry, mountPoint.second.Vector())
|
||||
{
|
||||
tlog5 << "loading resource at " << entry["path"].String() << ": ";
|
||||
std::string filename = initialLoader->getResourceName(entry["path"].String());
|
||||
|
||||
if (!filename.empty())
|
||||
{
|
||||
if (entry["type"].String() == "dir")
|
||||
{
|
||||
int depth = 16;
|
||||
if (!entry["depth"].isNull())
|
||||
depth = entry["depth"].Float();
|
||||
resourceLoader->addLoader(mountPoint.first, new CFilesystemLoader(filename, depth));
|
||||
}
|
||||
|
||||
if (entry["type"].String() == "file")
|
||||
resourceLoader->addLoader(mountPoint.first, new CLodArchiveLoader(filename));
|
||||
|
||||
tlog5 << "OK\n";
|
||||
}
|
||||
else
|
||||
tlog5 << "Not found\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CResourceHandler::loadModsFilesystems()
|
||||
{
|
||||
#ifdef ENABLE_ERA_FILESYSTEM
|
||||
|
||||
auto iterator = initialLoader->getIterator([](const ResourceID & ident)
|
||||
{
|
||||
return ident.getType() == EResType::TEXT
|
||||
&& boost::algorithm::starts_with(ident.getName(), "ALL/MODS/")
|
||||
&& boost::algorithm::ends_with(ident.getName(), "FILESYSTEM");
|
||||
});
|
||||
|
||||
while (iterator.hasNext())
|
||||
{
|
||||
tlog1 << "Found mod filesystem: " << iterator->getName() << "\n";
|
||||
loadFileSystem(iterator->getName());
|
||||
++iterator;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user