2013-06-21 16:49:29 +00:00
|
|
|
#include "ResourceManager.h"
|
|
|
|
#include "../Log.h"
|
|
|
|
#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-06-21 16:49:29 +00:00
|
|
|
|
|
|
|
//from ES_logo_16.cpp
|
|
|
|
extern const size_t es_logo_16_data_len;
|
|
|
|
extern const unsigned char es_logo_16_data[];
|
|
|
|
|
|
|
|
//from ES_logo_32.cpp
|
|
|
|
extern const size_t es_logo_32_data_len;
|
|
|
|
extern const unsigned char es_logo_32_data[];
|
|
|
|
|
|
|
|
struct EmbeddedResource
|
|
|
|
{
|
|
|
|
const char* internal_path;
|
|
|
|
ResourceData resourceData;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int embedded_resource_count = 2;
|
|
|
|
static const EmbeddedResource embedded_resources[embedded_resource_count] = {
|
2013-07-09 05:44:24 +00:00
|
|
|
{ "internal://es_logo_16.png", {std::shared_ptr<unsigned char>((unsigned char*)es_logo_16_data, nop_deleter), es_logo_16_data_len} },
|
|
|
|
{ "internal://es_logo_32.png", {std::shared_ptr<unsigned char>((unsigned char*)es_logo_32_data, nop_deleter), es_logo_32_data_len} }
|
2013-06-21 16:49:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
const ResourceData ResourceManager::getFileData(const std::string& path) const
|
|
|
|
{
|
|
|
|
for(int i = 0; i < embedded_resource_count; i++)
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2013-07-09 05:44:24 +00:00
|
|
|
if(strcmp(embedded_resources[i].internal_path, path.c_str()) == 0)
|
|
|
|
{
|
|
|
|
//this embedded resource matches the filepath; use it
|
|
|
|
return embedded_resources[i].resourceData;
|
|
|
|
}
|
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-07-09 05:44:24 +00:00
|
|
|
for(int i = 0; i < embedded_resource_count; i++)
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2013-07-09 05:44:24 +00:00
|
|
|
if(strcmp(embedded_resources[i].internal_path, path.c_str()) == 0)
|
|
|
|
{
|
|
|
|
//this embedded resource matches the filepath
|
|
|
|
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:44:24 +00:00
|
|
|
for(auto iter = mReloadables.begin(); iter != mReloadables.end(); iter++)
|
2013-07-03 07:54:55 +00:00
|
|
|
{
|
2013-07-09 05:44:24 +00:00
|
|
|
if(!iter->expired())
|
|
|
|
iter->lock()->unload(*this);
|
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:44:24 +00:00
|
|
|
for(auto iter = mReloadables.begin(); iter != mReloadables.end(); iter++)
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
2013-07-09 05:44:24 +00:00
|
|
|
if(!iter->expired())
|
|
|
|
iter->lock()->reload(*this);
|
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
|
|
|
}
|