2013-07-06 06:11:23 +05:30
|
|
|
#include <QString>
|
2013-07-01 02:09:57 +05:30
|
|
|
#include <QDebug>
|
|
|
|
#include <QtXml/QtXml>
|
2013-07-06 06:11:23 +05:30
|
|
|
|
2013-07-06 05:20:07 +05:30
|
|
|
#include "dlqueue.h"
|
2013-07-01 02:09:57 +05:30
|
|
|
|
|
|
|
inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname)
|
|
|
|
{
|
|
|
|
QDomNodeList elementList = parent.elementsByTagName(tagname);
|
|
|
|
if (elementList.count())
|
|
|
|
return elementList.at(0).toElement();
|
|
|
|
else
|
|
|
|
return QDomElement();
|
|
|
|
}
|
|
|
|
|
2013-07-07 21:42:09 +05:30
|
|
|
class ThreadedDeleter : public QThread
|
2013-07-06 06:11:23 +05:30
|
|
|
{
|
2013-07-07 21:42:09 +05:30
|
|
|
Q_OBJECT
|
2013-07-06 06:11:23 +05:30
|
|
|
public:
|
2013-07-07 21:42:09 +05:30
|
|
|
void run()
|
2013-07-06 06:11:23 +05:30
|
|
|
{
|
|
|
|
QDirIterator iter(m_base, QDirIterator::Subdirectories);
|
|
|
|
QStringList nuke_list;
|
|
|
|
int base_length = m_base.length();
|
|
|
|
while (iter.hasNext())
|
|
|
|
{
|
|
|
|
QString filename = iter.next();
|
|
|
|
QFileInfo current(filename);
|
|
|
|
// we keep the dirs... whatever
|
|
|
|
if(current.isDir())
|
|
|
|
continue;
|
|
|
|
QString trimmedf = filename;
|
|
|
|
trimmedf.remove(0, base_length + 1);
|
|
|
|
if(m_whitelist.contains(trimmedf))
|
|
|
|
{
|
|
|
|
//qDebug() << trimmedf << " gets to live";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// DO NOT TOLERATE JUNK
|
|
|
|
//qDebug() << trimmedf << " dies";
|
|
|
|
QFile f (filename);
|
|
|
|
f.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
QString m_base;
|
|
|
|
QStringList m_whitelist;
|
|
|
|
};
|
|
|
|
|
2013-07-07 21:42:09 +05:30
|
|
|
class NukeAndPaveJob: public Job
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
|
|
|
|
explicit NukeAndPaveJob(QString base, QStringList whitelist)
|
|
|
|
:Job()
|
|
|
|
{
|
|
|
|
QDir dir(base);
|
|
|
|
deleterThread.m_base = dir.absolutePath();
|
|
|
|
deleterThread.m_whitelist = whitelist;
|
|
|
|
};
|
|
|
|
public slots:
|
|
|
|
virtual void start()
|
|
|
|
{
|
|
|
|
connect(&deleterThread, SIGNAL(finished()), SLOT(threadFinished()));
|
|
|
|
deleterThread.start();
|
|
|
|
};
|
|
|
|
void threadFinished()
|
|
|
|
{
|
|
|
|
emit finish();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
ThreadedDeleter deleterThread;
|
|
|
|
};
|
|
|
|
|
2013-07-01 02:09:57 +05:30
|
|
|
class DlMachine : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public slots:
|
2013-07-05 04:27:20 +05:30
|
|
|
void filesFinished()
|
|
|
|
{
|
|
|
|
qApp->quit();
|
|
|
|
}
|
|
|
|
|
2013-07-01 02:09:57 +05:30
|
|
|
void fetchFinished()
|
|
|
|
{
|
2013-07-06 06:11:23 +05:30
|
|
|
QString prefix("http://s3.amazonaws.com/Minecraft.Resources/");
|
|
|
|
QString fprefix("assets/");
|
|
|
|
QStringList nuke_whitelist;
|
|
|
|
|
2013-07-06 04:25:54 +05:30
|
|
|
JobPtr firstJob = index_job->getFirstJob();
|
|
|
|
auto DlJob = firstJob.dynamicCast<DownloadJob>();
|
|
|
|
QByteArray ba = DlJob->m_data;
|
2013-07-01 02:09:57 +05:30
|
|
|
|
|
|
|
QString xmlErrorMsg;
|
|
|
|
QDomDocument doc;
|
|
|
|
if (!doc.setContent(ba, false, &xmlErrorMsg))
|
|
|
|
{
|
|
|
|
qDebug() << "Failed to process s3.amazonaws.com/Minecraft.Resources. XML error:" <<
|
|
|
|
xmlErrorMsg << ba;
|
|
|
|
}
|
2013-07-07 21:42:09 +05:30
|
|
|
//QRegExp etag_match(".*([a-f0-9]{32}).*");
|
2013-07-01 02:09:57 +05:30
|
|
|
QDomNodeList contents = doc.elementsByTagName("Contents");
|
|
|
|
|
2013-07-06 04:25:54 +05:30
|
|
|
JobList *job = new JobList();
|
2013-07-05 04:27:20 +05:30
|
|
|
connect(job, SIGNAL(finished()), SLOT(filesFinished()));
|
|
|
|
|
2013-07-01 02:09:57 +05:30
|
|
|
for (int i = 0; i < contents.length(); i++)
|
|
|
|
{
|
|
|
|
QDomElement element = contents.at(i).toElement();
|
|
|
|
|
|
|
|
if (element.isNull())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QDomElement keyElement = getDomElementByTagName(element, "Key");
|
|
|
|
QDomElement lastmodElement = getDomElementByTagName(element, "LastModified");
|
|
|
|
QDomElement etagElement = getDomElementByTagName(element, "ETag");
|
|
|
|
QDomElement sizeElement = getDomElementByTagName(element, "Size");
|
|
|
|
|
|
|
|
if (keyElement.isNull() || lastmodElement.isNull() || etagElement.isNull() || sizeElement.isNull())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QString keyStr = keyElement.text();
|
|
|
|
QString lastModStr = lastmodElement.text();
|
|
|
|
QString etagStr = etagElement.text();
|
|
|
|
QString sizeStr = sizeElement.text();
|
|
|
|
|
|
|
|
//Filter folder keys
|
|
|
|
if (sizeStr == "0")
|
|
|
|
continue;
|
|
|
|
|
2013-07-05 04:27:20 +05:30
|
|
|
QString trimmedEtag = etagStr.remove('"');
|
2013-07-06 04:25:54 +05:30
|
|
|
job->add(DownloadJob::create(QUrl(prefix + keyStr),fprefix + keyStr, trimmedEtag));
|
2013-07-06 06:11:23 +05:30
|
|
|
nuke_whitelist.append(keyStr);
|
2013-07-01 02:09:57 +05:30
|
|
|
}
|
2013-07-06 06:11:23 +05:30
|
|
|
job->add(JobPtr(new NukeAndPaveJob(fprefix, nuke_whitelist)));
|
2013-07-05 04:27:20 +05:30
|
|
|
files_job.reset(job);
|
|
|
|
dl.enqueue(files_job);
|
2013-07-01 02:09:57 +05:30
|
|
|
}
|
|
|
|
void fetchStarted()
|
|
|
|
{
|
2013-07-01 04:15:32 +05:30
|
|
|
qDebug() << "Started downloading!";
|
|
|
|
}
|
2013-07-01 02:09:57 +05:30
|
|
|
public:
|
|
|
|
void start()
|
|
|
|
{
|
2013-07-06 04:25:54 +05:30
|
|
|
JobList *job = new JobList();
|
|
|
|
job->add(DownloadJob::create(QUrl("http://s3.amazonaws.com/Minecraft.Resources/")));
|
2013-07-01 02:09:57 +05:30
|
|
|
connect(job, SIGNAL(finished()), SLOT(fetchFinished()));
|
|
|
|
connect(job, SIGNAL(started()), SLOT(fetchStarted()));
|
2013-07-05 04:27:20 +05:30
|
|
|
index_job.reset(job);
|
|
|
|
dl.enqueue(index_job);
|
2013-07-01 02:09:57 +05:30
|
|
|
}
|
2013-07-06 04:25:54 +05:30
|
|
|
JobListQueue dl;
|
|
|
|
JobListPtr index_job;
|
|
|
|
JobListPtr files_job;
|
2013-07-01 02:09:57 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
|
|
|
DlMachine dl;
|
|
|
|
dl.start();
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|
2013-07-05 04:27:20 +05:30
|
|
|
|
|
|
|
#include "asset_test.moc"
|