-
Notifications
You must be signed in to change notification settings - Fork 0
/
assetsmanager.cpp
86 lines (78 loc) · 2.83 KB
/
assetsmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "assetsmanager.h"
AssetsManager::AssetsManager(QObject *parent) : QObject(parent)
{
m_ongoingInstalls = 0;
}
#ifndef Q_OS_WIN
void AssetsManager::fixCases() {
fixCases(QDir(settings.value("installs").toList()[settings.value("currentInstall").toInt()].toMap()["dir"].toString()));
}
void AssetsManager::fixCases(QDir directory) {
directory.setFilter(QDir::NoDotAndDotDot | QDir::AllEntries);
QFileInfoList list = directory.entryInfoList();
QString fileName;
for (int i = 0; i < list.size(); ++i) {
if (list.at(i).isDir() && list.at(i).fileName() != "lib") {
fixCases(list.at(i).absoluteFilePath());
}
fileName = list.at(i).fileName();
if (fileName.toLower() != fileName) {
QFileInfo dest(directory.filePath(fileName.toLower()));
if (dest.exists()){
if (dest.isDir()) {
QDir(dest.absoluteFilePath()).removeRecursively();
} else {
QFile(dest.absoluteFilePath()).remove();
}
}
directory.rename(fileName, fileName.toLower());
}
}
}
#endif
QVariantList AssetsManager::progresses() {
return m_progresses;
}
void AssetsManager::setProgress(int index, qint64 a, qint64 b) {
if (b > 0) {
m_progresses[index] = (90.0*a/b);
emit progressesChanged();
}
}
void AssetsManager::installAsset(QString URL) {
return installAsset(QUrl(URL));
}
void AssetsManager::installAsset(QUrl URL) {
m_ongoingInstalls++;
int myIndex = m_downloads.length();
m_downloads.append(new FileDownloader(URL, this));
m_progresses.append(0);
emit progressesChanged();
connect(m_downloads[myIndex], &FileDownloader::downloaded, [=](QFile* file){ installAsset(myIndex, file); });
connect(m_downloads[myIndex], &FileDownloader::progressChanged, [=](qint64 a, qint64 b){ setProgress(myIndex, a, b); });
}
void AssetsManager::installAsset(int index, QFile* file) {
QProcess* sevenzip = new QProcess();
QString dir = settings.value("installs").toList()[settings.value("currentInstall").toInt()].toMap()["dir"].toString();
sevenzip->setWorkingDirectory(dir);
#ifdef Q_OS_WIN
QFile::copy(":/7za.exe", dir+"\\7za.exe");
sevenzip->start(dir+"\\7za.exe", {"x", "-y", file->fileName().replace('/', '\\')});
#else
sevenzip->start("7za", {"x", "-y", file->fileName()});
#endif
connect(sevenzip, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), [=](){
sevenzip->deleteLater();
m_downloads[index]->deleteLater();
m_progresses[index] = 100;
m_ongoingInstalls--;
if (m_ongoingInstalls == 0) {
m_progresses.clear();
m_downloads.clear();
#ifndef Q_OS_WIN
fixCases();
#endif
}
emit progressesChanged();
});
}