2015-01-31 15:59:03 +00:00
|
|
|
#include "Env.h"
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "net/HttpMetaCache.h"
|
2015-02-02 00:09:28 +00:00
|
|
|
#include "BaseVersion.h"
|
|
|
|
#include "BaseVersionList.h"
|
2015-01-31 15:59:03 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QNetworkProxy>
|
|
|
|
#include <QNetworkAccessManager>
|
2015-02-02 01:14:14 +00:00
|
|
|
#include <QDebug>
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "tasks/Task.h"
|
2016-04-06 22:09:30 +01:00
|
|
|
#include "wonko/WonkoIndex.h"
|
2015-01-31 18:21:47 +00:00
|
|
|
#include <QDebug>
|
2015-01-31 15:59:03 +00:00
|
|
|
|
2015-02-02 00:09:28 +00:00
|
|
|
/*
|
|
|
|
* The *NEW* global rat nest of an object. Handle with care.
|
|
|
|
*/
|
|
|
|
|
2015-01-31 15:59:03 +00:00
|
|
|
Env::Env()
|
|
|
|
{
|
2015-01-31 18:21:47 +00:00
|
|
|
m_qnam = std::make_shared<QNetworkAccessManager>();
|
2015-01-31 15:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Env::destroy()
|
|
|
|
{
|
|
|
|
m_metacache.reset();
|
|
|
|
m_qnam.reset();
|
2015-02-02 00:09:28 +00:00
|
|
|
m_versionLists.clear();
|
2015-01-31 15:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Env& Env::Env::getInstance()
|
|
|
|
{
|
|
|
|
static Env instance;
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2015-01-31 18:21:47 +00:00
|
|
|
std::shared_ptr< HttpMetaCache > Env::metacache()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_metacache != nullptr);
|
|
|
|
return m_metacache;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr< QNetworkAccessManager > Env::qnam()
|
|
|
|
{
|
|
|
|
return m_qnam;
|
|
|
|
}
|
|
|
|
|
2015-02-02 00:09:28 +00:00
|
|
|
/*
|
|
|
|
class NullVersion : public BaseVersion
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
virtual QString name()
|
|
|
|
{
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
virtual QString descriptor()
|
|
|
|
{
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
virtual QString typeString() const
|
|
|
|
{
|
|
|
|
return "Null";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class NullTask: public Task
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
virtual void executeTask()
|
|
|
|
{
|
|
|
|
emitFailed(tr("Nothing to do."));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class NullVersionList: public BaseVersionList
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
virtual const BaseVersionPtr at(int i) const
|
|
|
|
{
|
|
|
|
return std::make_shared<NullVersion>();
|
|
|
|
}
|
|
|
|
virtual int count() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
virtual Task* getLoadTask()
|
|
|
|
{
|
|
|
|
return new NullTask;
|
|
|
|
}
|
|
|
|
virtual bool isLoaded()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
virtual void sort()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void updateListData(QList< BaseVersionPtr >)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
BaseVersionPtr Env::getVersion(QString component, QString version)
|
|
|
|
{
|
|
|
|
auto list = getVersionList(component);
|
2015-04-02 19:18:23 +01:00
|
|
|
if(!list)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-02-02 00:09:28 +00:00
|
|
|
return list->findVersion(version);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr< BaseVersionList > Env::getVersionList(QString component)
|
|
|
|
{
|
|
|
|
auto iter = m_versionLists.find(component);
|
|
|
|
if(iter != m_versionLists.end())
|
|
|
|
{
|
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
//return std::make_shared<NullVersionList>();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Env::registerVersionList(QString name, std::shared_ptr< BaseVersionList > vlist)
|
|
|
|
{
|
|
|
|
m_versionLists[name] = vlist;
|
|
|
|
}
|
|
|
|
|
2016-04-06 22:09:30 +01:00
|
|
|
std::shared_ptr<WonkoIndex> Env::wonkoIndex()
|
|
|
|
{
|
|
|
|
if (!m_wonkoIndex)
|
|
|
|
{
|
|
|
|
m_wonkoIndex = std::make_shared<WonkoIndex>();
|
|
|
|
}
|
|
|
|
return m_wonkoIndex;
|
|
|
|
}
|
|
|
|
|
2015-01-31 18:21:47 +00:00
|
|
|
|
2015-12-27 02:34:03 +00:00
|
|
|
void Env::initHttpMetaCache()
|
2015-01-31 15:59:03 +00:00
|
|
|
{
|
|
|
|
m_metacache.reset(new HttpMetaCache("metacache"));
|
|
|
|
m_metacache->addBase("asset_indexes", QDir("assets/indexes").absolutePath());
|
|
|
|
m_metacache->addBase("asset_objects", QDir("assets/objects").absolutePath());
|
|
|
|
m_metacache->addBase("versions", QDir("versions").absolutePath());
|
|
|
|
m_metacache->addBase("libraries", QDir("libraries").absolutePath());
|
|
|
|
m_metacache->addBase("minecraftforge", QDir("mods/minecraftforge").absolutePath());
|
|
|
|
m_metacache->addBase("fmllibs", QDir("mods/minecraftforge/libs").absolutePath());
|
|
|
|
m_metacache->addBase("liteloader", QDir("mods/liteloader").absolutePath());
|
|
|
|
m_metacache->addBase("general", QDir("cache").absolutePath());
|
|
|
|
m_metacache->addBase("skins", QDir("accounts/skins").absolutePath());
|
2015-12-27 02:34:03 +00:00
|
|
|
m_metacache->addBase("root", QDir::currentPath());
|
|
|
|
m_metacache->addBase("translations", QDir("translations").absolutePath());
|
2015-05-28 18:38:29 +01:00
|
|
|
m_metacache->addBase("icons", QDir("cache/icons").absolutePath());
|
2016-04-06 22:09:30 +01:00
|
|
|
m_metacache->addBase("wonko", QDir("cache/wonko").absolutePath());
|
2015-01-31 15:59:03 +00:00
|
|
|
m_metacache->Load();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Env::updateProxySettings(QString proxyTypeStr, QString addr, int port, QString user, QString password)
|
|
|
|
{
|
|
|
|
// Set the application proxy settings.
|
|
|
|
if (proxyTypeStr == "SOCKS5")
|
|
|
|
{
|
|
|
|
QNetworkProxy::setApplicationProxy(
|
|
|
|
QNetworkProxy(QNetworkProxy::Socks5Proxy, addr, port, user, password));
|
|
|
|
}
|
|
|
|
else if (proxyTypeStr == "HTTP")
|
|
|
|
{
|
|
|
|
QNetworkProxy::setApplicationProxy(
|
|
|
|
QNetworkProxy(QNetworkProxy::HttpProxy, addr, port, user, password));
|
|
|
|
}
|
|
|
|
else if (proxyTypeStr == "None")
|
|
|
|
{
|
|
|
|
// If we have no proxy set, set no proxy and return.
|
|
|
|
QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::NoProxy));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If we have "Default" selected, set Qt to use the system proxy settings.
|
|
|
|
QNetworkProxyFactory::setUseSystemConfiguration(true);
|
|
|
|
}
|
|
|
|
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << "Detecting proxy settings...";
|
2015-01-31 15:59:03 +00:00
|
|
|
QNetworkProxy proxy = QNetworkProxy::applicationProxy();
|
|
|
|
if (m_qnam.get())
|
|
|
|
m_qnam->setProxy(proxy);
|
|
|
|
QString proxyDesc;
|
|
|
|
if (proxy.type() == QNetworkProxy::NoProxy)
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << "Using no proxy is an option!";
|
2015-01-31 15:59:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (proxy.type())
|
|
|
|
{
|
|
|
|
case QNetworkProxy::DefaultProxy:
|
|
|
|
proxyDesc = "Default proxy: ";
|
|
|
|
break;
|
|
|
|
case QNetworkProxy::Socks5Proxy:
|
|
|
|
proxyDesc = "Socks5 proxy: ";
|
|
|
|
break;
|
|
|
|
case QNetworkProxy::HttpProxy:
|
|
|
|
proxyDesc = "HTTP proxy: ";
|
|
|
|
break;
|
|
|
|
case QNetworkProxy::HttpCachingProxy:
|
|
|
|
proxyDesc = "HTTP caching: ";
|
|
|
|
break;
|
|
|
|
case QNetworkProxy::FtpCachingProxy:
|
|
|
|
proxyDesc = "FTP caching: ";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
proxyDesc = "DERP proxy: ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
proxyDesc += QString("%3@%1:%2 pass %4")
|
|
|
|
.arg(proxy.hostName())
|
|
|
|
.arg(proxy.port())
|
|
|
|
.arg(proxy.user())
|
|
|
|
.arg(proxy.password());
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << proxyDesc;
|
2015-01-31 15:59:03 +00:00
|
|
|
}
|
2015-02-02 00:09:28 +00:00
|
|
|
|
2015-05-28 18:38:29 +01:00
|
|
|
#include "Env.moc"
|