mirror of
https://github.com/Almamu/linux-wallpaperengine.git
synced 2025-09-14 13:56:48 +08:00

Textures now have a cache system that prevents loading them more than once Signed-off-by: Alexis Maiquez <almamu@almamu.com>
64 lines
1.6 KiB
C++
64 lines
1.6 KiB
C++
#include <sys/stat.h>
|
|
|
|
#include "CDirectory.h"
|
|
#include "CAssetLoadException.h"
|
|
|
|
using namespace WallpaperEngine::Assets;
|
|
|
|
CDirectory::CDirectory (std::string basepath) :
|
|
m_basepath (std::move (basepath))
|
|
{
|
|
// ensure the specified path exists
|
|
struct stat buffer;
|
|
|
|
if (stat (this->m_basepath.c_str (), &buffer) != 0)
|
|
throw std::runtime_error ("Cannot find " + this->m_basepath + ". This folder is required for wallpaper engine to work");
|
|
|
|
if (!S_ISDIR(buffer.st_mode))
|
|
throw std::runtime_error ("Cannot find " + this->m_basepath + ". There's an assets file in it's place");
|
|
}
|
|
|
|
CDirectory::~CDirectory ()
|
|
{
|
|
|
|
}
|
|
|
|
const void* CDirectory::readFile (std::string filename, uint32_t* length) const
|
|
{
|
|
std::string final = this->m_basepath + filename;
|
|
|
|
// first check the cache, if the file is there already just return the data in there
|
|
auto it = this->m_cache.find (final);
|
|
|
|
if (it != this->m_cache.end ())
|
|
{
|
|
if (length != nullptr)
|
|
*length = (*it).second.length;
|
|
|
|
return (*it).second.address;
|
|
}
|
|
|
|
FILE* fp = fopen (final.c_str (), "rb");
|
|
|
|
if (fp == nullptr)
|
|
throw CAssetLoadException(filename, "Cannot find file");
|
|
|
|
// go to the end, get the position and return to the beginning
|
|
fseek (fp, 0, SEEK_END);
|
|
long size = ftell (fp);
|
|
fseek (fp, 0, SEEK_SET);
|
|
|
|
// now read the whole file
|
|
char* contents = new char[size];
|
|
|
|
if (fread (contents, size, 1, fp) != 1)
|
|
{
|
|
delete[] contents;
|
|
throw CAssetLoadException (filename, "Unexpected error when reading the file");
|
|
}
|
|
|
|
if (length != nullptr)
|
|
*length = size;
|
|
|
|
return contents;
|
|
} |