New, simpler and versioned format for the patch load order.

This commit is contained in:
Petr Mrázek 2014-05-20 01:17:54 +02:00
parent e118b1f990
commit 48d3052ac1
4 changed files with 79 additions and 66 deletions

View File

@ -33,8 +33,9 @@ InstanceVersion::InstanceVersion(OneSixInstance *instance, QObject *parent)
void InstanceVersion::reload(const QStringList &external) void InstanceVersion::reload(const QStringList &external)
{ {
m_externalPatches = external;
beginResetModel(); beginResetModel();
VersionBuilder::build(this, m_instance, external); VersionBuilder::build(this, m_instance, m_externalPatches);
reapply(true); reapply(true);
endResetModel(); endResetModel();
} }
@ -87,10 +88,11 @@ bool InstanceVersion::remove(const int index)
} }
if(!QFile::remove(VersionPatches.at(index)->getPatchFilename())) if(!QFile::remove(VersionPatches.at(index)->getPatchFilename()))
return false; return false;
beginResetModel(); beginRemoveRows(QModelIndex(), index, index);
VersionPatches.removeAt(index); VersionPatches.removeAt(index);
endRemoveRows();
reapply(true); reapply(true);
endResetModel(); saveCurrentOrder();
return true; return true;
} }
@ -173,11 +175,13 @@ bool InstanceVersion::revertToVanilla()
if(!preremove(*it)) if(!preremove(*it))
{ {
endResetModel(); endResetModel();
saveCurrentOrder();
return false; return false;
} }
if(!QFile::remove((*it)->getPatchFilename())) if(!QFile::remove((*it)->getPatchFilename()))
{ {
endResetModel(); endResetModel();
saveCurrentOrder();
return false; return false;
} }
it = VersionPatches.erase(it); it = VersionPatches.erase(it);
@ -187,6 +191,7 @@ bool InstanceVersion::revertToVanilla()
} }
reapply(true); reapply(true);
endResetModel(); endResetModel();
saveCurrentOrder();
return true; return true;
} }
@ -295,32 +300,17 @@ int InstanceVersion::columnCount(const QModelIndex &parent) const
return 2; return 2;
} }
QMap<QString, int> InstanceVersion::getExistingOrder() const void InstanceVersion::saveCurrentOrder() const
{ {
QMap<QString, int> order; PatchOrder order;
int index = 0; int index = 0;
// overriden
{
QMap<QString, int> overridenOrder = VersionBuilder::readOverrideOrders(m_instance);
for (auto id : order.keys())
{
if (overridenOrder.contains(id))
{
order[id] = overridenOrder[id];
}
}
}
for(auto item: VersionPatches) for(auto item: VersionPatches)
{ {
// things with fixed (negative) order.
if(!item->isMoveable()) if(!item->isMoveable())
continue; continue;
// the other things. order.append(item->getPatchID());
auto id = item->getPatchID();
order[id] = index;
index++;
} }
return order; VersionBuilder::writeOverrideOrders(m_instance, order);
} }
void InstanceVersion::move(const int index, const MoveDirection direction) void InstanceVersion::move(const int index, const MoveDirection direction)
@ -352,16 +342,16 @@ void InstanceVersion::move(const int index, const MoveDirection direction)
{ {
return; return;
} }
beginMoveRows(QModelIndex(), index, index, QModelIndex(), togap); beginMoveRows(QModelIndex(), index, index, QModelIndex(), togap);
VersionPatches.swap(index, theirIndex); VersionPatches.swap(index, theirIndex);
endMoveRows(); endMoveRows();
saveCurrentOrder();
reapply(); reapply();
} }
void InstanceVersion::resetOrder() void InstanceVersion::resetOrder()
{ {
QDir(m_instance->instanceRoot()).remove("order.json"); QDir(m_instance->instanceRoot()).remove("order.json");
reapply(); reload(m_externalPatches);
} }
void InstanceVersion::reapply(const bool alreadyReseting) void InstanceVersion::reapply(const bool alreadyReseting)
@ -477,6 +467,7 @@ void InstanceVersion::installJarModByFilename(QString filepath)
beginInsertRows(QModelIndex(), index, index); beginInsertRows(QModelIndex(), index, index);
VersionPatches.append(f); VersionPatches.append(f);
endInsertRows(); endInsertRows();
saveCurrentOrder();
} }
int InstanceVersion::getFreeOrderNumber() int InstanceVersion::getFreeOrderNumber()

View File

@ -175,7 +175,8 @@ public:
VersionPatchPtr versionPatch(int index); VersionPatchPtr versionPatch(int index);
private: private:
QStringList m_externalPatches;
OneSixInstance *m_instance; OneSixInstance *m_instance;
QMap<QString, int> getExistingOrder() const; void saveCurrentOrder() const;
int getFreeOrderNumber(); int getFreeOrderNumber();
}; };

View File

@ -73,10 +73,6 @@ void VersionBuilder::buildFromCustomJson()
file->order = -1; file->order = -1;
file->version = QString(); file->version = QString();
m_version->VersionPatches.append(file); m_version->VersionPatches.append(file);
// QObject::tr("The version descriptors of this instance are not compatible with the
// current version of MultiMC"));
// QObject::tr("Error while applying %1. Please check MultiMC-0.log for more info.")
// some final touches
m_version->finalize(); m_version->finalize();
return; return;
} }
@ -102,18 +98,48 @@ void VersionBuilder::buildFromVersionJson()
void VersionBuilder::readInstancePatches() void VersionBuilder::readInstancePatches()
{ {
PatchOrder userOrder;
readOverrideOrders(m_instance, userOrder);
QDir patches(instance_root.absoluteFilePath("patches/")); QDir patches(instance_root.absoluteFilePath("patches/"));
// first, load things by sort order.
for (auto id : userOrder)
{
// ignore builtins
if (id == "net.minecraft")
continue;
if (id == "org.lwjgl")
continue;
// parse the file
QString filename = patches.absoluteFilePath(id + ".json");
QLOG_INFO() << "Reading" << filename << "by user order";
auto file = parseJsonFile(QFileInfo(filename), false);
// sanity check. prevent tampering with files.
if (file->fileId != id)
{
throw VersionBuildError(
QObject::tr("load id %1 does not match internal id %2").arg(id, file->fileId));
}
m_version->VersionPatches.append(file);
}
// now load the rest by internal preference.
QMap<int, QPair<QString, VersionFilePtr>> files; QMap<int, QPair<QString, VersionFilePtr>> files;
for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files)) for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files))
{ {
// parse the file
QLOG_INFO() << "Reading" << info.fileName(); QLOG_INFO() << "Reading" << info.fileName();
auto file = parseJsonFile(info, true); auto file = parseJsonFile(info, true);
if(file->fileId == "net.minecraft") // ignore builtins
if (file->fileId == "net.minecraft")
continue; continue;
if(file->fileId == "org.lwjgl") if (file->fileId == "org.lwjgl")
continue;
// do not load what we already loaded in the first pass
if (userOrder.contains(file->fileId))
continue; continue;
if (files.contains(file->order)) if (files.contains(file->order))
{ {
// FIXME: do not throw?
throw VersionBuildError(QObject::tr("%1 has the same order as %2") throw VersionBuildError(QObject::tr("%1 has the same order as %2")
.arg(file->fileId, files[file->order].second->fileId)); .arg(file->fileId, files[file->order].second->fileId));
} }
@ -152,7 +178,7 @@ void VersionBuilder::buildFromMultilayer()
auto minecraftList = MMC->minecraftlist(); auto minecraftList = MMC->minecraftlist();
auto mcversion = minecraftList->findVersion(m_instance->intendedVersionId()); auto mcversion = minecraftList->findVersion(m_instance->intendedVersionId());
auto minecraftPatch = std::dynamic_pointer_cast<VersionPatch>(mcversion); auto minecraftPatch = std::dynamic_pointer_cast<VersionPatch>(mcversion);
if(!minecraftPatch) if (!minecraftPatch)
{ {
throw VersionIncomplete("net.minecraft"); throw VersionIncomplete("net.minecraft");
} }
@ -162,7 +188,7 @@ void VersionBuilder::buildFromMultilayer()
QResource LWJGL(":/versions/LWJGL/2.9.1.json"); QResource LWJGL(":/versions/LWJGL/2.9.1.json");
auto lwjgl = parseJsonFile(LWJGL.absoluteFilePath(), false, false); auto lwjgl = parseJsonFile(LWJGL.absoluteFilePath(), false, false);
auto lwjglPatch = std::dynamic_pointer_cast<VersionPatch>(lwjgl); auto lwjglPatch = std::dynamic_pointer_cast<VersionPatch>(lwjgl);
if(!lwjglPatch) if (!lwjglPatch)
{ {
throw VersionIncomplete("org.lwjgl"); throw VersionIncomplete("org.lwjgl");
} }
@ -205,13 +231,9 @@ void VersionBuilder::readJsonAndApply(const QJsonObject &obj)
m_version->clear(); m_version->clear();
auto file = VersionFile::fromJson(QJsonDocument(obj), QString(), false); auto file = VersionFile::fromJson(QJsonDocument(obj), QString(), false);
// QObject::tr("Error while reading. Please check MultiMC-0.log for more info."));
file->applyTo(m_version); file->applyTo(m_version);
m_version->VersionPatches.append(file); m_version->VersionPatches.append(file);
// QObject::tr("Error while applying. Please check MultiMC-0.log for more info."));
// QObject::tr("The version descriptors of this instance are not compatible with the current
// version of MultiMC"));
} }
VersionFilePtr VersionBuilder::parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder, VersionFilePtr VersionBuilder::parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder,
@ -233,8 +255,6 @@ VersionFilePtr VersionBuilder::parseJsonFile(const QFileInfo &fileInfo, const bo
.arg(error.offset)); .arg(error.offset));
} }
return VersionFile::fromJson(doc, file.fileName(), requireOrder, isFTB); return VersionFile::fromJson(doc, file.fileName(), requireOrder, isFTB);
// QObject::tr("Error while reading %1. Please check MultiMC-0.log for more
// info.").arg(file.fileName());
} }
VersionFilePtr VersionBuilder::parseBinaryJsonFile(const QFileInfo &fileInfo) VersionFilePtr VersionBuilder::parseBinaryJsonFile(const QFileInfo &fileInfo)
@ -256,22 +276,17 @@ VersionFilePtr VersionBuilder::parseBinaryJsonFile(const QFileInfo &fileInfo)
return VersionFile::fromJson(doc, file.fileName(), false, false); return VersionFile::fromJson(doc, file.fileName(), false, false);
} }
QMap<QString, int> VersionBuilder::readOverrideOrders(OneSixInstance *instance) static const int currentOrderFileVersion = 1;
bool VersionBuilder::readOverrideOrders(OneSixInstance *instance, PatchOrder &order)
{ {
QMap<QString, int> out;
// make sure the order file exists
if (!QDir(instance->instanceRoot()).exists("order.json"))
return out;
// and it can be opened
QFile orderFile(instance->instanceRoot() + "/order.json"); QFile orderFile(instance->instanceRoot() + "/order.json");
if (!orderFile.open(QFile::ReadOnly)) if (!orderFile.open(QFile::ReadOnly))
{ {
QLOG_ERROR() << "Couldn't open" << orderFile.fileName() QLOG_ERROR() << "Couldn't open" << orderFile.fileName()
<< " for reading:" << orderFile.errorString(); << " for reading:" << orderFile.errorString();
QLOG_WARN() << "Ignoring overriden order"; QLOG_WARN() << "Ignoring overriden order";
return out; return false;
} }
// and it's valid JSON // and it's valid JSON
@ -281,42 +296,46 @@ QMap<QString, int> VersionBuilder::readOverrideOrders(OneSixInstance *instance)
{ {
QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString(); QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString();
QLOG_WARN() << "Ignoring overriden order"; QLOG_WARN() << "Ignoring overriden order";
return out; return false;
} }
// and then read it and process it if all above is true. // and then read it and process it if all above is true.
try try
{ {
auto obj = MMCJson::ensureObject(doc); auto obj = MMCJson::ensureObject(doc);
for (auto it = obj.begin(); it != obj.end(); ++it) // check order file version.
auto version = MMCJson::ensureInteger(obj.value("version"), "version");
if (version != currentOrderFileVersion)
{ {
if (it.key().startsWith("org.multimc.")) throw JSONValidationError(QObject::tr("Invalid order file version, expected %1")
{ .arg(currentOrderFileVersion));
continue; }
} auto orderArray = MMCJson::ensureArray(obj.value("order"));
out.insert(it.key(), MMCJson::ensureInteger(it.value())); for(auto item: orderArray)
{
order.append(MMCJson::ensureString(item));
} }
} }
catch (JSONValidationError &err) catch (JSONValidationError &err)
{ {
QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ": bad file format"; QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ": bad file format";
QLOG_WARN() << "Ignoring overriden order"; QLOG_WARN() << "Ignoring overriden order";
return out; order.clear();
return false;
} }
return out; return true;
} }
bool VersionBuilder::writeOverrideOrders(const QMap<QString, int> &order, bool VersionBuilder::writeOverrideOrders(OneSixInstance *instance, const PatchOrder &order)
OneSixInstance *instance)
{ {
QJsonObject obj; QJsonObject obj;
for (auto it = order.cbegin(); it != order.cend(); ++it) obj.insert("version", currentOrderFileVersion);
QJsonArray orderArray;
for(auto str: order)
{ {
int order = it.value(); orderArray.append(str);
if(order < 0)
continue;
obj.insert(it.key(), it.value());
} }
obj.insert("order", orderArray);
QFile orderFile(instance->instanceRoot() + "/order.json"); QFile orderFile(instance->instanceRoot() + "/order.json");
if (!orderFile.open(QFile::WriteOnly)) if (!orderFile.open(QFile::WriteOnly))
{ {

View File

@ -24,6 +24,8 @@ class OneSixInstance;
class QJsonObject; class QJsonObject;
class QFileInfo; class QFileInfo;
typedef QStringList PatchOrder;
class VersionBuilder class VersionBuilder
{ {
VersionBuilder(); VersionBuilder();
@ -33,8 +35,8 @@ public:
static VersionFilePtr parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder, bool isFTB = false); static VersionFilePtr parseJsonFile(const QFileInfo &fileInfo, const bool requireOrder, bool isFTB = false);
static VersionFilePtr parseBinaryJsonFile(const QFileInfo &fileInfo); static VersionFilePtr parseBinaryJsonFile(const QFileInfo &fileInfo);
static QMap<QString, int> readOverrideOrders(OneSixInstance *instance); bool readOverrideOrders(OneSixInstance *instance, PatchOrder &order);
static bool writeOverrideOrders(const QMap<QString, int> &order, OneSixInstance *instance); static bool writeOverrideOrders(OneSixInstance *instance, const PatchOrder &order);
private: private:
InstanceVersion *m_version; InstanceVersion *m_version;