2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-21 12:25:28 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
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
|
|
|
|
2020-07-08 15:01:47 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "Scripting.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
2022-01-10 17:56:04 +00:00
|
|
|
#include "utils/PlatformUtil.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2020-07-08 15:01:47 +00:00
|
|
|
|
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; };
|
|
|
|
|
2022-01-04 20:21:26 +00:00
|
|
|
ResourceManager& ResourceManager::getInstance()
|
2013-10-04 23:09:54 +00:00
|
|
|
{
|
2022-01-04 20:21:26 +00:00
|
|
|
static ResourceManager instance;
|
|
|
|
return instance;
|
2013-10-04 23:09:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
std::string ResourceManager::getResourcePath(const std::string& path, bool terminateOnFailure) 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] == '/')) {
|
|
|
|
|
2020-06-21 17:35:43 +00:00
|
|
|
// Check under the home directory.
|
2022-01-19 17:01:54 +00:00
|
|
|
std::string testHome =
|
|
|
|
Utils::FileSystem::getHomePath() + "/.emulationstation/resources/" + &path[2];
|
2020-07-08 15:01:47 +00:00
|
|
|
if (Utils::FileSystem::exists(testHome))
|
|
|
|
return testHome;
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(__APPLE__)
|
2020-08-21 19:49:45 +00:00
|
|
|
// For macOS, check in the ../Resources directory relative to the executable directory.
|
|
|
|
std::string applePackagePath =
|
2021-07-07 18:31:46 +00:00
|
|
|
Utils::FileSystem::getExePath() + "/../Resources/resources/" + &path[2];
|
2020-08-21 19:49:45 +00:00
|
|
|
|
|
|
|
if (Utils::FileSystem::exists(applePackagePath)) {
|
|
|
|
return applePackagePath;
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#elif defined(__unix__)
|
2020-07-12 10:47:39 +00:00
|
|
|
// Check under the data installation directory (Unix only).
|
|
|
|
std::string testDataPath;
|
|
|
|
|
2020-07-08 15:01:47 +00:00
|
|
|
testDataPath = Utils::FileSystem::getProgramDataPath() + "/resources/" + &path[2];
|
2020-07-03 18:23:51 +00:00
|
|
|
|
2020-07-08 15:01:47 +00:00
|
|
|
if (Utils::FileSystem::exists(testDataPath)) {
|
|
|
|
return testDataPath;
|
|
|
|
}
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2020-07-12 10:47:39 +00:00
|
|
|
|
|
|
|
// Check under the ES executable directory.
|
2020-08-21 19:49:45 +00:00
|
|
|
std::string testExePath = Utils::FileSystem::getExePath() + "/resources/" + &path[2];
|
2020-07-12 10:47:39 +00:00
|
|
|
|
|
|
|
if (Utils::FileSystem::exists(testExePath)) {
|
|
|
|
return testExePath;
|
|
|
|
}
|
2020-07-08 15:01:47 +00:00
|
|
|
// For missing resources, log an error and terminate the application. This should
|
2020-10-19 15:28:20 +00:00
|
|
|
// indicate that we have a broken EmulationStation installation. If the argument
|
|
|
|
// terminateOnFailure is set to false though, then skip this step.
|
2020-07-08 15:01:47 +00:00
|
|
|
else {
|
2020-10-19 15:28:20 +00:00
|
|
|
if (terminateOnFailure) {
|
|
|
|
LOG(LogError) << "Program resource missing: " << path;
|
|
|
|
LOG(LogError) << "Tried to find the resource in the following locations:";
|
|
|
|
LOG(LogError) << testHome;
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(__APPLE__)
|
2020-10-19 15:28:20 +00:00
|
|
|
LOG(LogError) << applePackagePath;
|
2021-07-07 18:31:46 +00:00
|
|
|
#elif defined(__unix__)
|
2020-10-19 15:28:20 +00:00
|
|
|
LOG(LogError) << testDataPath;
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2020-10-19 15:28:20 +00:00
|
|
|
LOG(LogError) << testExePath;
|
|
|
|
LOG(LogError) << "Has EmulationStation been properly installed?";
|
|
|
|
Scripting::fireEvent("quit");
|
2022-01-10 17:43:17 +00:00
|
|
|
Utils::Platform::emergencyShutdown();
|
2020-10-19 15:28:20 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "";
|
|
|
|
}
|
2020-07-08 15:01:47 +00:00
|
|
|
}
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-08-17 16:41:45 +00:00
|
|
|
ResourceData data = {nullptr, 0};
|
2020-06-21 12:25:28 +00:00
|
|
|
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
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-10 16:32:23 +00:00
|
|
|
std::ifstream stream(Utils::String::stringToWideString(path).c_str(), std::ios::binary);
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
std::ifstream stream(path, std::ios::binary);
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2013-07-03 07:54:55 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
stream.seekg(0, stream.end);
|
2020-11-17 22:06:54 +00:00
|
|
|
size_t size = static_cast<size_t>(stream.tellg());
|
2020-06-21 12:25:28 +00:00
|
|
|
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);
|
2020-11-17 22:06:54 +00:00
|
|
|
stream.read(reinterpret_cast<char*>(data.get()), size);
|
2020-06-21 12:25:28 +00:00
|
|
|
stream.close();
|
2013-07-03 07:54:55 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
ResourceData ret = {data, size};
|
2020-06-21 12:25:28 +00:00
|
|
|
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()) {
|
2022-01-04 20:21:26 +00:00
|
|
|
iter->lock()->unload(getInstance());
|
2021-11-17 16:48:49 +00:00
|
|
|
++iter;
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
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()) {
|
2022-01-04 20:21:26 +00:00
|
|
|
iter->lock()->reload(getInstance());
|
2021-11-17 16:48:49 +00:00
|
|
|
++iter;
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|