-
Notifications
You must be signed in to change notification settings - Fork 2
/
merger.cpp
268 lines (208 loc) · 8.16 KB
/
merger.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "merger.h"
#include "unpacker.h"
#include "pausemessagebox.h"
#include <QDirIterator>
Merger::Merger(const QList<Mod*>& mods, const Settings* s, QObject* parent)
: QObject(parent), modList(mods), settings(s)
{
shouldPause = settings->showPauseMessage;
wcc = new QProcess(this);
QString workingDir = settings->pathWcc;
wcc->setWorkingDirectory(workingDir.remove("wcc_lite.exe"));
//Merger slots
connect(this, &Merger::mergingStarted, this, &Merger::prepare);
connect(this, &Merger::startImagesDeleting, this, &Merger::deleteImages);
connect(this, &Merger::startCooking, this, &Merger::cookAll);
connect(this, &Merger::skipCooking, this, &Merger::unpackAll);
//Process slots
connect(wcc, &QProcess::readyReadStandardOutput, this, &Merger::processOutput);
connect(wcc, &QProcess::readyReadStandardError, this, &Merger::processOutput);
connect(wcc, &QProcess::errorOccurred,
[=](QProcess::ProcessError error) {
toLog("Process errror: " + QString::number( error ));
}
);
}
Merger::~Merger()
{
}
void Merger::startMerging()
{
for (auto mod : modList) {
if (mod->checked) {
chosenMods.append(mod);
if (mod->hasCache) {
uncookQueue.enqueue( parseCmdArgs(settings->cmdUncook, mod->folderPathNative) );
}
}
}
if (chosenMods.size() == 0) {
toLog( tr("No mods chosen!", "Log warning message.") );
chosenMods.clear();
uncookQueue.clear();
finish();
}
else {
isRunning = true;
emit mergingStarted();
}
}
void Merger::prepare()
{
toLog( tr("Merging process started!", "Log message.") );
nothingUncooked = uncookQueue.size() == 0;
connect(wcc, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
this, &Merger::uncookNext);
uncookNext();
}
void Merger::uncookNext()
{
toStatusbar( tr("Uncooking...", "Statusbar text (you can keep it untranslated if you want).") );
if ( !uncookQueue.isEmpty() ) {
QStringList args = uncookQueue.dequeue();
QString name = args.at(1);
name.remove(0, 7);
toLog( tr("Uncooking: %1", "Log message (you can keep it untranslated if you want).").arg(name) );
wcc->start( settings->pathWcc, args );
}
else {
disconnect(wcc, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), 0, 0);
emit startImagesDeleting();
}
}
void Merger::deleteImages()
{
QString format = settings->cmdUncook.mid( settings->cmdUncook.indexOf("-imgfmt=") + QString("-imgfmt=").size() , 3 );
QDirIterator iter(settings->pathUncooked, QDirIterator::Subdirectories);
QString current;
while (iter.hasNext()) {
current = iter.next();
if (current.endsWith(format, Qt::CaseInsensitive)) {
QFile file(current);
if ( file.remove() ) {
toLog( tr(" %1 removed.", "File removal message.").arg(current) );
}
}
}
pause( tr("Merging has been paused! Please delete unnecessary files and press \"OK\" to continue.", "Pause message (check Merger page for more info)."),
"Uncooked",
settings->pathUncooked,
true );
if (nothingUncooked) {
emit skipCooking();
}
else {
emit startCooking();
}
}
void Merger::cookAll()
{
toLog( tr("\nCooking started...", "Log message (you can keep it untranslated if you want)."));
toStatusbar( tr("Cooking...", "Statusbar text (you can keep it untranslated if you want).") );
QStringList args = parseCmdArgs(settings->cmdCook);
connect(wcc, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
this, &Merger::unpackAll);
wcc->start( settings->pathWcc, args );
}
void Merger::unpackAll()
{
toLog( tr("\nUnpacking bundles...", "Log message (you can keep it untranslated if you want).") );
toStatusbar( tr("Unpacking...", "Statusbar text.") );
Unpacker* unpack = new Unpacker(chosenMods, settings);
if (nothingUncooked) {
connect(unpack, &Unpacker::finished, this, &Merger::packAll);
} else {
connect(unpack, &Unpacker::finished, this, &Merger::cacheBuild);
}
connect(unpack, &Unpacker::toLog, this, &Merger::toLog);
connect(unpack, &Unpacker::finished, unpack, &Unpacker::deleteLater);
unpack->run();
}
void Merger::cacheBuild()
{
pause( tr("Merging has been paused again! Please delete unnecessary files and press \"OK\" to continue.", "Pause message (check Merger page for more info)."),
"Cooked",
settings->pathCooked,
false );
toLog( tr("\nCache building started...", "Log message.") );
toStatusbar( tr("Cache building...", "Statusbar text.") );
disconnect(wcc, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), 0, 0);
connect(wcc, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
this, &Merger::packAll);
QStringList args = parseCmdArgs(settings->cmdCache);
wcc->start( settings->pathWcc, args );
}
void Merger::packAll()
{
pause( tr("Merging has been paused again! Please delete unnecessary files and press \"OK\" to continue.", "Pause message (check Merger page for more info)."),
"Cooked",
settings->pathCooked,
false );
toLog( tr("\nPacking process started...", "Log message (you can keep it untranslated if you want)."));
toStatusbar( tr("Packing...", "Statusbar text (you can keep it untranslated if you want).") );
disconnect(wcc, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), 0, 0);
connect(wcc, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
this, &Merger::generateMetadata);
QStringList args = parseCmdArgs(settings->cmdPack);
wcc->start( settings->pathWcc, args );
}
void Merger::generateMetadata()
{
toLog( tr("\nMetadata creation started...", "Log message (you can keep it untranslated if you want).") );
toStatusbar( tr("Generating metadata...", "Statusbar text (you can keep it untranslated if you want)."));
disconnect(wcc, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), 0, 0);
connect(wcc, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
this, &Merger::finish);
QStringList args = parseCmdArgs(settings->cmdMetadata);
wcc->start( settings->pathWcc, args );
}
void Merger::finish()
{
isRunning = false;
emit mergingFinished();
}
/** UTILS **/
QStringList Merger::parseCmdArgs(QString cmd)
{
QStringList templateArgs;
templateArgs << cmd.split(" ");
QStringList parsedArgs;
for (QString arg : templateArgs) {
arg.replace("%UNCOOK%", settings->pathUncooked);
arg.replace("%COOK%", settings->pathCooked);
arg.replace("%PACK%", settings->pathPacked);
arg.replace("%NAME%", settings->mergedModName);
parsedArgs << arg;
}
return parsedArgs;
}
QStringList Merger::parseCmdArgs(QString cmd, QString path)
{
QStringList templateArgs;
templateArgs << cmd.split(" ");
QStringList parsedArgs;
for (QString arg : templateArgs) {
arg.replace("%MOD%", path);
arg.replace("%UNCOOK%", settings->pathUncooked);
parsedArgs << arg;
}
return parsedArgs;
}
void Merger::processOutput()
{
toLog( QString(wcc->readAllStandardOutput()) );
toLog( QString(wcc->readAllStandardError()) );
}
void Merger::pause(QString message, QString folder, QString path, bool showNextTime)
{
if (shouldPause) {
shouldPause = showNextTime;
PauseMessagebox msg(message, folder, path);
toStatusbar( tr("Paused...", "Statusbar text.") );
toLog( tr("\nMerging is paused so you can delete unnecessary files from the %1 folder.", "Pause log message.").arg(folder) );
if (folder == "Uncooked") {
toLog( tr("Please remember which files were deleted because you have to delete the same files from the Cooked folder during the next pause.", "Pause log message.") );
}
msg.exec();
}
}