libgui/source/resources/Resources.cpp

181 lines
5.5 KiB
C++
Raw Normal View History

2017-10-29 10:28:14 +01:00
#include <malloc.h>
#include <string.h>
2018-06-21 20:44:58 +02:00
#include <resources/Resources.h>
#include <resources/filelist.h>
2017-10-29 10:28:14 +01:00
#include <system/AsyncDeleter.h>
#include <fs/FSUtils.h>
2018-06-21 20:44:58 +02:00
#include <gui/GuiImageAsync.h>
#include <gui/GuiSound.h>
2017-10-29 10:28:14 +01:00
Resources * Resources::instance = NULL;
2018-06-21 20:44:58 +02:00
void Resources::Clear() {
2017-10-29 10:28:14 +01:00
ResourceFile * ResourceList = getResourceList();
if(ResourceList == NULL) return;
2018-06-21 20:44:58 +02:00
for(int32_t i = 0; ResourceList[i].filename != NULL; ++i) {
if(ResourceList[i].CustomFile) {
free(ResourceList[i].CustomFile);
ResourceList[i].CustomFile = NULL;
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
if(ResourceList[i].CustomFileSize != 0)
ResourceList[i].CustomFileSize = 0;
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
if(instance)
2017-10-29 10:28:14 +01:00
delete instance;
instance = NULL;
}
2018-06-21 20:44:58 +02:00
bool Resources::LoadFiles(const char * path) {
if(!path)
return false;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
bool result = false;
Clear();
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
ResourceFile * ResourceList = getResourceList();
if(ResourceList == NULL) return false;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
for(int32_t i = 0; ResourceList[i].filename != NULL; ++i) {
2017-10-29 10:28:14 +01:00
std::string fullpath(path);
fullpath += "/";
fullpath += ResourceList[i].filename;
2018-06-21 20:44:58 +02:00
uint8_t * buffer = NULL;
uint32_t filesize = 0;
2017-10-29 10:28:14 +01:00
FSUtils::LoadFileToMem(fullpath.c_str(), &buffer, &filesize);
ResourceList[i].CustomFile = buffer;
2018-06-21 20:44:58 +02:00
ResourceList[i].CustomFileSize = (uint32_t) filesize;
2017-10-29 10:28:14 +01:00
result |= (buffer != 0);
2018-06-21 20:44:58 +02:00
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return result;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
const uint8_t * Resources::GetFile(const char * filename) {
2017-10-29 10:28:14 +01:00
ResourceFile * ResourceList = getResourceList();
if(ResourceList == NULL) return NULL;
2018-06-21 20:44:58 +02:00
for(int32_t i = 0; ResourceList[i].filename != NULL; ++i) {
if(strcasecmp(filename, ResourceList[i].filename) == 0) {
return (ResourceList[i].CustomFile ? ResourceList[i].CustomFile : ResourceList[i].DefaultFile);
}
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return NULL;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
uint32_t Resources::GetFileSize(const char * filename) {
2017-10-29 10:28:14 +01:00
ResourceFile * ResourceList = getResourceList();
if(ResourceList == NULL) return 0;
2018-06-21 20:44:58 +02:00
for(int32_t i = 0; ResourceList[i].filename != NULL; ++i) {
if(strcasecmp(filename, ResourceList[i].filename) == 0) {
return (ResourceList[i].CustomFile ? ResourceList[i].CustomFileSize : ResourceList[i].DefaultFileSize);
}
}
return 0;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
GuiImageData * Resources::GetImageData(const char * filename) {
2017-10-29 10:28:14 +01:00
if(!instance)
instance = new Resources;
ResourceFile * ResourceList = getResourceList();
if(ResourceList == NULL) return NULL;
2018-06-21 20:44:58 +02:00
std::map<std::string, std::pair<uint32_t, GuiImageData *> >::iterator itr = instance->imageDataMap.find(std::string(filename));
if(itr != instance->imageDataMap.end()) {
2017-10-29 10:28:14 +01:00
itr->second.first++;
return itr->second.second;
}
2018-06-21 20:44:58 +02:00
for(int32_t i = 0; ResourceList[i].filename != NULL; ++i) {
if(strcasecmp(filename, ResourceList[i].filename) == 0) {
const uint8_t * buff = ResourceList[i].CustomFile ? ResourceList[i].CustomFile : ResourceList[i].DefaultFile;
const uint32_t size = ResourceList[i].CustomFile ? ResourceList[i].CustomFileSize : ResourceList[i].DefaultFileSize;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
if(buff == NULL)
2017-10-29 10:28:14 +01:00
return NULL;
GuiImageData * image = new GuiImageData(buff, size);
instance->imageDataMap[std::string(filename)].first = 1;
instance->imageDataMap[std::string(filename)].second = image;
return image;
2018-06-21 20:44:58 +02:00
}
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return NULL;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void Resources::RemoveImageData(GuiImageData * image) {
std::map<std::string, std::pair<uint32_t, GuiImageData *> >::iterator itr;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
for(itr = instance->imageDataMap.begin(); itr != instance->imageDataMap.end(); itr++) {
if(itr->second.second == image) {
2017-10-29 10:28:14 +01:00
itr->second.first--;
2018-06-21 20:44:58 +02:00
if(itr->second.first == 0) {
2017-10-29 10:28:14 +01:00
AsyncDeleter::pushForDelete( itr->second.second );
instance->imageDataMap.erase(itr);
}
break;
}
}
}
2018-06-21 20:44:58 +02:00
GuiSound * Resources::GetSound(const char * filename) {
2017-10-29 10:28:14 +01:00
if(!instance)
instance = new Resources;
2018-06-21 20:44:58 +02:00
std::map<std::string, std::pair<uint32_t, GuiSound *> >::iterator itr = instance->soundDataMap.find(std::string(filename));
if(itr != instance->soundDataMap.end()) {
2017-10-29 10:28:14 +01:00
itr->second.first++;
return itr->second.second;
}
ResourceFile * ResourceList = getResourceList();
if(ResourceList == NULL) return NULL;
2018-06-21 20:44:58 +02:00
for(int32_t i = 0; ResourceList[i].filename != NULL; ++i) {
if(strcasecmp(filename, ResourceList[i].filename) == 0) {
const uint8_t * buff = ResourceList[i].CustomFile ? ResourceList[i].CustomFile : ResourceList[i].DefaultFile;
const uint32_t size = ResourceList[i].CustomFile ? ResourceList[i].CustomFileSize : ResourceList[i].DefaultFileSize;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
if(buff == NULL)
2017-10-29 10:28:14 +01:00
return NULL;
GuiSound * sound = new GuiSound(buff, size);
instance->soundDataMap[std::string(filename)].first = 1;
instance->soundDataMap[std::string(filename)].second = sound;
return sound;
2018-06-21 20:44:58 +02:00
}
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return NULL;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void Resources::RemoveSound(GuiSound * sound) {
std::map<std::string, std::pair<uint32_t, GuiSound *> >::iterator itr;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
for(itr = instance->soundDataMap.begin(); itr != instance->soundDataMap.end(); itr++) {
if(itr->second.second == sound) {
2017-10-29 10:28:14 +01:00
itr->second.first--;
2018-06-21 20:44:58 +02:00
if(itr->second.first == 0) {
2017-10-29 10:28:14 +01:00
AsyncDeleter::pushForDelete( itr->second.second );
instance->soundDataMap.erase(itr);
}
break;
}
}
}