pollymc/logic/OneSixAssets.cpp

128 lines
3.1 KiB
C++
Raw Normal View History

/* Copyright 2013 MultiMC Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2013-08-04 03:28:39 +05:30
#include <QString>
#include "logger/QsLog.h"
2013-08-04 03:28:39 +05:30
#include <QtXml/QtXml>
#include "OneSixAssets.h"
#include "net/NetJob.h"
#include "net/HttpMetaCache.h"
#include "net/S3ListBucket.h"
#include "MultiMC.h"
2013-08-04 03:28:39 +05:30
#define ASSETS_URL "http://resources.download.minecraft.net/"
2013-08-04 03:28:39 +05:30
class ThreadedDeleter : public QThread
{
Q_OBJECT
public:
void run()
{
2013-10-06 04:43:40 +05:30
QLOG_INFO() << "Cleaning up assets folder...";
QDirIterator iter(m_base, QDirIterator::Subdirectories);
2013-08-04 03:28:39 +05:30
int base_length = m_base.length();
while (iter.hasNext())
2013-08-04 03:28:39 +05:30
{
QString filename = iter.next();
QFileInfo current(filename);
2013-08-04 03:28:39 +05:30
// we keep the dirs... whatever
if (current.isDir())
2013-08-04 03:28:39 +05:30
continue;
QString trimmedf = filename;
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";
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
void OneSixAssets::S3BucketFinished()
2013-08-04 03:28:39 +05:30
{
QString prefix(ASSETS_URL);
2013-09-02 03:55:40 +05:30
nuke_whitelist.clear();
2013-08-04 03:28:39 +05:30
emit filesStarted();
2013-09-02 03:55:40 +05:30
auto firstJob = index_job->first();
auto objectList = std::dynamic_pointer_cast<S3ListBucket>(firstJob)->objects;
2013-08-04 03:28:39 +05:30
NetJob *job = new NetJob("Assets");
2013-08-04 03:28:39 +05:30
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-04 03:28:39 +05:30
auto metacache = MMC->metacache();
2013-08-04 03:28:39 +05:30
for (auto object : objectList)
{
// Filter folder keys (zero size)
if (object.size == 0)
2013-08-04 03:28:39 +05:30
continue;
nuke_whitelist.append(object.Key);
2013-08-04 03:28:39 +05:30
auto entry = metacache->resolveEntry("assets", object.Key, object.ETag);
if (entry->stale)
2013-09-02 03:55:40 +05:30
{
job->addNetAction(CacheDownload::make(QUrl(prefix + object.Key), entry));
2013-09-02 03:55:40 +05:30
}
}
if (job->size())
2013-09-02 03:55:40 +05:30
{
files_job.reset(job);
2013-09-02 03:55:40 +05:30
files_job->start();
}
else
{
delete job;
emit finished();
2013-08-04 03:28:39 +05:30
}
}
2013-08-04 03:28:39 +05:30
void OneSixAssets::start()
{
auto job = new NetJob("Assets index");
job->addNetAction(S3ListBucket::make(QUrl(ASSETS_URL)));
connect(job, SIGNAL(succeeded()), SLOT(S3BucketFinished()));
2013-11-03 21:32:59 +05:30
connect(job, SIGNAL(failed()), SIGNAL(failed()));
emit indexStarted();
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"