2013-08-04 03:28:39 +05:30
|
|
|
#include <QString>
|
2013-10-06 04:43:40 +05:30
|
|
|
#include <logger/QsLog.h>
|
2013-08-04 03:28:39 +05:30
|
|
|
#include <QtXml/QtXml>
|
|
|
|
#include "OneSixAssets.h"
|
2013-08-07 05:08:18 +05:30
|
|
|
#include "net/DownloadJob.h"
|
2013-09-08 05:45:20 +05:30
|
|
|
#include "net/HttpMetaCache.h"
|
|
|
|
#include "MultiMC.h"
|
2013-08-04 03:28:39 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ThreadedDeleter : public QThread
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
void run()
|
|
|
|
{
|
2013-10-06 04:43:40 +05:30
|
|
|
QLOG_INFO() << "Cleaning up assets folder...";
|
2013-09-02 03:55:40 +05:30
|
|
|
QDirIterator iter ( m_base, QDirIterator::Subdirectories );
|
2013-08-04 03:28:39 +05:30
|
|
|
int base_length = m_base.length();
|
2013-09-02 03:55:40 +05:30
|
|
|
while ( iter.hasNext() )
|
2013-08-04 03:28:39 +05:30
|
|
|
{
|
|
|
|
QString filename = iter.next();
|
2013-09-02 03:55:40 +05:30
|
|
|
QFileInfo current ( filename );
|
2013-08-04 03:28:39 +05:30
|
|
|
// we keep the dirs... whatever
|
2013-09-02 03:55:40 +05:30
|
|
|
if ( current.isDir() )
|
2013-08-04 03:28:39 +05:30
|
|
|
continue;
|
|
|
|
QString trimmedf = filename;
|
2013-09-02 03:55:40 +05:30
|
|
|
trimmedf.remove ( 0, base_length + 1 );
|
|
|
|
if ( m_whitelist.contains ( trimmedf ) )
|
2013-08-04 03:28:39 +05:30
|
|
|
{
|
2013-10-06 04:43:40 +05:30
|
|
|
QLOG_TRACE() << trimmedf << " gets to live";
|
2013-08-04 03:28:39 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// DO NOT TOLERATE JUNK
|
2013-10-06 04:43:40 +05:30
|
|
|
QLOG_TRACE() << trimmedf << " dies";
|
2013-09-02 03:55:40 +05:30
|
|
|
QFile f ( filename );
|
2013-08-04 03:28:39 +05:30
|
|
|
f.remove();
|
|
|
|
}
|
|
|
|
}
|
2013-09-02 03:55:40 +05:30
|
|
|
}
|
2013-08-04 03:28:39 +05:30
|
|
|
QString m_base;
|
|
|
|
QStringList m_whitelist;
|
|
|
|
};
|
|
|
|
|
2013-09-02 03:55:40 +05:30
|
|
|
void OneSixAssets::downloadFinished()
|
2013-08-04 03:28:39 +05:30
|
|
|
{
|
2013-09-02 03:55:40 +05:30
|
|
|
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-04 03:28:39 +05:30
|
|
|
|
|
|
|
|
2013-09-02 03:55:40 +05:30
|
|
|
void OneSixAssets::fetchXMLFinished()
|
2013-08-04 03:28:39 +05:30
|
|
|
{
|
|
|
|
QString prefix ( "http://s3.amazonaws.com/Minecraft.Resources/" );
|
|
|
|
QString fprefix ( "assets/" );
|
2013-09-02 03:55:40 +05:30
|
|
|
nuke_whitelist.clear();
|
2013-08-04 03:28:39 +05:30
|
|
|
|
2013-10-26 06:58:50 +05:30
|
|
|
emit filesStarted();
|
|
|
|
|
2013-09-02 03:55:40 +05:30
|
|
|
auto firstJob = index_job->first();
|
2013-10-06 04:43:40 +05:30
|
|
|
QByteArray ba = std::dynamic_pointer_cast<ByteArrayDownload>(firstJob)->m_data;
|
2013-08-04 03:28:39 +05:30
|
|
|
|
|
|
|
QString xmlErrorMsg;
|
|
|
|
QDomDocument doc;
|
|
|
|
if ( !doc.setContent ( ba, false, &xmlErrorMsg ) )
|
|
|
|
{
|
2013-10-06 04:43:40 +05:30
|
|
|
QLOG_ERROR() << "Failed to process s3.amazonaws.com/Minecraft.Resources. XML error:" << xmlErrorMsg << ba;
|
|
|
|
emit failed();
|
|
|
|
return;
|
2013-08-04 03:28:39 +05:30
|
|
|
}
|
|
|
|
//QRegExp etag_match(".*([a-f0-9]{32}).*");
|
|
|
|
QDomNodeList contents = doc.elementsByTagName ( "Contents" );
|
|
|
|
|
2013-09-08 05:45:20 +05:30
|
|
|
DownloadJob *job = new DownloadJob("Assets");
|
2013-09-02 03:55:40 +05:30
|
|
|
connect ( job, SIGNAL(succeeded()), SLOT(downloadFinished()) );
|
|
|
|
connect ( job, SIGNAL(failed()), SIGNAL(failed()) );
|
2013-10-26 06:58:50 +05:30
|
|
|
connect ( job, SIGNAL(filesProgress(int, int, int)), SIGNAL(filesProgress(int, int, int)) );
|
2013-08-04 03:28:39 +05:30
|
|
|
|
2013-09-08 05:45:20 +05:30
|
|
|
auto metacache = MMC->metacache();
|
|
|
|
|
2013-08-04 03:28:39 +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;
|
|
|
|
|
|
|
|
nuke_whitelist.append ( keyStr );
|
2013-09-08 05:45:20 +05:30
|
|
|
|
|
|
|
auto entry = metacache->resolveEntry("assets", keyStr, etagStr);
|
|
|
|
if(entry->stale)
|
2013-09-02 03:55:40 +05:30
|
|
|
{
|
2013-09-30 06:04:46 +05:30
|
|
|
job->addCacheDownload(QUrl(prefix + keyStr), entry);
|
2013-09-02 03:55:40 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
if(job->size())
|
|
|
|
{
|
|
|
|
files_job.reset ( job );
|
|
|
|
files_job->start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete job;
|
|
|
|
emit finished();
|
2013-08-04 03:28:39 +05:30
|
|
|
}
|
|
|
|
}
|
2013-10-26 06:58:50 +05:30
|
|
|
|
2013-08-04 03:28:39 +05:30
|
|
|
void OneSixAssets::start()
|
|
|
|
{
|
2013-09-08 05:45:20 +05:30
|
|
|
auto job = new DownloadJob("Assets index");
|
2013-09-30 06:04:46 +05:30
|
|
|
job->addByteArrayDownload(QUrl ( "http://s3.amazonaws.com/Minecraft.Resources/" ));
|
2013-09-02 03:55:40 +05:30
|
|
|
connect ( job, SIGNAL(succeeded()), SLOT ( fetchXMLFinished() ) );
|
2013-10-26 06:58:50 +05:30
|
|
|
emit indexStarted();
|
2013-08-04 07:49:10 +05:30
|
|
|
index_job.reset ( job );
|
2013-09-02 03:55:40 +05:30
|
|
|
job->start();
|
2013-08-04 03:28:39 +05:30
|
|
|
}
|
|
|
|
|
2013-09-02 03:55:40 +05:30
|
|
|
#include "OneSixAssets.moc"
|