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 "installerquick.h"
#include <QDialog>
#include <QtPlugin>
#include <uibase/game_features/igamefeatures.h>
#include <uibase/iplugingame.h>
#include <uibase/log.h>
#include "simpleinstalldialog.h"
using namespace MOBase;
InstallerQuick::InstallerQuick() : m_MOInfo(nullptr) {}
bool InstallerQuick::init(IOrganizer* moInfo)
{
m_MOInfo = moInfo;
// Note: Cannot retrieve the checker here because the game might
// not be initialized yet.
return true;
}
QString InstallerQuick::name() const
{
return "Simple Installer";
}
QString InstallerQuick::localizedName() const
{
return tr("Simple Installer");
}
QString InstallerQuick::author() const
{
return "Tannin";
}
QString InstallerQuick::description() const
{
return tr("Installer for very simple archives");
}
VersionInfo InstallerQuick::version() const
{
return VersionInfo(1, 3, 0, VersionInfo::RELEASE_FINAL);
}
QList<PluginSetting> InstallerQuick::settings() const
{
return {PluginSetting("silent",
"simple plugins will be installed without any user interaction",
QVariant(false))};
}
unsigned int InstallerQuick::priority() const
{
return 50;
}
bool InstallerQuick::isManualInstaller() const
{
return false;
}
bool InstallerQuick::isDataTextArchiveTopLayer(std::shared_ptr<const IFileTree> tree,
QString const& dataFolderName,
ModDataChecker*) const
{
// A "DataText" archive is defined as having exactly one folder named like
// `dataFolderName` and one or more "useless" files (text files, pdf, or images).
static const std::set<QString, FileNameComparator> txtExtensions{
"txt", "pdf", "md", "jpg", "jpeg", "png", "bmp"};
bool dataFound = false;
bool txtFound = false;
for (auto entry : *tree) {
if (entry->isDir()) {
// If data was already found, or this is a directory not named "data", fail:
if (dataFound || entry->compare(dataFolderName) != 0) {
return false;
}
dataFound = true;
} else {
if (txtExtensions.count(entry->suffix()) == 0) {
return false;
}
txtFound = true;
}
}
return dataFound && txtFound;
}
std::shared_ptr<const IFileTree>
InstallerQuick::getSimpleArchiveBase(std::shared_ptr<const IFileTree> dataTree,
QString const& dataFolderName,
ModDataChecker* checker) const
{
if (!checker) {
return nullptr;
}
while (true) {
if (checker->dataLooksValid(dataTree) == ModDataChecker::CheckReturn::VALID ||
isDataTextArchiveTopLayer(dataTree, dataFolderName, checker)) {
return dataTree;
} else if (dataTree->size() == 1 && dataTree->at(0)->isDir()) {
dataTree = dataTree->at(0)->astree();
} else {
log::debug("Archive is not a simple archive.");
return nullptr;
}
}
}
bool InstallerQuick::isArchiveSupported(std::shared_ptr<const IFileTree> tree) const
{
auto checker = m_MOInfo->gameFeatures()->gameFeature<ModDataChecker>();
if (!checker) {
return false;
}
if (getSimpleArchiveBase(tree, m_MOInfo->managedGame()->dataDirectory().dirName(),
checker.get()) != nullptr) {
return true;
}
return checker->dataLooksValid(tree) == ModDataChecker::CheckReturn::FIXABLE;
}
IPluginInstaller::EInstallResult
InstallerQuick::install(GuessedValue<QString>& modName,
std::shared_ptr<IFileTree>& tree, QString&, int&)
{
const QString dataFolderName = m_MOInfo->managedGame()->dataDirectory().dirName();
auto checker = m_MOInfo->gameFeatures()->gameFeature<ModDataChecker>();
auto base = std::const_pointer_cast<IFileTree>(
getSimpleArchiveBase(tree, dataFolderName, checker.get()));
if (base == nullptr &&
checker->dataLooksValid(tree) == ModDataChecker::CheckReturn::FIXABLE) {
tree = checker->fix(tree);
} else {
tree = base;
}
if (tree != nullptr) {
SimpleInstallDialog dialog(modName, parentWidget());
if (m_MOInfo->pluginSetting(name(), "silent").toBool() ||
dialog.exec() == QDialog::Accepted) {
modName.update(dialog.getName(), GUESS_USER);
// If we have a data+txt archive, we move files to the data folder and
// switch to the data folder. We need to check that we actually have a
// checker here, otherwise it is anyway impossible that
// isDataTextArchiveTopLayer() returned true.
if (checker && isDataTextArchiveTopLayer(tree, dataFolderName, checker.get())) {
auto dataTree = tree->findDirectory(dataFolderName);
dataTree->detach();
dataTree->merge(tree);
tree = dataTree;
}
return RESULT_SUCCESS;
} else {
if (dialog.manualRequested()) {
modName.update(dialog.getName(), GUESS_USER);
return RESULT_MANUALREQUESTED;
} else {
return RESULT_CANCELED;
}
}
} else {
// install shouldn't even have even have been called
qCritical("unsupported archive for quick installer");
return RESULT_FAILED;
}
}
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
Q_EXPORT_PLUGIN2(installerQuick, InstallerQuick)
#endif
|