Cache forge version list (it's huge)

This commit is contained in:
Petr Mrázek 2013-09-22 14:00:37 +02:00
parent ceca6959d2
commit 9d03a9c1e3
6 changed files with 118 additions and 97 deletions

View File

@ -1,10 +1,12 @@
#include "ForgeInstaller.h"
#include "OneSixVersion.h"
#include "OneSixLibrary.h"
#include "net/HttpMetaCache.h"
#include <quazip.h>
#include <quazipfile.h>
#include <pathutils.h>
#include <QStringList>
#include "MultiMC.h"
ForgeInstaller::ForgeInstaller(QString filename, QString universal_url)
{
@ -53,6 +55,8 @@ ForgeInstaller::ForgeInstaller(QString filename, QString universal_url)
// where do we put the library? decode the mojang path
OneSixLibrary lib(libraryName);
lib.finalize();
auto cacheentry = MMC->metacache()->resolveEntry("libraries", lib.storagePath());
finalPath = "libraries/" + lib.storagePath();
if (!ensureFilePathExists(finalPath))
return;
@ -71,6 +75,12 @@ ForgeInstaller::ForgeInstaller(QString filename, QString universal_url)
return;
if (!extraction.commit())
return;
QCryptographicHash md5sum(QCryptographicHash::Md5);
md5sum.addData(data);
cacheentry->stale = false;
cacheentry->md5sum = md5sum.result().toHex().constData();
MMC->metacache()->updateEntry(cacheentry);
}
file.close();

View File

@ -9,3 +9,4 @@ public:
explicit NostalgiaInstance(const QString &rootDir, SettingsObject * settings, QObject *parent = 0);
virtual QString getStatusbarDescription();
};

View File

@ -320,3 +320,4 @@ QString OneSixInstance::instanceConfigFolder() const
{
return PathCombine(minecraftRoot(), "config");
}

View File

@ -25,10 +25,8 @@
#define JSON_URL "http://files.minecraftforge.net/minecraftforge/json"
ForgeVersionList::ForgeVersionList(QObject *parent) : BaseVersionList(parent)
{
}
Task *ForgeVersionList::getLoadTask()
@ -154,17 +152,17 @@ void ForgeVersionList::sort()
// NO-OP for now
}
ForgeListLoadTask::ForgeListLoadTask(ForgeVersionList *vlist) : Task()
{
m_list = vlist;
}
void ForgeListLoadTask::executeTask()
{
auto job = new DownloadJob("Version index");
job->add(QUrl(JSON_URL));
// we do not care if the version is stale or not.
auto forgeListEntry = MMC->metacache()->resolveEntry("minecraftforge", "list.json");
job->add(QUrl(JSON_URL), forgeListEntry);
listJob.reset(job);
connect(listJob.data(), SIGNAL(succeeded()), SLOT(list_downloaded()));
connect(listJob.data(), SIGNAL(failed()), SLOT(versionFileFailed()));
@ -173,14 +171,21 @@ void ForgeListLoadTask::executeTask()
}
void ForgeListLoadTask::list_downloaded()
{
QByteArray data;
{
auto DlJob = listJob->first();
auto data = DlJob.dynamicCast<ByteArrayDownload>()->m_data;
auto filename = DlJob.dynamicCast<CacheDownload>()->m_target_path;
QFile listFile(filename);
if(!listFile.open(QIODevice::ReadOnly))
return;
data = listFile.readAll();
DlJob.reset();
}
QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
DlJob.reset();
if (jsonError.error != QJsonParseError::NoError)
{
@ -199,7 +204,8 @@ void ForgeListLoadTask::list_downloaded()
// Now, get the array of versions.
if (!root.value("builds").isArray())
{
emitFailed("Error parsing version list JSON: version list object is missing 'builds' array");
emitFailed(
"Error parsing version list JSON: version list object is missing 'builds' array");
return;
}
QJsonArray builds = root.value("builds").toArray();
@ -273,8 +279,3 @@ void ForgeListLoadTask::list_downloaded()
emitSucceeded();
return;
}

View File

@ -39,9 +39,11 @@ void CacheDownload::start()
QNetworkReply *rep = worker->get(request);
m_reply = QSharedPointer<QNetworkReply>(rep, &QObject::deleteLater);
connect ( rep, SIGNAL ( downloadProgress ( qint64,qint64 ) ), SLOT ( downloadProgress ( qint64,qint64 ) ) );
connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
SLOT(downloadProgress(qint64, qint64)));
connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
connect ( rep, SIGNAL ( error ( QNetworkReply::NetworkError ) ), SLOT ( downloadError ( QNetworkReply::NetworkError ) ) );
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
SLOT(downloadError(QNetworkReply::NetworkError)));
connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
}
@ -74,15 +76,18 @@ void CacheDownload::downloadFinished()
{
if (m_output_file.open(QIODevice::ReadOnly))
{
m_entry->md5sum = QCryptographicHash::hash ( m_output_file.readAll(), QCryptographicHash::Md5 ).toHex().constData();
m_entry->md5sum =
QCryptographicHash::hash(m_output_file.readAll(), QCryptographicHash::Md5)
.toHex()
.constData();
m_output_file.close();
}
}
QFileInfo output_file_info(m_target_path);
m_entry->etag = m_reply->rawHeader("ETag").constData();
m_entry->last_changed_timestamp = output_file_info.lastModified().toUTC().toMSecsSinceEpoch();
m_entry->last_changed_timestamp =
output_file_info.lastModified().toUTC().toMSecsSinceEpoch();
m_entry->stale = false;
MMC->metacache()->updateEntry(m_entry);

View File

@ -30,7 +30,8 @@ public:
MetaEntryPtr getEntry(QString base, QString resource_path);
// get the entry from cache and verify that it isn't stale (within reason)
MetaEntryPtr resolveEntry(QString base, QString resource_path, QString expected_etag = QString());
MetaEntryPtr resolveEntry(QString base, QString resource_path,
QString expected_etag = QString());
// add a previously resolved stale entry
bool updateEntry(MetaEntryPtr stale_entry);
@ -41,8 +42,10 @@ public:
void SaveEventually();
void Load();
QString getBasePath(QString base);
public slots:
public
slots:
void SaveNow();
private:
// create a new stale entry, given the parameters
MetaEntryPtr staleEntry(QString base, QString resource_path);