summaryrefslogtreecommitdiffstats
path: root/logic/minecraft/onesix
diff options
context:
space:
mode:
Diffstat (limited to 'logic/minecraft/onesix')
-rw-r--r--logic/minecraft/onesix/OneSixProfileStrategy.cpp6
-rw-r--r--logic/minecraft/onesix/OneSixVersionFormat.cpp194
-rw-r--r--logic/minecraft/onesix/OneSixVersionFormat.h24
3 files changed, 205 insertions, 19 deletions
diff --git a/logic/minecraft/onesix/OneSixProfileStrategy.cpp b/logic/minecraft/onesix/OneSixProfileStrategy.cpp
index 7dca3619..fe4e0d26 100644
--- a/logic/minecraft/onesix/OneSixProfileStrategy.cpp
+++ b/logic/minecraft/onesix/OneSixProfileStrategy.cpp
@@ -56,7 +56,7 @@ void OneSixProfileStrategy::upgradeDeprecatedFiles()
file->fileId = "net.minecraft";
file->version = file->id;
file->name = "Minecraft";
- auto data = OneSixVersionFormat::toJson(file, false).toJson();
+ auto data = OneSixVersionFormat::profilePatchToJson(file, false).toJson();
QSaveFile newPatchFile(mcJson);
if(!newPatchFile.open(QIODevice::WriteOnly))
{
@@ -301,7 +301,7 @@ bool OneSixProfileStrategy::customizePatch(ProfilePatchPtr patch)
{
return false;
}
- auto document = OneSixVersionFormat::toJson(patch, true);
+ auto document = OneSixVersionFormat::profilePatchToJson(patch, true);
jsonFile.write(document.toJson());
if(!jsonFile.commit())
{
@@ -404,7 +404,7 @@ bool OneSixProfileStrategy::installJarMods(QStringList filepaths)
<< "for reading:" << file.errorString();
return false;
}
- file.write(OneSixVersionFormat::toJson(f, true).toJson());
+ file.write(OneSixVersionFormat::profilePatchToJson(f, true).toJson());
file.close();
profile->appendPatch(f);
}
diff --git a/logic/minecraft/onesix/OneSixVersionFormat.cpp b/logic/minecraft/onesix/OneSixVersionFormat.cpp
index defbd69a..dc9cf9c4 100644
--- a/logic/minecraft/onesix/OneSixVersionFormat.cpp
+++ b/logic/minecraft/onesix/OneSixVersionFormat.cpp
@@ -2,6 +2,8 @@
#include <minecraft/NullProfileStrategy.h>
#include <Json.h>
#include "minecraft/ParseUtils.h"
+#include <minecraft/MinecraftVersion.h>
+#include <minecraft/VersionBuildError.h>
using namespace Json;
@@ -22,7 +24,103 @@ static QString readStringRet(const QJsonObject &root, const QString &key)
return QString();
}
-VersionFilePtr OneSixVersionFormat::fromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder)
+RawLibraryPtr OneSixVersionFormat::libraryFromJson(const QJsonObject &libObj, const QString &filename)
+{
+ RawLibraryPtr out(new RawLibrary());
+ if (!libObj.contains("name"))
+ {
+ throw JSONValidationError(filename +
+ "contains a library that doesn't have a 'name' field");
+ }
+ out->m_name = libObj.value("name").toString();
+
+ readString(libObj, "url", out->m_base_url);
+ readString(libObj, "MMC-hint", out->m_hint);
+ readString(libObj, "MMC-absulute_url", out->m_absolute_url);
+ readString(libObj, "MMC-absoluteUrl", out->m_absolute_url);
+ if (libObj.contains("extract"))
+ {
+ out->applyExcludes = true;
+ auto extractObj = requireObject(libObj.value("extract"));
+ for (auto excludeVal : requireArray(extractObj.value("exclude")))
+ {
+ out->extract_excludes.append(requireString(excludeVal));
+ }
+ }
+ if (libObj.contains("natives"))
+ {
+ QJsonObject nativesObj = requireObject(libObj.value("natives"));
+ for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it)
+ {
+ if (!it.value().isString())
+ {
+ qWarning() << filename << "contains an invalid native (skipping)";
+ }
+ OpSys opSys = OpSys_fromString(it.key());
+ if (opSys != Os_Other)
+ {
+ out->m_native_classifiers[opSys] = it.value().toString();
+ }
+ }
+ }
+ if (libObj.contains("rules"))
+ {
+ out->applyRules = true;
+ out->m_rules = rulesFromJsonV4(libObj);
+ }
+ return out;
+}
+
+QJsonObject OneSixVersionFormat::libraryToJson(RawLibrary *library)
+{
+ QJsonObject libRoot;
+ libRoot.insert("name", (QString)library->m_name);
+ if (library->m_absolute_url.size())
+ libRoot.insert("MMC-absoluteUrl", library->m_absolute_url);
+ if (library->m_hint.size())
+ libRoot.insert("MMC-hint", library->m_hint);
+ if (library->m_base_url != "http://" + URLConstants::AWS_DOWNLOAD_LIBRARIES &&
+ library->m_base_url != "https://" + URLConstants::AWS_DOWNLOAD_LIBRARIES &&
+ library->m_base_url != "https://" + URLConstants::LIBRARY_BASE && !library->m_base_url.isEmpty())
+ {
+ libRoot.insert("url", library->m_base_url);
+ }
+ if (library->isNative())
+ {
+ QJsonObject nativeList;
+ auto iter = library->m_native_classifiers.begin();
+ while (iter != library->m_native_classifiers.end())
+ {
+ nativeList.insert(OpSys_toString(iter.key()), iter.value());
+ iter++;
+ }
+ libRoot.insert("natives", nativeList);
+ if (library->extract_excludes.size())
+ {
+ QJsonArray excludes;
+ QJsonObject extract;
+ for (auto exclude : library->extract_excludes)
+ {
+ excludes.append(exclude);
+ }
+ extract.insert("exclude", excludes);
+ libRoot.insert("extract", extract);
+ }
+ }
+ if (library->m_rules.size())
+ {
+ QJsonArray allRules;
+ for (auto &rule : library->m_rules)
+ {
+ QJsonObject ruleObj = rule->toJson();
+ allRules.append(ruleObj);
+ }
+ libRoot.insert("rules", allRules);
+ }
+ return libRoot;
+}
+
+VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder)
{
VersionFilePtr out(new VersionFile());
if (doc.isEmpty() || doc.isNull())
@@ -102,7 +200,7 @@ VersionFilePtr OneSixVersionFormat::fromJson(const QJsonDocument &doc, const QSt
{
auto libObj = requireObject(libVal);
- auto lib = RawLibrary::fromJson(libObj, filename);
+ auto lib = libraryFromJson(libObj, filename);
out->overwriteLibs.append(lib);
}
}
@@ -113,7 +211,7 @@ VersionFilePtr OneSixVersionFormat::fromJson(const QJsonDocument &doc, const QSt
{
QJsonObject libObj = requireObject(libVal);
// parse the jarmod
- auto lib = Jarmod::fromJson(libObj, filename, out->name);
+ auto lib = OneSixVersionFormat::jarModFromJson(libObj, filename, out->name);
if(lib->originalName.isEmpty())
{
auto fixed = out->name;
@@ -131,7 +229,7 @@ VersionFilePtr OneSixVersionFormat::fromJson(const QJsonDocument &doc, const QSt
{
QJsonObject libObj = requireObject(libVal);
// parse the library
- auto lib = RawLibrary::fromJson(libObj, filename);
+ auto lib = libraryFromJson(libObj, filename);
out->addLibs.append(lib);
}
}
@@ -152,6 +250,15 @@ VersionFilePtr OneSixVersionFormat::fromJson(const QJsonDocument &doc, const QSt
return out;
}
+template <typename T>
+struct libraryConversion
+{
+ static QJsonObject convert(std::shared_ptr<RawLibrary> &value)
+ {
+ return OneSixVersionFormat::libraryToJson(value.get());
+ }
+};
+
static QJsonDocument versionFileToJson(VersionFilePtr patch, bool saveOrder)
{
QJsonObject root;
@@ -179,9 +286,33 @@ static QJsonDocument versionFileToJson(VersionFilePtr patch, bool saveOrder)
writeStringList(root, "tweakers", patch->overwriteTweakers);
writeStringList(root, "+tweakers", patch->addTweakers);
writeStringList(root, "+traits", patch->traits.toList());
- writeObjectList(root, "libraries", patch->overwriteLibs);
- writeObjectList(root, "+libraries", patch->addLibs);
- writeObjectList(root, "+jarMods", patch->jarMods);
+ if (!patch->overwriteLibs.isEmpty())
+ {
+ QJsonArray array;
+ for (auto value: patch->overwriteLibs)
+ {
+ array.append(OneSixVersionFormat::libraryToJson(value.get()));
+ }
+ root.insert("libraries", array);
+ }
+ if (!patch->addLibs.isEmpty())
+ {
+ QJsonArray array;
+ for (auto value: patch->addLibs)
+ {
+ array.append(OneSixVersionFormat::libraryToJson(value.get()));
+ }
+ root.insert("+libraries", array);
+ }
+ if (!patch->jarMods.isEmpty())
+ {
+ QJsonArray array;
+ for (auto value: patch->jarMods)
+ {
+ array.append(OneSixVersionFormat::jarModtoJson(value.get()));
+ }
+ root.insert("+jarMods", array);
+ }
// write the contents to a json document.
{
QJsonDocument out;
@@ -190,23 +321,40 @@ static QJsonDocument versionFileToJson(VersionFilePtr patch, bool saveOrder)
}
}
-QJsonDocument OneSixVersionFormat::toJson(const ProfilePatchPtr &patch, bool saveOrder)
+static QJsonDocument minecraftVersionToJson(MinecraftVersionPtr patch, bool saveOrder)
+{
+ if(patch->m_versionSource == Local && patch->getVersionFile())
+ {
+ return OneSixVersionFormat::profilePatchToJson(patch->getVersionFile(), saveOrder);
+ }
+ else
+ {
+ throw VersionIncomplete(QObject::tr("Can't write incomplete/builtin Minecraft version %1").arg(patch->name()));
+ }
+}
+
+QJsonDocument OneSixVersionFormat::profilePatchToJson(const ProfilePatchPtr &patch, bool saveOrder)
{
auto vfile = std::dynamic_pointer_cast<VersionFile>(patch);
if(vfile)
{
return versionFileToJson(vfile, saveOrder);
}
- return QJsonDocument();
+ auto mversion = std::dynamic_pointer_cast<MinecraftVersion>(patch);
+ if(mversion)
+ {
+ return minecraftVersionToJson(mversion, saveOrder);
+ }
+ throw VersionIncomplete(QObject::tr("Unhandled object type while processing %1").arg(patch->getPatchName()));
}
-std::shared_ptr<MinecraftProfile> OneSixVersionFormat::readProfileFromSingleFile(const QJsonObject &obj)
+std::shared_ptr<MinecraftProfile> OneSixVersionFormat::profileFromSingleJson(const QJsonObject &obj)
{
std::shared_ptr<MinecraftProfile> version(new MinecraftProfile(new NullProfileStrategy()));
try
{
version->clear();
- auto file = fromJson(QJsonDocument(obj), QString(), false);
+ auto file = versionFileFromJson(QJsonDocument(obj), QString(), false);
file->applyTo(version.get());
version->appendPatch(file);
}
@@ -216,3 +364,27 @@ std::shared_ptr<MinecraftProfile> OneSixVersionFormat::readProfileFromSingleFile
}
return version;
}
+
+JarmodPtr OneSixVersionFormat::jarModFromJson(const QJsonObject &libObj, const QString &filename, const QString &originalName)
+{
+ JarmodPtr out(new Jarmod());
+ if (!libObj.contains("name"))
+ {
+ throw JSONValidationError(filename +
+ "contains a jarmod that doesn't have a 'name' field");
+ }
+ out->name = libObj.value("name").toString();
+ out->originalName = libObj.value("originalName").toString();
+ return out;
+}
+
+QJsonObject OneSixVersionFormat::jarModtoJson(Jarmod *jarmod)
+{
+ QJsonObject out;
+ writeString(out, "name", jarmod->name);
+ if(!jarmod->originalName.isEmpty())
+ {
+ writeString(out, "originalName", jarmod->originalName);
+ }
+ return out;
+}
diff --git a/logic/minecraft/onesix/OneSixVersionFormat.h b/logic/minecraft/onesix/OneSixVersionFormat.h
index ab21a6a0..9d16cced 100644
--- a/logic/minecraft/onesix/OneSixVersionFormat.h
+++ b/logic/minecraft/onesix/OneSixVersionFormat.h
@@ -2,10 +2,24 @@
#include <minecraft/VersionFile.h>
#include <minecraft/MinecraftProfile.h>
+#include <minecraft/RawLibrary.h>
#include <QJsonDocument>
-namespace OneSixVersionFormat {
- std::shared_ptr<MinecraftProfile> readProfileFromSingleFile(const QJsonObject &obj);
- VersionFilePtr fromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder);
- QJsonDocument toJson(const ProfilePatchPtr &patch, bool saveOrder);
-}
+class OneSixVersionFormat
+{
+public:
+ // whole profiles from single file
+ static std::shared_ptr<MinecraftProfile> profileFromSingleJson(const QJsonObject &obj);
+
+ // version files / profile patches
+ static VersionFilePtr versionFileFromJson(const QJsonDocument &doc, const QString &filename, const bool requireOrder);
+ static QJsonDocument profilePatchToJson(const ProfilePatchPtr &patch, bool saveOrder);
+
+ // libraries
+ static RawLibraryPtr libraryFromJson(const QJsonObject &libObj, const QString &filename);
+ static QJsonObject libraryToJson(RawLibrary *library);
+
+ // jar mods
+ static JarmodPtr jarModFromJson(const QJsonObject &libObj, const QString &filename, const QString &originalName);
+ static QJsonObject jarModtoJson(Jarmod * jarmod);
+};