2022-05-27 02:48:54 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2013-01-15 05:12:38 +05:30
|
|
|
*
|
2022-05-27 02:48:54 +05:30
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
2013-10-06 04:43:40 +05:30
|
|
|
*
|
2022-05-27 02:48:54 +05:30
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2013-01-15 05:12:38 +05:30
|
|
|
*
|
2022-05-27 02:48:54 +05:30
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 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.
|
2013-01-15 05:12:38 +05:30
|
|
|
*/
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
#include <QDebug>
|
2013-02-19 23:45:22 +05:30
|
|
|
#include <QDir>
|
2018-07-24 04:54:06 +05:30
|
|
|
#include <QDirIterator>
|
2013-02-19 23:45:22 +05:30
|
|
|
#include <QFile>
|
2018-07-24 04:54:06 +05:30
|
|
|
#include <QFileSystemWatcher>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
2021-10-26 03:21:42 +05:30
|
|
|
#include <QMimeData>
|
2022-07-30 23:12:33 +05:30
|
|
|
#include <QSet>
|
|
|
|
#include <QStack>
|
|
|
|
#include <QPair>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QUuid>
|
|
|
|
#include <QXmlStreamReader>
|
2013-02-19 23:45:22 +05:30
|
|
|
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "BaseInstance.h"
|
2022-07-30 23:12:33 +05:30
|
|
|
#include "ExponentialSeries.h"
|
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "InstanceList.h"
|
2018-07-24 04:54:06 +05:30
|
|
|
#include "InstanceTask.h"
|
|
|
|
#include "NullInstance.h"
|
|
|
|
#include "WatchLock.h"
|
2022-07-30 23:12:33 +05:30
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "settings/INISettingsObject.h"
|
2018-07-24 04:54:06 +05:30
|
|
|
|
2022-05-10 01:28:08 +05:30
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
2018-07-24 04:54:06 +05:30
|
|
|
const static int GROUP_FILE_FORMAT_VERSION = 1;
|
2013-02-19 23:45:22 +05:30
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
InstanceList::InstanceList(SettingsObjectPtr settings, const QString& instDir, QObject* parent)
|
2018-07-24 04:54:06 +05:30
|
|
|
: QAbstractListModel(parent), m_globalSettings(settings)
|
2013-02-19 23:45:22 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
resumeWatch();
|
2018-07-24 04:54:06 +05:30
|
|
|
// Create aand normalize path
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!QDir::current().exists(instDir)) {
|
2018-07-24 04:54:06 +05:30
|
|
|
QDir::current().mkpath(instDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(this, &InstanceList::instancesChanged, this, &InstanceList::providerUpdated);
|
|
|
|
|
|
|
|
// NOTE: canonicalPath requires the path to exist. Do not move this above the creation block!
|
|
|
|
m_instDir = QDir(instDir).canonicalPath();
|
|
|
|
m_watcher = new QFileSystemWatcher(this);
|
|
|
|
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &InstanceList::instanceDirContentsChanged);
|
|
|
|
m_watcher->addPath(m_instDir);
|
2013-02-19 23:45:22 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
InstanceList::~InstanceList() {}
|
2013-08-26 02:18:41 +05:30
|
|
|
|
2021-10-26 03:21:42 +05:30
|
|
|
Qt::DropActions InstanceList::supportedDragActions() const
|
|
|
|
{
|
|
|
|
return Qt::MoveAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::DropActions InstanceList::supportedDropActions() const
|
|
|
|
{
|
|
|
|
return Qt::MoveAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InstanceList::canDropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) const
|
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
if (data && data->hasFormat("application/x-instanceid")) {
|
2021-10-26 03:21:42 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InstanceList::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent)
|
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
if (data && data->hasFormat("application/x-instanceid")) {
|
2021-10-26 03:21:42 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList InstanceList::mimeTypes() const
|
|
|
|
{
|
|
|
|
auto types = QAbstractListModel::mimeTypes();
|
|
|
|
types.push_back("application/x-instanceid");
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
QMimeData* InstanceList::mimeData(const QModelIndexList& indexes) const
|
2021-10-26 03:21:42 +05:30
|
|
|
{
|
|
|
|
auto mimeData = QAbstractListModel::mimeData(indexes);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (indexes.size() == 1) {
|
2021-10-26 03:21:42 +05:30
|
|
|
auto instanceId = data(indexes[0], InstanceIDRole).toString();
|
|
|
|
mimeData->setData("application/x-instanceid", instanceId.toUtf8());
|
|
|
|
}
|
|
|
|
return mimeData;
|
|
|
|
}
|
|
|
|
|
2023-02-16 10:31:27 +05:30
|
|
|
QStringList InstanceList::getLinkedInstancesById(const QString &id) const
|
|
|
|
{
|
|
|
|
QStringList linkedInstances;
|
|
|
|
for (auto inst : m_instances) {
|
|
|
|
if (inst->isLinkedToInstanceId(id))
|
|
|
|
linkedInstances.append(inst->id());
|
|
|
|
}
|
|
|
|
return linkedInstances;
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
int InstanceList::rowCount(const QModelIndex& parent) const
|
2013-08-26 10:00:11 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
Q_UNUSED(parent);
|
|
|
|
return m_instances.count();
|
2013-08-26 10:00:11 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
QModelIndex InstanceList::index(int row, int column, const QModelIndex& parent) const
|
2013-08-26 10:00:11 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
Q_UNUSED(parent);
|
|
|
|
if (row < 0 || row >= m_instances.size())
|
|
|
|
return QModelIndex();
|
2022-07-30 23:12:33 +05:30
|
|
|
return createIndex(row, column, (void*)m_instances.at(row).get());
|
2013-08-26 10:00:11 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
QVariant InstanceList::data(const QModelIndex& index, int role) const
|
2013-08-26 10:00:11 +05:30
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!index.isValid()) {
|
2018-07-15 18:21:05 +05:30
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
BaseInstance *pdata = static_cast<BaseInstance *>(index.internalPointer());
|
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case InstancePointerRole:
|
|
|
|
{
|
2022-05-02 22:40:45 +05:30
|
|
|
QVariant v = QVariant::fromValue((void *)pdata);
|
2018-07-15 18:21:05 +05:30
|
|
|
return v;
|
|
|
|
}
|
|
|
|
case InstanceIDRole:
|
2014-03-30 23:41:05 +05:30
|
|
|
{
|
|
|
|
return pdata->id();
|
|
|
|
}
|
2018-11-22 06:20:32 +05:30
|
|
|
case Qt::EditRole:
|
2018-07-15 18:21:05 +05:30
|
|
|
case Qt::DisplayRole:
|
|
|
|
{
|
|
|
|
return pdata->name();
|
|
|
|
}
|
2019-07-22 00:42:05 +05:30
|
|
|
case Qt::AccessibleTextRole:
|
|
|
|
{
|
|
|
|
return tr("%1 Instance").arg(pdata->name());
|
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
case Qt::ToolTipRole:
|
|
|
|
{
|
|
|
|
return pdata->instanceRoot();
|
|
|
|
}
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
{
|
|
|
|
return pdata->iconKey();
|
|
|
|
}
|
2021-10-26 03:21:42 +05:30
|
|
|
// HACK: see InstanceView.h in gui!
|
2018-07-15 18:21:05 +05:30
|
|
|
case GroupRole:
|
|
|
|
{
|
2018-07-31 05:24:08 +05:30
|
|
|
return getInstanceGroup(pdata->id());
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
2013-08-26 10:00:11 +05:30
|
|
|
}
|
|
|
|
|
2018-11-22 06:20:32 +05:30
|
|
|
bool InstanceList::setData(const QModelIndex& index, const QVariant& value, int role)
|
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!index.isValid()) {
|
2018-11-22 06:20:32 +05:30
|
|
|
return false;
|
|
|
|
}
|
2022-07-30 23:12:33 +05:30
|
|
|
if (role != Qt::EditRole) {
|
2018-11-22 06:20:32 +05:30
|
|
|
return false;
|
|
|
|
}
|
2022-07-30 23:12:33 +05:30
|
|
|
BaseInstance* pdata = static_cast<BaseInstance*>(index.internalPointer());
|
2018-11-22 06:20:32 +05:30
|
|
|
auto newName = value.toString();
|
2022-07-30 23:12:33 +05:30
|
|
|
if (pdata->name() == newName) {
|
2018-11-22 06:20:32 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
pdata->setName(newName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
Qt::ItemFlags InstanceList::flags(const QModelIndex& index) const
|
2013-08-26 10:00:11 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
Qt::ItemFlags f;
|
2022-07-30 23:12:33 +05:30
|
|
|
if (index.isValid()) {
|
2018-11-22 06:20:32 +05:30
|
|
|
f |= (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
return f;
|
2013-08-26 10:00:11 +05:30
|
|
|
}
|
2013-08-26 02:18:41 +05:30
|
|
|
|
2018-07-31 05:24:08 +05:30
|
|
|
GroupId InstanceList::getInstanceGroup(const InstanceId& id) const
|
|
|
|
{
|
|
|
|
auto inst = getInstanceById(id);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!inst) {
|
2018-07-31 05:24:08 +05:30
|
|
|
return GroupId();
|
|
|
|
}
|
2019-08-20 06:28:27 +05:30
|
|
|
auto iter = m_instanceGroupIndex.find(inst->id());
|
2022-07-30 23:12:33 +05:30
|
|
|
if (iter != m_instanceGroupIndex.end()) {
|
2018-07-31 05:24:08 +05:30
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
return GroupId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceList::setInstanceGroup(const InstanceId& id, const GroupId& name)
|
|
|
|
{
|
|
|
|
auto inst = getInstanceById(id);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!inst) {
|
2018-07-31 05:24:08 +05:30
|
|
|
qDebug() << "Attempt to set a null instance's group";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool changed = false;
|
2019-08-20 06:28:27 +05:30
|
|
|
auto iter = m_instanceGroupIndex.find(inst->id());
|
2022-07-30 23:12:33 +05:30
|
|
|
if (iter != m_instanceGroupIndex.end()) {
|
|
|
|
if (*iter != name) {
|
2018-07-31 05:24:08 +05:30
|
|
|
*iter = name;
|
|
|
|
changed = true;
|
|
|
|
}
|
2022-07-30 23:12:33 +05:30
|
|
|
} else {
|
2018-07-31 05:24:08 +05:30
|
|
|
changed = true;
|
2019-08-20 06:28:27 +05:30
|
|
|
m_instanceGroupIndex[id] = name;
|
2018-07-31 05:24:08 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
if (changed) {
|
2019-08-20 06:28:27 +05:30
|
|
|
m_groupNameCache.insert(name);
|
2018-11-12 04:24:16 +05:30
|
|
|
auto idx = getInstIndex(inst.get());
|
2022-07-30 23:12:33 +05:30
|
|
|
emit dataChanged(index(idx), index(idx), { GroupRole });
|
2018-07-31 05:24:08 +05:30
|
|
|
saveGroupList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-15 22:40:51 +05:30
|
|
|
QStringList InstanceList::getGroups()
|
|
|
|
{
|
2022-05-02 22:40:45 +05:30
|
|
|
return m_groupNameCache.values();
|
2013-12-15 22:40:51 +05:30
|
|
|
}
|
|
|
|
|
2016-04-11 05:00:50 +05:30
|
|
|
void InstanceList::deleteGroup(const QString& name)
|
|
|
|
{
|
2018-07-31 05:24:08 +05:30
|
|
|
bool removed = false;
|
|
|
|
qDebug() << "Delete group" << name;
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto& instance : m_instances) {
|
|
|
|
const auto& instID = instance->id();
|
2018-07-31 05:24:08 +05:30
|
|
|
auto instGroupName = getInstanceGroup(instID);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (instGroupName == name) {
|
2019-08-20 06:28:27 +05:30
|
|
|
m_instanceGroupIndex.remove(instID);
|
2018-07-31 05:24:08 +05:30
|
|
|
qDebug() << "Remove" << instID << "from group" << name;
|
|
|
|
removed = true;
|
|
|
|
auto idx = getInstIndex(instance.get());
|
2022-07-30 23:12:33 +05:30
|
|
|
if (idx > 0) {
|
|
|
|
emit dataChanged(index(idx), index(idx), { GroupRole });
|
2018-07-31 05:24:08 +05:30
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
2022-07-30 23:12:33 +05:30
|
|
|
if (removed) {
|
2018-07-31 05:24:08 +05:30
|
|
|
saveGroupList();
|
|
|
|
}
|
2016-04-11 05:00:50 +05:30
|
|
|
}
|
|
|
|
|
2019-08-20 06:28:27 +05:30
|
|
|
bool InstanceList::isGroupCollapsed(const QString& group)
|
|
|
|
{
|
|
|
|
return m_collapsedGroups.contains(group);
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
bool InstanceList::trashInstance(const InstanceId& id)
|
|
|
|
{
|
|
|
|
auto inst = getInstanceById(id);
|
|
|
|
if (!inst) {
|
|
|
|
qDebug() << "Cannot trash instance" << id << ". No such instance is present (deleted externally?).";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto cachedGroupId = m_instanceGroupIndex[id];
|
|
|
|
|
|
|
|
qDebug() << "Will trash instance" << id;
|
|
|
|
QString trashedLoc;
|
|
|
|
|
|
|
|
if (m_instanceGroupIndex.remove(id)) {
|
|
|
|
saveGroupList();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FS::trash(inst->instanceRoot(), &trashedLoc)) {
|
|
|
|
qDebug() << "Trash of instance" << id << "has not been completely successfully...";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "Instance" << id << "has been trashed by the launcher.";
|
|
|
|
m_trashHistory.push({id, inst->instanceRoot(), trashedLoc, cachedGroupId});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InstanceList::trashedSomething() {
|
|
|
|
return !m_trashHistory.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceList::undoTrashInstance() {
|
|
|
|
if (m_trashHistory.empty()) {
|
|
|
|
qWarning() << "Nothing to recover from trash.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto top = m_trashHistory.pop();
|
|
|
|
|
|
|
|
while (QDir(top.polyPath).exists()) {
|
|
|
|
top.id += "1";
|
|
|
|
top.polyPath += "1";
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "Moving" << top.trashPath << "back to" << top.polyPath;
|
|
|
|
QFile(top.trashPath).rename(top.polyPath);
|
|
|
|
|
|
|
|
m_instanceGroupIndex[top.id] = top.groupName;
|
|
|
|
m_groupNameCache.insert(top.groupName);
|
|
|
|
|
|
|
|
saveGroupList();
|
|
|
|
emit instancesChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:19:26 +05:30
|
|
|
void InstanceList::deleteInstance(const InstanceId& id)
|
|
|
|
{
|
|
|
|
auto inst = getInstanceById(id);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!inst) {
|
2019-06-17 20:15:32 +05:30
|
|
|
qDebug() << "Cannot delete instance" << id << ". No such instance is present (deleted externally?).";
|
2018-07-23 04:19:26 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
if (m_instanceGroupIndex.remove(id)) {
|
2018-07-31 05:24:08 +05:30
|
|
|
saveGroupList();
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:19:26 +05:30
|
|
|
qDebug() << "Will delete instance" << id;
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!FS::deletePath(inst->instanceRoot())) {
|
2018-07-23 04:19:26 +05:30
|
|
|
qWarning() << "Deletion of instance" << id << "has not been completely successful ...";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-18 04:17:02 +05:30
|
|
|
qDebug() << "Instance" << id << "has been deleted by the launcher.";
|
2018-07-23 04:19:26 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
static QMap<InstanceId, InstanceLocator> getIdMapping(const QList<InstancePtr>& list)
|
2013-08-26 02:18:41 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QMap<InstanceId, InstanceLocator> out;
|
|
|
|
int i = 0;
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto& item : list) {
|
2018-07-15 18:21:05 +05:30
|
|
|
auto id = item->id();
|
2022-07-30 23:12:33 +05:30
|
|
|
if (out.contains(id)) {
|
2018-07-15 18:21:05 +05:30
|
|
|
qWarning() << "Duplicate ID" << id << "in instance list";
|
|
|
|
}
|
|
|
|
out[id] = std::make_pair(item, i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return out;
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
2016-04-11 05:00:50 +05:30
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
QList<InstanceId> InstanceList::discoverInstances()
|
2018-07-24 04:54:06 +05:30
|
|
|
{
|
|
|
|
qDebug() << "Discovering instances in" << m_instDir;
|
|
|
|
QList<InstanceId> out;
|
|
|
|
QDirIterator iter(m_instDir, QDir::Dirs | QDir::NoDot | QDir::NoDotDot | QDir::Readable | QDir::Hidden, QDirIterator::FollowSymlinks);
|
2022-07-30 23:12:33 +05:30
|
|
|
while (iter.hasNext()) {
|
2018-07-24 04:54:06 +05:30
|
|
|
QString subDir = iter.next();
|
|
|
|
QFileInfo dirInfo(subDir);
|
|
|
|
if (!QFileInfo(FS::PathCombine(subDir, "instance.cfg")).exists())
|
|
|
|
continue;
|
|
|
|
// if it is a symlink, ignore it if it goes to the instance folder
|
2022-07-30 23:12:33 +05:30
|
|
|
if (dirInfo.isSymLink()) {
|
2018-07-24 04:54:06 +05:30
|
|
|
QFileInfo targetInfo(dirInfo.symLinkTarget());
|
|
|
|
QFileInfo instDirInfo(m_instDir);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (targetInfo.canonicalPath() == instDirInfo.canonicalFilePath()) {
|
2018-07-24 04:54:06 +05:30
|
|
|
qDebug() << "Ignoring symlink" << subDir << "that leads into the instances folder";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto id = dirInfo.fileName();
|
|
|
|
out.append(id);
|
|
|
|
qDebug() << "Found instance ID" << id;
|
|
|
|
}
|
2022-05-02 22:40:45 +05:30
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
instanceSet = QSet<QString>(out.begin(), out.end());
|
|
|
|
#else
|
2018-07-24 04:54:06 +05:30
|
|
|
instanceSet = out.toSet();
|
2022-05-02 22:40:45 +05:30
|
|
|
#endif
|
2018-07-24 04:54:06 +05:30
|
|
|
m_instancesProbed = true;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-07-24 03:41:24 +05:30
|
|
|
InstanceList::InstListError InstanceList::loadList()
|
2016-10-03 04:25:54 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto existingIds = getIdMapping(m_instances);
|
2013-12-15 22:40:51 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QList<InstancePtr> newList;
|
2013-12-15 22:40:51 +05:30
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto& id : discoverInstances()) {
|
|
|
|
if (existingIds.contains(id)) {
|
2018-07-24 03:41:24 +05:30
|
|
|
auto instPair = existingIds[id];
|
|
|
|
existingIds.remove(id);
|
|
|
|
qDebug() << "Should keep and soft-reload" << id;
|
2022-07-30 23:12:33 +05:30
|
|
|
} else {
|
2018-07-24 04:54:06 +05:30
|
|
|
InstancePtr instPtr = loadInstance(id);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (instPtr) {
|
2018-07-24 03:41:24 +05:30
|
|
|
newList.append(instPtr);
|
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
2016-10-03 04:25:54 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// TODO: looks like a general algorithm with a few specifics inserted. Do something about it.
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!existingIds.isEmpty()) {
|
2018-07-15 18:21:05 +05:30
|
|
|
// get the list of removed instances and sort it by their original index, from last to first
|
|
|
|
auto deadList = existingIds.values();
|
2022-07-30 23:12:33 +05:30
|
|
|
auto orderSortPredicate = [](const InstanceLocator& a, const InstanceLocator& b) -> bool { return a.second > b.second; };
|
2018-07-15 18:21:05 +05:30
|
|
|
std::sort(deadList.begin(), deadList.end(), orderSortPredicate);
|
|
|
|
// remove the contiguous ranges of rows
|
|
|
|
int front_bookmark = -1;
|
|
|
|
int back_bookmark = -1;
|
|
|
|
int currentItem = -1;
|
2022-07-30 23:12:33 +05:30
|
|
|
auto removeNow = [&]() {
|
2018-07-15 18:21:05 +05:30
|
|
|
beginRemoveRows(QModelIndex(), front_bookmark, back_bookmark);
|
|
|
|
m_instances.erase(m_instances.begin() + front_bookmark, m_instances.begin() + back_bookmark + 1);
|
|
|
|
endRemoveRows();
|
|
|
|
front_bookmark = -1;
|
|
|
|
back_bookmark = currentItem;
|
|
|
|
};
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto& removedItem : deadList) {
|
2018-07-15 18:21:05 +05:30
|
|
|
auto instPtr = removedItem.first;
|
|
|
|
instPtr->invalidate();
|
|
|
|
currentItem = removedItem.second;
|
2022-07-30 23:12:33 +05:30
|
|
|
if (back_bookmark == -1) {
|
2018-07-15 18:21:05 +05:30
|
|
|
// no bookmark yet
|
|
|
|
back_bookmark = currentItem;
|
2022-07-30 23:12:33 +05:30
|
|
|
} else if (currentItem == front_bookmark - 1) {
|
2018-07-15 18:21:05 +05:30
|
|
|
// part of contiguous sequence, continue
|
2022-07-30 23:12:33 +05:30
|
|
|
} else {
|
2018-07-15 18:21:05 +05:30
|
|
|
// seam between previous and current item
|
|
|
|
removeNow();
|
|
|
|
}
|
|
|
|
front_bookmark = currentItem;
|
|
|
|
}
|
2022-07-30 23:12:33 +05:30
|
|
|
if (back_bookmark != -1) {
|
2018-07-15 18:21:05 +05:30
|
|
|
removeNow();
|
|
|
|
}
|
|
|
|
}
|
2022-07-30 23:12:33 +05:30
|
|
|
if (newList.size()) {
|
2018-07-15 18:21:05 +05:30
|
|
|
add(newList);
|
|
|
|
}
|
2018-07-24 03:41:24 +05:30
|
|
|
m_dirty = false;
|
2019-07-10 07:53:24 +05:30
|
|
|
updateTotalPlayTime();
|
2018-07-15 18:21:05 +05:30
|
|
|
return NoError;
|
2013-08-26 02:18:41 +05:30
|
|
|
}
|
|
|
|
|
2019-07-10 07:53:24 +05:30
|
|
|
void InstanceList::updateTotalPlayTime()
|
|
|
|
{
|
|
|
|
totalPlayTime = 0;
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto const& itr : m_instances) {
|
2019-07-10 07:53:24 +05:30
|
|
|
totalPlayTime += itr.get()->totalTimePlayed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 23:06:28 +05:30
|
|
|
void InstanceList::saveNow()
|
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto& item : m_instances) {
|
2018-07-15 18:21:05 +05:30
|
|
|
item->saveNow();
|
|
|
|
}
|
2017-12-03 23:06:28 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
void InstanceList::add(const QList<InstancePtr>& t)
|
2013-01-15 05:12:38 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
beginInsertRows(QModelIndex(), m_instances.count(), m_instances.count() + t.size() - 1);
|
|
|
|
m_instances.append(t);
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto& ptr : t) {
|
2018-07-15 18:21:05 +05:30
|
|
|
connect(ptr.get(), &BaseInstance::propertiesChanged, this, &InstanceList::propertiesChanged);
|
|
|
|
}
|
|
|
|
endInsertRows();
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2016-10-03 04:25:54 +05:30
|
|
|
void InstanceList::resumeWatch()
|
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
if (m_watchLevel > 0) {
|
2018-07-15 18:21:05 +05:30
|
|
|
qWarning() << "Bad suspend level resume in instance list";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_watchLevel++;
|
2022-07-30 23:12:33 +05:30
|
|
|
if (m_watchLevel > 0 && m_dirty) {
|
2018-07-15 18:21:05 +05:30
|
|
|
loadList();
|
|
|
|
}
|
2013-07-28 12:10:15 +05:30
|
|
|
}
|
|
|
|
|
2016-10-03 04:25:54 +05:30
|
|
|
void InstanceList::suspendWatch()
|
2013-07-28 12:10:15 +05:30
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
m_watchLevel--;
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
2013-10-06 04:43:40 +05:30
|
|
|
|
2016-10-03 04:25:54 +05:30
|
|
|
void InstanceList::providerUpdated()
|
|
|
|
{
|
2018-07-24 03:41:24 +05:30
|
|
|
m_dirty = true;
|
2022-07-30 23:12:33 +05:30
|
|
|
if (m_watchLevel == 1) {
|
2018-07-15 18:21:05 +05:30
|
|
|
loadList();
|
|
|
|
}
|
2013-10-06 04:43:40 +05:30
|
|
|
}
|
2013-10-29 01:25:12 +05:30
|
|
|
|
2013-11-27 19:30:44 +05:30
|
|
|
InstancePtr InstanceList::getInstanceById(QString instId) const
|
2013-03-19 03:30:46 +05:30
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
if (instId.isEmpty())
|
2018-07-15 18:21:05 +05:30
|
|
|
return InstancePtr();
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto& inst : m_instances) {
|
|
|
|
if (inst->id() == instId) {
|
2018-07-15 18:21:05 +05:30
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return InstancePtr();
|
2013-03-19 10:54:34 +05:30
|
|
|
}
|
|
|
|
|
2022-09-26 15:20:31 +05:30
|
|
|
InstancePtr InstanceList::getInstanceByManagedName(const QString& managed_name) const
|
2022-07-08 21:30:44 +05:30
|
|
|
{
|
|
|
|
if (managed_name.isEmpty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
for (auto instance : m_instances) {
|
|
|
|
if (instance->getManagedPackName() == managed_name)
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex InstanceList::getInstanceIndexById(const QString &id) const
|
2013-11-27 19:30:44 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return index(getInstIndex(getInstanceById(id).get()));
|
2013-11-27 19:30:44 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
int InstanceList::getInstIndex(BaseInstance* inst) const
|
2013-03-19 10:54:34 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
int count = m_instances.count();
|
2022-07-30 23:12:33 +05:30
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (inst == m_instances[i].get()) {
|
2018-07-15 18:21:05 +05:30
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2013-05-04 01:11:37 +05:30
|
|
|
}
|
2013-08-26 10:00:11 +05:30
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
void InstanceList::propertiesChanged(BaseInstance* inst)
|
2013-08-26 10:00:11 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
int i = getInstIndex(inst);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (i != -1) {
|
2018-07-15 18:21:05 +05:30
|
|
|
emit dataChanged(index(i), index(i));
|
2019-07-10 07:53:24 +05:30
|
|
|
updateTotalPlayTime();
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2013-08-26 10:00:11 +05:30
|
|
|
}
|
2018-07-24 04:54:06 +05:30
|
|
|
|
|
|
|
InstancePtr InstanceList::loadInstance(const InstanceId& id)
|
|
|
|
{
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!m_groupsLoaded) {
|
2018-07-24 04:54:06 +05:30
|
|
|
loadGroupList();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto instanceRoot = FS::PathCombine(m_instDir, id);
|
|
|
|
auto instanceSettings = std::make_shared<INISettingsObject>(FS::PathCombine(instanceRoot, "instance.cfg"));
|
|
|
|
InstancePtr inst;
|
2022-06-04 15:29:12 +05:30
|
|
|
|
|
|
|
instanceSettings->registerSetting("InstanceType", "");
|
|
|
|
|
|
|
|
QString inst_type = instanceSettings->get("InstanceType").toString();
|
|
|
|
|
|
|
|
// NOTE: Some PolyMC versions didn't save the InstanceType properly. We will just bank on the probability that this is probably a OneSix instance
|
|
|
|
if (inst_type == "OneSix" || inst_type.isEmpty())
|
|
|
|
{
|
|
|
|
inst.reset(new MinecraftInstance(m_globalSettings, instanceSettings, instanceRoot));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inst.reset(new NullInstance(m_globalSettings, instanceSettings, instanceRoot));
|
|
|
|
}
|
2018-07-24 04:54:06 +05:30
|
|
|
qDebug() << "Loaded instance " << inst->name() << " from " << inst->instanceRoot();
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceList::saveGroupList()
|
|
|
|
{
|
|
|
|
qDebug() << "Will save group list now.";
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!m_instancesProbed) {
|
2018-07-24 04:54:06 +05:30
|
|
|
qDebug() << "Group saving prevented because we don't know the full list of instances yet.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WatchLock foo(m_watcher, m_instDir);
|
|
|
|
QString groupFileName = m_instDir + "/instgroups.json";
|
|
|
|
QMap<QString, QSet<QString>> reverseGroupMap;
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto iter = m_instanceGroupIndex.begin(); iter != m_instanceGroupIndex.end(); iter++) {
|
2018-07-24 04:54:06 +05:30
|
|
|
QString id = iter.key();
|
|
|
|
QString group = iter.value();
|
|
|
|
if (group.isEmpty())
|
|
|
|
continue;
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!instanceSet.contains(id)) {
|
2018-07-24 04:54:06 +05:30
|
|
|
qDebug() << "Skipping saving missing instance" << id << "to groups list.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!reverseGroupMap.count(group)) {
|
2018-07-24 04:54:06 +05:30
|
|
|
QSet<QString> set;
|
|
|
|
set.insert(id);
|
|
|
|
reverseGroupMap[group] = set;
|
2022-07-30 23:12:33 +05:30
|
|
|
} else {
|
|
|
|
QSet<QString>& set = reverseGroupMap[group];
|
2018-07-24 04:54:06 +05:30
|
|
|
set.insert(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QJsonObject toplevel;
|
|
|
|
toplevel.insert("formatVersion", QJsonValue(QString("1")));
|
|
|
|
QJsonObject groupsArr;
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto iter = reverseGroupMap.begin(); iter != reverseGroupMap.end(); iter++) {
|
2018-07-24 04:54:06 +05:30
|
|
|
auto list = iter.value();
|
|
|
|
auto name = iter.key();
|
|
|
|
QJsonObject groupObj;
|
|
|
|
QJsonArray instanceArr;
|
2019-08-20 06:28:27 +05:30
|
|
|
groupObj.insert("hidden", QJsonValue(m_collapsedGroups.contains(name)));
|
2022-07-30 23:12:33 +05:30
|
|
|
for (auto item : list) {
|
2018-07-24 04:54:06 +05:30
|
|
|
instanceArr.append(QJsonValue(item));
|
|
|
|
}
|
|
|
|
groupObj.insert("instances", instanceArr);
|
|
|
|
groupsArr.insert(name, groupObj);
|
|
|
|
}
|
|
|
|
toplevel.insert("groups", groupsArr);
|
|
|
|
QJsonDocument doc(toplevel);
|
2022-07-30 23:12:33 +05:30
|
|
|
try {
|
2018-07-24 04:54:06 +05:30
|
|
|
FS::write(groupFileName, doc.toJson());
|
|
|
|
qDebug() << "Group list saved.";
|
2022-07-30 23:12:33 +05:30
|
|
|
} catch (const FS::FileSystemException& e) {
|
2018-07-24 04:54:06 +05:30
|
|
|
qCritical() << "Failed to write instance group file :" << e.cause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceList::loadGroupList()
|
|
|
|
{
|
|
|
|
qDebug() << "Will load group list now.";
|
|
|
|
|
|
|
|
QString groupFileName = m_instDir + "/instgroups.json";
|
|
|
|
|
|
|
|
// if there's no group file, fail
|
|
|
|
if (!QFileInfo(groupFileName).exists())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QByteArray jsonData;
|
2022-07-30 23:12:33 +05:30
|
|
|
try {
|
2018-07-24 04:54:06 +05:30
|
|
|
jsonData = FS::read(groupFileName);
|
2022-07-30 23:12:33 +05:30
|
|
|
} catch (const FS::FileSystemException& e) {
|
2018-07-24 04:54:06 +05:30
|
|
|
qCritical() << "Failed to read instance group file :" << e.cause();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error);
|
|
|
|
|
|
|
|
// if the json was bad, fail
|
2022-07-30 23:12:33 +05:30
|
|
|
if (error.error != QJsonParseError::NoError) {
|
2018-07-24 04:54:06 +05:30
|
|
|
qCritical() << QString("Failed to parse instance group file: %1 at offset %2")
|
2022-07-30 23:12:33 +05:30
|
|
|
.arg(error.errorString(), QString::number(error.offset))
|
|
|
|
.toUtf8();
|
2018-07-24 04:54:06 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the root of the json wasn't an object, fail
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!jsonDoc.isObject()) {
|
2018-07-24 04:54:06 +05:30
|
|
|
qWarning() << "Invalid group file. Root entry should be an object.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject rootObj = jsonDoc.object();
|
|
|
|
|
|
|
|
// Make sure the format version matches, otherwise fail.
|
|
|
|
if (rootObj.value("formatVersion").toVariant().toInt() != GROUP_FILE_FORMAT_VERSION)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the groups. if it's not an object, fail
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!rootObj.value("groups").isObject()) {
|
2018-07-24 04:54:06 +05:30
|
|
|
qWarning() << "Invalid group list JSON: 'groups' should be an object.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-20 06:28:27 +05:30
|
|
|
QSet<QString> groupSet;
|
|
|
|
m_instanceGroupIndex.clear();
|
2018-07-24 04:54:06 +05:30
|
|
|
|
|
|
|
// Iterate through all the groups.
|
|
|
|
QJsonObject groupMapping = rootObj.value("groups").toObject();
|
2022-07-30 23:12:33 +05:30
|
|
|
for (QJsonObject::iterator iter = groupMapping.begin(); iter != groupMapping.end(); iter++) {
|
2018-07-24 04:54:06 +05:30
|
|
|
QString groupName = iter.key();
|
|
|
|
|
|
|
|
// If not an object, complain and skip to the next one.
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!iter.value().isObject()) {
|
2019-08-20 06:28:27 +05:30
|
|
|
qWarning() << QString("Group '%1' in the group list should be an object.").arg(groupName).toUtf8();
|
2018-07-24 04:54:06 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject groupObj = iter.value().toObject();
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!groupObj.value("instances").isArray()) {
|
|
|
|
qWarning() << QString("Group '%1' in the group list is invalid. It should contain an array called 'instances'.")
|
|
|
|
.arg(groupName)
|
|
|
|
.toUtf8();
|
2018-07-24 04:54:06 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep a list/set of groups for choosing
|
|
|
|
groupSet.insert(groupName);
|
|
|
|
|
2019-08-20 06:28:27 +05:30
|
|
|
auto hidden = groupObj.value("hidden").toBool(false);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (hidden) {
|
2019-08-20 06:28:27 +05:30
|
|
|
m_collapsedGroups.insert(groupName);
|
|
|
|
}
|
|
|
|
|
2018-07-24 04:54:06 +05:30
|
|
|
// Iterate through the list of instances in the group.
|
|
|
|
QJsonArray instancesArray = groupObj.value("instances").toArray();
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
for (QJsonArray::iterator iter2 = instancesArray.begin(); iter2 != instancesArray.end(); iter2++) {
|
2019-08-20 06:28:27 +05:30
|
|
|
m_instanceGroupIndex[(*iter2).toString()] = groupName;
|
2018-07-24 04:54:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
m_groupsLoaded = true;
|
2019-08-20 06:28:27 +05:30
|
|
|
m_groupNameCache.unite(groupSet);
|
2018-07-24 04:54:06 +05:30
|
|
|
qDebug() << "Group list loaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceList::instanceDirContentsChanged(const QString& path)
|
|
|
|
{
|
|
|
|
Q_UNUSED(path);
|
|
|
|
emit instancesChanged();
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
void InstanceList::on_InstFolderChanged(const Setting& setting, QVariant value)
|
2018-07-24 04:54:06 +05:30
|
|
|
{
|
|
|
|
QString newInstDir = QDir(value.toString()).canonicalPath();
|
2022-07-30 23:12:33 +05:30
|
|
|
if (newInstDir != m_instDir) {
|
|
|
|
if (m_groupsLoaded) {
|
2018-07-24 04:54:06 +05:30
|
|
|
saveGroupList();
|
|
|
|
}
|
|
|
|
m_instDir = newInstDir;
|
|
|
|
m_groupsLoaded = false;
|
|
|
|
emit instancesChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 06:28:27 +05:30
|
|
|
void InstanceList::on_GroupStateChanged(const QString& group, bool collapsed)
|
|
|
|
{
|
|
|
|
qDebug() << "Group" << group << (collapsed ? "collapsed" : "expanded");
|
2022-07-30 23:12:33 +05:30
|
|
|
if (collapsed) {
|
2019-08-20 06:28:27 +05:30
|
|
|
m_collapsedGroups.insert(group);
|
|
|
|
} else {
|
|
|
|
m_collapsedGroups.remove(group);
|
|
|
|
}
|
|
|
|
saveGroupList();
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
class InstanceStaging : public Task {
|
|
|
|
Q_OBJECT
|
2018-07-24 04:54:06 +05:30
|
|
|
const unsigned minBackoff = 1;
|
|
|
|
const unsigned maxBackoff = 16;
|
2022-07-30 23:12:33 +05:30
|
|
|
public:
|
2022-07-15 00:43:23 +05:30
|
|
|
InstanceStaging(InstanceList* parent, InstanceTask* child, QString stagingPath, InstanceName const& instanceName, QString groupName)
|
|
|
|
: m_parent(parent), backoff(minBackoff, maxBackoff), m_stagingPath(std::move(stagingPath)), m_instance_name(std::move(instanceName)), m_groupName(std::move(groupName))
|
2018-07-24 04:54:06 +05:30
|
|
|
{
|
|
|
|
m_child.reset(child);
|
|
|
|
connect(child, &Task::succeeded, this, &InstanceStaging::childSucceded);
|
|
|
|
connect(child, &Task::failed, this, &InstanceStaging::childFailed);
|
2022-07-29 00:28:04 +05:30
|
|
|
connect(child, &Task::aborted, this, &InstanceStaging::childAborted);
|
2022-08-21 17:56:27 +05:30
|
|
|
connect(child, &Task::abortStatusChanged, this, &InstanceStaging::setAbortable);
|
2018-07-24 04:54:06 +05:30
|
|
|
connect(child, &Task::status, this, &InstanceStaging::setStatus);
|
|
|
|
connect(child, &Task::progress, this, &InstanceStaging::setProgress);
|
|
|
|
connect(&m_backoffTimer, &QTimer::timeout, this, &InstanceStaging::childSucceded);
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
virtual ~InstanceStaging(){};
|
2018-12-11 01:19:21 +05:30
|
|
|
|
|
|
|
// FIXME/TODO: add ability to abort during instance commit retries
|
|
|
|
bool abort() override
|
|
|
|
{
|
2022-08-06 05:55:21 +05:30
|
|
|
if (!canAbort())
|
|
|
|
return false;
|
2022-07-29 00:28:04 +05:30
|
|
|
|
2022-08-06 05:55:21 +05:30
|
|
|
m_child->abort();
|
|
|
|
|
|
|
|
return Task::abort();
|
2018-12-11 01:19:21 +05:30
|
|
|
}
|
|
|
|
bool canAbort() const override
|
|
|
|
{
|
2022-07-29 00:28:04 +05:30
|
|
|
return (m_child && m_child->canAbort());
|
2018-12-11 01:19:21 +05:30
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
protected:
|
|
|
|
virtual void executeTask() override { m_child->start(); }
|
|
|
|
QStringList warnings() const override { return m_child->warnings(); }
|
2018-07-24 04:54:06 +05:30
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
private slots:
|
2018-07-24 04:54:06 +05:30
|
|
|
void childSucceded()
|
|
|
|
{
|
|
|
|
unsigned sleepTime = backoff();
|
2022-12-03 18:45:38 +05:30
|
|
|
if (m_parent->commitStagedInstance(m_stagingPath, m_instance_name, m_groupName, *m_child.get()))
|
2022-07-08 21:30:44 +05:30
|
|
|
{
|
2018-07-24 04:54:06 +05:30
|
|
|
emitSucceeded();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// we actually failed, retry?
|
2022-07-30 23:12:33 +05:30
|
|
|
if (sleepTime == maxBackoff) {
|
2018-07-24 04:54:06 +05:30
|
|
|
emitFailed(tr("Failed to commit instance, even after multiple retries. It is being blocked by something."));
|
|
|
|
return;
|
|
|
|
}
|
2022-07-15 00:43:23 +05:30
|
|
|
qDebug() << "Failed to commit instance" << m_instance_name.name() << "Initiating backoff:" << sleepTime;
|
2018-07-24 04:54:06 +05:30
|
|
|
m_backoffTimer.start(sleepTime * 500);
|
|
|
|
}
|
2022-07-30 23:12:33 +05:30
|
|
|
void childFailed(const QString& reason)
|
2018-07-24 04:54:06 +05:30
|
|
|
{
|
|
|
|
m_parent->destroyStagingPath(m_stagingPath);
|
|
|
|
emitFailed(reason);
|
|
|
|
}
|
|
|
|
|
2022-07-29 00:28:04 +05:30
|
|
|
void childAborted()
|
|
|
|
{
|
|
|
|
emitAborted();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-07-15 00:43:23 +05:30
|
|
|
InstanceList * m_parent;
|
2018-07-24 04:54:06 +05:30
|
|
|
/*
|
|
|
|
* WHY: the whole reason why this uses an exponential backoff retry scheme is antivirus on Windows.
|
2021-10-18 04:17:02 +05:30
|
|
|
* Basically, it starts messing things up while the launcher is extracting/creating instances
|
2018-07-24 04:54:06 +05:30
|
|
|
* and causes that horrible failure that is NTFS to lock files in place because they are open.
|
|
|
|
*/
|
|
|
|
ExponentialSeries backoff;
|
|
|
|
QString m_stagingPath;
|
2022-07-08 21:30:44 +05:30
|
|
|
unique_qobject_ptr<InstanceTask> m_child;
|
2022-07-15 00:43:23 +05:30
|
|
|
InstanceName m_instance_name;
|
2018-07-24 04:54:06 +05:30
|
|
|
QString m_groupName;
|
|
|
|
QTimer m_backoffTimer;
|
|
|
|
};
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
Task* InstanceList::wrapInstanceTask(InstanceTask* task)
|
2018-07-24 04:54:06 +05:30
|
|
|
{
|
|
|
|
auto stagingPath = getStagedInstancePath();
|
|
|
|
task->setStagingPath(stagingPath);
|
|
|
|
task->setParentSettings(m_globalSettings);
|
2022-07-15 00:43:23 +05:30
|
|
|
return new InstanceStaging(this, task, stagingPath, *task, task->group());
|
2018-07-24 04:54:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QString InstanceList::getStagedInstancePath()
|
|
|
|
{
|
2023-02-24 07:38:35 +05:30
|
|
|
QString key = QUuid::createUuid().toString(QUuid::WithoutBraces);
|
2022-05-10 01:28:08 +05:30
|
|
|
QString tempDir = ".LAUNCHER_TEMP/";
|
|
|
|
QString relPath = FS::PathCombine(tempDir, key);
|
2018-07-24 04:54:06 +05:30
|
|
|
QDir rootPath(m_instDir);
|
|
|
|
auto path = FS::PathCombine(m_instDir, relPath);
|
2022-07-30 23:12:33 +05:30
|
|
|
if (!rootPath.mkpath(relPath)) {
|
2018-07-24 04:54:06 +05:30
|
|
|
return QString();
|
|
|
|
}
|
2022-05-10 01:28:08 +05:30
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
auto tempPath = FS::PathCombine(m_instDir, tempDir);
|
|
|
|
SetFileAttributesA(tempPath.toStdString().c_str(), FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED);
|
|
|
|
#endif
|
2018-07-24 04:54:06 +05:30
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2022-12-03 18:45:38 +05:30
|
|
|
bool InstanceList::commitStagedInstance(const QString& path, InstanceName const& instanceName, const QString& groupName, InstanceTask const& commiting)
|
2018-07-24 04:54:06 +05:30
|
|
|
{
|
|
|
|
QDir dir;
|
2022-07-08 21:30:44 +05:30
|
|
|
QString instID;
|
|
|
|
InstancePtr inst;
|
|
|
|
|
2022-12-03 18:45:38 +05:30
|
|
|
auto should_override = commiting.shouldOverride();
|
|
|
|
|
2022-07-08 21:30:44 +05:30
|
|
|
if (should_override) {
|
2022-12-03 18:45:38 +05:30
|
|
|
instID = commiting.originalInstanceID();
|
2022-07-08 21:30:44 +05:30
|
|
|
} else {
|
2022-07-15 00:43:23 +05:30
|
|
|
instID = FS::DirNameFromString(instanceName.modifiedName(), m_instDir);
|
2022-07-08 21:30:44 +05:30
|
|
|
}
|
|
|
|
|
2022-12-03 18:45:38 +05:30
|
|
|
Q_ASSERT(!instID.isEmpty());
|
|
|
|
|
2018-07-24 04:54:06 +05:30
|
|
|
{
|
|
|
|
WatchLock lock(m_watcher, m_instDir);
|
|
|
|
QString destination = FS::PathCombine(m_instDir, instID);
|
2022-07-08 21:30:44 +05:30
|
|
|
|
|
|
|
if (should_override) {
|
|
|
|
if (!FS::overrideFolder(destination, path)) {
|
|
|
|
qWarning() << "Failed to override" << path << "to" << destination;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!dir.rename(path, destination)) {
|
|
|
|
qWarning() << "Failed to move" << path << "to" << destination;
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-11 21:54:58 +05:30
|
|
|
|
|
|
|
m_instanceGroupIndex[instID] = groupName;
|
|
|
|
m_groupNameCache.insert(groupName);
|
2018-07-24 04:54:06 +05:30
|
|
|
}
|
2022-09-11 21:54:58 +05:30
|
|
|
|
2022-07-08 21:30:44 +05:30
|
|
|
instanceSet.insert(instID);
|
|
|
|
|
2018-07-24 04:54:06 +05:30
|
|
|
emit instancesChanged();
|
2019-06-23 18:24:17 +05:30
|
|
|
emit instanceSelectRequest(instID);
|
2018-07-24 04:54:06 +05:30
|
|
|
}
|
2022-07-08 21:30:44 +05:30
|
|
|
|
2018-07-24 04:54:06 +05:30
|
|
|
saveGroupList();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InstanceList::destroyStagingPath(const QString& keyPath)
|
|
|
|
{
|
|
|
|
return FS::deletePath(keyPath);
|
|
|
|
}
|
|
|
|
|
2022-07-30 23:12:33 +05:30
|
|
|
int InstanceList::getTotalPlayTime()
|
|
|
|
{
|
2019-07-10 07:53:24 +05:30
|
|
|
updateTotalPlayTime();
|
|
|
|
return totalPlayTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "InstanceList.moc"
|