2020-06-21 12:25:28 +00:00
|
|
|
//
|
|
|
|
// ResourceManager.cpp
|
|
|
|
//
|
|
|
|
// Handles the application resources (fonts, graphics, sounds etc.).
|
|
|
|
// Loading and unloading of these files are done here.
|
|
|
|
//
|
|
|
|
|
2013-06-21 16:49:29 +00:00
|
|
|
#include "ResourceManager.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
2013-06-21 16:49:29 +00:00
|
|
|
#include <fstream>
|
2013-07-09 05:44:24 +00:00
|
|
|
|
|
|
|
auto array_deleter = [](unsigned char* p) { delete[] p; };
|
2017-11-17 14:58:52 +00:00
|
|
|
auto nop_deleter = [](unsigned char* /*p*/) { };
|
2013-07-09 05:44:24 +00:00
|
|
|
|
2013-10-04 23:09:54 +00:00
|
|
|
std::shared_ptr<ResourceManager> ResourceManager::sInstance = nullptr;
|
|
|
|
|
|
|
|
ResourceManager::ResourceManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ResourceManager>& ResourceManager::getInstance()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!sInstance)
|
|
|
|
sInstance = std::shared_ptr<ResourceManager>(new ResourceManager());
|
2013-10-04 23:09:54 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return sInstance;
|
2013-10-04 23:09:54 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 18:27:44 +00:00
|
|
|
std::string ResourceManager::getResourcePath(const std::string& path) const
|
2013-07-09 05:44:24 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// Check if this is a resource file.
|
|
|
|
if ((path[0] == ':') && (path[1] == '/')) {
|
|
|
|
std::string test;
|
|
|
|
|
2020-06-21 17:35:43 +00:00
|
|
|
// Check under the home directory.
|
2020-06-21 12:25:28 +00:00
|
|
|
test = Utils::FileSystem::getHomePath() + "/.emulationstation/resources/" + &path[2];
|
|
|
|
if (Utils::FileSystem::exists(test))
|
|
|
|
return test;
|
|
|
|
|
2020-06-21 19:08:54 +00:00
|
|
|
// Check under the data installation directory (install prefix).
|
|
|
|
test = Utils::FileSystem::getInstallPrefixPath() +
|
|
|
|
"/emulationstation/resources/" + &path[2];
|
|
|
|
if (Utils::FileSystem::exists(test))
|
2020-06-21 12:25:28 +00:00
|
|
|
return test;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a resource, return unmodified path.
|
|
|
|
return path;
|
2018-02-08 18:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ResourceData ResourceManager::getFileData(const std::string& path) const
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// Check if its a resource.
|
|
|
|
const std::string respath = getResourcePath(path);
|
|
|
|
|
|
|
|
if (Utils::FileSystem::exists(respath)) {
|
|
|
|
ResourceData data = loadFile(respath);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the file doesn't exist, return an "empty" ResourceData.
|
|
|
|
ResourceData data = {NULL, 0};
|
|
|
|
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
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::ifstream stream(path, std::ios::binary);
|
2013-07-03 07:54:55 +00:00
|
|
|
|
2020-06-21 12:25:28 +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
|
|
|
|
2020-06-21 12:25:28 +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
|
|
|
|
2020-06-21 12:25:28 +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
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// If it exists as a resource file, return true.
|
|
|
|
if (getResourcePath(path) != path)
|
|
|
|
return true;
|
2013-07-03 07:54:55 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return Utils::FileSystem::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
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
auto iter = mReloadables.cbegin();
|
|
|
|
while (iter != mReloadables.cend()) {
|
|
|
|
if (!iter->expired()) {
|
|
|
|
iter->lock()->unload(sInstance);
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iter = mReloadables.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
auto iter = mReloadables.cbegin();
|
|
|
|
while (iter != mReloadables.cend()) {
|
|
|
|
if (!iter->expired()) {
|
|
|
|
iter->lock()->reload(sInstance);
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iter = mReloadables.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mReloadables.push_back(reloadable);
|
2013-06-21 16:49:29 +00:00
|
|
|
}
|