summaryrefslogtreecommitdiffstats
path: root/gui/dialogs/OneSixModEditDialog.cpp
blob: d8b84d3ed565a5027759f1eacdf9c939977e1edc (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* Copyright 2013 MultiMC Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "MultiMC.h"

#include <pathutils.h>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QEvent>
#include <QKeyEvent>
#include <QDesktopServices>

#include "OneSixModEditDialog.h"
#include "ModEditDialogCommon.h"
#include "ui_OneSixModEditDialog.h"

#include "gui/Platform.h"
#include "gui/dialogs/CustomMessageBox.h"
#include "gui/dialogs/VersionSelectDialog.h"

#include "gui/dialogs/ProgressDialog.h"

#include "logic/ModList.h"
#include "logic/OneSixVersion.h"
#include "logic/EnabledItemFilter.h"
#include "logic/lists/ForgeVersionList.h"
#include "logic/ForgeInstaller.h"
#include "logic/LiteLoaderInstaller.h"

OneSixModEditDialog::OneSixModEditDialog(OneSixInstance *inst, QWidget *parent)
	: QDialog(parent), ui(new Ui::OneSixModEditDialog), m_inst(inst)
{
	MultiMCPlatform::fixWM_CLASS(this);
	ui->setupUi(this);
	// libraries!

	m_version = m_inst->getFullVersion();
	if (m_version)
	{
		main_model = new EnabledItemFilter(this);
		main_model->setActive(true);
		main_model->setSourceModel(m_version.get());
		ui->libraryTreeView->setModel(main_model);
		ui->libraryTreeView->installEventFilter(this);
		ui->mainClassEdit->setText(m_version->mainClass);
		updateVersionControls();
	}
	else
	{
		disableVersionControls();
	}
	// Loader mods
	{
		ensureFolderPathExists(m_inst->loaderModsDir());
		m_mods = m_inst->loaderModList();
		ui->loaderModTreeView->setModel(m_mods.get());
		ui->loaderModTreeView->installEventFilter(this);
		m_mods->startWatching();
		auto smodel = ui->loaderModTreeView->selectionModel();
		connect(smodel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
				SLOT(loaderCurrent(QModelIndex, QModelIndex)));
	}
	// resource packs
	{
		ensureFolderPathExists(m_inst->resourcePacksDir());
		m_resourcepacks = m_inst->resourcePackList();
		ui->resPackTreeView->setModel(m_resourcepacks.get());
		ui->resPackTreeView->installEventFilter(this);
		m_resourcepacks->startWatching();
	}
}

OneSixModEditDialog::~OneSixModEditDialog()
{
	m_mods->stopWatching();
	m_resourcepacks->stopWatching();
	delete ui;
}

void OneSixModEditDialog::updateVersionControls()
{
	bool customVersion = m_inst->versionIsCustom();
	ui->customizeBtn->setEnabled(!customVersion);
	ui->revertBtn->setEnabled(customVersion);
	ui->forgeBtn->setEnabled(true);
	ui->liteloaderBtn->setEnabled(LiteLoaderInstaller(m_inst->intendedVersionId()).canApply());
}

void OneSixModEditDialog::disableVersionControls()
{
	ui->customizeBtn->setEnabled(false);
	ui->revertBtn->setEnabled(false);
	ui->forgeBtn->setEnabled(false);
	ui->liteloaderBtn->setEnabled(false);
}

void OneSixModEditDialog::on_customizeBtn_clicked()
{
	if (m_inst->customizeVersion())
	{
		m_version = m_inst->getFullVersion();
		main_model->setSourceModel(m_version.get());
		updateVersionControls();
	}
}

void OneSixModEditDialog::on_revertBtn_clicked()
{
	auto response = CustomMessageBox::selectable(
		this, tr("Revert?"), tr("Do you want to revert the "
								"version of this instance to its original configuration?"),
		QMessageBox::Question, QMessageBox::Yes | QMessageBox::No)->exec();
	if (response == QMessageBox::Yes)
	{
		if (m_inst->revertCustomVersion())
		{
			m_version = m_inst->getFullVersion();
			main_model->setSourceModel(m_version.get());
			updateVersionControls();
		}
	}
}

void OneSixModEditDialog::on_forgeBtn_clicked()
{
	VersionSelectDialog vselect(MMC->forgelist().get(), tr("Select Forge version"), this);
	vselect.setFilter(1, m_inst->currentVersionId());
	if (vselect.exec() && vselect.selectedVersion())
	{
		if (m_inst->versionIsCustom())
		{
			auto reply = QMessageBox::question(
				this, tr("Revert?"),
				tr("This will revert any "
				   "changes you did to the version up to this point. Is that "
				   "OK?"),
				QMessageBox::Yes | QMessageBox::No);
			if (reply == QMessageBox::Yes)
			{
				m_inst->revertCustomVersion();
				m_inst->customizeVersion();
				{
					m_version = m_inst->getFullVersion();
					main_model->setSourceModel(m_version.get());
					updateVersionControls();
				}
			}
			else
				return;
		}
		else
		{
			m_inst->customizeVersion();
			m_version = m_inst->getFullVersion();
			main_model->setSourceModel(m_version.get());
			updateVersionControls();
		}
		ForgeVersionPtr forgeVersion =
			std::dynamic_pointer_cast<ForgeVersion>(vselect.selectedVersion());
		if (!forgeVersion)
			return;
		auto entry = MMC->metacache()->resolveEntry("minecraftforge", forgeVersion->filename);
		if (entry->stale)
		{
			NetJob *fjob = new NetJob("Forge download");
			fjob->addNetAction(CacheDownload::make(forgeVersion->installer_url, entry));
			ProgressDialog dlg(this);
			dlg.exec(fjob);
			if (dlg.result() == QDialog::Accepted)
			{
				// install
				QString forgePath = entry->getFullPath();
				ForgeInstaller forge(forgePath, forgeVersion->universal_url);
				if (!forge.apply(m_version))
				{
					// failure notice
				}
			}
			else
			{
				// failed to download forge :/
			}
		}
		else
		{
			// install
			QString forgePath = entry->getFullPath();
			ForgeInstaller forge(forgePath, forgeVersion->universal_url);
			if (!forge.apply(m_version))
			{
				// failure notice
			}
		}
	}
}

void OneSixModEditDialog::on_liteloaderBtn_clicked()
{
	LiteLoaderInstaller liteloader(m_inst->intendedVersionId());
	if (!liteloader.canApply())
	{
		QMessageBox::critical(
			this, tr("LiteLoader"),
			tr("There is no information available on how to install LiteLoader "
			   "into this version of Minecraft"));
		return;
	}
	if (!m_inst->versionIsCustom())
	{
		m_inst->customizeVersion();
		m_version = m_inst->getFullVersion();
		main_model->setSourceModel(m_version.get());
		updateVersionControls();
	}
	if (!liteloader.apply(m_version))
	{
		QMessageBox::critical(
			this, tr("LiteLoader"),
			tr("For reasons unknown, the LiteLoader installation failed. Check your MultiMC log files for details."));
	}
}

bool OneSixModEditDialog::loaderListFilter(QKeyEvent *keyEvent)
{
	switch (keyEvent->key())
	{
	case Qt::Key_Delete:
		on_rmModBtn_clicked();
		return true;
	case Qt::Key_Plus:
		on_addModBtn_clicked();
		return true;
	default:
		break;
	}
	return QDialog::eventFilter(ui->loaderModTreeView, keyEvent);
}

bool OneSixModEditDialog::resourcePackListFilter(QKeyEvent *keyEvent)
{
	switch (keyEvent->key())
	{
	case Qt::Key_Delete:
		on_rmResPackBtn_clicked();
		return true;
	case Qt::Key_Plus:
		on_addResPackBtn_clicked();
		return true;
	default:
		break;
	}
	return QDialog::eventFilter(ui->resPackTreeView, keyEvent);
}

bool OneSixModEditDialog::eventFilter(QObject *obj, QEvent *ev)
{
	if (ev->type() != QEvent::KeyPress)
	{
		return QDialog::eventFilter(obj, ev);
	}
	QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
	if (obj == ui->loaderModTreeView)
		return loaderListFilter(keyEvent);
	if (obj == ui->resPackTreeView)
		return resourcePackListFilter(keyEvent);
	return QDialog::eventFilter(obj, ev);
}

void OneSixModEditDialog::on_buttonBox_rejected()
{
	close();
}

void OneSixModEditDialog::on_addModBtn_clicked()
{
	QStringList fileNames = QFileDialog::getOpenFileNames(
		this, QApplication::translate("LegacyModEditDialog", "Select Loader Mods"));
	for (auto filename : fileNames)
	{
		m_mods->stopWatching();
		m_mods->installMod(QFileInfo(filename));
		m_mods->startWatching();
	}
}
void OneSixModEditDialog::on_rmModBtn_clicked()
{
	int first, last;
	auto list = ui->loaderModTreeView->selectionModel()->selectedRows();

	if (!lastfirst(list, first, last))
		return;
	m_mods->stopWatching();
	m_mods->deleteMods(first, last);
	m_mods->startWatching();
}
void OneSixModEditDialog::on_viewModBtn_clicked()
{
	openDirInDefaultProgram(m_inst->loaderModsDir(), true);
}

void OneSixModEditDialog::on_addResPackBtn_clicked()
{
	QStringList fileNames = QFileDialog::getOpenFileNames(
		this, QApplication::translate("LegacyModEditDialog", "Select Resource Packs"));
	for (auto filename : fileNames)
	{
		m_resourcepacks->stopWatching();
		m_resourcepacks->installMod(QFileInfo(filename));
		m_resourcepacks->startWatching();
	}
}
void OneSixModEditDialog::on_rmResPackBtn_clicked()
{
	int first, last;
	auto list = ui->resPackTreeView->selectionModel()->selectedRows();

	if (!lastfirst(list, first, last))
		return;
	m_resourcepacks->stopWatching();
	m_resourcepacks->deleteMods(first, last);
	m_resourcepacks->startWatching();
}
void OneSixModEditDialog::on_viewResPackBtn_clicked()
{
	openDirInDefaultProgram(m_inst->resourcePacksDir(), true);
}

void OneSixModEditDialog::loaderCurrent(QModelIndex current, QModelIndex previous)
{
	if (!current.isValid())
	{
		ui->frame->clear();
		return;
	}
	int row = current.row();
	Mod &m = m_mods->operator[](row);
	ui->frame->updateWithMod(m);
}