-
Notifications
You must be signed in to change notification settings - Fork 13
/
versionmanager.cpp
176 lines (158 loc) · 6.05 KB
/
versionmanager.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "versionmanager.h"
#include <QTextStream>
#include <QStandardPaths>
#include <QDir>
#include <QSettings>
#ifndef LAUNCHER_VERSIONDB_URL
#define LAUNCHER_VERSIONDB_URL "https://raw.githubusercontent.com/minecraft-linux/mcpelauncher-versiondb/master"
#endif
VersionManager::VersionManager() : m_versionList(m_versions), m_archival(LAUNCHER_VERSIONDB_URL) {
baseDir = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)).filePath("mcpelauncher/versions");
QDir().mkpath(baseDir);
loadVersions();
}
#include "supportedandroidabis.h"
void VersionManager::loadVersions() {
QSettings settings(QDir(baseDir).filePath("versions.ini"), QSettings::IniFormat);
for (QString const& group : settings.childGroups()) {
settings.beginGroup(group);
int size = settings.beginReadArray("codes");
if (size >= 1) {
auto ver = new VersionInfo(this);
int i = 0;
while (i < size) {
settings.setArrayIndex(i++);
auto versionCode = settings.value("code").toInt();
ver->codes.insert(settings.value("arch").toString(), versionCode);
m_versions[versionCode] = ver;
}
settings.endArray();
ver->directory = group;
ver->versionName = settings.value("versionName").toString();
} else {
settings.endArray();
// Migrate previous format
bool ok = false;
int versionCode = settings.value("versionCode").toInt(&ok);
if (ok) {
auto& ver = m_versions[versionCode];
if (ver == nullptr)
ver = new VersionInfo(this);
ver->directory = group;
ver->versionName = settings.value("versionName").toString();
for (auto &&abi : SupportedAndroidAbis::getAbis()) {
if (QFile(getDirectoryFor(ver->directory) + "/lib/" + QString::fromStdString(abi.first) + "/libminecraftpe.so").exists()) {
ver->codes[QString::fromStdString(abi.first)] = versionCode;
}
}
}
}
settings.endGroup();
}
}
void VersionManager::saveVersions() {
QSettings settings(QDir(baseDir).filePath("versions.ini"), QSettings::IniFormat);
settings.clear();
// TODO skip writeing duplicates, e.g. one game version has multiple versionscode's
for (auto const& ver : m_versions) {
settings.beginGroup(ver->directory);
int i = 0;
settings.setValue("versionName", ver->versionName);
settings.setValue("versionCode", ver->versionCode());
auto size = ver->codes.size();
settings.beginWriteArray("codes", size);
QHash<QString, int>::const_iterator it = ver->codes.constBegin();
while (it != ver->codes.constEnd()) {
settings.setArrayIndex(i++);
settings.setValue("code", it.value());
settings.setValue("arch", it.key());
++it;
}
settings.endArray();
settings.endGroup();
}
settings.sync();
}
QString VersionManager::getTempTemplate() {
return QDir(getBaseDir()).filePath("temp-XXXXXX");
}
QString VersionManager::getDirectoryFor(QString const& directory) {
return QDir(getBaseDir()).filePath(directory);
}
QString VersionManager::getDirectoryFor(std::string const& directory) {
return getDirectoryFor(QString::fromStdString(directory));
}
QString VersionManager::getDirectoryFor(VersionInfo *version) {
if (version == nullptr)
return QString();
return getDirectoryFor(version->directory);
}
void VersionManager::addVersion(QString directory, QString versionName, int versionCode) {
auto& ver = m_versions[versionCode];
if (ver == nullptr) {
for (auto const& ver2 : m_versions) {
// Find existing entry
if (ver2 && directory == ver2->directory) {
ver = ver2;
break;
}
}
// Fallback old behavior
if (ver == nullptr) {
ver = new VersionInfo(this);
}
}
ver->directory = directory;
ver->versionName = versionName;
for (auto &&abi : SupportedAndroidAbis::getAbis()) {
auto && it = ver->codes.constFind(QString::fromStdString(abi.first));
if (it == ver->codes.constEnd() && QFile(getDirectoryFor(ver->directory) + "/lib/" + QString::fromStdString(abi.first) + "/libminecraftpe.so").exists()) {
ver->codes[QString::fromStdString(abi.first)] = versionCode;
}
}
saveVersions();
emit versionListChanged();
}
void VersionManager::removeVersion(VersionInfo* version) {
if (!version) return;
for (auto && versionCode : version->codes) {
auto val = m_versions.find(versionCode);
if (val.value() != version)
return;
QDir(getDirectoryFor(version)).removeRecursively();
m_versions.erase(val);
}
saveVersions();
emit versionListChanged();
}
void VersionManager::removeVersion(VersionInfo* version, QStringList abis) {
if (!version) return;
for (auto&& abi : abis) {
auto && versionCode = version->codes.constFind(abi);
if (versionCode != version->codes.constEnd()) {
auto val = m_versions.find(versionCode.value());
if (val.value() != version)
continue;
m_versions.erase(val);
}
}
saveVersions();
emit versionListChanged();
}
bool VersionManager::checkSupport(VersionInfo* version) {
if(!version) return false;
return checkSupport(version->directory);
}
bool VersionManager::checkSupport(QString const& directory) {
for (auto &&abi : SupportedAndroidAbis::getAbis()) {
if (abi.second.compatible && QFile(getDirectoryFor(directory) + "/lib/" + QString::fromStdString(abi.first) + "/libminecraftpe.so").exists()) {
return true;
}
}
return false;
}
VersionInfo* VersionList::latestDownloadedVersion() const {
if (m_versions.empty())
return nullptr;
return m_versions.last();
}