2012-07-22 18:02:13 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CResourceLoader.h"
|
|
|
|
#include "CFileInfo.h"
|
|
|
|
#include "CLodArchiveLoader.h"
|
|
|
|
|
|
|
|
CResourceLoader * CResourceLoaderFactory::resourceLoader = nullptr;
|
|
|
|
|
2012-07-23 13:23:43 +03:00
|
|
|
ResourceIdentifier::ResourceIdentifier()
|
|
|
|
:type(EResType::OTHER)
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceIdentifier::ResourceIdentifier(const std::string & name, EResType type) : name(name), type(type)
|
|
|
|
{
|
|
|
|
boost::to_upper(this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ResourceIdentifier::getName() const
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
EResType ResourceIdentifier::getType() const
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceIdentifier::setName(const std::string & name)
|
|
|
|
{
|
|
|
|
this->name = name;
|
|
|
|
boost::to_upper(this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceIdentifier::setType(EResType type)
|
|
|
|
{
|
|
|
|
this->type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
CResourceLoader::CResourceLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CResourceLoader::~CResourceLoader()
|
|
|
|
{
|
|
|
|
// Delete all loader objects
|
2012-07-23 13:23:43 +03:00
|
|
|
BOOST_FOREACH ( ISimpleResourceLoader* it, loaders)
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
2012-07-23 13:23:43 +03:00
|
|
|
delete it;
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CInputStream> CResourceLoader::load(const ResourceIdentifier & resourceIdent) const
|
|
|
|
{
|
2012-07-23 13:23:43 +03:00
|
|
|
auto resource = resources.find(resourceIdent);
|
|
|
|
|
|
|
|
if(resource == resources.end())
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Resource with name " + resourceIdent.getName() + " and type "
|
|
|
|
+ EResTypeHelper::getEResTypeAsString(resourceIdent.getType()) + " wasn't found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the last added resource(most overriden)
|
2012-07-23 13:23:43 +03:00
|
|
|
const ResourceLocator & locator = resource->second.back();
|
2012-07-22 18:02:13 +03:00
|
|
|
|
|
|
|
// load the resource and return it
|
|
|
|
return locator.getLoader()->load(locator.getResourceName());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CResourceLoader::existsResource(const ResourceIdentifier & resourceIdent) const
|
|
|
|
{
|
|
|
|
// Check if resource is registered
|
2012-07-23 13:23:43 +03:00
|
|
|
return resources.find(resourceIdent) != resources.end();
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CResourceLoader::addLoader(ISimpleResourceLoader * loader)
|
|
|
|
{
|
|
|
|
loaders.push_back(loader);
|
|
|
|
|
|
|
|
// Get entries and add them to the resources list
|
|
|
|
const std::list<std::string> & entries = loader->getEntries();
|
|
|
|
|
2012-07-23 13:23:43 +03:00
|
|
|
BOOST_FOREACH (const std::string & entry, entries)
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
|
|
|
CFileInfo file(entry);
|
|
|
|
|
|
|
|
// Create identifier and locator and add them to the resources list
|
|
|
|
ResourceIdentifier ident(file.getStem(), file.getType());
|
|
|
|
ResourceLocator locator(loader, entry);
|
|
|
|
resources[ident].push_back(locator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CResourceLoader * CResourceLoaderFactory::getInstance()
|
|
|
|
{
|
|
|
|
if(resourceLoader != nullptr)
|
|
|
|
{
|
|
|
|
return resourceLoader;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::stringstream string;
|
|
|
|
string << "Error: Resource loader wasn't initialized. "
|
|
|
|
<< "Make sure that you set one via CResourceLoaderFactory::setInstance";
|
|
|
|
throw std::runtime_error(string.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CResourceLoaderFactory::setInstance(CResourceLoader * resourceLoader)
|
|
|
|
{
|
|
|
|
CResourceLoaderFactory::resourceLoader = resourceLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceLocator::ResourceLocator(ISimpleResourceLoader * loader, const std::string & resourceName)
|
|
|
|
: loader(loader), resourceName(resourceName)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ISimpleResourceLoader * ResourceLocator::getLoader() const
|
|
|
|
{
|
|
|
|
return loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ResourceLocator::getResourceName() const
|
|
|
|
{
|
|
|
|
return resourceName;
|
|
|
|
}
|
|
|
|
|
|
|
|
EResType EResTypeHelper::getTypeFromExtension(std::string extension)
|
|
|
|
{
|
|
|
|
boost::to_upper(extension);
|
|
|
|
|
2012-07-23 13:23:43 +03:00
|
|
|
static const std::map<std::string, EResType> stringToRes =
|
|
|
|
boost::assign::map_list_of
|
|
|
|
(".TXT", EResType::TEXT)
|
|
|
|
(".JSON", EResType::TEXT)
|
|
|
|
(".DEF", EResType::ANIMATION)
|
|
|
|
(".MSK", EResType::MASK)
|
|
|
|
(".MSG", EResType::MASK)
|
|
|
|
(".H3C", EResType::CAMPAIGN)
|
|
|
|
(".H3M", EResType::MAP)
|
|
|
|
(".FNT", EResType::FONT)
|
|
|
|
(".BMP", EResType::IMAGE)
|
|
|
|
(".JPG", EResType::IMAGE)
|
|
|
|
(".PCX", EResType::IMAGE)
|
|
|
|
(".PNG", EResType::IMAGE)
|
|
|
|
(".TGA", EResType::IMAGE)
|
|
|
|
(".WAV", EResType::SOUND)
|
|
|
|
(".SMK", EResType::VIDEO)
|
|
|
|
(".BIK", EResType::VIDEO)
|
|
|
|
(".MJPG", EResType::VIDEO)
|
|
|
|
(".MP3", EResType::MUSIC)
|
|
|
|
(".OGG", EResType::MUSIC)
|
|
|
|
(".LOD", EResType::ARCHIVE)
|
|
|
|
(".VID", EResType::ARCHIVE)
|
|
|
|
(".SND", EResType::ARCHIVE)
|
|
|
|
(".VCGM1", EResType::CLIENT_SAVEGAME)
|
|
|
|
(".VLGM1", EResType::LIB_SAVEGAME)
|
|
|
|
(".VSGM1", EResType::SERVER_SAVEGAME);
|
|
|
|
|
|
|
|
auto iter = stringToRes.find(extension);
|
|
|
|
if (iter == stringToRes.end())
|
2012-07-22 18:02:13 +03:00
|
|
|
return EResType::OTHER;
|
2012-07-23 13:23:43 +03:00
|
|
|
return iter->second;
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string EResTypeHelper::getEResTypeAsString(EResType type)
|
|
|
|
{
|
|
|
|
|
2012-07-23 13:23:43 +03:00
|
|
|
#define MAP_ENUM(value) (EResType::value, "value")
|
|
|
|
|
|
|
|
static const std::map<EResType, std::string> stringToRes = boost::assign::map_list_of
|
|
|
|
MAP_ENUM(ANY)
|
|
|
|
MAP_ENUM(TEXT)
|
|
|
|
MAP_ENUM(ANIMATION)
|
|
|
|
MAP_ENUM(MASK)
|
|
|
|
MAP_ENUM(CAMPAIGN)
|
|
|
|
MAP_ENUM(MAP)
|
|
|
|
MAP_ENUM(FONT)
|
|
|
|
MAP_ENUM(IMAGE)
|
|
|
|
MAP_ENUM(VIDEO)
|
|
|
|
MAP_ENUM(SOUND)
|
|
|
|
MAP_ENUM(MUSIC)
|
|
|
|
MAP_ENUM(ARCHIVE)
|
|
|
|
MAP_ENUM(CLIENT_SAVEGAME)
|
|
|
|
MAP_ENUM(LIB_SAVEGAME)
|
|
|
|
MAP_ENUM(SERVER_SAVEGAME)
|
|
|
|
MAP_ENUM(OTHER);
|
|
|
|
|
|
|
|
#undef MAP_ENUM
|
|
|
|
|
|
|
|
auto iter = stringToRes.find(type);
|
|
|
|
assert(iter != stringToRes.end());
|
|
|
|
|
|
|
|
return iter->second;
|
|
|
|
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|