summaryrefslogtreecommitdiffstats
path: root/api/logic/meta/format
diff options
context:
space:
mode:
Diffstat (limited to 'api/logic/meta/format')
-rw-r--r--api/logic/meta/format/Format.cpp84
-rw-r--r--api/logic/meta/format/Format.h57
-rw-r--r--api/logic/meta/format/FormatV1.cpp161
-rw-r--r--api/logic/meta/format/FormatV1.h33
4 files changed, 335 insertions, 0 deletions
diff --git a/api/logic/meta/format/Format.cpp b/api/logic/meta/format/Format.cpp
new file mode 100644
index 00000000..a3f9f730
--- /dev/null
+++ b/api/logic/meta/format/Format.cpp
@@ -0,0 +1,84 @@
+/* Copyright 2015-2017 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.
+ */
+
+#include "Format.h"
+
+#include "FormatV1.h"
+
+#include "meta/Index.h"
+#include "meta/Version.h"
+#include "meta/VersionList.h"
+
+namespace Meta
+{
+
+static int formatVersion(const QJsonObject &obj)
+{
+ if (!obj.contains("formatVersion")) {
+ throw ParseException(QObject::tr("Missing required field: 'formatVersion'"));
+ }
+ if (!obj.value("formatVersion").isDouble()) {
+ throw ParseException(QObject::tr("Required field has invalid type: 'formatVersion'"));
+ }
+ return obj.value("formatVersion").toInt();
+}
+
+void Format::parseIndex(const QJsonObject &obj, Index *ptr)
+{
+ const int version = formatVersion(obj);
+ switch (version) {
+ case 1:
+ ptr->merge(FormatV1().parseIndexInternal(obj));
+ break;
+ default:
+ throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version));
+ }
+}
+void Format::parseVersion(const QJsonObject &obj, Version *ptr)
+{
+ const int version = formatVersion(obj);
+ switch (version) {
+ case 1:
+ ptr->merge(FormatV1().parseVersionInternal(obj));
+ break;
+ default:
+ throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version));
+ }
+}
+void Format::parseVersionList(const QJsonObject &obj, VersionList *ptr)
+{
+ const int version = formatVersion(obj);
+ switch (version) {
+ case 10:
+ ptr->merge(FormatV1().parseVersionListInternal(obj));
+ break;
+ default:
+ throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version));
+ }
+}
+
+QJsonObject Format::serializeIndex(const Index *ptr)
+{
+ return FormatV1().serializeIndexInternal(ptr);
+}
+QJsonObject Format::serializeVersion(const Version *ptr)
+{
+ return FormatV1().serializeVersionInternal(ptr);
+}
+QJsonObject Format::serializeVersionList(const VersionList *ptr)
+{
+ return FormatV1().serializeVersionListInternal(ptr);
+}
+}
diff --git a/api/logic/meta/format/Format.h b/api/logic/meta/format/Format.h
new file mode 100644
index 00000000..79605a6f
--- /dev/null
+++ b/api/logic/meta/format/Format.h
@@ -0,0 +1,57 @@
+/* Copyright 2015-2017 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.
+ */
+
+#pragma once
+
+#include <QJsonObject>
+#include <memory>
+
+#include "Exception.h"
+#include "meta/BaseEntity.h"
+
+namespace Meta
+{
+class Index;
+class Version;
+class VersionList;
+
+class ParseException : public Exception
+{
+public:
+ using Exception::Exception;
+};
+
+class Format
+{
+public:
+ virtual ~Format() {}
+
+ static void parseIndex(const QJsonObject &obj, Index *ptr);
+ static void parseVersion(const QJsonObject &obj, Version *ptr);
+ static void parseVersionList(const QJsonObject &obj, VersionList *ptr);
+
+ static QJsonObject serializeIndex(const Index *ptr);
+ static QJsonObject serializeVersion(const Version *ptr);
+ static QJsonObject serializeVersionList(const VersionList *ptr);
+
+protected:
+ virtual BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) const = 0;
+ virtual BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) const = 0;
+ virtual BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) const = 0;
+ virtual QJsonObject serializeIndexInternal(const Index *ptr) const = 0;
+ virtual QJsonObject serializeVersionInternal(const Version *ptr) const = 0;
+ virtual QJsonObject serializeVersionListInternal(const VersionList *ptr) const = 0;
+};
+}
diff --git a/api/logic/meta/format/FormatV1.cpp b/api/logic/meta/format/FormatV1.cpp
new file mode 100644
index 00000000..23f57100
--- /dev/null
+++ b/api/logic/meta/format/FormatV1.cpp
@@ -0,0 +1,161 @@
+/* Copyright 2015-2017 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.
+ */
+
+#include "FormatV1.h"
+#include <minecraft/onesix/OneSixVersionFormat.h>
+
+#include "Json.h"
+
+#include "meta/Index.h"
+#include "meta/Version.h"
+#include "meta/VersionList.h"
+#include "Env.h"
+
+using namespace Json;
+
+namespace Meta
+{
+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"));
+ if (obj.value("time").isString())
+ {
+ version->setTime(QDateTime::fromString(requireString(obj, "time"), Qt::ISODate).toMSecsSinceEpoch() / 1000);
+ }
+ else
+ {
+ version->setTime(requireInteger(obj, "time"));
+ }
+ version->setType(ensureString(obj, "type", QString()));
+ version->setRequires(requires);
+ return version;
+}
+static void serializeCommonVersion(const Version *version, QJsonObject &obj)
+{
+ QJsonArray requires;
+ for (const Reference &ref : version->requires())
+ {
+ if (ref.version().isEmpty())
+ {
+ QJsonObject out;
+ out["uid"] = ref.uid();
+ requires.append(out);
+ }
+ else
+ {
+ QJsonObject out;
+ out["uid"] = ref.uid();
+ out["version"] = ref.version();
+ requires.append(out);
+ }
+ }
+
+ obj.insert("version", version->version());
+ obj.insert("type", version->type());
+ obj.insert("time", version->time().toString(Qt::ISODate));
+ obj.insert("requires", requires);
+}
+
+BaseEntity::Ptr FormatV1::parseIndexInternal(const QJsonObject &obj) const
+{
+ const QVector<QJsonObject> objects = requireIsArrayOf<QJsonObject>(obj, "index");
+ 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);
+}
+BaseEntity::Ptr FormatV1::parseVersionInternal(const QJsonObject &obj) const
+{
+ 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;
+}
+BaseEntity::Ptr FormatV1::parseVersionListInternal(const QJsonObject &obj) const
+{
+ 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), [this, 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;
+}
+
+QJsonObject FormatV1::serializeIndexInternal(const Index *ptr) const
+{
+ QJsonArray index;
+ for (const VersionListPtr &list : ptr->lists())
+ {
+ QJsonObject out;
+ out["uid"] = list->uid();
+ out["version"] = list->name();
+ index.append(out);
+ }
+ QJsonObject out;
+ out["formatVersion"] = 1;
+ out["index"] = index;
+ return out;
+}
+QJsonObject FormatV1::serializeVersionInternal(const Version *ptr) const
+{
+ QJsonObject obj = OneSixVersionFormat::versionFileToJson(ptr->data(), true).object();
+ serializeCommonVersion(ptr, obj);
+ obj.insert("formatVersion", 1);
+ obj.insert("uid", ptr->uid());
+ // TODO: the name should be looked up in the UI based on the uid
+ obj.insert("name", ENV.metadataIndex()->getListGuaranteed(ptr->uid())->name());
+
+ return obj;
+}
+QJsonObject FormatV1::serializeVersionListInternal(const VersionList *ptr) const
+{
+ QJsonArray versions;
+ for (const VersionPtr &version : ptr->versions())
+ {
+ QJsonObject obj;
+ serializeCommonVersion(version.get(), obj);
+ versions.append(obj);
+ }
+ QJsonObject out;
+ out["formatVersion"] = 10;
+ out["uid"] = ptr->uid();
+ out["name"] = ptr->name().isNull() ? QJsonValue() : ptr->name();
+ out["versions"] = versions;
+ return out;
+}
+}
diff --git a/api/logic/meta/format/FormatV1.h b/api/logic/meta/format/FormatV1.h
new file mode 100644
index 00000000..ab986083
--- /dev/null
+++ b/api/logic/meta/format/FormatV1.h
@@ -0,0 +1,33 @@
+/* Copyright 2015-2017 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.
+ */
+
+#pragma once
+
+#include "Format.h"
+
+namespace Meta
+{
+class FormatV1 : public Format
+{
+public:
+ BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) const override;
+ BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) const override;
+ BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) const override;
+
+ QJsonObject serializeIndexInternal(const Index *ptr) const override;
+ QJsonObject serializeVersionInternal(const Version *ptr) const override;
+ QJsonObject serializeVersionListInternal(const VersionList *ptr) const override;
+};
+}