image caching
This commit is contained in:
parent
6fedcabd9b
commit
2045315977
|
@ -6,6 +6,8 @@
|
|||
#include <QJsonDocument>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
#include "datadragonimagecache.h"
|
||||
|
||||
class DataDragon {
|
||||
public:
|
||||
DataDragon();
|
||||
|
@ -25,9 +27,9 @@ public:
|
|||
};
|
||||
|
||||
enum class ImageType {
|
||||
SQUARE,
|
||||
LOADING,
|
||||
SPLASH
|
||||
SQUARE = 0,
|
||||
LOADING = 1,
|
||||
SPLASH = 2
|
||||
};
|
||||
|
||||
const std::string& getVersion();
|
||||
|
@ -46,6 +48,8 @@ protected:
|
|||
|
||||
private:
|
||||
CURL* curl = nullptr; // the curl (does curling)
|
||||
|
||||
DataDragonImageCache cache[3];
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <QByteArray>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
class DataDragonImageCache {
|
||||
public:
|
||||
DataDragonImageCache(const std::string& folderextra, const std::string& imageext = ".jpg");
|
||||
~DataDragonImageCache();
|
||||
|
||||
cv::Mat getImage(const std::string& name);
|
||||
void addImageRaw(const QByteArray& arr, const std::string& name);
|
||||
private:
|
||||
std::string getFilepath(const std::string& name) const;
|
||||
|
||||
std::string cacheDir;
|
||||
std::string imageext; // file extention including dot
|
||||
};
|
|
@ -24,6 +24,7 @@ defineReplace(prependAll) {
|
|||
|
||||
SOURCES += \
|
||||
src/datadragon.cpp \
|
||||
src/datadragonimagecache.cpp \
|
||||
src/fakescreen.cpp \
|
||||
src/lolautoaccept.cpp \
|
||||
src/main.cpp \
|
||||
|
@ -38,6 +39,7 @@ SOURCES += \
|
|||
|
||||
HEADERS += \
|
||||
include/datadragon.h \
|
||||
include/datadragonimagecache.h \
|
||||
include/fakescreen.h \
|
||||
include/lolautoaccept.h \
|
||||
include/mainwindow.h \
|
||||
|
|
|
@ -49,7 +49,7 @@ static T getValue(const QJsonObject& obj, const char* key, const T& def) {
|
|||
return def;
|
||||
}
|
||||
|
||||
DataDragon::DataDragon() {
|
||||
DataDragon::DataDragon() : cache({{"square", ".png"}, {"loading", "_0.jpg"}, {"splash", "_0.jpg"}}) {
|
||||
curl = curl_easy_init();
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, arrayWriteCallback);
|
||||
|
@ -112,6 +112,11 @@ const std::vector<DataDragon::ChampData>& DataDragon::getChamps() {
|
|||
}
|
||||
|
||||
cv::Mat DataDragon::getImage(const std::string& champid, ImageType imgtype) {
|
||||
// query cache
|
||||
cv::Mat img = cache[(int) imgtype].getImage(champid);
|
||||
if(!img.empty())
|
||||
return img;
|
||||
|
||||
std::string url;
|
||||
if(imgtype == ImageType::SQUARE) {
|
||||
if(getVersion().empty()) {
|
||||
|
@ -134,8 +139,6 @@ cv::Mat DataDragon::getImage(const std::string& champid, ImageType imgtype) {
|
|||
return {};
|
||||
}
|
||||
|
||||
cv::Mat rawData(1, arr.size(), CV_8UC1, (void*) arr.data());
|
||||
|
||||
// propably an error
|
||||
if(arr.size() < 1000) {
|
||||
Log::info << "small icon url: " << getCDNString() << "img/champion/" << champid << ".png";
|
||||
|
@ -143,6 +146,11 @@ cv::Mat DataDragon::getImage(const std::string& champid, ImageType imgtype) {
|
|||
return {};
|
||||
}
|
||||
|
||||
// store cache
|
||||
cache[(int) imgtype].addImageRaw(arr, champid);
|
||||
|
||||
cv::Mat rawData(1, arr.size(), CV_8UC1, (void*) arr.data());
|
||||
|
||||
cv::Mat decodedImage = cv::imdecode(rawData, cv::IMREAD_COLOR);
|
||||
cv::cvtColor(decodedImage, decodedImage, cv::COLOR_BGR2RGB);
|
||||
return decodedImage;
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
#include "datadragonimagecache.h"
|
||||
|
||||
#include <Log.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <QFile>
|
||||
|
||||
static bool mkdirs(const std::string& path) {
|
||||
size_t offset = 0;
|
||||
while(offset < path.size()) {
|
||||
offset = path.find('/', offset+1);
|
||||
int res = mkdir(path.substr(0, offset).c_str(), S_IRWXU | S_IRWXG); // 770
|
||||
if(res == -1 && errno != EEXIST) {
|
||||
// mkdirs failed
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
DataDragonImageCache::DataDragonImageCache(const std::string& folderextra, const std::string& imageext) : imageext(imageext) {
|
||||
// init cache dir
|
||||
const char* homevar = getenv("HOME");
|
||||
if(homevar == nullptr) {
|
||||
Log::warn << "$HOME is not set! Defaulting to ./";
|
||||
homevar = "./";
|
||||
}
|
||||
cacheDir = std::string(homevar) + "/.cache/lolautoaccept/" + folderextra + "/";
|
||||
|
||||
mkdirs(cacheDir);
|
||||
}
|
||||
DataDragonImageCache::~DataDragonImageCache() {}
|
||||
|
||||
cv::Mat DataDragonImageCache::getImage(const std::string& name) {
|
||||
auto img = cv::imread(getFilepath(name));
|
||||
if(!img.empty())
|
||||
cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
|
||||
return img;
|
||||
}
|
||||
|
||||
void DataDragonImageCache::addImageRaw(const QByteArray& arr, const std::string& name) {
|
||||
QFile file(QString::fromStdString(getFilepath(name)));
|
||||
file.open(QIODevice::WriteOnly);
|
||||
file.write(arr);
|
||||
}
|
||||
|
||||
std::string DataDragonImageCache::getFilepath(const std::string& name) const {
|
||||
return cacheDir + name + imageext;
|
||||
}
|
Loading…
Reference in New Issue