mirror of
https://github.com/moonlight-stream/moonlight-qt.git
synced 2025-07-02 07:46:07 +00:00
Only update gamepad mappings if they're newer than what we already have
This commit is contained in:
parent
ae5df938b6
commit
861ebc151a
27
app/path.cpp
27
app/path.cpp
@ -29,13 +29,31 @@ QByteArray Path::readDataFile(QString fileName)
|
|||||||
return dataFile.readAll();
|
return dataFile.readAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Path::writeDataFile(QString fileName, QByteArray data)
|
void Path::writeCacheFile(QString fileName, QByteArray data)
|
||||||
{
|
{
|
||||||
QFile dataFile(QDir(s_CacheDir).absoluteFilePath(fileName));
|
QDir cacheDir(s_CacheDir);
|
||||||
|
|
||||||
|
// Create the cache path if it does not exist
|
||||||
|
if (!cacheDir.exists()) {
|
||||||
|
cacheDir.mkpath(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile dataFile(cacheDir.absoluteFilePath(fileName));
|
||||||
dataFile.open(QIODevice::WriteOnly);
|
dataFile.open(QIODevice::WriteOnly);
|
||||||
dataFile.write(data);
|
dataFile.write(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Path::deleteCacheFile(QString fileName)
|
||||||
|
{
|
||||||
|
QFile dataFile(QDir(s_CacheDir).absoluteFilePath(fileName));
|
||||||
|
dataFile.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
QFileInfo Path::getCacheFileInfo(QString fileName)
|
||||||
|
{
|
||||||
|
return QFileInfo(QDir(s_CacheDir), fileName);
|
||||||
|
}
|
||||||
|
|
||||||
QString Path::getDataFilePath(QString fileName)
|
QString Path::getDataFilePath(QString fileName)
|
||||||
{
|
{
|
||||||
QString candidatePath;
|
QString candidatePath;
|
||||||
@ -79,7 +97,10 @@ void Path::initialize(bool portable)
|
|||||||
if (portable) {
|
if (portable) {
|
||||||
s_LogDir = QDir::currentPath();
|
s_LogDir = QDir::currentPath();
|
||||||
s_BoxArtCacheDir = QDir::currentPath() + "/boxart";
|
s_BoxArtCacheDir = QDir::currentPath() + "/boxart";
|
||||||
s_CacheDir = QDir::currentPath();
|
|
||||||
|
// In order for the If-Modified-Since logic to work in MappingFetcher,
|
||||||
|
// the cache directory must be different than the current directory.
|
||||||
|
s_CacheDir = QDir::currentPath() + "/cache";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
#ifdef Q_OS_DARWIN
|
#ifdef Q_OS_DARWIN
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QFileInfo>
|
||||||
|
|
||||||
class Path
|
class Path
|
||||||
{
|
{
|
||||||
@ -10,7 +11,9 @@ public:
|
|||||||
static QString getBoxArtCacheDir();
|
static QString getBoxArtCacheDir();
|
||||||
|
|
||||||
static QByteArray readDataFile(QString fileName);
|
static QByteArray readDataFile(QString fileName);
|
||||||
static void writeDataFile(QString fileName, QByteArray data);
|
static void writeCacheFile(QString fileName, QByteArray data);
|
||||||
|
static void deleteCacheFile(QString fileName);
|
||||||
|
static QFileInfo getCacheFileInfo(QString fileName);
|
||||||
|
|
||||||
// Only safe to use directly for Qt classes
|
// Only safe to use directly for Qt classes
|
||||||
static QString getDataFilePath(QString fileName);
|
static QString getDataFilePath(QString fileName);
|
||||||
|
@ -27,9 +27,17 @@ void MappingFetcher::start()
|
|||||||
QT_WARNING_POP
|
QT_WARNING_POP
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// We'll get a callback when this is finished
|
|
||||||
QUrl url("https://moonlight-stream.org/SDL_GameControllerDB/gamecontrollerdb.txt");
|
QUrl url("https://moonlight-stream.org/SDL_GameControllerDB/gamecontrollerdb.txt");
|
||||||
m_Nam.get(QNetworkRequest(url));
|
QNetworkRequest request(url);
|
||||||
|
|
||||||
|
// Only download the file if it's newer than what we have
|
||||||
|
QFileInfo existingFileInfo = Path::getCacheFileInfo("gamecontrollerdb.txt");
|
||||||
|
if (existingFileInfo.exists() && existingFileInfo.size() > 0) {
|
||||||
|
request.setHeader(QNetworkRequest::IfModifiedSinceHeader, existingFileInfo.lastModified().toUTC());
|
||||||
|
}
|
||||||
|
|
||||||
|
// We'll get a callback when this is finished
|
||||||
|
m_Nam.get(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MappingFetcher::handleMappingListFetched(QNetworkReply* reply)
|
void MappingFetcher::handleMappingListFetched(QNetworkReply* reply)
|
||||||
@ -40,10 +48,21 @@ void MappingFetcher::handleMappingListFetched(QNetworkReply* reply)
|
|||||||
// Queue the reply for deletion
|
// Queue the reply for deletion
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|
||||||
// Update the cached data on disk for next call to applyMappings()
|
// If we get a 304 back, Qt will happily just tell us our request was
|
||||||
Path::writeDataFile("gamecontrollerdb.txt", reply->readAll());
|
// successful and let us try to write our empty response to disk. Check
|
||||||
|
// the status code directly to prevent this.
|
||||||
|
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 304) {
|
||||||
|
qInfo() << "Gamepad mappings are up to date";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Update the cached data on disk for next call to applyMappings()
|
||||||
|
QByteArray data = reply->readAll();
|
||||||
|
if (!data.isEmpty()) {
|
||||||
|
Path::writeCacheFile("gamecontrollerdb.txt", data);
|
||||||
|
}
|
||||||
|
|
||||||
qInfo() << "Downloaded updated gamepad mappings";
|
qInfo() << "Downloaded updated gamepad mappings";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
qWarning() << "Failed to download updated gamepad mappings:" << reply->error();
|
qWarning() << "Failed to download updated gamepad mappings:" << reply->error();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user