2021-01-18 07:28:54 +00:00
|
|
|
/* Copyright 2013-2021 MultiMC Contributors
|
2013-01-14 23:42:38 +00:00
|
|
|
*
|
|
|
|
* 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
|
2013-10-05 23:13:40 +00:00
|
|
|
*
|
2013-01-14 23:42:38 +00:00
|
|
|
* 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-02-19 18:15:22 +00:00
|
|
|
#include <QDir>
|
2018-07-23 23:24:06 +00:00
|
|
|
#include <QDirIterator>
|
2013-08-25 20:48:41 +00:00
|
|
|
#include <QSet>
|
2013-02-19 18:15:22 +00:00
|
|
|
#include <QFile>
|
2013-03-18 22:00:46 +00:00
|
|
|
#include <QThread>
|
2013-03-18 22:35:54 +00:00
|
|
|
#include <QTextStream>
|
2013-12-20 13:47:26 +00:00
|
|
|
#include <QXmlStreamReader>
|
2018-07-23 23:24:06 +00:00
|
|
|
#include <QTimer>
|
2015-02-04 20:10:10 +00:00
|
|
|
#include <QDebug>
|
2018-07-23 23:24:06 +00:00
|
|
|
#include <QFileSystemWatcher>
|
|
|
|
#include <QUuid>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
2013-02-19 18:15:22 +00:00
|
|
|
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "InstanceList.h"
|
|
|
|
#include "BaseInstance.h"
|
2018-07-23 23:24:06 +00:00
|
|
|
#include "InstanceTask.h"
|
|
|
|
#include "settings/INISettingsObject.h"
|
|
|
|
#include "minecraft/legacy/LegacyInstance.h"
|
|
|
|
#include "NullInstance.h"
|
|
|
|
#include "minecraft/MinecraftInstance.h"
|
2018-07-22 22:49:26 +00:00
|
|
|
#include "FileSystem.h"
|
2018-07-23 23:24:06 +00:00
|
|
|
#include "ExponentialSeries.h"
|
|
|
|
#include "WatchLock.h"
|
|
|
|
|
|
|
|
const static int GROUP_FILE_FORMAT_VERSION = 1;
|
2013-02-19 18:15:22 +00:00
|
|
|
|
2018-07-23 23:24:06 +00:00
|
|
|
InstanceList::InstanceList(SettingsObjectPtr settings, const QString & instDir, QObject *parent)
|
|
|
|
: QAbstractListModel(parent), m_globalSettings(settings)
|
2013-02-19 18:15:22 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
resumeWatch();
|
2018-07-23 23:24:06 +00:00
|
|
|
// Create aand normalize path
|
|
|
|
if (!QDir::current().exists(instDir))
|
|
|
|
{
|
|
|
|
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 18:15:22 +00:00
|
|
|
}
|
|
|
|
|
2013-08-25 20:48:41 +00:00
|
|
|
InstanceList::~InstanceList()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-05 23:13:40 +00:00
|
|
|
int InstanceList::rowCount(const QModelIndex &parent) const
|
2013-08-26 04:30:11 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
Q_UNUSED(parent);
|
|
|
|
return m_instances.count();
|
2013-08-26 04:30:11 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 23:13:40 +00:00
|
|
|
QModelIndex InstanceList::index(int row, int column, const QModelIndex &parent) const
|
2013-08-26 04:30:11 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
Q_UNUSED(parent);
|
|
|
|
if (row < 0 || row >= m_instances.size())
|
|
|
|
return QModelIndex();
|
|
|
|
return createIndex(row, column, (void *)m_instances.at(row).get());
|
2013-08-26 04:30:11 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 23:13:40 +00:00
|
|
|
QVariant InstanceList::data(const QModelIndex &index, int role) const
|
2013-08-26 04:30:11 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
BaseInstance *pdata = static_cast<BaseInstance *>(index.internalPointer());
|
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case InstancePointerRole:
|
|
|
|
{
|
|
|
|
QVariant v = qVariantFromValue((void *)pdata);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
case InstanceIDRole:
|
2014-03-30 18:11:05 +00:00
|
|
|
{
|
|
|
|
return pdata->id();
|
|
|
|
}
|
2018-11-22 00:50:32 +00:00
|
|
|
case Qt::EditRole:
|
2018-07-15 12:51:05 +00:00
|
|
|
case Qt::DisplayRole:
|
|
|
|
{
|
|
|
|
return pdata->name();
|
|
|
|
}
|
2019-07-21 19:12:05 +00:00
|
|
|
case Qt::AccessibleTextRole:
|
|
|
|
{
|
|
|
|
return tr("%1 Instance").arg(pdata->name());
|
|
|
|
}
|
2018-07-15 12:51:05 +00:00
|
|
|
case Qt::ToolTipRole:
|
|
|
|
{
|
|
|
|
return pdata->instanceRoot();
|
|
|
|
}
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
{
|
|
|
|
return pdata->iconKey();
|
|
|
|
}
|
|
|
|
// HACK: see GroupView.h in gui!
|
|
|
|
case GroupRole:
|
|
|
|
{
|
2018-07-30 23:54:08 +00:00
|
|
|
return getInstanceGroup(pdata->id());
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
2013-08-26 04:30:11 +00:00
|
|
|
}
|
|
|
|
|
2018-11-22 00:50:32 +00:00
|
|
|
bool InstanceList::setData(const QModelIndex& index, const QVariant& value, int role)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(role != Qt::EditRole)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BaseInstance *pdata = static_cast<BaseInstance *>(index.internalPointer());
|
|
|
|
auto newName = value.toString();
|
|
|
|
if(pdata->name() == newName)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
pdata->setName(newName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-05 23:13:40 +00:00
|
|
|
Qt::ItemFlags InstanceList::flags(const QModelIndex &index) const
|
2013-08-26 04:30:11 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
Qt::ItemFlags f;
|
|
|
|
if (index.isValid())
|
|
|
|
{
|
2018-11-22 00:50:32 +00:00
|
|
|
f |= (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
|
|
|
return f;
|
2013-08-26 04:30:11 +00:00
|
|
|
}
|
2013-08-25 20:48:41 +00:00
|
|
|
|
2018-07-30 23:54:08 +00:00
|
|
|
GroupId InstanceList::getInstanceGroup(const InstanceId& id) const
|
|
|
|
{
|
|
|
|
auto inst = getInstanceById(id);
|
|
|
|
if(!inst)
|
|
|
|
{
|
|
|
|
return GroupId();
|
|
|
|
}
|
2019-08-20 00:58:27 +00:00
|
|
|
auto iter = m_instanceGroupIndex.find(inst->id());
|
|
|
|
if(iter != m_instanceGroupIndex.end())
|
2018-07-30 23:54:08 +00:00
|
|
|
{
|
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
return GroupId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceList::setInstanceGroup(const InstanceId& id, const GroupId& name)
|
|
|
|
{
|
|
|
|
auto inst = getInstanceById(id);
|
|
|
|
if(!inst)
|
|
|
|
{
|
|
|
|
qDebug() << "Attempt to set a null instance's group";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool changed = false;
|
2019-08-20 00:58:27 +00:00
|
|
|
auto iter = m_instanceGroupIndex.find(inst->id());
|
|
|
|
if(iter != m_instanceGroupIndex.end())
|
2018-07-30 23:54:08 +00:00
|
|
|
{
|
|
|
|
if(*iter != name)
|
|
|
|
{
|
|
|
|
*iter = name;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
changed = true;
|
2019-08-20 00:58:27 +00:00
|
|
|
m_instanceGroupIndex[id] = name;
|
2018-07-30 23:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(changed)
|
|
|
|
{
|
2019-08-20 00:58:27 +00:00
|
|
|
m_groupNameCache.insert(name);
|
2018-11-11 22:54:16 +00:00
|
|
|
auto idx = getInstIndex(inst.get());
|
|
|
|
emit dataChanged(index(idx), index(idx), {GroupRole});
|
2018-07-30 23:54:08 +00:00
|
|
|
saveGroupList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-15 17:10:51 +00:00
|
|
|
QStringList InstanceList::getGroups()
|
|
|
|
{
|
2019-08-20 00:58:27 +00:00
|
|
|
return m_groupNameCache.toList();
|
2013-12-15 17:10:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 23:30:50 +00:00
|
|
|
void InstanceList::deleteGroup(const QString& name)
|
|
|
|
{
|
2018-07-30 23:54:08 +00:00
|
|
|
bool removed = false;
|
|
|
|
qDebug() << "Delete group" << name;
|
2018-07-15 12:51:05 +00:00
|
|
|
for(auto & instance: m_instances)
|
|
|
|
{
|
2018-07-30 23:54:08 +00:00
|
|
|
const auto & instID = instance->id();
|
|
|
|
auto instGroupName = getInstanceGroup(instID);
|
2018-07-15 12:51:05 +00:00
|
|
|
if(instGroupName == name)
|
|
|
|
{
|
2019-08-20 00:58:27 +00:00
|
|
|
m_instanceGroupIndex.remove(instID);
|
2018-07-30 23:54:08 +00:00
|
|
|
qDebug() << "Remove" << instID << "from group" << name;
|
|
|
|
removed = true;
|
|
|
|
auto idx = getInstIndex(instance.get());
|
|
|
|
if(idx > 0)
|
|
|
|
{
|
|
|
|
emit dataChanged(index(idx), index(idx), {GroupRole});
|
|
|
|
}
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 23:54:08 +00:00
|
|
|
if(removed)
|
|
|
|
{
|
|
|
|
saveGroupList();
|
|
|
|
}
|
2016-04-10 23:30:50 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 00:58:27 +00:00
|
|
|
bool InstanceList::isGroupCollapsed(const QString& group)
|
|
|
|
{
|
|
|
|
return m_collapsedGroups.contains(group);
|
|
|
|
}
|
|
|
|
|
2018-07-22 22:49:26 +00:00
|
|
|
void InstanceList::deleteInstance(const InstanceId& id)
|
|
|
|
{
|
|
|
|
auto inst = getInstanceById(id);
|
|
|
|
if(!inst)
|
|
|
|
{
|
2019-06-17 14:45:32 +00:00
|
|
|
qDebug() << "Cannot delete instance" << id << ". No such instance is present (deleted externally?).";
|
2018-07-22 22:49:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-20 00:58:27 +00:00
|
|
|
if(m_instanceGroupIndex.remove(id))
|
2018-07-30 23:54:08 +00:00
|
|
|
{
|
|
|
|
saveGroupList();
|
|
|
|
}
|
|
|
|
|
2018-07-22 22:49:26 +00:00
|
|
|
qDebug() << "Will delete instance" << id;
|
|
|
|
if(!FS::deletePath(inst->instanceRoot()))
|
|
|
|
{
|
|
|
|
qWarning() << "Deletion of instance" << id << "has not been completely successful ...";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "Instance" << id << "has been deleted by MultiMC.";
|
|
|
|
}
|
|
|
|
|
2016-10-02 22:55:54 +00:00
|
|
|
static QMap<InstanceId, InstanceLocator> getIdMapping(const QList<InstancePtr> &list)
|
2013-08-25 20:48:41 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QMap<InstanceId, InstanceLocator> out;
|
|
|
|
int i = 0;
|
|
|
|
for(auto & item: list)
|
|
|
|
{
|
|
|
|
auto id = item->id();
|
|
|
|
if(out.contains(id))
|
|
|
|
{
|
|
|
|
qWarning() << "Duplicate ID" << id << "in instance list";
|
|
|
|
}
|
|
|
|
out[id] = std::make_pair(item, i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return out;
|
2016-10-02 22:55:54 +00:00
|
|
|
}
|
2016-04-10 23:30:50 +00:00
|
|
|
|
2018-07-23 23:24:06 +00:00
|
|
|
QList< InstanceId > InstanceList::discoverInstances()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
while (iter.hasNext())
|
|
|
|
{
|
|
|
|
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
|
|
|
|
if(dirInfo.isSymLink())
|
|
|
|
{
|
|
|
|
QFileInfo targetInfo(dirInfo.symLinkTarget());
|
|
|
|
QFileInfo instDirInfo(m_instDir);
|
|
|
|
if(targetInfo.canonicalPath() == instDirInfo.canonicalFilePath())
|
|
|
|
{
|
|
|
|
qDebug() << "Ignoring symlink" << subDir << "that leads into the instances folder";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto id = dirInfo.fileName();
|
|
|
|
out.append(id);
|
|
|
|
qDebug() << "Found instance ID" << id;
|
|
|
|
}
|
|
|
|
instanceSet = out.toSet();
|
|
|
|
m_instancesProbed = true;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-07-23 22:11:24 +00:00
|
|
|
InstanceList::InstListError InstanceList::loadList()
|
2016-10-02 22:55:54 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
auto existingIds = getIdMapping(m_instances);
|
2013-12-15 17:10:51 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
QList<InstancePtr> newList;
|
2013-12-15 17:10:51 +00:00
|
|
|
|
2018-07-23 23:24:06 +00:00
|
|
|
for(auto & id: discoverInstances())
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
2018-07-23 22:11:24 +00:00
|
|
|
if(existingIds.contains(id))
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
2018-07-23 22:11:24 +00:00
|
|
|
auto instPair = existingIds[id];
|
|
|
|
existingIds.remove(id);
|
|
|
|
qDebug() << "Should keep and soft-reload" << id;
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
2018-07-23 22:11:24 +00:00
|
|
|
else
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
2018-07-23 23:24:06 +00:00
|
|
|
InstancePtr instPtr = loadInstance(id);
|
2018-07-23 22:11:24 +00:00
|
|
|
if(instPtr)
|
|
|
|
{
|
|
|
|
newList.append(instPtr);
|
|
|
|
}
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-02 22:55:54 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
// TODO: looks like a general algorithm with a few specifics inserted. Do something about it.
|
|
|
|
if(!existingIds.isEmpty())
|
|
|
|
{
|
|
|
|
// get the list of removed instances and sort it by their original index, from last to first
|
|
|
|
auto deadList = existingIds.values();
|
|
|
|
auto orderSortPredicate = [](const InstanceLocator & a, const InstanceLocator & b) -> bool
|
|
|
|
{
|
|
|
|
return a.second > b.second;
|
|
|
|
};
|
|
|
|
std::sort(deadList.begin(), deadList.end(), orderSortPredicate);
|
|
|
|
// remove the contiguous ranges of rows
|
|
|
|
int front_bookmark = -1;
|
|
|
|
int back_bookmark = -1;
|
|
|
|
int currentItem = -1;
|
|
|
|
auto removeNow = [&]()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
for(auto & removedItem: deadList)
|
|
|
|
{
|
|
|
|
auto instPtr = removedItem.first;
|
|
|
|
instPtr->invalidate();
|
|
|
|
currentItem = removedItem.second;
|
|
|
|
if(back_bookmark == -1)
|
|
|
|
{
|
|
|
|
// no bookmark yet
|
|
|
|
back_bookmark = currentItem;
|
|
|
|
}
|
|
|
|
else if(currentItem == front_bookmark - 1)
|
|
|
|
{
|
|
|
|
// part of contiguous sequence, continue
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// seam between previous and current item
|
|
|
|
removeNow();
|
|
|
|
}
|
|
|
|
front_bookmark = currentItem;
|
|
|
|
}
|
|
|
|
if(back_bookmark != -1)
|
|
|
|
{
|
|
|
|
removeNow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(newList.size())
|
|
|
|
{
|
|
|
|
add(newList);
|
|
|
|
}
|
2018-07-23 22:11:24 +00:00
|
|
|
m_dirty = false;
|
2019-07-10 02:23:24 +00:00
|
|
|
updateTotalPlayTime();
|
2018-07-15 12:51:05 +00:00
|
|
|
return NoError;
|
2013-08-25 20:48:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 02:23:24 +00:00
|
|
|
void InstanceList::updateTotalPlayTime()
|
|
|
|
{
|
|
|
|
totalPlayTime = 0;
|
|
|
|
for(auto const& itr : m_instances)
|
|
|
|
{
|
|
|
|
totalPlayTime += itr.get()->totalTimePlayed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 17:36:28 +00:00
|
|
|
void InstanceList::saveNow()
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
for(auto & item: m_instances)
|
|
|
|
{
|
|
|
|
item->saveNow();
|
|
|
|
}
|
2017-12-03 17:36:28 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 22:55:54 +00:00
|
|
|
void InstanceList::add(const QList<InstancePtr> &t)
|
2013-01-14 23:42:38 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
beginInsertRows(QModelIndex(), m_instances.count(), m_instances.count() + t.size() - 1);
|
|
|
|
m_instances.append(t);
|
|
|
|
for(auto & ptr : t)
|
|
|
|
{
|
|
|
|
connect(ptr.get(), &BaseInstance::propertiesChanged, this, &InstanceList::propertiesChanged);
|
|
|
|
}
|
|
|
|
endInsertRows();
|
2016-10-02 22:55:54 +00:00
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2016-10-02 22:55:54 +00:00
|
|
|
void InstanceList::resumeWatch()
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
if(m_watchLevel > 0)
|
|
|
|
{
|
|
|
|
qWarning() << "Bad suspend level resume in instance list";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_watchLevel++;
|
2018-07-23 22:11:24 +00:00
|
|
|
if(m_watchLevel > 0 && m_dirty)
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
|
|
|
loadList();
|
|
|
|
}
|
2013-07-28 06:40:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 22:55:54 +00:00
|
|
|
void InstanceList::suspendWatch()
|
2013-07-28 06:40:15 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
m_watchLevel --;
|
2016-10-02 22:55:54 +00:00
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
|
2016-10-02 22:55:54 +00:00
|
|
|
void InstanceList::providerUpdated()
|
|
|
|
{
|
2018-07-23 22:11:24 +00:00
|
|
|
m_dirty = true;
|
2018-07-15 12:51:05 +00:00
|
|
|
if(m_watchLevel == 1)
|
|
|
|
{
|
|
|
|
loadList();
|
|
|
|
}
|
2013-10-05 23:13:40 +00:00
|
|
|
}
|
2013-10-28 19:55:12 +00:00
|
|
|
|
2013-11-27 14:00:44 +00:00
|
|
|
InstancePtr InstanceList::getInstanceById(QString instId) const
|
2013-03-18 22:00:46 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
if(instId.isEmpty())
|
|
|
|
return InstancePtr();
|
|
|
|
for(auto & inst: m_instances)
|
|
|
|
{
|
|
|
|
if (inst->id() == instId)
|
|
|
|
{
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return InstancePtr();
|
2013-03-19 05:24:34 +00:00
|
|
|
}
|
|
|
|
|
2013-11-27 14:00:44 +00:00
|
|
|
QModelIndex InstanceList::getInstanceIndexById(const QString &id) const
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return index(getInstIndex(getInstanceById(id).get()));
|
2013-11-27 14:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int InstanceList::getInstIndex(BaseInstance *inst) const
|
2013-03-19 05:24:34 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
int count = m_instances.count();
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
if (inst == m_instances[i].get())
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2013-05-03 19:41:37 +00:00
|
|
|
}
|
2013-08-26 04:30:11 +00:00
|
|
|
|
2013-10-05 23:13:40 +00:00
|
|
|
void InstanceList::propertiesChanged(BaseInstance *inst)
|
2013-08-26 04:30:11 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
int i = getInstIndex(inst);
|
|
|
|
if (i != -1)
|
|
|
|
{
|
|
|
|
emit dataChanged(index(i), index(i));
|
2019-07-10 02:23:24 +00:00
|
|
|
updateTotalPlayTime();
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
2013-08-26 04:30:11 +00:00
|
|
|
}
|
2018-07-23 23:24:06 +00:00
|
|
|
|
|
|
|
InstancePtr InstanceList::loadInstance(const InstanceId& id)
|
|
|
|
{
|
|
|
|
if(!m_groupsLoaded)
|
|
|
|
{
|
|
|
|
loadGroupList();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto instanceRoot = FS::PathCombine(m_instDir, id);
|
|
|
|
auto instanceSettings = std::make_shared<INISettingsObject>(FS::PathCombine(instanceRoot, "instance.cfg"));
|
|
|
|
InstancePtr inst;
|
|
|
|
|
|
|
|
instanceSettings->registerSetting("InstanceType", "Legacy");
|
|
|
|
|
|
|
|
QString inst_type = instanceSettings->get("InstanceType").toString();
|
|
|
|
|
|
|
|
if (inst_type == "OneSix" || inst_type == "Nostalgia")
|
|
|
|
{
|
|
|
|
inst.reset(new MinecraftInstance(m_globalSettings, instanceSettings, instanceRoot));
|
|
|
|
}
|
|
|
|
else if (inst_type == "Legacy")
|
|
|
|
{
|
|
|
|
inst.reset(new LegacyInstance(m_globalSettings, instanceSettings, instanceRoot));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inst.reset(new NullInstance(m_globalSettings, instanceSettings, instanceRoot));
|
|
|
|
}
|
|
|
|
qDebug() << "Loaded instance " << inst->name() << " from " << inst->instanceRoot();
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceList::saveGroupList()
|
|
|
|
{
|
|
|
|
qDebug() << "Will save group list now.";
|
|
|
|
if(!m_instancesProbed)
|
|
|
|
{
|
|
|
|
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;
|
2019-08-20 00:58:27 +00:00
|
|
|
for (auto iter = m_instanceGroupIndex.begin(); iter != m_instanceGroupIndex.end(); iter++)
|
2018-07-23 23:24:06 +00:00
|
|
|
{
|
|
|
|
QString id = iter.key();
|
|
|
|
QString group = iter.value();
|
|
|
|
if (group.isEmpty())
|
|
|
|
continue;
|
|
|
|
if(!instanceSet.contains(id))
|
|
|
|
{
|
|
|
|
qDebug() << "Skipping saving missing instance" << id << "to groups list.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reverseGroupMap.count(group))
|
|
|
|
{
|
|
|
|
QSet<QString> set;
|
|
|
|
set.insert(id);
|
|
|
|
reverseGroupMap[group] = set;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QSet<QString> &set = reverseGroupMap[group];
|
|
|
|
set.insert(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QJsonObject toplevel;
|
|
|
|
toplevel.insert("formatVersion", QJsonValue(QString("1")));
|
|
|
|
QJsonObject groupsArr;
|
|
|
|
for (auto iter = reverseGroupMap.begin(); iter != reverseGroupMap.end(); iter++)
|
|
|
|
{
|
|
|
|
auto list = iter.value();
|
|
|
|
auto name = iter.key();
|
|
|
|
QJsonObject groupObj;
|
|
|
|
QJsonArray instanceArr;
|
2019-08-20 00:58:27 +00:00
|
|
|
groupObj.insert("hidden", QJsonValue(m_collapsedGroups.contains(name)));
|
2018-07-23 23:24:06 +00:00
|
|
|
for (auto item : list)
|
|
|
|
{
|
|
|
|
instanceArr.append(QJsonValue(item));
|
|
|
|
}
|
|
|
|
groupObj.insert("instances", instanceArr);
|
|
|
|
groupsArr.insert(name, groupObj);
|
|
|
|
}
|
|
|
|
toplevel.insert("groups", groupsArr);
|
|
|
|
QJsonDocument doc(toplevel);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
FS::write(groupFileName, doc.toJson());
|
|
|
|
qDebug() << "Group list saved.";
|
|
|
|
}
|
|
|
|
catch (const FS::FileSystemException &e)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
jsonData = FS::read(groupFileName);
|
|
|
|
}
|
|
|
|
catch (const FS::FileSystemException &e)
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to read instance group file :" << e.cause();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error);
|
|
|
|
|
|
|
|
// if the json was bad, fail
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
qCritical() << QString("Failed to parse instance group file: %1 at offset %2")
|
|
|
|
.arg(error.errorString(), QString::number(error.offset))
|
|
|
|
.toUtf8();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the root of the json wasn't an object, fail
|
|
|
|
if (!jsonDoc.isObject())
|
|
|
|
{
|
|
|
|
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
|
|
|
|
if (!rootObj.value("groups").isObject())
|
|
|
|
{
|
|
|
|
qWarning() << "Invalid group list JSON: 'groups' should be an object.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-20 00:58:27 +00:00
|
|
|
QSet<QString> groupSet;
|
|
|
|
m_instanceGroupIndex.clear();
|
2018-07-23 23:24:06 +00:00
|
|
|
|
|
|
|
// Iterate through all the groups.
|
|
|
|
QJsonObject groupMapping = rootObj.value("groups").toObject();
|
|
|
|
for (QJsonObject::iterator iter = groupMapping.begin(); iter != groupMapping.end(); iter++)
|
|
|
|
{
|
|
|
|
QString groupName = iter.key();
|
|
|
|
|
|
|
|
// If not an object, complain and skip to the next one.
|
|
|
|
if (!iter.value().isObject())
|
|
|
|
{
|
2019-08-20 00:58:27 +00:00
|
|
|
qWarning() << QString("Group '%1' in the group list should be an object.").arg(groupName).toUtf8();
|
2018-07-23 23:24:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject groupObj = iter.value().toObject();
|
|
|
|
if (!groupObj.value("instances").isArray())
|
|
|
|
{
|
2019-08-20 00:58:27 +00:00
|
|
|
qWarning() << QString("Group '%1' in the group list is invalid. It should contain an array called 'instances'.").arg(groupName).toUtf8();
|
2018-07-23 23:24:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep a list/set of groups for choosing
|
|
|
|
groupSet.insert(groupName);
|
|
|
|
|
2019-08-20 00:58:27 +00:00
|
|
|
auto hidden = groupObj.value("hidden").toBool(false);
|
|
|
|
if(hidden) {
|
|
|
|
m_collapsedGroups.insert(groupName);
|
|
|
|
}
|
|
|
|
|
2018-07-23 23:24:06 +00:00
|
|
|
// Iterate through the list of instances in the group.
|
|
|
|
QJsonArray instancesArray = groupObj.value("instances").toArray();
|
|
|
|
|
|
|
|
for (QJsonArray::iterator iter2 = instancesArray.begin(); iter2 != instancesArray.end(); iter2++)
|
|
|
|
{
|
2019-08-20 00:58:27 +00:00
|
|
|
m_instanceGroupIndex[(*iter2).toString()] = groupName;
|
2018-07-23 23:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_groupsLoaded = true;
|
2019-08-20 00:58:27 +00:00
|
|
|
m_groupNameCache.unite(groupSet);
|
2018-07-23 23:24:06 +00:00
|
|
|
qDebug() << "Group list loaded.";
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceList::instanceDirContentsChanged(const QString& path)
|
|
|
|
{
|
|
|
|
Q_UNUSED(path);
|
|
|
|
emit instancesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceList::on_InstFolderChanged(const Setting &setting, QVariant value)
|
|
|
|
{
|
|
|
|
QString newInstDir = QDir(value.toString()).canonicalPath();
|
|
|
|
if(newInstDir != m_instDir)
|
|
|
|
{
|
|
|
|
if(m_groupsLoaded)
|
|
|
|
{
|
|
|
|
saveGroupList();
|
|
|
|
}
|
|
|
|
m_instDir = newInstDir;
|
|
|
|
m_groupsLoaded = false;
|
|
|
|
emit instancesChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 00:58:27 +00:00
|
|
|
void InstanceList::on_GroupStateChanged(const QString& group, bool collapsed)
|
|
|
|
{
|
|
|
|
qDebug() << "Group" << group << (collapsed ? "collapsed" : "expanded");
|
|
|
|
if(collapsed) {
|
|
|
|
m_collapsedGroups.insert(group);
|
|
|
|
} else {
|
|
|
|
m_collapsedGroups.remove(group);
|
|
|
|
}
|
|
|
|
saveGroupList();
|
|
|
|
}
|
|
|
|
|
2018-07-23 23:24:06 +00:00
|
|
|
class InstanceStaging : public Task
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
const unsigned minBackoff = 1;
|
|
|
|
const unsigned maxBackoff = 16;
|
|
|
|
public:
|
|
|
|
InstanceStaging (
|
|
|
|
InstanceList * parent,
|
|
|
|
Task * child,
|
|
|
|
const QString & stagingPath,
|
|
|
|
const QString& instanceName,
|
|
|
|
const QString& groupName )
|
|
|
|
: backoff(minBackoff, maxBackoff)
|
|
|
|
{
|
|
|
|
m_parent = parent;
|
|
|
|
m_child.reset(child);
|
|
|
|
connect(child, &Task::succeeded, this, &InstanceStaging::childSucceded);
|
|
|
|
connect(child, &Task::failed, this, &InstanceStaging::childFailed);
|
|
|
|
connect(child, &Task::status, this, &InstanceStaging::setStatus);
|
|
|
|
connect(child, &Task::progress, this, &InstanceStaging::setProgress);
|
|
|
|
m_instanceName = instanceName;
|
|
|
|
m_groupName = groupName;
|
|
|
|
m_stagingPath = stagingPath;
|
|
|
|
m_backoffTimer.setSingleShot(true);
|
|
|
|
connect(&m_backoffTimer, &QTimer::timeout, this, &InstanceStaging::childSucceded);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~InstanceStaging() {};
|
|
|
|
|
2018-12-10 19:49:21 +00:00
|
|
|
|
|
|
|
// FIXME/TODO: add ability to abort during instance commit retries
|
|
|
|
bool abort() override
|
|
|
|
{
|
2018-12-14 00:18:18 +00:00
|
|
|
if(m_child && m_child->canAbort())
|
2018-12-10 19:49:21 +00:00
|
|
|
{
|
|
|
|
return m_child->abort();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool canAbort() const override
|
|
|
|
{
|
2018-12-14 00:18:18 +00:00
|
|
|
if(m_child && m_child->canAbort())
|
2018-12-10 19:49:21 +00:00
|
|
|
{
|
2018-12-14 00:18:18 +00:00
|
|
|
return true;
|
2018-12-10 19:49:21 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-23 23:24:06 +00:00
|
|
|
protected:
|
|
|
|
virtual void executeTask() override
|
|
|
|
{
|
|
|
|
m_child->start();
|
|
|
|
}
|
|
|
|
QStringList warnings() const override
|
|
|
|
{
|
|
|
|
return m_child->warnings();
|
|
|
|
}
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void childSucceded()
|
|
|
|
{
|
|
|
|
unsigned sleepTime = backoff();
|
|
|
|
if(m_parent->commitStagedInstance(m_stagingPath, m_instanceName, m_groupName))
|
|
|
|
{
|
|
|
|
emitSucceeded();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// we actually failed, retry?
|
|
|
|
if(sleepTime == maxBackoff)
|
|
|
|
{
|
|
|
|
emitFailed(tr("Failed to commit instance, even after multiple retries. It is being blocked by something."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qDebug() << "Failed to commit instance" << m_instanceName << "Initiating backoff:" << sleepTime;
|
|
|
|
m_backoffTimer.start(sleepTime * 500);
|
|
|
|
}
|
|
|
|
void childFailed(const QString & reason)
|
|
|
|
{
|
|
|
|
m_parent->destroyStagingPath(m_stagingPath);
|
|
|
|
emitFailed(reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/*
|
|
|
|
* WHY: the whole reason why this uses an exponential backoff retry scheme is antivirus on Windows.
|
|
|
|
* Basically, it starts messing things up while MultiMC is extracting/creating instances
|
|
|
|
* and causes that horrible failure that is NTFS to lock files in place because they are open.
|
|
|
|
*/
|
|
|
|
ExponentialSeries backoff;
|
|
|
|
QString m_stagingPath;
|
|
|
|
InstanceList * m_parent;
|
|
|
|
unique_qobject_ptr<Task> m_child;
|
|
|
|
QString m_instanceName;
|
|
|
|
QString m_groupName;
|
|
|
|
QTimer m_backoffTimer;
|
|
|
|
};
|
|
|
|
|
|
|
|
Task * InstanceList::wrapInstanceTask(InstanceTask * task)
|
|
|
|
{
|
|
|
|
auto stagingPath = getStagedInstancePath();
|
|
|
|
task->setStagingPath(stagingPath);
|
|
|
|
task->setParentSettings(m_globalSettings);
|
|
|
|
return new InstanceStaging(this, task, stagingPath, task->name(), task->group());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString InstanceList::getStagedInstancePath()
|
|
|
|
{
|
|
|
|
QString key = QUuid::createUuid().toString();
|
|
|
|
QString relPath = FS::PathCombine("_MMC_TEMP/" , key);
|
|
|
|
QDir rootPath(m_instDir);
|
|
|
|
auto path = FS::PathCombine(m_instDir, relPath);
|
|
|
|
if(!rootPath.mkpath(relPath))
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InstanceList::commitStagedInstance(const QString& path, const QString& instanceName, const QString& groupName)
|
|
|
|
{
|
|
|
|
QDir dir;
|
|
|
|
QString instID = FS::DirNameFromString(instanceName, m_instDir);
|
|
|
|
{
|
|
|
|
WatchLock lock(m_watcher, m_instDir);
|
|
|
|
QString destination = FS::PathCombine(m_instDir, instID);
|
|
|
|
if(!dir.rename(path, destination))
|
|
|
|
{
|
|
|
|
qWarning() << "Failed to move" << path << "to" << destination;
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-20 00:58:27 +00:00
|
|
|
m_instanceGroupIndex[instID] = groupName;
|
2018-07-23 23:24:06 +00:00
|
|
|
instanceSet.insert(instID);
|
2019-08-20 00:58:27 +00:00
|
|
|
m_groupNameCache.insert(groupName);
|
2018-07-23 23:24:06 +00:00
|
|
|
emit instancesChanged();
|
2019-06-23 12:54:17 +00:00
|
|
|
emit instanceSelectRequest(instID);
|
2018-07-23 23:24:06 +00:00
|
|
|
}
|
|
|
|
saveGroupList();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InstanceList::destroyStagingPath(const QString& keyPath)
|
|
|
|
{
|
|
|
|
return FS::deletePath(keyPath);
|
|
|
|
}
|
|
|
|
|
2019-07-10 02:23:24 +00:00
|
|
|
int InstanceList::getTotalPlayTime() {
|
|
|
|
updateTotalPlayTime();
|
|
|
|
return totalPlayTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "InstanceList.moc"
|