2013-06-21 16:49:29 +00:00
|
|
|
#include "ResourceManager.h"
|
|
|
|
#include "../Log.h"
|
2013-08-07 22:40:27 +00:00
|
|
|
#include "../../data/Resources.h"
|
2013-06-21 16:49:29 +00:00
|
|
|
#include <fstream>
|
2013-07-09 05:44:24 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
|
|
auto array_deleter = [](unsigned char* p) { delete[] p; };
|
|
|
|
auto nop_deleter = [](unsigned char* p) { };
|
|
|
|
|
2013-10-04 23:09:54 +00:00
|
|
|
std::shared_ptr<ResourceManager> ResourceManager::sInstance = nullptr;
|
|
|
|
|
|
|
|
ResourceManager::ResourceManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ResourceManager>& ResourceManager::getInstance()
|
|
|
|
{
|
|
|
|
if(!sInstance)
|
|
|
|
sInstance = std::shared_ptr<ResourceManager>(new ResourceManager());
|
|
|
|
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
const ResourceData ResourceManager::getFileData(const std::string& path) const
|
|
|
|
{
|
2013-08-07 22:40:27 +00:00
|
|
|
//check if its embedded
|
|
|
|
|
|
|
|
if(res2hMap.find(path) != res2hMap.end())
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2013-08-07 22:40:27 +00:00
|
|
|
//it is
|
|
|
|
Res2hEntry embeddedEntry = res2hMap.find(path)->second;
|
|
|
|
ResourceData data = {
|
|
|
|
std::shared_ptr<unsigned char>(const_cast<unsigned char*>(embeddedEntry.data), nop_deleter),
|
|
|
|
embeddedEntry.size
|
|
|
|
};
|
|
|
|
return data;
|
2013-06-21 16:49:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
//it's not embedded; load the file
|
|
|
|
if(!fs::exists(path))
|
|
|
|
{
|
|
|
|
//if the file doesn't exist, return an "empty" ResourceData
|
|
|
|
ResourceData data = {NULL, 0};
|
|
|
|
return data;
|
|
|
|
}else{
|
|
|
|
ResourceData data = loadFile(path);
|
|
|
|
return data;
|
|
|
|
}
|
2013-06-21 16:49:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
ResourceData ResourceManager::loadFile(const std::string& path) const
|
2013-07-03 07:54:55 +00:00
|
|
|
{
|
2013-07-09 05:44:24 +00:00
|
|
|
std::ifstream stream(path, std::ios::binary);
|
2013-07-03 07:54:55 +00:00
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
stream.seekg(0, stream.end);
|
|
|
|
size_t size = (size_t)stream.tellg();
|
|
|
|
stream.seekg(0, stream.beg);
|
2013-07-03 07:54:55 +00:00
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
//supply custom deleter to properly free array
|
|
|
|
std::shared_ptr<unsigned char> data(new unsigned char[size], array_deleter);
|
|
|
|
stream.read((char*)data.get(), size);
|
|
|
|
stream.close();
|
2013-07-03 07:54:55 +00:00
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
ResourceData ret = {data, size};
|
|
|
|
return ret;
|
2013-07-03 07:54:55 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
bool ResourceManager::fileExists(const std::string& path) const
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2013-08-07 22:40:27 +00:00
|
|
|
//if it exists as an embedded file, return true
|
|
|
|
if(res2hMap.find(path) != res2hMap.end())
|
|
|
|
return true;
|
2013-07-03 07:54:55 +00:00
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
return fs::exists(path);
|
2013-06-21 16:49:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
void ResourceManager::unloadAll()
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2013-07-09 05:57:28 +00:00
|
|
|
auto iter = mReloadables.begin();
|
|
|
|
while(iter != mReloadables.end())
|
2013-07-03 07:54:55 +00:00
|
|
|
{
|
2013-07-09 05:44:24 +00:00
|
|
|
if(!iter->expired())
|
2013-07-09 05:57:28 +00:00
|
|
|
{
|
2013-10-04 23:09:54 +00:00
|
|
|
iter->lock()->unload(sInstance);
|
2013-07-09 05:57:28 +00:00
|
|
|
iter++;
|
|
|
|
}else{
|
2013-08-23 17:21:22 +00:00
|
|
|
iter = mReloadables.erase(iter);
|
2013-07-09 05:57:28 +00:00
|
|
|
}
|
2013-07-03 07:54:55 +00:00
|
|
|
}
|
2013-06-21 16:49:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
void ResourceManager::reloadAll()
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2013-07-09 05:57:28 +00:00
|
|
|
auto iter = mReloadables.begin();
|
|
|
|
while(iter != mReloadables.end())
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2013-07-09 05:44:24 +00:00
|
|
|
if(!iter->expired())
|
2013-07-09 05:57:28 +00:00
|
|
|
{
|
2013-10-04 23:09:54 +00:00
|
|
|
iter->lock()->reload(sInstance);
|
2013-07-09 05:57:28 +00:00
|
|
|
iter++;
|
|
|
|
}else{
|
2013-08-23 17:21:22 +00:00
|
|
|
iter = mReloadables.erase(iter);
|
2013-07-09 05:57:28 +00:00
|
|
|
}
|
2013-06-21 16:49:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
void ResourceManager::addReloadable(std::weak_ptr<IReloadable> reloadable)
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2013-07-09 05:44:24 +00:00
|
|
|
mReloadables.push_back(reloadable);
|
2013-06-21 16:49:29 +00:00
|
|
|
}
|