diff options
Diffstat (limited to 'api/logic/meta/format/Format.cpp')
-rw-r--r-- | api/logic/meta/format/Format.cpp | 101 |
1 files changed, 77 insertions, 24 deletions
diff --git a/api/logic/meta/format/Format.cpp b/api/logic/meta/format/Format.cpp index e0fddb0b..39d3f14f 100644 --- a/api/logic/meta/format/Format.cpp +++ b/api/logic/meta/format/Format.cpp @@ -15,15 +15,85 @@ #include "Format.h" -#include "FormatV1.h" +#include "minecraft/onesix/OneSixVersionFormat.h"" #include "meta/Index.h" #include "meta/Version.h" #include "meta/VersionList.h" +#include "Json.h" +using namespace Json; + namespace Meta { +static const int currentFormatVersion = 0; + +// Index +static BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) +{ + const QVector<QJsonObject> objects = requireIsArrayOf<QJsonObject>(obj, "packages"); + QVector<VersionListPtr> lists; + lists.reserve(objects.size()); + std::transform(objects.begin(), objects.end(), std::back_inserter(lists), [](const QJsonObject &obj) + { + VersionListPtr list = std::make_shared<VersionList>(requireString(obj, "uid")); + list->setName(ensureString(obj, "name", QString())); + return list; + }); + return std::make_shared<Index>(lists); +} + +// Version +static VersionPtr parseCommonVersion(const QString &uid, const QJsonObject &obj) +{ + const QVector<QJsonObject> requiresRaw = obj.contains("requires") ? requireIsArrayOf<QJsonObject>(obj, "requires") : QVector<QJsonObject>(); + QVector<Reference> requires; + requires.reserve(requiresRaw.size()); + std::transform(requiresRaw.begin(), requiresRaw.end(), std::back_inserter(requires), [](const QJsonObject &rObj) + { + Reference ref(requireString(rObj, "uid")); + ref.setVersion(ensureString(rObj, "version", QString())); + return ref; + }); + + VersionPtr version = std::make_shared<Version>(uid, requireString(obj, "version")); + version->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); + version->setType(ensureString(obj, "type", QString())); + version->setRequires(requires); + return version; +} + +static BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) +{ + VersionPtr version = parseCommonVersion(requireString(obj, "uid"), obj); + + version->setData(OneSixVersionFormat::versionFileFromJson(QJsonDocument(obj), + QString("%1/%2.json").arg(version->uid(), version->version()), + obj.contains("order"))); + return version; +} + +// Version list / package +static BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) +{ + const QString uid = requireString(obj, "uid"); + + const QVector<QJsonObject> versionsRaw = requireIsArrayOf<QJsonObject>(obj, "versions"); + QVector<VersionPtr> versions; + versions.reserve(versionsRaw.size()); + std::transform(versionsRaw.begin(), versionsRaw.end(), std::back_inserter(versions), [uid](const QJsonObject &vObj) + { + return parseCommonVersion(uid, vObj); + }); + + VersionListPtr list = std::make_shared<VersionList>(uid); + list->setName(ensureString(obj, "name", QString())); + list->setVersions(versions); + return list; +} + + static int formatVersion(const QJsonObject &obj) { if (!obj.contains("formatVersion")) { @@ -35,56 +105,39 @@ static int formatVersion(const QJsonObject &obj) return obj.value("formatVersion").toInt(); } -void Format::parseIndex(const QJsonObject &obj, Index *ptr) +void parseIndex(const QJsonObject &obj, Index *ptr) { const int version = formatVersion(obj); switch (version) { case 0: - ptr->merge(FormatV1().parseIndexInternal(obj)); + ptr->merge(parseIndexInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } -void Format::parseVersionList(const QJsonObject &obj, VersionList *ptr) +void parseVersionList(const QJsonObject &obj, VersionList *ptr) { const int version = formatVersion(obj); switch (version) { case 0: - ptr->merge(FormatV1().parseVersionListInternal(obj)); + ptr->merge(parseVersionListInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } -void Format::parseVersion(const QJsonObject &obj, Version *ptr) +void parseVersion(const QJsonObject &obj, Version *ptr) { const int version = formatVersion(obj); switch (version) { case 0: - ptr->merge(FormatV1().parseVersionInternal(obj)); + ptr->merge(parseVersionInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } - - -QJsonObject Format::serializeIndex(const Index *ptr) -{ - return FormatV1().serializeIndexInternal(ptr); -} - -QJsonObject Format::serializeVersionList(const VersionList *ptr) -{ - return FormatV1().serializeVersionListInternal(ptr); -} - -QJsonObject Format::serializeVersion(const Version *ptr) -{ - return FormatV1().serializeVersionInternal(ptr); -} - } |