2013-08-03 22:58:39 +01:00
|
|
|
#include <QString>
|
2013-10-06 00:13:40 +01:00
|
|
|
#include <logger/QsLog.h>
|
2013-08-03 22:58:39 +01:00
|
|
|
#include <QtXml/QtXml>
|
|
|
|
#include "OneSixAssets.h"
|
2013-10-26 18:55:48 +01:00
|
|
|
#include "net/NetJob.h"
|
2013-09-08 01:15:20 +01:00
|
|
|
#include "net/HttpMetaCache.h"
|
2013-10-26 18:55:48 +01:00
|
|
|
#include "net/S3ListBucket.h"
|
2013-09-08 01:15:20 +01:00
|
|
|
#include "MultiMC.h"
|
2013-08-03 22:58:39 +01:00
|
|
|
|
|
|
|
class ThreadedDeleter : public QThread
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
void run()
|
|
|
|
{
|
2013-10-06 00:13:40 +01:00
|
|
|
QLOG_INFO() << "Cleaning up assets folder...";
|
2013-10-26 18:55:48 +01:00
|
|
|
QDirIterator iter(m_base, QDirIterator::Subdirectories);
|
2013-08-03 22:58:39 +01:00
|
|
|
int base_length = m_base.length();
|
2013-10-26 18:55:48 +01:00
|
|
|
while (iter.hasNext())
|
2013-08-03 22:58:39 +01:00
|
|
|
{
|
|
|
|
QString filename = iter.next();
|
2013-10-26 18:55:48 +01:00
|
|
|
QFileInfo current(filename);
|
2013-08-03 22:58:39 +01:00
|
|
|
// we keep the dirs... whatever
|
2013-10-26 18:55:48 +01:00
|
|
|
if (current.isDir())
|
2013-08-03 22:58:39 +01:00
|
|
|
continue;
|
|
|
|
QString trimmedf = filename;
|
2013-10-26 18:55:48 +01:00
|
|
|
trimmedf.remove(0, base_length + 1);
|
|
|
|
if (m_whitelist.contains(trimmedf))
|
2013-08-03 22:58:39 +01:00
|
|
|
{
|
2013-10-06 00:13:40 +01:00
|
|
|
QLOG_TRACE() << trimmedf << " gets to live";
|
2013-08-03 22:58:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// DO NOT TOLERATE JUNK
|
2013-10-06 00:13:40 +01:00
|
|
|
QLOG_TRACE() << trimmedf << " dies";
|
2013-10-26 18:55:48 +01:00
|
|
|
QFile f(filename);
|
2013-08-03 22:58:39 +01:00
|
|
|
f.remove();
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 23:25:40 +01:00
|
|
|
}
|
2013-08-03 22:58:39 +01:00
|
|
|
QString m_base;
|
|
|
|
QStringList m_whitelist;
|
|
|
|
};
|
|
|
|
|
2013-09-01 23:25:40 +01:00
|
|
|
void OneSixAssets::downloadFinished()
|
2013-08-03 22:58:39 +01:00
|
|
|
{
|
2013-09-01 23:25:40 +01:00
|
|
|
deleter = new ThreadedDeleter();
|
|
|
|
QDir dir("assets");
|
|
|
|
deleter->m_base = dir.absolutePath();
|
|
|
|
deleter->m_whitelist = nuke_whitelist;
|
|
|
|
connect(deleter, SIGNAL(finished()), SIGNAL(finished()));
|
|
|
|
deleter->start();
|
|
|
|
}
|
2013-08-03 22:58:39 +01:00
|
|
|
|
2013-10-26 18:55:48 +01:00
|
|
|
void OneSixAssets::S3BucketFinished()
|
2013-08-03 22:58:39 +01:00
|
|
|
{
|
2013-10-26 18:55:48 +01:00
|
|
|
QString prefix("http://s3.amazonaws.com/Minecraft.Resources/");
|
2013-09-01 23:25:40 +01:00
|
|
|
nuke_whitelist.clear();
|
2013-08-03 22:58:39 +01:00
|
|
|
|
2013-10-26 02:28:50 +01:00
|
|
|
emit filesStarted();
|
|
|
|
|
2013-09-01 23:25:40 +01:00
|
|
|
auto firstJob = index_job->first();
|
2013-10-26 18:55:48 +01:00
|
|
|
auto objectList = std::dynamic_pointer_cast<S3ListBucket>(firstJob)->objects;
|
2013-08-03 22:58:39 +01:00
|
|
|
|
2013-10-26 18:55:48 +01:00
|
|
|
NetJob *job = new NetJob("Assets");
|
2013-08-03 22:58:39 +01:00
|
|
|
|
2013-10-26 18:55:48 +01:00
|
|
|
connect(job, SIGNAL(succeeded()), SLOT(downloadFinished()));
|
|
|
|
connect(job, SIGNAL(failed()), SIGNAL(failed()));
|
|
|
|
connect(job, SIGNAL(filesProgress(int, int, int)), SIGNAL(filesProgress(int, int, int)));
|
2013-08-03 22:58:39 +01:00
|
|
|
|
2013-09-08 01:15:20 +01:00
|
|
|
auto metacache = MMC->metacache();
|
2013-08-03 22:58:39 +01:00
|
|
|
|
2013-10-26 18:55:48 +01:00
|
|
|
for (auto object: objectList)
|
|
|
|
{
|
|
|
|
// Filter folder keys (zero size)
|
|
|
|
if (object.size == 0)
|
2013-08-03 22:58:39 +01:00
|
|
|
continue;
|
|
|
|
|
2013-10-26 18:55:48 +01:00
|
|
|
nuke_whitelist.append(object.Key);
|
2013-08-03 22:58:39 +01:00
|
|
|
|
2013-10-26 18:55:48 +01:00
|
|
|
auto entry = metacache->resolveEntry("assets", object.Key, object.ETag);
|
|
|
|
if (entry->stale)
|
2013-09-01 23:25:40 +01:00
|
|
|
{
|
2013-10-26 18:55:48 +01:00
|
|
|
job->addNetAction(CacheDownload::make(QUrl(prefix + object.Key), entry));
|
2013-09-01 23:25:40 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 18:55:48 +01:00
|
|
|
if (job->size())
|
2013-09-01 23:25:40 +01:00
|
|
|
{
|
2013-10-26 18:55:48 +01:00
|
|
|
files_job.reset(job);
|
2013-09-01 23:25:40 +01:00
|
|
|
files_job->start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete job;
|
|
|
|
emit finished();
|
2013-08-03 22:58:39 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 02:28:50 +01:00
|
|
|
|
2013-08-03 22:58:39 +01:00
|
|
|
void OneSixAssets::start()
|
|
|
|
{
|
2013-10-26 18:55:48 +01:00
|
|
|
auto job = new NetJob("Assets index");
|
|
|
|
job->addNetAction(
|
|
|
|
S3ListBucket::make(QUrl("http://s3.amazonaws.com/Minecraft.Resources/")));
|
|
|
|
connect(job, SIGNAL(succeeded()), SLOT(S3BucketFinished()));
|
2013-11-03 16:02:59 +00:00
|
|
|
connect(job, SIGNAL(failed()), SIGNAL(failed()));
|
2013-10-26 02:28:50 +01:00
|
|
|
emit indexStarted();
|
2013-10-26 18:55:48 +01:00
|
|
|
index_job.reset(job);
|
2013-09-01 23:25:40 +01:00
|
|
|
job->start();
|
2013-08-03 22:58:39 +01:00
|
|
|
}
|
|
|
|
|
2013-09-01 23:25:40 +01:00
|
|
|
#include "OneSixAssets.moc"
|