2021-01-18 12:58:54 +05:30
|
|
|
/* Copyright 2013-2021 MultiMC Contributors
|
2017-09-17 02:39:05 +05:30
|
|
|
*
|
|
|
|
* 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 "LegacyModList.h"
|
|
|
|
#include <FileSystem.h>
|
|
|
|
#include <QString>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
LegacyModList::LegacyModList(const QString &dir, const QString &list_file)
|
2018-07-15 18:21:05 +05:30
|
|
|
: m_dir(dir), m_list_file(list_file)
|
2017-09-17 02:39:05 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
FS::ensureFolderPathExists(m_dir.absolutePath());
|
2019-08-03 06:42:48 +05:30
|
|
|
m_dir.setFilter(QDir::Readable | QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs | QDir::NoSymLinks);
|
2018-07-15 18:21:05 +05:30
|
|
|
m_dir.setSorting(QDir::Name | QDir::IgnoreCase | QDir::LocaleAware);
|
2017-09-17 02:39:05 +05:30
|
|
|
}
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
struct OrderItem
|
|
|
|
{
|
|
|
|
QString id;
|
|
|
|
bool enabled = false;
|
|
|
|
};
|
|
|
|
typedef QList<OrderItem> OrderList;
|
2017-09-17 02:39:05 +05:30
|
|
|
|
2019-08-03 06:42:48 +05:30
|
|
|
static void internalSort(QList<LegacyModList::Mod> &what)
|
2017-09-17 02:39:05 +05:30
|
|
|
{
|
2019-08-03 06:42:48 +05:30
|
|
|
auto predicate = [](const LegacyModList::Mod &left, const LegacyModList::Mod &right)
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2019-08-03 06:42:48 +05:30
|
|
|
return left.fileName().localeAwareCompare(right.fileName()) < 0;
|
2018-07-15 18:21:05 +05:30
|
|
|
};
|
|
|
|
std::sort(what.begin(), what.end(), predicate);
|
2017-09-17 02:39:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static OrderList readListFile(const QString &m_list_file)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
OrderList itemList;
|
|
|
|
if (m_list_file.isNull() || m_list_file.isEmpty())
|
|
|
|
return itemList;
|
2017-09-17 02:39:05 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QFile textFile(m_list_file);
|
|
|
|
if (!textFile.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
return OrderList();
|
2017-09-17 02:39:05 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QTextStream textStream;
|
|
|
|
textStream.setAutoDetectUnicode(true);
|
|
|
|
textStream.setDevice(&textFile);
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
QString line = textStream.readLine();
|
|
|
|
if (line.isNull() || line.isEmpty())
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OrderItem it;
|
|
|
|
it.enabled = !line.endsWith(".disabled");
|
|
|
|
if (!it.enabled)
|
|
|
|
{
|
|
|
|
line.chop(9);
|
|
|
|
}
|
|
|
|
it.id = line;
|
|
|
|
itemList.append(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
textFile.close();
|
|
|
|
return itemList;
|
2017-09-17 02:39:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool LegacyModList::update()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!m_dir.exists() || !m_dir.isReadable())
|
|
|
|
return false;
|
2017-09-17 02:39:05 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QList<Mod> orderedMods;
|
|
|
|
QList<Mod> newMods;
|
|
|
|
m_dir.refresh();
|
|
|
|
auto folderContents = m_dir.entryInfoList();
|
2017-09-17 02:39:05 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// first, process the ordered items (if any)
|
|
|
|
OrderList listOrder = readListFile(m_list_file);
|
|
|
|
for (auto item : listOrder)
|
|
|
|
{
|
|
|
|
QFileInfo infoEnabled(m_dir.filePath(item.id));
|
|
|
|
QFileInfo infoDisabled(m_dir.filePath(item.id + ".disabled"));
|
|
|
|
int idxEnabled = folderContents.indexOf(infoEnabled);
|
|
|
|
int idxDisabled = folderContents.indexOf(infoDisabled);
|
|
|
|
bool isEnabled;
|
|
|
|
// if both enabled and disabled versions are present, it's a special case...
|
|
|
|
if (idxEnabled >= 0 && idxDisabled >= 0)
|
|
|
|
{
|
|
|
|
// we only process the one we actually have in the order file.
|
|
|
|
// and exactly as we have it.
|
|
|
|
// THIS IS A CORNER CASE
|
|
|
|
isEnabled = item.enabled;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// only one is present.
|
|
|
|
// we pick the one that we found.
|
|
|
|
// we assume the mod was enabled/disabled by external means
|
|
|
|
isEnabled = idxEnabled >= 0;
|
|
|
|
}
|
|
|
|
int idx = isEnabled ? idxEnabled : idxDisabled;
|
|
|
|
QFileInfo &info = isEnabled ? infoEnabled : infoDisabled;
|
|
|
|
// if the file from the index file exists
|
|
|
|
if (idx != -1)
|
|
|
|
{
|
|
|
|
// remove from the actual folder contents list
|
|
|
|
folderContents.takeAt(idx);
|
|
|
|
// append the new mod
|
2019-08-03 06:42:48 +05:30
|
|
|
orderedMods.append(info);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
2019-08-03 06:42:48 +05:30
|
|
|
// if there are any untracked files... append them sorted at the end
|
2018-07-15 18:21:05 +05:30
|
|
|
if (folderContents.size())
|
|
|
|
{
|
|
|
|
for (auto entry : folderContents)
|
|
|
|
{
|
2019-08-03 06:42:48 +05:30
|
|
|
newMods.append(entry);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
internalSort(newMods);
|
|
|
|
orderedMods.append(newMods);
|
|
|
|
}
|
|
|
|
mods.swap(orderedMods);
|
|
|
|
return true;
|
2017-09-17 02:39:05 +05:30
|
|
|
}
|